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.AddQuotaFormulaRuleDto;
import com.xjrsoft.module.student.dto.QuotaFormulaRulePageDto;
import com.xjrsoft.module.student.dto.UpdateQuotaFormulaRuleDto;
import com.xjrsoft.module.student.entity.QuotaFormulaRule;
import com.xjrsoft.module.student.service.IQuotaFormulaRuleService;
import com.xjrsoft.module.student.vo.QuotaFormulaRulePageVo;
import com.xjrsoft.module.student.vo.QuotaFormulaRuleVo;
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" + "/quotaFormulaRule")
@Api(value = "/student"  + "/quotaFormulaRule",tags = "指标公式规则管理代码")
@AllArgsConstructor
public class QuotaFormulaRuleController {


    private final IQuotaFormulaRuleService quotaFormulaRuleService;

    @GetMapping(value = "/page")
    @ApiOperation(value="指标公式规则管理列表(分页)")
    @SaCheckPermission("quotaformularule:detail")
    public RT<PageOutput<QuotaFormulaRulePageVo>> page(@Valid QuotaFormulaRulePageDto dto){

        Page<QuotaFormulaRulePageVo> page = quotaFormulaRuleService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<QuotaFormulaRulePageVo> pageOutput = ConventPage.getPageOutput(page, QuotaFormulaRulePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询指标公式规则管理信息")
    @SaCheckPermission("quotaformularule:detail")
    public RT<QuotaFormulaRuleVo> info(@RequestParam Long id){
        QuotaFormulaRule quotaFormulaRule = quotaFormulaRuleService.getById(id);
        if (quotaFormulaRule == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(quotaFormulaRule, QuotaFormulaRuleVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增指标公式规则管理")
    @SaCheckPermission("quotaformularule:add")
    public RT<Boolean> add(@Valid @RequestBody AddQuotaFormulaRuleDto dto){
        QuotaFormulaRule quotaFormulaRule = BeanUtil.toBean(dto, QuotaFormulaRule.class);
        boolean isSuccess = quotaFormulaRuleService.save(quotaFormulaRule);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改指标公式规则管理")
    @SaCheckPermission("quotaformularule:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateQuotaFormulaRuleDto dto){

        QuotaFormulaRule quotaFormulaRule = BeanUtil.toBean(dto, QuotaFormulaRule.class);
        return RT.ok(quotaFormulaRuleService.updateById(quotaFormulaRule));

    }

    @DeleteMapping
    @ApiOperation(value = "删除指标公式规则管理")
    @SaCheckPermission("quotaformularule:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(quotaFormulaRuleService.removeBatchByIds(ids));

    }

}