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.progresspayment.ProgressDivisionBatchSaveReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.progresspayment.ProgressDivisionSaveReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.progresspayment.ProgressDivisionTreeRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.progresspayment.ProgressPaymentModeWithDivisionsRespVO;
import com.yhy.module.core.dal.dataobject.workbench.ProgressDivisionDO;
import com.yhy.module.core.service.workbench.ProgressDivisionService;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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/progress-division")
@Validated
public class ProgressDivisionController {

    @Resource
    private ProgressDivisionService progressDivisionService;

    @PostMapping("/save")
    @Operation(summary = "保存或更新期数值")
    @PreAuthorize("@ss.hasPermission('core:wb-progress-payment:update')")
    public CommonResult<Long> save(@Valid @RequestBody ProgressDivisionSaveReqVO saveReqVO) {
        return success(progressDivisionService.saveOrUpdate(
                saveReqVO.getProgressPaymentModeId(),
                saveReqVO.getBoqDivisionId(),
                saveReqVO.getPeriodValue()));
    }

    @PostMapping("/batch-save")
    @Operation(summary = "批量保存或更新期数值")
    @PreAuthorize("@ss.hasPermission('core:wb-progress-payment:update')")
    public CommonResult<Boolean> batchSave(@Valid @RequestBody ProgressDivisionBatchSaveReqVO batchSaveReqVO) {
        progressDivisionService.batchSaveOrUpdate(
                batchSaveReqVO.getProgressPaymentModeId(),
                batchSaveReqVO.getPeriodValueMap());
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "根据进度款模式ID获取关联列表")
    @Parameter(name = "progressPaymentModeId", description = "进度款模式ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-progress-payment:query')")
    public CommonResult<List<ProgressDivisionDO>> getList(@RequestParam("progressPaymentModeId") Long progressPaymentModeId) {
        return success(progressDivisionService.getListByProgressPaymentModeId(progressPaymentModeId));
    }

    @GetMapping("/map")
    @Operation(summary = "根据进度款模式ID获取清单ID到期数值的映射")
    @Parameter(name = "progressPaymentModeId", description = "进度款模式ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-progress-payment:query')")
    public CommonResult<java.util.Map<Long, java.math.BigDecimal>> getMap(@RequestParam("progressPaymentModeId") Long progressPaymentModeId) {
        return success(progressDivisionService.getPeriodValueMap(progressPaymentModeId));
    }

    @PostMapping("/delete-by-payment-mode")
    @Operation(summary = "根据进度款模式ID删除所有关联")
    @Parameter(name = "progressPaymentModeId", description = "进度款模式ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-progress-payment:delete')")
    public CommonResult<Boolean> deleteByPaymentMode(@RequestParam("progressPaymentModeId") Long progressPaymentModeId) {
        progressDivisionService.deleteByProgressPaymentModeId(progressPaymentModeId);
        return success(true);
    }

    @GetMapping("/tree-with-period-info")
    @Operation(summary = "获取分部分项树（含定额单价计算，并绑定进度款期数信息）")
    @Parameter(name = "compileTreeId", description = "编制模式树的单位工程节点ID", required = true, example = "1")
    @Parameter(name = "progressPaymentModeId", description = "进度款模式ID", example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-progress-payment:query')")
    public CommonResult<List<ProgressDivisionTreeRespVO>> getTreeWithPeriodInfo(
            @RequestParam("compileTreeId") Long compileTreeId,
            @RequestParam(value = "progressPaymentModeId", required = false) Long progressPaymentModeId) {
        return success(progressDivisionService.getTreeWithPeriodInfo(compileTreeId, progressPaymentModeId));
    }

    @GetMapping("/mode-with-divisions-list")
    @Operation(summary = "获取项目下的进度款模式列表（含关联清单）")
    @Parameter(name = "projectId", description = "项目ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-progress-payment:query')")
    public CommonResult<List<ProgressPaymentModeWithDivisionsRespVO>> getModeWithDivisionsList(
            @RequestParam("projectId") Long projectId) {
        return success(progressDivisionService.getProgressPaymentModeWithDivisionsList(projectId));
    }
}
