package com.product.face.service; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.IdUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.product.admin.service.PublicService; import com.product.common.lang.StringUtils; import com.product.core.cache.DataPoolCacheImpl; import com.product.core.config.CoreConst; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.exception.BaseException; import com.product.core.service.support.AbstractBaseService; import com.product.core.transfer.Transactional; 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.poi.hdgf.pointers.Pointer; import org.apache.poi.ss.formula.functions.T; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.*; /** * @Author cheng * @Date 2023/4/21 17:46 * @Desc 表单设计Service */ @Service public class FaceDesignService extends AbstractBaseService implements IFaceDesignService { @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)) { throw new BaseException(ErrorCode.GET_FACE_CONFIG_FAIL); } List result = FaceUtil.getFaceControlList(fse, fieldPropertyDt); Map 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); } public class Pointer { int data = 0; public int getData() { return ++data; } public void setData(int data) { this.data = data; } } /** * 保存表单内容数据 * * @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 fieldSetEntityList = new ArrayList<>(); //声明一个带指针的int变量 Pointer pointer = new Pointer(); 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, pointer)); } 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()}); } if (StringUtils.isEmpty(fse.getString("terminal_type"))) { fse.setValue("terminal_type", 1); } getBaseDao().saveFieldSetEntity(fse); fse.getSubData().clear(); return fse; } public List controlTraverse(Object o, String parentUuid, Integer sequence, Pointer pointer) { List 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, pointer)); } } catch (Exception e) { e.printStackTrace(); throw e; } } else { //json对象 JSONObject property = (JSONObject) o; String groupUuid = IdUtil.randomUUID(); for (Map.Entry 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, pointer)); 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, pointer.getData(), 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; } 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); } }