TaskHeaderServiceImpl.java 22.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
package com.huaheng.pc.task.taskHeader.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DataUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
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.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.mapper.TaskDetailMapper;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.ShipmentTaskCreateModel;
import com.sun.jmx.snmp.tasks.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.mapper.TaskHeaderMapper;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class TaskHeaderServiceImpl extends ServiceImpl<TaskHeaderMapper, TaskHeader> implements TaskHeaderService {

    @Autowired
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Autowired
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Autowired
    private LocationService locationService;
    @Autowired
    private InventoryDetailService inventoryDetailService;
    @Autowired
    private TaskDetailService taskDetailService;
    @Autowired
    private TaskHeaderService taskHeaderService;

    @Autowired
    private InventoryHeaderService inventoryHeaderService;

    @Autowired
    private InventoryTransactionService  inventoryTransactionService;

    @Resource
    private TaskHeaderMapper taskHeaderMapper;

    /**
     * 生成出库任务
     *
     * @param shipmentTaskCreateModel
     * @return
     */
    @Override
    @Transactional
    public AjaxResult createTaskFromShipmentContainers(ShipmentTaskCreateModel shipmentTaskCreateModel) {
        Integer shipmentContainerHeaderId = shipmentTaskCreateModel.getShipmentContainerHeaderIds();
        //获取表头
        ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getById(shipmentContainerHeaderId);
        if (shipmentContainerHeader == null) {
            return AjaxResult.error("出库货箱" + shipmentContainerHeaderId + "未找到,操作中止");
        }
        if (shipmentContainerHeader.getStatus() > 9) {
            return AjaxResult.error("出库货箱" + shipmentContainerHeader.getContainerCode() + "已经生成任务,请不要重复生成,操作中止");
        }
        //获取所有子货箱
        LambdaQueryWrapper<ShipmentContainerDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentContainerDetail::getShippingContainerId, shipmentContainerHeader.getId());
        List<ShipmentContainerDetail> shipmentContainerDetails = shipmentContainerDetailService.list(lambdaQueryWrapper);
        if (shipmentContainerDetails == null || shipmentContainerDetails.size() == 0) {
            return AjaxResult.error("货箱" + shipmentContainerHeader.getContainerCode() + "没有子任务,操作中止");
        }
        //检测库位
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper.eq(Location::getCode, shipmentContainerHeader.getLocationCode())
                .eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(Location::getDeleted, false);
        Location location = locationService.getOne(locationLambdaQueryWrapper);
        if (location == null) {
            return AjaxResult.error("库位禁用或不存在!");
        }
        //创建任务头
        TaskHeader task = new TaskHeader();
        //分拣出库
        task.setTaskType(400);
        task.setLocationCode(shipmentContainerHeader.getLocationCode());
        //判断是否整出任务,钱柜和AGV不能整出
        if (shipmentContainerHeader.getStatus().intValue() == 300) {
            //表示整出优先
            //判断当前子货箱所有数量是否等于该托盘对应的所有库存的数量,
            //这里必须与库存的在库数量对比,后期可能存在一个配盘在执行任务,后一个配盘又在配这个的情况(这个时候不能整出)
            // 如果相等,则说明这个货箱包含了所有的数量,则可以整出,否则,创建拣选任务;
            //查询所有库存
            InventoryDetail inventoryCondition = new InventoryDetail();
            LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getLocationCode, shipmentContainerHeader.getLocationCode())
                    .eq(InventoryDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());
            List<InventoryDetail> inventories = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
            BigDecimal inventoryTotal = new BigDecimal("0");
            for (InventoryDetail item : inventories) {
                inventoryTotal = inventoryTotal.add(item.getQty());
            }
            BigDecimal containerTotal = new BigDecimal("0");
            for (ShipmentContainerDetail item : shipmentContainerDetails) {
                containerTotal = containerTotal.add(item.getQty());
            }
            if (inventoryTotal.compareTo(containerTotal) == 0) {
                task.setTaskType(300);//整盘出库
                task.setLocationCode("");
            }
        }
        task.setWarehouseCode(shipmentContainerHeader.getWarehouseCode());
        task.setCompanyCode(shipmentContainerHeader.getCompanyCode());
        task.setInternalTaskType(null);
        task.setAssignedUser(ShiroUtils.getLoginName());
        task.setConfirmedBy(ShiroUtils.getLoginName());
        task.setStatus(1);
        task.setContainerCode(shipmentContainerHeader.getContainerCode());
        task.setCreatedBy(ShiroUtils.getLoginName());
        task.setCreated(new Date());
        task.setLastUpdatedBy(ShiroUtils.getLoginName());
        task.setLastUpdated(null);
        this.save(task);
        //遍历子货箱创建子任务
        for (ShipmentContainerDetail shipmentContainerDetail : shipmentContainerDetails) {
            TaskDetail taskDetail = new TaskDetail();
            taskDetail.setTaskId(task.getId());
            taskDetail.setWarehouseCode(task.getWarehouseCode());
            taskDetail.setCompanyCode(task.getCompanyCode());
            taskDetail.setTaskType("s");
            taskDetail.setBillCode(shipmentContainerDetail.getShipmentCode());
            taskDetail.setBillId(shipmentContainerDetail.getShipmentId());
            taskDetail.setBillDetailId(shipmentContainerDetail.getShipmentDetailId());
            taskDetail.setMaterialCode(shipmentContainerDetail.getMaterialCode());
            taskDetail.setMaterialName(shipmentContainerDetail.getMaterialName());
            taskDetail.setMaterialSpec(shipmentContainerDetail.getMaterialSpec());
            taskDetail.setMaterialUnit(shipmentContainerDetail.getMaterialUnit());
            taskDetail.setFromInventoryId(shipmentContainerDetail.getInventoryId());
            taskDetail.setQty(shipmentContainerDetail.getQty());
            taskDetail.setContainerCode(task.getContainerCode());
            taskDetail.setFromLocation(task.getLocationCode());
            taskDetail.setToLocation(task.getLocationCode());
            taskDetail.setStatus(1);
            taskDetail.setTaskType("s");
            taskDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
            taskDetail.setLastUpdated(null);
            taskDetailService.save(taskDetail);
        }
        //更新货位状态
        ShipmentContainerHeader record = new ShipmentContainerHeader();
        record.setId(shipmentContainerHeaderId);
        record.setStatus(10);
        record.setStatus(task.getTaskType());//实际出库类型
        shipmentContainerHeaderService.saveOrUpdate(record);
        return AjaxResult.success(task.getId());

    }

    @Override
    public AjaxResult<TaskHeader> sendTaskToWcs(Integer[] taskIds) {
        TaskHeader task = null;
        for (Integer taskId : taskIds) {
            task = taskHeaderService.getById(taskId);
            if (task.getStatus() > 9) {
                return AjaxResult.error("任务" + taskId + "已经下发,请不要重复下发,操作中止");
            }
            //修改任务头表
            task.setStatus(10);
            task.setStartPickDateTime(new Date());   //生成时间
            task.setLastUpdated(new Date());
            task.setLastUpdatedBy(ShiroUtils.getLoginName());
            LambdaUpdateWrapper<TaskHeader> HeaderUpdateWrapper = Wrappers.lambdaUpdate();
            HeaderUpdateWrapper.eq(TaskHeader::getId, taskId);
            taskHeaderService.update(task, HeaderUpdateWrapper);
            //修改任务明细状态
            TaskDetail record = new TaskDetail();
            record.setStatus(10);
            record.setLastUpdated(new Date());
            record.setLastUpdatedBy(ShiroUtils.getLoginName());
            LambdaUpdateWrapper<TaskDetail> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
            lambdaUpdateWrapper.eq(TaskDetail::getTaskId, task.getId());
            taskDetailService.update(record, lambdaUpdateWrapper);


//            if (task.getInternalTaskType().equals("100"))
//            {
//                List<Map<String, Object>> maps = taskDetailService.selectListMapByEqual("billId, billDetailId", condition);
//                for (Map<String, Object> item : maps){
//                    Integer billDetailId = DataUtils.getInteger(item.get("billDetailId"));
//                    receiptHeaderService.updateDetailStatus(billDetailId, (short)300);
//                }
//                maps.stream().map(X -> X.get("billId")).distinct().forEach(X -> receiptHeaderService.receiptStatusUpdate(DataUtils.getInteger(X), (short)300));
//            }
//            //盘点单执行
//            if(task.getType() == 700){
//                CyclecountDetail cyclecountDetai = cyclecountDetailService.selectEntityById(
//                        task.getAllocationHeadId());
//                cyclecountDetai.setStatus(10);
//                cyclecountDetailService.updateByModel(cyclecountDetai);
//            }
////            //如果是单排人工库,那么出入库都是先完成,在下发AGV任务
////            if (task.getType().intValue() == 300 || task.getType().intValue() == 600)   {
////                if (task.getSourceLocation().startsWith("L03") == false )   {
////                    taskAgvService.createTaskAgv(task);
////                }
////            }
////            else    {
////                taskAgvService.createTaskAgv(task);
////            }
//            //任务类型是出库,那就完成任务在叫agv
//            if (task.getType().intValue() == 100 || task.getType().intValue() == 500)   {
//                taskAgvService.createTaskAgv(task);
//            }
        }
        return AjaxResult.success("下发任务成功", task);
    }

    @Override
    public AjaxResult completeTaskByWMS(Integer[] taskIds) throws Exception {
        for (int taskId : taskIds) {
            TaskHeader task = taskHeaderService.getById(taskId);
            if (task == null) {
                throw new ServiceException("任务" + taskId + "未找到,执行中止");
            }
            //如果已完成则不管
            if (task.getStatus() == 100) {
                throw new ServiceException("任务(" + taskId + ")任务已经是完成的!");
            }
            //如果没有库位不能完成
            if (StringUtils.isEmpty(task.getLocationCode())) {
                throw new ServiceException("任务" + taskId + "没有库位,执行中止");
            }
            this.completeTask(task);
        }

        return AjaxResult.success("完成任务成功!");
    }

    /**
     * 完成任务
     *
     * @param task
     * @throws Exception
     */
    public void completeTask(TaskHeader task) throws Exception {
        //区分任务类型
        if (task.getInternalTaskType() == 100 || task.getInternalTaskType() == 200) {
            //入库任务
            completeReceiptTask(task);
        }
        if (task.getInternalTaskType() == 300 || task.getInternalTaskType() == 400) {
//            //出库任务
//            completeShipmentTask(task);
        }
        //...其他任务类型暂时不处理    // 700 盘点   800 移库  900 出库查看
        if (task.getInternalTaskType() == 700 || task.getInternalTaskType() == 900) {
//            completeCycleCountOrSeeOutTask(task);
        }
        if (task.getInternalTaskType() == 800) {
//            //移库
//            completeTransferTask(task);
        }
        if (task.getInternalTaskType() == 500) {
//            //空托盘入库
//            completeEmptyIn(task);
        }
        if (task.getInternalTaskType() == 600) {
//            //空托盘出库
//            completeEmptyOut(task);
        }
    }

    @Override
    public AjaxResult completeReceiptTask(TaskHeader task) throws Exception {
//        String LocationCode = task.getIsDoubleIn().intValue() == 0 ? task.getDestinationLocation() : task.getSecondDestinationLocation();
        List<Map<String, Object>> taskReceiptContainerDetail =taskHeaderMapper.getReceiptTask(task.getId());
        if (taskReceiptContainerDetail.size()<1){
            return AjaxResult.success("未找到对应任务的入库单号!!!");
        }
        for (Map<String, Object> map : taskReceiptContainerDetail) {
            //将未完成的任务数量更新到库存表
            if (DataUtils.getInteger(map.get("status")) < 100) {
                LambdaQueryWrapper<InventoryDetail> inventory =Wrappers.lambdaQuery();
                inventory.eq(InventoryDetail::getWarehouseCode,ShiroUtils.getWarehouseCode())
                         .eq(InventoryDetail::getLocationCode,task.getLocationCode())
                         .eq(InventoryDetail::getReceiptDetailId,DataUtils.getString(map.get("id")))
                         .eq(InventoryDetail::getContainerCode,DataUtils.getString(map.get("containerCode")));
                         InventoryDetail detail=inventoryDetailService.getOne(inventory);
                if (detail == null)
                {
                    //添加库存单
                    InventoryHeader header = new InventoryHeader();
                    header.setWarehouseCode(DataUtils.getString(map.get("warehouseCode")));//仓库
                    header.setCompanyCode(task.getCompanyCode());//货主
                    header.setContainerCode(DataUtils.getString(map.get("containerCode")));//容器号
                    header.setTotalQty(DataUtils.getInteger(map.get("totalQty")));//总数量
                    header.setLocking(1);
                    header.setEnable(1);
                    header.setCreatedBy(ShiroUtils.getLoginName());
                    header.setCreated(new Date());
                    header.setLastUpdated(new Date());
                    inventoryHeaderService.save(header);
                    //库存明细添加
                    detail = new InventoryDetail();
                    detail.setInventoryHeaderId(header.getId());//库存头ID
                    detail.setWarehouseCode(DataUtils.getString(map.get("warehouseCode")));//仓库
                    detail.setCompanyCode(task.getCompanyCode());//货主
                    detail.setLocationCode(task.getLocationCode());//库位号
                    detail.setContainerCode(DataUtils.getString(map.get("containerCode")));//容器号
                    detail.setMaterialCode(DataUtils.getString(map.get("materialCode")));//物料号
                    detail.setMaterialName(DataUtils.getString(map.get("materialName")));//物料名称
                    detail.setMaterialSpec(DataUtils.getString(map.get("materialSpec")));//物料规格
                    detail.setReceiptCode(DataUtils.getString(map.get("receiptCode")));//入库单编码
                    detail.setReceiptDetailId(DataUtils.getInteger(map.get("id")));//入库单明细ID
                    detail.setBatch(DataUtils.getString(map.get("batch")));//批次
                    detail.setLot(DataUtils.getString(map.get("lot")));//批号
                    detail.setInventorySts(DataUtils.getString(map.get("inventorySts")));//库存状态
                    detail.setManufactureDate(DataUtils.getDateTime(map.get("manufactureDate")));//生产日期
                    detail.setExpirationDate(DataUtils.getDateTime(map.get("expirationDate")));//失效日期
                    detail.setQty(DataUtils.getBigDecimal(map.get("qty")));//数量
                    detail.setTaskQty(DataUtils.getBigDecimal(map.get("taskQty")));
                    detail.setCreatedBy(ShiroUtils.getLoginName());//创建人
                    detail.setLastUpdatedBy(ShiroUtils.getLoginName());//创建时间
                    inventoryDetailService.save(detail);
                }
                else
                {
                    detail.setQty(detail.getQty().add(DataUtils.getBigDecimal(map.get("qty"))));
                    detail.setLastUpdatedBy(ShiroUtils.getLoginName());
                    LambdaUpdateWrapper<InventoryDetail> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
                    lambdaUpdateWrapper.eq(InventoryDetail::getId,DataUtils.getInteger(map.get("id")));
                    inventoryDetailService.update(detail, lambdaUpdateWrapper);
                }
                //记录库存交易记录
                InventoryTransaction inventoryTransaction = new InventoryTransaction();
                inventoryTransaction.setTransactionType(10);
                inventoryTransaction.setWarehouseCode(DataUtils.getString(map.get("warehouseCode")));
                inventoryTransaction.setCompanyCode(task.getCompanyCode());
                inventoryTransaction.setLocationCode(task.getLocationCode());
                inventoryTransaction.setContainerCode(DataUtils.getString(map.get("containerCode")));
                inventoryTransaction.setMaterialCode(DataUtils.getString(map.get("materialCode")));
                inventoryTransaction.setMaterialName(DataUtils.getString(map.get("materialName")));
                inventoryTransaction.setBillCode(DataUtils.getString(map.get("receiptCode")));
                inventoryTransaction.setBillDetailId(DataUtils.getInteger(map.get("id")));
                inventoryTransaction.setBatch(DataUtils.getString(map.get("batch")));
                inventoryTransaction.setLot(DataUtils.getString(map.get("lot")));
                inventoryTransaction.setManufactureDate(DataUtils.getDateTime(map.get("manufactureDate")));
                inventoryTransaction.setExpirationDate(DataUtils.getDateTime(map.get("expirationDate")));
                inventoryTransaction.setInventorySts(DataUtils.getString((map.get("inventorySts"))));
                inventoryTransaction.setTaskQty(DataUtils.getInteger(map.get("taskQty")));
                inventoryTransaction.setCreated(new Date());
                inventoryTransaction.setCreatedBy(ShiroUtils.getLoginName());
                inventoryTransactionService.save(inventoryTransaction);
//                //修改任务明细的状态为完成
//                TaskDetail taskDetail = new TaskDetail();
//                taskDetail.setId(DataUtils.getInteger(map.get("taskDetailId")));
//                taskDetail.setStatus((short)100);
//                taskDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
//                taskDetail.setEndTime(new Date());   //完成时间
//                taskDetailService.updateByModel(taskDetail);
//                //修改入库单明细的状态
//                receiptHeaderService.updateDetailStatus(DataUtils.getInteger(map.get("billDetailId")), (short)800);
//            }
//        }
//        //更新收货单主表的首状态
//        taskReceiptContainerDetail.stream().
//                filter(X -> DataUtils.getInteger(X.get("status")) < 100).
//                map(X -> X.get("billId")).
//                distinct().
//                forEach(X -> receiptHeaderService.receiptStatusUpdate(DataUtils.getInteger(X), (short)800));
//        //修改任务主表状态,因为立库任务表单头只对应一个货箱,表单详情的任务会同时完成
//        task.setFirstStatus((short)100);
//        task.setLastStatus((short)100);
//        task.setLastUpdatedBy(ShiroUtils.getLoginName());
//        task.setEndTime(new Date());
//        this.updateByModel(task);
//        //修改库位状态和对应的容器
//        locationService.updateContainerCodeAndStatus(LocationCode, task.getContainerCode(),"empty");
//        if (task.getIsDoubleIn().intValue() != 0)        {
//            locationService.updateStatus(task.getDestinationLocation(), "empty");
//        }
//        //修改容器状态和对应的库位
//        containerService.updateLocationCodeAndStatus(task.getContainerCode(), LocationCode, "some");
//        //修改组盘表状态为20
//        ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
//        receiptContainerHeader.setId(task.getAllocationHeadId());
//        receiptContainerHeader.setStatus((short)20);
//        receiptContainerHeaderService.updateByModel(receiptContainerHeader);
           }
        }
                return AjaxResult.success("完成入库任务");
    }

//    @Override
//    public List<Map<String, Object>> getReceiptTask(Integer taskId) {
//        return taskHeaderMapper.getReceiptTask(taskId) ;
//    }


}