QuickReceiptService.java 9.41 KB
package com.huaheng.api.wcs.service.quickReceipt;


import com.alibaba.fastjson.JSONException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.mobile.receipt.ReceiptBill;
import com.huaheng.mobile.receipt.ReceiptJJBean;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.station.domain.Station;
import com.huaheng.pc.config.station.service.StationService;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerView;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
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.receipt.receiving.controller.ReceivingController;
import com.huaheng.pc.system.user.service.IUserService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.ReceiptTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


@Service
public class QuickReceiptService {
    private static final Logger logger = LoggerFactory.getLogger(QuickReceiptService.class);
    @Resource
    private ContainerService containerService;
    @Resource
    private StationService stationService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ReceiptTaskService receiptTaskService;

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult quickReceiptJJ(List<ReceiptJJBean> receiptBills) {
        String companyCode = receiptBills.get(0).getCompanyCode();
        String containerCode = receiptBills.get(0).getContainerCode();
        String warehosueCode = ShiroUtils.getWarehouseCode();
        String fromPort = receiptBills.get(0).getFromPort();
        String inventorySts = receiptBills.get(0).getInventorySts();
        String referCode = receiptBills.get(0).getReferCode();
        Container container = containerService.getContainerByCode(containerCode, warehosueCode);
        int receiptHeaderId = receiptHeaderService.createTodayHeaderJJ(companyCode, container.getUWarehouseCode());
//        if (!inventorySts.equals("good")) {
            ReceiptHeader receiptHeader1 = new ReceiptHeader();
            receiptHeader1.setId(receiptHeaderId);
            receiptHeader1.setReferCode(referCode);
            receiptHeaderService.updateById(receiptHeader1);
//        }
        List<Integer> receiptDetailIds = receiptDetailService.insertTodayReceiptDetailJJ(receiptHeaderId, receiptBills, false, companyCode);
        if (receiptDetailIds != null && receiptDetailIds.size() > 0) {
            for (Integer i : receiptDetailIds) {
                //组盘
                final ReceiptDetail receiptDetail = receiptDetailService.getById(Integer.valueOf(i));
                ReceiptContainerView receiptContainerView = new ReceiptContainerView();
                receiptContainerView.setReceiptCode(receiptDetail.getReceiptCode());
                receiptContainerView.setQty(receiptDetail.getQty());
                receiptContainerView.setContainerCode(containerCode);
                receiptContainerView.setReceiptDetailId(Integer.valueOf(i));
//                receiptContainerView.setPort(receiptBills.get(0).getFromPort());
                AjaxResult ajaxResult = receiptContainerHeaderService.saveContainer(receiptContainerView);
                if (ajaxResult.hasErr()) {
                    throw new ServiceException(ajaxResult.getMsg());
                }
            }
            //创建任务
            LambdaQueryWrapper<ReceiptContainerHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode);
            lambdaQueryWrapper.le(ReceiptContainerHeader::getStatus, 10);
            lambdaQueryWrapper.last("limit 1");
            ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getOne(lambdaQueryWrapper);
            if (receiptContainerHeader != null) {
                String ids2 = receiptContainerHeader.getId().toString();
                Integer[] idArray = Convert.toIntArray(ids2);
                List<Integer> islist = Arrays.asList(idArray);
                try {
                    AjaxResult ajaxResult1 = receiptTaskService.createReceiptTask(islist);
                    if (ajaxResult1.hasErr()) {
                        throw new ServiceException(ajaxResult1.getMsg());
                    }
                    List<String> taskIds = (List<String>) ajaxResult1.getData();
                    if (taskIds != null && taskIds.size() > 0) {
                        String taskId = taskIds.get(0);
                        TaskHeader taskHeader = new TaskHeader();
                        taskHeader.setId(Integer.valueOf(taskId));
                        taskHeader.setPort(fromPort);
                        taskHeader.setToPort(fromPort);
                        taskHeader.setPickingCartPos(fromPort);
                        taskHeaderService.updateById(taskHeader);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("receipt/receiving/save:idArray=" + idArray + "msg:" + e.getMessage());
                }
            }

            return AjaxResult.success();

        }

        return AjaxResult.success();
    }

    public AjaxResult checkReceiptData(List<ReceiptJJBean> receiptBills) {
        ReceiptJJBean receiptJJBean = receiptBills.get(0);
        if (StringUtils.isEmpty(receiptJJBean.getContainerCode())) {
            return AjaxResult.error("容器编码不能为空");
        }
        if (StringUtils.isEmpty(receiptJJBean.getCompanyCode())) {
            return AjaxResult.error("货主编码不能为空");
        }
        if (StringUtils.isEmpty(receiptJJBean.getInventorySts())) {
            return AjaxResult.error("合格标志不能为空");
        }
        if (StringUtils.isEmpty(receiptJJBean.getMaterialCode())) {
            return AjaxResult.error("物料编码不能为空");
        }
        if (StringUtils.isEmpty(receiptJJBean.getFromPort())) {
            return AjaxResult.error("起点不能为空");
        }
        if (receiptJJBean.getQty() == null) {
            return AjaxResult.error("数量不能为空");
        }
        if (receiptJJBean.getQty().compareTo(BigDecimal.ZERO) <= 0) {
            return AjaxResult.error("收货数量大于0");
        }
        LambdaQueryWrapper<Container> containerQueryWrapper = Wrappers.lambdaQuery();
        containerQueryWrapper.eq(Container::getCode, receiptJJBean.getContainerCode());
        containerQueryWrapper.eq(Container::getWarehouseCode, ShiroUtils.getWarehouseCode());
        containerQueryWrapper.last("limit 1");
        Container container = containerService.getOne(containerQueryWrapper);
        if (container == null) {
            return AjaxResult.error("不存在这个托盘:" + receiptJJBean.getContainerCode());
        }
        //pda入库增加托盘任务校验
        List<TaskHeader> unfinishTask = taskHeaderService.getUnCompleteTaskList().stream().filter((t) -> {
            return t.getContainerCode().equals(container.getCode());
        }).collect(Collectors.toList());
        if (unfinishTask.size() > 0) {
            TaskHeader t = unfinishTask.get(0);
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(t.getCreated());
            return AjaxResult.error(container.getCode() + "托盘已经有未完成的任务,请不要重复下发\n该托盘任务创建于:" + time);
        }
        LambdaQueryWrapper<Station> stationLambdaQueryWrapper = Wrappers.lambdaQuery();
        stationLambdaQueryWrapper.eq(Station::getCode, receiptJJBean.getFromPort());
        stationLambdaQueryWrapper.eq(Station::getWarehouseCode, ShiroUtils.getWarehouseCode());
        stationLambdaQueryWrapper.in(Station::getType, "4","1");
        stationLambdaQueryWrapper.last("limit 1");
        Station station = stationService.getOne(stationLambdaQueryWrapper);
        if (station == null) {
            return AjaxResult.error("不存在这个起点:" + receiptJJBean.getFromPort());
        }
        return AjaxResult.success();
    }

}