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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.attendance.dto.UpdateAttendanceMessageSetDto;
import com.xjrsoft.module.attendance.entity.AttendanceMessageUserRelation;
import com.xjrsoft.module.attendance.mapper.AttendanceMessageUserRelationMapper;
import com.xjrsoft.module.attendance.entity.AttendanceMessageSet;
import com.xjrsoft.module.attendance.mapper.AttendanceMessageSetMapper;
import com.xjrsoft.module.attendance.service.IAttendanceMessageSetService;
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;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

/**
* @title: 考勤消息设置
* @Author dzx
* @Date: 2024-05-21
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class AttendanceMessageSetServiceImpl extends MPJBaseServiceImpl<AttendanceMessageSetMapper, AttendanceMessageSet> implements IAttendanceMessageSetService {
    private final AttendanceMessageSetMapper attendanceMessageSetAttendanceMessageSetMapper;

    private final AttendanceMessageUserRelationMapper attendanceMessageSetAttendanceMessageUserRelationMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AttendanceMessageSet attendanceMessageSet) {
        attendanceMessageSetAttendanceMessageSetMapper.insert(attendanceMessageSet);
        for (AttendanceMessageUserRelation attendanceMessageUserRelation : attendanceMessageSet.getAttendanceMessageUserRelationList()) {
            attendanceMessageUserRelation.setAttendanceMessageSetId(attendanceMessageSet.getId());
            attendanceMessageSetAttendanceMessageUserRelationMapper.insert(attendanceMessageUserRelation);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(AttendanceMessageSet attendanceMessageSet) {
        attendanceMessageSetAttendanceMessageSetMapper.updateById(attendanceMessageSet);
        //********************************* AttendanceMessageUserRelation  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<AttendanceMessageUserRelation> attendanceMessageUserRelationList = attendanceMessageSetAttendanceMessageUserRelationMapper.selectList(Wrappers.lambdaQuery(AttendanceMessageUserRelation.class).eq(AttendanceMessageUserRelation::getAttendanceMessageSetId, attendanceMessageSet.getId()).select(AttendanceMessageUserRelation::getId));
            List<Long> attendanceMessageUserRelationIds = attendanceMessageUserRelationList.stream().map(AttendanceMessageUserRelation::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> attendanceMessageUserRelationOldIds = attendanceMessageSet.getAttendanceMessageUserRelationList().stream().map(AttendanceMessageUserRelation::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> attendanceMessageUserRelationRemoveIds = attendanceMessageUserRelationIds.stream().filter(item -> !attendanceMessageUserRelationOldIds.contains(item)).collect(Collectors.toList());

            for (AttendanceMessageUserRelation attendanceMessageUserRelation : attendanceMessageSet.getAttendanceMessageUserRelationList()) {
                //如果不等于空则修改
                if (attendanceMessageUserRelation.getId() != null) {
                    attendanceMessageSetAttendanceMessageUserRelationMapper.updateById(attendanceMessageUserRelation);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    attendanceMessageUserRelation.setAttendanceMessageSetId(attendanceMessageSet.getId());
                    attendanceMessageSetAttendanceMessageUserRelationMapper.insert(attendanceMessageUserRelation);
                }
            }
            //已经不存在的id 删除
            if(attendanceMessageUserRelationRemoveIds.size() > 0){
                attendanceMessageSetAttendanceMessageUserRelationMapper.deleteBatchIds(attendanceMessageUserRelationRemoveIds);
            }
        }
        //********************************* AttendanceMessageUserRelation  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean edit(List<UpdateAttendanceMessageSetDto>  attendanceMessageSetDtos) {
        for (UpdateAttendanceMessageSetDto attendanceMessageSetDto : attendanceMessageSetDtos) {
            // 先删除 再添加
            AttendanceMessageSet attendanceMessageSetDel = attendanceMessageSetAttendanceMessageSetMapper.selectOne(Wrappers.lambdaQuery(AttendanceMessageSet.class)
                    .eq(AttendanceMessageSet::getRoleType, attendanceMessageSetDto.getRoleType())
                    .eq(AttendanceMessageSet::getMessageCategory, attendanceMessageSetDto.getMessageCategory()));
            if (attendanceMessageSetDel != null) {
                attendanceMessageSetAttendanceMessageSetMapper.deleteById(attendanceMessageSetDel.getId());
                attendanceMessageSetAttendanceMessageUserRelationMapper.delete(Wrappers.lambdaQuery(AttendanceMessageUserRelation.class).eq(AttendanceMessageUserRelation::getAttendanceMessageSetId, attendanceMessageSetDel.getId()));
            }

            if (attendanceMessageSetDto.getDeleteMark() != 1) {
                AttendanceMessageSet attendanceMessageSet = BeanUtil.toBean(attendanceMessageSetDto, AttendanceMessageSet.class);
                add(attendanceMessageSet);
            }

        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        attendanceMessageSetAttendanceMessageSetMapper.deleteBatchIds(ids);
        attendanceMessageSetAttendanceMessageUserRelationMapper.delete(Wrappers.lambdaQuery(AttendanceMessageUserRelation.class).in(AttendanceMessageUserRelation::getAttendanceMessageSetId, ids));

        return true;
    }
}
