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.AddBaseStudentBursariesStudentDto;
import com.xjrsoft.module.student.dto.BaseStudentBursariesStudentPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentBursariesStudentDto;
import com.xjrsoft.module.student.entity.BaseStudentBursariesStudent;
import com.xjrsoft.module.student.service.IBaseStudentBursariesStudentService;
import com.xjrsoft.module.student.vo.BaseStudentBursariesStudentPageVo;
import com.xjrsoft.module.student.vo.BaseStudentBursariesStudentVo;
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 devil
* @Date: 2023-11-29
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/baseStudentBursariesStudent")
@Api(value = "/student"  + "/baseStudentBursariesStudent",tags = "助学金申请学生信息代码")
@AllArgsConstructor
public class BaseStudentBursariesStudentController {


    private final IBaseStudentBursariesStudentService baseStudentBursariesStudentService;

    @GetMapping(value = "/page")
    @ApiOperation(value="助学金申请学生信息列表(分页)")
    @SaCheckPermission("basestudentbursariesstudent:detail")
    public RT<PageOutput<BaseStudentBursariesStudentPageVo>> page(@Valid BaseStudentBursariesStudentPageDto dto){

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询助学金申请学生信息信息")
    @SaCheckPermission("basestudentbursariesstudent:detail")
    public RT<BaseStudentBursariesStudentVo> info(@RequestParam Long id){
        BaseStudentBursariesStudent baseStudentBursariesStudent = baseStudentBursariesStudentService.getById(id);
        if (baseStudentBursariesStudent == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(baseStudentBursariesStudent, BaseStudentBursariesStudentVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增助学金申请学生信息")
    @SaCheckPermission("basestudentbursariesstudent:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentBursariesStudentDto dto){
        BaseStudentBursariesStudent baseStudentBursariesStudent = BeanUtil.toBean(dto, BaseStudentBursariesStudent.class);
        boolean isSuccess = baseStudentBursariesStudentService.save(baseStudentBursariesStudent);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改助学金申请学生信息")
    @SaCheckPermission("basestudentbursariesstudent:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentBursariesStudentDto dto){

        BaseStudentBursariesStudent baseStudentBursariesStudent = BeanUtil.toBean(dto, BaseStudentBursariesStudent.class);
        return RT.ok(baseStudentBursariesStudentService.updateById(baseStudentBursariesStudent));

    }

    @DeleteMapping
    @ApiOperation(value = "删除助学金申请学生信息")
    @SaCheckPermission("basestudentbursariesstudent:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(baseStudentBursariesStudentService.removeBatchByIds(ids));

    }

}