fix
Helius
2021-08-05 8bbb027dc36b3f3c7f2d505fc75180261fb4d640
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
package com.xzx.gc.user.service;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.common.Result;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.OrderEnum;
import com.xzx.gc.common.constant.UserEnum;
import com.xzx.gc.common.dto.CommonDto;
import com.xzx.gc.common.exception.PlatformException;
import com.xzx.gc.common.exception.RestException;
import com.xzx.gc.common.utils.*;
import com.xzx.gc.common.utils.gdmap.GdTraceUtil;
import com.xzx.gc.entity.*;
import com.xzx.gc.model.admin.PunchChannelModel;
import com.xzx.gc.model.admin.UserEditroleModel;
import com.xzx.gc.model.admin.UserModel;
import com.xzx.gc.user.dto.RoleDto;
import com.xzx.gc.user.mapper.AccountMapper;
import com.xzx.gc.user.mapper.OtherUserMapper;
import com.xzx.gc.user.mapper.UserMapper;
import com.xzx.gc.user.mapper.UserRoleMapper;
import com.xzx.gc.util.PageQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
 
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.*;
 
@Service
@Transactional
@Slf4j
public class UserRoleService {
 
 
    @Autowired
    private PartnerGaodeService partnerGaodeService;
 
    @Autowired
    private UserRoleMapper userRoleMapper;
 
    @Autowired
    private PartnerAccountService partnerAccountService;
 
    @Autowired
    private PartnerAccountLogService partnerAccountLogService;
 
    @Autowired
    private OtherUserService otherUserService;
 
 
    @Autowired
    private OtherUserMapper otherUserMapper;
 
    @Autowired
    private TokenUtil tokenUtil;
 
    @Autowired
    private AccountMapper accountMapper;
 
 
    @Autowired
    private AccountService accountService;
 
    @Autowired
    private PartnerTraceService partnerTraceService;
 
 
    @Autowired
    private ConfigService configService;
 
 
    @Autowired
    private OrderService orderService;
 
    @Autowired
    private IdUtils idUtils;
 
    @Autowired
    private BusinessUtil businessUtil;
 
 
    @Autowired
    private AddressLevelService addressLevelService;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private PartnerFenceService partnerFenceService;
 
    @Autowired
    private CityPartnerService cityPartnerService;
 
    @Autowired
    private UserMapper userMapper;
 
    public List<UserRole> findByRoleCode(String code){
 
        Example example=new Example(UserRole.class);
 
        Example.Criteria criteria = example.createCriteria();
 
        if(StrUtil.isNotBlank(code)){
            criteria.andEqualTo("roleCode",code);
        }
 
        criteria.andEqualTo("delFlag",Constants.DEL_NOT_FLAG);
        List<UserRole> userRoles = userRoleMapper.selectByExample(example);
        return userRoles;
    }
 
