package com.yhy.module.core.controller.admin.infoprice;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceBookPageReqVO;
import com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceBookRespVO;
import com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceBookSaveReqVO;
import com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceResourceRespVO;
import com.yhy.module.core.service.infoprice.InfoPriceBookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 信息价册")
@RestController
@RequestMapping("/core/info-price/book")
@Validated
public class InfoPriceBookController {

    @Resource
    private InfoPriceBookService infoPriceBookService;

    @PostMapping("/create")
    @Operation(summary = "创建信息价册")
    @PreAuthorize("@ss.hasPermission('core:info-price:create')")
    public CommonResult<Long> createInfoPriceBook(@Valid @RequestBody InfoPriceBookSaveReqVO createReqVO) {
        return success(infoPriceBookService.createInfoPriceBook(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新信息价册")
    @PreAuthorize("@ss.hasPermission('core:info-price:update')")
    public CommonResult<Boolean> updateInfoPriceBook(@Valid @RequestBody InfoPriceBookSaveReqVO updateReqVO) {
        infoPriceBookService.updateInfoPriceBook(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除信息价册")
    @Parameter(name = "id", description = "信息价册ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:info-price:delete')")
    public CommonResult<Boolean> deleteInfoPriceBook(@RequestParam("id") Long id) {
        infoPriceBookService.deleteInfoPriceBook(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取信息价册")
    @Parameter(name = "id", description = "信息价册ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:info-price:query')")
    public CommonResult<InfoPriceBookRespVO> getInfoPriceBook(@RequestParam("id") Long id) {
        return success(infoPriceBookService.getInfoPriceBook(id));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询信息价册")
    @PreAuthorize("@ss.hasPermission('core:info-price:query')")
    public CommonResult<PageResult<InfoPriceBookRespVO>> getInfoPriceBookPage(@Valid InfoPriceBookPageReqVO pageReqVO) {
        return success(infoPriceBookService.getInfoPriceBookPage(pageReqVO));
    }

    @GetMapping("/copyPage")
    @Operation(summary = "分页查询信息价册-复制按钮（排除租户1）")
    @PreAuthorize("@ss.hasPermission('core:info-price:query')")
    public CommonResult<PageResult<InfoPriceBookRespVO>> getInfoPriceBookCopyPage(@Valid InfoPriceBookPageReqVO pageReqVO) {
        // 忽略租户过滤，查询所有租户数据，然后在 Service 层排除租户1
        PageResult<InfoPriceBookRespVO> result = TenantUtils.executeIgnore(() ->
                infoPriceBookService.getInfoPriceBookPageExcludeTenant(pageReqVO, 1L)
        );
        return success(result);
    }
    @PostMapping("/create/copy")
    @Operation(summary = "创建信息价册-复制")
    @PreAuthorize("@ss.hasPermission('core:info-price:create')")
    public CommonResult<Long> createInfoPriceBookCopy(@RequestParam("id") Long id) {
        return success(infoPriceBookService.copyInfoPriceBook(id));
    }

    @GetMapping("/list")
    @Operation(summary = "根据树节点ID查询全部信息价册")
    @Parameter(name = "treeNodeId", description = "树节点ID", required = true)
    @Parameter(name = "excludeBookId", description = "排除的信息价册ID", required = false)
    @PreAuthorize("@ss.hasPermission('core:info-price:query')")
    public CommonResult<java.util.List<InfoPriceBookRespVO>> getInfoPriceBookAll(InfoPriceBookPageReqVO pageReqVO) {
        java.util.List<InfoPriceBookRespVO> result = TenantUtils.execute(1L,() ->
                infoPriceBookService.getInfoPriceBookAll(pageReqVO)
        );
        return success(result);
    }

    @GetMapping("/wb-list")
    @Operation(summary = "根据树节点ID查询信息价册列表（工作台专用，不过滤发布状态）")
    @Parameter(name = "treeNodeId", description = "树节点ID", required = true)
    @PreAuthorize("@ss.hasPermission('core:info-price:query')")
    public CommonResult<java.util.List<InfoPriceBookRespVO>> getInfoPriceBookListForWorkbench(@RequestParam("treeNodeId") Long treeNodeId) {
        java.util.List<InfoPriceBookRespVO> result = TenantUtils.execute(1L, () ->
                infoPriceBookService.getInfoPriceBookListForWorkbench(treeNodeId)
        );
        return success(result);
    }
}
