package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.AddSchoolRollTransferStudentDto;
import com.xjrsoft.module.student.dto.SchoolRollTransferStudentPageDto;
import com.xjrsoft.module.student.dto.UpdateSchoolRollTransferStudentDto;
import com.xjrsoft.module.student.entity.SchoolRollTransferStudent;
import com.xjrsoft.module.student.service.ISchoolRollTransferStudentService;
import com.xjrsoft.module.student.vo.SchoolRollTransferStudentPageVo;
import com.xjrsoft.module.student.vo.SchoolRollTransferStudentVo;
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 dzx
* @Date: 2023-11-30
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/schoolRollTransferStudent")
@Api(value = "/student"  + "/schoolRollTransferStudent",tags = "插班生管理代码")
@AllArgsConstructor
public class SchoolRollTransferStudentController {


    private final ISchoolRollTransferStudentService schoolRollTransferStudentService;

    @GetMapping(value = "/page")
    @ApiOperation(value="插班生管理列表(分页)")
    @SaCheckPermission("schoolrolltransferstudent:detail")
    public RT<PageOutput<SchoolRollTransferStudentPageVo>> page(@Valid SchoolRollTransferStudentPageDto dto){

        LambdaQueryWrapper<SchoolRollTransferStudent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(SchoolRollTransferStudent::getId)
                .select(SchoolRollTransferStudent.class,x -> VoToColumnUtil.fieldsToColumns(SchoolRollTransferStudentPageVo.class).contains(x.getProperty()));
        IPage<SchoolRollTransferStudent> page = schoolRollTransferStudentService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<SchoolRollTransferStudentPageVo> pageOutput = ConventPage.getPageOutput(page, SchoolRollTransferStudentPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询插班生管理信息")
    @SaCheckPermission("schoolrolltransferstudent:detail")
    public RT<SchoolRollTransferStudentVo> info(@RequestParam Long id){
        SchoolRollTransferStudent schoolRollTransferStudent = schoolRollTransferStudentService.getById(id);
        if (schoolRollTransferStudent == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(schoolRollTransferStudent, SchoolRollTransferStudentVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增插班生管理")
    @SaCheckPermission("schoolrolltransferstudent:add")
    public RT<Boolean> add(@Valid @RequestBody AddSchoolRollTransferStudentDto dto){
        SchoolRollTransferStudent schoolRollTransferStudent = BeanUtil.toBean(dto, SchoolRollTransferStudent.class);
        boolean isSuccess = schoolRollTransferStudentService.save(schoolRollTransferStudent);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改插班生管理")
    @SaCheckPermission("schoolrolltransferstudent:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateSchoolRollTransferStudentDto dto){

        SchoolRollTransferStudent schoolRollTransferStudent = BeanUtil.toBean(dto, SchoolRollTransferStudent.class);
        return RT.ok(schoolRollTransferStudentService.updateById(schoolRollTransferStudent));

    }

    @DeleteMapping
    @ApiOperation(value = "删除插班生管理")
    @SaCheckPermission("schoolrolltransferstudent:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(schoolRollTransferStudentService.removeBatchByIds(ids));

    }

}