package cn.iocoder.lyzsys.module.tjt.controller.admin.yearkvalue;

import cn.iocoder.lyzsys.framework.common.pojo.CommonResult;
import cn.iocoder.lyzsys.framework.common.pojo.PageResult;
import cn.iocoder.lyzsys.framework.common.util.object.BeanUtils;
import cn.iocoder.lyzsys.module.tjt.controller.admin.yearkvalue.vo.YearKValuePageReqVO;
import cn.iocoder.lyzsys.module.tjt.controller.admin.yearkvalue.vo.YearKValueRespVO;
import cn.iocoder.lyzsys.module.tjt.controller.admin.yearkvalue.vo.YearKValueSaveReqVO;
import cn.iocoder.lyzsys.module.tjt.service.yearkvalue.YearKValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.PathVariable;
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;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Map;

import static cn.iocoder.lyzsys.framework.common.exception.util.ServiceExceptionUtil.invalidParamException;
import static cn.iocoder.lyzsys.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 年度 K 值")
@RestController
@RequestMapping("/tjt/year-k-value")
@Validated
public class YearKValueController {

    @Resource
    private YearKValueService yearKValueService;

    @PostMapping("/create")
    @Operation(summary = "创建年度 K 值")
    @PreAuthorize("@ss.hasPermission('tjt:year-k-value:create')")
    public CommonResult<Long> createYearKValue(@RequestBody Map<String, Object> requestBody) {
        YearKValueSaveReqVO createReqVO = buildSaveReqVO(requestBody, false);
        return success(yearKValueService.createYearKValue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改年度 K 值")
    @PreAuthorize("@ss.hasPermission('tjt:year-k-value:update')")
    public CommonResult<Boolean> updateYearKValue(@RequestBody Map<String, Object> requestBody) {
        YearKValueSaveReqVO updateReqVO = buildSaveReqVO(requestBody, true);
        yearKValueService.updateYearKValue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除年度 K 值")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('tjt:year-k-value:delete')")
    public CommonResult<Boolean> deleteYearKValue(@RequestParam("id") Long id) {
        yearKValueService.deleteYearKValue(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得年度 K 值详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('tjt:year-k-value:query')")
    public CommonResult<YearKValueRespVO> getYearKValue(@RequestParam("id") Long id) {
        return success(yearKValueService.getYearKValue(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得年度 K 值分页")
    @PreAuthorize("@ss.hasPermission('tjt:year-k-value:query')")
    public CommonResult<PageResult<YearKValueRespVO>> getYearKValuePage(@Valid YearKValuePageReqVO pageReqVO) {
        return success(yearKValueService.getYearKValuePage(pageReqVO));
    }

    private YearKValueSaveReqVO buildSaveReqVO(Map<String, Object> requestBody, boolean requireId) {
        YearKValueSaveReqVO reqVO = BeanUtils.toBean(requestBody, YearKValueSaveReqVO.class);
        if (requireId && reqVO.getId() == null) {
            throw invalidParamException("编号不能为空");
        }
        if (reqVO.getKYear() == null) {
            throw invalidParamException("年度不能为空");
        }
        if (reqVO.getKValue() == null) {
            throw invalidParamException("K值不能为空");
        }
        if (reqVO.getKValue().compareTo(BigDecimal.ZERO) < 0
                || reqVO.getKValue().compareTo(BigDecimal.ONE) > 0) {
            throw invalidParamException("K值必须在 0% 到 100% 之间");
        }
        if (reqVO.getRemark() != null && reqVO.getRemark().length() > 255) {
            throw invalidParamException("备注长度不能超过 255 个字符");
        }
        return reqVO;
    }

}
