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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.product.admin.service;
 
import java.util.Date;
 
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.product.common.lang.DateUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldMetaEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.module.sys.service.UserService;
import com.product.common.lang.StringUtils;
import com.product.email.service.SendEmailService;
import com.product.admin.config.CmnConst;
import com.product.admin.service.idel.ISystemManagerSerivce;
 
@Component
public class SystemManagerService extends AbstractBaseService implements ISystemManagerSerivce {
    @Autowired
    UserService userService;
    @Autowired
    private SendEmailService sendEmailService;
 
    public UserService getUserService() {
        return userService;
    }
 
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
 
    @Autowired
    public BaseDao baseDao;
 
    public DataTableEntity getManagerList(String user_uuid) throws BaseException {
        String sql = "SELECT user_uuid,GROUP_CONCAT(`org_level_uuid` SEPARATOR ',') org_level_uuid, role_uuids FROM product_sys_org_manager ";
        if (!StringUtils.isEmpty(user_uuid)) {
            sql += " where org_level_uuid in (SELECT uuid FROM product_sys_org_levels` where org_level_code_parent in (select org_level_uuid FROM product_sys_org_manager where user_uuid='"
                    + user_uuid + "' and manager_type=2) )";
        } else {
            sql += " where manager_type=2";
        }
        sql += " GROUP BY user_uuid,role_uuids";
        return baseDao.listTable(sql, new Object[]{});
    }
 
    @Transactional
    public String addManager(FieldSetEntity fse) throws BaseException {
        FieldSetEntity userFs = new FieldSetEntity();
        userFs.setTableName(CmnConst.PRODUCT_SYS_USERS);
        userFs.setValue(CmnConst.STATUS, fse.getString(CmnConst.STATUS));
        userFs.setValue(CmnConst.USER_ACCOUNT, fse.getString(CmnConst.USER_ACCOUNT));
        userFs.setValue(CmnConst.USER_NAME, fse.getString(CmnConst.USER_NAME));
        userFs.setValue(CmnConst.USER_PWD, fse.getString(CmnConst.USER_PWD));
        userFs.setValue(CmnConst.IS_MANAGER, 1);// 是否是管理员 0:否 1:是
        String datetime = DateUtils.getDateTime();
        userFs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        userFs.setValue(CmnConst.UPDATED_UTC_DATETIME, datetime);
        FieldSetEntity sub = fse.getSubDataTable(CmnConst.PRODUCT_SYS_MANAGER).getData().get(0);
        String[] org_level_uuids = sub.getString("org_level_uuids").split(",");
        String user_uuid = baseDao.add(userFs);
        if (sub.getString("org_level_uuids") != null && org_level_uuids != null) {
            DataTableEntity newSubDataTable = new DataTableEntity();
            FieldMetaEntity f = new FieldMetaEntity();
            f.setTableName(new Object[]{CmnConst.PRODUCT_SYS_MANAGER});
            newSubDataTable.setMeta(f);
            FieldSetEntity newSubFs = null;
            for (int i = 0; i < org_level_uuids.length; i++) {
                newSubFs = new FieldSetEntity();
                newSubFs.setTableName(CmnConst.PRODUCT_SYS_MANAGER);
                newSubFs.setValue(CmnConst.MANAGER_TYPE, 2);// 管理员类型:1超级管理员 2.普通管理员
                newSubFs.setValue(CmnConst.USER_UUID, user_uuid);
                newSubFs.setValue(CmnConst.ORG_LEVEL_UUID, org_level_uuids[i]);// 管理员信息表拆分保存
                newSubFs.setValue(CmnConst.ROLE_UUIDS, "role_uuid_4");// 关联角色,默认普通管理员角色
                newSubFs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
                newSubFs.setValue(CmnConst.UPDATED_UTC_DATETIME, datetime);
                newSubDataTable.addFieldSetEntity(newSubFs);
            }
 
            baseDao.add(newSubDataTable);
        }
 
        return user_uuid;
    }
 
 
 
