Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / MeetScheduleServiceImpl.java
package com.newfiber.api.pc.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newfiber.api.core.commons.ResultObj;
import com.newfiber.api.pc.dao.MeetScheduleMapper;
import com.newfiber.api.pc.dao.MeetTaskMapper;
import com.newfiber.api.pc.model.meet.MeetSchedule;
import com.newfiber.api.pc.model.meet.MeetTask;
import com.newfiber.api.pc.model.vo.MeetScheduleDataVo;
import com.newfiber.api.pc.model.vo.MeetScheduleParam;
import com.newfiber.api.pc.model.vo.MeetScheduleVo;
import com.newfiber.api.pc.model.vo.MeetUser;
import com.newfiber.api.pc.service.MeetScheduleService;
import com.newfiber.api.pc.service.MeetTaskService;
import com.newfiber.api.core.utils.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Author:zzh
 * @CreateDate:2020/11/26 16:44
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MeetScheduleServiceImpl extends ServiceImpl<MeetScheduleMapper, MeetSchedule> implements MeetScheduleService {

    @Autowired
    private MeetScheduleMapper meetScheduleMapper;

    @Autowired
    private MeetTaskService meetTaskService;

    @Autowired
    private MeetTaskMapper meetTaskMapper;

    @Override
    public PageInfo<MeetScheduleVo> selectListByTime(Integer taskId,String userName,String startTime, String endTime,Integer pageNo,Integer pageSize) {
        if(pageNo == null && pageSize == null){
            pageNo = 0;
            pageSize = 9999;
        }
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<MeetScheduleVo> pageInfo = new PageInfo<>();
        List<MeetScheduleVo> result = meetScheduleMapper.selectListByTime(taskId,null, userName, startTime, endTime);
        if(!result.isEmpty()){
            pageInfo = new PageInfo<MeetScheduleVo>(result);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<MeetScheduleDataVo> selectDataListByTime(Integer taskId,Integer taskType, String userName, String startTime, String endTime, Integer pageNo, Integer pageSize) {
        if(pageNo == null && pageSize == null){
            pageNo = 0;
            pageSize = 9999;
        }
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<MeetScheduleDataVo> pageInfo = new PageInfo<>();
        List<MeetScheduleVo> result = meetScheduleMapper.selectListByTime(taskId,taskType, userName, startTime, endTime);
        if(!result.isEmpty()){
            List<MeetScheduleDataVo> vos = new ArrayList<>();
            result.stream().forEach(e->{
                MeetScheduleDataVo vo = new MeetScheduleDataVo();
                vo.setId(e.getId());
                vo.setTaskId(e.getTaskId());
                vo.setStart(e.getStartTime());
                if(e.getTaskType()!=null && e.getTaskType()==1){
                    vo.setEnd(e.getStartTime());
                }else{
                    vo.setEnd(e.getEndTime());
                }
                vo.setUserId(e.getUserId());
                vo.setTitle(e.getUserName());
                if(e.getStatus().equals("0")){
                    vo.setCssClass("scheduled");
                }else{
                    vo.setCssClass("unScheduled");
                }
                vos.add(vo);
            });
            pageInfo = new PageInfo<MeetScheduleDataVo>(vos);
        }
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultObj addTask(MeetScheduleParam data) {
        if(data == null){
            return new ResultObj(200,"添加失败");
        }
        //判断重复
        MeetTask meetTask = meetTaskMapper.selectByName(data.getTaskName());
        if(meetTask != null){
            return new ResultObj(200,"添加失败,排班名称已存在",0);
        }

        List<MeetUser> dayUsers = data.getDayUserIds();
        List<Long> dayIds = new ArrayList<>();
        //List<String> userNames = new ArrayList<>();
        Map<Long, List<MeetUser>> dayCollect = new HashMap<>();
        if(!dayUsers.isEmpty()){
            dayCollect = dayUsers.stream().collect(Collectors.groupingBy(MeetUser::getId));
            for (MeetUser user : dayUsers) {
                dayIds.add(user.getId());
                //userNames.add(user.getUserName());
            }
        }

        //夜班人员id
        List<Long> nightIds = new ArrayList<>();
        List<MeetUser> nightUsers = data.getNightUserIds();
        //List<String> userNames = new ArrayList<>();
        Map<Long, List<MeetUser>> nightCollect = new HashMap<>();
        if(!nightUsers.isEmpty()){
            nightCollect = nightUsers.stream().collect(Collectors.groupingBy(MeetUser::getId));
            for (MeetUser user : nightUsers) {
                nightIds.add(user.getId());
                //userNames.add(user.getUserName());
            }
        }


        MeetTask task = new MeetTask();
        task.setName(data.getTaskName());
        task.setChangeTime(data.getChangeTime());

        Date taskTime = new Date();
        task.setDayUserIds(org.apache.commons.lang.StringUtils.join(dayIds,","));
        task.setNighUserIds(org.apache.commons.lang.StringUtils.join(nightIds,","));
        task.setWeek(StringUtils.join(data.getWeek(),","));
        //获取排班开始时间和结束时间,获取每天开始时间和结束时间
        Date dayStartTime = data.getDayStartTime();
        Date dayEndTime = data.getDayEndTime();
        task.setDayStartTime(dayStartTime);
        task.setDayEndTime(dayEndTime);
        Date startTime = data.getStartTime();
        Date endTime = data.getEndTime();
        task.setStartTime(startTime);
        task.setEndTime(endTime);
        task.setCreateTime(taskTime);
        meetTaskService.insert(task);

        LocalDate startDate= DateUtil.dateToLocalDate(startTime);
        LocalDate endDate= DateUtil.dateToLocalDate(endTime);

        LocalDate nightStartDate= DateUtil.dateToLocalDate(startTime);
        LocalDate nightEndDate= DateUtil.dateToLocalDate(endTime);

        //每天开始和结束时间
        String dayStartDay = "08:00:00";
        String dayEndDay = "18:00:00";

        String nightStartDay = "18:00:00";
        String nightEndDay = "8:00:00";

        //周一到周日
        List<Integer> weekList = new ArrayList<Integer>(){{
            add(1);add(2);add(3);add(4);add(5);add(6);add(7);
        }};
        //获得不排班的星期
        weekList.removeAll(data.getWeek());

        //添加任务
        List<MeetSchedule> result = new ArrayList<>();
        int dayIndex = 0;
        while (startDate.isBefore(endDate)){//比较是缺少最后一天
            MeetSchedule meet = new MeetSchedule();
            meet.setTaskId(task.getId());
            //设置排班时间
            meet.setTaskTime(taskTime);
            //开始时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                meet.setStartTime(sdf.parse(DateUtil.getYearAndMonth(DateUtil.localDateToDate(startDate)) + " " + dayStartDay));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //结束时间
            try {
                meet.setEndTime(sdf.parse(DateUtil.getYearAndMonth(DateUtil.localDateToDate(startDate))+" "+ dayEndDay));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int value = startDate.getDayOfWeek().getValue();
            //跳过每周不排班的天次
            if(!weekList.isEmpty() && weekList.contains(value)){
                startDate = startDate.plusDays(1);
                continue;
            }
            startDate = startDate.plusDays(1);
            //排班人员轮流值班
            Long aLong = dayIds.get((dayIndex = dayIndex % dayIds.size()));
            meet.setUserId(dayCollect.get(aLong).get(0).getId());
            meet.setUserName(dayCollect.get(aLong).get(0).getUserName());
            dayIndex++;
            meet.setCreateTime(taskTime);
            meet.setUpdateTime(taskTime);
            meet.setTaskType(0L);
            result.add(meet);
        }
        //补上最后一天
        lastDaySchedule(dayIds, dayCollect, task, taskTime, startDate, dayStartDay, dayEndDay, weekList, dayIndex, result);

        //夜班
        int nightIndex = 0;
        nightTask(nightIds, nightCollect, task, taskTime, nightStartDate, nightEndDate, nightStartDay, nightEndDay, weekList, result, nightIndex);
        //lastNightSchedule(nightIds, nightCollect, task, taskTime, startDate, nightStartDay, nightEndDay, weekList, nightIndex, result);

        if(!result.isEmpty()){
            this.insertBatch(result);
            return new ResultObj(200,"添加成功",1);
        }else{
            meetTaskService.deleteById(task.getId());
            return new ResultObj(200,"添加失败,当前排班时间和选择日期不符",0);
        }
    }

    private void nightTask(List<Long> nightIds, Map<Long, List<MeetUser>> nightCollect, MeetTask task, Date taskTime, LocalDate startDate, LocalDate endDate, String nightStartDay, String nightEndDay, List<Integer> weekList, List<MeetSchedule> result, int nightIndex) {
        while (startDate.isBefore(endDate)){//比较是缺少最后一天
            MeetSchedule meet = new MeetSchedule();
            meet.setTaskId(task.getId());
            //设置排班时间
            meet.setTaskTime(taskTime);
            //开始时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                meet.setStartTime(sdf.parse(DateUtil.getYearAndMonth(DateUtil.localDateToDate(startDate)) + " " + nightStartDay));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //结束时间
            try {
                meet.setEndTime(sdf.parse(DateUtil.getYearAndMonth(DateUtil.localDateToDate(startDate.plusDays(1)))+" "+ nightEndDay));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int value = startDate.getDayOfWeek().getValue();
            //跳过每周不排班的天次
            if(!weekList.isEmpty() && weekList.contains(value)){
                startDate = startDate.plusDays(1);
                continue;
            }
            startDate = startDate.plusDays(1);
            //排班人员轮流值班
            Long aLong = nightIds.get((nightIndex = nightIndex % nightIds.size()));
            meet.setUserId(nightCollect.get(aLong).get(0).getId());
            meet.setUserName(nightCollect.get(aLong).get(0).getUserName());
            nightIndex++;
            meet.setCreateTime(taskTime);
            meet.setUpdateTime(taskTime);
            meet.setTaskType(1L);
            result.add(meet);
        }
        lastNightSchedule(nightIds, nightCollect, task, taskTime, startDate, nightStartDay, nightEndDay, weekList, nightIndex, result);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOne(MeetSchedule data) {
        MeetTask meetTask = meetTaskService.selectById(data.getTaskId());
        MeetSchedule meetSchedule = new MeetSchedule();
        meetSchedule.setStatus((byte)0);
        meetSchedule.setTaskId(meetTask.getId());
        meetSchedule.setUserId(data.getUserId());
        meetSchedule.setUserName(data.getUserName());
        meetSchedule.setTaskTime(meetTask.getCreateTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            meetSchedule.setStartTime(sdf.parse(DateUtil.getYearAndMonth(data.getStartTime())+" "+DateUtil.getHourAndMinute(meetTask.getDayStartTime())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            meetSchedule.setEndTime(sdf.parse(DateUtil.getYearAndMonth(data.getStartTime())+" "+DateUtil.getHourAndMinute(meetTask.getDayEndTime())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        meetSchedule.setCreateTime(new Date());
        meetSchedule.setUpdateTime(new Date());
        boolean insert = this.insert(meetSchedule);
        if(insert){
            return 1;
        }else{
            return 0;
        }

    }

    @Override
    public MeetScheduleVo getOne() {
        Date startTime = new Date(DateUtil.getDayStartTime(new Date()).getTime());
        Date endTime = new Date(DateUtil.getDayEndTime(new Date()).getTime());
        //Date startTime =null;
        //Date endTime =null;
        return meetScheduleMapper.getOne(startTime,endTime);
    }

    /**
     * 最后一天排班
     * @param ids
     * @param collect
     * @param task
     * @param taskTime
     * @param startDate
     * @param startDay
     * @param endDay
     * @param weekList
     * @param index
     * @param result
     */
    private void lastDaySchedule(List<Long> ids, Map<Long, List<MeetUser>> collect, MeetTask task, Date taskTime, LocalDate startDate, String startDay, String endDay, List<Integer> weekList, int index, List<MeetSchedule> result) {
        if(weekList.isEmpty() || !weekList.contains(startDate.getDayOfWeek().getValue())){
            MeetSchedule meet = new MeetSchedule();
            meet.setTaskId(task.getId());
            //设置排班时间
            meet.setTaskTime(taskTime);
            //开始时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                meet.setStartTime(sdf.parse(DateUtil.getYearAndMonth(DateUtil.localDateToDate(startDate)) + " " + startDay));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //结束时间
            try {
                meet.setEndTime(sdf.parse(DateUtil.getYearAndMonth(DateUtil.localDateToDate(startDate))+" "+endDay));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Long aLong = ids.get((index = index % ids.size()));
            meet.setUserId(collect.get(aLong).get(0).getId());
            meet.setUserName(collect.get(aLong).get(0).getUserName());
            meet.setCreateTime(taskTime);
            meet.setUpdateTime(taskTime);
            meet.setTaskType(0L);
            result.add(meet);
        }
    }


    private void lastNightSchedule(List<Long> ids, Map<Long, List<MeetUser>> collect, MeetTask task, Date taskTime, LocalDate startDate, String startDay, String endDay, List<Integer> weekList, int index, List<MeetSchedule> result) {
        if(weekList.isEmpty() || !weekList.contains(startDate.getDayOfWeek().getValue())){
            MeetSchedule meet = new MeetSchedule();
            meet.setTaskId(task.getId());
            //设置排班时间
            meet.setTaskTime(taskTime);
            //开始时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                meet.setStartTime(sdf.parse(DateUtil.getYearAndMonth(DateUtil.localDateToDate(startDate)) + " " + startDay));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //结束时间
            try {
                meet.setEndTime(sdf.parse(DateUtil.getYearAndMonth(DateUtil.localDateToDate(startDate.plusDays(1)))+" "+endDay));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Long aLong = ids.get((index = index % ids.size()));
            meet.setUserId(collect.get(aLong).get(0).getId());
            meet.setUserName(collect.get(aLong).get(0).getUserName());
            meet.setCreateTime(taskTime);
            meet.setUpdateTime(taskTime);
            meet.setTaskType(1L);
            result.add(meet);
        }
    }

}