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 cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.base.entity.BaseSemester;
import com.xjrsoft.module.student.dto.AddBaseStudentScholarshipApplicantDto;
import com.xjrsoft.module.student.dto.BaseStudentScholarshipApplicantCategoryPageDto;
import com.xjrsoft.module.student.dto.BaseStudentScholarshipApplicantPageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentScholarshipApplicantDto;
import com.xjrsoft.module.student.entity.BaseStudentScholarshipApplicant;
import com.xjrsoft.module.student.entity.BaseStudentScholarshipCategory;
import com.xjrsoft.module.student.service.IBaseStudentScholarshipApplicantService;
import com.xjrsoft.module.student.service.IBaseStudentScholarshipCategoryService;
import com.xjrsoft.module.student.vo.BaseStudentScholarshipApplicantCategoryPageVo;
import com.xjrsoft.module.student.vo.BaseStudentScholarshipApplicantPageVo;
import com.xjrsoft.module.student.vo.BaseStudentScholarshipApplicantVo;
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-23
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/baseStudentScholarshipApplicant")
@Api(value = "/student"  + "/baseStudentScholarshipApplicant",tags = "奖学金申请代码")
@AllArgsConstructor
public class BaseStudentScholarshipApplicantController {


    private final IBaseStudentScholarshipApplicantService baseStudentScholarshipApplicantService;
    private final IBaseStudentScholarshipCategoryService baseStudentScholarshipCategoryService;

    @GetMapping(value = "/page")
    @ApiOperation(value="奖学金申请列表(分页)")
    @SaCheckPermission("basestudentscholarshipapplicant:detail")
    public RT<PageOutput<BaseStudentScholarshipApplicantPageVo>> page(@Valid BaseStudentScholarshipApplicantPageDto dto){
        IPage<BaseStudentScholarshipApplicantPageVo> page = baseStudentScholarshipApplicantService.selectJoinListPage(ConventPage.getPage(dto), BaseStudentScholarshipApplicantPageVo.class,
        MPJWrappers.<BaseStudentScholarshipApplicant>lambdaJoin()
                .like(StrUtil.isNotEmpty(dto.getName()), BaseStudentScholarshipApplicant::getName, dto.getName())
                .eq(ObjectUtil.isNotNull(dto.getSemesterId()), BaseStudentScholarshipCategory::getBaseSemesterId, dto.getSemesterId())
                .eq(ObjectUtil.isNotNull(dto.getReleaseStatus()), BaseStudentScholarshipApplicant::getReleaseStatus, dto.getReleaseStatus())
                .eq(BaseStudentScholarshipApplicant::getStatus, 1)
                .between(ObjectUtil.isNotNull(dto.getStartDate()) && ObjectUtil.isNotNull(dto.getEndDate()), BaseStudentScholarshipApplicant::getAwardDate,dto.getStartDate(),dto.getEndDate())
                .eq(BaseStudentScholarshipApplicant::getBaseStudentScholarshipCategoryId,dto.getBaseStudentScholarshipCategoryId())
                .orderByAsc(BaseStudentScholarshipApplicant::getSortCode)
                .selectAs(BaseStudentScholarshipApplicant::getId, BaseStudentScholarshipApplicantPageVo::getId)
                .selectAs(DictionaryDetail::getName, BaseStudentScholarshipApplicantPageVo::getBankTypeCn)
                .selectAs(BaseSemester::getName, BaseStudentScholarshipApplicantPageVo::getSemesterName)
                .select(BaseStudentScholarshipApplicant.class,x -> VoToColumnUtil.fieldsToColumns(BaseStudentScholarshipApplicantPageVo.class).contains(x.getProperty()))
                .innerJoin(BaseStudentScholarshipCategory.class, BaseStudentScholarshipCategory::getId, BaseStudentScholarshipApplicant::getBaseStudentScholarshipCategoryId)
                .leftJoin(BaseSemester.class, BaseSemester::getId, BaseStudentScholarshipApplicant::getBaseSemesterId)
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, BaseStudentScholarshipApplicant::getBankType)
        );
        PageOutput<BaseStudentScholarshipApplicantPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentScholarshipApplicantPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/scholarship-page")
    @ApiOperation(value="奖学金名称表(分页)")
    @SaCheckPermission("basestudentscholarshipapplicant:detail")
    public RT<PageOutput<BaseStudentScholarshipApplicantCategoryPageVo>> scholarshiPage(@Valid BaseStudentScholarshipApplicantCategoryPageDto dto){
        IPage<BaseStudentScholarshipApplicantCategoryPageVo> page = baseStudentScholarshipApplicantService.getScholarshiPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<BaseStudentScholarshipApplicantCategoryPageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentScholarshipApplicantCategoryPageVo.class);
        return RT.ok(pageOutput);
    }

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


    @PostMapping
    @ApiOperation(value = "新增奖学金申请")
    @SaCheckPermission("basestudentscholarshipapplicant:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentScholarshipApplicantDto dto){
        BaseStudentScholarshipApplicant baseStudentScholarshipApplicant = BeanUtil.toBean(dto, BaseStudentScholarshipApplicant.class);
        boolean isSuccess = baseStudentScholarshipApplicantService.save(baseStudentScholarshipApplicant);
    return RT.ok(isSuccess);
    }

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

        BaseStudentScholarshipApplicant baseStudentScholarshipApplicant = BeanUtil.toBean(dto, BaseStudentScholarshipApplicant.class);
        return RT.ok(baseStudentScholarshipApplicantService.updateById(baseStudentScholarshipApplicant));

    }

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

    }

}