|
@@ -0,0 +1,228 @@
|
|
|
|
|
+package nckd.fi.er.formplugin;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
|
+import kd.bos.dataentity.serialization.SerializationUtils;
|
|
|
|
|
+import kd.bos.entity.EntityMetadataCache;
|
|
|
|
|
+import kd.bos.entity.EntityTypeUtil;
|
|
|
|
|
+import kd.bos.entity.GetFilterFieldsParameter;
|
|
|
|
|
+import kd.bos.entity.MainEntityType;
|
|
|
|
|
+import kd.bos.entity.filter.*;
|
|
|
|
|
+import kd.bos.exception.KDBizException;
|
|
|
|
|
+import kd.bos.form.control.Button;
|
|
|
|
|
+import kd.bos.form.control.FilterGrid;
|
|
|
|
|
+import kd.bos.form.plugin.AbstractFormPlugin;
|
|
|
|
|
+import nckd.fi.er.common.enums.BillStatusEnums;
|
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+import java.util.stream.IntStream;
|
|
|
|
|
+
|
|
|
|
|
+public class FilterGridFormPlugin extends AbstractFormPlugin {
|
|
|
|
|
+ /**确定按钮*/
|
|
|
|
|
+ private final static String BTNOK = "btnok";
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 比较符
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final List<String> compareTypes = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ static {
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.CHECKBOXEQUAL.getId());
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.EQUAL.getId());
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.NOTEQUAL.getId());
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.CHECKBOXNOTEQUAL.getId());
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.GREATER.getId());
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.LESS.getId());
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.LESSOREQUAL.getId());
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.GREATEROREQUAL.getId());
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.ISNULL.getId());
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.ISNOTNULL.getId());
|
|
|
|
|
+ compareTypes.add(CompareTypeEnum.ORGEQUAL.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void registerListener(EventObject e) {
|
|
|
|
|
+ super.registerListener(e);
|
|
|
|
|
+ this.addClickListeners(BTNOK);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void afterBindData(EventObject e) {
|
|
|
|
|
+ super.afterBindData(e);
|
|
|
|
|
+ //父页面单据状态
|
|
|
|
|
+ String status = (String) this.getView().getParentView().getModel().getValue("status");
|
|
|
|
|
+ // //非暂存状态,不可编辑过滤条件
|
|
|
|
|
+ if (!BillStatusEnums.SAVE.getValue().equals(status)) {
|
|
|
|
|
+ this.getView().setVisible(Boolean.FALSE, "operatepanel");
|
|
|
|
|
+ this.getView().setEnable(Boolean.FALSE, "nckd_filtergridap");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //业务对象编码
|
|
|
|
|
+ String bizObjectNumber = this.getView().getFormShowParameter().getCustomParam("bizobjectid");
|
|
|
|
|
+ //过滤条件
|
|
|
|
|
+ String qFilter = this.getView().getFormShowParameter().getCustomParam("qfilter");
|
|
|
|
|
+ //设置通用过滤控件业务对象
|
|
|
|
|
+ setQFilter(bizObjectNumber, qFilter);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void click(EventObject evt) {
|
|
|
|
|
+ super.click(evt);
|
|
|
|
|
+ String key = ((Button) evt.getSource()).getKey();
|
|
|
|
|
+ if (key.equals(BTNOK)) {
|
|
|
|
|
+ this.getView().returnDataToParent(setFieldCondition());
|
|
|
|
|
+ this.getView().close();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 设置字段配置信息
|
|
|
|
|
+ */
|
|
|
|
|
+ private Map<String, Object> setFieldCondition() {
|
|
|
|
|
+ Map<String, Object> filterMap = new HashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ //业务对象编码
|
|
|
|
|
+ String billSign = this.getView().getFormShowParameter().getCustomParam("bizobjectid");
|
|
|
|
|
+ FilterCondition condition = getFilterCondition();
|
|
|
|
|
+ //过滤控件序列化 保存字段中,每次加载页面是反序列化把数据放回过滤控件中显示
|
|
|
|
|
+ String filter = SerializationUtils.toJsonString(condition);
|
|
|
|
|
+ MainEntityType entity = EntityMetadataCache.getDataEntityType(billSign);
|
|
|
|
|
+ //解析过滤字段
|
|
|
|
|
+ FilterBuilder filterBuilder = new FilterBuilder(entity, condition, true);
|
|
|
|
|
+ filterBuilder.buildFilter();
|
|
|
|
|
+ //过滤控件内部主要数据(字段名、比较符、字段值)
|
|
|
|
|
+ List<SimpleFilterRow> filterRow = condition.getFilterRow();
|
|
|
|
|
+ StringBuilder parseConditionSql = new StringBuilder();
|
|
|
|
|
+ if (CollectionUtil.isEmpty(filterRow)) {
|
|
|
|
|
+ return filterMap;
|
|
|
|
|
+ }
|
|
|
|
|
+ //转化成sql
|
|
|
|
|
+ IntStream.range(0, filterRow.size()).forEach(i -> {
|
|
|
|
|
+ checkCompareType(filterRow.get(i).getCompareType());
|
|
|
|
|
+ String rowSql = parseFilterRow(filterRow.get(i), i == filterRow.size() - 1, filterBuilder.getFilterObject().getAllFilterFields());
|
|
|
|
|
+ parseConditionSql.append(rowSql);
|
|
|
|
|
+ });
|
|
|
|
|
+ //配置的字段
|
|
|
|
|
+ List<String> fieldNames = filterRow.stream().map(SimpleFilterRow::getFieldName).collect(Collectors.toList());
|
|
|
|
|
+ String fieldName = String.join(",", fieldNames);
|
|
|
|
|
+
|
|
|
|
|
+ //过滤条件信息
|
|
|
|
|
+ filterMap.put("nckd_qfilter", filter);
|
|
|
|
|
+ filterMap.put("nckd_filtercondition", parseConditionSql.toString());
|
|
|
|
|
+ filterMap.put("nckd_fieldname", fieldName);
|
|
|
|
|
+ return filterMap;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 解析字段配置拼接成sql
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param simpleFilterRow
|
|
|
|
|
+ * @param flag
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ private String parseFilterRow(SimpleFilterRow simpleFilterRow, boolean flag, Map<String, FilterField> filterFields) {
|
|
|
|
|
+ StringBuilder sql = new StringBuilder();
|
|
|
|
|
+ //字段名
|
|
|
|
|
+ String fieldName = simpleFilterRow.getFieldName();
|
|
|
|
|
+ //比较符
|
|
|
|
|
+ String compareType = simpleFilterRow.getCompareType();
|
|
|
|
|
+ //并且 或者
|
|
|
|
|
+ String logic = simpleFilterRow.getLogic();
|
|
|
|
|
+ //字段值
|
|
|
|
|
+ List<FilterValue> value = simpleFilterRow.getValue();
|
|
|
|
|
+ //字段控件
|
|
|
|
|
+ FilterField filterField = filterFields.get(fieldName);
|
|
|
|
|
+ //字段类型
|
|
|
|
|
+ Class<?> propertyType = filterField.getFieldProp().getPropertyType();
|
|
|
|
|
+ String simpleName = propertyType.getSimpleName();
|
|
|
|
|
+ sql.append(fieldName);
|
|
|
|
|
+ boolean isNumType = "BigDecimal".equals(simpleName) || "Integer".equals(simpleName)
|
|
|
|
|
+ || "Long".equals(simpleName);
|
|
|
|
|
+ if (CompareTypeEnum.CHECKBOXEQUAL.getId().equals(compareType) || CompareTypeEnum.EQUAL.getId().equals(compareType) || CompareTypeEnum.ORGEQUAL.getId().equals(compareType)) {
|
|
|
|
|
+ if (isNumType) {
|
|
|
|
|
+ sql.append(" = ").append(value.get(0).getValue());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ sql.append(" = '").append(value.get(0).getValue()).append("'");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (CompareTypeEnum.NOTEQUAL.getId().equals(compareType) || CompareTypeEnum.CHECKBOXNOTEQUAL.getId().equals(compareType)) {
|
|
|
|
|
+ if (isNumType) {
|
|
|
|
|
+ sql.append(" != ").append(value.get(0).getValue());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ sql.append(" != '").append(value.get(0).getValue()).append("'");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (CompareTypeEnum.GREATER.getId().equals(compareType)) {
|
|
|
|
|
+ sql.append(" > ").append(value.get(0).getValue());
|
|
|
|
|
+ } else if (CompareTypeEnum.LESS.getId().equals(compareType)) {
|
|
|
|
|
+ sql.append(" < ").append(value.get(0).getValue());
|
|
|
|
|
+ } else if (CompareTypeEnum.LESSOREQUAL.getId().equals(compareType)) {
|
|
|
|
|
+ sql.append(" <= ").append(value.get(0).getValue());
|
|
|
|
|
+ } else if (CompareTypeEnum.GREATEROREQUAL.getId().equals(compareType)) {
|
|
|
|
|
+ sql.append(" >= ").append(value.get(0).getValue());
|
|
|
|
|
+ } else if (CompareTypeEnum.ISNULL.getId().equals(compareType)) {
|
|
|
|
|
+ sql.append(" = null and ").append(fieldName).append(" = '' ");
|
|
|
|
|
+ } else if (CompareTypeEnum.ISNOTNULL.getId().equals(compareType)) {
|
|
|
|
|
+ sql.append(" <> null and ").append(fieldName).append(" != '' ");
|
|
|
|
|
+ ;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!flag) {
|
|
|
|
|
+ sql = new StringBuilder(simpleFilterRow.getLeftBracket() + sql.append(simpleFilterRow.getRightBracket() + " ").append("1".equals(logic) ? " OR " : " AND "));
|
|
|
|
|
+ }
|
|
|
|
|
+ return sql.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private FilterCondition getFilterCondition() {
|
|
|
|
|
+ FilterGrid filterGrid = this.getView().getControl("nckd_filtergridap");
|
|
|
|
|
+ FilterGrid.FilterGridState filterGridState = filterGrid.getFilterGridState();
|
|
|
|
|
+ return filterGridState.getFilterCondition();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 检查字段配置的条件是否支持
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param compareType
|
|
|
|
|
+ */
|
|
|
|
|
+ private void checkCompareType(String compareType) {
|
|
|
|
|
+ if (!compareTypes.contains(compareType)) {
|
|
|
|
|
+ throw new KDBizException("字段配置中的条件暂不支持,请检查修改!");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 设置通用控件业务对象
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param number 业务对象编码
|
|
|
|
|
+ */
|
|
|
|
|
+ private void setQFilter(String number, String qFilter) {
|
|
|
|
|
+ FilterGrid filterGrid = this.getView().getControl("nckd_filtergridap");
|
|
|
|
|
+
|
|
|
|
|
+ // 设置业务对象属性
|
|
|
|
|
+ MainEntityType mainEntityType = EntityMetadataCache.getDataEntityType(number);
|
|
|
|
|
+ filterGrid.setMainEntityType(mainEntityType);
|
|
|
|
|
+ filterGrid.setEntityNumber(number);
|
|
|
|
|
+
|
|
|
|
|
+ //设置字段属性
|
|
|
|
|
+ GetFilterFieldsParameter getFilterFieldsParameter = new GetFilterFieldsParameter(mainEntityType);
|
|
|
|
|
+ getFilterFieldsParameter.setNeedMulBasedataField(false);
|
|
|
|
|
+ getFilterFieldsParameter.setOnlyMainEntityField(true);
|
|
|
|
|
+ List<Map<String, Object>> filterColumns = EntityTypeUtil.createFilterColumns(getFilterFieldsParameter);
|
|
|
|
|
+ filterGrid.setFilterColumns(filterColumns);
|
|
|
|
|
+
|
|
|
|
|
+ //设置过滤条件
|
|
|
|
|
+ if (StringUtils.isNotBlank(qFilter)) {
|
|
|
|
|
+ FilterCondition filterCondition = SerializationUtils.fromJsonString(qFilter, FilterCondition.class);
|
|
|
|
|
+ filterGrid.SetValue(filterCondition);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //设置比较符,部分比较符表达式无法支撑
|
|
|
|
|
+ for (Map<String, Object> map : filterColumns) {
|
|
|
|
|
+ List<CompareTypeDto> compareTypeList = (List<CompareTypeDto>) map.get("compareTypes");
|
|
|
|
|
+ map.put("compareTypes", compareTypeList.stream()
|
|
|
|
|
+ .filter(it -> compareTypes.contains(it.getId()))
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ this.getView().updateView("nckd_filtergridap");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|