WaveService.java 3.42 KB
package com.huaheng.pc.shipment.wave.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.wave.domain.Wave;
import com.huaheng.pc.shipment.wave.mapper.WaveMapper;
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.Arrays;
import java.util.List;

@Service
public class WaveService extends ServiceImpl<WaveMapper, Wave> {


    @Autowired
    private ShipmentDetailService shipmentDetailService;
    @Autowired
    private ShipmentContainerHeaderService shipmentContainerHeaderService;

    /**
     * 开始波次,对带有此波次号的单据进行后续的后台操作
     * 1、查看波次是否符合开始波次的条件
     * 2、整合波次的所有子单单据
     * 3、对每个单据自动组盘,失败则回退
     * 4、生成任务
     * 5、修改波次的状态
     */

    @Transactional
    public AjaxResult startWave(String ids) {
        List<Wave> waves = new ArrayList<>();
        List<ShipmentDetail> list = new ArrayList<>();
        for (Integer id : Convert.toIntArray(ids)) {

            //查看此波次的状态,状态不为o时,无法开始波次
            Wave wave = this.getById(id);
            if(wave == null || wave.getStatus() != 0){
                return AjaxResult.error("id为"+id+"的波次找不到,或者状态不能做开始操作");
            }
            waves.add(wave);

            //找到此波次的单据
            LambdaQueryWrapper<ShipmentDetail> lam = Wrappers.lambdaQuery();
            lam.eq(ShipmentDetail::getWaveId,id)
                .eq(ShipmentDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());
            List<ShipmentDetail> shipmentDetails=shipmentDetailService.list(lam);

            //整合所有加入波次的单据
            if(shipmentDetails != null){
                list.addAll(shipmentDetails);
            }
        }

        //自动组盘
        AjaxResult ajaxResult=shipmentContainerHeaderService.autoCombination(list);
        if(ajaxResult.getData() != null){
            //生成任务
            shipmentContainerHeaderService.createTask(Arrays.asList(Convert.toIntArray(ajaxResult.getData().toString())));
        }

        for(Wave wave : waves){
            wave.setStatus(400);
            wave.setCurrentWaveStep("生成任务");
        }
        Boolean flag = this.updateBatchById(waves);
        if(flag == false){
            throw new ServiceException("波次运行失败,修改波次状态时报错");
        }
        return AjaxResult.success("波次运行成功");
    }


    /**
     * 释放波次,执行任务
     * @param ids
     * @return
     */
    @Transactional
    public AjaxResult freed(String ids) {

        return null;
    }
}