package com.ruoyi.project.training.bccnew.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.document.domain.TPlantproglist;
import com.ruoyi.project.document.mapper.TPlantproglistMapper;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.mapper.TStaffmgrMapper;
import com.ruoyi.project.shiftmgr.domain.TShiftRoster;
import com.ruoyi.project.shiftmgr.mapper.TShiftRosterMapper;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.training.bccnew.domain.*;
import com.ruoyi.project.training.bccnew.mapper.*;
import com.ruoyi.project.training.bccnew.service.ITTsApproveService;
import com.ruoyi.project.training.bccnew.service.ITTsNewService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 导师带徒Service业务层处理
 *
 * @author ssy
 * @date 2024-05-20
 */
@Service
public class TTsNewServiceImpl implements ITTsNewService {
    @Autowired
    private TTsNewMapper tTsNewMapper;

    @Autowired
    private TTsLjplanTmplMapper tTsLjplanTmplMapper;

    @Autowired
    private TTsLjplanMapper tTsLjplanMapper;

    @Autowired
    private TTsLjplanTmplContentMapper tTsLjplanTmplContentMapper;

    @Autowired
    private TTsLjplanContentMapper tTsLjplanContentMapper;


    @Autowired
    private TTsYsplanTmplMapper tTsYsplanTmplMapper;

    @Autowired
    private TTsYsplanMapper tTsYsplanMapper;

    @Autowired
    private TTsYsplanTmplContentMapper tTsYsplanTmplContentMapper;

    @Autowired
    private TTsYsplanContentMapper tTsYsplanContentMapper;


    @Autowired
    private TTsFlplanTmplMapper tTsFlplanTmplMapper;

    @Autowired
    private TTsFlplanMapper tTsFlplanMapper;

    @Autowired
    private TTsFlplanTmplContentMapper tTsFlplanTmplContentMapper;

    @Autowired
    private TTsFlplanContentMapper tTsFlplanContentMapper;


    @Autowired
    private TTsFtplanTmplMapper tTsFtplanTmplMapper;

    @Autowired
    private TTsFtplanMapper tTsFtplanMapper;

    @Autowired
    private TTsFtplanTmplContentMapper tTsFtplanTmplContentMapper;

    @Autowired
    private TTsFtplanContentMapper tTsFtplanContentMapper;

    @Autowired
    private TTsFirstplanMapper tTsFirstplanMapper;

    @Autowired
    private TTsFirstplanTmplMapper tTsFirstplanTmplMapper;

    @Autowired
    private TPlantproglistMapper tPlantproglistMapper;

    @Autowired
    private ITTsApproveService tsApproveService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TShiftRosterMapper tShiftRosterMapper;

    @Autowired
    private TStaffmgrMapper tStaffmgrMapper;

    /**
     * 查询导师带徒
     *
     * @param newId 导师带徒ID
     * @return 导师带徒
     */
    @Override
    public TTsNew selectTTsNewById(Long newId) {
        return tTsNewMapper.selectTTsNewById(newId);
    }

    /**
     * 查询导师带徒列表
     *
     * @param tTsNew 导师带徒
     * @return 导师带徒
     */
    @Override
    public List<TTsNew> selectTTsNewList(TTsNew tTsNew) {
        return tTsNewMapper.selectTTsNewList(tTsNew);
    }

