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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.service.workbench.WbAdjustmentSettingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
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/adjustment-setting")
@Validated
public class WbAdjustmentSettingController {

    @Resource
    private WbAdjustmentSettingService wbAdjustmentSettingService;

    @GetMapping("/combined-list")
    @Operation(summary = "获取调整设置与明细的组合列表")
    @Parameter(name = "divisionId", description = "定额节点ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-adjustment-setting:query')")
    public CommonResult<List<Map<String, Object>>> getCombinedList(@RequestParam("divisionId") Long divisionId) {
        return success(wbAdjustmentSettingService.getAdjustmentCombinedList(divisionId));
    }

    @PostMapping("/apply-adjustment")
    @Operation(summary = "应用调整设置")
    @PreAuthorize("@ss.hasPermission('core:wb-adjustment-setting:update')")
    public CommonResult<Boolean> applyAdjustment(
            @RequestParam("divisionId") Long divisionId,
            @RequestParam("adjustmentSettingId") Long adjustmentSettingId,
            @RequestParam("enabled") boolean enabled) {
        wbAdjustmentSettingService.applyAdjustmentSetting(divisionId, adjustmentSettingId, enabled);
        return success(true);
    }

    @PostMapping("/apply-dynamic-adjustment")
    @Operation(summary = "应用动态调整")
    @PreAuthorize("@ss.hasPermission('core:wb-adjustment-setting:update')")
    public CommonResult<Boolean> applyDynamicAdjustment(@RequestBody WbDynamicAdjustReqVO reqVO) {
        wbAdjustmentSettingService.applyDynamicAdjustment(
                reqVO.getDivisionId(), 
                reqVO.getAdjustmentSettingId(), 
                reqVO.getInputValues());
        return success(true);
    }

    @PostMapping("/apply-dynamic-merge")
    @Operation(summary = "应用动态合并定额")
    @PreAuthorize("@ss.hasPermission('core:wb-adjustment-setting:update')")
    public CommonResult<Boolean> applyDynamicMerge(@RequestBody WbDynamicAdjustReqVO reqVO) {
        wbAdjustmentSettingService.applyDynamicMerge(
                reqVO.getDivisionId(), 
                reqVO.getAdjustmentSettingId(), 
                reqVO.getInputValues());
        return success(true);
    }

    @PostMapping("/update")
    @Operation(summary = "更新调整设置")
    @PreAuthorize("@ss.hasPermission('core:wb-adjustment-setting:update')")
    public CommonResult<Boolean> updateAdjustmentSetting(@RequestBody WbUpdateAdjustmentSettingReqVO reqVO) {
        wbAdjustmentSettingService.updateAdjustmentSetting(reqVO.getId(), reqVO.getAdjustmentContent());
        return success(true);
    }

    /**
     * 动态调整请求VO
     */
    @lombok.Data
    public static class WbDynamicAdjustReqVO {
        private Long divisionId;
        private Long adjustmentSettingId;
        private Map<String, BigDecimal> inputValues;
    }

    /**
     * 更新调整设置请求VO
     */
    @lombok.Data
    public static class WbUpdateAdjustmentSettingReqVO {
        private Long id;
        private String adjustmentContent;
    }
}
