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
package com.nanometer.smartlab.controller;
 
import com.alibaba.druid.util.StringUtils;
import com.nanometer.smartlab.entity.BaseMeta;
import com.nanometer.smartlab.entity.BaseRole;
import com.nanometer.smartlab.entity.SysUser;
import com.nanometer.smartlab.entity.enumtype.ApproverFlag;
import com.nanometer.smartlab.entity.enumtype.SeeFlag;
import com.nanometer.smartlab.entity.enumtype.ValidFlag;
import com.nanometer.smartlab.service.BaseMetaService;
import com.nanometer.smartlab.service.BaseRoleService;
import com.nanometer.smartlab.service.SysUserService;
import com.nanometer.smartlab.util.Constants;
import com.nanometer.smartlab.util.FacesUtils;
import com.nanometer.smartlab.util.IDUtils;
import com.nanometer.smartlab.util.MD5Utils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.primefaces.model.UploadedFile;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
 
import javax.annotation.Resource;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.CharacterCodingException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
 
/**
 * Created by johnny on 17/11/20.
 */
@Controller
@Scope("session")
public class UserMngController extends BaseController {
 
    private static Logger logger = Logger.getLogger(UserMngController.class);
 
    @Resource
    private SysUserService sysUserService;
    @Resource
    private BaseMetaService baseMetaService;
    @Resource
    private BaseRoleService baseRoleService;
 
    private LazyDataModel<SysUser> dataModel;
    private SysUser sysUser;
    private List<SysUser> selectedList;
    private List<ApproverFlag> approverFlagSelectList;
    private List<SeeFlag> seeFlagSelectList;
    private String arp;
    private String name;
    private String username;
    private String oriPassword;
    private String newPassword;
    private String newPasswordSecond;
    private String departmentName;
    private String editPasswor;
    private List<BaseMeta> codeList;
    private List<BaseRole> roleList;
 
    public List<BaseRole> getRoleList() {
        return roleList;
    }
 
    public void setRoleList(List<BaseRole> roleList) {
        this.roleList = roleList;
    }
 
    public List<BaseMeta> getCodeList() {
        return codeList;
    }
 
    public void setCodeList(List<BaseMeta> codeList) {
        this.codeList = codeList;
    }
 
    public String getEditPasswor() {
        return editPasswor;
    }
 
    public void setEditPasswor(String editPasswor) {
        this.editPasswor = editPasswor;
    }
 
    private int action;
 
    public void onNewBtnClick() {
        this.sysUser = new SysUser();
        this.action = Constants.ACTION_ADD;
    }
 