    /**
     * 新增导师带徒
     *
     * @param tTsNew 导师带徒
     * @return 结果
     */
    @Override
    public int insertTTsNew(TTsNew tTsNew) {
        tTsNewMapper.insertTTsNew(tTsNew);
        TShiftRoster tShiftRoster = new TShiftRoster();
        tShiftRoster.setShiftDate(tTsNew.getStartdate());
        if(tTsNew.getVacation()!=null&&tTsNew.getVacation()==1) {
            tShiftRoster.setVacation(tTsNew.getVacation());
        }
        tShiftRoster.setWeek(0L);
        List<TShiftRoster> rosters = tShiftRosterMapper.selectTShiftRosterListByTrain(tShiftRoster);
        int i = 0;//排班日期下标
        double studyCount = 0;//累计学习时长
        int c = 1;//课程统计（一天3门课程）
        for (TTsFirstplanTmpl tTsFirstplanTmpl : tTsFirstplanTmplMapper.selectTTsFirstplanTmplList(new TTsFirstplanTmpl())) {
            TTsFirstplan tTsFirstplan = new TTsFirstplan();
            TPlantproglist tpl = new TPlantproglist();
            tpl.setFileno(tTsFirstplanTmpl.getCourseCode());
            tpl.setFilename(tTsFirstplanTmpl.getTopic());
            List<TPlantproglist> tPlantproglists = tPlantproglistMapper.selectTPlantproglistList(tpl);
            if (CollectionUtils.isNotEmpty(tPlantproglists)) {
                TPlantproglist tPlantproglist = tPlantproglists.get(0);
                SysUser sysUser = new SysUser();
                sysUser.setNickName(tPlantproglist.getResponsibility());
                sysUser.setDeptId(103L);
                List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
                if (CollectionUtils.isNotEmpty(sysUsers)) {
                    SysUser user = sysUsers.get(0);
                    if (user.getNickName().equals("张磊")&&!user.getUserName().equals("ZHANGL49")){
                        user = sysUsers.get(1);
                    }
                    tTsFirstplan.setTrainer(user.getNickName());
                    tTsFirstplan.setTrainerId(user.getUserId());
                }else{
                    tTsFirstplan.setTrainer(tTsFirstplanTmpl.getTrainer());
                }
            }else{
                tTsFirstplan.setTrainer(tTsFirstplanTmpl.getTrainer());
            }
            tTsFirstplan.setNewId(tTsNew.getNewId());
            tTsFirstplan.setCourseCode(tTsFirstplanTmpl.getCourseCode());
            tTsFirstplan.setTopic(tTsFirstplanTmpl.getTopic());
            tTsFirstplan.setContent(tTsFirstplanTmpl.getContent());
            tTsFirstplan.setCourseDay(tTsFirstplanTmpl.getCourseDay());
            tTsFirstplan.setCourseHour(tTsFirstplanTmpl.getCourseHour());
            if (tTsFirstplan.getCourseHour() != null) {
//                tTsFirstplan.setTimerNeed(tTsFirstplan.getCourseHour() * 60);
                if (CollectionUtils.isNotEmpty(rosters)) {
//                    计算培训时长
                    studyCount += tTsFirstplan.getCourseHour();
                    if (tTsFirstplan.getCourseHour() <= 6) {//当前主题培训时长小于6个小时的
                        if (studyCount <= 6&&c<=3) {//累计时长小于6个小时，放在同一天
                            if (rosters.size() > i) {
                                tTsFirstplan.setTrainingPlanDate(rosters.get(i).getShiftDate());
                            }
                        } else {//超过6个小时放在下一天
                            i++;
                            if (rosters.size() > i) {
                                tTsFirstplan.setTrainingPlanDate(rosters.get(i).getShiftDate());
                            }
                            studyCount = 0;
                            c=0;
                        }
                    }else{
                        // 学习时长累计大于6个小时时往后再延一天
                        i++;
                        tTsFirstplan.setTrainingPlanDate(rosters.get(i).getShiftDate());
                        i++;
                        studyCount = 0;
                    }
                }
            }
            tTsFirstplan.setCourseType(tTsFirstplanTmpl.getCourseType());
            tTsFirstplan.setCourseDate(tTsFirstplanTmpl.getCourseDate());
            tTsFirstplan.setAssess(tTsFirstplanTmpl.getAssess());
            tTsFirstplan.setPlanStatus(0L);
            tTsFirstplan.setFileName(tTsFirstplanTmpl.getFileName());
            tTsFirstplan.setFileUrl(tTsFirstplanTmpl.getFileUrl());
            tTsFirstplan.setTimer(0L);
            tTsFirstplan.setTimerNeed(tTsFirstplanTmpl.getTimerNeed());
            tTsFirstplan.setSortTmpl(tTsFirstplanTmpl.getSortTmpl());
            tTsFirstplan.setMaterialVer(tTsFirstplanTmpl.getMaterialVer());
            tTsFirstplan.setExamType(tTsFirstplanTmpl.getExamType());
            tTsFirstplan.setExamId(tTsFirstplanTmpl.getExamId());
            tTsFirstplanMapper.insertTTsFirstplan(tTsFirstplan);
            c++;
        }
        TStaffmgr tStaffmgr = tStaffmgrMapper.selectTStaffmgrByStaffId(tTsNew.getStaffId());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tTsNew.getStartdate());
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
        tShiftRoster.setShiftDate(calendar.getTime());
        tShiftRoster.setDclass(tStaffmgr.getTeam());
        tShiftRoster.setNclass(tStaffmgr.getTeam());
        tShiftRoster.setVacation(null);
        tShiftRoster.setWeek(null);
        rosters = tShiftRosterMapper.selectTShiftRosterListByTrain(tShiftRoster);
        i=0;
        if (tTsNew.getPlanType() == 1) {
            for (TTsLjplanTmpl item : tTsLjplanTmplMapper.selectLjTmplListWithoutContent(new TTsLjplanTmpl())) {
                TTsLjplan entity = new TTsLjplan();
                entity.setTopic(item.getTopic());
                entity.setDetailPlan(item.getDetailPlan());
                entity.setNewId(tTsNew.getNewId());
                entity.setFileName(item.getFileName());
                entity.setFileUrl(item.getFileUrl());
                entity.setTimerNeed(item.getTimerNeed());
                entity.setTimer(0L);
                entity.setSortTmpl(item.getSortTmpl());
                entity.setPlanStatus(0L);
                tTsLjplanMapper.insertTTsLjplan(entity);
                TTsLjplanTmplContent content = new TTsLjplanTmplContent();
                content.setPid(item.getId());
                for (TTsLjplanTmplContent tmplContent : tTsLjplanTmplContentMapper.selectTTsLjplanTmplContentList(content)) {
                    TTsLjplanContent contentEntity = new TTsLjplanContent();
                    contentEntity.setPid(entity.getId());
                    contentEntity.setContent(tmplContent.getContent());
                    contentEntity.setCourseDay(tmplContent.getCourseDay());
                    contentEntity.setSortTmpl(tmplContent.getSortTmpl());
                    contentEntity.setPlanDate(rosters.get(i).getShiftDate());
                    tTsLjplanContentMapper.insertTTsLjplanContent(contentEntity);
                    i++;
                }
            }
        } else if (tTsNew.getPlanType() == 2) {
            for (TTsYsplanTmpl item : tTsYsplanTmplMapper.selectYsTmplListWithoutContent(new TTsYsplanTmpl())) {
                TTsYsplan entity = new TTsYsplan();
                entity.setTopic(item.getTopic());
                entity.setDetailPlan(item.getDetailPlan());
                entity.setNewId(tTsNew.getNewId());
                entity.setFileName(item.getFileName());
                entity.setFileUrl(item.getFileUrl());
                entity.setTimerNeed(item.getTimerNeed());
                entity.setTimer(0L);
                entity.setSortTmpl(item.getSortTmpl());
                entity.setPlanStatus(0L);
                tTsYsplanMapper.insertTTsYsplan(entity);
                TTsYsplanTmplContent content = new TTsYsplanTmplContent();
                content.setPid(item.getId());
                for (TTsYsplanTmplContent tmplContent : tTsYsplanTmplContentMapper.selectTTsYsplanTmplContentList(content)) {
                    TTsYsplanContent contentEntity = new TTsYsplanContent();
                    contentEntity.setPid(entity.getId());
                    contentEntity.setContent(tmplContent.getContent());
                    contentEntity.setCourseDay(tmplContent.getCourseDay());
                    contentEntity.setSortTmpl(tmplContent.getSortTmpl());
                    contentEntity.setPlanDate(rosters.get(i).getShiftDate());
                    tTsYsplanContentMapper.insertTTsYsplanContent(contentEntity);
                    i++;
                }
            }
        } else if (tTsNew.getPlanType() == 3) {
            for (TTsFlplanTmpl item : tTsFlplanTmplMapper.selectFlTmplListWithoutContent(new TTsFlplanTmpl())) {
                TTsFlplan entity = new TTsFlplan();
                entity.setTopic(item.getTopic());
                entity.setDetailPlan(item.getDetailPlan());
                entity.setNewId(tTsNew.getNewId());
                entity.setFileName(item.getFileName());
                entity.setFileUrl(item.getFileUrl());
                entity.setTimerNeed(item.getTimerNeed());
                entity.setTimer(0L);
                entity.setSortTmpl(item.getSortTmpl());
                entity.setPlanStatus(0L);
                tTsFlplanMapper.insertTTsFlplan(entity);
                TTsFlplanTmplContent content = new TTsFlplanTmplContent();
                content.setPid(item.getId());
                for (TTsFlplanTmplContent tmplContent : tTsFlplanTmplContentMapper.selectTTsFlplanTmplContentList(content)) {
                    TTsFlplanContent contentEntity = new TTsFlplanContent();
                    contentEntity.setPid(entity.getId());
                    contentEntity.setContent(tmplContent.getContent());
                    contentEntity.setCourseDay(tmplContent.getCourseDay());
                    contentEntity.setSortTmpl(tmplContent.getSortTmpl());
                    contentEntity.setPlanDate(rosters.get(i).getShiftDate());
                    tTsFlplanContentMapper.insertTTsFlplanContent(contentEntity);
                    i++;
                }
            }
        } else if (tTsNew.getPlanType() == 4) {
            for (TTsFtplanTmpl item : tTsFtplanTmplMapper.selectFtTmplListWithoutContent(new TTsFtplanTmpl())) {
                TTsFtplan entity = new TTsFtplan();
                entity.setTopic(item.getTopic());
                entity.setDetailPlan(item.getDetailPlan());
                entity.setNewId(tTsNew.getNewId());
                entity.setFileName(item.getFileName());
                entity.setFileUrl(item.getFileUrl());
                entity.setTimerNeed(item.getTimerNeed());
                entity.setTimer(0L);
                entity.setSortTmpl(item.getSortTmpl());
                entity.setPlanStatus(0L);
                tTsFtplanMapper.insertTTsFtplan(entity);
                TTsFtplanTmplContent content = new TTsFtplanTmplContent();
                content.setPid(item.getId());
                for (TTsFtplanTmplContent tmplContent : tTsFtplanTmplContentMapper.selectTTsFtplanTmplContentList(content)) {
                    TTsFtplanContent contentEntity = new TTsFtplanContent();
                    contentEntity.setPid(entity.getId());
                    contentEntity.setContent(tmplContent.getContent());
                    contentEntity.setCourseDay(tmplContent.getCourseDay());
                    contentEntity.setSortTmpl(tmplContent.getSortTmpl());
                    contentEntity.setPlanDate(rosters.get(i).getShiftDate());
                    tTsFtplanContentMapper.insertTTsFtplanContent(contentEntity);
                    i++;
                }
            }
        }
        return 1;
    }

    /**
     * 修改导师带徒
     *
     * @param tTsNew 导师带徒
     * @return 结果
     */
    @Override
    public int updateTTsNew(TTsNew tTsNew) {
        TTsNew tsNew = tTsNewMapper.selectTTsNewById(tTsNew.getNewId());
        if (tTsNew.getPlanStatus() == 1) {
            TTsFirstplan tTsFirstplan = new TTsFirstplan();
            tTsFirstplan.setNewId(tTsNew.getNewId());
            for (TTsFirstplan tsFirstplan : tTsFirstplanMapper.selectTTsFirstplanListByNewId(tTsFirstplan)) {
                tsFirstplan.setPlanStatus(1L);
                tTsFirstplanMapper.updateTTsFirstplan(tsFirstplan);
            }
            if (tsNew.getPlanType() == 1) {
                TTsLjplan entity = new TTsLjplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsLjplan item : tTsLjplanMapper.selectTTsLjplanListByNewId(entity)) {
                    item.setPlanStatus(1L);
                    tTsLjplanMapper.updateTTsLjplan(item);
                }
            } else if (tsNew.getPlanType() == 2) {
                TTsYsplan entity = new TTsYsplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsYsplan item : tTsYsplanMapper.selectTTsYsplanListByNewId(entity)) {
                    item.setPlanStatus(1L);
                    tTsYsplanMapper.updateTTsYsplan(item);
                }
            } else if (tsNew.getPlanType() == 3) {
                TTsFlplan entity = new TTsFlplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsFlplan item : tTsFlplanMapper.selectTTsFlplanListByNewId(entity)) {
                    item.setPlanStatus(1L);
                    tTsFlplanMapper.updateTTsFlplan(item);
                }
            } else if (tsNew.getPlanType() == 4) {
                TTsFtplan entity = new TTsFtplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsFtplan item : tTsFtplanMapper.selectTTsFtplanListByNewId(entity)) {
                    item.setPlanStatus(1L);
                    tTsFtplanMapper.updateTTsFtplan(item);
                }
            }
            tTsNewMapper.updateTTsNew(tTsNew);
        }
        if (tTsNew.getPlanStatus() == 3) {
            TTsFirstplan tTsFirstplan = new TTsFirstplan();
            tTsFirstplan.setNewId(tTsNew.getNewId());
            for (TTsFirstplan tsFirstplan : tTsFirstplanMapper.selectTTsFirstplanListByNewId(tTsFirstplan)) {
                tsFirstplan.setPlanStatus(3L);
                tTsFirstplanMapper.updateTTsFirstplan(tsFirstplan);
            }
            if (tsNew.getPlanType() == 1) {
                TTsLjplan entity = new TTsLjplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsLjplan item : tTsLjplanMapper.selectTTsLjplanListByNewId(entity)) {
                    item.setPlanStatus(3L);
                    tTsLjplanMapper.updateTTsLjplan(item);
                }
            } else if (tsNew.getPlanType() == 2) {
                TTsYsplan entity = new TTsYsplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsYsplan item : tTsYsplanMapper.selectTTsYsplanListByNewId(entity)) {
                    item.setPlanStatus(3L);
                    tTsYsplanMapper.updateTTsYsplan(item);
                }
            } else if (tsNew.getPlanType() == 3) {
                TTsFlplan entity = new TTsFlplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsFlplan item : tTsFlplanMapper.selectTTsFlplanListByNewId(entity)) {
                    item.setPlanStatus(3L);
                    tTsFlplanMapper.updateTTsFlplan(item);
                }
            } else if (tsNew.getPlanType() == 4) {
                TTsFtplan entity = new TTsFtplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsFtplan item : tTsFtplanMapper.selectTTsFtplanListByNewId(entity)) {
                    item.setPlanStatus(3L);
                    tTsFtplanMapper.updateTTsFtplan(item);
                }
            }
            tTsNewMapper.updateTTsNew(tTsNew);
        }
        return tTsNewMapper.updateTTsNew(tTsNew);
    }

    @Override
    public int updateTTsNew(TTsNew tTsNew, Long userId, String nickName) {
        TTsNew tsNew = tTsNewMapper.selectTTsNewById(tTsNew.getNewId());
        if (tTsNew.getPlanStatus() == 1) {
            TTsFirstplan tTsFirstplan = new TTsFirstplan();
            tTsFirstplan.setNewId(tTsNew.getNewId());
            for (TTsFirstplan tsFirstplan : tTsFirstplanMapper.selectTTsFirstplanListByNewId(tTsFirstplan)) {
                tsFirstplan.setPlanStatus(1L);
                tTsFirstplanMapper.updateTTsFirstplan(tsFirstplan);
            }
            if (tsNew.getPlanType() == 1) {
                TTsLjplan entity = new TTsLjplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsLjplan item : tTsLjplanMapper.selectTTsLjplanListByNewId(entity)) {
                    item.setPlanStatus(1L);
                    tTsLjplanMapper.updateTTsLjplan(item);
                }
            } else if (tsNew.getPlanType() == 2) {
                TTsYsplan entity = new TTsYsplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsYsplan item : tTsYsplanMapper.selectTTsYsplanListByNewId(entity)) {
                    item.setPlanStatus(1L);
                    tTsYsplanMapper.updateTTsYsplan(item);
                }
            } else if (tsNew.getPlanType() == 3) {
                TTsFlplan entity = new TTsFlplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsFlplan item : tTsFlplanMapper.selectTTsFlplanListByNewId(entity)) {
                    item.setPlanStatus(1L);
                    tTsFlplanMapper.updateTTsFlplan(item);
                }
            } else if (tsNew.getPlanType() == 4) {
                TTsFtplan entity = new TTsFtplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsFtplan item : tTsFtplanMapper.selectTTsFtplanListByNewId(entity)) {
                    item.setPlanStatus(1L);
                    tTsFtplanMapper.updateTTsFtplan(item);
                }
            }
            tTsNewMapper.updateTTsNew(tTsNew);
        }
        if (tTsNew.getPlanStatus() == 3) {
            TTsFirstplan tTsFirstplan = new TTsFirstplan();
            tTsFirstplan.setNewId(tTsNew.getNewId());
            for (TTsFirstplan tsFirstplan : tTsFirstplanMapper.selectTTsFirstplanListByNewId(tTsFirstplan)) {
                tsFirstplan.setPlanStatus(3L);
                tTsFirstplanMapper.updateTTsFirstplan(tsFirstplan);
            }
            if (tsNew.getPlanType() == 1) {
                TTsLjplan entity = new TTsLjplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsLjplan item : tTsLjplanMapper.selectTTsLjplanListByNewId(entity)) {
                    item.setPlanStatus(3L);
                    tTsLjplanMapper.updateTTsLjplan(item);
                }
            } else if (tsNew.getPlanType() == 2) {
                TTsYsplan entity = new TTsYsplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsYsplan item : tTsYsplanMapper.selectTTsYsplanListByNewId(entity)) {
                    item.setPlanStatus(3L);
                    tTsYsplanMapper.updateTTsYsplan(item);
                }
            } else if (tsNew.getPlanType() == 3) {
                TTsFlplan entity = new TTsFlplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsFlplan item : tTsFlplanMapper.selectTTsFlplanListByNewId(entity)) {
                    item.setPlanStatus(3L);
                    tTsFlplanMapper.updateTTsFlplan(item);
                }
            } else if (tsNew.getPlanType() == 4) {
                TTsFtplan entity = new TTsFtplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsFtplan item : tTsFtplanMapper.selectTTsFtplanListByNewId(entity)) {
                    item.setPlanStatus(3L);
                    tTsFtplanMapper.updateTTsFtplan(item);
                }
            }
            tTsNewMapper.updateTTsNew(tTsNew);
        }
        if (tTsNew.getPlanStatus() == 4) {
            TTsFirstplan tTsFirstplan = new TTsFirstplan();
            tTsFirstplan.setNewId(tTsNew.getNewId());
            for (TTsFirstplan tsFirstplan : tTsFirstplanMapper.selectTTsFirstplanListByNewId(tTsFirstplan)) {
                if (tsFirstplan.getPlanStatus() == null || tsFirstplan.getPlanStatus() == 0 || tsFirstplan.getPlanStatus() == 2) {
                    tsFirstplan.setPlanStatus(4L);
                    tTsFirstplanMapper.updateTTsFirstplan(tsFirstplan);
                }
            }
            if (tsNew.getPlanType() == 1) {
                TTsLjplan entity = new TTsLjplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsLjplan item : tTsLjplanMapper.selectTTsLjplanListByNewId(entity)) {
                    if (item.getPlanStatus() == null || item.getPlanStatus() == 0 || item.getPlanStatus() == 2) {
                        item.setPlanStatus(4L);
                        tTsLjplanMapper.updateTTsLjplan(item);
                    }
                }
            } else if (tsNew.getPlanType() == 2) {
                TTsYsplan entity = new TTsYsplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsYsplan item : tTsYsplanMapper.selectTTsYsplanListByNewId(entity)) {
                    if (item.getPlanStatus() == null || item.getPlanStatus() == 0 || item.getPlanStatus() == 2) {
                        item.setPlanStatus(4L);
                        tTsYsplanMapper.updateTTsYsplan(item);
                    }
                }
            } else if (tsNew.getPlanType() == 3) {
                TTsFlplan entity = new TTsFlplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsFlplan item : tTsFlplanMapper.selectTTsFlplanListByNewId(entity)) {
                    if (item.getPlanStatus() == null || item.getPlanStatus() == 0 || item.getPlanStatus() == 2) {
                        item.setPlanStatus(4L);
                        tTsFlplanMapper.updateTTsFlplan(item);
                    }
                }
            } else if (tsNew.getPlanType() == 4) {
                TTsFtplan entity = new TTsFtplan();
                entity.setNewId(tTsNew.getNewId());
                for (TTsFtplan item : tTsFtplanMapper.selectTTsFtplanListByNewId(entity)) {
                    if (item.getPlanStatus() == null || item.getPlanStatus() == 0 || item.getPlanStatus() == 2) {
                        item.setPlanStatus(4L);
                        tTsFtplanMapper.updateTTsFtplan(item);
                    }
                }
            }
            tTsNewMapper.updateTTsNew(tTsNew);
            tsApproveService.doApprove(tTsNew.getNewId(), tsNew.getPlanType().toString(), userId, nickName, "dsdtTraining");
        }
        if (tTsNew.getPlanStatus() == 5) {
            TTsFirstplan plan = new TTsFirstplan();
            plan.setNewId(tTsNew.getNewId());
            List<TTsFirstplan> tTsFirstplans = tTsFirstplanMapper.selectTTsFirstplanListByNewId(plan);
            plan.setPlanStatus(5L);
            List<TTsFirstplan> tTsFirstplansDone = tTsFirstplanMapper.selectTTsFirstplanListByNewId(plan);
            boolean flag = false;
            if (1 == tsNew.getPlanType()) {
                TTsLjplan tTsLjplan = new TTsLjplan();
                tTsLjplan.setNewId(tTsNew.getNewId());
                List<TTsLjplan> tTsLjplans = tTsLjplanMapper.selectTTsLjplanListByNewId(tTsLjplan);
                List<TTsLjplan> newList = new ArrayList<>();
                for (TTsLjplan item : tTsLjplans) {
                    if (item.getPlanStatus() != 1) {
                        newList.add(item);
                    }
                }
                tTsLjplan.setPlanStatus(5L);
                List<TTsLjplan> tTsLjplansDone = tTsLjplanMapper.selectTTsLjplanListByNewId(tTsLjplan);
                if (!CollectionUtils.isEmpty(newList)
                        && !CollectionUtils.isEmpty(tTsLjplansDone)
                        && tTsLjplansDone.size() == newList.size()) {
                    flag = true;
                }
            } else if (2 == tsNew.getPlanType()) {
                TTsYsplan tTsYsplan = new TTsYsplan();
                tTsYsplan.setNewId(tTsNew.getNewId());
                List<TTsYsplan> tTsYsplans = tTsYsplanMapper.selectTTsYsplanListByNewId(tTsYsplan);
                List<TTsYsplan> newList = new ArrayList<>();
                for (TTsYsplan item : tTsYsplans) {
                    if (item.getPlanStatus() != 1) {
                        newList.add(item);
                    }
                }
                tTsYsplan.setPlanStatus(5L);
                List<TTsYsplan> tTsYsplansDone = tTsYsplanMapper.selectTTsYsplanListByNewId(tTsYsplan);
                if (!CollectionUtils.isEmpty(newList)
                        && !CollectionUtils.isEmpty(tTsYsplansDone)
                        && tTsYsplansDone.size() == newList.size()) {
                    flag = true;
                }
            } else if (3 == tsNew.getPlanType()) {
                TTsFlplan tTsFlplan = new TTsFlplan();
                tTsFlplan.setNewId(tTsNew.getNewId());
                List<TTsFlplan> tTsFlplans = tTsFlplanMapper.selectTTsFlplanListByNewId(tTsFlplan);
                List<TTsFlplan> newList = new ArrayList<>();
                for (TTsFlplan item : tTsFlplans) {
                    if (item.getPlanStatus() != 1) {
                        newList.add(item);
                    }
                }
                tTsFlplan.setPlanStatus(5L);
                List<TTsFlplan> tTsFlplansDone = tTsFlplanMapper.selectTTsFlplanListByNewId(tTsFlplan);
                if (!CollectionUtils.isEmpty(newList)
                        && !CollectionUtils.isEmpty(tTsFlplansDone)
                        && tTsFlplansDone.size() == newList.size()) {
                    flag = true;
                }
            } else if (4 == tsNew.getPlanType()) {
                TTsFtplan tTsFtplan = new TTsFtplan();
                tTsFtplan.setNewId(tTsNew.getNewId());
                List<TTsFtplan> tTsFtplans = tTsFtplanMapper.selectTTsFtplanListByNewId(tTsFtplan);
                List<TTsFtplan> newList = new ArrayList<>();
                for (TTsFtplan item : tTsFtplans) {
                    if (item.getPlanStatus() != 1) {
                        newList.add(item);
                    }
                }
                tTsFtplan.setPlanStatus(5L);
                List<TTsFtplan> tTsFtplansDone = tTsFtplanMapper.selectTTsFtplanListByNewId(tTsFtplan);
                if (!CollectionUtils.isEmpty(newList)
                        && !CollectionUtils.isEmpty(tTsFtplansDone)
                        && tTsFtplansDone.size() == newList.size()) {
                    flag = true;
                }
            }
            if (!CollectionUtils.isEmpty(tTsFirstplans) &&
                    !CollectionUtils.isEmpty(tTsFirstplansDone) &&
                    tTsFirstplans.size() == tTsFirstplansDone.size() && flag) {
                tTsNewMapper.updateTTsNew(tTsNew);
                tsApproveService.doApprove(tTsNew.getNewId(), tsNew.getPlanType().toString(), userId, nickName, "dsdtTraining");
            }
        }
        return 1;
    }

    /**
     * 批量删除导师带徒
     *
     * @param newIds 需要删除的导师带徒ID
     * @return 结果
     */
    @Override
    public int deleteTTsNewByIds(Long[] newIds) {
        return tTsNewMapper.deleteTTsNewByIds(newIds);
    }

    /**
     * 删除导师带徒信息
     *
     * @param newId 导师带徒ID
     * @return 结果
     */
    @Override
    public int deleteTTsNewById(Long newId) {
        return tTsNewMapper.deleteTTsNewById(newId);
    }
}
