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.WbBoqResourceBatchUpdateResultVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbBoqResourceCategorySummaryVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbBoqResourceCreateBlankReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbBoqResourceFillReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbBoqResourceRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbBoqResourceSaveReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.WbResourceSearchRespVO;
import com.yhy.module.core.service.workbench.WbBoqResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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/boq-resource")
@Validated
public class WbBoqResourceController {

    @Resource
    private WbBoqResourceService wbBoqResourceService;

    @PostMapping("/create")
    @Operation(summary = "创建工料机消耗")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-resource:create')")
    public CommonResult<Long> create(@Valid @RequestBody WbBoqResourceSaveReqVO createReqVO) {
        return success(wbBoqResourceService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工料机消耗")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-resource:update')")
    public CommonResult<WbBoqResourceBatchUpdateResultVO> update(@Valid @RequestBody WbBoqResourceSaveReqVO updateReqVO) {
        WbBoqResourceBatchUpdateResultVO result = wbBoqResourceService.update(updateReqVO);
        return success(result);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工料机消耗")
    @Parameter(name = "id", description = "ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-resource:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        wbBoqResourceService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取工料机消耗详情")
    @Parameter(name = "id", description = "ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-resource:query')")
    public CommonResult<WbBoqResourceRespVO> get(@RequestParam("id") Long id) {
        return success(wbBoqResourceService.get(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获取定额节点下的工料机列表")
    @Parameter(name = "divisionId", description = "定额节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-resource:query')")
    public CommonResult<List<WbBoqResourceRespVO>> getList(@RequestParam("divisionId") Long divisionId) {
        return success(wbBoqResourceService.getListByDivisionId(divisionId));
    }

    @GetMapping("/list-by-boq")
    @Operation(summary = "获取清单节点下所有定额的工料机汇总")
    @Parameter(name = "boqDivisionId", description = "清单节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-resource:query')")
    public CommonResult<List<WbBoqResourceRespVO>> getListByBoq(@RequestParam("boqDivisionId") Long boqDivisionId) {
        return success(wbBoqResourceService.getListByBoqDivisionId(boqDivisionId));
    }

    @GetMapping("/category-summary")
    @Operation(summary = "获取定额节点下工料机的分类汇总")
    @Parameter(name = "divisionId", description = "定额节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-resource:query')")
    public CommonResult<List<WbBoqResourceCategorySummaryVO>> getCategorySummary(@RequestParam("divisionId") Long divisionId) {
        return success(wbBoqResourceService.getCategorySummary(divisionId));
    }

    @GetMapping("/search-by-code")
    @Operation(summary = "编码弹窗查询工料机（本项目+非项目）")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-resource:query')")
    public CommonResult<List<WbResourceSearchRespVO>> searchByCode(
            @RequestParam("divisionId") Long divisionId,
            @RequestParam("code") String code) {
        return success(wbBoqResourceService.searchByCode(divisionId, code));
    }

    @PostMapping("/create-blank")
    @Operation(summary = "创建空白工料机行")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-resource:create')")
    public CommonResult<Long> createBlank(@Valid @RequestBody WbBoqResourceCreateBlankReqVO createBlankReqVO) {
        return success(wbBoqResourceService.createBlank(createBlankReqVO));
    }

    @PutMapping("/fill-from-search")
    @Operation(summary = "双击填充工料机数据到空白行")
    @PreAuthorize("@ss.hasPermission('core:wb-boq-resource:update')")
    public CommonResult<Boolean> fillFromSearch(@Valid @RequestBody WbBoqResourceFillReqVO fillReqVO) {
        wbBoqResourceService.fillFromSearch(fillReqVO);
        return success(true);
    }
}
