dobe_comic8/main/java/shkd/repc/repmd/formplugin/TotalAssignmentPlugin.java

201 lines
8.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.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.EventObject;
/**
* 位置:【项目主数据】-【项目建立】-表单插件
* 功能:面积数据合计赋值
* 时间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;
@Override
public void registerListener(EventObject e) {
super.registerListener(e);
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");
}
@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){
calculateArea(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");
DynamicObjectCollection subEntryEntity = (DynamicObjectCollection) this.getModel().getValue(SUB_ENTRY);
if (subEntryEntity!=null){
Object pkValue = this.getModel().getValue("id");
BigDecimal publicAndInside = calculateArea(subEntryEntity);
//更新产品指标
setProductEntryValue(productEntry,subEntryEntity,publicAndInside);
//获取产品类型id
Long productTypeId = getProductTypeId(productEntry, subEntryEntity);
//更新指标信息
updateMetricInfo(publicAndInside,productTypeId,pkValue);
calculateArea(subEntryEntity);
}
this.getView().updateView(SUB_ENTRY);
}
}
}
private BigDecimal calculateArea(DynamicObjectCollection subEntryEntity) {
if (subEntryEntity == null || subEntryEntity.isEmpty()) {
return BigDecimal.ZERO; // 防止空集合
}
// 合计值
BigDecimal publicArea = BigDecimal.ZERO;
BigDecimal publicAndInside = BigDecimal.ZERO;
BigDecimal watertight = 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);
break;
case "防水":
watertight = watertight.add(value);
break;
default:
this.getView().showMessage("无效的分类: " + type + ", 请重新选择!");
break;
}
}
}
// 更新控件显示
updateLabel(publicAmountLabel, publicArea);
updateLabel(pubInsideAmountLabel, publicAndInside);
updateLabel(watertightAmountLabel, watertight);
return publicAndInside;
}
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,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);
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;
}
}
}
}