    /**
     * 创建隐藏管理员,一个客户只创建一个,关联顶级公司,并且该客户下面创建子公司时,也要关联此管理员
     *
     * @param client 客户,必须包含公司
     * @return
     * @throws BaseException
     */
    @Transactional
    public void addHideManager(FieldSetEntity client) throws BaseException {
        // 用户表
        FieldSetEntity userFs = new FieldSetEntity();
        userFs.setTableName(CmnConst.PRODUCT_SYS_USERS);
        userFs.setValue(CmnConst.STATUS, "1");
        // 生成账户名
        String account = client.getString("client_short_code") + "_hidden_" + (int) ((Math.random() * 9 + 1) * 10000);
        userFs.setValue(CmnConst.USER_ACCOUNT, account);// 使用客户短编码+hidden+5位 随机数字
        // 随机生成8位密码
        String passWord = RandomStringUtils.randomAlphanumeric(8);
        userFs.setValue(CmnConst.USER_NAME, account);
        userFs.setValue(CmnConst.USER_PWD, userService.createPassWord(account, passWord));
        userFs.setValue(CmnConst.IS_MANAGER, 1);// 是否是管理员 0:否 1:是
        String datetime = DateUtils.getDateTime();
        userFs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        userFs.setValue(CmnConst.UPDATED_UTC_DATETIME, datetime);
        String uuid = baseDao.add(userFs);
        FieldSetEntity uf = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_USERS, uuid, false);
        // 管理员表
        FieldSetEntity newSubFs = new FieldSetEntity();
        newSubFs.setTableName(CmnConst.PRODUCT_SYS_MANAGER);
        newSubFs.setValue(CmnConst.MANAGER_TYPE, 3);// 管理员类型:1超级管理员 2.普通管理员 3.隐藏管理员
        newSubFs.setValue(CmnConst.USER_ID, uf == null ? null : uf.getInteger(CmnConst.USER_ID));
        // 郑盟 2020年12月21日 下午14:05
        newSubFs.setValue(CmnConst.IS_USED, "1");
        newSubFs.setValue(CmnConst.ORG_LEVEL_UUID, client.getString(CmnConst.ORG_LEVEL_UUID));// 管理员关联公司
        newSubFs.setValue(CmnConst.ROLE_UUIDS, "a8e2a6ac-a507-47da-851b-9ac61541cd5a-notchange");// 关联角色,隐藏管理员角色
        newSubFs.setValue(CmnConst.CLIENTS_UUID, client.getString("uuid"));
        newSubFs.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        newSubFs.setValue(CmnConst.UPDATED_UTC_DATETIME, datetime);
        baseDao.add(newSubFs);
 
