WmsCreateTaskApiService.java 8.77 KB
package com.huaheng.api.sap.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.huaheng.api.sap.domain.ReceiptQuick;
import com.huaheng.api.sap.domain.ZarDomain;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.BusinessException;
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.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.sap.domain.Zarsh;
import com.huaheng.pc.sap.domain.Zarsi;
import com.huaheng.pc.sap.service.ZarshService;
import com.huaheng.pc.sap.service.ZarsiService;

@Service
public class WmsCreateTaskApiService {
    @Resource
    private ZarshService zarshService;
    @Resource
    private ZarsiService zarsiService;
    @Resource
    private ZoneService zoneService;
    @Resource
    private ZarApiService zarApiService;
    @Resource
    private ContainerService containerService;

    /**
     * 创建sap下发任务domain
     * pda wms的pc页面
     * @param receiptQuicks
     */
    public AjaxResult createZarshBywms(List<ReceiptQuick> receiptQuicks) {
        ZarDomain zarDomain = new ZarDomain();
        ReceiptQuick receiptQuick = receiptQuicks.get(0);
        String zoneCode = receiptQuick.getZoneCode();
        if (StringUtils.isEmpty(zoneCode)) {
            zoneCode = ShiroUtils.getZoneCode();
        }
//        if (StringUtils.isEmpty(zoneCode)) {
//            return AjaxResult.error("请选择仓库");
//        }
        Zone zone = zoneService.getZoneByCode(zoneCode);
        Zarsh zarsh = new Zarsh();
        zarsh.setUniqueId(DataUtils.createUniqueIdV1());
        zarsh.setCreateBy("WMS");
        zarsh.setMandt("600");
        zarsh.setWerks("SC00");
        zarsh.setLgnum(zone.getNickName());
        zarsh.setLgort(zone.getUserDef1());
        zarsh.setDrumId(receiptQuick.getDrumId());
        zarsh.setMFlag("2");
        zarsh.setFromPos(receiptQuick.getFromPos());
        zarsh.setHFlag(receiptQuick.getHFlag());
        zarsh.setRlFlag(receiptQuick.getRlFlag());
        zarsh.setInKind(1);
        zarsh.setPlType(1);

        String splitStr = receiptQuick.getSplitStr();
        String rollNumber = receiptQuick.getRollNumber();
        if (StringUtils.isEmpty(splitStr) && StringUtils.isEmpty(rollNumber)) {
            zarsh.setInKind(0);
        }
        String containercode = receiptQuick.getDrumId();
        if (StringUtils.isNotEmpty(containercode)) {
            Container container = containerService.getContainerByCode(containercode, QuantityConstant.WAREHOUSECODE);
            if (container != null) {
                zarsh.setPlType(container.getPlType());
            }
        }

        AjaxResult zarsiAjax = createZarsiBywms(receiptQuicks, zoneCode, zarsh);
        if (zarsiAjax.hasErr()) {
            return zarsiAjax;
        }
        List<Zarsi> zarsiList = (List<Zarsi>)zarsiAjax.getData();
        zarDomain.setZarsh(zarsh);
        zarDomain.setZarsiList(zarsiList);
        return AjaxResult.success().setData(zarDomain);
    }

    public AjaxResult createZarsiBywms(List<ReceiptQuick> receiptQuicks, String zoneCode, Zarsh zarsh) {
        List<Zarsi> list = new ArrayList<>();
        for (ReceiptQuick receiptQuick : receiptQuicks) {
            String splitStr = receiptQuick.getSplitStr();
            String rollNumber = receiptQuick.getRollNumber();
            if (StringUtils.isEmpty(splitStr) && StringUtils.isEmpty(rollNumber)) {
                continue;
            }
            Zarsi zarsi = new Zarsi();
            zarsi.setMandt(zarsh.getMandt());
            zarsi.setWerks(zarsh.getWerks());
            zarsi.setLgnum(zarsh.getLgnum());
            zarsi.setLgort(zarsh.getLgort());
            zarsi.setDrumId(zarsh.getDrumId());
            zarsi.setUniqueId(zarsh.getUniqueId());
            zarsi.setPosnr(receiptQuick.getPosnr());
            if (StringUtils.isNotEmpty(splitStr)) {
                // 解析批号、卷号、数量
                zarsi = parseMatnr(splitStr, zoneCode, zarsi);
                if (zarsi.getVerme().compareTo(new BigDecimal(0)) == 0) {
                    zarsi.setVerme(new BigDecimal(1));
                }
                if (zoneCode.equals("F")) {
                    if (StringUtils.isEmpty(receiptQuick.getLot())) {
                        return AjaxResult.error("请输入外观批号");
                    }
                    zarsi.setCharg(receiptQuick.getLot());
                }

            } else if (StringUtils.isNotEmpty(rollNumber)) {
                // pc端
                if (StringUtils.isEmpty(receiptQuick.getMatnr())) {
                    return AjaxResult.error("请输入物料code");
                }
                zarsi.setMatnr(receiptQuick.getMatnr());
                zarsi.setCharg(receiptQuick.getRollNumber());
                zarsi.setVerme(new BigDecimal(1));
                if (zoneCode.equals("F")) {
                    zarsi.setWjffh(receiptQuick.getWjffh());
                }
            }
            list.add(zarsi);
        }
        return AjaxResult.success().setData(list);
    }

