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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.workbench.vo.WbResourceSourceBoqRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbResourceSourceUnitRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbResourceSummaryRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbResourceSummaryTreeRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbResourceSummaryUpdateReqVO;
import com.yhy.module.core.service.workbench.WbResourceSummaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 工料机汇总 Controller
 *
 * @author yhy
 */
@Tag(name = "工作台 - 工料机汇总")
@RestController
@RequestMapping("/core/wb/resource-summary")
@Validated
public class WbResourceSummaryController {

    @Resource
    private WbResourceSummaryService wbResourceSummaryService;

    @GetMapping("/tree")
    @Operation(summary = "获取工料机汇总树")
    @Parameters({
            @Parameter(name = "projectId", description = "项目ID", required = true, example = "1"),
            @Parameter(name = "compileTreeId", description = "编制模式树的单位工程节点ID（可选，传了只汇总该单位工程）", example = "1")
    })
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:query')")
    public CommonResult<List<WbResourceSummaryTreeRespVO>> getSummaryTree(
            @RequestParam("projectId") Long projectId,
            @RequestParam(value = "compileTreeId", required = false) Long compileTreeId) {
        return success(wbResourceSummaryService.getSummaryTree(projectId, compileTreeId));
    }

    @GetMapping("/list")
    @Operation(summary = "获取工料机汇总列表（按类别）")
    @Parameters({
            @Parameter(name = "projectId", description = "项目ID", required = true, example = "1"),
            @Parameter(name = "category", description = "类别：labor/material/machine/bid_material", required = true, example = "material"),
            @Parameter(name = "compileTreeId", description = "编制模式树的单位工程节点ID（可选，传了只汇总该单位工程）", example = "1")
    })
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:query')")
    public CommonResult<List<WbResourceSummaryRespVO>> getSummaryList(
            @RequestParam("projectId") Long projectId,
            @RequestParam("category") String category,
            @RequestParam(value = "compileTreeId", required = false) Long compileTreeId) {
        return success(wbResourceSummaryService.getSummaryList(projectId, category, compileTreeId));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工料机汇总（打印、评标指定材料、编码等）")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:update')")
    public CommonResult<Boolean> updateSummary(@Valid @RequestBody WbResourceSummaryUpdateReqVO updateReqVO) {
        wbResourceSummaryService.updateSummary(updateReqVO);
        return success(true);
    }

    @GetMapping("/source-units")
    @Operation(summary = "获取工料机来源-单位工程列表")
    @Parameters({
            @Parameter(name = "projectId", description = "项目ID", required = true, example = "1"),
            @Parameter(name = "resourceKey", description = "资源唯一键", required = true, example = "abc123")
    })
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:query')")
    public CommonResult<List<WbResourceSourceUnitRespVO>> getSourceUnits(
            @RequestParam("projectId") Long projectId,
            @RequestParam("resourceKey") String resourceKey) {
        return success(wbResourceSummaryService.getSourceUnits(projectId, resourceKey));
    }

    @GetMapping("/source-boqs")
    @Operation(summary = "获取工料机来源-清单列表")
    @Parameters({
            @Parameter(name = "compileTreeId", description = "编制模式树ID（单位工程节点）", required = true, example = "1"),
            @Parameter(name = "resourceKey", description = "资源唯一键", required = true, example = "abc123")
    })
    @PreAuthorize("@ss.hasPermission('core:wb-boq-division:query')")
    public CommonResult<List<WbResourceSourceBoqRespVO>> getSourceBoqs(
            @RequestParam("compileTreeId") Long compileTreeId,
            @RequestParam("resourceKey") String resourceKey) {
        return success(wbResourceSummaryService.getSourceBoqs(compileTreeId, resourceKey));
    }
}