    public List<UserRole> findByRoleCodes(String codes){
 
        Example example=new Example(UserRole.class);
 
        Example.Criteria criteria = example.createCriteria();
 
        criteria.andIn("roleCode",Arrays.asList(codes.split(",")));
 
        criteria.andEqualTo("delFlag",Constants.DEL_NOT_FLAG);
        List<UserRole> userRoles = userRoleMapper.selectByExample(example);
        return userRoles;
    }
 
 
    public Result edit(RoleDto roleDto){
 
 
        if((roleDto.getOldUserType().equals(CommonEnum.运营员.getValue())||roleDto.getUserType().equals(CommonEnum.运营员.getValue()))&&!roleDto.getUserType().equals(roleDto.getOldUserType())) {
            return Result.error(-1,"运营员不能更改");
        }
 
 
        //编辑 除了运营员都需要判断金额>0 以及可使用额度==固定额度
        if(roleDto.getOldUserType().equals(CommonEnum.回收员.getValue())){
            List<OrderInfo> list1 =   orderService.findByManageReceiver(roleDto.getUserId());
            if(CollUtil.isNotEmpty(list1)){
                return Result.error(-1,"当前回收员存在未完成的订单");
            }
        }
 
        if(!roleDto.getOldUserType().equals(CommonEnum.运营员.getValue())&&!roleDto.getUserType().equals(roleDto.getOldUserType())) {
            AccountInfo byUserId = accountService.findByUserId(roleDto.getUserId());
            String money = byUserId.getMoney();
            BigDecimal fixedLimit = Convert.toBigDecimal(byUserId.getFixedLimit(),Constants.MONEY_INIT);
            BigDecimal overdraftLimit =Convert.toBigDecimal(byUserId.getOverdraftLimit(),Constants.MONEY_INIT);
            if (StrUtil.isNotBlank(money) && Convert.toBigDecimal(money, Constants.MONEY_INIT).compareTo(BigDecimal.ZERO) != 0) {
                return Result.error(-1, "当前用户还存在未提现的金额:" + money + "元");
            }
 
            if(CommonEnum.回收员.getValue().equals(roleDto.getOldUserType())) {
                if (overdraftLimit.compareTo(fixedLimit) != 0) {
                    return Result.error(-1, "当前用户可使用额度与固定额度不一致");
                }
            }
        }
 
 
        //更换了身份
        if(!roleDto.getUserType().equals(roleDto.getOldUserType())){
            //删除当前用户
            OtherUserInfo otherUserInfo=new OtherUserInfo();
            otherUserInfo.setUserId(roleDto.getUserId());
            otherUserInfo.setDelFlag(Constants.DEL_FLAG);
            otherUserMapper.updateByPrimaryKeySelective(otherUserInfo);
 
            //如果是打包员或者入库员 其账户下的可用额度将分别退还到打包站的账户和合伙人的账户
            OtherUserInfo byId = otherUserService.findById(roleDto.getUserId());
            String partnerId = byId.getPartnerId();
            AccountInfo byUserId = accountService.findByUserId(byId.getUserId());
            if(byUserId!=null){
                String overdraftLimit = byUserId.getOverdraftLimit();
                partnerAccountService.updateAddMoneyByPartnerId(partnerId,Convert.toBigDecimal(overdraftLimit));
 
                PartnerAccountLog partnerAccountLog=new PartnerAccountLog();
                partnerAccountLog.setPartnerId(partnerId);
                partnerAccountLog.setFlowNo(roleDto.getUserId());
                partnerAccountLog.setType(OrderEnum.人员变动.getValue());
                partnerAccountLog.setMoney(overdraftLimit);
                partnerAccountLogService.add(partnerAccountLog);
            }
 
 
 
            //删除账户
            accountService.deleteByUserId(roleDto.getUserId());
 
 
            //先删除再添加
            //删除终端
            if (CommonEnum.回收员.getValue().equals(roleDto.getOldUserType())) {
                partnerTraceService.deleteTerimainal(partnerId,OrderEnum.回收员订单轨迹.getValue(),byId.getTerminalId());
                partnerTraceService.deleteTerimainal(partnerId,OrderEnum.回收员全天轨迹.getValue(),byId.getTerminalDayId());
            } else if (CommonEnum.推广员.getValue().equals(roleDto.getOldUserType())) {
                partnerTraceService.deleteTerimainal(partnerId,OrderEnum.推广员全天轨迹.getValue(),byId.getTerminalId());
            }
            //添加
            add(roleDto,partnerId);
            tokenUtil.delete(roleDto.getUserId());
        }else {
            OtherUserInfo otherUserInfo=new OtherUserInfo();
            BeanUtil.copyProperties(roleDto,otherUserInfo);
            if(StrUtil.isNotBlank(roleDto.getTownId())) {
                otherUserInfo.setTownId(roleDto.getTownId());
            }
            //赋值省市区
            changeArea(roleDto, otherUserInfo);
            otherUserService.update(otherUserInfo);
        }
 
        return Result.success("");
    }
 
