package com.chinaztt.mes.quality.service.impl; import cn.hutool.http.HttpRequest; import com.alibaba.druid.util.StringUtils; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.chinaztt.mes.quality.dto.ZxWeightLabelDTO; import com.chinaztt.mes.quality.entity.StockBean; import com.chinaztt.mes.quality.entity.ZxLabelBindRelation; import com.chinaztt.mes.quality.entity.ZxWeightLabel; import com.chinaztt.mes.quality.entity.ZxWeightLabelConfig; import com.chinaztt.mes.quality.mapper.ZxLabelBindRelationMapper; import com.chinaztt.mes.quality.mapper.ZxWeightLabelConfigMapper; import com.chinaztt.mes.quality.mapper.ZxWeightLabelMapper; import com.chinaztt.mes.quality.service.ZxWeightLabelService; import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Description : 中兴称重标签 * @ClassName : ZxWeightLabelServiceImpl * @Author : sll * @Date: 2022-12-01 23:26 */ @Service @AllArgsConstructor @Component @Transactional(rollbackFor = Exception.class) public class ZxWeightLabelServiceImpl extends ServiceImpl implements ZxWeightLabelService{ private ZxWeightLabelConfigMapper zxWeightLabelConfigMapper; private ZxLabelBindRelationMapper zxLabelBindRelationMapper; private StockBean stockBean; private static final ScriptEngineManager manager = new ScriptEngineManager(); @Override public ZxWeightLabel addWeightLabel(ZxWeightLabelDTO zxWeightLabelDTO) throws Exception{ if(null == zxWeightLabelDTO.getShopCode() || !zxWeightLabelDTO.getShopCode().equals("GD")){ throw new RuntimeException("车间代码异常"); } if(null == zxWeightLabelDTO.getPkgCode() || null == zxWeightLabelDTO.getGrossWeight()){ throw new RuntimeException("PKG码及毛重数据不可为空"); } //解析pkg码 try{ List stringList = Arrays.asList(zxWeightLabelDTO.getPkgCode().split("&")); if(stringList.size() < 3){ //正常的按&切割后是8段,但只有前3段对我们是有用的 throw new RuntimeException("PKG码格式错误"); }else{ ZxWeightLabelDTO.PkgNode pkgNode = new ZxWeightLabelDTO.PkgNode(); pkgNode.setPkgId(stringList.get(0));//pkgid pkgNode.setMaterialCode(stringList.get(1));//物料编码 pkgNode.setQuality(new BigDecimal(stringList.get(2)));//数量 zxWeightLabelDTO.setPkgNode(pkgNode); } }catch(Exception e){ throw new RuntimeException("解析pkg码异常 -> " + e.getMessage()); } //校验 //1.pkgid是否绑定 ZxLabelBindRelation zxLabelBindRelation = zxLabelBindRelationMapper.selectOne(Wrappers.lambdaQuery().eq(ZxLabelBindRelation::getPkgId,zxWeightLabelDTO.getPkgNode().getPkgId()).last("limit 1")); if(null == zxLabelBindRelation){ throw new RuntimeException("该PKG码未进行标签关系绑定 -> PKGID = 【" + zxWeightLabelDTO.getPkgNode().getPkgId() + "】"); } //2.pkgid是否已经生产过称重标签 ZxWeightLabel zxWeightLabel = baseMapper.selectOne(Wrappers.lambdaQuery().eq(ZxWeightLabel::getPkgId,zxWeightLabelDTO.getPkgNode().getPkgId()).last("limit 1")); if(null != zxWeightLabel){ throw new RuntimeException("该PKGID = 【" + zxWeightLabelDTO.getPkgNode().getPkgId() + "】 -> 已生成过称重记录"); } //3.根据客户物料编码去配置表中取配置数据 List zxWeightLabelConfigList = zxWeightLabelConfigMapper.selectList(Wrappers.lambdaQuery().eq(ZxWeightLabelConfig::getMaterialCode,zxWeightLabelDTO.getPkgNode().getMaterialCode())); if(null == zxWeightLabelConfigList || zxWeightLabelConfigList.size() == 0){ throw new RuntimeException("客户物料编码 = 【" + zxWeightLabelDTO.getPkgNode().getMaterialCode() + "】 -> 未找到包装配置数据"); } ScriptEngine se = manager.getEngineByName("js"); Boolean door = false;//标记 for(ZxWeightLabelConfig zxWeightLabelConfig : zxWeightLabelConfigList){ zxWeightLabelDTO.setMeterWeight(zxWeightLabelConfig.getMeterWeight());//米重 zxWeightLabelDTO.setFloatParam(zxWeightLabelConfig.getFloatParam());//防呆范围浮动参数 String s = zxWeightLabelConfig.getMeterScope().replace("x",zxWeightLabelDTO.getPkgNode().getQuality().toString());//将公式中的x替换成pkg码中解析出来的数量 if((Boolean)se.eval(s)){ //判断对应的包装参数是否需要人工手动填写 if(!zxWeightLabelConfig.getIsAuto()){ //判断前端是否传入包装参数 if(null == zxWeightLabelDTO.getPackMaterial() || null == zxWeightLabelDTO.getPackSize() || null == zxWeightLabelDTO.getPackWeight()){ //前端没传,则需要前端弹出窗体,让用户填写 zxWeightLabelDTO.setPopWindow(true); return zxWeightLabelDTO; }else{ //检验一下包装尺寸格式是否合规 List stringList = Arrays.asList(zxWeightLabelDTO.getPackSize().split("\\*")); if(null == stringList || stringList.size() != 3){ throw new RuntimeException("包尺寸格式不合规"); } for(String cc : stringList){ if(!StringUtils.isNumber(cc) || Double.parseDouble(cc) <= 0){ throw new RuntimeException("包装尺寸 -> 长、宽、高必须填写数值且不可小于等于0"); } } door = true; break; } }else{ zxWeightLabelDTO.setPackMaterial(zxWeightLabelConfig.getPackMaterial());//包装材料 zxWeightLabelDTO.setPackSize(zxWeightLabelConfig.getPackSize());//包装尺寸 zxWeightLabelDTO.setPackWeight(zxWeightLabelConfig.getPackWeight());//包装重量 door = true;//获取配置成功 break; } } } //4.分割米标是否合规 if(!door){ throw new RuntimeException("根据客户物料编码 = 【" + zxWeightLabelDTO.getPkgNode().getMaterialCode() + "】的分割计米 = 【" + zxWeightLabelDTO.getPkgNode().getQuality() + "】 -> 不合规"); } //5.校验重量范围 净重*(1-防呆范围浮动参数)<=理论重量<=净重*(1+防呆范围浮动参数) zxWeightLabelDTO.setNetWeight(zxWeightLabelDTO.getGrossWeight().subtract(zxWeightLabelDTO.getPackWeight()));//净重 = 毛重 - 包装重量 zxWeightLabelDTO.setTheoryWeight(zxWeightLabelDTO.getPkgNode().getQuality().multiply(zxWeightLabelDTO.getMeterWeight()));//理论重量 = pkg码中数量 * 米重 BigDecimal a = new BigDecimal("1"); BigDecimal b = zxWeightLabelDTO.getFloatParam();//防呆范围浮动参数改成从配置表中获取 if(zxWeightLabelDTO.getTheoryWeight().compareTo(zxWeightLabelDTO.getNetWeight().multiply(a.subtract(b))) >= 0 && zxWeightLabelDTO.getTheoryWeight().compareTo(zxWeightLabelDTO.getNetWeight().multiply(a.add(b))) <= 0){ //创建数据 zxWeightLabelDTO.setPkgId(zxWeightLabelDTO.getPkgNode().getPkgId());//pkgid zxWeightLabelDTO.setQuantity(zxWeightLabelDTO.getPkgNode().getQuality());//数量 zxWeightLabelDTO.setMaterialCode(zxWeightLabelDTO.getPkgNode().getMaterialCode());//客户物料编码 baseMapper.insert(zxWeightLabelDTO); return zxWeightLabelDTO; }else{ DecimalFormat decimalFormatTip = new DecimalFormat("00.00");//报错提示精度 throw new RuntimeException("理论净重 = 【" + decimalFormatTip.format(zxWeightLabelDTO.getTheoryWeight()) + "】 -> 不在[" + decimalFormatTip.format(zxWeightLabelDTO.getNetWeight().multiply(a.subtract(b))) + "," + decimalFormatTip.format(zxWeightLabelDTO.getNetWeight().multiply(a.add(b))) + "]范围内"); } } @Override public String weighing(){ //从地磅获取称重 String ok = "OK"; String key = "result"; String weight = "message"; JSONObject jsonObject; try { //从地磅获取称重 Map paramMap = new HashMap<>(16); String result = HttpRequest.get(stockBean.getGd_1()).form(paramMap).execute().body(); //String result = "[{\"result\":\"OK\",\"message\":\"0.5\"}]";//测试用 JSONArray jsonArray = JSONObject.parseArray(result); jsonObject = jsonArray.getJSONObject(0); } catch (Exception e) { throw new RuntimeException("调用接口失败"); } if (jsonObject.getString(key).equals(ok)){ return jsonObject.getString(weight); }else{ throw new RuntimeException("获取称重失败 -> " + jsonObject.getString(weight)); } // String xmlres = "10OK"; // String res = "0"; // Map paramMap = new HashMap<>(16); // if(true){ // xmlres = HttpRequest.get(stockBean.getGd_1()).form(paramMap).execute().body(); // } // // Pattern p = Pattern.compile("\r|\n"); // Matcher m = p.matcher(xmlres); // xmlres = m.replaceAll(""); // // // 创建一个新的字符串 // StringReader read = new StringReader(xmlres); // // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入 // InputSource source = new InputSource(read); // // 创建一个新的SAXBuilder // SAXBuilder sb = new SAXBuilder(); // // 通过输入源构造一个Document // Document doc = sb.build(source); // // 取的根元素 // Element root = doc.getRootElement(); // System.out.println(root.getName());// 输出根元素的名称(测试)ArrayOfWeightModel // // 得到根元素所有子元素的集合 // List jiedian = root.getChildren(); // // 获得XML中的命名空间(XML中未定义可不写) // Namespace name = root.getNamespace(); // Element et = null; // // for (int i = 0; i < jiedian.size(); i++) { // et = (Element) jiedian.get(i);// 循环依次得到子元素 // if (i == 0) {//获取参数 // if (et.getChild("result", name).getText().equals("OK")){ // res = et.getChild("message", name).getText(); // }else{ // throw new RuntimeException("获取称重数据失败 -> " + et.getChild("message", name).getText()); // } // } // } // return res; } }