许鹏程
2024-10-16 e0613b0d64ae2ae3c6734714e1e64b268c740da6
src/main/java/com/product/face/service/FaceDesignService.java
@@ -1,13 +1,9 @@
package com.product.face.service;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.drew.metadata.Face;
import com.product.admin.service.PublicService;
import com.product.common.lang.StringUtils;
import com.product.core.config.CoreConst;
@@ -16,18 +12,15 @@
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.transfer.Transactional;
import com.product.core.util.JsonUtil;
import com.product.face.config.ErrorCode;
import com.product.face.config.FaceConst;
import com.product.face.service.ide.IFaceDesignService;
import com.product.face.util.FaceUtil;
import com.product.util.BaseUtil;
import org.apache.logging.log4j.core.Core;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @Author cheng
@@ -38,266 +31,198 @@
public class FaceDesignService extends AbstractBaseService implements IFaceDesignService {
    @Resource
    private PublicService publicService;
   @Resource
   public PublicService publicService;
    /**
     * 获取表单配置详情
     *
     * @param uuid
     * @return
     * @throws BaseException
     */
    @Override
    public String getFaceConfigInfo(String uuid) throws BaseException {
        FieldSetEntity fse = getBaseDao().getFieldSetEntity(FaceConst.TABLE_FACE_CONFIG, uuid, true);
        DataTableEntity fieldPropertyDt = fse.getSubDataTable(FaceConst.TABLE_FACE_FIELD_CONTROL_PROPERTY);
        if (DataTableEntity.isEmpty(fieldPropertyDt)) {
            return null;
        }
        List<JSONObject> result = new ArrayList<>();
        List<FieldSetEntity> data = fieldPropertyDt.getData();
        Map<String, List<FieldSetEntity>> collect = data.stream()
                .filter(item -> StringUtils.isEmpty(item.getString(FaceConst.FIELD_PARENT_UUID))) //过滤 取父级
//            .sorted(Comparator.comparing(item -> item.getInteger(FaceConst.CONTROL_SEQUENCE))) //排序
                .collect(Collectors.groupingBy(item -> item.getString(FaceConst.FIELD_GROUP_UUID))); //分组
        Map<String, List<FieldSetEntity>> groupContainer = data.stream()
                .filter(item -> !StringUtils.isEmpty(item.getString(FaceConst.FIELD_PARENT_UUID))) //过滤 取子级
                .collect(Collectors.groupingBy(item -> item.getString(FaceConst.FIELD_PARENT_UUID))); //分组
        for (Map.Entry<String, List<FieldSetEntity>> entry : collect.entrySet()) {
            List<FieldSetEntity> propertyList = entry.getValue();
            result.add(getPropertyJson(propertyList, groupContainer));
        }
        result.sort(Comparator.comparing(item -> item.getInteger(FaceConst.CONTROL_SEQUENCE)));
        System.out.println(result);
        Map<String, Object> other = new HashMap<>();
        other.put("drawingList", result);
        fse.getSubData().clear();
        return BaseUtil.success(fse, other);
    }
    private JSONObject getPropertyJson(List<FieldSetEntity> propertyList, Map<String, List<FieldSetEntity>> groupContainer) {
        JSONObject property = new JSONObject();
        String propertyValue = null;
        if (!CollectionUtil.isEmpty(propertyList)) {
            for (FieldSetEntity fs : propertyList) {
                //属性类型 1
                String groupUuid = fs.getUUID();
                String propertyType = fs.getString(FaceConst.FIELD_PROPERTY_TYPE);
                String propertyName = fs.getString(FaceConst.FIELD_PROPERTY_NAME);
                propertyValue = fs.getString(FaceConst.FIELD_PROPERTY_VALUE);
                switch (propertyType) {
                    case "1"://字符串
                        property.put(propertyName, propertyValue);
                        break;
                    case "2"://数组
                        property.put(propertyName, propertyValue.split(","));
                        break;
                    case "3"://子级对象
                        try {
                            JSONObject propertyJson = getPropertyJson(groupContainer.get(groupUuid), groupContainer);
                            property.put(propertyName, propertyJson);
                        } catch (Exception e) {
                            e.printStackTrace();
                            throw e;
                        }
                        break;
                    case "4"://子级数组对象
                        List<FieldSetEntity> propertyListChild = groupContainer.get(groupUuid);
                        if (propertyListChild != null && !propertyListChild.isEmpty()) {
                            List<Object> array = propertyListChild.stream()
                                    .collect(Collectors.groupingBy(item -> item.getString(FaceConst.FIELD_GROUP_UUID)))
                                    .values().stream().map(item -> getPropertyJson(item, groupContainer)).sorted(Comparator.comparing(item -> item.getInteger(FaceConst.CONTROL_SEQUENCE))).collect(Collectors.toList());
                            property.put(propertyName, new JSONArray(array));
                        }
//               groupContainer.put(groupUuid, array);
                        break;
                    case "5": //boolean 类型
                        Boolean value = null;
                        if ("1".equals(propertyValue)) {
                            value = true;
                        } else if ("0".equals(propertyValue)) {
                            value = false;
                        }
                        property.put(propertyName, value);
                        break;
                    case "6":// int 类型
                        if (NumberUtil.isNumber(propertyValue)) {
                            property.put(propertyName, NumberUtil.parseInt(propertyValue));
                        }
                        break;
                    case "7"://double 类型
                        if (NumberUtil.isDouble(propertyValue)) {
                            property.put(propertyName, NumberUtil.parseDouble(propertyValue));
                        }
                        break;
                }
            }
        }
        if (!StringUtils.isEmpty(propertyValue)) {
            Object o = groupContainer.get(propertyValue);
            if (o != null) {
                if (o instanceof JSONArray) {
                    ((JSONArray) o).add(property);
                } else {
                    ((JSONObject) o).putAll(property);
                    groupContainer.remove(propertyValue);
                }
            }
        }
        return property;
    }
   /**
    * 获取表单配置详情
    *
    * @param uuid
    * @return
    * @throws BaseException
    */
   @Override
   public String getFaceConfigInfo(String uuid) throws BaseException {
      FieldSetEntity fse = getBaseDao().getFieldSetEntity(FaceConst.TABLE_FACE_CONFIG, uuid, true);
      DataTableEntity fieldPropertyDt = fse.getSubDataTable(FaceConst.TABLE_FACE_FIELD_CONTROL_PROPERTY);
      if (DataTableEntity.isEmpty(fieldPropertyDt)) {
         throw new BaseException(ErrorCode.GET_FACE_CONFIG_FAIL);
      }
      List<JSONObject> result = FaceUtil.getFaceControlList(fse, fieldPropertyDt);
      Map<String, Object> other = new HashMap<>();
      other.put("drawingList", result);
      DataTableEntity faceCustomField = fse.getSubDataTable("product_sys_face_custom_field");
      if (!DataTableEntity.isEmpty(faceCustomField)) {
         for (int i = 0; i < faceCustomField.getRows(); i++) {
            faceCustomField.setFieldValue(i, "customField", true);
            faceCustomField.setFieldValue(i, "added", true);
         }
         other.put("product_sys_face_custom_field", BaseUtil.dataTableEntityToJson(faceCustomField));
      } else {
         other.put("product_sys_face_custom_field", new String[]{});
      }
      fse.getSubData().clear();
      return BaseUtil.success(fse, other);
   }
    /**
     * 保存表单内容数据
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Override
    @Transactional
    public FieldSetEntity saveFaceData(FieldSetEntity fse) throws BaseException {
        DataTableEntity fields = fse.getSubDataTable(FaceConst.FACE_FIELD);
        if (DataTableEntity.isEmpty(fields)) {
            ErrorCode.SAVE_EMPTY_FACE_FAIL.throwException();
        }
        List<FieldSetEntity> fieldSetEntityList = new ArrayList<>();
        for (int i = 0; i < fields.getRows(); i++) {
            fieldSetEntityList.addAll(controlTraverse(new JSONObject((Map) fields.getFieldSetEntity(i).getValues()), null, i));
        }
        fse.removeSubData(FaceConst.FACE_FIELD);
        BaseUtil.createCreatorAndCreationTime(fse);
        fields.setData(fieldSetEntityList);
        fse.addSubDataTable(fields);
   /**
    * 保存表单内容数据
    *
    * @param fse
    * @return
    * @throws BaseException
    */
   @Override
   @Transactional
   public FieldSetEntity saveFaceData(FieldSetEntity fse) throws BaseException {
      DataTableEntity fields = fse.getSubDataTable(FaceConst.FACE_FIELD);
      if (DataTableEntity.isEmpty(fields)) {
         ErrorCode.SAVE_EMPTY_FACE_FAIL.throwException();
      }
      List<FieldSetEntity> fieldSetEntityList = new ArrayList<>();
      for (int i = 0; i < fields.getRows(); i++) {
         if (fields.getBoolean(i, "event_property")) {
            DataTableEntity subDataTable = fields.getFieldSetEntity(i).getSubDataTable("event_array");
            if (DataTableEntity.isEmpty(subDataTable)) {
               continue;
            }
            fields.setFieldValue(i, "event_array", BaseUtil.dataTableEntityToJson(subDataTable));
         }
         fieldSetEntityList.addAll(controlTraverse(new JSONObject((Map) fields.getFieldSetEntity(i).getValues()), null, i));
      }
      fse.removeSubData(FaceConst.FACE_FIELD);
      BaseUtil.createCreatorAndCreationTime(fse);
      fields.setData(fieldSetEntityList);
      fse.addSubDataTable(fields);
//        fse.setValue(FaceConst.FIELD_FACE_TYPE, 2);
        if (!StringUtils.isEmpty(fse.getUUID())) {
            getBaseDao().delete(FaceConst.TABLE_FACE_FIELD_CONTROL_PROPERTY, "master_uuid=?", new Object[]{fse.getUUID()});
        }
        getBaseDao().saveFieldSetEntity(fse);
        fse.getSubData().clear();
        return fse;
    }
      if (!StringUtils.isEmpty(fse.getUUID())) {
         getBaseDao().delete(FaceConst.TABLE_FACE_FIELD_CONTROL_PROPERTY, "master_uuid=?", new Object[]{fse.getUUID()});
      }
      if (StringUtils.isEmpty(fse.getString("terminal_type"))) {
         fse.setValue("terminal_type", 1);
      }
      getBaseDao().saveFieldSetEntity(fse);
      fse.getSubData().clear();
      return fse;
   }
    public List<FieldSetEntity> controlTraverse(Object o, String parentUuid, Integer sequence) {
        List<FieldSetEntity> fieldSetList = new ArrayList<>();
        try {
            if (o instanceof JSONArray) {
                //数组
                JSONArray jsonArray = (JSONArray) o;
                try {
                    for (int i = 0; i < jsonArray.size(); i++) {
                        Object obj = jsonArray.get(i);
                        fieldSetList.addAll(controlTraverse(obj, parentUuid, i));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    throw e;
                }
            } else {
                //json对象
                JSONObject property = (JSONObject) o;
                String groupUuid = IdUtil.randomUUID();
                for (Map.Entry<String, Object> entry : property.entrySet()) {
   public List<FieldSetEntity> controlTraverse(Object o, String parentUuid, Integer sequence) {
      List<FieldSetEntity> fieldSetList = new ArrayList<>();
      try {
         if (o instanceof JSONArray) {
            //数组
            JSONArray jsonArray = (JSONArray) o;
            try {
               for (int i = 0; i < jsonArray.size(); i++) {
                  Object obj = jsonArray.get(i);
                  fieldSetList.addAll(controlTraverse(obj, parentUuid, i));
               }
            } catch (Exception e) {
               e.printStackTrace();
               throw e;
            }
         } else {
            //json对象
            JSONObject property = (JSONObject) o;
            String groupUuid = IdUtil.randomUUID();
            for (Map.Entry<String, Object> entry : property.entrySet()) {
                    FieldSetEntity fs = getTemplateFieldSet(entry.getKey(), entry.getValue(), groupUuid);
                    if (fs != null) {
                        int propertyType = fs.getInteger(FaceConst.FIELD_PROPERTY_TYPE).intValue();
                        if ((4 == propertyType || 3 == propertyType) && (entry.getValue() instanceof JSONArray || entry.getValue() instanceof JSONObject)) {
                            try {
                                fieldSetList.addAll(controlTraverse(entry.getValue(), fs.getUUID(), null));
                                fs.remove(FaceConst.FIELD_PROPERTY_VALUE);
                            } catch (Exception e) {
                                e.printStackTrace();
                                throw e;
                            }
                        }
                        fs.setValue(FaceConst.FIELD_PARENT_UUID, parentUuid);
                        fieldSetList.add(fs);
                    }
                }
                if (sequence != null) {
                    FieldSetEntity fs = getTemplateFieldSet(FaceConst.CONTROL_SEQUENCE, sequence, groupUuid);
                    fs.setValue(FaceConst.FIELD_PARENT_UUID, parentUuid);
                    fieldSetList.add(fs);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return fieldSetList;
    }
               FieldSetEntity fs = getTemplateFieldSet(entry.getKey(), entry.getValue(), groupUuid);
               if (fs != null) {
                  int propertyType = fs.getInteger(FaceConst.FIELD_PROPERTY_TYPE).intValue();
                  if ((4 == propertyType || 3 == propertyType) && (entry.getValue() instanceof JSONArray || entry.getValue() instanceof JSONObject)) {
                     try {
                        fieldSetList.addAll(controlTraverse(entry.getValue(), fs.getUUID(), null));
                        fs.remove(FaceConst.FIELD_PROPERTY_VALUE);
                     } catch (Exception e) {
                        e.printStackTrace();
                        throw e;
                     }
                  }
                  fs.setValue(FaceConst.FIELD_PARENT_UUID, parentUuid);
                  fieldSetList.add(fs);
               }
            }
            if (sequence != null) {
               FieldSetEntity fs = getTemplateFieldSet(FaceConst.CONTROL_SEQUENCE, sequence, groupUuid);
               fs.setValue(FaceConst.FIELD_PARENT_UUID, parentUuid);
               fieldSetList.add(fs);
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
         throw e;
      }
      return fieldSetList;
   }
    /**
     * 获取模板FieldSet
     *
     * @return
     */
    private FieldSetEntity getTemplateFieldSet(String propertyName, Object propertyValue, String groupUuid) {
        FieldSetEntity fse = new FieldSetEntity(FaceConst.TABLE_FACE_FIELD_CONTROL_PROPERTY);
        //设置操作类型为新增
        fse.setValue(CoreConst.SYSTEM_DATA_OPERATE_TYPE, CoreConst.SYSTEM_DATA_OPERATE_ADD);
        //手动生成uuid
        fse.setValue(CoreConst.UUID, IdUtil.randomUUID());
        fse.setValue("last_update_time", new Date());
        fse.setValue(FaceConst.FIELD_PROPERTY_NAME, propertyName);
        fse.setValue(FaceConst.FIELD_GROUP_UUID, groupUuid);
        int properType = 1;
        if (propertyValue instanceof JSONArray) {
            JSONArray array = (JSONArray) propertyValue;
            if (array == null || array.isEmpty()) {
                return null;
            }
            Object object = array.get(0);
            if (!(object instanceof JSONObject)) {
                //普通数组 转为字符串
                fse.setValue(FaceConst.FIELD_PROPERTY_VALUE, ArrayUtil.join(array.toArray(), ","));//有子级对象
                properType = 2;
            } else {
                //有子级对象
                properType = 4;
            }
        } else if (propertyValue instanceof JSONObject) {
            //属性值是对象
            JSONObject value = (JSONObject) propertyValue;
            if (value == null || value.isEmpty()) {
                return null;
            }
            properType = 3;
        } else {
            if (propertyValue == null || StringUtils.isEmpty(propertyValue)) {
                return null;
            }
   /**
    * 获取模板FieldSet
    *
    * @return
    */
   private FieldSetEntity getTemplateFieldSet(String propertyName, Object propertyValue, String groupUuid) {
      FieldSetEntity fse = new FieldSetEntity(FaceConst.TABLE_FACE_FIELD_CONTROL_PROPERTY);
      //设置操作类型为新增
      fse.setValue(CoreConst.SYSTEM_DATA_OPERATE_TYPE, CoreConst.SYSTEM_DATA_OPERATE_ADD);
      //手动生成uuid
      fse.setValue(CoreConst.UUID, IdUtil.randomUUID());
      fse.setValue("last_update_time", new Date());
      fse.setValue(FaceConst.FIELD_PROPERTY_NAME, propertyName);
      fse.setValue(FaceConst.FIELD_GROUP_UUID, groupUuid);
      int properType = 1;
      if (propertyValue instanceof JSONArray) {
         JSONArray array = (JSONArray) propertyValue;
         if (array == null || array.isEmpty()) {
            return null;
         }
         Object object = array.get(0);
         if (!(object instanceof JSONObject)) {
            //普通数组 转为字符串
            fse.setValue(FaceConst.FIELD_PROPERTY_VALUE, ArrayUtil.join(array.toArray(), ","));//有子级对象
            properType = 2;
         } else {
            //有子级对象
            properType = 4;
         }
      } else if (propertyValue instanceof JSONObject) {
         //属性值是对象
         JSONObject value = (JSONObject) propertyValue;
         if (value == null || value.isEmpty()) {
            return null;
         }
         properType = 3;
      } else {
         if (propertyValue == null || StringUtils.isEmpty(propertyValue)) {
            return null;
         }
            if (propertyValue instanceof Boolean) {
                properType = 5;
            } else if (propertyValue instanceof Integer) {
                properType = 6;
            } else if (propertyValue instanceof Double) {
                properType = 7;
            }
            fse.setValue(FaceConst.FIELD_PROPERTY_VALUE, propertyValue);
        }
        fse.setValue(FaceConst.FIELD_PROPERTY_TYPE, properType);
        return fse;
    }
         if (propertyValue instanceof Boolean) {
            properType = 5;
         } else if (propertyValue instanceof Integer) {
            properType = 6;
         } else if (propertyValue instanceof Double) {
            properType = 7;
         }
         fse.setValue(FaceConst.FIELD_PROPERTY_VALUE, propertyValue);
      }
      fse.setValue(FaceConst.FIELD_PROPERTY_TYPE, properType);
      return fse;
   }
    /**
     * 删除表单设计
     *
     * @param fse
     * @throws BaseException
     */
    @Override
    @Transactional
    public void deleteFace(FieldSetEntity fse) throws BaseException {
        publicService.delete(fse);
    }
   /**
    * 删除表单设计
    *
    * @param fse
    * @throws BaseException
    */
   @Override
   @Transactional
   public void deleteFace(FieldSetEntity fse) throws BaseException {
      publicService.delete(fse);
   }
}