    public void updatePassword() {
            this.username = getUser().getName();
            this.sysUser = getUser();
    }
 
 
    public void uploadFile(FileUploadEvent event) {
        System.out.println("=========导入开始=====");
        this.codeList=baseMetaService.getAllBaseMeta();
        this.roleList=baseRoleService.getBaseRoleList(null,null,null,null);
        try{
            UploadedFile file = event.getFile();
            InputStream is=file.getInputstream();
            boolean isExcel2003=true;
            if (file.getFileName().matches("^.+\\.(?i)(xlsx)$")) {
                isExcel2003 = false;
            }
            Workbook wb=null;
            if(isExcel2003) {
                wb = new HSSFWorkbook(is);
            }else{
                wb = new XSSFWorkbook(is);
            }
            Sheet sheet=wb.getSheetAt(0);
            Row row=null;
            int totalCells=0;
            int totalRows = sheet.getPhysicalNumberOfRows();
            List<SysUser> userList=new ArrayList<>();
            System.out.println("totalRows=========="+totalRows);
            for (int i = 1; i < totalRows; i++) {
                row=sheet.getRow(i);
                totalCells= row.getLastCellNum();
                List<String> valuesList=new ArrayList<String>();
                for (int t = 0; t < totalCells; t++) {
                    String cellInfo="";
                    String groupId = "";
 
                    if (row.getCell(t) != null) {
 
                        if (row.getCell(t).getCellTypeEnum().toString().equals("NUMERIC")) {
                            cellInfo = String.valueOf(row.getCell(t).getNumericCellValue());
                        } else {
                            cellInfo = row.getCell(t).getStringCellValue();
                        }
                    }
                    if(t == 0 || t == 1){
                        if( t == 0){
                            groupId = "user_company";
                        }else if(t == 1){
                            groupId = "user_department";
                        }
                        String id=getId(groupId,cellInfo);
                        if(id == "" && cellInfo!=null && cellInfo!="" ) {
                            BaseMeta baseMeta = new BaseMeta();
                            baseMeta.setId(IDUtils.uuid());
                            baseMeta.setGroupId(groupId);
                            baseMeta.setMetaValue(cellInfo);
                            baseMeta.setMetaKey(cellInfo);
                            baseMetaService.insertBaseMeta(baseMeta);
                            id = baseMeta.getId();
                            this.codeList.add(baseMeta);
                        }
                        valuesList.add(id);
                    }else if(t==10){
                        String id=getRoleId(cellInfo);
                        if(id == "" && cellInfo!=null && cellInfo!="" ){
                            BaseRole role=new BaseRole();
                            role.setId(IDUtils.uuid());
                            role.setName(cellInfo);
                            role.setValidFlag(ValidFlag.VALID);
                        }
                        valuesList.add(id);
                    }else if(t==6){
                        cellInfo=new BigDecimal(""+cellInfo).toString();
                        valuesList.add(cellInfo);
                    }
                    else {
                        valuesList.add(cellInfo);
                    }
 
                }
                System.out.println("size :     "+valuesList.size());
                System.out.println("value :     "+valuesList);
                SysUser sysUser=new SysUser();
                sysUser.setCompany(valuesList.get(0));//单位
                sysUser.setDepartment(valuesList.get(1));//部门
                sysUser.setArp((int)Double.parseDouble(valuesList.get(2))+"");//arp号
                sysUser.setName(valuesList.get(3));//姓名
                sysUser.setAccount(valuesList.get(4));//账号
                sysUser.setPassword(MD5Utils.encode((int)Double.parseDouble(valuesList.get(5))+""));//密码
                sysUser.setPhone(valuesList.get(6));//电话
                sysUser.setEmail(valuesList.get(7));//邮箱
                sysUser.setIdCard(valuesList.get(8));//id卡号
                sysUser.setPoint((int)Double.parseDouble(valuesList.get(9)));//分数
                sysUser.setRoleId(valuesList.get(10));//角色
                if(valuesList.get(11).equals("是")){
                    sysUser.setApproverFlag(ApproverFlag.YES);
                }else {
                    sysUser.setApproverFlag(ApproverFlag.NO);
                }
                if(valuesList.get(12).equals(SeeFlag.MANAGE.getText())){
                    sysUser.setSeeFlag(SeeFlag.MANAGE);
                }
                if(valuesList.get(12).equals(SeeFlag.NORMAL.getText())){
                    sysUser.setSeeFlag(SeeFlag.NORMAL);
                }
                if(valuesList.get(12).equals(SeeFlag.SOFTER.getText())){
                    sysUser.setSeeFlag(SeeFlag.SOFTER);
                }
                if(valuesList.get(12).equals(SeeFlag.LEADING.getText())){
                    sysUser.setSeeFlag(SeeFlag.LEADING);
                }
                if(valuesList.size()==14){
                    sysUser.setMemo(valuesList.get(13));
                }else {
                    sysUser.setMemo("");
                }
 
                sysUser.setValidFlag(ValidFlag.VALID);
                userList.add(sysUser);
            }
            this.sysUserService.insertSysUserList(userList);
            System.out.println("=========导入结束=====");
            FacesUtils.info("导入成功。");
        }catch (Exception ex) {
            ex.printStackTrace();
            FacesUtils.info("导入失败。");
        }
    }
 