    /**
     * 新增角色
     * @param roleDto
     * @return
     */
    public Result<String> add (RoleDto roleDto,String partnerId){
        if(roleDto.getUserType().equals(CommonEnum.运营员.getValue())){
            return Result.error(-1, "运营员不能新增");
        }
 
        OtherUserInfo otherUserInfo=new OtherUserInfo();
        BeanUtil.copyProperties(roleDto,otherUserInfo);
        otherUserInfo.setPartnerId(partnerId);
        String user = idUtils.generate("USER", 5);
 
        if(StrUtil.isBlank(roleDto.getUserId())) {
            roleDto.setUserId(user);
        }
 
        otherUserInfo.setUserId(user);
        otherUserInfo.setRegistTime(DateUtil.now());
 
        UserRole byCode = findByCode(roleDto.getUserType());
        if(byCode!=null){
            otherUserInfo.setRoleIds(Convert.toStr(byCode.getId()));
        }
 
        if(StrUtil.isNotBlank(roleDto.getPassword())){
            otherUserInfo.setSalt(Constants.PWD_DECRET);
        }
 
        if(StrUtil.isBlank(otherUserInfo.getAvatar())){
            otherUserInfo.setAvatar(Constants.DEFAULT_AVATAR);
        }
 
        //赋值省市区
        changeArea(roleDto,otherUserInfo);
 
        String terimalId=null;
        String terimalDayId=null;
        //创建终端
        if (CommonEnum.回收员.getValue().equals(roleDto.getUserType())) {
            terimalId=partnerTraceService.createTerimainal(partnerId,OrderEnum.回收员订单轨迹.getValue(),user);
            terimalDayId=partnerTraceService.createTerimainal(partnerId,OrderEnum.回收员全天轨迹.getValue(),user);
        } else if (CommonEnum.推广员.getValue().equals(roleDto.getUserType())) {
            terimalId=partnerTraceService.createTerimainal(partnerId,OrderEnum.推广员全天轨迹.getValue(),user);
        }
        otherUserInfo.setTerminalId(terimalId);
        otherUserInfo.setTerminalDayId(terimalDayId);
        otherUserMapper.insertSelective(otherUserInfo);
 
 
        //增加一个默认账户
        AccountInfo accountInfo =new AccountInfo();
        accountInfo.setAccountId(idUtils.generate("ZH",4));
        accountInfo.setAccountName(roleDto.getMobilePhone());
        accountInfo.setUserId(user);
        accountInfo.setFreezeMoney("0");
        accountInfo.setWithdrawMoney("0");
        accountInfo.setMoney("0");
 
        ConfigInfo configInfo=null;
        if(roleDto.getUserType().equals(CommonEnum.回收员.getValue())){
             configInfo = configService.findByCode("OVERDRAW_PRICE");
        }else if(roleDto.getUserType().equals(CommonEnum.入库员.getValue())){
             configInfo = configService.findByCode("WAREHOUSING_PRICE");
        }else{
            configInfo=new ConfigInfo();
            configInfo.setConfigValue(Constants.MONEY_INIT.toString());
        }
        accountInfo.setOverdraftLimit(configInfo.getConfigValue());
        accountInfo.setFixedLimit(configInfo.getConfigValue());
        accountInfo.setDelFlag(0);
        accountInfo.setErrorTimes(0);
        accountInfo.setIsProhibit("0");
        accountInfo.setCreateTime(DateUtil.now());
        accountInfo.setUpdateTime(DateUtil.now());
        accountInfo.setCollectScore("0");
        accountMapper.insertSelective(accountInfo);
 
        return Result.success(partnerId);
    }
 
 
    private void changeArea(RoleDto roleDto,OtherUserInfo otherUserInfo){
        String areaId = roleDto.getAreaId();
        String areaName = roleDto.getAreaName();
        int level = roleDto.getLevel();
        if(level==2){
            otherUserInfo.setCityId(Integer.parseInt(areaId));
            otherUserInfo.setCity(areaName);
        }else if(level==3){
            otherUserInfo.setTownshipId(Integer.parseInt(areaId));
            otherUserInfo.setTownship(areaName);
            AddressLevelInfo addressLevelInfo=new AddressLevelInfo();
            addressLevelInfo.setLevel3Id(areaId);
            List<AddressLevelInfo> byAny = addressLevelService.findByAny(addressLevelInfo);
            otherUserInfo.setProvinceId(Convert.toInt(byAny.get(0).getLevel1Id()));
            otherUserInfo.setProvince(byAny.get(0).getLevel1Name());
            otherUserInfo.setCityId(Convert.toInt(byAny.get(0).getLevel2Id()));
            otherUserInfo.setCity(byAny.get(0).getLevel2Name());
        }
    }
 
 
 
 
 
