BarCodeApiService.java 8.72 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.general.domain.BarCodeDomain;
import com.huaheng.api.wcs.service.taskInfo.TaskInfoService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.BusinessException;
import com.huaheng.common.exception.service.ServiceException;
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.List;

@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;

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult checkBarCode(BarCodeDomain barCodeDomain) {
        BarCodeHeader barCodeHeader = barCodeDomain.getBarCodeHeader();
        String barCodeHeaderCode = barCodeHeader.getCode();
        if(barCodeHeaderCode == null){
            return AjaxResult.error("主码不存在 code");
        }

        barCodeHeader.setBarCodeHeaderCode(barCodeHeaderCode);

        LambdaQueryWrapper<BarCodeHeader> barCodeHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        barCodeHeaderLambdaQueryWrapper.eq(BarCodeHeader::getBarCodeHeaderCode,barCodeHeader.getBarCodeHeaderCode());
        List<BarCodeHeader> barCodeHeaderList = barCodeHeaderService.list(barCodeHeaderLambdaQueryWrapper);
        if(barCodeHeaderList.size()>0){
            return AjaxResult.error("主码重复 code");
        }

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

            String barCodeDetailReceiptCode = barCodeDetail.getCode();
            if(barCodeDetailReceiptCode == null){
                return AjaxResult.error("子条码不存在 code");
            }
            barCodeDetail.setReceiptCode(barCodeDetailReceiptCode);
            String materialCode = barCodeDetail.getMaterialCode();
            if(materialCode == null){
                return AjaxResult.error("物料编码不存在 materialCode");
            }

//            String materialUnit = barCodeDetail.getMaterialUnit();
//            if(materialUnit == null){
//                return AjaxResult.error("物料单位不存在 materialUnit");
//            }

            BigDecimal qty = barCodeDetail.getQty();
            if(qty == null){
                return AjaxResult.error("数量不存在 qty");
            }

            String sn = barCodeDetail.getSn();
            if(sn == null){
                return AjaxResult.error("序列号不存在 sn");
            }

        }
        return AjaxResult.success();
    }



    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addBarCodeApi(BarCodeDomain barCodeDomain){
        boolean result = false;
        BarCodeHeader header = barCodeDomain.getBarCodeHeader();
        //原料批次
        String materialBatch = header.getMaterialBatch();
        if(materialBatch ==null){
            return AjaxResult.error("包装信息头错误:原料批次不存在");
        }
        String barCodeHeaderCode = header.getCode();
        String fbillno = header.getFbillno();
        if(barCodeHeaderCode == null || "".equals(barCodeHeaderCode)){
            return AjaxResult.error("主码不存在 code");
        }
        String code = barCodeHeaderService.createCode("BZXXRK");

        BarCodeHeader barCodeHeader = barCodeHeaderService.getOne(new LambdaQueryWrapper<BarCodeHeader>()
                .eq(BarCodeHeader::getBarCodeHeaderCode,barCodeHeaderCode)
                .eq(BarCodeHeader::getFbillno,fbillno));
        if(barCodeHeader != null){
            return AjaxResult.error("包装信息头错误:主条码重复【"+barCodeHeaderCode+"】。");
        }
        barCodeHeader = new BarCodeHeader();
        barCodeHeader.setCode(code);
        barCodeHeader.setMaterialBatch(materialBatch);
        barCodeHeader.setReceiptType("BZXXRK");
        barCodeHeader.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
        barCodeHeader.setCompanyCode(QuantityConstant.DEFAULT_COMPANYCODE);
        barCodeHeader.setFbillno(fbillno);
        barCodeHeader.setCreatedBy(QuantityConstant.PLATFORM_MES);
        barCodeHeader.setBarCodeHeaderCode(barCodeHeaderCode);
        barCodeHeader.setReceiptNote(header.getReceiptNote());
        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.setPower(detail.getPower());
            barCodeDetail.setReceiptCode(barCodeHeader.getCode());

            String materialCode = detail.getMaterialCode();
            String materialUnit = detail.getMaterialUnit();
            BigDecimal qty = detail.getQty();
            BigDecimal weight = detail.getWeight();
            if(weight == null){
                return AjaxResult.error("包装信息入库子明细,重量不能为空");
            }
            if(qty == null || qty.compareTo(BigDecimal.ZERO) <= 0){
                return AjaxResult.error("数量不存在 qty");
            }
            String sn = detail.getSn();
            totalQty.add(qty);
            totalWeight.add(weight);


            LambdaQueryWrapper<Material> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.eq(Material::getCode,materialCode)
                    .eq(Material::getUnit,materialUnit);
            Material material = materialService.getOne(queryWrapper1);
            if(material == null){
                return AjaxResult.error("没改物料编码:"+barCodeDetail.getMaterialCode());
            }
            barCodeDetail.setMaterialCode(material.getCode());
            barCodeDetail.setMaterialName(material.getName());
            barCodeDetail.setMaterialSpec(material.getSpec());
            barCodeDetail.setMaterialUnit(material.getUnit());
            barCodeDetail.setQty(BigDecimal.ONE);
            barCodeDetail.setSn(sn);
            barCodeDetail.setWeight(weight);
            result = barCodeDetailService.save(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.success();
    }

}