package com.yida.data.school.smart.service.impl;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.school.EduDormitory;
import com.yida.data.common.core.entity.school.EduDormitoryRoom;
import com.yida.data.common.core.entity.smart.*;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.user.EduParent;
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.EduUserDept;
import com.yida.data.common.core.enums.EnableStatusEnum;
import com.yida.data.common.core.enums.GuideStepTypeEnum;
import com.yida.data.common.core.enums.RegistrationStatusEnum;
import com.yida.data.common.core.exception.FebsException;
import com.yida.data.common.core.utils.ExcelUtil;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.dto.index.ListDormRoomDTO;
import com.yida.data.school.dto.smart.GuideSaveDTO;
import com.yida.data.school.dto.smart.GuideSelectPageDTO;
import com.yida.data.school.dto.smart.GuideStepSaveDTO;
import com.yida.data.school.dto.smart.UpdateQrCodeInfoDTO;
import com.yida.data.school.facility.service.EduDormitoryRoomService;
import com.yida.data.school.facility.service.EduDormitoryService;
import com.yida.data.school.smart.mapper.EduSmartWelcomeGuideMapper;
import com.yida.data.school.smart.service.*;
import com.yida.data.school.vo.smart.GuideInfoVO;
import com.yida.data.school.vo.smart.GuideSelectPageVO;
import com.yida.data.user.vo.ImportFaceVO;

import java.io.File;
import java.io.IOException;
import java.util.*;

import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.stream.Collectors;

