package com.fs.sop.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.common.exception.base.BaseException;
import com.fs.common.utils.PubFun;
import com.fs.common.utils.StringUtils;
import com.fs.course.config.CourseConfig;
import com.fs.course.domain.FsUserCourse;
import com.fs.course.domain.FsUserCourseVideo;
import com.fs.course.domain.FsUserCourseVideoRedPackage;
import com.fs.course.mapper.FsUserCourseMapper;
import com.fs.course.mapper.FsUserCourseVideoMapper;
import com.fs.course.service.IFsUserCourseVideoRedPackageService;
import com.fs.course.vo.FsCourseRedPacketLogListPVO;
import com.fs.fastGpt.domain.FastGptChatReplaceWords;
import com.fs.fastGpt.mapper.FastGptChatReplaceWordsMapper;
import com.fs.qw.vo.QwSopTempSetting2;
import com.fs.qw.vo.SortDayVo;
import com.fs.sop.domain.QwSopTemp;
import com.fs.sop.domain.QwSopTempContent;
import com.fs.sop.domain.QwSopTempDay;
import com.fs.sop.domain.QwSopTempRules;
import com.fs.sop.mapper.QwSopTempMapper;
import com.fs.sop.params.QwSopShareTempParam;
import com.fs.sop.service.*;
import com.fs.sop.util.TimeCalculator;
import com.fs.sop.vo.QwSopTempRedPackageVo;
import com.fs.sop.vo.VoiceVo;
import com.fs.system.domain.SysConfig;
import com.fs.system.service.ISysConfigService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * sop模板Service业务层处理
 *
 * @author fs
 * @date 2024-09-26
 */
@Service
@AllArgsConstructor
public class QwSopTempServiceImpl implements IQwSopTempService {
    private final QwSopTempMapper qwSopTempMapper;
    private final FastGptChatReplaceWordsMapper fastGptChatReplaceWordsMapper;
    private final IQwSopTempRulesService qwSopTempRulesService;
    private final IQwSopTempDayService qwSopTempDayService;
    private final IQwSopTempContentService qwSopTempContentService;
    private final IQwSopTempVoiceService qwSopTempVoiceService;
    private final RocketMQTemplate rocketMQTemplate;
    private final FsUserCourseMapper fsUserCourseMapper;
    private final FsUserCourseVideoMapper fsUserCourseVideoMapper;
    private final IFsUserCourseVideoRedPackageService fsUserCourseVideoRedPackageService;
    private final ISysConfigService sysConfigService;

    /**
     * 查询sop模板
     *
     * @param id sop模板主键
     * @return sop模板
     */
    @Override
    public QwSopTemp selectQwSopTempById(String id) {
//        QwSopTemp qwSopTemp = qwSopTempMapper.selectQwSopTempById(id);
        QwSopTemp qwSopTemp = qwSopTempMapper.selectQwSopTempById(id);
        if (qwSopTemp == null) return null;
        List<QwSopTempDay> qwSopTempDays = qwSopTempDayService.listByTempId(id);
        qwSopTemp.setList(qwSopTempDays);
//        List<QwSopTempRules> rulesList = qwSopTempRulesService.listByTempId(id);
//        if(!rulesList.isEmpty()){
//            Map<Integer, List<QwSopTempRules>> ruleDayMap = PubFun.listToMapByGroupList(rulesList, QwSopTempRules::getDayNum);
//            qwSopTemp.setRules(ruleDayMap.values().stream().map(qwSopTempRules -> {
//                Map<String, Object> map = new HashMap<>();
//                QwSopTempRules rules = qwSopTempRules.get(0);
//                map.put("name", rules.getName());
//                map.put("id", rules.getTempId());
//                map.put("dayNum", rules.getDayNum());
////                map.put("content", qwSopTempRules);
//                return map;
//            }).collect(Collectors.toList()));
//        }else{
//            qwSopTemp.setRules(new ArrayList<>());
//        }
        return qwSopTemp;
    }

    /**
     * 查询sop模板列表
     *
     * @param qwSopTemp sop模板
     * @return sop模板
     */
    @Override
    public List<QwSopTemp> selectQwSopTempList(QwSopTemp qwSopTemp) {
        return qwSopTempMapper.selectQwSopTempList(qwSopTemp);
    }

