skyouc
2025-01-11 645da5e4e48dd94b760ea4dcbf80e22bf2a3f33c
zy-asrs-wms/src/main/java/com/zy/asrs/wms/asrs/service/impl/MobileServiceImpl.java
@@ -1,54 +1,323 @@
package com.zy.asrs.wms.asrs.service.impl;
import com.zy.asrs.wms.asrs.entity.WaitPakin;
import com.zy.asrs.wms.asrs.entity.enums.LocTypeHeightType;
import com.zy.asrs.wms.asrs.entity.enums.TaskStsType;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.asrs.entity.*;
import com.zy.asrs.wms.asrs.entity.dto.PickSheetDetlDto;
import com.zy.asrs.wms.asrs.entity.enums.*;
import com.zy.asrs.wms.asrs.entity.enums.OrderType;
import com.zy.asrs.wms.asrs.entity.param.BatchMergeOrdersParam;
import com.zy.asrs.wms.asrs.entity.param.GeneratePakInParam;
import com.zy.asrs.wms.asrs.service.MobileService;
import com.zy.asrs.wms.asrs.service.WaitPakinService;
import com.zy.asrs.wms.asrs.service.WorkService;
import com.zy.asrs.wms.asrs.entity.param.PageRequest;
import com.zy.asrs.wms.asrs.entity.param.PakinOnShelvesParams;
import com.zy.asrs.wms.asrs.mapper.CacheSiteMapper;
import com.zy.asrs.wms.asrs.service.*;
import com.zy.asrs.wms.system.entity.Host;
import com.zy.asrs.wms.system.service.HostService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class MobileServiceImpl implements MobileService {
    @Autowired
    private WaitPakinService waitPakinService;
    @Autowired
    private WaitPakinLogService waitPakinLogService;
    @Autowired
    private WorkService workService;
    @Autowired
    private HostService hostService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private OrderLogService orderLogService;
    @Autowired
    private LocService locService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private LocAreaService locAreaService;
    @Autowired
    private LocAreaTypeService locAreaTypeService;
    @Autowired
    private PickSheetService pickSheetService;
    @Autowired
    private PickSheetDetlService pickSheetDetlService;
    @Autowired
    private MatService matService;
    @Autowired
    private WaveService waveService;
    @Autowired
    private WaveDetlService waveDetlService;
    @Autowired
    private CacheSiteMapper cacheSiteMapper;
    @Override
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public boolean batchMergeOrders(BatchMergeOrdersParam ordersParam) {
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo, ordersParam.getOrderNo()));
        if (Objects.isNull(order)) {
            throw new CoolException("订单不存在!!");
        }
        ArrayList<WaitPakin> waitPakins = new ArrayList<>();
        ordersParam.getOrderDetls().forEach(orderdetl -> {
            WaitPakin waitPakin = new WaitPakin();
            waitPakin.setBatch(orderdetl.getBatch()).setAnfme(orderdetl.getMergeNum()).setBarcode(ordersParam.getMergeNo()).setMatnr(orderdetl.getMatnr()).setDetlId(orderdetl.getDetlId()).setIoStatus(0).setOrderNo(orderdetl.getOrderNo()).setOrderId(orderdetl.getOrderId()).setStatus(1);
            waitPakin.setBatch(orderdetl.getBatch())
                    .setAnfme(orderdetl.getMergeNum())
                    .setBarcode(ordersParam.getMergeNo())
                    .setMatnr(orderdetl.getMatnr())
                    .setDetlId(orderdetl.getDetlId())
                    .setIoStatus(0)
                    .setOrderNo(orderdetl.getOrderNo()).setOrderId(orderdetl.getOrderId()).setStatus(1);
            waitPakins.add(waitPakin);
        });
        /*** 项目下发流程 * 1. PDA组拖通知档* 2. 生成任务档* 3. 通过定时任务下发至ESS** */
        //组拖通知档
        waitPakins.forEach(pakin -> {
            waitPakinService.comb(pakin);
        });
        //生成任务档
        GeneratePakInParam generatePakInParam = new GeneratePakInParam();
        //当前没有起始站点,默认101, 高低位默认传低库位
        generatePakInParam.setBarcode(ordersParam.getMergeNo())
                .setOriginSite("101")
                .setTaskType(TaskStsType.GENERATE_IN.id).setLocTypeHeight(LocTypeHeightType.LOW.id);
        if (workService.generatePakIn(generatePakInParam)) {
            return true;
        // UTC入库单据(非平库入库单据)
        if (order.getOrderType() != OrderType.PK_IN_ORDER.id) {
            /*** 项目下发流程 * 1. PDA组拖通知档* 2. 生成任务档* 3. 通过定时任务下发至ESS** */
            //生成任务档
            GeneratePakInParam generatePakInParam = new GeneratePakInParam();
            //当前没有起始站点,默认101, 高低位默认传低库位
            generatePakInParam.setBarcode(ordersParam.getMergeNo())
                    .setOriginSite("101")
                    .setTaskType(TaskStsType.GENERATE_IN.id).setLocTypeHeight(LocTypeHeightType.LOW.id);
            if (workService.generatePakIn(generatePakInParam)) {
                return true;
            }
        } else {
            //fixme 平库是否需要预约入库
        }
        return false;
    }
    @Override
    public List<Host> getHosts() {
        return hostService.list();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean pakinToStock(PakinOnShelvesParams shelvesParams) {
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, shelvesParams.getLoc()));
        if (Objects.isNull(loc)) {
            throw new CoolException("库位不存在!!");
        }
        if (loc.getLocStsId() != LocStsType.O.val()) {
            throw new CoolException("当前库位状态不是空库状态." + LocStsType.O.val());
        }
        //判断当前仓库是否为平库位
        List<LocArea> locAreas = locAreaService.list(new LambdaQueryWrapper<LocArea>().eq(LocArea::getLocId, loc.getId()));
        if (locAreas.isEmpty()) {
            throw new CoolException("库位没有分配所属仓库区域!!");
        }
        locAreas.forEach(locArea -> {
            LocAreaType typeServiceOne = locAreaTypeService.getOne(new LambdaQueryWrapper<LocAreaType>().eq(LocAreaType::getId, locArea.getTypeId()), false);
            if (typeServiceOne.getParentId() != LocAreaTypeSts.LOC_AREA_TYPE_FLAT.id && typeServiceOne.getId() != LocAreaTypeSts.LOC_AREA_TYPE_FLAT.id) {
                throw new CoolException("请选择平库区库位,再操作!!");
            }
        });
        loc.setBarcode(shelvesParams.getBarcode());
        loc.setUpdateTime(new Date());
        //库存状态修改为在库状态
        loc.setLocStsId(LocStsType.F.val());
        if (!locService.updateById(loc)) {
            throw new CoolException("库位更新失败!!");
        }
        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, shelvesParams.getBarcode()));
        waitPakins.forEach(pakin -> {
            LocDetl locDetl = new LocDetl();
            locDetl.setAnfme(pakin.getAnfme());
            locDetl.setBatch(pakin.getBatch());
            locDetl.setMatId(pakin.getMatnrId$());
            locDetl.setCreateTime(new Date());
            locDetl.setOrderNo(pakin.getOrderNo());
            locDetl.setLocNo(loc.getLocNo());
            locDetl.setLocId(loc.getId());
            locDetl.setMatnr(pakin.getMatnr());
            Mat one = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, pakin.getMatnr()));
            if (!Objects.isNull(one)) {
                throw new CoolException("当前物料不存在!!");
            }
            locDetl.setMatId(one.getId());
            locDetl.setMemo(pakin.getMemo());
            locDetl.setUpdateTime(new Date());
            if (!locDetlService.saveOrUpdate(locDetl)) {
                throw new CoolException("库存明细更新失败!!");
            }
            //修改状态为入库中
            pakin.setIoStatus(1);
        });
        //删除组拖档,加入历史组拖档
        waitPakins.forEach(waitPakin -> {
            WaitPakinLog pakinLog = new WaitPakinLog();
            BeanUtils.copyProperties(waitPakin, pakinLog);
            if (!waitPakinLogService.saveOrUpdate(pakinLog)) {
                throw new CoolException("组拖历史档更新失败");
            }
        });
        if (!waitPakinService.removeBatchByIds(waitPakins)) {
            throw new CoolException("组拖档删除失败!!");
        }
        //通过组拖订单ID获取订单,并删除原单据,加入单据历史档
        List<Long> list = waitPakins.stream().map(WaitPakin::getOrderId).collect(Collectors.toList());
        List<Order> orders = orderService.list(new LambdaQueryWrapper<Order>().in(Order::getId, list));
        orders.forEach(order -> {
            OrderLog orderLog = new OrderLog();
            BeanUtils.copyProperties(order, orderLog);
            if (!orderLogService.save(orderLog)) {
                throw new CoolException("历史单据更新失败!!");
            }
        });
        if (!orderService.removeBatchByIds(orders)) {
            throw new CoolException("订单删除失败!!");
        }
        return true;
    }
    @Override
    public PickSheetDetlDto outFlatSheet(String code) {
        PickSheetDetlDto pickDetlDto = new PickSheetDetlDto();
        PickSheet pickSheet = pickSheetService.getOne(new LambdaQueryWrapper<PickSheet>()
                .eq(PickSheet::getPickNo, code)
                .eq(PickSheet::getDeleted, 0)
                .eq(PickSheet::getStatus, 1)
        );
        BeanUtils.copyProperties(pickSheet, pickDetlDto);
        if (Objects.isNull(pickDetlDto)) {
            throw new CoolException("对象复制失败!!");
        }
        List<PickSheetDetl> sheetDetls = pickSheetDetlService.list(new LambdaQueryWrapper<PickSheetDetl>()
                .eq(PickSheetDetl::getPickId, pickSheet.getId())
                .eq(PickSheetDetl::getStatus, 1)
                .eq(PickSheetDetl::getDeleted, 0));
        pickDetlDto.setPickSheetDetlList(sheetDetls);
        return pickDetlDto;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean confirmFlatSheet(String code) {
        PickSheet pickSheet = pickSheetService.getOne(new LambdaQueryWrapper<PickSheet>().eq(PickSheet::getPickNo, code));
        if (Objects.isNull(pickSheet)) {
            throw new CoolException("拣货单不存在!!");
        }
        if (pickSheet.getStatus() == 1) {
            //状态修改为: 已转历史档
            pickSheet.setStatus(3);
            if (!pickSheetService.updateById(pickSheet)) {
                throw new CoolException("转历史档失败!!");
            } else{
                if (!pickSheetDetlService.update(new LambdaUpdateWrapper<PickSheetDetl>().eq(PickSheetDetl::getPickId, pickSheet.getId()).set(PickSheetDetl::getStatus, 3))) {
                    throw new CoolException("拣货单明细转历史档失败!!");
                }
            }
        } else {
            throw new CoolException("请勿操作历史档数据!!");
        }
        List<PickSheetDetl> sheetDetls = pickSheetDetlService.list(new LambdaQueryWrapper<PickSheetDetl>().eq(PickSheetDetl::getPickId, pickSheet.getId()));
        Map<Long, List<PickSheetDetl>> listMap = sheetDetls.stream().collect(Collectors.groupingBy(PickSheetDetl::getLocId));
        listMap.keySet().forEach(key -> {
            Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getId, key));
            listMap.get(key).forEach(pickDetl -> {
                LocDetl detl = locDetlService.getOne(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getId, pickDetl.getLocDetlId()));
                if (Objects.isNull(detl)) {
                    throw new CoolException("数据错误!!");
                }
                //更新锁定库存
                BigDecimal workQty = BigDecimal.valueOf(detl.getAnfme()).subtract(pickDetl.getAnfme());
                detl.setWorkQty(workQty.doubleValue());
                //计算结果小于等于零,移出库存明细
                BigDecimal qty = BigDecimal.valueOf(detl.getAnfme()).subtract(pickDetl.getAnfme());
                detl.setAnfme(qty.doubleValue());
                if (qty.compareTo(new BigDecimal("0.00")) <= 0) {
                    detl.setDeleted(1);
                    if (!locDetlService.removeById(detl)) {
                        throw new CoolException("库存明细删除失败!!");
                    }
                } else { //结果大于零,更新库存明细
                    if (!locDetlService.updateById(detl)) {
                        throw new CoolException("库存明细更新失败!!");
                    }
                }
            });
            /**明细更新完成后,判断是否为空,为空更新主单状态为空库,拖盘码清空,不为空不做更新**/
            List<LocDetl> list = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, loc.getId()).eq(LocDetl::getDeleted, 0));
            if (list.isEmpty()) {
                boolean update = locService.update(new LambdaUpdateWrapper<Loc>()
                        .eq(Loc::getId, loc.getId())
                        .set(Loc::getBarcode, null)
                        .set(Loc::getUpdateTime, new Date())
                        .set(Loc::getLocStsId, LocStsType.O.val()));
                if (update) {
                    throw new CoolException("库存更新失败!! ");
                }
            }
        });
        //删除波次及对应明细信息
        if (!waveService.remove(new LambdaQueryWrapper<Wave>().eq(Wave::getId, pickSheet.getWaveId()))) {
            throw new CoolException("波次列表单据删除失败!!");
        }
        if (!waveDetlService.remove(new LambdaQueryWrapper<WaveDetl>().eq(WaveDetl::getWaveId, pickSheet.getWaveId()))) {
            throw new CoolException("波次明细列表删除失败!!");
        }
        List<Order> orders = orderService.list(new LambdaQueryWrapper<Order>().eq(Order::getWaveId, pickSheet.getWaveId()));
        List<Long> list = orders.stream().map(Order::getId).collect(Collectors.toList());
        boolean remove = orderDetlService.remove(new LambdaQueryWrapper<OrderDetl>().in(OrderDetl::getOrderId, list));
        if (!remove) {
            throw new CoolException("原始订单明细删除失败!!");
        }
        if (orderService.remove(new LambdaQueryWrapper<Order>().in(Order::getId, list))) {
            throw new CoolException("原始订单删除失败!!");
        }
        return true;
    }
    @Override
    public List<CacheSite> getSeedLocs() {
        return cacheSiteMapper.selectList(new LambdaQueryWrapper<>());
    }
    /**
     * 订单绑定播种墙
     * @param param
     * @return
     */
    @Override
    public boolean bindOrderBySite(Map<String, Object> param) {
//        return cacheSiteMapper.bindOrderBySite(param);
        return false;
    }
}