/**
 * 智慧迎新指南表Service实现
 *
 * @author ZYJ
 * @date 2023-05-15 15:33:18
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduSmartWelcomeGuideServiceImpl extends ServiceImpl
    <EduSmartWelcomeGuideMapper, EduSmartWelcomeGuide> implements EduSmartWelcomeGuideService {

    private final EduSmartWelcomeCampusService eduSmartWelcomeCampusService;
    private final EduSmartWelcomeGuideStepService eduSmartWelcomeGuideStepService;
    private final EduSmartWelcomeGuideRosterService eduSmartWelcomeGuideRosterService;
    private final EduSmartWelcomeGuideStepStaffService eduSmartWelcomeGuideStepStaffService;
    private final EduSmartWelcomeGuideRosterRelationStepService eduSmartWelcomeGuideRosterRelationStepService;
    private final EduSmartWelcomeGuideStepClassService eduSmartWelcomeGuideStepClassService;
    private final EduSmartWelcomeGuideStepDormService eduSmartWelcomeGuideStepDormService;
    private final EduSmartWelcomeGuideStepClassFixService eduSmartWelcomeGuideStepClassFixService;
    private final EduSmartWelcomeDepartmentService eduSmartWelcomeDepartmentService;
    private final EduSmartWelcomeGuideStepDormFixService eduSmartWelcomeGuideStepDormFixService;
    private final RedisService redisService;
    private final EduDormitoryRoomService eduDormitoryRoomService;
    @Value("${febs.uploadUrl}")
    private String uploadUrl;

    @Override
    public IPage<GuideSelectPageVO> listGuidePage(GuideSelectPageDTO dto) {
        // 登陆用户信息
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        dto.setDeptId(currentUser.getDeptId());

        IPage<GuideSelectPageVO> page = baseMapper.listGuidePage(dto.toPage(), dto);
        // 统计报名人数问题
        for (GuideSelectPageVO record : page.getRecords()) {
            // 花名册总人数
            long totalNumber = eduSmartWelcomeGuideRosterService.count(Wrappers.lambdaQuery(new EduSmartWelcomeGuideRoster())
                .eq(EduSmartWelcomeGuideRoster::getGuideId, record.getId()));
            record.setTotalNumber(totalNumber);
            if (totalNumber > 0) {
                // 查询报名成功人数
                long countedNumber = eduSmartWelcomeGuideRosterService
                        .count(Wrappers.lambdaQuery(new EduSmartWelcomeGuideRoster())
                        .eq(EduSmartWelcomeGuideRoster::getGuideId, record.getId())
                        .eq(EduSmartWelcomeGuideRoster::getRegistrationStatus, RegistrationStatusEnum.REGISTRATION_SUCCESS.getStatus())
                );
                record.setCompleteNumber(countedNumber);
            }
            // 校区信息
            if (Objects.nonNull(record.getCampusId())) {
                EduSmartWelcomeCampus campus = eduSmartWelcomeCampusService.getById(record.getCampusId());
                Optional.ofNullable(campus).ifPresent(x -> record.setCampusName(x.getCampusName()));
            }
        }
        return page;
    }

    @Override
    public GuideInfoVO getGuideInfo(Long guideId) {
        EduSmartWelcomeGuide welcomeGuide = getById(guideId);
        GuideInfoVO guideInfoVO = new GuideInfoVO();
        if (Objects.nonNull(welcomeGuide)) {
            BeanUtils.copyProperties(welcomeGuide, guideInfoVO);
            // 查询步骤信息
            List<EduSmartWelcomeGuideStep> stepList = eduSmartWelcomeGuideStepService
                .list(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStep())
                    .eq(EduSmartWelcomeGuideStep::getGuideId, welcomeGuide.getId()));
            if (CollUtil.isNotEmpty(stepList)) {
                for (EduSmartWelcomeGuideStep guideStep : stepList) {
                    if (ObjectUtil.equals(GuideStepTypeEnum.EXAMINE_TYPE.getType(), guideStep.getStepType())
                        || ObjectUtil.equals(GuideStepTypeEnum.CLASS.getType(), guideStep.getStepType())
                        || ObjectUtil.equals(GuideStepTypeEnum.DORM.getType(), guideStep.getStepType())) {
                        List<Long> staffIdList = eduSmartWelcomeGuideStepStaffService
                            .list(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepStaff())
                                .eq(EduSmartWelcomeGuideStepStaff::getStepId, guideStep.getId()))
                            .stream().map(EduSmartWelcomeGuideStepStaff::getStaffId).collect(Collectors.toList());
                        guideStep.setStaffIdList(staffIdList);
                    }

                    if (ObjectUtil.equals(GuideStepTypeEnum.CLASS.getType(), guideStep.getStepType())) {
                        List<EduSmartWelcomeGuideStepClass> classList = eduSmartWelcomeGuideStepClassService
                            .list(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepClass())
                                .eq(EduSmartWelcomeGuideStepClass::getGuideId, guideId)
                                .eq(EduSmartWelcomeGuideStepClass::getStepId, guideStep.getId()));

                        List<EduSmartWelcomeGuideStepClassFix> classFixList = eduSmartWelcomeGuideStepClassFixService
                            .list(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepClassFix())
                                .eq(EduSmartWelcomeGuideStepClassFix::getGuideId, guideId)
                                .eq(EduSmartWelcomeGuideStepClassFix::getStepId, guideStep.getId()));
                        guideStep.setClassList(classList);
                        guideStep.setClassFixList(classFixList);
                    }
                    if (ObjectUtil.equals(GuideStepTypeEnum.DORM.getType(), guideStep.getStepType())) {
                        List<EduSmartWelcomeGuideStepDorm> dormList = eduSmartWelcomeGuideStepDormService
                            .list(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepDorm())
                                .eq(EduSmartWelcomeGuideStepDorm::getGuideId, guideId)
                                .eq(EduSmartWelcomeGuideStepDorm::getStepId, guideStep.getId()));
                        guideStep.setDormList(dormList);
                        List<EduSmartWelcomeGuideStepDormFix> dormFixList = eduSmartWelcomeGuideStepDormFixService
                            .list(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepDormFix())
                                .eq(EduSmartWelcomeGuideStepDormFix::getGuideId, guideId)
                                .eq(EduSmartWelcomeGuideStepDormFix::getStepId, guideStep.getId()));
                        guideStep.setDormFixList(dormFixList);
                    }
                }
                guideInfoVO.setStepList(stepList);
            }
        }
        return guideInfoVO;
    }

    @Override
    public void saveGuide(GuideSaveDTO dto) {
        EduSmartWelcomeGuide guide = new EduSmartWelcomeGuide();
        BeanUtils.copyProperties(dto, guide);
        if (Objects.isNull(dto.getId())) {
            // 新增
            guide.setDeptId(FebsUtil.getTopDeptId());
        } else {
            // 删除附表数据
            eduSmartWelcomeGuideStepService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStep())
                .eq(EduSmartWelcomeGuideStep::getGuideId, dto.getId()));
            eduSmartWelcomeGuideStepStaffService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepStaff())
                .eq(EduSmartWelcomeGuideStepStaff::getGuideId, dto.getId()));
            eduSmartWelcomeGuideStepClassService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepClass())
                .eq(EduSmartWelcomeGuideStepClass::getGuideId, dto.getId()));
            eduSmartWelcomeGuideStepClassFixService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepClassFix())
                .eq(EduSmartWelcomeGuideStepClassFix::getGuideId, dto.getId()));
            eduSmartWelcomeGuideStepDormService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepDorm())
                .eq(EduSmartWelcomeGuideStepDorm::getGuideId, dto.getId()));
            eduSmartWelcomeGuideStepDormFixService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepDormFix())
                .eq(EduSmartWelcomeGuideStepDormFix::getGuideId, dto.getId()));
        }
        saveOrUpdate(guide);
        // 步骤信息集合
        List<EduSmartWelcomeGuideStep> stepList = new ArrayList<>();
        if (CollUtil.isNotEmpty(dto.getStepList())) {
            // 保存步骤信息
            for (GuideStepSaveDTO stepSaveDTO : dto.getStepList()) {
                EduSmartWelcomeGuideStep guideStep = new EduSmartWelcomeGuideStep();
                BeanUtils.copyProperties(stepSaveDTO, guideStep);
                guideStep.setDeptId(FebsUtil.getTopDeptId());
                guideStep.setGuideId(guide.getId());
                eduSmartWelcomeGuideStepService.save(guideStep);
                stepList.add(guideStep);
                // 处理审核人员数据
                if (CollUtil.isNotEmpty(stepSaveDTO.getStaffIdList())) {
                    for (Long staffId : stepSaveDTO.getStaffIdList()) {
                        EduSmartWelcomeGuideStepStaff stepStaff = new EduSmartWelcomeGuideStepStaff();
                        stepStaff.setDeptId(FebsUtil.getTopDeptId());
                        stepStaff.setGuideId(guide.getId());
                        stepStaff.setStepId(guideStep.getId());
                        stepStaff.setStaffId(staffId);
                        eduSmartWelcomeGuideStepStaffService.save(stepStaff);
                    }
                }
                //处理预分班级数据
                if (ObjectUtil.equals(GuideStepTypeEnum.CLASS.getType(), guideStep.getStepType())) {
                    if (CollUtil.isNotEmpty(stepSaveDTO.getClassList())) {
                        List<EduSmartWelcomeDepartment> departmentList = eduSmartWelcomeDepartmentService.list();
                        List<EduSmartWelcomeGuideStepClass> classList = new ArrayList<>();
                        for (EduSmartWelcomeGuideStepClass eduSmartWelcomeGuideStepClass : stepSaveDTO.getClassList()) {
                            eduSmartWelcomeGuideStepClass.setId(null);
                            eduSmartWelcomeGuideStepClass.setGuideId(guide.getId());
                            eduSmartWelcomeGuideStepClass.setStepId(guideStep.getId());
                            eduSmartWelcomeGuideStepClass.setDeptId(FebsUtil.getTopDeptId());
                            eduSmartWelcomeGuideStepClass.setSchoolId(FebsUtil.getTopDeptId());
                            int total = eduSmartWelcomeGuideStepClass.getMale() + eduSmartWelcomeGuideStepClass.getFemale();
//                            int femaleNumber =
//                                eduSmartWelcomeGuideStepClass.getMale() * (eduSmartWelcomeGuideStepClass.getMale() / total);
//                            int maleNumber =
//                                eduSmartWelcomeGuideStepClass.getFemaleNumber() * (eduSmartWelcomeGuideStepClass.getFemaleNumber()
//                                    / total);
                            eduSmartWelcomeGuideStepClass.setFemaleNumber(eduSmartWelcomeGuideStepClass.getFemale());
                            eduSmartWelcomeGuideStepClass.setMaleNumber(eduSmartWelcomeGuideStepClass.getMale());
                            classList.add(eduSmartWelcomeGuideStepClass);
                        }

                        if (CollUtil.isNotEmpty(classList)) {
                            eduSmartWelcomeGuideStepClassService.saveBatch(classList);
                        }
                    }

                    if (CollUtil.isNotEmpty(stepSaveDTO.getClassFixList())) {
                        List<EduSmartWelcomeGuideStepClassFix> classFixList = new ArrayList<>();
                        for (EduSmartWelcomeGuideStepClassFix eduSmartWelcomeGuideStepClassFix : stepSaveDTO.getClassFixList()) {
                            eduSmartWelcomeGuideStepClassFix.setGuideId(guide.getId());
                            eduSmartWelcomeGuideStepClassFix.setStepId(guideStep.getId());
                            eduSmartWelcomeGuideStepClassFix.setDeptId(FebsUtil.getTopDeptId());
                            eduSmartWelcomeGuideStepClassFix.setSchoolId(FebsUtil.getTopDeptId());
                            eduSmartWelcomeGuideStepClassFix.setId(null);
                            //查询人员ID
                            EduSmartWelcomeGuideRoster roster = eduSmartWelcomeGuideRosterService
                                .getOne(Wrappers.lambdaQuery(new EduSmartWelcomeGuideRoster())
                                    .eq(EduSmartWelcomeGuideRoster::getGuideId, guide.getId())
                                    .eq(EduSmartWelcomeGuideRoster::getIdNumber, eduSmartWelcomeGuideStepClassFix.getIdNumber())
                                    .eq(EduSmartWelcomeGuideRoster::getFullName,
                                        eduSmartWelcomeGuideStepClassFix.getRosterName()));
                            if (ObjectUtil.isNotNull(roster)) {
                                eduSmartWelcomeGuideStepClassFix.setRosterId(roster.getId());
                                classFixList.add(eduSmartWelcomeGuideStepClassFix);
                            }
                        }
                        if (CollUtil.isNotEmpty(classFixList)) {
                            eduSmartWelcomeGuideStepClassFixService.saveBatch(classFixList);
                        }
                    }
                }

                //处理预分宿舍数据
                if (ObjectUtil.equals(GuideStepTypeEnum.DORM.getType(), guideStep.getStepType())) {
                    if (CollUtil.isNotEmpty(stepSaveDTO.getDormList())) {
                        List<EduSmartWelcomeGuideStepDorm> dormList = new ArrayList<>();
                        for (EduSmartWelcomeGuideStepDorm eduSmartWelcomeGuideStepDorm : stepSaveDTO.getDormList()) {
                            eduSmartWelcomeGuideStepDorm.setGuideId(guide.getId());
                            eduSmartWelcomeGuideStepDorm.setStepId(guideStep.getId());
                            eduSmartWelcomeGuideStepDorm.setDeptId(FebsUtil.getTopDeptId());
                            eduSmartWelcomeGuideStepDorm.setSchoolId(FebsUtil.getTopDeptId());
                            eduSmartWelcomeGuideStepDorm.setId(null);
                            //增加宿舍床位总数
                            EduDormitoryRoom dormitoryRoom = eduDormitoryRoomService
                                .getById(eduSmartWelcomeGuideStepDorm.getDormRoomId());
                            eduSmartWelcomeGuideStepDorm.setNumber(dormitoryRoom.getBedNum());

                            dormList.add(eduSmartWelcomeGuideStepDorm);
                        }
                        if (CollUtil.isNotEmpty(dormList)) {
                            eduSmartWelcomeGuideStepDormService.saveBatch(dormList);
                        }
                    }
                    if (CollUtil.isNotEmpty(stepSaveDTO.getDormFixList())) {
                        List<EduSmartWelcomeGuideStepDormFix> dormFixList = new ArrayList<>();
                        for (EduSmartWelcomeGuideStepDormFix eduSmartWelcomeGuideStepDormFix : stepSaveDTO.getDormFixList()) {
                            eduSmartWelcomeGuideStepDormFix.setGuideId(guide.getId());
                            eduSmartWelcomeGuideStepDormFix.setStepId(guideStep.getId());
                            eduSmartWelcomeGuideStepDormFix.setDeptId(FebsUtil.getTopDeptId());
                            eduSmartWelcomeGuideStepDormFix.setSchoolId(FebsUtil.getTopDeptId());
                            eduSmartWelcomeGuideStepDormFix.setId(null);
                            dormFixList.add(eduSmartWelcomeGuideStepDormFix);
                        }
                        if (CollUtil.isNotEmpty(dormFixList)) {
                            eduSmartWelcomeGuideStepDormFixService.saveBatch(dormFixList);
                        }
                    }
                }

            }
            // 再次循环处理前提步骤信息
            for (GuideStepSaveDTO stepSaveDTO : dto.getStepList()) {
                if (StringUtils.isNotBlank(stepSaveDTO.getPremiseStepUniqueId())) {
                    // 需要保存的基础信息
                    List<EduSmartWelcomeGuideStep> baseList = stepList.stream()
                        .filter(x -> x.getUniqueId().equals(stepSaveDTO.getUniqueId())).collect(Collectors.toList());
                    if (CollUtil.isEmpty(baseList)) {
                        continue;
                    }
                    // 前提步骤
                    List<EduSmartWelcomeGuideStep> premiseList = stepList.stream()
                        .filter(x -> x.getUniqueId().equals(stepSaveDTO.getPremiseStepUniqueId())).collect(Collectors.toList());
                    if (CollUtil.isEmpty(premiseList)) {
                        continue;
                    }
                    EduSmartWelcomeGuideStep base = baseList.get(0);
                    base.setPremiseStepId(premiseList.get(0).getId());
                    eduSmartWelcomeGuideStepService.updateById(base);
                }
            }
        }
    }

    @Override
    public void deleteGuide(Long guideId) {
        // 删除附表数据
        eduSmartWelcomeGuideStepService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStep())
            .eq(EduSmartWelcomeGuideStep::getGuideId, guideId));
        eduSmartWelcomeGuideStepStaffService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepStaff())
            .eq(EduSmartWelcomeGuideStepStaff::getGuideId, guideId));
        eduSmartWelcomeGuideStepClassService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepClass())
            .eq(EduSmartWelcomeGuideStepClass::getGuideId, guideId));
        eduSmartWelcomeGuideStepClassFixService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepClassFix())
            .eq(EduSmartWelcomeGuideStepClassFix::getGuideId, guideId));
        eduSmartWelcomeGuideStepDormService.remove(Wrappers.lambdaQuery(new EduSmartWelcomeGuideStepDorm())
            .eq(EduSmartWelcomeGuideStepDorm::getGuideId, guideId));
        // TODO 2023/5/15 需要处理人员数据

        removeById(guideId);
    }

    @Override
    public void updateGuideEnableStatus(Long guideId, Integer enableStatus) {
        EduSmartWelcomeGuide welcomeGuide = getById(guideId);
        if (Objects.isNull(welcomeGuide) || Objects.isNull(welcomeGuide.getCampusId())) {
            throw new FebsException("迎新指南信息错误！");
        }
        // 判断当前已经启用的指南数量
        long count = count(Wrappers.lambdaQuery(new EduSmartWelcomeGuide())
            .eq(EduSmartWelcomeGuide::getDeptId, FebsUtil.getDeptId())
            .eq(EduSmartWelcomeGuide::getCampusId, welcomeGuide.getCampusId())
            .eq(EduSmartWelcomeGuide::getEnableStatus, EnableStatusEnum.ENABLE_STATUS.getStatus())
        );
        if (EnableStatusEnum.ENABLE_STATUS.getStatus().equals(enableStatus) && count >= 1) {
            throw new FebsException("每个校区只能同时启用一个迎新指南");
        }
        EduSmartWelcomeGuide guide = new EduSmartWelcomeGuide();
        guide.setId(guideId);
        guide.setEnableStatus(enableStatus);
        guide.setLastEnableDate(LocalDateTime.now());
        saveOrUpdate(guide);
    }

    @Override
    public void updateQrCodeInfo(UpdateQrCodeInfoDTO dto) {
        EduSmartWelcomeGuide guide = new EduSmartWelcomeGuide();
        guide.setId(dto.getGuideId());
        guide.setQrCodeEnableStatus(dto.getQrCodeEnableStatus());
        guide.setQrCodeNoticeMessage(dto.getQrCodeNoticeMessage());
        saveOrUpdate(guide);
    }

    @Override
    public void uploadStudentAvatarBatch(String file, Long guideId, Long createUserId) {
        int total;
        int finish = 0;
        int percent = 0;
        // 上传信息
        ImportFaceVO faceVO = new ImportFaceVO();
        faceVO.setFinish(0);
        faceVO.setFileName(cn.hutool.core.io.FileUtil.getName(file));
        faceVO.setPercent(0);
        redisService.set(CachePrefixConstant.IMPORT_GUIDE_AVATAR_KEY + createUserId.toString(), faceVO);
        try {
            // 解压文件到本地
            File zipFile = new File(file);
            ZipUtil.unzip(zipFile, CharsetUtil.charset(CharsetUtil.GBK));
            // 遍历图片
            String imgPath;
            String zipPath = zipFile.getAbsolutePath();
            List<File> files = cn.hutool.core.io.FileUtil.loopFiles(zipPath.split("\\.")[0]);
            total = files.size();
            // 无法导入的图片
            List<File> errorFiles = new ArrayList<>();
            for (File img : files) {
                String userFlag = img.getName().split("\\.")[0];
                // 按身份证号/名字导入
                EduSmartWelcomeGuideRoster roster = eduSmartWelcomeGuideRosterService
                    .getOne(Wrappers.lambdaQuery(new EduSmartWelcomeGuideRoster())
                        .eq(EduSmartWelcomeGuideRoster::getIdNumber, userFlag)
                        .eq(EduSmartWelcomeGuideRoster::getGuideId, guideId));
                if (ObjectUtil.isNull(roster)) {
                    List<EduSmartWelcomeGuideRoster> rosterList = eduSmartWelcomeGuideRosterService
                        .list(Wrappers.lambdaQuery(new EduSmartWelcomeGuideRoster())
                            .eq(EduSmartWelcomeGuideRoster::getFullName, userFlag)
                            .eq(EduSmartWelcomeGuideRoster::getGuideId, guideId));
                    if (CollUtil.isNotEmpty(rosterList)) {
                        if (rosterList.size() > 1) {
                            errorFiles.add(img);
                            continue;
                        } else {
                            roster = rosterList.get(0);
                        }
                    }
                }

                if (ObjectUtil.isNull(roster)) {
                    errorFiles.add(img);
                    continue;
                }
                // 压缩图片至1000KB
                File compressImg = null;
                try {
                    compressImg = com.yida.data.common.core.utils.FileUtil.compressImg(img, 100L);
                } catch (Exception e) {
                    log.error("压缩图片失败", e);
                    errorFiles.add(img);
                    continue;
                }

                imgPath = com.yida.data.common.core.utils.FileUtil.uploadFileToMediaServer(uploadUrl, compressImg);

                eduSmartWelcomeGuideRosterService.update(Wrappers.lambdaUpdate(new EduSmartWelcomeGuideRoster())
                    .eq(EduSmartWelcomeGuideRoster::getId, roster.getId())
                    .set(EduSmartWelcomeGuideRoster::getAvatar, imgPath));

                // TODO: 2023/10/18 后续优化花名册缓存信息
                // 更新缓存
//                redisService.hset(CachePrefixConstant.WELCOME_ROSTER, roster.getId().toString(),
//                    eduSmartWelcomeGuideRosterService.getById(roster.getId()));

                // 进度增加更新进度
                if (finish * 100 / total > percent) {
                    faceVO.setPercent(finish * 100 / total);
                    redisService.set(CachePrefixConstant.IMPORT_GUIDE_AVATAR_KEY + createUserId, faceVO);
                }
                finish++;
            }
            // 打包错误文件
            if (CollUtil.isNotEmpty(errorFiles)) {
                String failPath = com.yida.data.common.core.utils.FileUtil.getRandomPath("zip");
                ZipUtil.zip(new File(failPath), false, errorFiles.toArray(new File[0]));
                faceVO.setFailPath(failPath);
            }
        } catch (Exception e) {
            log.error("上传出现异常", e);
        } finally {
            faceVO.setFinish(1);
            redisService.set(CachePrefixConstant.IMPORT_GUIDE_AVATAR_KEY + createUserId, faceVO);
        }
    }
}
