wzy
2022-08-14 b141df9bf9764db8567efebed48006e12ab1f657
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
package com.xzx.gc.role.controller;
 
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.common.HttpRequestLocal;
import com.xzx.gc.common.annotations.PassToken;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.RedisKeyConstant;
import com.xzx.gc.common.dto.log.OperationAppLog;
import com.xzx.gc.common.request.BaseController;
import com.xzx.gc.common.utils.MqUtil;
import com.xzx.gc.common.utils.RedisUtil;
import com.xzx.gc.common.utils.SecurityUtil;
import com.xzx.gc.common.utils.SpringUtil;
import com.xzx.gc.entity.*;
import com.xzx.gc.model.JsonResult;
import com.xzx.gc.model.admin.AppRoleModel;
import com.xzx.gc.model.dto.FunctionNodeView;
import com.xzx.gc.model.dto.MenuNodeView;
import com.xzx.gc.model.dto.SystemMenuView;
import com.xzx.gc.model.query.OrgQuery;
import com.xzx.gc.model.query.RoleQuery;
import com.xzx.gc.role.model.CoreDictQueryModel;
import com.xzx.gc.role.model.CoreRoleModel;
import com.xzx.gc.role.model.CoreUserModel;
import com.xzx.gc.role.rbac.UserLoginInfoAdmin;
import com.xzx.gc.role.rbac.tree.FunctionItem;
import com.xzx.gc.role.rbac.tree.MenuItem;
import com.xzx.gc.role.rbac.tree.OrgItem;
import com.xzx.gc.role.service.*;
import com.xzx.gc.role.util.TokenUtils;
import com.xzx.gc.util.ConvertUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;
 
@RestController
public class CoreUserController extends BaseController {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
    @Autowired
    CorePlatformService platformService;
 
    @Autowired
    private MqUtil mqUtil;
 
    @Autowired
    CoreUserService userService;
 
    @Autowired
    HttpRequestLocal httpRequestLocal;
 
    @Autowired
    CuserConsoleService userConsoleService;
    @Autowired
    CoreRoleService coreRoleService;
    @Autowired
    private OrgConsoleService orgConsoleService;
    @Autowired
    CoreDictService coreDictService;
    @Autowired
    CityPartnerService cityPartnerService;
    @Autowired
    RoleConsoleService roleConsoleService;
    @Autowired
    RedisUtil redisService;
 
