| 
									
										
										
										
											2025-09-03 09:49:27 +00:00
										 |  |  | package tqq9.lc123.cloud.app.plugin.operate.im;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import kd.bos.dataentity.entity.DynamicObject;
 | 
					
						
							|  |  |  | import kd.bos.dataentity.entity.DynamicObjectCollection;
 | 
					
						
							| 
									
										
										
										
											2025-09-15 10:26:35 +00:00
										 |  |  | import kd.bos.dataentity.utils.StringUtils;
 | 
					
						
							| 
									
										
										
										
											2025-09-03 09:49:27 +00:00
										 |  |  | import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
 | 
					
						
							|  |  |  | import kd.bos.entity.plugin.args.AfterOperationArgs;
 | 
					
						
							|  |  |  | import kd.bos.logging.Log;
 | 
					
						
							|  |  |  | import kd.bos.logging.LogFactory;
 | 
					
						
							|  |  |  | import kd.bos.orm.query.QCP;
 | 
					
						
							|  |  |  | import kd.bos.orm.query.QFilter;
 | 
					
						
							|  |  |  | import kd.bos.servicehelper.BusinessDataServiceHelper;
 | 
					
						
							|  |  |  | import kd.bos.servicehelper.operation.SaveServiceHelper;
 | 
					
						
							|  |  |  | import kd.sdk.plugin.Plugin;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-29 09:57:36 +00:00
										 |  |  | import java.math.BigDecimal;
 | 
					
						
							| 
									
										
										
										
											2025-09-03 09:49:27 +00:00
										 |  |  | import java.util.Date;
 | 
					
						
							|  |  |  | import java.util.HashMap;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * 采购入库反写批次注册证管理
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | public class PurInIotManagePlugin extends AbstractOperationServicePlugIn implements Plugin {
 | 
					
						
							|  |  |  |     private final static Log logger = LogFactory.getLog(PurInIotManagePlugin.class);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Override
 | 
					
						
							|  |  |  |     public void afterExecuteOperationTransaction(AfterOperationArgs e) {
 | 
					
						
							|  |  |  |         super.afterExecuteOperationTransaction(e);
 | 
					
						
							|  |  |  |         //物料Map
 | 
					
						
							|  |  |  |         DynamicObject[] materials = BusinessDataServiceHelper.load("bd_material", "id,number,name,tqq9_brand,baseunit,tqq9_brand", null);
 | 
					
						
							|  |  |  |         HashMap<Long, DynamicObject> materialMap = new HashMap<Long, DynamicObject>();
 | 
					
						
							|  |  |  |         for (DynamicObject dynamicObject : materials) {
 | 
					
						
							|  |  |  |             materialMap.put(dynamicObject.getLong("id"), dynamicObject);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         for (DynamicObject dataEntity : e.getDataEntities()) {
 | 
					
						
							|  |  |  |             String billno = dataEntity.getString("billno");
 | 
					
						
							|  |  |  |             QFilter qFilter = new QFilter("billno", QCP.equals, billno);
 | 
					
						
							| 
									
										
										
										
											2025-10-24 08:31:37 +00:00
										 |  |  |             DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("im_purinbill",
 | 
					
						
							| 
									
										
										
										
											2025-10-30 04:23:25 +00:00
										 |  |  |                     "billentry.producedate,billentry.expirydate,billentry.lotnumber,billentry.tqq9_registration,billentry.material,billentry.warehouse,billentry.lot,billentry.qty,billentry.tqq9_goods",
 | 
					
						
							| 
									
										
										
										
											2025-10-24 08:31:37 +00:00
										 |  |  |                     qFilter.toArray());
 | 
					
						
							| 
									
										
										
										
											2025-09-03 09:49:27 +00:00
										 |  |  |             DynamicObjectCollection billentry = dynamicObject.getDynamicObjectCollection("billentry");
 | 
					
						
							| 
									
										
										
										
											2025-09-29 09:40:02 +00:00
										 |  |  | //            DynamicObject[] tqq9_goodlotmanages=new DynamicObject[billentry.size()];
 | 
					
						
							|  |  |  |             DynamicObjectCollection tqq9_goodlotmanages = new DynamicObjectCollection();
 | 
					
						
							|  |  |  |             boolean falg = false;
 | 
					
						
							| 
									
										
										
										
											2025-09-03 09:49:27 +00:00
										 |  |  |             for (int i = 0; i < billentry.size(); i++) {
 | 
					
						
							|  |  |  |                 DynamicObject entry = billentry.get(i);
 | 
					
						
							|  |  |  |                 DynamicObject tqq9_goodlotmanage = BusinessDataServiceHelper.newDynamicObject("tqq9_goodlotmanage");
 | 
					
						
							|  |  |  |                 Date producedate = entry.getDate("producedate");//生产日期
 | 
					
						
							|  |  |  |                 Date expirydate = entry.getDate("expirydate");//到期日期
 | 
					
						
							|  |  |  |                 String lotnumber = entry.getString("lotnumber");//批号
 | 
					
						
							|  |  |  |                 DynamicObject tqq9_registration = entry.getDynamicObject("tqq9_registration");//商品注册证
 | 
					
						
							|  |  |  |                 DynamicObject materialpurch = entry.getDynamicObject("material");//物料采购信息
 | 
					
						
							|  |  |  |                 DynamicObject masterid = materialpurch.getDynamicObject("masterid");//物料id
 | 
					
						
							|  |  |  |                 DynamicObject material = materialMap.get(masterid.getLong("id"));//物料
 | 
					
						
							|  |  |  |                 DynamicObject tqq9_brand = material.getDynamicObject("tqq9_brand");//商品品牌
 | 
					
						
							|  |  |  |                 String number = material.getString("number");//商品编码
 | 
					
						
							|  |  |  |                 String name = null;
 | 
					
						
							| 
									
										
										
										
											2025-09-29 09:40:02 +00:00
										 |  |  |                 if (tqq9_registration != null) {
 | 
					
						
							|  |  |  |                     name = tqq9_registration.getString("name");
 | 
					
						
							| 
									
										
										
										
											2025-09-03 09:49:27 +00:00
										 |  |  |                 }
 | 
					
						
							| 
									
										
										
										
											2025-09-29 09:40:02 +00:00
										 |  |  |                 tqq9_goodlotmanage.set("number", number);//商品编码
 | 
					
						
							|  |  |  |                 tqq9_goodlotmanage.set("tqq9_lot", lotnumber);//批号
 | 
					
						
							|  |  |  |                 tqq9_goodlotmanage.set("name", name);//商品注册证名称
 | 
					
						
							|  |  |  |                 tqq9_goodlotmanage.set("tqq9_brand", tqq9_brand);//商品品牌
 | 
					
						
							|  |  |  |                 tqq9_goodlotmanage.set("tqq9_crreatdate", producedate);//生产日期
 | 
					
						
							|  |  |  |                 tqq9_goodlotmanage.set("tqq9_invaliddate", expirydate);//到期日期
 | 
					
						
							|  |  |  |                 tqq9_goodlotmanage.set("status", "C");//数据状态
 | 
					
						
							|  |  |  |                 tqq9_goodlotmanage.set("enable", "1");//使用状态
 | 
					
						
							|  |  |  |                 QFilter goodlotmanageF = new QFilter("number", QCP.equals, number);
 | 
					
						
							|  |  |  |                 goodlotmanageF = goodlotmanageF.and(new QFilter("tqq9_lot", QCP.equals, lotnumber));
 | 
					
						
							|  |  |  |                 DynamicObject tqq9_goodlotmanage1 = BusinessDataServiceHelper.loadSingle("tqq9_goodlotmanage", goodlotmanageF.toArray());
 | 
					
						
							|  |  |  |                 if (tqq9_goodlotmanage1 == null && StringUtils.isNotBlank(lotnumber)) {
 | 
					
						
							|  |  |  |                     falg = true;
 | 
					
						
							|  |  |  |                     tqq9_goodlotmanages.add(tqq9_goodlotmanage);
 | 
					
						
							| 
									
										
										
										
											2025-09-03 09:49:27 +00:00
										 |  |  |                 }
 | 
					
						
							| 
									
										
										
										
											2025-10-29 09:57:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 String warehouseNumber = null;
 | 
					
						
							|  |  |  |                 DynamicObject warehouse = entry.getDynamicObject("warehouse");
 | 
					
						
							|  |  |  |                 if(warehouse != null){
 | 
					
						
							|  |  |  |                     warehouseNumber = warehouse.getString("number");
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-30 04:23:25 +00:00
										 |  |  |                 DynamicObject tqq9_goods = entry.getDynamicObject("tqq9_goods");
 | 
					
						
							|  |  |  |                 if(tqq9_goods != null){
 | 
					
						
							|  |  |  |                     tqq9_goods = BusinessDataServiceHelper.loadSingle(tqq9_goods.getPkValue(), tqq9_goods.getDynamicObjectType().getName());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-29 09:57:36 +00:00
										 |  |  |                     // 物料已经被封装成了商品
 | 
					
						
							|  |  |  |                     boolean isExistEntry = false;//在明细中是否能找到对应的商品信息
 | 
					
						
							| 
									
										
										
										
											2025-10-30 04:23:25 +00:00
										 |  |  |                     DynamicObjectCollection entryies = tqq9_goods.getDynamicObjectCollection("tqq9_entry");
 | 
					
						
							| 
									
										
										
										
											2025-10-29 09:57:36 +00:00
										 |  |  |                     for (DynamicObject entry1 : entryies) {
 | 
					
						
							|  |  |  |                         String lotNumber = null;
 | 
					
						
							|  |  |  |                         DynamicObject tqq9_lot = entry1.getDynamicObject("tqq9_lot");
 | 
					
						
							|  |  |  |                         if(tqq9_lot != null){
 | 
					
						
							|  |  |  |                             lotNumber = tqq9_lot.getString("number");
 | 
					
						
							|  |  |  |                         }
 | 
					
						
							|  |  |  |                         String whNumber = "";
 | 
					
						
							|  |  |  |                         DynamicObject wh = entry1.getDynamicObject("tqq9_basedatafield");
 | 
					
						
							|  |  |  |                         if(wh != null){
 | 
					
						
							|  |  |  |                             whNumber = wh.getString("number");
 | 
					
						
							|  |  |  |                         }
 | 
					
						
							|  |  |  |                         Date tqq9_productdate = entry1.getDate("tqq9_productdate");
 | 
					
						
							|  |  |  |                         Date tqq9_todate = entry1.getDate("tqq9_todate");
 | 
					
						
							| 
									
										
										
										
											2025-10-30 03:40:03 +00:00
										 |  |  |                         if(((StringUtils.isBlank(lotNumber) && StringUtils.isBlank(lotnumber)) || lotNumber.equals(lotnumber))
 | 
					
						
							|  |  |  |                                 && ((StringUtils.isBlank(whNumber) && StringUtils.isBlank(warehouseNumber)) || whNumber.equals(warehouseNumber))
 | 
					
						
							| 
									
										
										
										
											2025-10-29 09:57:36 +00:00
										 |  |  |                                 && ((tqq9_productdate == null && producedate == null) || (tqq9_productdate.equals(producedate)))
 | 
					
						
							|  |  |  |                                 && ((tqq9_todate == null && expirydate == null) || (tqq9_todate.equals(expirydate)))){
 | 
					
						
							|  |  |  |                             // 如果现有的商品数据存在
 | 
					
						
							|  |  |  |                             BigDecimal tqq9_availablestock = entry1.getBigDecimal("tqq9_availablestock");
 | 
					
						
							|  |  |  |                             entry1.set("tqq9_availablestock", tqq9_availablestock.add(entry.getBigDecimal("qty")));
 | 
					
						
							|  |  |  |                             BigDecimal tqq9_upstock = entry1.getBigDecimal("tqq9_upstock");
 | 
					
						
							|  |  |  |                             entry1.set("tqq9_upstock", tqq9_upstock.add(entry.getBigDecimal("qty")));
 | 
					
						
							| 
									
										
										
										
											2025-10-30 03:40:03 +00:00
										 |  |  |                             isExistEntry = true;
 | 
					
						
							|  |  |  |                         }
 | 
					
						
							|  |  |  |                         if(isExistEntry){
 | 
					
						
							|  |  |  |                             break;
 | 
					
						
							| 
									
										
										
										
											2025-10-29 09:57:36 +00:00
										 |  |  |                         }
 | 
					
						
							|  |  |  |                     }
 | 
					
						
							|  |  |  |                     if(!isExistEntry){
 | 
					
						
							|  |  |  |                         DynamicObject entry1 = entryies.addNew();
 | 
					
						
							|  |  |  |                         entry1.set("tqq9_lot", entry.getDynamicObject("lot"));//批号
 | 
					
						
							|  |  |  |                         entry1.set("tqq9_basedatafield", warehouse);//仓库
 | 
					
						
							|  |  |  |                         entry1.set("tqq9_productdate", producedate);//生产日期
 | 
					
						
							|  |  |  |                         entry1.set("tqq9_todate", expirydate);//到期日期
 | 
					
						
							|  |  |  |                         entry1.set("tqq9_availablestock", entry.getBigDecimal("qty"));//到期日期
 | 
					
						
							|  |  |  |                         entry1.set("tqq9_upstock", entry.getBigDecimal("qty"));//到期日期
 | 
					
						
							|  |  |  |                     }
 | 
					
						
							| 
									
										
										
										
											2025-10-30 04:23:25 +00:00
										 |  |  |                     SaveServiceHelper.save(new DynamicObject[]{tqq9_goods});
 | 
					
						
							| 
									
										
										
										
											2025-10-29 09:57:36 +00:00
										 |  |  |                 }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-03 09:49:27 +00:00
										 |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2025-09-29 09:40:02 +00:00
										 |  |  |             if (falg) {
 | 
					
						
							|  |  |  |                 DynamicObject[] objects = new DynamicObject[tqq9_goodlotmanages.size()];
 | 
					
						
							|  |  |  |                 for (int i = 0; i < tqq9_goodlotmanages.size(); i++) {
 | 
					
						
							|  |  |  |                     objects[i] = tqq9_goodlotmanages.get(i);
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 SaveServiceHelper.save(objects);
 | 
					
						
							| 
									
										
										
										
											2025-09-03 09:49:27 +00:00
										 |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 |