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

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.module.student.dto.AddEnrollmentPlanMajorSetDto;
import com.xjrsoft.module.student.dto.BaseGraduateSchoolOfEnrollmentPlanListDto;
import com.xjrsoft.module.student.dto.BaseMajorSetOfEnrollmentPlanPageDto;
import com.xjrsoft.module.student.entity.EnrollmentPlanGraduateSchool;
import com.xjrsoft.module.student.entity.EnrollmentPlanMajorSet;
import com.xjrsoft.module.student.mapper.EnrollmentPlanGraduateSchoolMapper;
import com.xjrsoft.module.student.mapper.EnrollmentPlanMajorSetMapper;
import com.xjrsoft.module.student.service.IEnrollmentPlanMajorSetService;
import com.xjrsoft.module.student.vo.BaseGraduateSchoolOfEnrollmentPlanListVo;
import com.xjrsoft.module.student.vo.BaseMajorSetOfEnrollmentPlanPageVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @title: 专业方向招生计划详情
* @Author phoenix
* @Date: 2024-06-13
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class EnrollmentPlanMajorSetServiceImpl extends MPJBaseServiceImpl<EnrollmentPlanMajorSetMapper, EnrollmentPlanMajorSet> implements IEnrollmentPlanMajorSetService {

    private EnrollmentPlanMajorSetMapper enrollmentPlanMajorSetMapper;

    private EnrollmentPlanGraduateSchoolMapper enrollmentPlanGraduateSchoolMapper;

    @Override
    public IPage<BaseMajorSetOfEnrollmentPlanPageVo> baseMajorSetOfEnrollmentPlan(BaseMajorSetOfEnrollmentPlanPageDto dto) {
        return enrollmentPlanMajorSetMapper.baseMajorSetOfEnrollmentPlan(ConventPage.getPage(dto),dto);
    }

    @Override
    public List<BaseGraduateSchoolOfEnrollmentPlanListVo> baseGraduateSchoolOfEnrollmentPlan(BaseGraduateSchoolOfEnrollmentPlanListDto dto) {
        return enrollmentPlanMajorSetMapper.baseGraduateSchoolOfEnrollmentPlan(dto);
    }

    @Override
    @Transactional
    public Boolean add(AddEnrollmentPlanMajorSetDto dto) {
        boolean isSuccess = false;
        //删除该招生计划下所有的专业方向招生详情和学校招生详情
        MPJLambdaWrapper<EnrollmentPlanGraduateSchool> enrollmentPlanGraduateSchoolDelete = new MPJLambdaWrapper<>();
        enrollmentPlanGraduateSchoolDelete
                .selectAs(EnrollmentPlanGraduateSchool::getId, EnrollmentPlanGraduateSchool::getId)
                .leftJoin(EnrollmentPlanMajorSet.class, EnrollmentPlanMajorSet::getId, EnrollmentPlanGraduateSchool::getEnrollmentPlanMajorSetId)
                .eq(EnrollmentPlanMajorSet::getEnrollmentPlanId, dto.getEnrollmentPlanId())
        ;

        List<EnrollmentPlanGraduateSchool> enrollmentPlanGraduateSchoolDeleteList = enrollmentPlanGraduateSchoolMapper.selectJoinList(EnrollmentPlanGraduateSchool.class, enrollmentPlanGraduateSchoolDelete);
        List<Long> idList = enrollmentPlanGraduateSchoolDeleteList.stream()
                .map(EnrollmentPlanGraduateSchool::getId) // 获取对象的主键id
                .collect(Collectors.toList()); // 将主键id收集到新的List中
        if(!idList.isEmpty()){
            enrollmentPlanGraduateSchoolMapper.deleteBatchIds(idList);
        }

        LambdaQueryWrapper<EnrollmentPlanMajorSet> enrollmentPlanMajorSetDelete = new LambdaQueryWrapper<>();
        enrollmentPlanMajorSetDelete
                .eq(EnrollmentPlanMajorSet::getEnrollmentPlanId, dto.getEnrollmentPlanId())
        ;

        this.remove(enrollmentPlanMajorSetDelete);

        //获取sortCode的最大数量
        QueryWrapper<EnrollmentPlanMajorSet> enrollmentPlanMajorSetSortcode = new QueryWrapper<>();
        enrollmentPlanMajorSetSortcode.select("IFNULL(MAX(sort_code),0) as sortCode");
        EnrollmentPlanMajorSet enrollmentPlanMajorSet = this.getOne(enrollmentPlanMajorSetSortcode);
        int epmsSortCode = enrollmentPlanMajorSet.getSortCode() + 1;

        QueryWrapper<EnrollmentPlanGraduateSchool> enrollmentPlanGraduateSchoolSortcode = new QueryWrapper<>();
        enrollmentPlanGraduateSchoolSortcode.select("IFNULL(MAX(sort_code),0) as sortCode");
        EnrollmentPlanGraduateSchool enrollmentPlanGraduateSchool = enrollmentPlanGraduateSchoolMapper.selectOne(enrollmentPlanGraduateSchoolSortcode);
        int epgsSortCode = enrollmentPlanGraduateSchool.getSortCode() + 1;


        for (Long baseMajorSetId : dto.getBaseMajorSetIds()){
            EnrollmentPlanMajorSet newEnrollmentPlanMajorSet = new EnrollmentPlanMajorSet();
            newEnrollmentPlanMajorSet.setEnrollmentPlanId(dto.getEnrollmentPlanId());
            newEnrollmentPlanMajorSet.setBaseMajorSetId(baseMajorSetId);
            newEnrollmentPlanMajorSet.setCreateDate(new Date());
            newEnrollmentPlanMajorSet.setCreateUserId(StpUtil.getLoginIdAsLong());
            newEnrollmentPlanMajorSet.setSortCode(epmsSortCode++);

            isSuccess = this.save(newEnrollmentPlanMajorSet);
            for (Long baseGraduateSchoolId : dto.getBaseGraduateSchoolIds()){
                int finalEpgsSortCode = epgsSortCode;
                isSuccess = enrollmentPlanGraduateSchoolMapper.insert(new EnrollmentPlanGraduateSchool() {{
                    setEnrollmentPlanMajorSetId(newEnrollmentPlanMajorSet.getId());
                    setBaseGraduateSchoolId(baseGraduateSchoolId);
                    setCreateDate(new Date());
                    setCreateUserId(StpUtil.getLoginIdAsLong());
                    setSortCode(finalEpgsSortCode);
                }}) > 0;
                epgsSortCode += 1;
            }
        }

        return isSuccess;
    }

    @Override
    @Transactional
    public Boolean removeBatch(List<Long> ids) {

        LambdaUpdateWrapper<EnrollmentPlanGraduateSchool> enrollmentPlanGraduateSchoolLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        enrollmentPlanGraduateSchoolLambdaUpdateWrapper
                .in(EnrollmentPlanGraduateSchool::getEnrollmentPlanMajorSetId, ids)
        ;
        enrollmentPlanGraduateSchoolMapper.delete(enrollmentPlanGraduateSchoolLambdaUpdateWrapper);

        return this.removeBatchByIds(ids);
    }
}
