DriverSchedulingExpandSmartServiceImpl.java 6.92 KB
package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.global.ResultCode;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.domain.DriverSchedulingExpandSmart;
import com.ruoyi.expand.domain.DriverSchedulingExpand;
import com.ruoyi.expand.mapper.DriverSchedulingExpandMapper;
import com.ruoyi.expand.service.IDriverSchedulingExpandService;
import com.ruoyi.pojo.dto.SmartExpandDto;
import com.ruoyi.pojo.vo.SmartExpandVo;
import com.ruoyi.service.DriverSchedulingExpandSmartService;
import com.ruoyi.mapper.DriverSchedulingExpandSmartMapper;
import com.ruoyi.utils.ConstDateUtil;
import com.ruoyi.utils.ToolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author 20412
 * @description 针对表【driver_scheduling_expand_smart(灵活跟班表)】的数据库操作Service实现
 * @createDate 2024-03-07 16:49:40
 */
@Service
@Slf4j
public class DriverSchedulingExpandSmartServiceImpl extends ServiceImpl<DriverSchedulingExpandSmartMapper, DriverSchedulingExpandSmart>
        implements DriverSchedulingExpandSmartService {

    @Autowired
    private IDriverSchedulingExpandService expandService;

    @Autowired
    private DriverSchedulingExpandMapper expandMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void smartAdd(SmartExpandDto dto) {
        List<DriverSchedulingExpandSmart> list = new ArrayList<>(dto.getActivities().size());
        for (SmartExpandDto.Activity activity : dto.getActivities()) {
            DriverSchedulingExpandSmart smart = new DriverSchedulingExpandSmart();
            BeanUtils.copyProperties(activity, smart, "id");
            smart.setSlaveJobCode(dto.getSlaveJobCode());
            list.add(smart);
        }
        list.sort(Comparator.comparing(DriverSchedulingExpandSmart::getStartDate));
        // 校验跟班时间正确
        isExpandCorrect(list);
        // 保存记录
        DriverSchedulingExpand expand = new DriverSchedulingExpand();
        BeanUtils.copyProperties(list.get(0), expand);
        expand.setType(dto.getType());
        expand.setRemark(dto.getRemark());
        expandService.insertDriverSchedulingExpand(expand);
        for (DriverSchedulingExpandSmart item : list) {
            item.setExpandId(expand.getId().toString());
        }
        this.saveBatch(list);
    }

    @Override
    public void computedExpand() {
        try {
            List<SmartExpandVo> vos = baseMapper.querySmartExpandAll();
            int sum = 0;
            for (SmartExpandVo vo : vos) {
                SmartExpandVo.Activity activity = handlerSmartExpand(vo);
                DriverSchedulingExpand expand = new DriverSchedulingExpand();
                BeanUtils.copyProperties(activity, expand);
                expand.setId(vo.getId());
                expand.setType(1);
                expand.setSlaveJobCode(vo.getSlaveJobCode());
                int result = expandMapper.updateDriverSchedulingExpand(expand);
                sum += result;
            }
            log.info("跟班计算完毕,共修改{}数据", sum);

        } catch (Exception e) {
            log.info("跟班计算出现问题:{}", e.getMessage());
        }
    }

    private SmartExpandVo.Activity handlerSmartExpand(SmartExpandVo vo) {
        vo.getActivities().sort(Comparator.comparing(SmartExpandVo.Activity::getStartDate));
        Date date = new Date();
        for (int i = 0; i < vo.getActivities().size(); i++) {
            int compare = StringUtils.compare(ConstDateUtil.formatDate(date), ConstDateUtil.formatDate(vo.getActivities().get(i).getEndDate()));
            if (compare > 0 && i < vo.getActivities().size() - 1) {
                continue;
            }
            return vo.getActivities().get(i);
        }
        log.error("没有找到跟班对象:{}", vo);
        throw new ServiceException("未知错误", ResultCode.CODE_500.getCode());
    }

    @Override
    public SmartExpandVo querySmartExpand(Long id) {
        return baseMapper.querySmartExpand(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSmartExpand(SmartExpandVo vo) {
        List<DriverSchedulingExpandSmart> list = new ArrayList<>(vo.getActivities().size());
        // 删除老数据
        LambdaQueryWrapper<DriverSchedulingExpandSmart> qw = new LambdaQueryWrapper<>();
        qw.eq(DriverSchedulingExpandSmart::getExpandId, vo.getId());
        remove(qw);
        // COPY
        for (SmartExpandVo.Activity activity : vo.getActivities()) {
            DriverSchedulingExpandSmart smart = new DriverSchedulingExpandSmart();
            BeanUtils.copyProperties(activity, smart, "id");
            smart.setSlaveJobCode(vo.getSlaveJobCode());
            list.add(smart);
        }
        list.sort(Comparator.comparing(DriverSchedulingExpandSmart::getStartDate));
        for (DriverSchedulingExpandSmart item : list) {
            item.setExpandId(vo.getId().toString());
        }
        this.saveBatch(list);
        // 更新当前跟班显示
        DriverSchedulingExpandSmart smart = ToolUtils.computedShowSmart(list);
        DriverSchedulingExpand expand = new DriverSchedulingExpand();
        BeanUtils.copyProperties(expand, smart, "id");
        expand.setRemark(vo.getRemark());
        expand.setType(vo.getType());
        expand.setId(vo.getId());
        expandService.updateDriverSchedulingExpand(expand);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteExpand(Long[] ids) {
        expandService.deleteDriverSchedulingExpandByIds(ids);
        LambdaQueryWrapper<DriverSchedulingExpandSmart> qw = new LambdaQueryWrapper<>();
        qw.eq(DriverSchedulingExpandSmart::getExpandId, ids[0]);
        remove(qw);
    }

    private void isExpandCorrect(List<DriverSchedulingExpandSmart> list) {
        // 校验跟班人选中的时间日期是否合法
        long day = 1000 * 60 * 60 * 24;
        for (int i = 0; i < list.size() - 1; i++) {
            if (list.get(i + 1).getStartDate().getTime() - list.get(i).getEndDate().getTime() < day) {
                String errMsg = "时间区间设置不正确,请确保时间是递增的" + ConstDateUtil.formatDate("yyyy-MM-dd", list.get(i).getEndDate()) + "->" + ConstDateUtil.formatDate("yyyy-MM-dd", list.get(i + 1).getStartDate());
                throw new ServiceException(errMsg, ResultCode.CODE_400.getCode());
            }
        }
    }
}