shichongfu
2023-04-25 ce0b49552668d3331055e2b1a1447a743dc54939
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
package com.product.org.admin.controller;
 
import java.util.Date;
 
import javax.servlet.http.HttpServletRequest;
 
import com.product.common.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import com.product.org.admin.config.CmnConst;
import com.product.org.admin.config.SystemCode;
import com.product.org.admin.service.CompanyService;
import com.product.util.BaseUtil;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.config.CoreConst;
import com.product.core.controller.support.AbstractBaseController;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.entity.RequestParameterEntity;
import com.product.core.exception.BaseException;
import com.product.core.permission.PermissionService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.module.sys.config.SystemErrorCode;
import com.product.module.sys.entity.SystemUser;
import com.product.module.sys.version.ApiVersion;
 
/**
 * Copyright LX-BASE
 *
 * @Title: CompanyController
 * @Project: LX-BASE-SERVER
 * @Date: 2020年5月29日 上午11:00:39
 * @Author: 郑盟
 * @Description:单位基本信息配置,对应单位表
 */
@RequestMapping("/api/company")
@RestController
public class CompanyController extends AbstractBaseController {
    @Autowired
    public CompanyService companyService;
 
    @Autowired
    PermissionService permissionService;
 
    @Autowired
    SystemOrgLevelsController systemOrgLevelsController;
 
    /**
     * 根据公司UUID查询单位基本信息详情 @param request @return String @throws
     */
    @RequestMapping(value = "/find-company/{varsion}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String findCompany(HttpServletRequest request) {
        try {
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            RequestParameterEntity reqp = (RequestParameterEntity) bean;
            FieldSetEntity fse = reqp.getFormData();
            String org_level_uuid = fse.getString(CmnConst.ORG_LEVEL_UUID);
            if ("".equals(org_level_uuid) || null == org_level_uuid) {
                return this.error(SystemCode.SYSTEM_COMPANY_FIND_FAIL.getValue(),
                        SystemCode.SYSTEM_COMPANY_FIND_FAIL.getText());
            }
            if (fse != null) {
                FieldSetEntity fs = new FieldSetEntity();
                fs.setTableName(CmnConst.PRODUCT_SYS_ORG_LEVELS);
                fs.setValue("uuid", fse.getString(CmnConst.ORG_LEVEL_UUID));
                systemOrgLevelsController.orgLevelOperPermission(true, 0, fs);
            }
            FieldSetEntity fs = companyService.findCompany(fse);
            return OK_List(fs);
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return error(SystemCode.SYSTEM_COMPANY_FIND_FAIL.getValue(),
                    SystemCode.SYSTEM_COMPANY_FIND_FAIL.getText() + e.getMessage());
        }
    }
 
    /**
     * 获取公司全称
     */
    @RequestMapping(value = "/find-level-all/{varsion}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String findLevelAll(HttpServletRequest request) {
        try {
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            RequestParameterEntity reqp = (RequestParameterEntity) bean;
            FieldSetEntity fse = reqp.getFormData();
            String uuid = fse.getString("uuid");
            if ("".equals(uuid) || null == uuid) {
                return this.error(SystemCode.SYSTEM_FIND_LEVEL_ARR_FAIL.getValue(),
                        SystemCode.SYSTEM_FIND_LEVEL_ARR_FAIL.getText());
            }
            if (fse != null) {
                FieldSetEntity fs = new FieldSetEntity();
                fs.setTableName(CmnConst.PRODUCT_SYS_ORG_LEVELS);
                fs.setValue("uuid", fse.getString(CmnConst.ORG_LEVEL_UUID));
                systemOrgLevelsController.orgLevelOperPermission(true, 0, fs);
            }
            FieldSetEntity fs = companyService.findLevelAll(fse);
            return OK_List(fs);
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return error(SystemCode.SYSTEM_FIND_LEVEL_ARR_FAIL.getValue(),
                    SystemCode.SYSTEM_FIND_LEVEL_ARR_FAIL.getText() + e.getMessage());
        }
    }
 
    /**
     * 新增或修改单位基本信息详情 @param request @return String @throws
     */
    @RequestMapping(value = "/add-update-company/{varsion}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String addCompany(HttpServletRequest request) {
        try {
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            RequestParameterEntity reqp = (RequestParameterEntity) bean;
            FieldSetEntity fse = reqp.getFormData();
            if (!StringUtils.isEmpty(fse.getString("uuid"))) {
                //数据操作的权限验证
                if (!permissionService.validDataPermission(fse, CoreConst.DATA_PERMISSION_VALID_TYPE_ORG)) {
                    SpringMVCContextHolder.getSystemLogger().error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
                    return this.error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
                }
                BaseUtil.updatedRegeneratorAndUpdateTime(SpringMVCContextHolder.getCurrentUser(), fse);
                boolean updateEmail = companyService.updateCompany(fse);
                if (updateEmail) {
                    return OK_Add(fse.getString("uuid"));
                } else {
                    return error(SystemCode.SYSTEM_COMPANY_UPDATE_FAIL.getValue(),
                            SystemCode.SYSTEM_COMPANY_UPDATE_FAIL.getText());
                }
            } else {
                fse.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
                fse.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
                return OK_Add(companyService.add(fse));
            }
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return error(SystemCode.SYSTEM_COMPANY_UPDATE_FAIL.getValue(),
                    SystemCode.SYSTEM_COMPANY_UPDATE_FAIL.getText() + e.getMessage());
        }
    }
 
