许鹏程
2023-06-01 48989d38c871957d33c58c9ac7f162bad7d9fb97
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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.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.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<JSONObject> result = FaceUtil.getFaceControlList(fse,fieldPropertyDt);
        Map<String, Object> other = new HashMap<>();
        other.put("drawingList", result);
        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);
//        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;
    }
 
    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;
    }
 
    /**
     * 获取模板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);
    }
 
 
}