BarCodeApiService.java 11.9 KB
package com.huaheng.api.general.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.erp.service.TaskReturnService;
import com.huaheng.api.general.domain.BarCodeDomain;
import com.huaheng.api.wcs.service.taskInfo.TaskInfoService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.constant.Res;
import com.huaheng.common.exception.BusinessException;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.spring.SpringUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.barcode.barcodeDetail.domain.BarCodeDetail;
import com.huaheng.pc.barcode.barcodeDetail.service.BarCodeDetailService;
import com.huaheng.pc.barcode.barcodeHeader.domain.BarCodeHeader;
import com.huaheng.pc.barcode.barcodeHeader.service.BarCodeHeaderService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.station.domain.Station;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
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.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Component
@Transactional
public class BarCodeApiService {
    @Resource
    private BarCodeHeaderService barCodeHeaderService;
    @Resource
    private MaterialService materialService;
    @Resource
    private BarCodeDetailService barCodeDetailService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private TaskInfoService taskInfoService;
    @Resource
    private ReceiptTaskService receiptTaskService;
    @Resource
    private TaskReturnService taskReturnService;


    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addBarCodeApi(BarCodeDomain barCodeDomain){

        AjaxResult ajaxResult = checkBarCode(barCodeDomain);
        if (ajaxResult.getCode()!=200) {
            return AjaxResult.error(ajaxResult.getMsg());
        }
        boolean result = false;
        BarCodeHeader header = barCodeDomain.getBarCodeHeader();
        BarCodeHeader barCodeHeader = new BarCodeHeader();
        barCodeHeader.setLocked(QuantityConstant.BARCODEHEADER_LOCKED_DEFAULT);
        barCodeHeader.setCode(barCodeHeaderService.createCode("BZXXRK"));
        barCodeHeader.setMaterialBatch(header.getMaterialBatch());
        barCodeHeader.setReceiptType("BZXXRK");
        barCodeHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
        barCodeHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANYCODE);
        barCodeHeader.setFbillno(header.getFbillno());
        barCodeHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
        barCodeHeader.setBarCodeHeaderCode(header.getCode());
        barCodeHeader.setReceiptNote(header.getReceiptNote());
        barCodeHeader.setEchelon(header.getEchelon());
        barCodeHeader.setTotalQty(BigDecimal.ZERO);
        barCodeHeader.setTotalLines(0);
        barCodeHeader.setTotalWeight(BigDecimal.ZERO);
        result = barCodeHeaderService.save(barCodeHeader);
        if(!result){
            throw new ServiceException("包装头信息创建失败");
        }
        List<BarCodeDetail> barCodeDetails = barCodeDomain.getBarCodeDetails();
        BigDecimal totalQty = BigDecimal.ZERO;
        BigDecimal totalWeight = BigDecimal.ZERO;
        int totalLines = barCodeDetails.size();

