package com.xjrsoft.module.student.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.enums.ArchivesStatusEnum;
import com.xjrsoft.module.student.dto.AddBaseStudentGraduateDto;
import com.xjrsoft.module.student.dto.BaseStudentGraduatePageDto;
import com.xjrsoft.module.student.entity.BaseStudentGraduate;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.student.mapper.BaseStudentGraduateMapper;
import com.xjrsoft.module.student.mapper.BaseStudentSchoolRollMapper;
import com.xjrsoft.module.student.service.IBaseStudentGraduateService;
import com.xjrsoft.module.student.vo.BaseStudentGraduatePageVo;
import com.xjrsoft.module.student.vo.BaseStudentGraduateVo;
import com.xjrsoft.module.system.entity.File;
import com.xjrsoft.module.system.service.IFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
* @title: 学生毕业管理
* @Author dzx
* @Date: 2024-01-26
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class BaseStudentGraduateServiceImpl extends MPJBaseServiceImpl<BaseStudentGraduateMapper, BaseStudentGraduate> implements IBaseStudentGraduateService {
    private final BaseStudentGraduateMapper baseStudentGraduateMapper;

    private final BaseStudentSchoolRollMapper baseStudentSchoolRollMapper;

    private final IFileService fileService;
    @Override
    public Page<BaseStudentGraduatePageVo> getPage(Page<BaseStudentGraduatePageDto> page, BaseStudentGraduatePageDto dto) {
        Page<BaseStudentGraduatePageVo> voPage = baseStudentGraduateMapper.getPage(page, dto);
        for (BaseStudentGraduatePageVo record : voPage.getRecords()) {
            if(record.getAppendixId() != null){
                record.setFileInfos(fileService.list(Wrappers.<File>query().lambda().eq(File::getFolderId,record.getAppendixId())));
            }
        }
        return voPage;
    }

    @Override
    public BaseStudentGraduateVo getInfoById(Long id) {
        BaseStudentGraduateVo baseStudentGraduateVo = baseStudentGraduateMapper.getInfoById(id);
        if(baseStudentGraduateVo != null && baseStudentGraduateVo.getAppendixId() != null){
            baseStudentGraduateVo.setFileInfos(fileService.list(Wrappers.<File>query().lambda().eq(File::getFolderId,baseStudentGraduateVo.getAppendixId())));
        }
        return baseStudentGraduateVo;
    }

    @Override
    @Transactional
    public Boolean addOne(AddBaseStudentGraduateDto dto) {
        BaseStudentGraduate baseStudentGraduate = BeanUtil.toBean(dto, BaseStudentGraduate.class);
        baseStudentGraduate.setCreateUserId(StpUtil.getLoginIdAsLong());
        baseStudentGraduate.setCreateDate(new Date());
        baseStudentGraduate.setAppendixId(dto.getFolderId());
        QueryWrapper<BaseStudentGraduate> queryWrapperSortcode = new QueryWrapper<>();
        queryWrapperSortcode.select("IFNULL(MAX(sort_code),0) as sortCode");
        BaseStudentGraduate t = this.getOne(queryWrapperSortcode);
        baseStudentGraduate.setSortCode(t.getSortCode() + 1);
        //修改学籍状态
        LambdaQueryWrapper<BaseStudentSchoolRoll> baseStudentSchoolRollLambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseStudentSchoolRollLambdaQueryWrapper
                .eq(BaseStudentSchoolRoll::getUserId, dto.getUserId());
         baseStudentSchoolRollMapper.update(new BaseStudentSchoolRoll(){{
            setArchivesStatus(ArchivesStatusEnum.FB2907.getCode());
        }}, baseStudentSchoolRollLambdaQueryWrapper);

        //保存毕业信息
        this.save(baseStudentGraduate);
        return true;
    }
}
