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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.unit.vo.UnitRateSettingRespVO;
import com.yhy.module.core.controller.admin.unit.vo.UnitRateSettingSaveReqVO;
import com.yhy.module.core.service.unit.UnitRateSettingService;
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.validation.Valid;
import lombok.RequiredArgsConstructor;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 工程费率设定 Controller
 */
@Tag(name = "管理后台 - 工程费率设定")
@RestController
@RequestMapping("/core/unit/rate-setting")
@Validated
@RequiredArgsConstructor
public class UnitRateSettingController {

    private final UnitRateSettingService unitRateSettingService;

    @PostMapping("/save")
    @Operation(summary = "保存或更新费率设定", description = "保存设置值变更，后端计算字段值并返回")
    public CommonResult<UnitRateSettingRespVO> saveOrUpdate(@Valid @RequestBody UnitRateSettingSaveReqVO saveReqVO) {
        return success(unitRateSettingService.saveOrUpdate(saveReqVO));
    }

    @GetMapping("/list")
    @Operation(summary = "获取单位工程的费率设定列表")
    @Parameter(name = "unitId", description = "单位工程ID", required = true)
    public CommonResult<List<UnitRateSettingRespVO>> getListByUnitId(@RequestParam("unitId") Long unitId) {
        return success(unitRateSettingService.getListByUnitId(unitId));
    }

    @GetMapping("/get")
    @Operation(summary = "获取单个费率设定")
    @Parameter(name = "unitId", description = "单位工程ID", required = true)
    @Parameter(name = "rateItemId", description = "费率项ID", required = true)
    public CommonResult<UnitRateSettingRespVO> getByUnitIdAndRateItemId(
            @RequestParam("unitId") Long unitId,
            @RequestParam("rateItemId") Long rateItemId) {
        return success(unitRateSettingService.getByUnitIdAndRateItemId(unitId, rateItemId));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除费率设定")
    @Parameter(name = "id", description = "费率设定ID", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        unitRateSettingService.delete(id);
        return success(true);
    }

    @PostMapping("/initialize")
    @Operation(summary = "初始化单位工程的费率设定", description = "创建快照，从定额费率模板复制初始值")
    @Parameter(name = "unitId", description = "单位工程ID", required = true)
    @Parameter(name = "catalogItemId", description = "费率模式节点ID", required = true)
    public CommonResult<Boolean> initializeSettings(
            @RequestParam("unitId") Long unitId,
            @RequestParam("catalogItemId") Long catalogItemId) {
        unitRateSettingService.initializeSettings(unitId, catalogItemId);
        return success(true);
    }
}