    public Zarsi parseMatnr(String matnr, String zoneCode, Zarsi zarsi) {
        String[] ls = matnr.split(" ");
        BigDecimal qtybigdecimal = new BigDecimal(0);
        String rollNumber = null;
        String materialCode = null;
        String wjffh = null;
        if (zoneCode.equals("E") || zoneCode.contains("D")) {
            if (ls.length < 4) {
                throw new BusinessException("物料二维码扫码错误");
            }
            if (ls.length == 4) {
                materialCode = ls[1];
                String qty = ls[2];
                boolean tag = DataUtils.isDigit(qty);
                if (!tag) {
                    throw new BusinessException("请确认物料二维码数据或站台或用户是否对应");
                }
                qtybigdecimal = new BigDecimal(qty);
                rollNumber = ls[3];
            } else {
                materialCode = ls[0];
                rollNumber = ls[1];
            }
        } else if (zoneCode.equals("B")) {
            if (ls.length < 4) {
                throw new BusinessException("物料二维码扫码错误");
            }
            // 商片仓
            materialCode = ls[1];
            String qty = ls[2];
            boolean tag = DataUtils.isDigit(qty);
            if (!tag) {
                throw new BusinessException("请确认物料二维码数据或站台或用户是否对应");
            }
            qtybigdecimal = new BigDecimal(qty);
            rollNumber = ls[3];
        } else if (zoneCode.contains("C") || zoneCode.contains("A")) {
            if (ls.length < 3) {
                throw new BusinessException("物料二维码扫码错误");
            }
            // 铜箔 玻璃布
            materialCode = ls[0];
            String qty = ls[1];
            boolean tag = DataUtils.isDigit(qty);
            if (!tag) {
                throw new BusinessException("请确认物料二维码数据或站台或用户是否对应");
            }
            qtybigdecimal = new BigDecimal(qty);
            rollNumber = ls[2];
        } else if (zoneCode.contains("G")) {
            if (ls.length < 3) {
                throw new BusinessException("物料二维码扫码错误");
            }
            // 成品
            materialCode = ls[0];
            String qty = ls[1];
            boolean tag = DataUtils.isDigit(qty);
            if (!tag) {
                throw new BusinessException("请确认物料二维码数据或站台或用户是否对应");
            }
            qtybigdecimal = new BigDecimal(qty);
            rollNumber = ls[2];
        } else if (zoneCode.contains("F")) {
            if (ls.length < 6) {
                throw new BusinessException("物料二维码扫码错误");
            }
            // 外观
            materialCode = ls[4];
            String qty = ls[3];
            boolean tag = DataUtils.isDigit(qty);
            if (!tag) {
                throw new BusinessException("请确认二维码数据或站台或用户是否对应");
            }
            qtybigdecimal = new BigDecimal(qty);
            wjffh = ls[5];
//            rollNumber = ls[5];
//            rollNumber = ls[0].substring(1, ls[0].length()) + " " + ls[1] + " " + ls[2];
        }
        zarsi.setMatnr(materialCode);
        zarsi.setCharg(rollNumber);
        zarsi.setWjffh(wjffh);
        zarsi.setVerme(qtybigdecimal.stripTrailingZeros());
        return zarsi;
    }

}