package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.AddSchoolRollReasonDto;
import com.xjrsoft.module.student.dto.SchoolRollReasonListDto;
import com.xjrsoft.module.student.dto.SchoolRollReasonPageDto;
import com.xjrsoft.module.student.dto.UpdateSchoolRollReasonDto;
import com.xjrsoft.module.student.entity.SchoolRollCategory;
import com.xjrsoft.module.student.entity.SchoolRollReason;
import com.xjrsoft.module.student.service.ISchoolRollReasonService;
import com.xjrsoft.module.student.vo.SchoolRollReasonPageVo;
import com.xjrsoft.module.student.vo.SchoolRollReasonVo;
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" + "/schoolRollReason")
@Api(value = "/student"  + "/schoolRollReason",tags = "学籍异动原因代码")
@AllArgsConstructor
public class SchoolRollReasonController {
    private final ISchoolRollReasonService schoolRollReasonService;

    @GetMapping(value = "/list")
    @ApiOperation(value="学籍异动原因列表(不分页)")
    @SaCheckPermission("schoolrollreason:detail")
    public RT<List<SchoolRollReasonVo>> list(@Valid SchoolRollReasonListDto dto){

        MPJLambdaWrapper<SchoolRollReason> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAs(SchoolRollReason::getId, SchoolRollReasonVo::getId)
                .selectAs(SchoolRollCategory::getName, SchoolRollReasonVo::getSchoolRollCategoryIdCN)
                .select(SchoolRollReason.class, x -> VoToColumnUtil.fieldsToColumns(SchoolRollReasonVo.class).contains(x.getProperty()))
                .leftJoin(SchoolRollCategory.class, SchoolRollCategory::getId, SchoolRollReason::getSchoolRollCategoryId)
                .eq(ObjectUtil.isNotNull(dto) && ObjectUtil.isNotNull(dto.getSchoolRollCategoryId()) && dto.getSchoolRollCategoryId() != "",SchoolRollReason::getSchoolRollCategoryId,dto.getSchoolRollCategoryId())
                .orderByDesc(SchoolRollReason::getId);

        List<SchoolRollReasonVo> list = schoolRollReasonService.selectJoinList(SchoolRollReasonVo.class, queryWrapper);
        return RT.ok(list);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value="学籍异动原因列表(分页)")
    @SaCheckPermission("schoolrollreason:detail")
    public RT<PageOutput<SchoolRollReasonPageVo>> page(@Valid SchoolRollReasonPageDto dto){

        MPJLambdaWrapper<SchoolRollReason> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAs(SchoolRollReason::getId, SchoolRollReasonPageVo::getId)
                .selectAs(SchoolRollCategory::getName, SchoolRollReasonPageVo::getSchoolRollCategoryIdCN)
                .select(SchoolRollReason.class, x -> VoToColumnUtil.fieldsToColumns(SchoolRollReasonPageVo.class).contains(x.getProperty()))
                .leftJoin(SchoolRollCategory.class, SchoolRollCategory::getId, SchoolRollReason::getSchoolRollCategoryId)
                .eq(ObjectUtil.isNotNull(dto) && ObjectUtil.isNotNull(dto.getSchoolRollCategoryId()) && dto.getSchoolRollCategoryId() != "",SchoolRollReason::getSchoolRollCategoryId,dto.getSchoolRollCategoryId())
                .orderByDesc(SchoolRollReason::getId);

        IPage<SchoolRollReasonPageVo> page = schoolRollReasonService.selectJoinListPage(ConventPage.getPage(dto),SchoolRollReasonPageVo.class, queryWrapper);
        PageOutput<SchoolRollReasonPageVo> pageOutput = ConventPage.getPageOutput(page, SchoolRollReasonPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学籍异动原因信息")
    @SaCheckPermission("schoolrollreason:detail")
    public RT<SchoolRollReasonVo> info(@RequestParam Long id){
        SchoolRollReason schoolRollReason = schoolRollReasonService.getById(id);
        if (schoolRollReason == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(schoolRollReason, SchoolRollReasonVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增学籍异动原因")
    @SaCheckPermission("schoolrollreason:add")
    public RT<Boolean> add(@Valid @RequestBody AddSchoolRollReasonDto dto){
        SchoolRollReason schoolRollReason = BeanUtil.toBean(dto, SchoolRollReason.class);
        boolean isSuccess = schoolRollReasonService.save(schoolRollReason);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学籍异动原因")
    @SaCheckPermission("schoolrollreason:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateSchoolRollReasonDto dto){

        SchoolRollReason schoolRollReason = BeanUtil.toBean(dto, SchoolRollReason.class);
        return RT.ok(schoolRollReasonService.updateById(schoolRollReason));

    }

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

    }

}