package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.student.dto.AddQuotaFormulaRuleConstantDto;
import com.xjrsoft.module.student.dto.QuotaFormulaRuleConstantPageDto;
import com.xjrsoft.module.student.dto.UpdateQuotaFormulaRuleConstantDto;
import com.xjrsoft.module.student.entity.QuotaFormulaRuleConstant;
import com.xjrsoft.module.student.service.IQuotaFormulaRuleConstantService;
import com.xjrsoft.module.student.vo.QuotaFormulaRuleConstantPageVo;
import com.xjrsoft.module.student.vo.QuotaFormulaRuleConstantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
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;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 考核基数
* @Author szs
* @Date: 2024-01-29
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/quotaFormulaRuleConstant")
@Api(value = "/student"  + "/quotaFormulaRuleConstant",tags = "考核基数代码")
@AllArgsConstructor
public class QuotaFormulaRuleConstantController {


    private final IQuotaFormulaRuleConstantService quotaFormulaRuleConstantService;

    @GetMapping(value = "/page")
    @ApiOperation(value="考核基数列表(分页)")
    @SaCheckPermission("quotaformularuleconstant:detail")
    public RT<PageOutput<QuotaFormulaRuleConstantPageVo>> page(@Valid QuotaFormulaRuleConstantPageDto dto){
        Page<QuotaFormulaRuleConstantPageVo> page = quotaFormulaRuleConstantService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<QuotaFormulaRuleConstantPageVo> pageOutput = ConventPage.getPageOutput(page, QuotaFormulaRuleConstantPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询考核基数信息")
    @SaCheckPermission("quotaformularuleconstant:detail")
    public RT<QuotaFormulaRuleConstantVo> info(@RequestParam Long id){
        QuotaFormulaRuleConstant quotaFormulaRuleConstant = quotaFormulaRuleConstantService.getById(id);
        if (quotaFormulaRuleConstant == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(quotaFormulaRuleConstant, QuotaFormulaRuleConstantVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增考核基数")
    @SaCheckPermission("quotaformularuleconstant:add")
    public RT<Boolean> add(@Valid @RequestBody AddQuotaFormulaRuleConstantDto dto){
        QuotaFormulaRuleConstant quotaFormulaRuleConstant = BeanUtil.toBean(dto, QuotaFormulaRuleConstant.class);
        boolean isSuccess = quotaFormulaRuleConstantService.save(quotaFormulaRuleConstant);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改考核基数")
    @SaCheckPermission("quotaformularuleconstant:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateQuotaFormulaRuleConstantDto dto){

        QuotaFormulaRuleConstant quotaFormulaRuleConstant = BeanUtil.toBean(dto, QuotaFormulaRuleConstant.class);
        return RT.ok(quotaFormulaRuleConstantService.updateById(quotaFormulaRuleConstant));

    }

    @DeleteMapping
    @ApiOperation(value = "删除考核基数")
    @SaCheckPermission("quotaformularuleconstant:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(quotaFormulaRuleConstantService.removeBatchByIds(ids));

    }

}