    /**
     * 新增sop模板
     *
     * @param qwSopTemp sop模板
     * @return 结果
     */
    @Override
    public int insertQwSopTemp(QwSopTemp qwSopTemp) {
        qwSopTemp.setId(UUID.randomUUID().toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        qwSopTemp.setCreateTime(sdf.format(new Date()));
        return qwSopTempMapper.insert(qwSopTemp);
    }

    // 提取通用替换逻辑的方法
    private void replaceContent(String contentType, String content, Consumer<String> setter, List<FastGptChatReplaceWords> words) {
        if (contentType != null && content != null && !content.isEmpty()) {
            for (FastGptChatReplaceWords word : words) {
                if (content.contains(word.getContent())) {
                    // 如果包含目标字段，则替换
                    setter.accept(content.replace(word.getContent(), word.getChangeCount()));
                }
            }
        }
    }

    /**
     * 修改sop模板
     *
     * @param qwSopTemp sop模板
     * @return 结果
     */
    @Override
    public int updateQwSopTemp(QwSopTemp qwSopTemp) {
        return qwSopTempMapper.updateQwSopTemp(qwSopTemp);
    }

    /**
     * 批量删除sop模板
     *
     * @param ids 需要删除的sop模板主键
     * @return 结果
     */
    @Override
    public int deleteQwSopTempByIds(String[] ids) {
        return qwSopTempMapper.deleteQwSopTempByIds(ids);
    }

    @Override
    public int shareQwSopTemp(QwSopShareTempParam param) {
        QwSopTemp qwSopTemp = qwSopTempMapper.selectQwSopTempById(param.getTempleId());
        Arrays.stream(param.getCompanyIds()).forEach(companyId -> {
            qwSopTemp.setCompanyId(companyId);
            copyTemplate(qwSopTemp);
        });
        return 1;
    }

    @Override
    public int updateQwSopTempByIds(String[] ids) {
        return qwSopTempMapper.updateQwSopTempByIds(ids);
    }

    /**
     * 删除sop模板信息
     *
     * @param id sop模板主键
     * @return 结果
     */
    @Override
    public int deleteQwSopTempById(String id) {
        return qwSopTempMapper.deleteQwSopTempById(id);
    }


    @Override
    public int addNew(QwSopTemp qwSopTemp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        qwSopTemp.setCreateTime(sdf.format(new Date()));
        qwSopTemp.setId(UUID.randomUUID().toString());
        return qwSopTempMapper.insertQwSopTemp(qwSopTemp);
    }

    @Override
    public void createSopTempRules(QwSopTemp temp) {
        FsUserCourse fsUserCourse = fsUserCourseMapper.selectFsUserCourseByCourseId(temp.getCourseId());
        temp.setProject(fsUserCourse.getProject());
        qwSopTempMapper.updateQwSopTemp(temp);
        List<FsUserCourseVideo> videoList = fsUserCourseVideoMapper.selectVideoByCourseId(fsUserCourse.getCourseId());
        AtomicInteger i = new AtomicInteger(1);
        Integer gap = temp.getGap();
        List<QwSopTempDay> collect = videoList.stream().map(e -> {
            QwSopTempDay day = new QwSopTempDay();
            day.setTempId(temp.getId());
            day.setDayNum(i.getAndIncrement() * gap);
            day.setName("第" + day.getDayNum() + "天");
            day.setSorts(day.getDayNum());
            day.setList(new ArrayList<>());
            List<String> timeList = new ArrayList<>();
            if (temp.getTimeList() != null) {
                timeList = JSON.parseArray(JSON.toJSONString(temp.getTimeList()), String.class);
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
            if(temp.getTime() != null){
                timeList.add(0, temp.getTime().format(formatter));
            }
            AtomicInteger sorts = new AtomicInteger(0);
            List<QwSopTempRules> rulesList = timeList.stream().map(time -> {
                QwSopTempRules rules = new QwSopTempRules();
                rules.setTempId(temp.getId());
                rules.setName(day.getName());
                rules.setTime(time);
                rules.setContentType(2);
                rules.setType(2);
                rules.setCourseType(0);
                rules.setCourseId(e.getCourseId());
                rules.setVideoId(e.getVideoId());
                rules.setSorts(sorts.getAndIncrement());
                // 设置消息类型
                if (rules.getSorts() == 0) {
                    rules.setCourseType(0);
                } else if (rules.getSorts() == 1) {
                    rules.setCourseType(1);
                } else {
                    rules.setCourseType(4);
                }
                QwSopTempContent content = new QwSopTempContent();
                content.setTempId(temp.getId());
                content.setContentType(3);
                QwSopTempSetting2.Content.Setting setting = new QwSopTempSetting2.Content.Setting();
                setting.setLinkTitle(fsUserCourse.getCourseName());
                setting.setIsBindUrl(1);
                setting.setLinkDescribe(e.getTitle());
                setting.setLinkImageUrl(fsUserCourse.getImgUrl());
                setting.setContentType("3");
                content.setContent(JSON.toJSONString(setting));
                content.setIsBindUrl(1);
                rules.setList(Collections.singletonList(content));
                return rules;
            }).collect(Collectors.toList());
            day.getList().addAll(rulesList);
            return day;
        }).collect(Collectors.toList());
        qwSopTempDayService.addOrUpdateBatch(collect);
        List<QwSopTempRules> ruleList = collect.stream().flatMap(e -> e.getList().stream().peek(r -> r.setDayId(e.getId()))).collect(Collectors.toList());
        qwSopTempRulesService.saveList(ruleList);
        List<QwSopTempContent> contentList = ruleList.stream().flatMap(e -> e.getList().stream().peek(r -> {
            r.setDayId(e.getDayId());
            r.setRulesId(e.getId());
        })).collect(Collectors.toList());
        qwSopTempContentService.saveList(contentList);
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("course.config");
        CourseConfig courseConfig = JSON.parseObject(sysConfig.getConfigValue(), CourseConfig.class);
        List<Long> videoIdList = PubFun.listToNewList(ruleList, QwSopTempRules::getVideoId);
        Map<Long, Optional<BigDecimal>> redMap;
        if (!videoIdList.isEmpty()) {
            List<FsUserCourseVideoRedPackage> redPackageList = fsUserCourseVideoRedPackageService.listByCompanyIdAndVideoIds(temp.getCompanyId(), videoIdList);
            redMap = redPackageList.stream().collect(Collectors.groupingBy(FsUserCourseVideoRedPackage::getVideoId, Collectors.mapping(FsUserCourseVideoRedPackage::getRedPacketMoney, Collectors.reducing((e1, e2) -> e1))));
        } else {
            redMap = new HashMap<>();
        }
        FsUserCourseVideoRedPackage red = new FsUserCourseVideoRedPackage();
        List<FsUserCourseVideoRedPackage> redPackage = ruleList.stream().filter(e -> e.getVideoId() != null && e.getSorts() == 0).map(e -> {
            red.setCompanyId(temp.getCompanyId());
            red.setVideoId(e.getVideoId());
            red.setRedPacketMoney(redMap.getOrDefault(e.getVideoId(), Optional.of(courseConfig.getRedPackageMoney())).orElse(BigDecimal.ZERO));
            red.setRuleId(e.getId());
            red.setDataType(3);
            return red;
        }).collect(Collectors.toList());
        fsUserCourseVideoRedPackageService.batchSaveFsUserCourseVideoRedPackage(redPackage);
    }

    @Override
    public List<QwSopTempRedPackageVo> redList(String id) {
        List<QwSopTempDay> dayList = qwSopTempRulesService.listByTempIdAll(id);
        if (CollectionUtils.isEmpty(dayList)) {
            return Collections.emptyList();
        }
        List<QwSopTempRules> rules = dayList.stream()
                .filter(e -> e != null && e.getList() != null)
                .flatMap(e -> e.getList().stream())
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        if (rules.isEmpty()) {
            return Collections.emptyList();
        }
        List<FsUserCourseVideoRedPackage> redPackageList = fsUserCourseVideoRedPackageService.listByRuleIds(PubFun.listToNewList(rules, QwSopTempRules::getId));
        Map<Long, FsUserCourseVideoRedPackage> redMap = PubFun.listToMapByGroupObject(redPackageList, FsUserCourseVideoRedPackage::getVideoId);
        List<FsUserCourseVideo> videoList = fsUserCourseVideoMapper.selectVideoByCourseId(rules.get(0).getCourseId());
        Map<Long, FsUserCourseVideo> videoMap = PubFun.listToMapByGroupObject(videoList, FsUserCourseVideo::getVideoId);
        return rules.stream()
                .filter(e -> e.getVideoId() != null && redMap.containsKey(e.getVideoId()) && videoMap.containsKey(e.getVideoId())).map(e -> {
                    FsUserCourseVideoRedPackage red = redMap.get(e.getVideoId());
                    FsUserCourseVideo video = videoMap.get(e.getVideoId());
                    QwSopTempRedPackageVo vo = new QwSopTempRedPackageVo();
                    vo.setId(red.getId());
                    vo.setRuleId(e.getId());
                    vo.setName(e.getName());
                    vo.setVideoId(e.getVideoId());
                    vo.setVideoName(video.getTitle());
                    vo.setCompanyId(red.getCompanyId());
                    vo.setRedPacketMoney(red.getRedPacketMoney());
                    vo.setDataType(red.getDataType());
                    return vo;
                }).collect(Collectors.toList());
    }

    @Override
    public void updateRedPackage(List<QwSopTempRedPackageVo> list) {
        fsUserCourseVideoRedPackageService.deleteFsUserCourseVideoRedPackageByIds(list.stream().map(QwSopTempRedPackageVo::getId).toArray(Long[]::new));
        List<FsUserCourseVideoRedPackage> redPackage = list.stream().map(e -> {
            FsUserCourseVideoRedPackage red = new FsUserCourseVideoRedPackage();
            red.setCompanyId(e.getCompanyId());
            red.setVideoId(e.getVideoId());
            red.setRedPacketMoney(e.getRedPacketMoney());
            red.setRuleId(e.getRuleId());
            red.setDataType(3);
            return red;
        }).collect(Collectors.toList());
        fsUserCourseVideoRedPackageService.batchSaveFsUserCourseVideoRedPackage(redPackage);
    }

    @Override
    public List<String> getSelectableRange() {
        SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("course.config");
        CourseConfig courseConfig = JSON.parseObject(sysConfig.getConfigValue(), CourseConfig.class);
        List<CourseConfig.DisabledTimeVo> disabledTimeList = courseConfig.getDisabledTimeList();
        if (disabledTimeList == null) {
            return Collections.emptyList();
        }
        return TimeCalculator.calculateAvailableTimes(disabledTimeList);
    }

    @Override
    @DataSource(DataSourceType.SOP)
    public int update(QwSopTemp qwSopTemp) {
        QwSopTemp temp = qwSopTempMapper.selectById(qwSopTemp.getId());
        int i = qwSopTempMapper.updateById(qwSopTemp);
        if (!Objects.equals(temp.getGap(), qwSopTemp.getGap())) {
            // 重新排序
            reorder(qwSopTemp.getId());
        }
        return i;
    }

    @Override
    public Map<String, Object> addOrUpdateSetting(QwSopTempDay day) {
        List<QwSopTempRules> list = day.getList();
        List<QwSopTempContent> collect = list.stream().flatMap(e -> e.getSettingList().stream()).collect(Collectors.toList());
        List<QwSopTempContent> voiceList = collect.stream().filter(e -> e.getContentType() == 7).collect(Collectors.toList());
        if (!voiceList.isEmpty()) {
//            day.setVoice(1);
        }
        qwSopTempDayService.saveOrUpdate(day);
        qwSopTempRulesService.removeByDayId(day.getId());
        qwSopTempContentService.removeByDayId(day.getId());

        list.forEach(item -> item.setDayId(day.getId()));
        processAndReplaceContent(list);
        QwSopTempRules rules = list.get(0);
        String tempId = rules.getTempId();
        // 批量插入
        qwSopTempRulesService.insertBatch(list);
        // 处理添加列表设置ID
        list.forEach(e -> e.getSettingList().forEach(item -> {
            item.setRulesId(e.getId());
            item.setDayId(e.getDayId());
            item.setTempId(tempId);
        }));
        // 批量插入内容

        qwSopTempContentService.insertBatch(collect);
        reorder(day.getTempId());
        Map<String, Object> map = new HashMap<>();
        map.put("id", day.getId());
        if (!voiceList.isEmpty()) {
            rocketMQTemplate.syncSend("voice-generation", JSON.toJSONString(VoiceVo.builder().type(0).id(day.getId().toString()).build()));
        }

        return map;
    }

    private void processAndReplaceContent(List<QwSopTempRules> tempSettings) {
        List<FastGptChatReplaceWords> words = fastGptChatReplaceWordsMapper.selectAllFastGptChatReplaceWords();
        //循环天
        tempSettings.forEach(settingList -> {
            //循环单日
            settingList.getSettingList().forEach(item -> {
                JSONObject obj = JSON.parseObject(item.getContent());
                List<String> list = Arrays.asList("linkTitle", "linkDescribe", "desc", "nickname", "value");
                list.stream().filter(obj::containsKey).forEach(key -> {
                    replaceContent(item.getContentType().toString(), obj.getString(key), val -> obj.put(key, val), words);
                });
                item.setContent(obj.toJSONString());
            });
        });
    }

    @Override
    public void delRules(Long id) {
        if (id == null) return;
        QwSopTempDay day = qwSopTempDayService.info(id);
        if (day == null) return;
        qwSopTempDayService.removeById(day.getId());
        qwSopTempContentService.removeByDayId(day.getId());
        reorder(day.getTempId());
    }

    @Override
    public QwSopTempDay selectRulesInfo(Long id) {
        QwSopTempDay day = qwSopTempDayService.info(id);
        List<QwSopTempRules> rulesList = qwSopTempRulesService.listByDayId(id);
        if (CollectionUtils.isNotEmpty(rulesList)) {
            List<QwSopTempContent> contentList = qwSopTempContentService.listByRulesIds(PubFun.listToNewList(rulesList, QwSopTempRules::getId));
            Map<Long, List<QwSopTempContent>> contentMap = PubFun.listToMapByGroupList(contentList, QwSopTempContent::getRulesId);
            rulesList.forEach(e -> {
                List<QwSopTempContent> contents = contentMap.get(e.getId());
                if (CollectionUtils.isNotEmpty(contents)) {
                    e.setSetting(contents.stream()
                            .filter(c -> c != null && StringUtils.isNotBlank(c.getContent()))
                            .map(c -> JSON.parseObject(c.getContent())).collect(Collectors.toList()));
                }
            });
        }
        day.setList(rulesList);
        return day;
    }

    @Override
    public List<QwSopTemp> selectQwSopTempListNew(QwSopTemp qwSopTemp) {
        List<QwSopTemp> list = qwSopTempMapper.selectQwSopTempListNew(qwSopTemp);
        if (qwSopTemp.isCuoser()) {
            List<QwSopTempRules> rulesList = qwSopTempRulesService.selectByTemplateIds(PubFun.listToNewList(list, QwSopTemp::getId));
            rulesList.sort(Comparator.comparing(
                    e -> {
                        String timeStr = e.getTime();
                        if (timeStr == null || timeStr.isEmpty()) {
                            return null; // 返回 null 表示该条目需排在最后
                        }
                        return LocalTime.parse(timeStr + ":00"); // 正常解析有效时间
                    },
                    Comparator.nullsLast(Comparator.naturalOrder()) // 处理 null 并排序
            ));
            Map<String, List<QwSopTempRules>> ruleMap = PubFun.listToMapByGroupList(rulesList, QwSopTempRules::getTempId);
            list.stream().filter(e -> ruleMap.containsKey(e.getId())).forEach(e -> {
                List<QwSopTempRules> rulesListSub = ruleMap.get(e.getId());
                Map<Long, QwSopTempRules> dayMap = PubFun.listToMapByGroupObject(rulesListSub, QwSopTempRules::getDayId);
                e.setCuoser(dayMap.entrySet().stream().allMatch(d -> d.getValue().getContentType() == 2));
            });
        }
        return list;
    }

    @Override
    public void copyTemplate(QwSopTemp qwSopTemp) {
        String oldId = qwSopTemp.getId();
        String newId = UUID.randomUUID().toString();
        qwSopTemp.setId(newId);
        qwSopTempMapper.insertQwSopTemp(qwSopTemp);
        List<QwSopTempDay> dayList = qwSopTempRulesService.listByTempIdAll(oldId);
        if (dayList.isEmpty()) return;
        List<QwSopTempRules> ruleList = dayList.stream().filter(e -> e.getList() != null).flatMap(e -> e.getList().stream()).collect(Collectors.toList());
        if (ruleList.isEmpty()) return;
        List<FsUserCourseVideoRedPackage> redList = fsUserCourseVideoRedPackageService.selectByRuleIds(PubFun.listToNewList(ruleList, QwSopTempRules::getId));
        Map<Long, FsUserCourseVideoRedPackage> redMap = PubFun.listToMapByGroupObject(redList, FsUserCourseVideoRedPackage::getRuleId);
        dayList.forEach(day -> {
            day.setTempId(newId);
            if (day.getList() != null && !day.getList().isEmpty()) {
                day.getList().forEach(e -> {
                    e.setRed(redMap.get(e.getId()));
                    e.setTempId(newId);
                    if (e.getSettingList() != null && !e.getSettingList().isEmpty()) {
                        e.getSettingList().forEach(item -> {
                            item.setTempId(newId);
                        });
                    }
                });
            }
        });
        qwSopTempDayService.insertBatch(dayList);
        dayList.forEach(e -> e.getList().forEach(item -> item.setDayId(e.getId())));
        List<QwSopTempRules> collect = dayList.stream().flatMap(e -> e.getList().stream()).collect(Collectors.toList());
        qwSopTempRulesService.insertBatch(collect);
        collect.forEach(e -> e.getSettingList().forEach(item -> {
            item.setRulesId(e.getId());
            item.setDayId(e.getDayId());
        }));
        qwSopTempContentService.insertBatch(collect.stream().flatMap(e -> e.getSettingList().stream()).collect(Collectors.toList()));
        List<FsUserCourseVideoRedPackage> redSaveList = collect.stream().filter(e -> e.getRed() != null).peek(e -> e.getRed().setRuleId(e.getId())).map(QwSopTempRules::getRed).collect(Collectors.toList());
        if (!redSaveList.isEmpty()) {
            fsUserCourseVideoRedPackageService.batchSaveCompanyRedPackage(redSaveList);
        }
    }

    @Override
    @DataSource(DataSourceType.SOP)
    public List<QwSopTemp> listTemp() {
        return qwSopTempMapper.selectList(new QueryWrapper<>());
    }

    @Override
    @DataSource(DataSourceType.SOP)
    public void sortDay(List<SortDayVo> list) {
        Collection<QwSopTempDay> days = qwSopTempDayService.listByIds(PubFun.listToNewList(list, SortDayVo::getId));
        Map<Long, Integer> dayMap = list.stream().collect(Collectors.toMap(SortDayVo::getId, SortDayVo::getDayNum));
        if (days.stream().anyMatch(e -> !dayMap.containsKey(e.getId()))) {
            throw new BaseException("数据错误！");
        }
        days.forEach(day -> day.setDayNum(dayMap.get(day.getId())));
        qwSopTempDayService.updateBatchById(days);
    }

    @Override
    public List<QwSopTempDay> dayList(String id) {
        return qwSopTempDayService.listByTempId(id);
    }


    @DataSource(DataSourceType.SOP)
    private void reorder(String tempId) {
        QwSopTemp qwSopTemp = qwSopTempMapper.selectQwSopTempById(tempId);
        List<QwSopTempDay> days = qwSopTempDayService.listByTempId(tempId);
        if (days.isEmpty()) return;
        for (int i = 0; i < days.size(); i++) {
            QwSopTempDay entity = days.get(i);
            entity.setSorts(i);
            entity.setDayNum((entity.getSorts() * qwSopTemp.getGap()) + 1);
        }
        qwSopTempDayService.addOrUpdateBatch(days);
    }
}
