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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.yhy.module.core.controller.admin.quota.vo.QuotaVariableSettingRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaVariableSettingSaveReqVO;
import com.yhy.module.core.dal.dataobject.quota.QuotaVariableSettingDO;
import com.yhy.module.core.service.quota.QuotaVariableSettingService;
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.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;

@Tag(name = "管理后台 - 单位工程变量设置")
@RestController
@RequestMapping("/core/quota/variable-setting")
@Validated
public class QuotaVariableSettingController {

    @Resource
    private QuotaVariableSettingService quotaVariableSettingService;

    @PostMapping("/create")
    @Operation(summary = "创建变量设置")
    @PreAuthorize("@ss.hasPermission('core:quota:variable-setting:create')")
    public CommonResult<Long> createVariableSetting(@Valid @RequestBody QuotaVariableSettingSaveReqVO createReqVO) {
        return success(quotaVariableSettingService.createVariableSetting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新变量设置")
    @PreAuthorize("@ss.hasPermission('core:quota:variable-setting:update')")
    public CommonResult<Boolean> updateVariableSetting(@Valid @RequestBody QuotaVariableSettingSaveReqVO updateReqVO) {
        quotaVariableSettingService.updateVariableSetting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除变量设置")
    @Parameter(name = "id", description = "变量设置ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:variable-setting:delete')")
    public CommonResult<Boolean> deleteVariableSetting(@RequestParam("id") Long id) {
        quotaVariableSettingService.deleteVariableSetting(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取变量设置详情")
    @Parameter(name = "id", description = "变量设置ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:quota:variable-setting:query')")
    public CommonResult<QuotaVariableSettingRespVO> getVariableSetting(@RequestParam("id") Long id) {
        QuotaVariableSettingDO variableSetting = quotaVariableSettingService.getVariableSetting(id);
        return success(BeanUtils.toBean(variableSetting, QuotaVariableSettingRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获取变量设置列表")
    @Parameters({
        @Parameter(name = "catalogItemId", description = "费率模式节点ID", required = true, example = "1002"),
        @Parameter(name = "category", description = "类别：division/measure/other/unit_summary", required = true, example = "division")
    })
    @PreAuthorize("@ss.hasPermission('core:quota:variable-setting:query')")
    public CommonResult<List<QuotaVariableSettingRespVO>> getVariableSettingList(
            @RequestParam("catalogItemId") Long catalogItemId,
            @RequestParam("category") String category) {
        List<QuotaVariableSettingRespVO> list = quotaVariableSettingService.getVariableSettingListWithFeeItems(catalogItemId, category);
        return success(list);
    }

    @GetMapping("/list-all")
    @Operation(summary = "获取所有类别的变量设置列表")
    @Parameter(name = "catalogItemId", description = "费率模式节点ID", required = true, example = "1002")
    @PreAuthorize("@ss.hasPermission('core:quota:variable-setting:query')")
    public CommonResult<List<QuotaVariableSettingRespVO>> getVariableSettingListAll(
            @RequestParam("catalogItemId") Long catalogItemId) {
        List<QuotaVariableSettingRespVO> list = quotaVariableSettingService.getVariableSettingListAllWithFeeItems(catalogItemId);
        return success(list);
    }

    @GetMapping("/list-all-with-summary")
    @Operation(summary = "获取所有类别的变量设置列表（包含汇总值计算）")
    @Parameters({
        @Parameter(name = "catalogItemId", description = "定额专业节点ID", required = true, example = "1002"),
        @Parameter(name = "compileTreeId", description = "编制模式树的单位工程节点ID", required = true, example = "1001"),
        @Parameter(name = "baseNumberRangeIds", description = "基数范围选中的节点ID列表（逗号分隔）", required = false, example = "1,2,3")
    })
    @PreAuthorize("@ss.hasPermission('core:quota:variable-setting:query')")
    public CommonResult<List<QuotaVariableSettingRespVO>> getVariableSettingListAllWithSummary(
            @RequestParam("catalogItemId") Long catalogItemId,
            @RequestParam("compileTreeId") Long compileTreeId,
            @RequestParam(value = "baseNumberRangeIds", required = false) List<Long> baseNumberRangeIds) {
        List<QuotaVariableSettingRespVO> list = quotaVariableSettingService.getVariableSettingListAllWithSummary(
            catalogItemId, compileTreeId, baseNumberRangeIds);
        return success(list);
    }

    @GetMapping("/list-by-compile-tree")
    @Operation(summary = "根据编制树ID获取所有变量设置（自动合并所有定额专业）")
    @Parameters({
        @Parameter(name = "compileTreeId", description = "编制模式树的单位工程节点ID", required = true, example = "1001"),
        @Parameter(name = "baseNumberRangeIds", description = "基数范围选中的节点ID列表（逗号分隔）", required = false, example = "1,2,3")
    })
    @PreAuthorize("@ss.hasPermission('core:quota:variable-setting:query')")
    public CommonResult<List<QuotaVariableSettingRespVO>> getVariableSettingListByCompileTree(
            @RequestParam("compileTreeId") Long compileTreeId,
            @RequestParam(value = "baseNumberRangeIds", required = false) List<Long> baseNumberRangeIds) {
        List<QuotaVariableSettingRespVO> list = quotaVariableSettingService.getVariableSettingListByCompileTree(
            compileTreeId, baseNumberRangeIds);
        return success(list);
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "交换排序")
    @Parameters({
        @Parameter(name = "nodeId1", description = "节点1 ID", required = true, example = "1"),
        @Parameter(name = "nodeId2", description = "节点2 ID", required = true, example = "2")
    })
    @PreAuthorize("@ss.hasPermission('core:quota:variable-setting:update')")
    public CommonResult<Boolean> swapSort(
            @RequestParam("nodeId1") Long nodeId1,
            @RequestParam("nodeId2") Long nodeId2) {
        quotaVariableSettingService.swapSort(nodeId1, nodeId2);
        return success(true);
    }
}
