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

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.MPJWrappers;
import com.xjrsoft.common.enums.ScoreTypeEnum;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.student.dto.BaseStudentBehaviorManageMobilePageDto;
import com.xjrsoft.module.student.dto.BaseStudentBehaviorManagePageDto;
import com.xjrsoft.module.student.entity.BaseStudentBehaviorClassRelation;
import com.xjrsoft.module.student.entity.BaseStudentBehaviorManage;
import com.xjrsoft.module.student.entity.BaseStudentBehaviorStudentRelation;
import com.xjrsoft.module.student.mapper.BaseStudentBehaviorClassRelationMapper;
import com.xjrsoft.module.student.mapper.BaseStudentBehaviorManageMapper;
import com.xjrsoft.module.student.mapper.BaseStudentBehaviorStudentRelationMapper;
import com.xjrsoft.module.student.service.IBaseStudentBehaviorManageService;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorClassRelationVo;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorManageMobilePageVo;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorManageMobileVo;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorManagePageVo;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorManageVo;
import com.xjrsoft.module.student.vo.BaseStudentBehaviorStudentRelationVo;
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-17
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class BaseStudentBehaviorManageServiceImpl extends MPJBaseServiceImpl<BaseStudentBehaviorManageMapper, BaseStudentBehaviorManage> implements IBaseStudentBehaviorManageService {
    private final BaseStudentBehaviorManageMapper baseStudentBehaviorManageMapper;

    private final BaseStudentBehaviorStudentRelationMapper baseStudentBehaviorStudentRelationMapper;
    private final BaseStudentBehaviorClassRelationMapper baseStudentBehaviorClassRelationMapper;
    private final IFileService fileService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(BaseStudentBehaviorManage baseStudentBehaviorManage) {
        baseStudentBehaviorManageMapper.insert(baseStudentBehaviorManage);
        for (BaseStudentBehaviorStudentRelation baseStudentBehaviorStudentRelation : baseStudentBehaviorManage.getBaseStudentBehaviorStudentRelationList()) {
            baseStudentBehaviorStudentRelation.setBaseStudentBehaviorManageId(baseStudentBehaviorManage.getId());
            baseStudentBehaviorStudentRelationMapper.insert(baseStudentBehaviorStudentRelation);
        }
        for (BaseStudentBehaviorClassRelation baseStudentBehaviorClassRelation : baseStudentBehaviorManage.getBaseStudentBehaviorClassRelationList()) {
            baseStudentBehaviorClassRelation.setBaseStudentBehaviorManageId(baseStudentBehaviorManage.getId());
            baseStudentBehaviorClassRelationMapper.insert(baseStudentBehaviorClassRelation);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(BaseStudentBehaviorManage baseStudentBehaviorManage) {
        baseStudentBehaviorManageMapper.updateById(baseStudentBehaviorManage);
        //********************************* BaseStudentBehaviorStudentRelation  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<BaseStudentBehaviorStudentRelation> baseStudentBehaviorStudentRelationList = baseStudentBehaviorStudentRelationMapper.selectList(Wrappers.lambdaQuery(BaseStudentBehaviorStudentRelation.class).eq(BaseStudentBehaviorStudentRelation::getBaseStudentBehaviorManageId, baseStudentBehaviorManage.getId()).select(BaseStudentBehaviorStudentRelation::getId));
            List<Long> baseStudentBehaviorStudentRelationIds = baseStudentBehaviorStudentRelationList.stream().map(BaseStudentBehaviorStudentRelation::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> baseStudentBehaviorStudentRelationOldIds = baseStudentBehaviorManage.getBaseStudentBehaviorStudentRelationList().stream().map(BaseStudentBehaviorStudentRelation::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> baseStudentBehaviorStudentRelationRemoveIds = baseStudentBehaviorStudentRelationIds.stream().filter(item -> !baseStudentBehaviorStudentRelationOldIds.contains(item)).collect(Collectors.toList());

            for (BaseStudentBehaviorStudentRelation baseStudentBehaviorStudentRelation : baseStudentBehaviorManage.getBaseStudentBehaviorStudentRelationList()) {
                //如果不等于空则修改
                if (baseStudentBehaviorStudentRelation.getId() != null) {
                    baseStudentBehaviorStudentRelationMapper.updateById(baseStudentBehaviorStudentRelation);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    baseStudentBehaviorStudentRelation.setBaseStudentBehaviorManageId(baseStudentBehaviorManage.getId());
                    baseStudentBehaviorStudentRelationMapper.insert(baseStudentBehaviorStudentRelation);
                }
            }
            //已经不存在的id 删除
            if(baseStudentBehaviorStudentRelationRemoveIds.size() > 0){
                baseStudentBehaviorStudentRelationMapper.deleteBatchIds(baseStudentBehaviorStudentRelationRemoveIds);
            }
        }
        //********************************* BaseStudentBehaviorStudentRelation  增删改  结束 *******************************************/

        //********************************* BaseStudentBehaviorClassRelation  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<BaseStudentBehaviorClassRelation> baseStudentBehaviorClassRelationList = baseStudentBehaviorClassRelationMapper.selectList(Wrappers.lambdaQuery(BaseStudentBehaviorClassRelation.class).eq(BaseStudentBehaviorClassRelation::getBaseStudentBehaviorManageId, baseStudentBehaviorManage.getId()).select(BaseStudentBehaviorClassRelation::getId));
            List<Long> baseStudentBehaviorClassRelationIds = baseStudentBehaviorClassRelationList.stream().map(BaseStudentBehaviorClassRelation::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> baseStudentBehaviorClassRelationOldIds = baseStudentBehaviorManage.getBaseStudentBehaviorClassRelationList().stream().map(BaseStudentBehaviorClassRelation::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> baseStudentBehaviorClassRelationRemoveIds = baseStudentBehaviorClassRelationIds.stream().filter(item -> !baseStudentBehaviorClassRelationOldIds.contains(item)).collect(Collectors.toList());

            for (BaseStudentBehaviorClassRelation baseStudentBehaviorClassRelation : baseStudentBehaviorManage.getBaseStudentBehaviorClassRelationList()) {
                //如果不等于空则修改
                if (baseStudentBehaviorClassRelation.getId() != null) {
                    baseStudentBehaviorClassRelationMapper.updateById(baseStudentBehaviorClassRelation);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    baseStudentBehaviorClassRelation.setBaseStudentBehaviorManageId(baseStudentBehaviorManage.getId());
                    baseStudentBehaviorClassRelationMapper.insert(baseStudentBehaviorClassRelation);
                }
            }
            //已经不存在的id 删除
            if(baseStudentBehaviorClassRelationRemoveIds.size() > 0){
                baseStudentBehaviorClassRelationMapper.deleteBatchIds(baseStudentBehaviorClassRelationRemoveIds);
            }
        }
        //********************************* BaseStudentBehaviorClassRelation  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        baseStudentBehaviorManageMapper.deleteBatchIds(ids);
        baseStudentBehaviorStudentRelationMapper.delete(Wrappers.lambdaQuery(BaseStudentBehaviorStudentRelation.class).in(BaseStudentBehaviorStudentRelation::getBaseStudentBehaviorManageId, ids));
        baseStudentBehaviorClassRelationMapper.delete(Wrappers.lambdaQuery(BaseStudentBehaviorClassRelation.class).in(BaseStudentBehaviorClassRelation::getBaseStudentBehaviorManageId, ids));

        return true;
    }

    @Override
    public Page<BaseStudentBehaviorManagePageVo> getPage(Page<BaseStudentBehaviorManagePageDto> page, BaseStudentBehaviorManagePageDto dto) {
        Page<BaseStudentBehaviorManagePageVo> result = baseStudentBehaviorManageMapper.getPage(page, dto);
        result.getRecords().forEach((node) -> {
            if (node.getScoreType().equals(ScoreTypeEnum.ScoreMinus.getCode())) {
                node.setScore(node.getScore() * -1);
            }
        });
        return result;
    }

    @Override
    public BaseStudentBehaviorManageVo getInfo(Long id) {
        BaseStudentBehaviorManageVo result = baseStudentBehaviorManageMapper.getInfo(id);
        if (result == null) {
            return null;
        }
        // 处理加减分
        if (result.getScoreType().equals(ScoreTypeEnum.ScoreMinus.getCode())) {
            result.setScore(result.getScore() * -1);
        }
        //查询班级
        List<BaseStudentBehaviorClassRelation> classList = baseStudentBehaviorClassRelationMapper.selectList(
                MPJWrappers.<BaseStudentBehaviorClassRelation>lambdaJoin()
                        .eq(BaseStudentBehaviorClassRelation::getBaseStudentBehaviorManageId, id)
                        .selectAs(BaseClass::getName, BaseStudentBehaviorClassRelationVo::getClassName)
                        .select(BaseStudentBehaviorClassRelation.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentBehaviorClassRelation.class).contains(x.getProperty()))
                        .innerJoin(BaseClass.class, BaseClass::getId, BaseStudentBehaviorClassRelation::getClassId)
        );
        result.setBaseStudentBehaviorClassRelationList(BeanUtil.copyToList(classList, BaseStudentBehaviorClassRelationVo.class));
        //查询学生
        List<BaseStudentBehaviorStudentRelation> studentList = baseStudentBehaviorStudentRelationMapper.selectList(
                MPJWrappers.<BaseStudentBehaviorStudentRelation>lambdaJoin()
                        .eq(BaseStudentBehaviorStudentRelation::getBaseStudentBehaviorManageId, id)
                        .select(BaseStudentBehaviorStudentRelation.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentBehaviorStudentRelation.class).contains(x.getProperty()))
        );
        result.setBaseStudentBehaviorStudentRelationList(BeanUtil.copyToList(studentList, BaseStudentBehaviorStudentRelationVo.class));
        // 获取文件列表
        result.setFileInfos(fileService.list(Wrappers.<File>query().lambda().eq(File::getFolderId, result.getFileId())));
        return result;
    }

    @Override
    public Page<BaseStudentBehaviorManageMobilePageVo> getMobilePage(Page<BaseStudentBehaviorManageMobilePageDto> page, BaseStudentBehaviorManageMobilePageDto dto) {
        dto.setTeacherId(StpUtil.getLoginIdAsLong());
        Page<BaseStudentBehaviorManageMobilePageVo> result = baseStudentBehaviorManageMapper.getMobilePage(page, dto);
        result.getRecords().forEach((node) -> {
            if (node.getScoreType().equals(ScoreTypeEnum.ScoreMinus.getCode())) {
                node.setScore(node.getScore() * -1);
            }
        });
        return result;
    }

    @Override
    public BaseStudentBehaviorManageMobileVo getMobileInfo(Long id) {
        return null;
    }
}
