fs-danaus
2024-04-18 b74110a969188df8fc5c67c33ac69f5936a24986
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package com.yc.action.customControl;
 
import com.yc.entity.customControl.*;
import com.yc.multiData.SpObserver;
import com.yc.service.customControl.ControlLayoutIfc;
import com.yc.utils.SessionKey;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @BelongsProject: eCoWorksV3
 * @BelongsPackage: com.yc.action.controlLayout
 * @author: xinyb
 * @CreateTime: 2023-06-12  15:57
 * @Description:
 */
@CrossOrigin
@RestController
@RequestMapping("/custom")
@ResponseBody
public class ControlLayoutAction {
    @Autowired
    ControlLayoutIfc controlLayoutIfc;
 
    /**
     * 获取用户自定义的页面布局(自定义控件信息)
     *
     * @param customParam
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/controlLayout.do")
    public CustomBack getControlLayout(@RequestBody CustomParam param, HttpServletRequest request, HttpServletResponse response) {
        CustomBack back = new CustomBack();
        try {
            if (param.getFormId() == null) {
                back.fail("缺少功能号,无法自定义布局");
                return back;
            }
            if (param.getFormType() == null) {
                back.fail("缺少功能类型,无法自定义布局");
                return back;
            }
            String userAgent = request.getHeader("User-Agent");
            HttpSession session = request.getSession();
            if (StringUtils.isBlank(param.getUserCode())) {//没userCode就拿登录的会话userCode
                param.setUserCode((String) session.getAttribute(SessionKey.USERCODE));
            }
            String dbId = (String) session.getAttribute(SessionKey.DATA_BASE_ID);
            SpObserver.setDBtoInstance("_" + dbId);
            List<MenuEntity> menuList = controlLayoutIfc.getMenu(param.getFormId(), param.getFormType());
            if (menuList == null) {
                back.fail("获取不到9810设置的功能号信息");
                return back;
            }
            List<CustomJson> json = new ArrayList<>();
            for (MenuEntity menu : menuList) {//遍历功能号(多表需要遍历)
                CustomJson customJson = new CustomJson();
                customJson.setFormId(menu.getFormId());
                customJson.setFormName(menu.getFormName());
                customJson.setTabId(menu.getTabId());
                customJson.setMainFormId(menu.getMainFormId());
                getCustomJson(param, menu, customJson, json);
                json.add(customJson);
            }
 
            //最终返回的数据格式
            Map<String, List<CustomJson>> backJson = new HashMap<String, List<CustomJson>>();
            for (CustomJson j : json) {
                if (backJson.get(j.getTabId()) == null || backJson.get(j.getTabId()).size() == 0) {//
                    List<CustomJson> customJsons = json.stream().filter(c -> c.getTabId().equals(j.getTabId())).collect(Collectors.toList());
                    backJson.put(j.getTabId(), customJsons);
                }
            }
            back.success(backJson);
        } catch (Exception e) {
            back.fail(e.getCause() == null ? e.getMessage() : e.getCause().getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return back;
    }
 
    /**
     * 保存控件布局信息
     *
     * @param json
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/saveControlLayout.do")
    public CustomBack saveControlLayout(@RequestBody List<CustomAttribute> json, HttpServletRequest request, HttpServletResponse response) {
        CustomBack back = new CustomBack();
        try {
            if (json == null || json.size() == 0) {
                back.fail("获取不到页面需要保存的内容");
                return back;
            }
            HttpSession session = request.getSession();
            String userCode = "";
            Set<String> userCodes = json.stream().map(CustomAttribute::getUserCode).collect(Collectors.toSet());
            if (userCodes.size() > 1) {//前端出现多个userCode,这种情况不能进行保存操作
                back.fail("保存的布局出现多个用户账号设置,账号:" + String.join(";", userCodes));
                return back;
            }
            userCode = String.join(",", userCodes);
            if (StringUtils.isBlank(userCode)) {//前端没有userCode值传过来的时候拿当前登录的userCode值
                userCode = (String) session.getAttribute(SessionKey.USERCODE);
            }
            Set<Integer> formIds = json.stream().map(CustomAttribute::getFormId).collect(Collectors.toSet());
            if (formIds.size() == 0) {
                back.fail("布局的功能号不能为空,请检查");
                return back;
            }
            String dbId = (String) session.getAttribute(SessionKey.DATA_BASE_ID);
            SpObserver.setDBtoInstance("_" + dbId);
            Integer count = controlLayoutIfc.saveCustomLayout(json, formIds, userCode);
            if (count > 0) {
                back.saveOk("保存成功");
            }
            return back;
        } catch (Exception e) {
            back.fail(e.getCause() == null ? e.getMessage() : e.getCause().getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return back;
    }
 
    /**
     * 删除自定义控件布局
     *
     * @param formId
     * @param formType
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/deleteControlLayout.do")
    public CustomBack deleteControlLayout(@RequestBody List<CustomAttribute> json, Integer fromIdAage, Integer formType,
                                          HttpServletRequest request, HttpServletResponse response) {
        CustomBack back = new CustomBack();
        HttpSession session = request.getSession();
        try {
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                back.fail("获取不到当前用户名");
                return back;
            }
 
            String dbId = (String) session.getAttribute(SessionKey.DATA_BASE_ID);
            SpObserver.setDBtoInstance("_" + dbId);
            Set<Integer> formIds = json.stream().map(CustomAttribute::getFormId).collect(Collectors.toSet());
            Set<Integer> formTypes = json.stream().map(CustomAttribute::getFormType).collect(Collectors.toSet());
            int cont = controlLayoutIfc.deleteCustomLayout(userCode, formIds, formTypes);
            if (cont > 0) {
                File file = null;
                String url = "";
                url = request.getServletContext().getRealPath("/app/" + dbId + "/0/cnzh/" + fromIdAage + "/" + formType + "/");
                for (Integer formId : formIds) {
                    String[] fileNames={
                            userCode + "_panel.jsp",
                            userCode +"_panelControl"+formId+".jsp",
                            userCode + "_grid.jsp",
                            formId + "_" + userCode + "_panel.jsp",
                            formId + "_" + userCode  +"_panelControl"+formId+".jsp",
                            formId + "_" + userCode + "_grid.jsp"
                    };
                    for (String fileName : fileNames) {
                        file = new File(url +fileName);
                        if (file.exists()) {
                            file.delete();
                        }
                    }
                }
                back.success("已初始化");
                return back;
            }
        } catch (Exception e) {
            back.fail(e.getCause() == null ? e.getMessage() : e.getCause().getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return back;
    }
 
    /**
     * 获取每个功能号在9802设置的参数(行 列 高 宽等)
     *
     * @param param
     * @param menu
     * @param json
     * @return
     */
    private void getCustomJson(CustomParam param, MenuEntity menu, CustomJson customJson, List<CustomJson> json) throws Exception {
        try {
            List<CustomAttribute> customAttributes = controlLayoutIfc.getCustomAttributes(param.getUserCode(), menu.getFormId(), menu.getFormType());//取本身的formtype
            if (customAttributes == null) {
                throw new Exception("获取不到功能号:" + menu.getFormId() + "的参数设置,返回内容:null");
            }
            int PanelHeadFlag=0;
            int GridHeadFlag=1;
            if(menu.getFormType()==1||menu.getFormType()==3||menu.getFormType()==7||menu.getFormType()==18||menu.getFormType()==19){
                //面板,表格的主从表和其他类型相反
                PanelHeadFlag=1;
                GridHeadFlag=0;
            }
            final  int finalPanelHeadFlag=PanelHeadFlag;
            final  int finalGridHeadFlag=GridHeadFlag;
 
            //获取是主表的全部参数设置 headFlag=0
            List<CustomAttribute> masterAll = customAttributes.stream().filter(c -> c.getHeadFlag() == finalPanelHeadFlag).collect(Collectors.toList());
            //获取是主表的参数设置(表格之前) headFlag=0 && masterFieldShowLocation==0
            List<CustomAttribute> master = masterAll.stream().filter(c -> c.getMasterFieldShowLocation() == 0).collect(Collectors.toList());
            customJson.getMaster().setList(master);
            //获取是从表的参数设置 headFlag=1
            List<CustomAttribute> slave = customAttributes.stream().filter(c -> c.getHeadFlag() == finalGridHeadFlag).collect(Collectors.toList());
            customJson.getSlave().setList(slave);
            //获取是主表的参数设置(表格之前) headFlag=0 && masterFieldShowLocation==1
            List<CustomAttribute> master1 = masterAll.stream().filter(c -> c.getMasterFieldShowLocation() == 1).collect(Collectors.toList());
            customJson.getMaster1().setList(master1);
            String where = "";
            //获取当前页面单据查询条件信息(主功能号和功能号相同)
            if (menu.getMainFormId().equals(menu.getFormId())) {
                if (StringUtils.isNotBlank(param.getDocCode())) {
                    where = "docCode='" + param.getDocCode() + "'";
                }
                if (StringUtils.isBlank(where) && StringUtils.isNotBlank(param.getQueryString())) {
                    String[] params = param.getQueryString().split(";");
                    for (int p = 0; p < params.length; p++) {
                        where += params[p];
                        if (p + 1 < params.length) {
                            where += " and ";
                        }
                    }
                }
            } else {
                //主功能号和功能号不相同说明是多表存在子功能号,where条件就要根据设置的参数进行组装
                List<CustomJson> jsonData = json.stream().filter(j -> j.getFormId().equals(menu.getMainFormId())).collect(Collectors.toList());
                if (jsonData.size() == 1) {//主功能号有数据
                    Map<String, Object> mapData = (Map<String, Object>) jsonData.get(0).getMaster().getData();//获取到主功能单据值(字段都是小写)
                    if (mapData != null && !mapData.isEmpty()) {
                        String[] fks = menu.getFk().split(";");
                        String[] seekGroupIds = menu.getSeekGroupId().split(";");
                        if (fks.length > 0 && fks.length == seekGroupIds.length) {//设置的参数相等时候才组装where
                            for (int i = 0; i < fks.length; i++) {
                                String value = mapData.get(fks[i].toLowerCase()) != null ? (String) mapData.get(fks[i].toLowerCase()) : "";//DBHelper.getValue(mapData, fks[i]);
                                where += seekGroupIds[i] + "='" + value + "'";
                                if (i + 1 < fks.length) {
                                    where += " and ";
                                }
                            }
                        }
                    }
                }
            }
            if (StringUtils.isBlank(where)) {
                where = "docCode=''";
            }
            //获取单据数据信息 主表
            if (masterAll.size() > 0 && StringUtils.isNotBlank(menu.getHdTable())) {
                //获取到要查询data数据的字段ID(主表)
                List<String> fieldId = masterAll.stream().map(CustomAttribute::getFieldId).collect(Collectors.toList());
                String fieldIds = String.join(",", fieldId);
                if (StringUtils.isBlank(fieldIds)) {
                    fieldIds = "*";//查询全部
                }
                if (menu.getFormType() == 1) {//1类型窗体处理
                    fieldIds = "top 1 " + fieldIds;
                }
                Map<String, Object> map = controlLayoutIfc.getFormData(fieldIds, menu.getHdTable(), where);
                customJson.getMaster().setData(map);//填充数据值到data(表格之前)
                customJson.getMaster1().setData(map);//填充数据值到data(表格之后)
            }
            //获取单据数据信息 从表
            if (slave.size() > 0 && StringUtils.isNotBlank(menu.getDtTable())) {
                //获取到要查询data数据的字段ID(从表)
                List<String> fieldId = slave.stream().map(CustomAttribute::getFieldId).collect(Collectors.toList());
                String fieldIds = String.join(",", fieldId);
                if (StringUtils.isBlank(fieldIds)) {
                    fieldIds = "*";//查询全部
                }
                Map<String, Object> map = controlLayoutIfc.getFormData("top 1 " + fieldIds, menu.getDtTable(), where);
                customJson.getSlave().setData(map);
            }
            if (";9;15;17;499;497;".contains(";" + param.getFormType() + ";") || menu.getFormType() == 1) {//这些是清单列表或者是多表的 1类型 设置成表格显示
                customJson.getMaster().setGrid(true);
                customJson.getMaster1().setList(new ArrayList<>());//是列表清单面板不需要显示 xin 2024-3-28 10:06:24
                customJson.getMaster1().setData(null);
            }
            if(menu.getFormType()!=20) {
                //20的从表也是面板
                customJson.getSlave().setGrid(true);//从表就直接是表格显示
            }
            //下面是针对18类型窗体的特殊处理
            if (param.getFormType() == 18) {
 
            }
        } catch (Exception e) {
            throw e;
        }
    }
}