    /**
     * 根据code查询
     * @param code
     * @return
     */
    public UserRole findByCode (String code){
        UserRole userRole=new UserRole();
        userRole.setRoleCode(code);
        userRole.setDelFlag(Constants.DEL_NOT_FLAG+"");
        UserRole userRole1 = userRoleMapper.selectOne(userRole);
        return userRole1;
    }
 
 
    public PageInfo<UserRole> queryByCondition(PageQuery query){
        PageHelper.startPage(Convert.toInt(query.getPageNumber()),Convert.toInt(query.getPageSize()));
        List<UserRole> ret =  userRoleMapper.queryByCondition(query);
        PageInfo<UserRole> pageInfo=new PageInfo<>(ret);
        return pageInfo;
    }
 
 
    public void batchDelXzxUserRoleInfo(List<Long> ids){
        try {
            userRoleMapper.batchDelXzxUserRoleInfoByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除XzxUserRoleInfo失败", e);
        }
    }
 
    public String queryPunchChannel(){
        return userRoleMapper.queryPunchChannel();
    }
 
    public void updatePunchChannel(PunchChannelModel model){
        model.setConfigTypeCode("CODE_SCAN_RK");
        userRoleMapper.updatePunchChannel(model);
    }
 
    public UserRole queryMaxAppRole(){
        return userRoleMapper.queryMaxAppRole();
    }
 
    public void save(UserRole xzxUserRoleInfo) {
        userRoleMapper.insertSelective(xzxUserRoleInfo);
    }
 
    public UserRole queryById(Long id) {
        return userRoleMapper.selectByPrimaryKey(id);
    }
 
    public boolean update(UserRole roleInfo) {
        return userRoleMapper.updateByPrimaryKeySelective(roleInfo)>0?true:false;
    }
 
    /**
     * 根据手机号码查询用户
     * @param model
     * @return
     */
    public Map<String, Object> queryEditroleApi(UserEditroleModel model){
        List<String> partnerIds = new ArrayList<>();
        if(null!=model.getPartnerId()&&!"".equals(model.getPartnerId())){
            partnerIds.add(model.getPartnerId());
        }else{
            partnerIds = cityPartnerService.queryPartnerByCurrent();
        }
        model.setPartnerIds(partnerIds);
        PageHelper.startPage(model.getPage(),model.getLimit());
        List<UserEditroleModel> list = userRoleMapper.queryEditroleApi(model);
        PageInfo<UserEditroleModel> pageInfo = new PageInfo<>(list);
        for (UserEditroleModel userModel:list) {
            if(userModel.getUserTypeValue().equals(CommonEnum.打包站运营员.getValue())){
                userModel.setUserType("打包员");
            }
            if(!StringUtils.isEmpty(userModel.getNickName())){
                    userModel.setNickName(com.xzx.gc.common.utils.StringUtils.decode(userModel.getNickName()));
            }
 
            if(StrUtil.isNotBlank(userModel.getTownshipId())){
                AddressLevelInfo addressLevelInfo=new AddressLevelInfo();
                addressLevelInfo.setLevel3Id(userModel.getTownshipId());
                List<AddressLevelInfo> byAny = addressLevelService.findByAny(addressLevelInfo);
                if(CollUtil.isNotEmpty(byAny)){
                    userModel.setProvinceName(byAny.get(0).getLevel1Name());
                    userModel.setCityName(byAny.get(0).getLevel2Name());
                    userModel.setTownshipName(byAny.get(0).getLevel3Name());
                }
            }
        }
        Map<String, Object> m = new HashMap<>();
        m.put("data", list);
        m.put("count", pageInfo.getTotal());
        m.put("code", 0);
        return  m;
    }
 
