package com.xjrsoft.module.teacher.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.teacher.dto.TeacherAwardDetailPageDto;
import com.xjrsoft.module.teacher.dto.TeacherAwardStatisticsPageDto;
import com.xjrsoft.module.teacher.entity.TeacherAward;
import com.xjrsoft.module.teacher.entity.TeacherAwardItem;
import com.xjrsoft.module.teacher.entity.XjrUser;
import com.xjrsoft.module.teacher.service.ITeacherAwardItemService;
import com.xjrsoft.module.teacher.service.ITeacherAwardService;
import com.xjrsoft.module.teacher.vo.TeacherAwardDetailPageVo;
import com.xjrsoft.module.teacher.vo.TeacherAwardStatisticsPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* @title: 教师奖项
* @Author dzx
* @Date: 2024-06-04
* @Version 1.0
*/
@RestController
@RequestMapping("/teacher" + "/teacherAward")
@Api(value = "/teacher"  + "/teacherAward",tags = "教师获奖代码")
@AllArgsConstructor
public class TeacherAwardController {


    private final ITeacherAwardItemService teacherAwardItemService;

    private final ITeacherAwardService teacherAwardService;

    @GetMapping(value = "/statistics-page")
    @ApiOperation(value="教师获奖统计")
    @SaCheckPermission("teacheraward:detail")
    public RT<PageOutput<TeacherAwardStatisticsPageVo>> page(@Valid TeacherAwardStatisticsPageDto dto){

        Page<TeacherAwardStatisticsPageVo> voPage = teacherAwardItemService.getStatisticsPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<TeacherAwardStatisticsPageVo> pageOutput = ConventPage.getPageOutput(voPage, TeacherAwardStatisticsPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/detail-page")
    @ApiOperation(value="教师获奖详情页")
    @SaCheckPermission("teacheraward:detail")
    public RT<PageOutput<TeacherAwardDetailPageVo>> detailPage(@Valid TeacherAwardDetailPageDto dto){
        MPJLambdaWrapper<TeacherAward> teacherAwardMPJLambdaWrapper = new MPJLambdaWrapper<>();
        teacherAwardMPJLambdaWrapper
                .disableSubLogicDel()
                .selectAs(TeacherAwardItem::getIsThesis, TeacherAwardDetailPageVo::getIsThesis)
                .selectAs(XjrUser::getName, TeacherAwardDetailPageVo::getApplicantUserIdCn)
                .select(TeacherAward.class,x -> VoToColumnUtil.fieldsToColumns(TeacherAwardDetailPageVo.class).contains(x.getProperty()))
                .leftJoin(TeacherAwardItem.class, TeacherAwardItem::getId, TeacherAward::getTeacherAwardItemId)
                .leftJoin(XjrUser.class, XjrUser::getId, TeacherAward::getApplicantUserId)
                .eq(dto.getApplicantUserId() != null && dto.getApplicantUserId() > 0,TeacherAward::getApplicantUserId, dto.getApplicantUserId())
                .eq(dto.getIsThesis() != null,TeacherAwardItem::getIsThesis, dto.getIsThesis())
                .like(dto.getApplicantUserIdCn() != null && !dto.getApplicantUserIdCn().equals(""), XjrUser::getName, dto.getApplicantUserIdCn())
                .like(dto.getWholeCompetitionName() != null && !dto.getWholeCompetitionName().equals(""),TeacherAward::getWholeCompetitionName, dto.getWholeCompetitionName())
                .eq(TeacherAward::getStatus, 1)
                .orderByDesc(TeacherAward::getCreateDate)
        ;
        IPage<TeacherAwardDetailPageVo> page = teacherAwardService.selectJoinListPage(ConventPage.getPage(dto), TeacherAwardDetailPageVo.class, teacherAwardMPJLambdaWrapper);

        PageOutput<TeacherAwardDetailPageVo> pageOutput = ConventPage.getPageOutput(page, TeacherAwardDetailPageVo.class);
        return RT.ok(pageOutput);
    }
}