    public String getRoleId(String name){
        String id="";
        for (int i=0;i<this.roleList.size();i++){
            if(this.roleList.get(i).getName().equals(name)){
                id=this.roleList.get(i).getId();
            }
        }
        return id;
    }
 
    //判断字典表里是否存在这组键值对
    public String getId(String groupId,String metaValue){
        String id="";
        if(this.codeList.size()>0){
            for (BaseMeta bm:this.codeList){
 
                if(bm.getGroupId().equals(groupId)&&bm.getMetaValue().equals(metaValue)){
                    id=bm.getId();
                }
            }
        }
        return id;
    }
 
    public String updatePasswordAction() throws NoSuchAlgorithmException, CharacterCodingException
    {
        if(!this.sysUser.getPassword().equals(MD5Utils.encode(this.oriPassword)))
        {
            FacesUtils.warn("原密码错误,请重新输入!");
            return null;
        }
            if(!this.newPassword.equals(this.newPasswordSecond))
            {
                FacesUtils.warn("新密码不一致,请重新输入。");
            return null;
            }
 
            this.sysUser.setName(username);
            this.sysUser.setPassword(MD5Utils.encode(this.newPassword));
        this.sysUserService.updateSysUser(sysUser);
        this.newPassword = null;
        this.oriPassword = null;
        this.newPasswordSecond = null;
        FacesUtils.info("修改成功,请重新登录!");
        RequestContext.getCurrentInstance().execute("PF('dialogPassword').hide()");
        this.getSubject().logout();
        return Constants.PAGE_LOGIN + Constants.REDIRECT;
    }
 
    public void onEditBtnClick() {
        if (this.selectedList == null
                || this.selectedList.size() == 0) {
            FacesUtils.warn("请选择数据。");
            return;
        }
        if (this.selectedList.size() > 1) {
            FacesUtils.warn("只能选择一个数据进行修改。");
            return;
        }
        this.sysUser = this.sysUserService.getSysUser(this.selectedList.get(0).getId());
        this.editPasswor=this.sysUser.getPassword();
        this.action = Constants.ACTION_EDIT;
        RequestContext.getCurrentInstance().execute("PF('dialog').show()");
    }
 
    public void onSaveBtnClick() {
        try {
            // 新建
            if (this.action == Constants.ACTION_ADD) {
                if (this.sysUser == null) {
                    FacesUtils.warn("新建对象为空。");
                    return;
                }
 
                // 检查ARP号
                if (this.sysUserService.isSysUserExist(this.sysUser.getArp(), null, null, null)) {
                    FacesUtils.warn("ARP已存在。");
                    return;
                }
 
                // 检查登陆账号
                if (this.sysUserService.isSysUserExist(null, this.sysUser.getAccount(), null, null)) {
                    FacesUtils.warn("登陆账号已存在。");
                    return;
                }
 
                // 检查idCard
                if (!StringUtils.isEmpty(this.sysUser.getIdCard()) && this.sysUserService.isSysUserExist(null, null, this.sysUser.getIdCard(), null)) {
                    FacesUtils.warn("ID卡号已存在。");
                    return;
                }
                if(!org.apache.commons.lang.StringUtils.isNotBlank(sysUser.getProjectId())){
                    sysUser.setProjectId(null);
                }
                sysUser.setPassword(MD5Utils.encode(sysUser.getPassword()));
                this.sysUserService.insertSysUser(sysUser);
 
                FacesUtils.info("新建成功。");
                RequestContext.getCurrentInstance().execute("PF('dialog').hide()");
            // 修改
            } else if (this.action == Constants.ACTION_EDIT) {
                if (this.sysUser == null) {
                    FacesUtils.warn("修改对象为空。");
                    return;
                }
 
                // 检查ARP号
                if (this.sysUserService.isSysUserExist(this.sysUser.getArp(), null, null, this.sysUser.getId())) {
                    FacesUtils.warn("ARP已存在。");
                    return;
                }
 
                // 检查登陆账号
                if (this.sysUserService.isSysUserExist(null, this.sysUser.getAccount(), null, this.sysUser.getId())) {
                    FacesUtils.warn("登陆账号已存在。");
                    return;
                }
 
                // 检查idCard
                if(this.sysUser.getIdCard()==null || this.sysUser.getIdCard()==""){
 
                }else {
                    if (this.sysUserService.isSysUserExist(null, null, this.sysUser.getIdCard(), this.sysUser.getId())) {
                        FacesUtils.warn("ID卡号已存在。");
                        return;
                    }
                }
                if(sysUser.getPassword()==null || sysUser.getPassword().equals("")){
                    sysUser.setPassword(this.editPasswor);
                }else {
                    sysUser.setPassword(MD5Utils.encode(sysUser.getPassword()));
                }
                this.sysUserService.updateSysUser(sysUser);
 
                FacesUtils.info("修改成功。");
                RequestContext.getCurrentInstance().execute("PF('dialog').hide()");
            }
        } catch (Exception e) {
            logger.error("操作失败。", e);
            FacesUtils.warn("操作失败。");
        }
    }
 
