shichongfu
2023-05-24 ad4a2b67db73e07992aee0b51493495c63d10bc4
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package com.product.admin.service;
 
import com.product.admin.config.CmnConst;
import com.product.admin.config.SystemCode;
import com.product.admin.service.idel.IPersonalCenterService;
import com.product.common.lang.StringUtils;
import com.product.email.service.SendEmailService;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.config.Global;
import com.product.core.dao.BaseDao;
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.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.module.sys.entity.SystemUser;
import com.product.module.sys.service.UserService;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
 
 
/**
 * Copyright  LX-BASE
 *
 * @Title: PersonalCenterService
 * @Project: LX-BASE-SERVER
 * @Date: 2020-05-30 17:28
 * @Author: ZhouJie
 * @Description: 个人中心
 */
@Component
public class PersonalCenterService extends AbstractBaseService implements IPersonalCenterService {
 
    @Autowired
    public BaseDao baseDao;
 
    @Autowired
    UserService userService;
    @Autowired
    SendEmailService sendEmailService;
 
    @Autowired
    UpdateLoginUserInfoService updateLoginUserInfoService;
 
    /**
     * 个人中心修改用户头像
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    public Boolean updateUserAvatar(FieldSetEntity fse) throws BaseException {
        FieldSetEntity fs = new FieldSetEntity();
        fs.setTableName(CmnConst.PRODUCT_SYS_USERS);
        fs.setValue(CmnConst.UUID, fse.getUUID());
        fs.setValue(CmnConst.THUMBNAIL_IMG, fse.getString(CmnConst.THUMBNAIL_IMG));
        baseDao.saveFieldSetEntity(fs);
        updateLoginUserInfoService.updateUserInfoByUserId(SpringMVCContextHolder.getCurrentUser().getUser_id() + "", 1, false);
        return true;
    }
 
    /**
     * 个人中心修改用户信息
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Transactional
    @Override
    public Boolean updatePersonalInfo(FieldSetEntity fse) throws BaseException {
        SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
        FieldSetEntity fs = new FieldSetEntity();
        fs.setTableName(CmnConst.PRODUCT_SYS_USERS);
        fs.setValue(CmnConst.USER_NAME, fse.getString(CmnConst.USER_NAME));
        fs.setValue(CmnConst.UUID, currentUser.getUuid());
        fs.setValue(CmnConst.USER_PHONE_NUMBER, fse.getString(CmnConst.USER_PHONE_NUMBER));
        fs.setValue(CmnConst.GENDER, fse.getString("sex"));
        fs.setValue(CmnConst.USER_PRIMARY_EMAIL, fse.getString(CmnConst.USER_PRIMARY_EMAIL));
        baseDao.saveFieldSetEntity(fs);
        baseDao.executeUpdate("UPDATE product_sys_staffs SET sex=?,show_name=?,staff_email=? WHERE user_id=?",
                new Object[]{fse.getString("sex"),
                        fse.getString(CmnConst.USER_NAME),
                        fse.getString(CmnConst.USER_PRIMARY_EMAIL), currentUser.getUser_id()});
        updateLoginUserInfoService.updateUserInfoByUserId(currentUser.getUser_id() + "", 1, false);
        return true;
    }
 
    public FieldSetEntity personalCenterInfo() throws BaseException {
        StringBuilder sql = new StringBuilder();
        SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
        if (!currentUser.isManager()) {
            sql.append("\n SELECT b.gender, b.user_phone_number,b.user_primary_email,b.user_name,a.sex,b.user_id,b.user_signature,b.thumbnail_img,c.job_post_name,d.org_level_name dept_name,d.uuid dept_uuid,d.org_level_uuid,d.org_level_all ,e.org_level_name company_name FROM  product_sys_staffs a  ");
            sql.append("\n join product_sys_users b on a.user_id=b.user_id and a.user_id=? ");
            sql.append("\n join product_sys_job_posts c on a.job_post_uuid=c.uuid ");
            sql.append("\n join product_sys_org_levels d on d.uuid=a.dept_uuid ");
            sql.append("\n join product_sys_org_levels e on e.uuid=a.org_level_uuid  ");
        } else {
            sql.append(" SELECT ");
            sql.append(" thumbnail_img,a.uuid,b.uuid uuid1,thumbnail_img,user_account,user_name, ");
            sql.append(" user_phone_number, ");
            sql.append(" user_primary_email, ");
            sql.append(" org_level_uuid, ");
            sql.append(" role_uuids, ");
            sql.append(" a.created_utc_datetime, ");
            sql.append(" gender ");
            sql.append(" FROM ");
            sql.append(" product_sys_users a ");
            sql.append(" JOIN product_sys_org_manager b ON a.user_id = b.user_id ");
            sql.append(" WHERE ");
            sql.append(" a.user_id =? ");
        }
        FieldSetEntity fs = getBaseDao().getFieldSetEntityBySQL(sql.toString(), new Object[]{SpringMVCContextHolder.getCurrentUserId()}, false);
        DataTableEntity dt = new DataTableEntity();
        dt.addFieldSetEntity(fs);
        baseDao.loadPromptData(dt);
        if (currentUser.isManager()) {
            String role_uuds = fs.getString("role_uuids");
            if (!StringUtils.isEmpty(role_uuds)) {
                sql = new StringBuilder();
                sql.append(" select group_concat(role_name) role_name FROM product_sys_role where ");
                sql.append(BaseUtil.buildQuestionMarkFilter("uuid", role_uuds.split(",").length, true));
                FieldSetEntity role = baseDao.getFieldSetEntityBySQL(sql.toString(), role_uuds.split(","), false);
                if (role != null) {
                    fs.setValue("role_uuids", role.getString("role_name"));
                }
            }
        }
        return fs;
    }
 
    /**
     * @throws BaseException
     * @Date: 2020-05-30 14:04
     * @Author: ZhouJie
     * @Description: 根据用户uuid 查询用户信息
     */
    public FieldSetEntity findUserInfoByUserUuid(String uuid) throws BaseException {
        return baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_USERS, uuid, true);
    }
 
    /**
     * @throws Exception
     * @Date: 2020-05-30 14:04
     * @Author: ZhouJie
     * @Description: 发送修改密码邮件
     */
    @Async
    public boolean sendPasswordResetEmail(FieldSetEntity fs) {
        String sendPwdRsetEmail = "sendPasswordResetEmail";
        String uAccount = fs.getString(CmnConst.USER_ACCOUNT);
        String login_url = fs.getString("login_url");
        //登录URL地址在修改成功后,跳转到该地址,如果是App端没有,则为空,前端判断没有,则提标手动进入登录界面
        if (login_url == null) {
            login_url = "";
        }
        //周杰 2020年12月17日 上午11:16
        if (StringUtils.isEmpty(uAccount)) {
            throw new BaseException(SystemCode.SYSTEM_GET_USERNAME_FAIL.getValue(), SystemCode.SYSTEM_GET_USERNAME_FAIL.getText(), this.getClass(), sendPwdRsetEmail);
        }
        //2021年2月2日 用户不存在,不提示用户不存在,不发邮件操作,正常结束。
        FieldSetEntity fsuname = baseDao.getFieldSetEntityByFilter("product_sys_users", "user_account = ?", new Object[]{uAccount}, false);
        if (fsuname == null) {
            return true;
        }
        String token = userService.createTokenInforMail(uAccount.toString());
        //把邮件token存入redis中
        DataPoolCacheImpl.getInstance().setUserMailToken(fsuname.getString("user_id"), token);
 
        FieldSetEntity fss = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS, CmnConst.USER_ACCOUNT + " = ?", new Object[]{uAccount}, false);
        if (fss == null) {
            throw new BaseException(SystemCode.SYSTEM_GET_USERINFO_FAIL.getValue(), SystemCode.SYSTEM_GET_USERINFO_FAIL.getText(), this.getClass(), sendPwdRsetEmail);
        }
        String url = "http://" + fs.getString("url") + "/#/reset-password";//忘记密码跳转地址
        //查询管理员/员工信息
        FieldSetEntity fsm = null;
        if ("1".equals(fss.getString(CmnConst.IS_MANAGER))) { //管理员
            url = Global.getSystemConfig("resetpwd.lxurl", "");//获取配置的管理员登录地址
            login_url = URLEncoder.encode(Global.getSystemConfig("resetpwd.lxlogin_url", ""));//获取配置的管理员忘记密码跳转页面地址
            fsm = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER, CmnConst.USER_ID + " = ?", new Object[]{fss.getString(CmnConst.USER_ID)}, false);
        } else { //普通员工员
            url = Global.getSystemConfig("resetpwd.lxeurl", "");//获取配置的员工登录地址
            login_url = URLEncoder.encode(Global.getSystemConfig("resetpwd.lxelogin_url", ""));//获取配置的员工忘记密码跳转页面地址
            fsm = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_STAFFS, CmnConst.USER_ID + " = ?", new Object[]{fss.getString(CmnConst.USER_ID)}, false);
        }
        if (fsm == null) {
            throw new BaseException(SystemCode.SYSTEM_FIND_MANAGEMENT_FAIL.getValue(), SystemCode.SYSTEM_FIND_MANAGEMENT_FAIL.getText(), this.getClass(), sendPwdRsetEmail);
        }
        // 设置邮件内容
        //周杰 2020年12月17日 上午11:16
        if (!StringUtils.isEmpty(fss.getString("default_language"))) {
            login_url = login_url + "&language=" + fss.getString("default_language");
        } else {
            //查询公司信息(多个公司默认查询第一个)
            String oluuid = fsm.getString(CmnConst.ORG_LEVEL_UUID);
            if (oluuid.indexOf(",") >= 0) {
                oluuid = oluuid.substring(0, oluuid.indexOf(","));
            }
            FieldSetEntity fsc = baseDao.getFieldSetEntityByFilter("product_sys_company_language", CmnConst.ORG_LEVEL_UUID + " = ?", new Object[]{oluuid}, false);
            if (fsc == null) {
                throw new BaseException(SystemCode.SYSTEM_GET_COMPANY_FAIL.getValue(), SystemCode.SYSTEM_GET_COMPANY_FAIL.getText(), this.getClass(), sendPwdRsetEmail);
            }
            login_url = login_url + "&language=" + fsc.getString("language_code");
        }
        fs.setValue(CmnConst.USER_ID, fss.getString(CmnConst.USER_ID));
        fs.setValue("url", url);
        fs.setValue("token", token);
        fs.setValue("login_url", login_url);
        fs.setValue(CmnConst.USER_PRIMARY_EMAIL, fss.getString(CmnConst.USER_PRIMARY_EMAIL));
        String resultInfo = sendEmailService.parseMailTemplate(CmnConst.FORGET_THE_PASSWORD, fs);
        if (SystemCode.SYSTEM_OPERATION_SUCCESS.getText().equals(resultInfo)) {
            return true;
        } else {
            throw new BaseException(SystemCode.SYSTEM_PASSWORD_EMAIL_FAIL.getValue(), SystemCode.SYSTEM_PASSWORD_EMAIL_FAIL.getText(), this.getClass(), sendPwdRsetEmail);
        }
 
    }
 
 
    /**
     * @Date: 2020-05-30 15:30
     * @Author: ZhouJie
     * @Description: 保存新密码(登录修改密码,验证旧密码)
     */
    @Transactional
    public boolean saveUpdatePwd(FieldSetEntity fs) throws BaseException {
        String saveUpdatePassword = "saveUpdatePwd";
        int uid = SpringMVCContextHolder.getCurrentUser().getUser_id();//回去当前登录用户id
        if (uid == 0) {
            throw new BaseException(SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getValue(), SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getText(), this.getClass(), saveUpdatePassword);
        }
        FieldSetEntity fsu = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS, CmnConst.USER_ID + " = ?", new Object[]{uid}, false);
        if (fsu == null) {
            throw new BaseException(SystemCode.SYSTEM_GET_USERINFO_FAIL.getValue(), SystemCode.SYSTEM_GET_USERINFO_FAIL.getText(), this.getClass(), saveUpdatePassword);
        }
        String userName = SpringMVCContextHolder.getCurrentUser().getUser_account();//用户名
        String oluuid = null;
        FieldSetEntity stfs = null;
        String isM = fsu.getString(CmnConst.IS_MANAGER);
        if (!"1".equals(isM)) {
            stfs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_STAFFS, CmnConst.USER_ID + " = ?", new Object[]{uid}, false);
            if (stfs == null) {
                throw new BaseException(SystemCode.SYSTEM_STAFF_FIND_FAIL.getValue(), SystemCode.SYSTEM_STAFF_FIND_FAIL.getText(), this.getClass(), saveUpdatePassword);
            }
            oluuid = stfs.getString(CmnConst.ORG_LEVEL_UUID);//用户公司
        } else if ("1".equals(isM)) {
            stfs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER, CmnConst.USER_ID + " = ?", new Object[]{uid}, false);
            if (stfs == null) {
                throw new BaseException(SystemCode.SYSTEM_FIND_MANAGEMENT_FAIL.getValue(), SystemCode.SYSTEM_FIND_MANAGEMENT_FAIL.getText(), this.getClass(), saveUpdatePassword);
            }
            oluuid = stfs.getString(CmnConst.ORG_LEVEL_UUID);//管理员公司
            if (oluuid.indexOf(",") != -1) {
                oluuid = oluuid.substring(0, oluuid.indexOf(","));
            }
        }
        if (stfs == null) {
            throw new BaseException(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText(), this.getClass(), saveUpdatePassword);
        }
        //周杰 2020年12月17日 上午11:16
        if (StringUtils.isEmpty(oluuid) || StringUtils.isEmpty(userName)) {
            throw new BaseException(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText(), this.getClass(), saveUpdatePassword);
        }
        //获取的客户端信息
        String oldPwd = fs.getString("old_pwd");//旧密码
        oldPwd = userService.createPassWord(userName, oldPwd);//转码
        String newPwd = fs.getString(CmnConst.USER_PWD);//新密码
        String nowPwd = SpringMVCContextHolder.getCurrentUser().getUser_pwd();//当前密码
        String uuid = SpringMVCContextHolder.getCurrentUser().getUuid();//获取当前登录人uuid
        //验证原密码与当前登录人密码是否一致
        if (oldPwd.equals(nowPwd)) {
            //验证密码格式是否符合要求
            if (checkNewPwd(oluuid, newPwd)) {
                newPwd = userService.createPassWord(userName, newPwd);//新密码转码
                //保存符合要求的新密码
                fs.setValue("uuid", uuid);
                fs.setValue(CmnConst.USER_PWD, newPwd);
                //先验证是否允许重复使用密码,再保存到历史记录中
                if (saveHistoryPassWord(newPwd)) {
                    fs.setValue(CmnConst.USER_ACCOUNT, userName);
                    baseDao.update(fs);
                    //修改成功给用户/(及管理员)发送邮件
                    isEmailNotification(fs.getString(CmnConst.USER_ACCOUNT));
                }
                return true;
            }
            throw new BaseException(SystemCode.SYSTEM_NEW_PASSWORD_ERROR.getValue(), SystemCode.SYSTEM_NEW_PASSWORD_ERROR.getText(), this.getClass(), saveUpdatePassword);
        }
        throw new BaseException(SystemCode.SYSTEM_OLD_PASSWORD_ERROR.getValue(), SystemCode.SYSTEM_OLD_PASSWORD_ERROR.getText(), this.getClass(), saveUpdatePassword);
    }
 
 
    /**
     * @Date: 2020-08-18 17:30
     * @Author: ZhouJie
     * @Description: 保存新密码(忘记密码,直接保存新密码)
     */
    @Transactional
    public boolean saveNewPwd(FieldSetEntity fs) throws BaseException {
        String saveNewPassword = "saveNewPwd";
        FieldSetEntity fss = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS, "user_account = ? or user_primary_email = ?", new Object[]{fs.getString(CmnConst.USER_ACCOUNT), fs.getString(CmnConst.USER_ACCOUNT)}, false);
        if (fss == null) {
            throw new BaseException(SystemCode.SYSTEM_GET_USERINFO_FAIL.getValue(), SystemCode.SYSTEM_GET_USERINFO_FAIL.getText(), this.getClass(), saveNewPassword);
        }
        String uuid = fss.getString(CmnConst.UUID);
        String uid = fss.getString(CmnConst.USER_ID);
        //获取的客户端信息
        String newPwd = fs.getString(CmnConst.USER_PWD);//新密码
        String userName = fss.getString(CmnConst.USER_ACCOUNT);//用户名
        newPwd = userService.createPassWord(userName, newPwd);
        String isM = fss.getString(CmnConst.IS_MANAGER);
        String oluuid = null;
        FieldSetEntity stfs = null;
        //员工
        if ("0".equals(isM)) {
            stfs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_STAFFS, CmnConst.USER_ID + " = ?", new Object[]{uid}, false);
            oluuid = stfs.getString(CmnConst.ORG_LEVEL_UUID);//用户公司
        }
        //管理员
        if ("1".equals(isM)) {
            stfs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_MANAGER, CmnConst.USER_ID + " = ?", new Object[]{uid}, false);
            oluuid = stfs.getString(CmnConst.ORG_LEVEL_UUID);//管理员公司
            if (oluuid.indexOf(",") != -1) {
                oluuid = oluuid.substring(0, oluuid.indexOf(","));
            }
        }
        if (stfs == null) {
            throw new BaseException(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText(), this.getClass(), saveNewPassword);
        }
        //周杰 2020年12月17日 上午11:16
        if (StringUtils.isEmpty(oluuid) || StringUtils.isEmpty(userName)) {
            throw new BaseException(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText(), this.getClass(), saveNewPassword);
        }
        if (checkNewPwd(oluuid, fs.getString("user_pwd"))) {
            //保存符合要求的新密码
            fs.setValue("uuid", uuid);
            fs.setValue(CmnConst.USER_ACCOUNT, userName);
            fs.setValue("user_pwd", newPwd);
            //先验证是否允许重复使用密码,再保存到历史记录中
            if (saveHistoryPassWord(newPwd)) {
                baseDao.update(fs);
                //清除登录失败信息
                userService.loginSuccessProcess(SpringMVCContextHolder.getCurrentUser());
                //修改成功给用户/(及管理员)发送邮件
                isEmailNotification(fs.getString(CmnConst.USER_ACCOUNT));
            }
            //清除忘记密码时邮件中的token
            DataPoolCacheImpl.getInstance().removeUserMailTokenById(uid + "");
        } else {
            throw new BaseException(SystemCode.SYSTEM_PASSWORD_UPDATE_FAIL.getValue(), SystemCode.SYSTEM_PASSWORD_UPDATE_FAIL.getText(), this.getClass(), saveNewPassword);
        }
        return true;
    }
 
    /**
     * @Date: 2020-06-15 16:43
     * @Author: ZhouJie
     * @Description: 验证密码强度正则表达式
     */
    public boolean checkNewPwd(String orgleveluuid, String pwd) throws BaseException {
        //周杰 2020年12月17日 上午11:16
        String checkNewPassword = "checkNewPwd";
        if (StringUtils.isEmpty(pwd)) {
            throw new BaseException(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText(), this.getClass(), checkNewPassword);
        }
        FieldSetEntity fs = null;
        //周杰 2020年12月17日 上午11:16
//        if (!StringUtils.isEmpty(orgleveluuid)) {
//            fs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, CmnConst.ORG_LEVEL_UUID + " = ?", new Object[]{orgleveluuid}, false);
//            if (fs == null) {
//                fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, SCUUID, false);
//                if (fs == null) {
//                    throw new BaseException(SystemCode.SYSTEM_PASSWORD_SECURITY_SET.getValue(), SystemCode.SYSTEM_PASSWORD_SECURITY_SET.getText(), this.getClass(), checkNewPassword);
//                }
//            }
//        } else {
//            fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, SCUUID, false);
//            if (fs == null) {
//                throw new BaseException(SystemCode.SYSTEM_PASSWORD_SECURITY_SET.getValue(), SystemCode.SYSTEM_PASSWORD_SECURITY_SET.getText(), this.getClass(), checkNewPassword);
//            }
//        }
        //获取当前登录人所属公司
        DataTableEntity t = baseDao.listTable(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, CmnConst.ORG_LEVEL_UUID + " = ? or " + CmnConst.ORG_LEVEL_UUID + " is null or " + CmnConst.ORG_LEVEL_UUID + "='' ", new Object[]{orgleveluuid}, CmnConst.ORG_LEVEL_UUID + " desc ");
        if (t != null && t.getRows() > 0) {
            fs = t.getFieldSetEntity(0);
        }
        if (fs == null) {
            fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, "security_uuid_01", false);
        }
        FieldSetEntity ff = regular(fs);
        String cecknum = ff.getString("cecknum");
        //判断密码强度
        if (cecknum.equals("^")) {
            return true;
        }
        if (!pwd.matches(cecknum)) {
            throw new BaseException(SystemCode.SYSTEM_NEW_PASSWORD_ERROR.getValue(), SystemCode.SYSTEM_NEW_PASSWORD_ERROR.getText(), this.getClass(), checkNewPassword);
        }
 
        return true;
    }
 
    /**
     * @Date: 2020-08-24 18:08
     * @Author: ZhouJie
     * @Description:返回密码强度正则表达式
     */
    public FieldSetEntity returnPwdSecurity(Object langaugeUuid) throws BaseException {
        String returnPasswordSecurity = "returnPwdSecurity";
        FieldSetEntity fs = null;
        String uAccount = SpringMVCContextHolder.getCurrentUser().getUser_account();//当前用户名
        //周杰 2020年12月17日 上午11:16
        if (StringUtils.isEmpty(uAccount)) {
            throw new BaseException(SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getValue(), SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getText(), this.getClass(), returnPasswordSecurity);
        }
        FieldSetEntity ufs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS, "user_account = ? or  user_primary_email = ?", new Object[]{uAccount, uAccount}, false);
        if (ufs == null) {
            throw new BaseException(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText(), this.getClass(), returnPasswordSecurity);
        }
        String uid = ufs.getString(CmnConst.USER_ID);
        //周杰 2020年12月17日 上午11:16
        if (StringUtils.isEmpty(uid)) {
            throw new BaseException(SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getValue(), SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getText(), this.getClass(), returnPasswordSecurity);
        }
        //获取当前登录人所属公司
        String oluuid = SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid();
        DataTableEntity t = baseDao.listTable(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, CmnConst.ORG_LEVEL_UUID + " = ? or " + CmnConst.ORG_LEVEL_UUID + " is null or " + CmnConst.ORG_LEVEL_UUID + "='' ", new Object[]{oluuid}, CmnConst.ORG_LEVEL_UUID + " desc ");
        if (t != null && t.getRows() > 0) {
            fs = t.getFieldSetEntity(0);
        }
        if (fs == null) {
            fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, "security_uuid_01", false);
        }
        if (fs != null) {
            fs.setLanguage_code((String) langaugeUuid);
        }
        return regular(fs);
    }
 
    public FieldSetEntity regular(FieldSetEntity fs) throws BaseException {
        if (fs == null) {
            throw new BaseException(SystemCode.SYSTEM_PASSWORD_SECURITY_NOT_FIND.getValue(), SystemCode.SYSTEM_PASSWORD_SECURITY_NOT_FIND.getText());
        }
        FieldSetEntity ff = new FieldSetEntity();
        String up = fs.getString("security_upper");//是否大写字母
        String low = fs.getString("security_lower");//是否小写字母
        String nums = fs.getString("security_number");//是否数字
        String oth = fs.getString("security_other");//是否其他字符
        String ipl = fs.getString("security_password");//是否需要密码长度
        String plss = fs.getString("security_password_length");
        int pls = 20;
        //周杰 2020年12月17日 上午11:16
        if (!StringUtils.isEmpty(plss)) {
            pls = Integer.parseInt(fs.getString("security_password_length"));//密码长度
        }
        /*
        String language_code = fs.getLanguage_code();
        DataTableEntity verifyMessage = new DataTableEntity();
        if (language_code != null) {
            //updatetime 2021年2月6日12:52:09 xupengcheng 密码强度验证消息国际化查询
            StringBuilder sb = new StringBuilder();
            sb.append(" SELECT ");
            sb.append(" language_master_final_key AS uuid, ");
            sb.append(" language_value ");
            sb.append(" FROM ");
            sb.append(" product_sys_language_master a ");
            sb.append(" JOIN product_sys_language_values b ON a.uuid = b.language_master_uuid ");
            sb.append(" WHERE ");
            sb.append(" language_master_final_key IN ( 'verify_ContainsNumbers', 'verify_Containscapitalletters', ");
            sb.append(" 'verify_Includelowercaseletters', 'verify_Containspecialcharacters', 'verify_want_strlen_length' ) ");
            sb.append(" AND language_code = ? ");
            DataTableEntity dt = baseDao.listTable(sb.toString(), new Object[]{language_code});
            if (dt != null) {
                verifyMessage = dt;
            }
        }
        */
        StringBuffer cecknum = new StringBuffer();
        cecknum.append("^");
        StringBuffer ceckmean = new StringBuffer();
        if ("1".equals(up)) {
            cecknum.append("(?=.*?[A-Z])");
            ceckmean.append("包含大写字母");
        }
        if ("1".equals(low)) {
            cecknum.append("(?=.*?[a-z])");
            if (ceckmean.length() > 0) {
                ceckmean.append("、");
            }
            ceckmean.append("包含小写字母");
//            ceckmean += message;
        }
        if ("1".equals(nums)) {
            cecknum.append("(?=.*?[0-9])");
            if (ceckmean.length() > 0) {
                ceckmean.append("、");
            }
            ceckmean.append("包含数字");
        }
        if ("1".equals(oth)) {
            cecknum.append("(?=.*?[~.\\/({\\\\\\[\\\\\\]})|<>#?!@$%^&*=+-])");
            if (ceckmean.length() > 0) {
                ceckmean.append("、");
            }
            ceckmean.append("包含特殊字符");
        }
        if ("1".equals(ipl)) {
            cecknum.append("(.{" + pls + ",20}$)");
            if (ceckmean.length() > 0) {
                ceckmean.append("、");
            }
            ceckmean.append("需要" + pls + "到20位有效字符");
        }
        if (ceckmean.length() > 0) {
            ceckmean.append("。");
        }
        ff.setTableName("regular");
        ff.setValue("cecknum", cecknum.toString());
        ff.setValue("ceckmean", ceckmean.toString());
        return ff;
 
    }
 
    /**
     * 密码强度验证消息国际化获取
     *
     * @param messages
     * @return
     * @throws BaseException
     */
    private String passwordVerifyMessage(List<FieldSetEntity> messages) throws BaseException {
        if (messages != null && messages.size() > 0) {
            FieldSetEntity fs = messages.get(0);
            if (fs != null) {
                String language_value = fs.getString("language_value");
                if (!StringUtils.isEmpty(language_value)) {
                    return language_value;
                }
            }
        }
        return "";
    }
 
    /**
     * 新密码保存到密码记录表中,同时比较是否与历史修改过的密码相同,如果公司允许相同,则保存,否则报错不允修改
     *
     * @param newPWD 新密码
     * @return true:可以修改
     * @throws BaseException 通用错误
     */
    public boolean saveHistoryPassWord(String newPWD) throws BaseException {
        FieldSetEntity fs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, CmnConst.ORG_LEVEL_UUID + "=?", new Object[]{SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid()}, false);
        if (fs != null) {
            int re = fs.getInteger("security_repeate_history_password");
            if (re == 0) {
                FieldSetEntity fp = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY_HISTORY, "history_password=? and user_id=?", new Object[]{newPWD, SpringMVCContextHolder.getCurrentUser().getUser_id()}, false);
                if (fp != null) {
                    throw new BaseException(SystemCode.SYSTEM_UPDATE_PWD_REPEAT.getValue(), SystemCode.SYSTEM_UPDATE_PWD_REPEAT.getText());
                }
            }
        }
        FieldSetEntity fsp = new FieldSetEntity();
        fsp.setTableName(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY_HISTORY);
        fsp.setValue("history_password", newPWD);
        fsp.setValue(CmnConst.USER_ID, SpringMVCContextHolder.getCurrentUser().getUser_id());
        fsp.setValue("created_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
        fsp.setValue("created_utc_datetime", new Date());
        return baseDao.add(fsp) != null ? true : false;
    }
 
    /**
     * 密码强度设置修改(或重置)密码成功是否以邮件形式通知管理员
     *
     * @param userName 当前账号
     * @return true:通知
     * @throws BaseException 通用错误
     */
    public boolean isEmailNotification(String userName) throws BaseException {
        String isEmailNotFicat = "isEmailNotification";
        //根据当前登陆人获取密码强度
        FieldSetEntity fs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, CmnConst.ORG_LEVEL_UUID + "=?", new Object[]{SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid()}, false);
        if (fs != null && "1".equals(fs.getString("security_notice_email"))) {
            //以邮件通知管理员
            FieldSetEntity fss = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS, CmnConst.USER_ACCOUNT + " = ?", new Object[]{userName}, false);
            fs.setValue(CmnConst.USER_ID, fss.getString(CmnConst.USER_ID));
            fs.setValue(CmnConst.USER_ACCOUNT, fss.getString(CmnConst.USER_ACCOUNT));//用戶名
            fs.setValue("user_primary_email", fss.getString("user_primary_email"));// 邮箱地址
            String resultInfo = sendEmailService.parseMailTemplate(CmnConst.PASSWORD_MODIFIED_SUCCESSFULLY, fss);
            if (!"admin".equals(fss.getString(CmnConst.USER_ACCOUNT))) {//员工  还需要给对应管理员发邮件
                FieldSetEntity use = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS, CmnConst.USER_ID + " = ?", new Object[]{fss.getString("created_by")}, false);
                use.setValue(CmnConst.USER_ACCOUNT, fss.getString(CmnConst.USER_ACCOUNT));//用戶名
                String resultInfo2 = sendEmailService.parseMailTemplate(CmnConst.PASSWORD_MODIFIED_SUCCESSFULLY, use);
                if (SystemCode.SYSTEM_OPERATION_SUCCESS.getText().equals(resultInfo2)) {
                    return true;
                } else {
                    throw new BaseException(SystemCode.SYSTEM_PASSWORD_EMAIL_FAIL.getValue(), SystemCode.SYSTEM_PASSWORD_EMAIL_FAIL.getText(), this.getClass(), isEmailNotFicat);
                }
            }
            if (SystemCode.SYSTEM_OPERATION_SUCCESS.getText().equals(resultInfo)) {
                return true;
            } else {
                throw new BaseException(SystemCode.SYSTEM_PASSWORD_EMAIL_FAIL.getValue(), SystemCode.SYSTEM_PASSWORD_EMAIL_FAIL.getText(), this.getClass(), isEmailNotFicat);
            }
        }
        return false;
    }
 
 
}