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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
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 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 baseStudentScholarshipCategoryBaseStudentScholarshipCategoryMapper;

    private final BaseStudentScholarshipLevelMapper baseStudentScholarshipCategoryBaseStudentScholarshipLevelMapper;


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

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(BaseStudentScholarshipCategory baseStudentScholarshipCategory) {
        baseStudentScholarshipCategoryBaseStudentScholarshipCategoryMapper.updateById(baseStudentScholarshipCategory);
        //********************************* BaseStudentScholarshipLevel  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<BaseStudentScholarshipLevel> baseStudentScholarshipLevelList = baseStudentScholarshipCategoryBaseStudentScholarshipLevelMapper.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) {
                    baseStudentScholarshipCategoryBaseStudentScholarshipLevelMapper.updateById(baseStudentScholarshipLevel);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    baseStudentScholarshipLevel.setBaseStudentScholarshipCategoryId(baseStudentScholarshipCategory.getId());
                    baseStudentScholarshipCategoryBaseStudentScholarshipLevelMapper.insert(baseStudentScholarshipLevel);
                }
            }
            //已经不存在的id 删除
            if(baseStudentScholarshipLevelRemoveIds.size() > 0){
                baseStudentScholarshipCategoryBaseStudentScholarshipLevelMapper.deleteBatchIds(baseStudentScholarshipLevelRemoveIds);
            }
        }
        //********************************* BaseStudentScholarshipLevel  增删改  结束 *******************************************/

        return true;
    }

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

        return true;
    }
}
