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.toolkit.MPJWrappers;
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.AddBaseStudentBursariesApplicantDto;
import com.xjrsoft.module.student.dto.BaseStudentBursariesApplicantPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentBursariesApplicantDto;
import com.xjrsoft.module.student.entity.BaseStudentBursariesApplicant;
import com.xjrsoft.module.student.entity.BaseStudentBursariesStudent;
import com.xjrsoft.module.student.service.IBaseStudentBursariesApplicantService;
import com.xjrsoft.module.student.vo.BaseStudentBursariesApplicantPageVo;
import com.xjrsoft.module.student.vo.BaseStudentBursariesApplicantVo;
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 dzx
* @Date: 2023-11-24
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/baseStudentBursariesApplicant")
@Api(value = "/student"  + "/baseStudentBursariesApplicant",tags = "助学金申请代码")
@AllArgsConstructor
public class BaseStudentBursariesApplicantController {


    private final IBaseStudentBursariesApplicantService baseStudentBursariesApplicantService;

    @GetMapping(value = "/page")
    @ApiOperation(value="助学金申请列表(分页)")
    @SaCheckPermission("basestudentbursariesapplicant:detail")
    public RT<PageOutput<BaseStudentBursariesApplicantPageVo>> page(@Valid BaseStudentBursariesApplicantPageDto dto) {

        IPage<BaseStudentBursariesApplicantPageVo> page = baseStudentBursariesApplicantService.selectJoinListPage(
                ConventPage.getPage(dto), BaseStudentBursariesApplicantPageVo.class,
                MPJWrappers.<BaseStudentBursariesApplicant>lambdaJoin()
                .eq(BaseStudentBursariesApplicant::getBaseStudentBursariesProjectId, dto.getProjectId())
                .select(BaseStudentBursariesStudent::getId)
                .selectAs(DictionaryDetail::getName, BaseStudentBursariesApplicantPageVo::getBankTypeCn)
                .select(BaseStudentBursariesApplicant.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentBursariesApplicantPageVo.class).contains(x.getProperty()))
                .leftJoin(BaseStudentBursariesStudent.class,BaseStudentBursariesStudent::getBaseStudentBursariesApplicantId,BaseStudentBursariesApplicant::getId)
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, BaseStudentBursariesStudent::getBankType)
                .select(BaseStudentBursariesStudent.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentBursariesApplicantPageVo.class).contains(x.getProperty()))
//                        .selectAs(BaseStudentBursariesStudent::getGradeName, BaseStudentBursariesApplicantPageVo::getGradeName)
        );
        PageOutput<BaseStudentBursariesApplicantPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentBursariesApplicantPageVo.class);
        return RT.ok(pageOutput);
    }

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


    @PostMapping
    @ApiOperation(value = "新增助学金申请")
    @SaCheckPermission("basestudentbursariesapplicant:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentBursariesApplicantDto dto){
        BaseStudentBursariesApplicant baseStudentBursariesApplicant = BeanUtil.toBean(dto, BaseStudentBursariesApplicant.class);
        boolean isSuccess = baseStudentBursariesApplicantService.save(baseStudentBursariesApplicant);
    return RT.ok(isSuccess);
    }

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

        BaseStudentBursariesApplicant baseStudentBursariesApplicant = BeanUtil.toBean(dto, BaseStudentBursariesApplicant.class);
        return RT.ok(baseStudentBursariesApplicantService.updateById(baseStudentBursariesApplicant));

    }

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

    }

}