RcsServiceImpl.java 9.19 KB
package com.huaheng.api.rcs.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.acs.domain.AgvTaskCS;
import com.huaheng.api.acs.service.AgvTaskCSService;
import com.huaheng.api.mes.domain.MesTask;
import com.huaheng.api.mes.service.MesService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.sendMail.service.MailService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * RCS实现层
 * @author xcq
 */
@Service
public class RcsServiceImpl implements RcsService{

    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private RcsService rcsService;
    @Resource
    private ContainerService containerService;
    @Resource
    private LocationService locationService;
    @Resource
    private AgvTaskCSService agvTaskCSService;

    @Override
    @Transactional
    public AjaxResult receiptTaskComplete(AgvTaskCS agvTaskCS) {
        // 查询入库单
        LambdaQueryWrapper<ReceiptHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReceiptHeader::getCode, agvTaskCS.getFromUserDef1());
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(queryWrapper);
        if (receiptHeader == null){
            return AjaxResult.error("查询入库单失败!");
        }
        LambdaQueryWrapper<ReceiptDetail> query = Wrappers.lambdaQuery();
        query.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
        ReceiptDetail receiptDetail = receiptDetailService.getOne(query);
        if (receiptDetail == null){
            return AjaxResult.error("查询入库单详情失败!");
        }
        // 获取可用容器编码
        Container  container = rcsService.getContainer(QuantityConstant.GT_RECEIPT_TYPE);
        if (container == null){
            return AjaxResult.error("无可用托盘码!");
        }
        // 获取入库库位
        Location location = locationService.getLocationByCode(agvTaskCS.getToPort(), QuantityConstant.WAREHOUSE_KS);
        if (location == null){
            return AjaxResult.error("根据库位编码"+agvTaskCS.getToPort()+"查询入库库位失败!");
        }
        // 添加库存
        AjaxResult result = rcsService.addInventory(receiptDetail, container.getCode(), location.getCode(), location.getZoneCode());
        if (result.hasErr()){
            return result;
        }
        Location locationTemp = new Location();
        locationTemp.setId(location.getId());
        locationTemp.setContainerCode(container.getCode());
        locationTemp.setStatus(QuantityConstant.STATUS_LOCATION_EMPTY);
        if (!locationService.updateById(locationTemp)){
            throw new RuntimeException("更新库位数据失败!");
        }
        Container containerTemp = new Container();
        containerTemp.setId(container.getId());
        containerTemp.setLocationCode(location.getCode());
        containerTemp.setStatus(QuantityConstant.STATUS_CONTAINER_SOME);
        if (!containerService.updateById(containerTemp)){
            throw new RuntimeException("更新容器状态失败!");
        }
        AgvTaskCS agvTaskCSTemp = new AgvTaskCS();
        agvTaskCSTemp.setId(agvTaskCS.getId());
        agvTaskCSTemp.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
        if (!agvTaskCSService.updateById(agvTaskCSTemp)){
            throw new RuntimeException("更新任务状态失败!");
        }
        ReceiptHeader receiptHeaderTemp = new ReceiptHeader();
        receiptHeaderTemp.setId(receiptHeader.getId());
        receiptHeaderTemp.setLastStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
        receiptHeaderTemp.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
        if (!receiptHeaderService.updateById(receiptHeaderTemp)){
            throw new RuntimeException("更新入库单头表失败!");
        }
        return AjaxResult.success("任务状态更新成功!");
    }

    @Override
    public AjaxResult addInventory(ReceiptDetail receiptDetail,String containerCode, String locationCode, String zoneCode) {
        // 添加库存头
        InventoryHeader inventoryHeader = new InventoryHeader();
        inventoryHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
        inventoryHeader.setLocationCode(locationCode);
        inventoryHeader.setContainerCode(containerCode);
        inventoryHeader.setWarehouseCode(QuantityConstant.WAREHOUSE_KS);
        inventoryHeader.setCompanyCode(QuantityConstant.COMPANY_GT);
        inventoryHeader.setContainerStatus(QuantityConstant.STATUS_CONTAINER_SOME);
        inventoryHeader.setTotalQty(receiptDetail.getQty());
        inventoryHeader.setTotalLines(1);
        inventoryHeader.setZoneCode(zoneCode);
        inventoryHeader.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
        if (!inventoryHeaderService.save(inventoryHeader)){
            throw new RuntimeException("新增库存头失败!");
        }
        InventoryDetail inventoryDetail = new InventoryDetail();
        inventoryDetail.setInventoryHeaderId(inventoryHeader.getId());
        inventoryDetail.setWarehouseCode(inventoryHeader.getWarehouseCode());
        inventoryDetail.setCompanyCode(inventoryHeader.getCompanyCode());
        inventoryDetail.setLocationCode(locationCode);
        inventoryDetail.setContainerCode(containerCode);
        inventoryDetail.setMaterialName(receiptDetail.getMaterialName());
        inventoryDetail.setMaterialCode(receiptDetail.getMaterialCode());
        inventoryDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
        inventoryDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
        inventoryDetail.setQty(receiptDetail.getQty());
        inventoryDetail.setTaskQty(BigDecimal.ZERO);
        inventoryDetail.setReceiptCode(receiptDetail.getReceiptCode());
        inventoryDetail.setReceiptDetailId(receiptDetail.getId());
        inventoryDetail.setInventorySts(QuantityConstant.GOOD);
        inventoryDetail.setZoneCode(zoneCode);
        inventoryDetail.setCreatedBy(inventoryHeader.getCreatedBy());
        inventoryDetail.setLastUpdatedBy(QuantityConstant.PLATFORM_MES);
        inventoryDetail.setReferCode(receiptDetail.getReferCode());
        if (!inventoryDetailService.save(inventoryDetail)){
            throw new RuntimeException("新增库存详情失败!");
        }
        return AjaxResult.success("库存添加成功!");
    }

    @Override
    public Container getContainer(String containerType) {
        LambdaQueryWrapper<Container> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Container::getContainerType, containerType)
                .eq(Container::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY)
                .eq(Container::getLocationCode, "").last("LIMIT 1");
        return containerService.getOne(queryWrapper);
    }

    @Resource
    private ZoneService zoneService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private MesService mesService;

    @Override
    public void autoCreateGTAgvTaskService() {
        LambdaQueryWrapper<TaskHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.lt(TaskHeader::getStatus, QuantityConstant.AGV_TASK_STATUS_RELEASE)
                .eq(TaskHeader::getZoneCode, QuantityConstant.GT_RECEIPT_TYPE);
        List<TaskHeader> taskHeaderList = taskHeaderService.list(queryWrapper);
        if (taskHeaderList != null && taskHeaderList.size() > 0){
            for (TaskHeader taskHeader : taskHeaderList){
                // 向光腾RCS下发任务
                mesService.sendTaskToAcs(taskHeader);
            }
        }

    }


}