package shkd.repc.repmd.formplugin; import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.entity.DynamicObjectCollection; import kd.bos.dataentity.utils.StringUtils; import kd.bos.entity.datamodel.IDataModel; import kd.bos.entity.datamodel.events.BeforeImportEntryEventArgs; import kd.bos.entity.datamodel.events.ImportDataEventArgs; import kd.bos.entity.datamodel.events.PropertyChangedArgs; import kd.bos.form.control.Control; import kd.bos.form.control.EntryGrid; import kd.bos.form.control.Label; import kd.bos.form.control.events.ItemClickEvent; import kd.bos.form.control.events.RowClickEvent; import kd.bos.form.control.events.RowClickEventListener; import kd.bos.form.plugin.AbstractFormPlugin; 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; import kd.tsc.tsrbs.business.domain.oprecord.service.helper.OprecordHelper; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.EventObject; import java.util.HashMap; /** * 位置:【项目主数据】-【项目建立】-表单插件 * 功能:面积数据合计赋值 * 时间:2024/12/16 */ public class TotalAssignmentPlugin extends AbstractFormPlugin implements RowClickEventListener, Plugin { private static final String ENTRY_VALUE = "qeug_jrgsbsz"; private static final String PRODUCT_ENTRY = "productentry"; private static final String SUB_ENTRY = "qeug_subentryentity"; // 控件缓存 private Label publicAmountLabel; private Label pubInsideAmountLabel; private Label watertightAmountLabel; private Label hardcoverAmount; @Override public void registerListener(EventObject e) { super.registerListener(e); this.addItemClickListeners("tbmain"); EntryGrid entryGrid = this.getControl(PRODUCT_ENTRY); entryGrid.addRowClickListener(this); // 缓存控件,减少重复调用 publicAmountLabel = (Label) this.getControl("qeug_publicamount"); pubInsideAmountLabel = (Label) this.getControl("qeug_pubinsideamount"); watertightAmountLabel = (Label) this.getControl("qeug_watertightamount"); hardcoverAmount=(Label) this.getControl("qeug_hardcoveramount"); } @Override public void entryRowClick(RowClickEvent evt) { EntryGrid entryGrid = (EntryGrid) evt.getSource(); if (PRODUCT_ENTRY.equals(entryGrid.getKey())) { // 业务逻辑 DynamicObjectCollection subEntryEntity = (DynamicObjectCollection) this.getModel().getValue(SUB_ENTRY); if (subEntryEntity!=null){ onlyCalculateArea(subEntryEntity); } this.getView().updateView(SUB_ENTRY); } } @Override public void propertyChanged(PropertyChangedArgs e) { super.propertyChanged(e); String fieldKey = e.getProperty().getName(); if (ENTRY_VALUE.equals(fieldKey)) { DynamicObjectCollection productEntry = this.getModel().getEntryEntity(PRODUCT_ENTRY); if (productEntry!=null){ // 缓存控件,减少重复调用 publicAmountLabel = (Label) this.getControl("qeug_publicamount"); pubInsideAmountLabel = (Label) this.getControl("qeug_pubinsideamount"); watertightAmountLabel = (Label) this.getControl("qeug_watertightamount"); hardcoverAmount=(Label) this.getControl("qeug_hardcoveramount"); DynamicObjectCollection subEntryEntity = (DynamicObjectCollection) this.getModel().getValue(SUB_ENTRY); if (subEntryEntity!=null){ //获取产品类型id Long productTypeId = getProductTypeId(productEntry, subEntryEntity); Object pkValue = this.getModel().getValue("id"); BigDecimal publicAndInside = calculateArea(subEntryEntity,productTypeId,pkValue); //更新产品指标 setProductEntryValue(productEntry,subEntryEntity,publicAndInside); } this.getView().updateView(SUB_ENTRY); } } } private BigDecimal calculateArea(DynamicObjectCollection subEntryEntity,Long productTypeId,Object pkValue ) { if (subEntryEntity == null || subEntryEntity.isEmpty()) { return BigDecimal.ZERO; // 防止空集合 } // 合计值 BigDecimal publicArea = BigDecimal.ZERO; BigDecimal publicAndInside = BigDecimal.ZERO; BigDecimal watertight = BigDecimal.ZERO; BigDecimal hardcover = BigDecimal.ZERO; // 分类处理 for (DynamicObject entry : subEntryEntity) { String type = entry.getString("qeug_fl"); BigDecimal value = entry.getBigDecimal(ENTRY_VALUE); if (StringUtils.isNotEmpty(type)) { switch (type) { case "公区": publicArea = publicArea.add(value); publicAndInside = publicAndInside.add(value); break; case "套内": publicAndInside = publicAndInside.add(value); hardcover = hardcover.add(value); break; case "防水": watertight = watertight.add(value); break; default: this.getView().showMessage("无效的分类: " + type + ", 请重新选择!"); break; } } } // 更新控件显示 updateLabel(publicAmountLabel, publicArea); updateLabel(pubInsideAmountLabel, publicAndInside); updateLabel(watertightAmountLabel, watertight); updateLabel(hardcoverAmount,hardcover); updateMetricInfo(publicAndInside,hardcover,productTypeId,pkValue); return publicAndInside; } private void onlyCalculateArea(DynamicObjectCollection subEntryEntity) { if (subEntryEntity == null || subEntryEntity.isEmpty()) { return ; // 防止空集合 } // 合计值 BigDecimal publicArea = BigDecimal.ZERO; BigDecimal publicAndInside = BigDecimal.ZERO; BigDecimal watertight = BigDecimal.ZERO; BigDecimal hardcover = BigDecimal.ZERO; // 分类处理 for (DynamicObject entry : subEntryEntity) { String type = entry.getString("qeug_fl"); BigDecimal value = entry.getBigDecimal(ENTRY_VALUE); if (StringUtils.isNotEmpty(type)) { switch (type) { case "公区": publicArea = publicArea.add(value); publicAndInside = publicAndInside.add(value); break; case "套内": publicAndInside = publicAndInside.add(value); hardcover = hardcover.add(value); break; case "防水": watertight = watertight.add(value); break; default: this.getView().showMessage("无效的分类: " + type + ", 请重新选择!"); break; } } } // 更新控件显示 updateLabel(publicAmountLabel, publicArea); updateLabel(pubInsideAmountLabel, publicAndInside); updateLabel(watertightAmountLabel, watertight); updateLabel(hardcoverAmount,hardcover); } private void updateLabel(Label label, BigDecimal value) { // 设置值并确保格式化保留两位小数 if (label != null && value != null) { label.setText(value.setScale(2, RoundingMode.HALF_UP).toString()); } } private void updateMetricInfo(BigDecimal publicAndInside,BigDecimal hardcover,Long productTypeId,Object pkValue ){ QFilter qFilter = new QFilter("mainprojectid", QCP.equals, pkValue); DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle( "repmd_index_products",qFilter.toArray()); if (null!=dynamicObject){ DynamicObjectCollection buildingEntry = dynamicObject.getDynamicObjectCollection("buildingindexentry"); if (buildingEntry!=null&&buildingEntry.size()!=0){ for (DynamicObject entry : buildingEntry) { Long typeId = entry.getLong("buildentry_producttype.id"); if (typeId.compareTo(productTypeId)==0){ entry.set("buildentry_allbuildarea",publicAndInside); entry.set("buildentry_onbuildarea",publicAndInside); entry.set("buildentry_downbuildarea",BigDecimal.ZERO); entry.set("buildentry_finedecortarea",hardcover); break; } } } } SaveServiceHelper.update(dynamicObject); } private Long getProductTypeId(DynamicObjectCollection productEntry,DynamicObjectCollection subEntryEntity){ Long typeId = null; DynamicObject dynamicObject = subEntryEntity.get(0); DynamicObject parent = (DynamicObject) dynamicObject.getParent(); Long subEntryId = parent.getLong("id"); for (DynamicObject entry : productEntry) { Long proEntryId = entry.getLong("id"); if (subEntryId.compareTo(proEntryId)==0){ typeId = entry.getLong("productentry_producttype.id"); break; } } return typeId; } /** * 产品构成分录赋值 * @param productEntry 产品构成分录 * @param subEntryEntity 面积数据分录 * @param publicAndInside 改建后面积 */ private void setProductEntryValue(DynamicObjectCollection productEntry,DynamicObjectCollection subEntryEntity,BigDecimal publicAndInside){ DynamicObject dynamicObject = subEntryEntity.get(0); DynamicObject dynamicObject2 = (DynamicObject) dynamicObject.getParent(); Long pkValue = (Long) dynamicObject2.getPkValue(); for (int i = 0; i < productEntry.size(); i++) { DynamicObject dynamicObject1 = productEntry.get(i); Long id = (Long)dynamicObject1.getPkValue(); if (pkValue.compareTo(id)==0){ dynamicObject1.set("productentry_buildingarea",publicAndInside); this.getView().updateView("productentry_buildingarea",i); break; } } } @Override public void afterBindData(EventObject e) { super.afterBindData(e); EntryGrid entryGrid = this.getControl("productentry"); DynamicObjectCollection productEntry = this.getModel().getEntryEntity(PRODUCT_ENTRY); if (productEntry!=null) { //选中最新的导入数据 entryGrid.selectRows(productEntry.size() - 1); //先清空计入估算表数值再赋值,触发保存 DynamicObjectCollection subEntryEntity = (DynamicObjectCollection) this.getModel().getValue(SUB_ENTRY); if (subEntryEntity!=null&& subEntryEntity.size()!=0) { for (int i = 0; i < subEntryEntity.size(); i++) { this.getModel().setValue("qeug_jrgsbsz", BigDecimal.ZERO, i); DynamicObject entry = subEntryEntity.get(i); BigDecimal all = entry.getBigDecimal("qeug_hjs"); this.getModel().setValue("qeug_jrgsbsz", all, i); } //反写产品构成和指标信息 //获取产品类型id Long productTypeId = getProductTypeId(productEntry, subEntryEntity); Object pkValue = this.getModel().getValue("id"); BigDecimal publicAndInside = calculateArea(subEntryEntity,productTypeId,pkValue); //更新产品指标 setProductEntryValue(productEntry,subEntryEntity,publicAndInside); } } } }