xinyb
6 天以前 aad276da26b3b44b7622343fa0bf15583e803585
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
package com.yc.crm.mail.action;
 
import com.yc.crm.mail.entity.FolderEntity;
import com.yc.crm.mail.entity.MailModuleBelowEntity;
import com.yc.crm.mail.entity.T482107Entity;
import com.yc.crm.mail.service.MailFolderIfc;
import com.yc.crm.mail.util.AllBackMsg;
import com.yc.entity.DataSourceEntity;
import com.yc.multiData.MultiDataSource;
import com.yc.multiData.SpObserver;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @BelongsProject: eCoWorksV3
 * @BelongsPackage: com.yc.crm.mail.action
 * @author: xinyb
 * @CreateTime: 2024-08-23  09:23
 * @Description:邮件文件夹
 */
@CrossOrigin
@RestController
@RequestMapping("/crm/mail/folder")
public class MailFolder {
 
    /**
     * 要获取rowId直接调用接口 http://yingchen.onbus.cn:9010/getRowid.do
     */
    @Autowired
    MailFolderIfc mailFolderIfc;
 
    /**
     * 添加邮件文件夹
     *
     * @param folder
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/addFolder.do")
    public AllBackMsg addFolder(@RequestBody T482107Entity folder, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
 
            HttpSession session = request.getSession();
            String companyId = (String) session.getAttribute(SessionKey.COMPANY_ID);
            String companyName = (String) session.getAttribute(SessionKey.COMPANY_NAME);
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            String userName = (String) session.getAttribute(SessionKey.USER_NAME);
            folder.setCompanyId(companyId);
            folder.setCompanyName(companyName);
            folder.setUserCode(userCode);
            folder.setUserName(userName);
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            String rowId = mailFolderIfc.saveFolder(folder);
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isNotBlank(rowId)) {
                map.put("cod", 0);
                map.put("rowId", rowId);
                msg.setSuccess("已保存", map);
            } else {
                map.put("cod", -1);
                map.put("msg", "保存后返回rowId出错");
                msg.setSuccess("已保存", map);
            }
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
 
    /**
     * 修改邮件文件夹
     *
     * @param folder
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/updateFolder.do")
    public AllBackMsg updateFolder(@RequestBody T482107Entity folder, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            if (folder.getFolderId() == null || folder.getFolderId().equals(0)) {
                msg.setFail("缺少文本夹ID值,请检查");
                return msg;
            }
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            folder.setUserCode(userCode);
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            mailFolderIfc.updateFolder(folder);
            msg.setOk("已更新");
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 删除邮件文件夹
     *
     * @param account
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/deleteFolder.do")
    public AllBackMsg deleteFolder(@RequestParam(defaultValue = "0") Integer folderId, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            if (folderId.equals(0)) {
                msg.setFail("邮件文件夹编号ID获取不到");
                return msg;
            }
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            Integer cont = mailFolderIfc.deleteFolder(userCode, folderId);
            if (cont > 0) {
                msg.setOk("已删除");
            }
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * @param search   搜索
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/getFolder.do")
    public AllBackMsg getFolder(HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            List<T482107Entity> t482107Entities = mailFolderIfc.getMailFolder(userCode);
            if (t482107Entities.size() > 0) {
                //返回树结构
                msg.setSuccess("执行成功", getFolderList(t482107Entities, ""));
            }
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 文件夹树结构
     *
     * @param t482107
     * @param rowId
     * @return
     */
    public static List<FolderEntity> getFolderList(List<T482107Entity> t482107, String rowId) {
        List<FolderEntity> folder = new ArrayList<>();
        try {
            for (T482107Entity t : t482107) {
                if (folder.stream().filter(f -> f.getParentRowId().equals(rowId)).count() == 0) {
                    List<T482107Entity> fo = t482107.stream().filter(o -> o.getParentRowId().equals(rowId)).collect(Collectors.toList());
                    if (fo.size() > 0) {
                        for (T482107Entity y : fo) {
                            FolderEntity folderEntity = new FolderEntity();
                            folderEntity.setFolderId(y.getFolderId());
                            folderEntity.setFolderName(y.getFolderName());
                            folderEntity.setRowId(y.getRowId());
                            folderEntity.setParentRowId(y.getParentRowId());
                            folderEntity.setTreeControl(y.getTreeControl());
                            if (!y.getRowId().equals(y.getParentRowId())) {
                                folderEntity.setList(getFolderList(t482107, y.getRowId()));
                            }
                            folder.add(folderEntity);
                        }
                    }
                }
            }
            return folder;
        } catch (Exception e) {
            throw e;
        }
    }
 
    /**
     * 文件夹树结构
     *
     * @param t482107
     * @param rowId
     * @return
     */
    public static List<MailModuleBelowEntity> getFolderModule(List<T482107Entity> t482107, String rowId) {
        List<MailModuleBelowEntity> folder = new ArrayList<>();
        try {
            for (T482107Entity t : t482107) {
                if (folder.stream().filter(f -> f.getParentKey().equals(rowId)).count() == 0) {
                    List<T482107Entity> fo = t482107.stream().filter(o -> o.getParentRowId().equals(rowId)).collect(Collectors.toList());
                    if (fo.size() > 0) {
                        for (T482107Entity y : fo) {
                            MailModuleBelowEntity folder1 = new MailModuleBelowEntity();
                            folder1.setId(y.getFolderId());
                            folder1.setType("folder");
                            folder1.setKey(y.getRowId());
                            folder1.setParentKey(y.getParentRowId());
                            folder1.setName(y.getFolderName());
                            if (!y.getRowId().equals(y.getParentRowId())) {
                                folder1.setList(getFolderModule(t482107, y.getRowId()));
                            }
                            folder.add(folder1);
                        }
                    } else {
                        break;
                    }
                }
            }
            return folder;
        } catch (Exception e) {
            throw e;
        }
    }
}