        for (BarCodeDetail detail : barCodeDetails) {
            BarCodeDetail barCodeDetail = new BarCodeDetail();
            barCodeDetail.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
            barCodeDetail.setCompanyCode(QuantityConstant.DEFAULT_COMPANYCODE);
            barCodeDetail.setReceiptId(barCodeHeader.getId());
            barCodeDetail.setCode(detail.getCode());
            barCodeDetail.setDayShift("day");
            AjaxResult ajaxResultCode = barCodeDetailService.createRegenerationCode(1);
            String regenerationCode = (String)ajaxResultCode.getData();
            barCodeDetail.setRegenerationCode(regenerationCode);
            barCodeDetail.setReceiptCode(barCodeHeader.getCode());
            String materialCode = detail.getMaterialCode();
            BigDecimal qty = detail.getQty();
            BigDecimal weight = detail.getWeight();
            BarCodeHeader oneBarCodeHeader = barCodeHeaderService.getOne(new LambdaQueryWrapper<BarCodeHeader>().eq(BarCodeHeader::getId, barCodeHeader.getId()));
            if(header.getEchelon()==null){
                oneBarCodeHeader.setProType("2");//拆解
            }else {
                oneBarCodeHeader.setProType("1");//装配
            }
            barCodeHeaderService.updateById(oneBarCodeHeader);

            if (detail.getCode()==null || "".equals(detail.getCode())) { //没有子条码,属于拆解中的散装,只有重量没有数量,重量赋值给数量
                qty=weight;
            }
            totalQty.add(qty);
            totalWeight.add(weight);
            Material material = materialService.getOne(new LambdaQueryWrapper<Material>().eq(Material::getCode,materialCode));
            barCodeDetail.setMaterialCode(material.getCode());
            barCodeDetail.setMaterialName(material.getName());
            barCodeDetail.setMaterialSpec(material.getSpec());
            barCodeDetail.setMaterialUnit(material.getUnit());
            barCodeDetail.setTotalQty(qty);
            barCodeDetail.setSn(detail.getSn());
            barCodeDetail.setWeight(weight);
            if(barCodeDetail.getCode().length() == 17 || barCodeDetail.getCode().length() == 24 || barCodeDetail.getCode().length() == 19){
                String code = barCodeDetail.getCode();
                String productType = code.substring(3,4);
                if(exist(productType)){
                    barCodeDetail = addAttr(barCodeDetail.getCode(),barCodeDetail,header);
                }
            }
//            barCodeDetail.setStorageType(detail.getStorageType());
//            barCodeDetail.setConstitute(detail.getConstitute());
//            barCodeDetail.setProductType(detail.getProductType());
//            barCodeDetail.setBatteryModel(detail.getBatteryModel());
//            barCodeDetail.setWarehouseInTime(detail.getWarehouseInTime());
            result = barCodeDetailService.save(barCodeDetail);
            if(!result){
                result=false;
                throw new ServiceException("包装子信息创建失败");
            }
//            if(barCodeDetail.getCode()!=null && !"".equals(barCodeDetail.getCode())){
//                Map<String, Object> dataByBarCode = taskReturnService.getDataByBarCode(barCodeDetail.getCode());
//                /**
//                 * Barcode 拆解物码
//                 * MaterialType 产品类型
//                 * PreviousBarcode 源电池包/模组/电芯码
//                 * SuperiorBarcode 拆解上级码  例如 电芯码的上级码(模组码)
//                 * BatteryType 电池类型
//                 * BatteryPackSource 来源公司编号
//                 */
//                String previousBarcode = (String) dataByBarCode.get("PreviousBarcode");
//                String superiorBarcode = (String) dataByBarCode.get("SuperiorBarcode");
//                String batteryPackSource = (String) dataByBarCode.get("BatteryPackSource");
//                barCodeDetail.setPreviousBarcode(previousBarcode);
//                barCodeDetail.setSuperiorBarcode(superiorBarcode);
//                barCodeDetail.setBatteryPackSource(batteryPackSource);
//                barCodeDetail = addAttr(barCodeDetail.getCode(), barCodeDetail);
//                barCodeDetailService.updateById(barCodeDetail);
//                if(!result){
//                    throw new ServiceException("包装子信息创建失败");
//                }
//            }
        }
        barCodeHeader.setTotalQty(barCodeHeader.getTotalQty().add(totalQty));
        barCodeHeader.setTotalWeight(barCodeHeader.getTotalWeight().add(totalWeight));
        barCodeHeader.setTotalLines(totalLines);
        result = barCodeHeaderService.updateById(barCodeHeader);
        if(!result){
            throw new ServiceException("更新包装入库头信息失败。");
        }
        return AjaxResult.toAjax(result);
    }

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult checkBarCode(BarCodeDomain barCodeDomain) {
        BarCodeHeader barCodeHeader = barCodeDomain.getBarCodeHeader();
        String barCodeHeaderCode = barCodeHeader.getCode();
        if(barCodeHeaderCode == null || "".equals(barCodeHeaderCode)){
            return AjaxResult.error("没有主条码");
        }

        if(barCodeHeader.getFbillno() == null || "".equals(barCodeHeader.getFbillno())){
            return AjaxResult.error("没有生产订单编号");
        }

        BarCodeHeader one = barCodeHeaderService.getOne(new LambdaQueryWrapper<BarCodeHeader>()
                .eq(BarCodeHeader::getBarCodeHeaderCode,barCodeHeaderCode)
                .eq(BarCodeHeader::getFbillno,barCodeHeader.getFbillno()));
        if(one != null){
            return AjaxResult.error("包装信息头错误:主条码【"+barCodeHeaderCode+"】和生产订单号都重复了 "+barCodeHeader.getFbillno());
        }

        List<BarCodeDetail> barCodeDetailList = barCodeDomain.getBarCodeDetails();
        for (BarCodeDetail barCodeDetail : barCodeDetailList) {

            String materialCode = barCodeDetail.getMaterialCode();
            if(materialCode == null){
                return AjaxResult.error("物料编码为空");
            }
            Material material = materialService.getOne(new LambdaQueryWrapper<Material>().eq(Material::getCode,barCodeDetail.getMaterialCode()));
            if(material == null){
                return AjaxResult.error("没有该物料编码:"+materialCode);
            }
        }
        return AjaxResult.success();
    }

    private BarCodeDetail addAttr(String barCode,BarCodeDetail detail,BarCodeHeader header){
        String type = "";
        if(barCode.length()==17){
            type = "RC";
        }else if(barCode.length()==19){
            type = barCode.substring(17,19);
        }else if("1".equals(header.getEchelon())){
            type = "RM";
        }
        String productType = barCode.substring(3,4);
        String batteryType = barCode.substring(4,5);
        String productDate = barCode.substring(7,10);
        String year = productDate.substring(0, 1);
        String month = productDate.substring(1, 2);
        String day = productDate.substring(2, 3);

        String days = Res.string().dateKey(day);
        String years = Res.string().yearKey(year);
        Date inTime = DateUtils.dateTime("yyyy-MM-dd", years + "-" + month + "-" + days);

        //默认都是 1 梯级利用 (0是再生利用 再生利用需要填type)
        detail.setStorageType("1");
//        detail.setType("1");
        detail.setConstitute(Res.string().batteryKey(type));
        detail.setProductType(Res.string().batteryKey(productType+"1"));
        detail.setBatteryModel(Res.string().batteryKey(batteryType));
        detail.setWarehouseInTime(inTime);
        return detail;
    }

    private Boolean exist(String proTypeStr){
        if(StringUtils.isNotEmpty(proTypeStr)){
            if(proTypeStr.contains("P")){
                return true;
            }
            if(proTypeStr.contains("M")){
                return true;
            }
            if(proTypeStr.contains("C")){
                return true;
            }
        }
        return false;
    }
}