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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.student.dto.BaseStudentScholarshipReleasePageDto;
import com.xjrsoft.module.student.entity.BaseStudentScholarshipCategory;
import com.xjrsoft.module.student.entity.BaseStudentScholarshipLevel;
import com.xjrsoft.module.student.mapper.BaseStudentScholarshipCategoryMapper;
import com.xjrsoft.module.student.mapper.BaseStudentScholarshipLevelMapper;
import com.xjrsoft.module.student.service.IBaseStudentScholarshipCategoryService;
import com.xjrsoft.module.student.vo.BaseStudentScholarshipCategoryStatisticPageVo;
import com.xjrsoft.module.student.vo.BaseStudentScholarshipReleasePageVo;
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-23
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class BaseStudentScholarshipCategoryServiceImpl extends MPJBaseServiceImpl<BaseStudentScholarshipCategoryMapper, BaseStudentScholarshipCategory> implements IBaseStudentScholarshipCategoryService {
    private final BaseStudentScholarshipCategoryMapper categoryMapper;

    private final BaseStudentScholarshipLevelMapper levelMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(BaseStudentScholarshipCategory baseStudentScholarshipCategory) {
        categoryMapper.insert(baseStudentScholarshipCategory);
        for (BaseStudentScholarshipLevel baseStudentScholarshipLevel : baseStudentScholarshipCategory.getBaseStudentScholarshipLevelList()) {
            baseStudentScholarshipLevel.setBaseStudentScholarshipCategoryId(baseStudentScholarshipCategory.getId());
            levelMapper.insert(baseStudentScholarshipLevel);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(BaseStudentScholarshipCategory baseStudentScholarshipCategory) {
        categoryMapper.updateById(baseStudentScholarshipCategory);
        //********************************* BaseStudentScholarshipLevel  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<BaseStudentScholarshipLevel> baseStudentScholarshipLevelList = levelMapper.selectList(Wrappers.lambdaQuery(BaseStudentScholarshipLevel.class).eq(BaseStudentScholarshipLevel::getBaseStudentScholarshipCategoryId, baseStudentScholarshipCategory.getId()).select(BaseStudentScholarshipLevel::getId));
            List<Long> baseStudentScholarshipLevelIds = baseStudentScholarshipLevelList.stream().map(BaseStudentScholarshipLevel::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> baseStudentScholarshipLevelOldIds = baseStudentScholarshipCategory.getBaseStudentScholarshipLevelList().stream().map(BaseStudentScholarshipLevel::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> baseStudentScholarshipLevelRemoveIds = baseStudentScholarshipLevelIds.stream().filter(item -> !baseStudentScholarshipLevelOldIds.contains(item)).collect(Collectors.toList());

            for (BaseStudentScholarshipLevel baseStudentScholarshipLevel : baseStudentScholarshipCategory.getBaseStudentScholarshipLevelList()) {
                //如果不等于空则修改
                if (baseStudentScholarshipLevel.getId() != null) {
                    levelMapper.updateById(baseStudentScholarshipLevel);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    baseStudentScholarshipLevel.setBaseStudentScholarshipCategoryId(baseStudentScholarshipCategory.getId());
                    levelMapper.insert(baseStudentScholarshipLevel);
                }
            }
            //已经不存在的id 删除
            if(baseStudentScholarshipLevelRemoveIds.size() > 0){
                levelMapper.deleteBatchIds(baseStudentScholarshipLevelRemoveIds);
            }
        }
        //********************************* BaseStudentScholarshipLevel  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        categoryMapper.deleteBatchIds(ids);
        levelMapper.delete(Wrappers.lambdaQuery(BaseStudentScholarshipLevel.class).in(BaseStudentScholarshipLevel::getBaseStudentScholarshipCategoryId, ids));

        return true;
    }

    @Override
    public Page<BaseStudentScholarshipCategoryStatisticPageVo> getStatisticsPage(Page<BaseStudentScholarshipReleasePageVo> page, BaseStudentScholarshipReleasePageDto dto) {
        return this.baseMapper.getStatisticsPage(page, dto);
    }
}
