package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.student.dto.AddBaseStudentAssessmentCategoryDto;
import com.xjrsoft.module.student.dto.BaseStudentAssessmentCategoryListDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentAssessmentCategoryDto;
import com.xjrsoft.module.student.entity.BaseStudentAssessmentCategory;
import com.xjrsoft.module.student.service.IBaseStudentAssessmentCategoryService;
import com.xjrsoft.module.student.vo.BaseStudentAssessmentCategoryListVo;
import com.xjrsoft.module.student.vo.BaseStudentAssessmentCategoryVo;
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.Date;
import java.util.List;

/**
 * @title: 学生考核类别
 * @Author fanxp
 * @Date: 2023-11-14
 * @Version 1.0
 */
@RestController
@RequestMapping("/student" + "/basestudentassessmentcategory")
@Api(value = "/student" + "/basestudentassessmentcategory", tags = "学生考核类别代码")
@AllArgsConstructor
public class BaseStudentAssessmentCategoryController {


    private final IBaseStudentAssessmentCategoryService baseStudentAssessmentCategoryService;

    @GetMapping(value = "/list")
    @ApiOperation(value = "学生考核类别列表(不分页)")
    @SaCheckPermission("basestudentassessmentcategory:detail")
    public RT<List<BaseStudentAssessmentCategoryListVo>> list(@Valid BaseStudentAssessmentCategoryListDto dto) {

        LambdaQueryWrapper<BaseStudentAssessmentCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .orderByDesc(BaseStudentAssessmentCategory::getSortCode)
                .select(BaseStudentAssessmentCategory.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentAssessmentCategoryListVo.class).contains(x.getProperty()));

        List<BaseStudentAssessmentCategory> list = baseStudentAssessmentCategoryService.list(queryWrapper);
        list.forEach((node) -> {
            if (node.getTotalScore() > 0) {
                node.setName(String.format("%s(总分：%s)", node.getName(), node.getTotalScore().toString()));
            }
        });
        List<BaseStudentAssessmentCategoryListVo> listVos = BeanUtil.copyToList(list, BaseStudentAssessmentCategoryListVo.class);
        return RT.ok(listVos);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询学生考核类别信息")
    @SaCheckPermission("basestudentassessmentcategory:detail")
    public RT<BaseStudentAssessmentCategoryVo> info(@RequestParam Long id) {
        BaseStudentAssessmentCategory baseStudentAssessmentCategory = baseStudentAssessmentCategoryService.getById(id);
        if (baseStudentAssessmentCategory == null) {
            return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseStudentAssessmentCategory, BaseStudentAssessmentCategoryVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学生考核类别")
    @SaCheckPermission("basestudentassessmentcategory:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentAssessmentCategoryDto dto) {
        BaseStudentAssessmentCategory baseStudentAssessmentCategory = BeanUtil.toBean(dto, BaseStudentAssessmentCategory.class);
        QueryWrapper<BaseStudentAssessmentCategory> queryWrapperSortcode = new QueryWrapper<>();
        queryWrapperSortcode.select("IFNULL(MAX(sort_code),0) as sortCode");
        BaseStudentAssessmentCategory b = baseStudentAssessmentCategoryService.getOne(queryWrapperSortcode);
        baseStudentAssessmentCategory.setSortCode(b.getSortCode() + 1);
        baseStudentAssessmentCategory.setCreateUserId(StpUtil.getLoginIdAsLong());
        baseStudentAssessmentCategory.setCreateDate(new Date());
        boolean isSuccess = baseStudentAssessmentCategoryService.save(baseStudentAssessmentCategory);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生考核类别")
    @SaCheckPermission("basestudentassessmentcategory:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentAssessmentCategoryDto dto) {
        BaseStudentAssessmentCategory baseStudentAssessmentCategory = BeanUtil.toBean(dto, BaseStudentAssessmentCategory.class);
        baseStudentAssessmentCategory.setModifyUserId(StpUtil.getLoginIdAsLong());
        baseStudentAssessmentCategory.setModifyDate(new Date());
        return RT.ok(baseStudentAssessmentCategoryService.updateById(baseStudentAssessmentCategory));
    }

    @DeleteMapping
    @ApiOperation(value = "删除学生考核类别")
    @SaCheckPermission("basestudentassessmentcategory:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids) {
        return RT.ok(baseStudentAssessmentCategoryService.removeBatchByIds(ids));

    }

}