package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.student.dto.AddSchoolRollCategoryDto;
import com.xjrsoft.module.student.dto.SchoolRollCategoryListDto;
import com.xjrsoft.module.student.dto.SchoolRollCategoryPageDto;
import com.xjrsoft.module.student.dto.UpdateSchoolRollCategoryDto;
import com.xjrsoft.module.student.entity.SchoolRollCategory;
import com.xjrsoft.module.student.service.ISchoolRollCategoryService;
import com.xjrsoft.module.student.vo.SchoolRollCategoryPageVo;
import com.xjrsoft.module.student.vo.SchoolRollCategoryVo;
import com.xjrsoft.module.system.entity.DictionaryDetail;
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: 2023-11-29
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/schoolRollCategory")
@Api(value = "/student"  + "/schoolRollCategory",tags = "学籍异动类别代码")
@AllArgsConstructor
public class SchoolRollCategoryController {


    private final ISchoolRollCategoryService schoolRollCategoryService;

    @GetMapping(value = "/list")
    @ApiOperation(value="学籍异动类别列表(不分页)")
    @SaCheckPermission("schoolrollcategory:detail")
    public RT<List<SchoolRollCategoryVo>> list(@Valid SchoolRollCategoryListDto dto){
        MPJLambdaWrapper<SchoolRollCategory> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(SchoolRollCategory::getId)
                .selectAs(DictionaryDetail::getName,SchoolRollCategoryVo::getScholarshipSourceCN)
                .select(SchoolRollCategory.class, x -> VoToColumnUtil.fieldsToColumns(SchoolRollCategoryVo.class).contains(x.getProperty()))
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, SchoolRollCategory::getScholarshipSource)
                .orderByDesc(SchoolRollCategory::getId);
        List<SchoolRollCategoryVo> list = schoolRollCategoryService.selectJoinList(SchoolRollCategoryVo.class, queryWrapper);
        return RT.ok(list);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value="学籍异动类别列表(分页)")
    @SaCheckPermission("schoolrollcategory:detail")
    public RT<PageOutput<SchoolRollCategoryPageVo>> page(@Valid SchoolRollCategoryPageDto dto){

        MPJLambdaWrapper<SchoolRollCategory> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(SchoolRollCategory::getId)
                .selectAs(DictionaryDetail::getName,SchoolRollCategoryPageVo::getScholarshipSourceCN)
                .select(SchoolRollCategory.class, x -> VoToColumnUtil.fieldsToColumns(SchoolRollCategoryPageVo.class).contains(x.getProperty()))
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, SchoolRollCategory::getScholarshipSource)
                .orderByDesc(SchoolRollCategory::getId);
        IPage<SchoolRollCategoryPageVo> page = schoolRollCategoryService.selectJoinListPage(ConventPage.getPage(dto),SchoolRollCategoryPageVo.class, queryWrapper);
        PageOutput<SchoolRollCategoryPageVo> pageOutput = ConventPage.getPageOutput(page, SchoolRollCategoryPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学籍异动类别信息")
    @SaCheckPermission("schoolrollcategory:detail")
    public RT<SchoolRollCategoryVo> info(@RequestParam Long id){
        SchoolRollCategory schoolRollCategory = schoolRollCategoryService.getById(id);
        if (schoolRollCategory == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(schoolRollCategory, SchoolRollCategoryVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学籍异动类别")
    @SaCheckPermission("schoolrollcategory:add")
    public RT<Boolean> add(@Valid @RequestBody AddSchoolRollCategoryDto dto){
        SchoolRollCategory schoolRollCategory = BeanUtil.toBean(dto, SchoolRollCategory.class);
        boolean isSuccess = schoolRollCategoryService.save(schoolRollCategory);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学籍异动类别")
    @SaCheckPermission("schoolrollcategory:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateSchoolRollCategoryDto dto){

        SchoolRollCategory schoolRollCategory = BeanUtil.toBean(dto, SchoolRollCategory.class);
        return RT.ok(schoolRollCategoryService.updateById(schoolRollCategory));

    }

    @DeleteMapping
    @ApiOperation(value = "删除学籍异动类别")
    @SaCheckPermission("schoolrollcategory:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(schoolRollCategoryService.removeBatchByIds(ids));

    }

}