    @Autowired
    private CoreUserService coreUserService;
 
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/login.json")
    public JsonResult<UserLoginInfoAdmin> login(String code, String password) {
        UserLoginInfoAdmin info = userService.login(code, password);
        if (info == null) {
            return JsonResult.failMessage("用户名密码错");
        }
        CoreUser user = info.getUser();
        CoreOrg currentOrg = info.getOrgs().get(0);
        for (CoreOrg org : info.getOrgs()) {
            if (org.getId() == user.getOrgId()) {
                currentOrg = org;
                break;
            }
        }
        info.setCurrentOrg(currentOrg);
        // 记录登录信息到session
        this.platformService.setLoginUser(info.getUser(), info.getCurrentOrg(), info.getOrgs());
        return JsonResult.success(info);
    }
    /**
     * 用户所在部门
     * 
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/org/myOrgs.json")
    public JsonResult<List<CoreOrg>> myOrgs() {
        List<CoreOrg> orgs = (List<CoreOrg>) httpRequestLocal.getSessionValue(CorePlatformService.ACCESS_USER_ORGS);
        return JsonResult.success(orgs);
    }
 
    /**
     * 切换部门
     * 
     * @param orgId
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/org/setOrg.json")
    public JsonResult login(Long orgId) {
 
        CoreUser user = platformService.getCurrentUser();
        // 检查是否存在orgId
        List<CoreOrg> orgs = platformService.getCurrentOrgs();
        CoreOrg currentOrg = null;
        for (CoreOrg org : orgs) {
            if (orgId == org.getId()) {
                currentOrg = org;
                break;
            }
        }
        if (currentOrg == null) {
            // 非法切换
            return JsonResult.failMessage("切换到不存在的部门");
        }
 
        httpRequestLocal.setSessionValue(CorePlatformService.ACCESS_CURRENT_ORG, currentOrg);
 
        return JsonResult.success();
    }
 
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/changePassword.json")
    public JsonResult chnagePassword(String password, String newPassword) {
        CoreUser temp = platformService.getCurrentUser();
        CoreUser realUser = userService.getUserById(temp.getId());
        String pwd =password;
        if (realUser.getPassword().equals(pwd)) {
            realUser.setPassword(newPassword);
            userService.update(realUser);
            return JsonResult.success();
        } else {
            return JsonResult.failMessage("密码错误");
        }
 
    }
 
    /**
     * 用户能查看的菜单
     * 
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/menu/menuTree.json")
    public JsonResult<List<MenuNodeView>> menus() {
        CoreUser currentUser = platformService.getCurrentUser();
        Long orgId = platformService.getCurrentOrgId();
        MenuItem item = platformService.getMenuItem(currentUser.getId(), orgId);
        List<MenuNodeView> view = this.build(item);
        return JsonResult.success(view);
    }
 
    /**
     * 获取系统
     * 
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/menu/system.json")
    public JsonResult<List<SystemMenuView>> getSystem() {
        CoreUser currentUser = platformService.getCurrentUser();
        Long orgId = platformService.getCurrentOrgId();
        MenuItem menuItem = platformService.getMenuItem(currentUser.getId(), orgId);
        List<MenuItem> list = menuItem.getChildren();
        List<SystemMenuView> systems = new ArrayList<SystemMenuView>();
        for (MenuItem item : list) {
            systems.add(new SystemMenuView(item.getId(), item.getData().getCode(), item.getData().getName()));
        }
        return     JsonResult.success(systems);
    }
 
    /**
     * 获取系统对应的菜单树
     * 
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/menu/systemMenu.json")
    public JsonResult<List<MenuNodeView>> getMenuBySystem(long systemId) {
        CoreUser currentUser = platformService.getCurrentUser();
        Long orgId = platformService.getCurrentOrgId();
        MenuItem menuItem = platformService.getMenuItem(currentUser.getId(), orgId);
        MenuItem item = menuItem.findChild(systemId);
        List<MenuNodeView> view = this.build(item);
        return JsonResult.success(view);
    }
 
    /**
     * 用户所在公司的组织机构树
     * 
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/org/org.json")
    public JsonResult<OrgItem> getUserCompany() {
        OrgItem orgItem = platformService.getUserOrgTree();
        return JsonResult.success(orgItem);
    }
 
    /**
     * 获取系统的菜单树
     * 
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/menu/tree.json")
    public JsonResult<List<MenuNodeView>> getMenuTree() {
        MenuItem menuItem = platformService.buildMenu();
        List<MenuNodeView> view = this.build(menuItem);
        return JsonResult.success(view);
    }
    /**
     * 获取功能点树
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/function/tree.json")
    public JsonResult<List<FunctionNodeView> > getFunctionTree() {
            FunctionItem root = this.platformService.buildFunction();
            List<FunctionNodeView> tree = buildFunctionTree(root);
            return JsonResult.success(tree);
       
    }
 
    
    private List<MenuNodeView> build(MenuItem node) {
        List<MenuItem> list = node.getChildren();
        if (list.size() == 0) {
            return Collections.EMPTY_LIST;
        }
 
        List<MenuNodeView> views = new ArrayList<MenuNodeView>(list.size());
 
        for (MenuItem item : list) {
            MenuNodeView view = new MenuNodeView();
            view.setCode(item.getData().getCode());
            view.setName(item.getData().getName());
            view.setIcon(item.getData().getIcon());
            view.setId(item.getData().getId());
            //view.setPath((String) item.getData().get("accessUrl"));
            view.setPath("");
            List<MenuNodeView> children = this.build(item);
            view.setChildren(children);
            views.add(view);
        }
        return views;
    }
    
    private List<FunctionNodeView> buildFunctionTree(FunctionItem node){
          List<FunctionItem> list = node.getChildren();
          if(list.size()==0){
              return Collections.EMPTY_LIST;
          }
          List<FunctionNodeView> views = new ArrayList<FunctionNodeView>(list.size());
          for(FunctionItem item :list){
              FunctionNodeView view = new FunctionNodeView();
              view.setCode(item.getData().getCode());
              view.setName(item.getData().getName());
              view.setId(item.getData().getId());
              view.setAccessUrl(item.getData().getAccessUrl());
              List<FunctionNodeView> children = this.buildFunctionTree(item);
              view.setChildren(children);
              views.add(view);
          }
          return views;
     }
 
    /**
     //     * 登陆接口
     //     * @param request
     //     * @param response
     //     * @return
     //     */
    @PassToken
    @PostMapping(Constants.ADMIN_VIEW_PREFIX+"/loginData.do")
    @ApiOperation(value="用户登陆", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "code", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "password", value = "密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "verify", value = "验证码", required = true, dataType = "String")
    })
    public JsonResult<Map<String,Object>> loginData(HttpServletRequest request, HttpServletResponse response, @RequestBody AppRoleModel model){
        //String ipAddr = request.getRequestURL().toString();
       String userType="1";
       List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> resultMap = new HashMap<>();
        if(!SpringUtil.isDev()) {
            //验证码
            Object cap = request.getSession().getAttribute("cap");
            if (cap == null || !cap.toString().equalsIgnoreCase(model.getVerify())) {
                resultMap.put("code", -1);
                resultMap.put("msg", "验证码错误");
                return JsonResult.failMessage("验证码错误");
            }
        }
 
 
        String jsessionid = request.getSession().getId();
        Cookie cookie =new Cookie("JSESSIONID",jsessionid);
        cookie.setMaxAge(60*60*24*7);
        response.addCookie(cookie);
        String code = model.getCode();
        String password = model.getPassword();
        Map<String,Object> map = userService.loginApi(code, password,userType);
        if(null!=map.get("msg")){
            return JsonResult.failMessage(map.get("msg").toString());
 
        }
        Object obj = map.get("userLoginInfo");
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        UserLoginInfoAdmin info = objectMapper.convertValue(obj, UserLoginInfoAdmin.class);
 
        if (map.get("msg") != null) {
            return JsonResult.failMessage(map.get("msg").toString());
        }
        CoreUser user = info.getUser();
        redisService.remove("xzx:user:token:"+user.getId());
        //redisService.remove("xzx:user:verify:"+model.getCode());
        CoreOrg currentOrg = info.getOrgs().get(0);
        for (CoreOrg org : info.getOrgs()) {
            if (org.getId() == user.getOrgId()) {
                currentOrg = org;
                break;
            }
        }
        info.setCurrentOrg(currentOrg);
        // 记录登录信息到session
        this.platformService.setLoginUser(info.getUser(), info.getCurrentOrg(), info.getOrgs());
        String token = TokenUtils.createToken();
        redisService.set("xzx:user:loginToken:"+info.getUser().getId(), token);
        //Gson gson = new Gson();
        Map<String,Object> pMap = new HashMap<>();
        pMap.put("userId",info.getUser().getId());
        pMap.put("token",token);
        pMap.put("userType",map.get("userType"));
        pMap.put("jsessionid",jsessionid);
        pMap.put("cityTown",map.get("cityTown"));
        list.add(pMap);
        resultMap.put("code",0);
        resultMap.put("data",list);
        //list.add(resultMap);
        //redisService.remove("xzx:user:verify:code");
        //放入用户ID及sessionId对应关系
        redisService.set(RedisKeyConstant.USER_SESSION_KEY +info.getUser().getId(),request.getSession().getId());
        return JsonResult.success(resultMap);
    }
 
 
    @PostMapping(Constants.ADMIN_VIEW_PREFIX+"/logoutApi.do")
    @ApiOperation(value="退出登录", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "session", value = "需要把sessionId放入请求头", required = true, dataType = "String")
    })
    @PassToken
    public JsonResult<String> logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Enumeration eum = session.getAttributeNames();
        while(eum.hasMoreElements()) {
            String key = (String)eum.nextElement();
            session.removeAttribute(key);
        }
        return JsonResult.success("登出成功!");
 
    }
 
    @PostMapping(Constants.ADMIN_VIEW_PREFIX+"/updatePassword.do")
    @ApiOperation(value="修改用户密码", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "oldPassword", value = "旧密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "password", value = "新密码", required = true, dataType = "String")
    })
    public JsonResult<String> updatePassword(HttpServletRequest request,@RequestBody AppRoleModel model) {
        CoreUser user =platformService.getCurrentUser();
        CityPartner partner = cityPartnerService.queryById(user.getId());
        if(null!=partner){
            //验证老密码
            if(partner.getPassword().equals(model.getOldPassword())){
                String salt1 =  RandomUtil.randomString(16);
                String newPassword =SecurityUtil.encrypt(salt1,model.getPassword());
                partner.setPassword(newPassword);
                partner.setSalt(salt1);
                cityPartnerService.updatePartner(partner);
            }else{
                if(null!=partner.getSalt()&&!"".equals(partner.getSalt())){
                    String oldPassword = SecurityUtil.decrypt(partner.getSalt(),partner.getPassword());
                   if(oldPassword.equals(model.getOldPassword())){
                       String salt1 =  RandomUtil.randomString(16);
                       String newPassword =SecurityUtil.encrypt(salt1,model.getPassword());
                       partner.setPassword(newPassword);
                       partner.setSalt(salt1);
                       cityPartnerService.updatePartner(partner);
                   }else{
                       return JsonResult.failMessage("老密码不正确!");
                   }
                }else{
                    return JsonResult.failMessage("老密码不正确!");
                }
            }
        }else{
             user = userService.getCoreUserById(user.getId());
            //验证老密码
            if(user.getPassword().equals(model.getOldPassword())){
                String salt1 =  RandomUtil.randomString(16);
                String newPassword =SecurityUtil.encrypt(salt1,model.getPassword());
                user.setSalt(salt1);
                user.setPassword(newPassword);
                userService.update(user);
            }else{
                if(null!=user.getSalt()&&!"".equals(user.getSalt())){
                    String oldPassword = SecurityUtil.decrypt(user.getSalt(),user.getPassword());
                    if(oldPassword.equals(model.getOldPassword())){
                        String salt1 =  RandomUtil.randomString(16);
                        String newPassword =SecurityUtil.encrypt(salt1,model.getPassword());
                        user.setPassword(newPassword);
                        user.setSalt(salt1);
                        userService.update(user);
                    }else{
                        return JsonResult.failMessage("老密码不正确!");
 
                    }
                }else{
                    return JsonResult.failMessage("老密码不正确!");
 
                }
            }
 
        }
 
        OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                .methodName(Constants.ROLE_MODUL_NAME).operateAction("修改用户密码-"+user.getId()).build();
        mqUtil.sendApp(build);
 
           return JsonResult.success("修改密码成功!");
    }
    /**
     * 菜单接口
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX+"/menu/userMenu.do")
    @ApiOperation(value="用户获取菜单", notes="用户跨域获取权限菜单")
    public JsonResult<Map<String, Object>> userMenu(){
        CoreUser user =platformService.getCurrentUser();
        //查询角色
        //CoreUserRole coreRole =coreRoleService.getRoleByUserId(user.getId(),user.getOrgId());
        //List<Long> dbs = functionConsoleService.getFunctionByRole(coreRole.getRoleId());
        //List<Long> dbs = functionConsoleService.getFunctionByRole(user.getRoleId());
        Map<String,Object> resultMap = new HashMap<>();
        List<MenuNodeView> allView= new ArrayList<>();
        List<MenuNodeView> view=new ArrayList<>();
        MenuItem allItem = platformService.getAllMenuItem();
        //判断是否是合伙人
        CityPartner partner = cityPartnerService.queryById(user.getId());
 
        if(null!=partner){
           /* String pUid = partner.getUserId();
            pUid =  pUid.substring(pUid.length()-6,pUid.length());
            CityPartner pn = xzxCityPartnerService.queryEntityByUserIds(pUid);
            String userId = pn.getUserId().substring(pn.getUserId().length()-6,pn.getUserId().length());*/
            MenuItem menuItem = platformService.getPartnerMenuItem(partner.getId(), user.getOrgId());
            view = this.build1(menuItem);
          }else{
              MenuItem menuItem = platformService.getMenuItem(user.getId(), user.getOrgId());
            view = this.build1(menuItem);
          }
                  allView = this.build1(allItem);
                  resultMap.put("user",user);
                  resultMap.put("menu",view);
                  resultMap.put("allView",allView);
        return JsonResult.success(resultMap);
    }
 
 
    private List<MenuNodeView> build1(MenuItem node) {
        List<MenuItem> list = node.getChildren();
        if (list.size() == 0) {
            return Collections.EMPTY_LIST;
        }
 
        List<MenuNodeView> views = new ArrayList<MenuNodeView>(list.size());
 
        for (MenuItem item : list) {
            MenuNodeView view = new MenuNodeView();
            view.setCode(item.getData().getCode());
            view.setName(item.getData().getName());
            view.setIcon(item.getData().getIcon());
            view.setId(item.getData().getId());
            if(null!=item.getCreateTime()&&!"".equals(item.getCreateTime())){
                view.setCreateTime(item.getCreateTime());
 
            }
            //view.setPath((String) item.getData().get("accessUrl"));
            List<MenuNodeView> children = this.build1(item);
            view.setChildren(children);
            views.add(view);
        }
        return views;
    }
 
    /**
     * 用户管理列表接口
     * @param
     * @return
     */
    @PostMapping( "/admin/front/user/userList.do")
    @ApiOperation(value="用户管理列表接口", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "code", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "name", value = "姓名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "orgId", value = "部门", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "state", value = "状态", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "jobType0", value = "职务", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "jobType1", value = "职务明细", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "startTime", value = "创建日期开始时间", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "endTime", value = "创建日期结束时间", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "limit", value = "每页数据个数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "page", value = "页码", required = true, dataType = "int")
    })
    public JsonResult<List<CoreUserModel>> userList(@RequestBody CoreUserModel query) {
        //userConsoleService.queryByCondtion(query);
        PageInfo<CoreUserModel> pageInfo = userConsoleService.queryByCondtion(query);
        List<CoreUserModel> list = pageInfo.getList();
        if(CollUtil.isNotEmpty(list)) {
            for (CoreUserModel model : list) {
                if(null!=model.getCreateTime()){
                    model.setCreateTimeStr(sdf.format(model.getCreateTime()));
 
                }
                //查询角色Id
                CoreUserRole coreRole = coreRoleService.getRoleByUserId(model.getId(), model.getOrgId());
                if (null != coreRole) {
                    model.setRoleId(coreRole.getRoleId());
                    model.setOrgName(orgConsoleService.queryById(model.getOrgId()).getName());
                    model.setJobType0Text(coreDictService.findNameByType(model.getJobType0()));
                    model.setJobType1Text(coreDictService.findNameByType(model.getJobType1()));
                    model.setStateText(coreDictService.findNameByType(model.getState()));
                }
            }
        }
        return JsonResult.successx(pageInfo.getList(),pageInfo.getTotal()+"");
    }
 
    /**
     * 用户管理删除操作
     * @param user
     * @return
     */
    @PostMapping("/admin/front/user/userDelete.do")
    @ApiOperation(value="用户管理(删除))", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "id", value = "用户Id", required = true, dataType = "Long")
    })
    public JsonResult userDelete(@RequestBody CoreUser user,HttpServletRequest request) {
        List<Long> dels = new ArrayList<>();
        dels.add(user.getId());
        userConsoleService.batchDelSysUser(dels);
 
        OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                .methodName(Constants.ROLE_MODUL_NAME).operateAction("用户管理(删除)-"+user.getId()).build();
        mqUtil.sendApp(build);
 
 
        return JsonResult.success();
    }
 
    /**
     * 用户修改
     * @param
     * @return
     */
    @PostMapping( "/admin/front/user/userUpdate.json")
    @ApiOperation(value="用户管理(修改))", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "id", value = "用户Id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query", name = "code", value = "用户编号不能为空", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名不能为空", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "orgId", value = "组织机构id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query", name = "password", value = "登陆密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "state", value = "状态(1:启用,2:禁用)", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "jobType0", value = "扩展例子", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "jobType1", value = "扩展例子1", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "attachmentId", value = "用户的个人资料附件,保存到Core_File 表里", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "roleId", value = "角色Id", required = true, dataType = "Long")
    })
    public JsonResult update(@RequestBody CoreUserModel model,HttpServletRequest request) {
        coreUserService.update(model);
        OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                .methodName(Constants.ROLE_MODUL_NAME).operateAction("用户管理(修改)-"+model.getId()).build();
        mqUtil.sendApp(build);
        return JsonResult.success();
    }
 
    /**
     * 用户添加
     * @param user
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/user/userAdd.json")
    @ApiOperation(value="用户管理(添加))", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "code", value = "用户编号不能为空", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名不能为空", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "orgId", value = "组织机构id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query", name = "password", value = "登陆密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "state", value = "状态(1:启用,2:禁用)", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "jobType0", value = "扩展例子", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "jobType1", value = "扩展例子1", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "attachmentId", value = "用户的个人资料附件,保存到Core_File 表里", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "roleId", value = "角色Id", required = true, dataType = "Long")
 
    })
    public JsonResult<Long> userAdd(@RequestBody CoreUserModel user,HttpServletRequest request) {
        if (!platformService.isAllowUserName(user.getCode())) {
            return JsonResult.failMessage("不允许的注册名字 " + user.getCode());
        }
        user.setCreateTime(new Date());
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        CoreUser coreUser = objectMapper.convertValue(user, CoreUser.class);
 
        coreUser.setPassword("123456");
        String salt1 =  RandomUtil.randomString(16);
        String newPassword =SecurityUtil.encrypt(salt1,"123456");
        coreUser.setPassword(newPassword);
        coreUser.setSalt(salt1);
        userConsoleService.saveUser(coreUser,user.getRoleId());
        OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                .methodName(Constants.ROLE_MODUL_NAME).operateAction("用户管理(添加)-"+user.getId()).build();
        mqUtil.sendApp(build);
 
        return JsonResult.success(user.getId());
    }
 
    /**
     * 用户所在公司的组织机构树
     *
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/org/userOrg.do")
    @ApiOperation(value="用户所在公司的组织机构树", notes="test: 仅0有正确返回")
    public JsonResult<List<OrgItem>> getUserCompany(HttpServletRequest request) {
        List<OrgItem> list = new ArrayList<>();
        OrgItem orgItem = platformService.getUserOrgTree();
        list.add(orgItem);
        return JsonResult.success(list);
    }
 
    /**
     * 组织机构列表  分页
     * @param condtion 查询条件
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/org/organizationList.do")
    @ApiOperation(value="组织机构列表  分页", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "code", value = "用户编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "type", value = "机构类型(1 集团 2 公司,3 部门,4 小组)", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "page", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "limit", value = "每页条数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "parentOrgId", value = "上一级机构", required = true, dataType = "String")
    })
    public JsonResult<List<CoreOrg>> organizationList(@RequestBody OrgQuery condtion) {
        PageInfo<CoreOrg> pageInfo = orgConsoleService.queryByCondtion(condtion);
        return JsonResult.successx(pageInfo.getList(),pageInfo.getTotal()+"");
    }
 
    /**
     * 保存组织机构数据
     * @param org
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/org/orgSave.json")
    @ApiOperation(value="保存组织机构数据", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "code", value = "用户编号不能为空", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名不能为空", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "type", value = "机构类型(1 集团 2 公司,3 部门,4 小组)", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query", name = "parentOrgId", value = "上一级机构", required = true, dataType = "String")
    })
    public JsonResult<Long> orgSave(@RequestBody CoreOrg org,HttpServletRequest request) {
        org.setCode(org.getName());
        org.setCreateTime(new Date());
        org.setDelFlag(0);
        orgConsoleService.save(org);
        platformService.clearOrgCache();
 
        OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                .methodName(Constants.ROLE_MODUL_NAME).operateAction("保存组织机构数据-"+org.getId()).build();
        mqUtil.sendApp(build);
 
 
        return JsonResult.success(org.getId());
    }
 
    /**
     * 更新数据
     * @param org
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/org/orgUpdate.json")
    @ApiOperation(value="更新组织机构数据", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "id", value = "用户编号不能为空", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query", name = "code", value = "用户编号不能为空", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名不能为空", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "type", value = "机构类型(1 集团 2 公司,3 部门,4 小组)", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query", name = "parentOrgId", value = "上一级机构", required = true, dataType = "String")
    })
    public JsonResult<String> orgUpdate(@RequestBody CoreOrg org,HttpServletRequest request){
        //判断是否为总公司,总公司不能选择他自己作为他的父类
        String orgId = org.getId()+"";
        CoreOrg  co  = orgConsoleService.queryById(org.getId());
 
        if(co.getParentOrgId()==null){
            if(org.getParentOrgId()!=null){
                if(org.getParentOrgId().equals(org.getId())){
                    return JsonResult.failMessage("保存失败,不能选择他自己作为父类");
                }
            }
        }else{
            if(co.getId().equals(org.getParentOrgId())){
                return JsonResult.failMessage("保存失败,不能选择他自己作为父类");
            }
        }
 
        boolean success = orgConsoleService.updateTemplate(org);
        if (success) {
            platformService.clearOrgCache();
            OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                    .methodName(Constants.ROLE_MODUL_NAME).operateAction("更新组织机构数据-"+org.getId()).build();
            mqUtil.sendApp(build);
 
            return JsonResult.successMessage("保存成功");
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }
 
 
    /**
     *字典列表
     * @param
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/typeValue.do")
    @ApiOperation(value="字典列表", notes="test: 仅0有正确返回")
    public JsonResult<List<Map<String,Object>>> typeValue(@RequestBody CoreDict dict) {
        List<CoreDict> list =coreDictService.findAllByType(dict.getType());
        List<Map<String,Object>> result = new ArrayList<>();
        for (CoreDict d:list) {
            List<Map<String,Object>> children = new ArrayList<>();
            List<CoreDict> childlist = coreDictService.findChildByParent(d.getId());
            for (CoreDict cmap:childlist) {
                Map<String,Object> m = new HashMap<>();
                m.put("id",cmap.getValue());
                m.put("name",cmap.getName());
                children.add(m);
            }
            Map<String,Object> map = new HashMap<>();
            map.put("id",d.getValue());
            map.put("name",d.getName());
            if(null!=children&&children.size()>0){
                map.put("childen",children);
            }
            result.add(map);
        }
        return JsonResult.success(result);
    }
 
    /**
     * 列表页、 分页数据
     *
     * @param condtion
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/role/userRoleList.do")
    @ApiOperation(value="角色管理 列表页、 分页数据", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "page", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "limit", value = "每页条数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "code", value = "编码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "name", value = "名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "type", value = "业务角色类型(1:操作角色,2:工作流角色)", required = true, dataType = "String")
    })
    public JsonResult<List<CoreRole>> userRoleList(@RequestBody RoleQuery condtion) {
        PageInfo<CoreRole> pageInfo = roleConsoleService.queryByCondtion(condtion);
        return JsonResult.successx(pageInfo.getList(),pageInfo.getTotal()+"");
    }
    /**
     * 管理员重置用户密码
     *
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/changeUserPassword.json")
    @ApiOperation(value="管理员重置用户密码", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "id", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query", name = "password", value = "密码", required = true, dataType = "String")
    })
    public JsonResult changeUserPassword(@RequestBody CoreUser user,HttpServletRequest request) {
        userConsoleService.resetPassword(user.getId(), user.getPassword());
 
        OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                .methodName(Constants.ROLE_MODUL_NAME).operateAction("管理员重置用户密码-"+user.getId()).build();
        mqUtil.sendApp(build);
 
        return new JsonResult().success();
    }
 
    /**
     * 保存添加角色
     *
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/role/addRole.json")
    @ApiOperation(value="角色管理添加角色", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "code", value = "角色编码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "name", value = "角色名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "type", value = "角色类型,系统的角色,工作流角色(R0,R1...)", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "ids", value = "角色Id,多个用逗号隔开", required = true, dataType = "String")
    })
    public JsonResult addRole(@RequestBody CoreRole role,HttpServletRequest request) {
        CoreRole role1 = roleConsoleService.queryByCode(role.getCode());
        if (role1 != null) {
            return JsonResult.failMessage("用户编号已存在");
        }
        JsonResult result = new JsonResult();
        role.setCreateTime(new Date());
        role.setDelFlag("0");
        roleConsoleService.save(role);
        platformService.clearFunctionCache();
 
        OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                .methodName(Constants.ROLE_MODUL_NAME).operateAction("角色管理添加角色-"+role.getId()).build();
        mqUtil.sendApp(build);
 
 
        return result.success();
    }
 
    /**
     * 更新角色
     *
     * @param role
     * @return
     */
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/role/updateRole.json")
    @ApiOperation(value="角色管理更新角色", notes="test: 仅0有正确返回")
    public JsonResult<String> updateRole(@RequestBody CoreRole role,HttpServletRequest request) {
        role.setDelFlag("0");
        boolean success = roleConsoleService.update(role);
        if (success) {
            platformService.clearFunctionCache();
 
            OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                    .methodName(Constants.ROLE_MODUL_NAME).operateAction("角色管理更新角色-"+role.getId()).build();
            mqUtil.sendApp(build);
 
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/role/deleteRole.json")
    @ApiOperation(value="角色管理删除角色", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "ids", value = "角色Id", required = true, dataType = "String")
    })
    public JsonResult deleteRole(@RequestBody CoreRoleModel model,HttpServletRequest request) {
        String ids = model.getIds();
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
 
        List<Long> idList = ConvertUtil.str2longs(ids);
        Long id = idList.get(0);
        CoreRole cr = roleConsoleService.queryById(id);
        cr.setDelFlag("1");
        roleConsoleService.update(cr);
 
        OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                .methodName(Constants.ROLE_MODUL_NAME).operateAction("角色管理删除角色-"+model.getIds()).build();
        mqUtil.sendApp(build);
 
        return new JsonResult().success();
    }
 
    @PostMapping(Constants.ADMIN_VIEW_PREFIX + "/dictList.json")
    @ApiOperation(value="组织机构列表", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "type", value = "类型(org_type:组织机构)", required = true, dataType = "String")
    })
    public JsonResult<List<Map<String,Object>>> dictList(@RequestBody CoreDictQueryModel condtion)
    {
        condtion.setPage(Integer.parseInt("1"));
        condtion.setLimit(Integer.parseInt("100"));
        //PageQuery page = condtion.getPageQuery();
        PageInfo info=coreDictService.queryByCondition(condtion);
        return JsonResult.successx(info.getList(),info.getTotal()+"");
    }
 
 
 
}