package com.yida.data.attendance.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.attendance.mapper.AttendanceRuleObjUserMapper;
import com.yida.data.attendance.service.AttendanceRuleObjUserService;
import com.yida.data.common.core.entity.attendance.attendance.AttendanceRuleObj;
import com.yida.data.common.core.entity.attendance.attendance.AttendanceRuleObjUser;
import com.yida.data.common.core.entity.user.EduStaff;
import com.yida.data.common.core.entity.user.EduStudent;
import com.yida.data.common.core.entity.user.enums.StudentStatusEnum;
import com.yida.data.common.core.entity.user.enums.StudentTypeEnum;
import com.yida.data.common.core.enums.UserTypeEnum;
import com.yida.data.common.core.enums.attendance.AttendanceTypeEnum;
import com.yida.data.common.core.enums.attendance.AttendanceUserTypeEnum;
import com.yida.data.common.service.CommonService;
import com.yida.data.user.dto.ListStaffDTO;
import com.yida.data.user.dto.ListStudentDTO;
import com.yida.data.user.feign.RemoteStaffService;
import com.yida.data.user.feign.RemoteStudentService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 规则适用部门 Service实现
 *
 * @date 2023-08-09 11:37:25
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
@Slf4j
public class AttendanceRuleObjUserServiceImpl extends ServiceImpl
    <AttendanceRuleObjUserMapper, AttendanceRuleObjUser> implements AttendanceRuleObjUserService {

    private final RemoteStudentService remoteStudentService;
    private final RemoteStaffService remoteStaffService;
    private final CommonService commonService;

    @Override
    public void addRuleUser(List<AttendanceRuleObj> ruleObjList, Long schoolId, Integer ruleType) {
        //先删除原有数据
        remove(Wrappers.lambdaQuery(new AttendanceRuleObjUser())
            .eq(AttendanceRuleObjUser::getRuleId, ruleObjList.get(0).getRuleId()));
        List<AttendanceRuleObjUser> userList = new ArrayList<>();
        //获取本校全部学生
        List<EduStudent> allEduStudentList = new ArrayList<>();
        if (!ObjectUtil.equals(UserTypeEnum.STAFF_USER_TYPE.getType(), ruleType)) {
            ListStudentDTO listStudentDTO = new ListStudentDTO();
            listStudentDTO.setIsQueryUserDept(false);
            listStudentDTO.setIsQueryParent(false);
            listStudentDTO.setIsQuerySchoolName(false);
            listStudentDTO.setSchoolId(schoolId);
            allEduStudentList = remoteStudentService.listStudent(listStudentDTO).getData();
            if (ObjectUtil.equals(AttendanceUserTypeEnum.STUDENT_TO_DORM.getValue(), ruleType)) {
                allEduStudentList = allEduStudentList.stream().filter(student -> ObjectUtil.equals(1, student.getType())
                    && (ObjectUtil.isNull(student.getStudentStatus()) || (ObjectUtil.isNotNull(student.getStatus()) && ObjectUtil
                    .equals(StudentStatusEnum.ZAIDU.getStatus(), student.getStudentStatus()))))
                    .collect(Collectors.toList());
            }
        }
        for (AttendanceRuleObj attendanceRuleObj : ruleObjList) {
            if (ObjectUtil.equals(UserTypeEnum.PARENT_USER_TYPE.getType(), attendanceRuleObj.getUserType())) {
                List<EduStudent> collect = allEduStudentList.stream()
                    .filter(eduStudent -> eduStudent.getClassId().equals(attendanceRuleObj.getDeptId())).collect(
                        Collectors.toList());

                if (CollUtil.isNotEmpty(collect)) {
                    for (EduStudent student : collect) {
                        AttendanceRuleObjUser user = new AttendanceRuleObjUser();
                        user.setDeptId(attendanceRuleObj.getDeptId());
                        user.setRuleId(attendanceRuleObj.getRuleId());
                        user.setUserType(attendanceRuleObj.getUserType());
                        user.setUserId(student.getId());
                        user.setUserName(student.getStuName());
                        userList.add(user);
                    }
                }
            } else {
                //职工
                ListStaffDTO dto = new ListStaffDTO();
                dto.setDeptId(attendanceRuleObj.getDeptId());
                List<EduStaff> eduStaffList = remoteStaffService.listStaff(dto).getData();
                if (CollUtil.isNotEmpty(eduStaffList)) {
                    for (EduStaff staff : eduStaffList) {
                        //判断是否已存在
                        AttendanceRuleObjUser user = new AttendanceRuleObjUser();
                        user.setDeptId(attendanceRuleObj.getDeptId());
                        user.setRuleId(attendanceRuleObj.getRuleId());
                        user.setUserType(attendanceRuleObj.getUserType());
                        user.setUserId(staff.getId());
                        user.setUserName(staff.getName());
                        userList.add(user);
                    }
                }
            }
        }
        if (CollUtil.isNotEmpty(userList)) {
            saveBatch(userList);
        }
    }
}
