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

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.page.ConventPage;
import com.xjrsoft.module.student.dto.BaseStudentPunishmentInfoMobilePageDto;
import com.xjrsoft.module.student.dto.BaseStudentPunishmentInfoPageDto;
import com.xjrsoft.module.student.entity.BasePunishmentStudentRelation;
import com.xjrsoft.module.student.entity.BaseStudentPunishmentInfo;
import com.xjrsoft.module.student.mapper.BasePunishmentStudentRelationMapper;
import com.xjrsoft.module.student.mapper.BaseStudentPunishmentInfoMapper;
import com.xjrsoft.module.student.service.IBasePunishStudentRelationService;
import com.xjrsoft.module.student.service.IBaseStudentPunishmentInfoService;
import com.xjrsoft.module.student.vo.BaseStudentPunishmentInfoDetailVo;
import com.xjrsoft.module.student.vo.BaseStudentPunishmentInfoMobileDetailVo;
import com.xjrsoft.module.student.vo.BaseStudentPunishmentInfoMobilePageVo;
import com.xjrsoft.module.student.vo.BaseStudentPunishmentInfoPageVo;
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.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @title: 学生处分信息管理
* @Author dzx
* @Date: 2023-11-15
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class BaseStudentPunishmentInfoServiceImpl extends MPJBaseServiceImpl<BaseStudentPunishmentInfoMapper, BaseStudentPunishmentInfo> implements IBaseStudentPunishmentInfoService {
    private final BaseStudentPunishmentInfoMapper baseStudentPunishmentInfoBaseStudentPunishmentInfoMapper;

    private final BasePunishmentStudentRelationMapper baseStudentPunishmentInfoBasePunishmentStudentRelationMapper;

    IBasePunishStudentRelationService basePunishStudentRelationService;

    private final IFileService fileService;


    /**
     * 学生处分信息管理列表(分页)
     * @param dto 处分管理信息前端请求实体对象
     * @return
     */
    @Override
    public IPage<BaseStudentPunishmentInfoPageVo> selectJoinListPagePc(BaseStudentPunishmentInfoPageDto dto) {

        IPage<BaseStudentPunishmentInfoPageVo> pageList = baseStudentPunishmentInfoBaseStudentPunishmentInfoMapper.getPage(ConventPage.getPage(dto),dto);

        return pageList;
    }

    /**
     * 学生处分信息管理列表(分页)(移动端）
     * @param dto 处分管理信息前端请求实体对象(移动端）
     * @return
     */
    @Override
    public IPage<BaseStudentPunishmentInfoMobilePageVo> getMobilePage(BaseStudentPunishmentInfoMobilePageDto dto) {


        return null;
    }

    /**
     * 学生处分信息详情
     *
     * @param id 处分管理信息id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseStudentPunishmentInfoDetailVo getDetailById(Long id) {

        BaseStudentPunishmentInfoDetailVo baseStudentPunishmentInfoDetailVo =  baseStudentPunishmentInfoBaseStudentPunishmentInfoMapper.getInfo(id);

        //添加学生列表集合
        QueryWrapper<BasePunishmentStudentRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("punishment_info_id", id);
        List<BasePunishmentStudentRelation> studentList = basePunishStudentRelationService.list(queryWrapper);

        baseStudentPunishmentInfoDetailVo.setStudentList(studentList);

        // 添加文件列表
        baseStudentPunishmentInfoDetailVo.setFileList(fileService.list(Wrappers.<File>query().lambda().eq(File::getFolderId, baseStudentPunishmentInfoDetailVo.getFileId())));

        return baseStudentPunishmentInfoDetailVo;
    }

    /**
     * 学生处分信息详情
     * @return
     */
    @Override
    public BaseStudentPunishmentInfoMobileDetailVo getMobileInfo(Long id) {

        return baseStudentPunishmentInfoBaseStudentPunishmentInfoMapper.getMobileInfo(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(BaseStudentPunishmentInfo baseStudentPunishmentInfo) {
        baseStudentPunishmentInfoBaseStudentPunishmentInfoMapper.insert(baseStudentPunishmentInfo);
        for (BasePunishmentStudentRelation basePunishmentStudentRelation : baseStudentPunishmentInfo.getBasePunishmentStudentRelationList()) {
            basePunishmentStudentRelation.setPunishmentInfoId(baseStudentPunishmentInfo.getId());
            baseStudentPunishmentInfoBasePunishmentStudentRelationMapper.insert(basePunishmentStudentRelation);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(BaseStudentPunishmentInfo baseStudentPunishmentInfo) {
        baseStudentPunishmentInfoBaseStudentPunishmentInfoMapper.updateById(baseStudentPunishmentInfo);
        //********************************* BasePunishmentStudentRelation  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<BasePunishmentStudentRelation> basePunishmentStudentRelationList = baseStudentPunishmentInfoBasePunishmentStudentRelationMapper.selectList(Wrappers.lambdaQuery(BasePunishmentStudentRelation.class).eq(BasePunishmentStudentRelation::getPunishmentInfoId, baseStudentPunishmentInfo.getId()).select(BasePunishmentStudentRelation::getId));
            List<Long> basePunishmentStudentRelationIds = basePunishmentStudentRelationList.stream().map(BasePunishmentStudentRelation::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> basePunishmentStudentRelationOldIds = baseStudentPunishmentInfo.getBasePunishmentStudentRelationList().stream().map(BasePunishmentStudentRelation::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> basePunishmentStudentRelationRemoveIds = basePunishmentStudentRelationIds.stream().filter(item -> !basePunishmentStudentRelationOldIds.contains(item)).collect(Collectors.toList());

            for (BasePunishmentStudentRelation basePunishmentStudentRelation : baseStudentPunishmentInfo.getBasePunishmentStudentRelationList()) {
                //如果不等于空则修改
                if (basePunishmentStudentRelation.getId() != null) {
                    baseStudentPunishmentInfoBasePunishmentStudentRelationMapper.updateById(basePunishmentStudentRelation);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    basePunishmentStudentRelation.setPunishmentInfoId(baseStudentPunishmentInfo.getId());
                    baseStudentPunishmentInfoBasePunishmentStudentRelationMapper.insert(basePunishmentStudentRelation);
                }
            }
            //已经不存在的id 删除
            if(basePunishmentStudentRelationRemoveIds.size() > 0){
                baseStudentPunishmentInfoBasePunishmentStudentRelationMapper.deleteBatchIds(basePunishmentStudentRelationRemoveIds);
            }
        }
        //********************************* BasePunishmentStudentRelation  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        baseStudentPunishmentInfoBaseStudentPunishmentInfoMapper.deleteBatchIds(ids);
        baseStudentPunishmentInfoBasePunishmentStudentRelationMapper.delete(Wrappers.lambdaQuery(BasePunishmentStudentRelation.class).in(BasePunishmentStudentRelation::getPunishmentInfoId, ids));

        return true;
    }

    @Override
    public Page<BaseStudentPunishmentInfoMobilePageVo> getMobilePage(Page<BaseStudentPunishmentInfoMobilePageDto> page, BaseStudentPunishmentInfoMobilePageDto dto) {
        dto.setTeacherId(StpUtil.getLoginIdAsLong());
        Page<BaseStudentPunishmentInfoMobilePageVo> result = baseStudentPunishmentInfoBaseStudentPunishmentInfoMapper.getMobilePage(page, dto);
        return result;
    }
}