    /**
     * 添加角色
     * @param userEditroleModel
     * @return
     */
    public int addEditroleApi(UserEditroleModel userEditroleModel){
        // 判断此用户类型是否已经删除过,如果已经删除了需要重新恢复
        String type =userEditroleModel.getUserTypeValue();
        String phone = userEditroleModel.getMobilePhone();
 
        //修改才会传值
        String userId1 = userEditroleModel.getUserId();
 
        List<OtherUserInfo> byMobile = otherUserService.findByMobileForBidden(phone);
        if(CollUtil.isNotEmpty(byMobile)){
            if(StrUtil.isNotBlank(userId1)){
                //修改
                long count = byMobile.stream().filter(x -> !x.getUserId().equals(userId1)).count();
                if(count>0){
                    throw new RestException("该手机号已存在别的身份");
                }
            }else {
                //添加
                throw new RestException("该手机号已存在别的身份");
            }
        }
 
        String roleId = userEditroleModel.getRoleId();
        String password = userEditroleModel.getPassword();
        String name = userEditroleModel.getName();
        String area = userEditroleModel.getArea();
        String cityId=null;
        String proviceId=null;
        String townshipId=null;
        String city=null;
        String province=null;
        String township=null;
        String storageId = userEditroleModel.getStorageId();
 
        if(StrUtil.isNotBlank(userEditroleModel.getTownshipId())){
            AddressLevelInfo addressLevelInfo=new AddressLevelInfo();
            addressLevelInfo.setLevel3Id(userEditroleModel.getTownshipId());
            List<AddressLevelInfo> byAny = addressLevelService.findByAny(addressLevelInfo);
            proviceId=byAny.get(0).getLevel1Id();
            province=byAny.get(0).getLevel1Name();
            cityId=byAny.get(0).getLevel2Id();
            city=byAny.get(0).getLevel2Name();
            townshipId=byAny.get(0).getLevel3Id();
            township=byAny.get(0).getLevel3Name();
        }
        String partnerId="";
        if(null!=area&&!"".equals(area)){
            String[] areaList= area.split(",");
            PartnerFence byFenceId = partnerFenceService.findByFenceId(areaList[0]);
            partnerId= byFenceId.getPartnerId();
        }
        if(null!=userEditroleModel.getPartnerId()&&!"".equals(userEditroleModel.getPartnerId())){
            partnerId = userEditroleModel.getPartnerId();
        }else{
            List<String> partnerIds = cityPartnerService.queryPartnerByCurrent();
            if(null!=partnerIds&&partnerIds.size()>0){
                userEditroleModel.setPartnerId(partnerIds.get(0));
                partnerId = partnerIds.get(0);
            }
        }
        if(null!=roleId&&!"".equals(roleId)){
            type = findById(Convert.toLong(roleId)).getRoleCode();
        }
 
 
        if(null==area&&"".equals(area)){
            return 0;
        }
 
        UserModel userModels = otherUserMapper.queryUserByUsertype1(phone, type, "1",null,null);
        Snowflake snowflake = IdUtil.getSnowflake(1, 3);
        String id="USER"+snowflake.nextIdStr();
        userEditroleModel.setUserId(id);
        boolean bool = true;
        if(userModels == null){
            // 添加角色需要添加到2个表,一个是用户表,一个是账号表
            // 一个账号不能有相同的角色
            UserModel oldModel = otherUserMapper.queryUserByUsertype1(phone, type, "0",null,null);
            if(oldModel!=null){
                if(type.equals("2")){
                    boolean flag = checkOtherUserByType("4",phone);
                    if(!flag){
                        return -1;
                    }
                }else if(type.equals("4")){
                    boolean flag = checkOtherUserByType("2",phone);
                    if(!flag){
                        return -1;
                    }else{
                        flag = checkOtherUserByType("6",phone);
                        if(!flag){
                            return -2;
                        }
                    }
                }else if(type.equals("6")){
                    boolean flag = checkOtherUserByType("4",phone);
                    if(!flag){
                        return -2;
                    }
                }
                String salt=null;
                String pass=null;
                if(null!=password&&!"".equals(password)){
                    salt =  RandomUtil.randomString(16);
                    pass  = SecurityUtil.encrypt(salt,password);
                }else{
                }
                if(type.equals("4")){
                    otherUserMapper.updateDelflag(phone, type,roleId,name, pass,salt,area,partnerId,cityId,storageId,proviceId,townshipId,province,city,township);
 
                }else{
                    otherUserMapper.updateDelflag(phone, type,roleId,name, pass,salt,area,partnerId,cityId,null,proviceId,townshipId,province,city,township);
 
                }
                CityPartner  partner = cityPartnerService.findById(Convert.toInt(partnerId));
 
                if(oldModel.getUserType().equals("2")){
                    if(type.equals("6")){
                        String sid = partnerTraceService.findByPartnerId(partnerId,Convert.toShort(OrderEnum.回收员订单轨迹.getValue())).getPartnerServiceId();
                        GdTraceUtil.deleteTerminal(partner.getPartnerKey(),sid,oldModel.getTerminalId());
                        sid = partnerTraceService.findByPartnerId(partnerId,Convert.toShort(OrderEnum.推广员全天轨迹.getValue())).getPartnerServiceId();
                        Result<String> terminal = GdTraceUtil.createTerminal(partner.getPartnerKey().split(",")[0], sid, id);
                        if(terminal.getCode()==0){
                            OtherUserInfo otherUserInfo=new OtherUserInfo();
                            otherUserInfo.setUserId(id);
                            otherUserInfo.setTerminalId(terminal.getData());
                            otherUserInfo.setBirthday("");
                            otherUserService.update(otherUserInfo);
                        }
                    }
                }else if(oldModel.getUserType().equals("6")){
                    if(type.equals("2")){
                        String sid =  partnerTraceService.findByPartnerId(partnerId,Convert.toShort(OrderEnum.推广员全天轨迹.getValue())).getPartnerServiceId();
                        GdTraceUtil.deleteTerminal(partner.getPartnerKey(),sid,oldModel.getTerminalId());
                        sid = partnerTraceService.findByPartnerId(partnerId,Convert.toShort(OrderEnum.回收员订单轨迹.getValue())).getPartnerServiceId();
                        Result<String> terminal = GdTraceUtil.createTerminal(partner.getPartnerKey().split(",")[0], sid, id);
                        String terimalId=null;
                        String terimalDayId=null;
                        if(terminal.getCode()==0){
                            terimalId=terminal.getData();
                        }
                        sid = partnerTraceService.findByPartnerId(partnerId,Convert.toShort(OrderEnum.回收员全天轨迹.getValue())).getPartnerServiceId();
                        terminal = GdTraceUtil.createTerminal(partner.getPartnerKey().split(",")[0], sid, id);
                        if(terminal.getCode()==0){
                            terimalDayId=terminal.getData();
                        }
                        OtherUserInfo otherUserInfo=new OtherUserInfo();
                        otherUserInfo.setUserId(id);
                        otherUserInfo.setTerminalDayId(terimalDayId);
                        otherUserInfo.setTerminalId(terimalId);
                        otherUserInfo.setBirthday("");
                        otherUserService.update(otherUserInfo);
 
                    }
                }
 
                // 删除redis 缓存
                // redis为map, key为user id, 先根据手机号码查询所有id, 进行遍历删除
                UserModel userModel = userMapper.queryUserByUsertype(phone, "1", "0",null,null);
                // redisService.removeToken(userModel.getUserId());
                if(null!=userModel){
                    redisUtil.remove("xzx:user:token:"+userModel.getUserId());
                    redisUtil.remove("xzx:user:token:"+id);
                    List<Map<String, Object>> userIds = otherUserMapper.queryCuserByPhoneRedisDelList(phone);
                    for (Map<String,Object> map:userIds) {
                        String userId = map.get("userId").toString();
                        redisUtil.remove("xzx:user:token:"+userId);
                    }
                    redisUtil.remove(phone);
                }
 
                return 1;
            }else{
                if(type.equals("7")){
                    return -2;
                }
 
 
                //新增用户 回收员不能与入库员共存
 
                if(type.equals("2")){
                    boolean flag = checkOtherUserByType("4",phone);
                    if(!flag){
                        return -1;
                    }
                }else if(type.equals("4")){
                    boolean flag = checkOtherUserByType("2",phone);
                    if(!flag){
                        return -1;
                    }
                }
            }
            // 插入用户表
            String salt =  RandomUtil.randomString(16);
            String pass="";
            if(null!=password&&!"".equals(password)){
                pass  = SecurityUtil.encrypt(salt,password);
            }else{
                pass  = SecurityUtil.encrypt(salt,"123456");
            }
            if(type.equals("4")){
                otherUserMapper.addCuserRole(id, phone, pass,type, area,partnerId,name,salt, roleId,cityId,storageId,proviceId,townshipId,province,city,township);
            }else{
                if(userEditroleModel.getUserType().equals("9")){
                    otherUserMapper.addCuserRole(id, phone, pass,type, area,"-1",name,salt, roleId,cityId,null,proviceId,townshipId,province,city,township);
 
                }else{
                    otherUserMapper.addCuserRole(id, phone, pass,type, area,partnerId,name,salt, roleId,cityId,null,proviceId,townshipId,province,city,township);
 
                }
 
            }
 
            //创建终端
            CityPartner  partner = cityPartnerService.findById(Convert.toInt(partnerId));
 
            //创建终端
            if (CommonEnum.回收员.getValue().equals(type)) {
                String sid = partnerTraceService.findByPartnerId(partnerId,Convert.toShort(OrderEnum.回收员订单轨迹.getValue())).getPartnerServiceId();
                Result<String> terminal = GdTraceUtil.createTerminal(partner.getPartnerKey().split(",")[0], sid, id);
                String terimalId=null;
                String terimalDayId=null;
                if(terminal.getCode()==0){
                    terimalId=terminal.getData();
                }
                sid = partnerTraceService.findByPartnerId(partnerId,Convert.toShort(OrderEnum.回收员全天轨迹.getValue())).getPartnerServiceId();
                terminal = GdTraceUtil.createTerminal(partner.getPartnerKey().split(",")[0], sid, id);
                if(terminal.getCode()==0){
                    terimalDayId=terminal.getData();
                }
                OtherUserInfo otherUserInfo=new OtherUserInfo();
                otherUserInfo.setUserId(id);
                otherUserInfo.setTerminalDayId(terimalDayId);
                otherUserInfo.setTerminalId(terimalId);
                otherUserInfo.setBirthday("");
                otherUserService.update(otherUserInfo);
 
            } else if (CommonEnum.推广员.getValue().equals(type)) {
                String sid =partnerTraceService.findByPartnerId(partnerId,Convert.toShort(OrderEnum.推广员全天轨迹.getValue())).getPartnerServiceId();
                Result<String> terminal = GdTraceUtil.createTerminal(partner.getPartnerKey().split(",")[0], sid, id);
                if(terminal.getCode()==0){
                    OtherUserInfo otherUserInfo=new OtherUserInfo();
                    otherUserInfo.setUserId(id);
                    otherUserInfo.setTerminalId(terminal.getData());
                    otherUserInfo.setBirthday("");
                    otherUserService.update(otherUserInfo);
                }
            }
 
 
        }else{
            if(type.equals("7")){
                return -2;
            }
            if(otherUserMapper.queryUserByUsertype1(phone, type, "1", null,null)!=null){
                //如果有账号就修改,没有就新增
                bool = false;
            }
            if(type.equals("2")){
                boolean flag = checkOtherUserByType("4",phone);
                if(!flag){
                    return -1;
                }
            }else if(type.equals("4")){
                boolean flag = checkOtherUserByType("2",phone);
                if(!flag){
                    return -1;
                }
            }
            String salt =null;
            String pass=null;
            if(type.equals("4")){
                otherUserMapper.updateDelflag(phone, type,roleId,name, pass,salt,area,partnerId,cityId,storageId,proviceId,townshipId,province,city,township);
 
            }else{
                if(userEditroleModel.getUserType().equals("9")){
                    otherUserMapper.updateDelflag(phone, type,roleId,name,pass,salt,area,"-1",cityId,null,proviceId,townshipId,province,city,township);
 
                }else{
                    otherUserMapper.updateDelflag(phone, type,roleId,name, pass,salt,area,partnerId,cityId,null,proviceId,townshipId,province,city,township);
 
                }
 
            }
        }
        UserModel u = otherUserMapper.queryUserByUsertype1(phone, type, "0",null,null);
 
        // 添加账号表
        id="ZH"+snowflake.nextIdStr();
        String value;
        if(type.equals("4")){
            value = configService.findByCode("WAREHOUSING_PRICE").getConfigValue();
        }else{
            value = configService.findByCode("OVERDRAW_PRICE").getConfigValue();
        }
 
        if(!bool){
            accountMapper.updateCuserAccount(phone,u.getUserId());
        }else{
            accountMapper.addCuserAccountRole(id, phone, u.getUserId(),value);
        }
        List<Map<String, Object>> userIds = otherUserMapper.queryCuserByPhoneRedisDelList(phone);
        for (Map<String,Object> map:userIds) {
            String userId = map.get("userId").toString();
            redisUtil.remove("xzx:user:token:"+userId);
        }
        redisUtil.remove(phone);
        return 1;
    }
 
    public UserRole findById(Long id){
        return userRoleMapper.selectByPrimaryKey(id);
    }
 
    private boolean checkOtherUserByType(String userType,String mobilePhone){
        List<UserModel> list = otherUserMapper.checkOtherUserByType(userType,mobilePhone);
        if(null!=list&&list.size()>0){
            return false;
        }else{
            return true;
        }
    }
}