许鹏程
2024-11-26 036ffc1b770cf0a3247890b8fdd88884376d8b6d
src/main/java/com/product/mobile/core/service/FaceMobileService.java
@@ -3,14 +3,17 @@
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.drew.metadata.Face;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.product.common.lang.StringUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
@@ -21,12 +24,15 @@
import com.product.tool.flow.config.CmnConst;
import com.product.tool.flow.service.FlowDetailService;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @Author cheng
@@ -39,8 +45,12 @@
   @Resource
   FlowDetailService flowDetailService;
   @Autowired
   public BaseDao baseDao;
   ///获取表单总入口
   /**
    *
    * @param fse传入表名,或 表UUID
    * @return
    */
@@ -66,7 +76,7 @@
         }
      }
      if (tableUuid != null) {
      if (tableUuid != null && fse.getString("table_name") == null) {
         FieldSetEntity tableInfo = BaseUtil.getSingleInfoByCache("所有表信息(uuid)", new String[]{tableUuid});
         if (!FieldSetEntity.isEmpty(tableInfo)) {
@@ -76,7 +86,7 @@
      }
      List<FaceFieldEntity> ls = getFaceFieldByTable(fse);
      if (ls == null) ls = new ArrayList<FaceFieldEntity>();
      addproperty(tableUuid, fse.getString("table_name"), ls);
      //所有表单uuid自动添加,并为hidden
      return ls;
@@ -85,13 +95,11 @@
   //递归把主表和子表的字段属性添加进去,如果没有配表单,则取表数据结构进行展示
   /**
    *
    * @param ls查出来的表单信息
    * @param tables     记录的所有表,对应的表单信息
    * @param tables 记录的所有表,对应的表单信息
    */
   private void addproperty(String table_uuid, String table_name, List<FaceFieldEntity> ls) {
      if (ls == null) {
         ls = new ArrayList<FaceFieldEntity>();
      }
      if (ls.size() > 0) {
         for (int i = 0; i < ls.size(); i++) {
            String subTableName = ls.get(i).getSubTableName();///可能是子表
@@ -115,6 +123,16 @@
                        !StringUtils.isEmpty(dt.getFieldSetEntity(0).getString("field_show_name"))) {
                     ls.get(i).setName(dt.getFieldSetEntity(0).getString("field_show_name"));
                  }
                  if ("file".indexOf(ls.get(i).getComponentType()) != -1) {
                     //附件
                     //获取字段类型
                     String fieldType = dt.getString(0, "field_type");
                     if ("file-image".equals(fieldType)) {
                        ls.get(i).putOtherParam("fieldType", "image");
                     } else {
//                        ls.get(i).putOtherParam("fieldType", "file");
                     }
                  }
               }
               ls.get(i).setTable(table_name);
            } else {
@@ -130,34 +148,79 @@
         DataTableEntity dt = DataPoolCacheImpl.getInstance().getCacheData("表字段信息", new String[]{table_uuid, "uuid"});
         if (!DataTableEntity.isEmpty(dt)) {
            FaceFieldEntity uuid = new FaceFieldEntity();
            uuid.setComponentType("uuid");
            uuid.setField("uuid");
            uuid.setName("编  码");
            uuid.setType("String");
            uuid.setTable(table_name);
            uuid.setDisabled("0");
            uuid.setHidden("1");
            ls.add(uuid);
         }
      } else {///以表单设计为准,如果没有配置表单,则以数据表为准,全部保留
         DataTableEntity dte = DataPoolCacheImpl.getInstance().getCacheData("所有字段信息并按表分组", new String[]{table_uuid});
         for (int j = 0; j < dte.getRows(); j++) {
            FaceFieldEntity uuid = new FaceFieldEntity();
            FieldSetEntity f = dte.getFieldSetEntity(j);
            if ("pk".equals(f.getString("field_type"))
                  || "updated_by".equals(f.getString("field_name"))
                  || "uuid".equals(f.getString("field_name"))
                  || "updated_utc_datetime".equals(f.getString("field_name"))) {
               continue;
         toConvertFace(table_uuid, table_name, ls, null);
         //返回子表
         Object[][] sub = baseDao.getSubTableName(table_name);
         if (sub != null && sub.length > 0 && sub[0].length > 0) {
            for (int i = 0; i < sub[0].length; i++) {
               toConvertFace(sub[0][i].toString(), sub[1][i].toString(), new ArrayList<FaceFieldEntity>(), ls);
            }
            uuid.setPrompt(f.getString("field_reference"));
            uuid.setName(f.getString("field_show_name"));
            uuid.setField(f.getString("field_name"));
            uuid.setType(f.getString("field_type"));
            uuid.setTable(table_name);
            ls.add(uuid);
         }
      }
   }
   /**
    * 数据库表字段定义转换成表单定义
    * @param table_uuid
    * @param table_name
    * @param ls
    * @param pls 父界面
    */
   private void toConvertFace(String table_uuid, String table_name, List<FaceFieldEntity> ls, List<FaceFieldEntity> pls) {
      if (ls == null) {
         ls = new ArrayList<FaceFieldEntity>();
      }
      DataTableEntity dte = DataPoolCacheImpl.getInstance().getCacheData("所有字段信息并按表分组", new String[]{table_uuid});
      for (int j = 0; j < dte.getRows(); j++) {
         FaceFieldEntity uuid = new FaceFieldEntity();
         FieldSetEntity f = dte.getFieldSetEntity(j);
         if ("pk".equals(f.getString("field_type"))
               || "updated_by".equals(f.getString("field_name"))
               || "updated_utc_datetime".equals(f.getString("field_name"))) {
            continue;
         }
         if ("uuid".equals(f.getString("field_name"))) {
            uuid.setHidden("1");
         }
         if ("userids".equals(f.getString("field_type")) || "orgUuids".equals(f.getString("field_type"))) {
            uuid.setMultiple("1");
         } else {
            uuid.setMultiple("0");
         }
         uuid.setComponentType("");
         uuid.setPrompt(f.getString("field_reference"));
         uuid.setName(f.getString("field_show_name"));
         uuid.setField(f.getString("field_name"));
         uuid.setType(f.getString("field_type"));
         uuid.setRequired(f.getString("is_required"));
         uuid.setTable(table_name);
         ls.add(uuid);
      }
      if (pls != null) {
         FaceFieldEntity uuid = new FaceFieldEntity();
         uuid.setHidden("");
         uuid.setComponentType("subTable");
         uuid.setPrompt("");
         uuid.setName("");
         uuid.setField("");
         uuid.setType("");
         uuid.setSubField(ls);
         uuid.setSubTableName(table_name);
         pls.add(uuid);
      }
   }
   public List<FaceFieldEntity> getFaceFieldByTable(FieldSetEntity fse) {
      String tableUuid = fse.getString("table_uuid");
@@ -214,7 +277,8 @@
   private List<FaceFieldEntity> getFaceField(FieldSetEntity faceFse) {
      if (FieldSetEntity.isEmpty(faceFse) || DataTableEntity.isEmpty(faceFse.getSubDataTable("product_sys_face_control_property"))) {
         throw new BaseException(MobileCoreCode.GET_FACE_CONFIG_FAIL);
         return null;
//         throw new BaseException(MobileCoreCode.GET_FACE_CONFIG_FAIL);
      }
      DataTableEntity productSysFaceControlProperty = faceFse.getSubDataTable("product_sys_face_control_property");
@@ -250,7 +314,7 @@
      }
      List<FaceFieldEntity> result = new ArrayList<>();
      List<List<FieldSetEntity>> subTableField = new ArrayList<>();
      Map<String, List<FieldSetEntity>> subTableFieldMap = new HashMap<>();
      for (FieldSetEntity fs : tagFieldSetEntityList) {
         String groupUuid = fs.getString("group_uuid");
         String parentUuid = fs.getString("parent_uuid");
@@ -258,16 +322,21 @@
            continue;
         }
         List<FieldSetEntity> fieldSetEntity = productSysFaceControlProperty.getFieldSetEntity(parentUuid);
         if (CollectionUtil.isEmpty(fieldSetEntity)) {
            continue;
         int i = result.size();
         to(groupMap, fieldSetEntity, subTableFieldMap, result, parentMap);
         String parentUuid1 = fieldSetEntity.get(0).getString("parent_uuid");
         if (!CollectionUtil.isEmpty(fieldSetEntity) && !StringUtils.isEmpty(parentUuid1)) {
            if (i + 1 == result.size()) {
               result.get(i).putOtherParam("parent_uuid", parentUuid1);
            }
         }
         to(groupMap, fieldSetEntity, subTableField, result, parentMap);
      }
      if (!CollectionUtil.isEmpty(subTableField)) {
      //根据group_uuid 将子表字段分组5
      if (!CollectionUtil.isEmpty(subTableFieldMap)) {
         //根据子表表名装载
         Map<String, FaceFieldEntity> tableByFaceField = new HashMap<>();
         for (List<FieldSetEntity> fieldSetEntities : subTableField) {
         for (Map.Entry<String, List<FieldSetEntity>> entry : subTableFieldMap.entrySet()) {
            List<FieldSetEntity> fieldSetEntities = entry.getValue();
            String parentUuid = fieldSetEntities.get(0).getString("parent_uuid");
            //先找到children
            List<FieldSetEntity> fieldSetEntity = productSysFaceControlProperty.getFieldSetEntity(parentUuid);
@@ -313,6 +382,12 @@
               //不是子表组件
               continue;
            }
            List<FaceFieldEntity> fields = new ArrayList<>();
            for (FieldSetEntity setEntity : fieldSetEntities) {
               to(groupMap, Lists.newArrayList(setEntity), null, fields, parentMap);
            }
            faceFieldEntity.setSubField(fields);
            for (FieldSetEntity setEntity : groupMap.get(groupUuid)) {
               String propertyName = setEntity.getString("property_name");
               if ("subTableName".equals(propertyName)) {
@@ -322,6 +397,8 @@
                  faceFieldEntity.setAllowAdd(setEntity.getBoolean("property_value"));
               } else if ("delete".equals(propertyName)) {
                  faceFieldEntity.setAllowDelete(setEntity.getBoolean("property_value"));
               } else if ("control_sequence".equals(propertyName)) {
                  faceFieldEntity.putOtherParam("controlSequence", setEntity.getInteger("property_value"));
               }
            }
            List<FaceFieldEntity> subFieldList = faceFieldEntity.getSubField();
@@ -329,18 +406,24 @@
               subFieldList = new ArrayList<>();
               faceFieldEntity.setSubField(subFieldList);
            }
            for (List<FieldSetEntity> setEntities : subTableField) {
               to(groupMap, setEntities, null, subFieldList, parentMap);
            }
            if (!tableByFaceField.containsKey(uuid)) {
               tableByFaceField.put(uuid, faceFieldEntity);
               result.add(faceFieldEntity);
            }
            break;
         }
      }
      result.sort((item, item2) -> {
         Object o = item.getOtherParams().get("controlSequence");
         if (o == null) {
            return -1;
         }
         Object o1 = item2.getOtherParams().get("controlSequence");
         if (o1 == null) {
            return 1;
         }
         return NumberUtil.parseInt(item.getOtherParams().get("controlSequence").toString()) - NumberUtil.parseInt(item2.getOtherParams().get("controlSequence").toString());
      });
      return result;
   }
@@ -365,13 +448,13 @@
            map.put(split1[0].trim(), value);
         }
         lines.set(i, lines.get(i).substring(0, lines.get(i).indexOf("WHERE")) + " WHERE `uuid`=" + map.get("uuid")+";");
         lines.set(i, lines.get(i).substring(0, lines.get(i).indexOf("WHERE")) + " WHERE `uuid`=" + map.get("uuid") + ";");
      }
      FileUtil.writeLines(lines, textFilePath, CharsetUtil.defaultCharset());
   }
   private void to(Map<String, List<FieldSetEntity>> groupMap, List<FieldSetEntity> fieldSetEntity, List<List<FieldSetEntity>> subTableField, List<FaceFieldEntity> result, Map<String, List<FieldSetEntity>> parentMap) {
   private void to(Map<String, List<FieldSetEntity>> groupMap, List<FieldSetEntity> fieldSetEntity, Map<String, List<FieldSetEntity>> subTableField, List<FaceFieldEntity> result, Map<String, List<FieldSetEntity>> parentMap) {
      FieldSetEntity parent = fieldSetEntity.get(0);
      String groupUuid1 = parent.getString("group_uuid");
      if (StringUtils.isEmpty(groupUuid1)) {
@@ -385,15 +468,28 @@
         for (FieldSetEntity setEntity : parentGroup) {
            //找到属性名称= __vModel__
            String propertyName = setEntity.getString("property_name");
            if ("componentType".equals(propertyName) || "prompt".equals(propertyName)) {
               continue;
            }
            if ("parentType".equals(propertyName) && "subTable".equals(setEntity.getString("property_value")) && subTableField != null) {
               //子表字段
               subTableField.add(parentGroup);
               String parentUuid = setEntity.getString("parent_uuid");
               List<FieldSetEntity> fieldSetEntities = subTableField.get(parentUuid);
               if (fieldSetEntities == null) {
                  fieldSetEntities = new ArrayList<>();
                  subTableField.put(parentUuid, fieldSetEntities);
               }
               fieldSetEntities.add(setEntity);
               result.remove(faceFieldEntity);
               break;
            }
            if ("__vModel__".equals(setEntity.getString("property_name"))) {
               result.add(faceFieldEntity);
               propertyName = "field";
            } else if ("label".equals(setEntity.getString("property_name"))) {
               propertyName = "name";
            } else if ("advanceName".equals(setEntity.getString("property_name"))) {
               propertyName = "prompt";
            } else if ("__config__".equals(propertyName)) {
               __config__ = setEntity.getUUID();
            }
@@ -404,7 +500,10 @@
               }
            }
            propertyName = StrUtil.toCamelCase(propertyName);
            if (ReflectUtil.hasField(FaceFieldEntity.class, propertyName)) {
            if ("defaultValue".equals(propertyName) && !"0".equals(setEntity.getString("property_value")) && !"1".equals(setEntity.getString("property_value"))) {
               continue;
            }
            if (ReflectUtil.hasField(FaceFieldEntity.class, propertyName) && !"field".equals(setEntity.getString("property_name")) && !"prompt".equals(setEntity.getString("property_name"))) {
               ReflectUtil.setFieldValue(faceFieldEntity, propertyName, setEntity.getObject("property_value"));
            } else if (!StringUtils.isEmpty(setEntity.getObject("property_value"))) {
@@ -439,15 +538,25 @@
      for (FieldSetEntity setEntity : configGroup) {
         //找到属性名称= __vModel__
         String propertyName = setEntity.getString("property_name");
         if ("componentType".equals(propertyName) || "prompt".equals(propertyName)) {
            continue;
         }
         if ("tag".equals(propertyName)) {
            propertyName = "component_type";
         } else if ("field".equals(propertyName)) {
            propertyName = "fileField";
         } else if ("tableName".equals(propertyName)) {
            propertyName = "fileTableName";
         } else if ("label".equals(setEntity.getString("property_name"))) {
            propertyName = "name";
         } else if ("advanceName".equals(setEntity.getString("property_name"))) {
            propertyName = "prompt";
         }
         propertyName = StrUtil.toCamelCase(propertyName);
         if (ReflectUtil.hasField(FaceFieldEntity.class, propertyName)) {
         if ("defaultValue".equals(propertyName) && !"0".equals(setEntity.getString("property_value")) && !"1".equals(setEntity.getString("property_value"))) {
            continue;
         }
         if (ReflectUtil.hasField(FaceFieldEntity.class, propertyName) && !"field".equals(setEntity.getString("property_name")) && !"prompt".equals(setEntity.getString("property_name"))) {
            ReflectUtil.setFieldValue(faceFieldEntity, propertyName, setEntity.getObject("property_value"));
         }
      }