        // 新增隐藏管理员发送邮件
        // 邮件内容
        String user_id = client.getString(CmnConst.CREATED_BY);
        FieldSetEntity khfs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS, " user_id = ? ",
                new Object[]{user_id}, false);
        if (khfs != null) {
            String login_url = client.getString(CmnConst.LOGIN_URL);
            FieldSetEntity nfs = new FieldSetEntity();
            nfs.setTableName(CmnConst.STAFF_EMAIL_CONTENT);
            nfs.setValue(CmnConst.LOGIN_URL, login_url);// 登录页面
            nfs.setValue(CmnConst.USER_ACCOUNT, account);// 用户名
            nfs.setValue(CmnConst.PASS_WORD, passWord);
            nfs.setValue(CmnConst.USER_PRIMARY_EMAIL, khfs.getString(CmnConst.USER_PRIMARY_EMAIL));// 接受邮件邮箱地址
            // 发送邮件
            sendEmailService.parseMailTemplate(CmnConst.NEW_HIDDEN_ADMINISTRATOR_ACTIVATION, nfs);
        }
    }
 
    /**
     * 隐藏管理员绑定公司
     *
     * @param org_levels 公司
     */
    public void updateHideManagerBindingCompany(FieldSetEntity org_levels) throws BaseException {
        String client_uuid = org_levels.getString(CmnConst.CLIENT_UUID);
        FieldSetEntity manager = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER,
                "clients_uuid=? and manager_type=3", new Object[]{client_uuid}, false);
        if (manager != null) {
            String org_level_uuid = manager.getString(CmnConst.ORG_LEVEL_UUID);
            if (org_level_uuid == null || org_level_uuid.length() == 0) {
                org_level_uuid = org_levels.getString("uuid");
            } else if (!("," + org_level_uuid + ",").contains("," + org_levels.getString("uuid") + ",")) {
                org_level_uuid += "," + org_levels.getString("uuid");
            }
            manager.setValue(CmnConst.ORG_LEVEL_UUID, org_level_uuid);
            baseDao.update(manager);
        }
    }
 
    /**
     * 隐藏管理员解除绑定公司
     *
     * @param org_levels 公司
     */
    public void updateHideManagerClearCompany(FieldSetEntity org_levels) throws BaseException {
        String client_uuid = org_levels.getString(CmnConst.CLIENT_UUID);
        FieldSetEntity manager = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER,
                "clients_uuid=? and manager_type=3", new Object[]{client_uuid}, false);
        if (manager != null) {
            String org_level_uuid = manager.getString(CmnConst.ORG_LEVEL_UUID);
            if (org_level_uuid != null && org_level_uuid.length() > 0
                    && ("," + org_level_uuid + ",").contains("," + org_levels.getString("uuid") + ",")) {
                org_level_uuid = ("," + org_level_uuid + ",").replaceAll("," + org_levels.getString("uuid") + ",", "");
                org_level_uuid = org_level_uuid.substring(1, org_level_uuid.length() - 1);
            }
            manager.setValue(CmnConst.ORG_LEVEL_UUID, org_level_uuid);
            baseDao.update(manager);
        }
    }
 
    public boolean updateManager(FieldSetEntity fse) throws BaseException {
        return baseDao.update(fse);
    }
 
    /**
     * 修改/重置密码
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    public boolean updateManagerPwd(FieldSetEntity fse) throws BaseException {
        FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_USERS, fse.getString(CmnConst.USER_UUID), false);
        String pwd = fse.getString(CmnConst.USER_PWD);
        fs.setValue(CmnConst.USER_PWD, pwd != null ? pwd : "");
        fs.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        fs.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
        return baseDao.update(fs);
    }
 
    /**
     * 删除管理员
     */
    @Transactional
    public boolean deleteManager(FieldSetEntity fse) throws BaseException {
        String user_uuid = fse.getString(CmnConst.USER_UUID);
        boolean flag = false;
        if (!StringUtils.isEmpty(user_uuid)) {
            flag = baseDao.delete(CmnConst.PRODUCT_SYS_MANAGER, "user_uuid=?", new String[]{user_uuid});
            if (!flag)
                return flag;
            flag = baseDao.delete(CmnConst.PRODUCT_SYS_USERS, "uuid=?", new String[]{user_uuid});
        }
        return flag;
    }
 
    /**
     * 修改管理员状态
     */
    public boolean updateManagerStatus(FieldSetEntity fse) throws BaseException {
        FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_USERS, fse.getString(CmnConst.USER_UUID), false);
        fs.setValue(CmnConst.STATUS, fse.getString(CmnConst.STATUS));// 状态 0:禁用 1:正常
        fs.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        fs.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
        return baseDao.update(fs);
    }
 
    /**
     * 根据用户uuid 查询管理员
     *
     * @param user_uuid
     * @return
     * @throws BaseException
     */
    public FieldSetEntity findManagerTypeByUserUuid(String user_uuid) throws BaseException {
        return baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER, "user_uuid=?", new String[]{user_uuid},
                false);
    }
 
    /**
     * 公司uuid 查询管理员
     *
     * @param org_level_uuid
     * @return
     * @throws BaseException
     */
    public FieldSetEntity findManagerType(String user_uuid, String org_level_uuid) throws BaseException {
        String filter = "  1=1";
        Object[] objects = new Object[]{};
        // 郑盟 2020年12月17日 下午16:05
        if (!StringUtils.isEmpty(user_uuid)) {
            filter += " and  user_uuid=?";
            objects = new Object[]{user_uuid};
        }
        // 郑盟 2020年12月17日 下午16:05
        if (!StringUtils.isEmpty(org_level_uuid)) {
            filter += " and  org_level_uuid=?";
            if (objects.length == 1) {
                objects = new Object[]{user_uuid, org_level_uuid};
            } else {
                objects = new Object[]{org_level_uuid};
            }
        }
 
        return baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER, filter, objects, false);
    }
 
    // 随机字母
    public static String getRandStr(int num) {
        String strs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        StringBuilder buff = new StringBuilder();
        for (int i = 1; i <= num; i++) {
            char str = strs.charAt((int) (Math.random() * 52));
            buff.append(str);
        }
        return buff.toString();
    }
 
}