    /**
     * 根据公司UUID删除单位基本信息
     *
     * @return
     */
    @RequestMapping(value = "/delete-company/{varsion}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String deleteCompany(HttpServletRequest request) {
        try {
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            RequestParameterEntity reqp = (RequestParameterEntity) bean;
            FieldSetEntity fse = reqp.getFormData();
            String org_level_uuid = fse.getString(CmnConst.ORG_LEVEL_UUID);
            if (fse != null) {
                FieldSetEntity fs = new FieldSetEntity();
                fs.setTableName(CmnConst.PRODUCT_SYS_ORG_LEVELS);
                fs.setValue("uuid", fse.getString(CmnConst.ORG_LEVEL_UUID));
                systemOrgLevelsController.orgLevelOperPermission(false, 0, fs);
            }
            if ("".equals(org_level_uuid) || null == org_level_uuid) {
                return this.error(SystemCode.SYSTEM_COMPANY_DELETE_FAIL.getValue(),
                        SystemCode.SYSTEM_COMPANY_DELETE_FAIL.getText());
            }
            return companyService.deleteCompanyList(fse) ? OK()
                    : error(SystemCode.SYSTEM_COMPANY_DELETE_FAIL.getValue(),
                    SystemCode.SYSTEM_COMPANY_DELETE_FAIL.getText());
        } catch (BaseException e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return error(SystemCode.SYSTEM_COMPANY_DELETE_FAIL.getValue(),
                    SystemCode.SYSTEM_COMPANY_DELETE_FAIL.getText() + e.getMessage());
        }
    }
 
    /**
     * 切换单位基
     *
     * @return
     */
    @RequestMapping(value = "/switch-company/{varsion}", method = RequestMethod.POST)
    @ApiVersion(1)
    /* deprecated */
    public String switchCompany(HttpServletRequest request) {
        try {
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            RequestParameterEntity reqp = (RequestParameterEntity) bean;
            FieldSetEntity fse = reqp.getFormData();
            String org_level_uuid = fse.getString(CmnConst.ORG_LEVEL_UUID);
            if ("".equals(org_level_uuid) || null == org_level_uuid) {
                return this.error(SystemCode.SYSTEM_SWITCH_COMPANY_FAIL.getValue(),
                        SystemCode.SYSTEM_SWITCH_COMPANY_FAIL.getText());
            }
            SystemUser u = SpringMVCContextHolder.getCurrentUser();
            boolean changeCompany = u.changeCompany(org_level_uuid);
            if (changeCompany) {
                // token与用户关联,放入缓存中
                DataPoolCacheImpl.getInstance().setSessionUser(u);
            }
            return changeCompany ? OK()
                    : error(SystemCode.SYSTEM_SWITCH_COMPANY_FAIL.getValue(),
                    SystemCode.SYSTEM_SWITCH_COMPANY_FAIL.getText());
        } catch (BaseException e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return error(SystemCode.SYSTEM_SWITCH_COMPANY_FAIL.getValue(),
                    SystemCode.SYSTEM_SWITCH_COMPANY_FAIL.getText() + e.getMessage());
        }
    }
 
    /**
     * 当前人员关联公司
     *
     * @return
     */
    @RequestMapping(value = "/relation-company/{varsion}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String relationCompany(HttpServletRequest request) {
        try {
            SystemUser user = SpringMVCContextHolder.getCurrentUser();
            StringBuilder filter = new StringBuilder();
            int user_type = user.getUserType();
            if (user_type == SystemUser.USER_TYPE_ADMIN) {// 超级管理员
                FieldSetEntity f = new FieldSetEntity();
                f.setTableName(CmnConst.PRODUCT_SYS_MANAGER);
                return this.OK_List(f);
            } else if (user_type == SystemUser.USER_TYPE_COM_ADMIN || user_type == SystemUser.USER_TYPE_HIDE_ADMIN) {// 隐藏管理员
                filter.append("uuid in('");
                filter.append(user.getCurrentManager().getString(CmnConst.ORG_LEVEL_UUID).replace(",", "','"));
                filter.append("') and org_level_type=0");
            } else if (user_type == SystemUser.USER_TYPE_SYS_ADMIN) {// 系统管理员
                filter.append("client_uuid in('");
                filter.append(user.getCurrentManager().getString("clients_uuid").replace(",", "','"));
                filter.append("') and org_level_type=0");
            } else {// 普通人员
                return this.OK_List(user.getCurrentCompany());
            }
            DataTableEntity dt = companyService.relationCompany(filter.toString());
            return this.OK_List(dt);
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return error(SystemCode.SYSTEM_FIND_STAFFS_COMPANY_FAIL.getValue(),
                    SystemCode.SYSTEM_FIND_STAFFS_COMPANY_FAIL.getText() + e.getMessage());
        }
    }
}