    public void onDeleteBtnClick() {
        try {
            if (this.selectedList == null
                    || this.selectedList.size() == 0) {
                FacesUtils.warn("请选择数据。");
                return;
            }
 
            this.sysUserService.deleteSysUser(this.selectedList);
 
            FacesUtils.info("删除成功。");
        } catch (Exception e) {
            logger.error("操作失败。", e);
            FacesUtils.warn("操作失败。");
        }
    }
 
    public LazyDataModel<SysUser> getDataModel() {
        if (this.dataModel == null) {
            this.dataModel = new LazyDataModel<SysUser>() {
                @Override
                public List<SysUser> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
                    List<SysUser> list = null;
                    try {
                        int count = sysUserService.getSysUserTotalCount(arp, name,departmentName);
                        this.setRowCount(count);
                        if (count > 0) {
                            list = sysUserService.getSysUserList(arp, name,departmentName, first, pageSize);
                        }
                    } catch (Exception e) {
                        logger.error(e);
                    }
                    return list;
                }
 
                @Override
                public SysUser getRowData(String rowKey) {
                    Iterator<SysUser> iterator = this.iterator();
                    if (iterator != null) {
                        SysUser su = null;
                        while (iterator.hasNext()) {
                            su = iterator.next();
                            if (rowKey.equals(su.getId())) {
                                return su;
                            }
                        }
                    }
                    return null;
                }
            };
        }
        return dataModel;
    }
 
    public SysUser getSysUser() {
        return sysUser;
    }
 
    public void setSysUser(SysUser sysUser) {
        this.sysUser = sysUser;
    }
 
    public List<SysUser> getSelectedList() {
        return selectedList;
    }
 
    public void setSelectedList(List<SysUser> selectedList) {
        this.selectedList = selectedList;
    }
 
    public int getAction() {
        return action;
    }
 
    public List<ApproverFlag> getApproverFlagSelectList() {
        if (this.approverFlagSelectList == null) {
            this.approverFlagSelectList = Arrays.asList(ApproverFlag.values());
        }
 
        return approverFlagSelectList;
    }
 
    public List<SeeFlag> getSeeFlagSelectList() {
        if (this.seeFlagSelectList == null) {
            this.seeFlagSelectList = Arrays.asList(SeeFlag.values());
        }
 
        return seeFlagSelectList;
    }
 
    public String getArp() {
        return arp;
    }
 
    public void setArp(String arp) {
        this.arp = arp;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getOriPassword() {
        return oriPassword;
    }
 
    public void setOriPassword(String oriPassword) {
        this.oriPassword = oriPassword;
    }
 
    public String getNewPassword() {
        return newPassword;
    }
 
    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }
 
    public String getNewPasswordSecond() {
        return newPasswordSecond;
    }
 
    public void setNewPasswordSecond(String newPasswordSecond) {
        this.newPasswordSecond = newPasswordSecond;
    }
 
    public String getDepartmentName() {
        return departmentName;
    }
 
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}