xiaoyong931011
2021-06-30 e708a57acad17877869fd3b418f7f9ea529841da
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
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.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
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.constant.UserEnum;
import com.xzx.gc.common.utils.*;
import com.xzx.gc.entity.*;
import com.xzx.gc.model.user.UserGatherApplyListParam;
import com.xzx.gc.model.user.UserGatherAuditParam;
import com.xzx.gc.user.mapper.CoreUserMapper;
import com.xzx.gc.user.mapper.SysItemUserMapper;
import com.xzx.gc.user.mapper.UserGatherApplyMapper;
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.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
@Transactional
public class UserGatherApplyService {
 
 
    @Autowired
    private UserGatherInfoService userGatherInfoService;
 
    @Autowired
    private IdUtils idUtils;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private AddressService addressService;
 
    @Autowired
    private UserGatherApplyMapper userGatherApplyMapper;
    
    @Autowired
    private AddressLevelService addressLevelService;
 
    @Autowired
    private AccountService accountService;
 
    @Autowired
    private HttpServletRequest request;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private TokenUtil tokenUtil;
 
    @Autowired
    private SysEnvironmentalInfoService sysEnvironmentalInfoService;
 
    @Autowired
    private OtherUserService otherUserService;
 
    @Autowired
    private UserRoleService userRoleService;
 
    @Autowired
    private PartnerTraceService partnerTraceService;
 
    @Autowired
    private PartnerGaodeService partnerGaodeService;
 
    @Autowired
    private UserScrapStoreService userScrapStoreService;
 
    @Autowired
    private SysItemUserMapper sysItemUserMapper;
 
    @Autowired
    private UserTargetInfoService userTargetInfoService;
 
    @Autowired
    private PartnerFenceService partnerFenceService;
 
    @Autowired
    private CoreUserMapper coreUserMapper;
 
    public UserGatherApply findByRelationId(String relationId){
        //可能申请多次
        Example example=new Example(UserGatherApply.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("relationId",relationId);
        example.setOrderByClause("create_time desc");
        List<UserGatherApply> userGatherApplies = userGatherApplyMapper.selectByExample(example);
        if(CollUtil.isNotEmpty(userGatherApplies)){
            return userGatherApplies.get(0);
        }
        return null;
    }
 
    public void apply(UserGatherApply userGatherApply) {
 
        //获取省市区
        AddressLevelInfo byAdcode = addressLevelService.findByLocation(userGatherApply.getLongitude(), userGatherApply.getLatitude());
        userGatherApply.setProvinceId(byAdcode.getLevel1Id());
        userGatherApply.setCityId(byAdcode.getLevel2Id());
        userGatherApply.setTownshipId(byAdcode.getLevel3Id());
        userGatherApply.setStatus(CommonEnum.待审核.getValue());
 
        //所属推广员ID
        Short applyOrigin = userGatherApply.getApplyOrigin();
        String createUserId = userGatherApply.getCreateUserId();
 
        /**
         * 设置推广人员
         */
        //小程序
        if(Convert.toShort(1).equals(applyOrigin)){
            if(StrUtil.isNotBlank(createUserId)){
                String mobilePhone = userService.findById(createUserId).getMobilePhone();
                List<UserTargetInfo> byMobile = userTargetInfoService.findByMobile(mobilePhone);
                if(CollUtil.isNotEmpty(byMobile)){
                    userGatherApply.setRelationUserId(byMobile.get(0).getRealUserId());
                }
            }
        }else {
            //安卓
            userGatherApply.setRelationUserId(createUserId);
        }
 
        add(userGatherApply);
    }
 
 
 
    /**
     * 审核集货员通过
     * @param userGatherApply
     */
    private String auditGather(UserGatherApply userGatherApply){
        //添加集货员表
        UserGatherInfo userGatherInfo=new UserGatherInfo();
        BeanUtil.copyProperties(userGatherApply,userGatherInfo);
 
        //查询是否有普通用户身份
        UserInfo byMobileForbidden = userService.findByMobileForbidden(userGatherApply.getMobilePhone());
        String userId;
        if(byMobileForbidden!=null){
            userId=byMobileForbidden.getUserId();
        }else {
            userId=idUtils.generate("YH",3);
        }
        userGatherInfo.setUserId(userId);
        userGatherInfo.setNickName(StringUtils.encode(userGatherInfo.getName()));
        UserGatherInfo byId = userGatherInfoService.findById(userId);
        if(byId==null) {
            userGatherInfoService.add(userGatherInfo);
        }else {
            userGatherInfo.setRegistTime(DateUtil.now());
            userGatherInfo.setDelFlag(Constants.DEL_NOT_FLAG);
            userGatherInfo.setIsProhibit(false);
            userGatherInfoService.updateById(userGatherInfo);
        }
 
        if(byMobileForbidden!=null){
            //更新用户类型
            byMobileForbidden.setUserType(CommonEnum.集货员.getValue());
            byMobileForbidden.setName(userGatherApply.getName());
            userService.updateById(byMobileForbidden);
            //将原所有地址设为非默认
            addressService.updateByUserIdSetNoDefault(userId);
 
            //删除缓存
            redisUtil.del(RedisKeyConstant.TOKEN+userId);
 
        }else {
            //复制到用户表
            UserInfo userInfo = new UserInfo();
            BeanUtil.copyProperties(userGatherInfo, userInfo);
            userService.add(userInfo);
 
            //添加账户
            AccountInfo accountInfo = accountService.init(userGatherApply.getMobilePhone(), userId);
            accountService.add(accountInfo);
        }
 
        //添加地址
        initAddAddress(userGatherApply,userId);
 
 
        //发送短信
//        SmsUtil.sendPoint(userGatherApply.getMobilePhone(), SmsConstant.GATHER_SUCCESS);
 
        return userId;
    }
 
 
 
    private void initAddAddress(UserGatherApply userGatherApply,String userId){
        AddressInfo addressInfo = new AddressInfo();
        BeanUtil.copyProperties(userGatherApply, addressInfo);
        addressInfo.setUserId(userId);
        addressInfo.setFlag(UserEnum.默认地址.getValue());
        addressInfo.setRelaPhone(userGatherApply.getMobilePhone());
        addressInfo.setRelaName(userGatherApply.getName());
        if(UserEnum.集货员类型.getValue().equals(userGatherApply.getType())) {
            //将address_area(定位地址) detail_address小区地址
            AddressLevelInfo addressLevelInfo = addressLevelService.find(userGatherApply.getProvinceId(), userGatherApply.getCityId(), userGatherApply.getTownshipId());
            if(addressLevelInfo!=null){
                addressInfo.setAddressArea(addressLevelInfo.getLevel1Name()+addressLevelInfo.getLevel2Name()+addressLevelInfo.getLevel3Name());
            }
            addressInfo.setDetailAddress(userGatherApply.getAddressArea());
            //小区名称
            addressInfo.setHouseName(userGatherApply.getDetailAddress());
        }
        addressService.add(addressInfo);
    }
 
    /**
     * 审核成为回收员
     * @param userGatherApply
     */
    private String auditOtherUser(UserGatherApply userGatherApply){
        String userId=idUtils.generate("USER",5);
        OtherUserInfo otherUserInfo=new OtherUserInfo();
        otherUserInfo.setUserId(userId);
        otherUserInfo.setMobilePhone(userGatherApply.getMobilePhone());
        String salt=RandomUtil.randomString(16);
//        String password=RandomUtil.randomNumbers(6);
        String password="123456";
        String encrypt = SecurityUtil.encrypt(salt, password);
        otherUserInfo.setPassword(encrypt);
        otherUserInfo.setSalt(salt);
        otherUserInfo.setName(userGatherApply.getName());
        otherUserInfo.setUserType(CommonEnum.回收员.getValue());
 
        //获取省市区
        otherUserInfo.setProvinceId(Convert.toInt(userGatherApply.getProvinceId()));
        otherUserInfo.setCityId(Convert.toInt(userGatherApply.getCityId()));
        otherUserInfo.setTownshipId(Convert.toInt(userGatherApply.getTownshipId()));
 
        //角色ID
        UserRole byCode = userRoleService.findByCode(CommonEnum.回收员.getValue());
        if(byCode!=null){
            otherUserInfo.setRoleIds(Convert.toStr(byCode.getId()));
        }
        otherUserInfo.setIsProhibit(false);
        otherUserInfo.setOriginType(Convert.toShort(1));
 
        //合伙人相关
//        PartnerGaode byTownId = partnerGaodeService.findByTownId(otherUserInfo.getTownshipId().toString());
//        if(byTownId!=null){
//            String partnerId = byTownId.getPartnerId();
//            String terimalId=partnerTraceService.createTerimainal(partnerId,OrderEnum.回收员订单轨迹.getValue(),userId);
//            String terimalDayId=partnerTraceService.createTerimainal(partnerId,OrderEnum.回收员全天轨迹.getValue(),userId);
//            otherUserInfo.setTerminalId(terimalId);
//            otherUserInfo.setTerminalDayId(terimalDayId);
//            otherUserInfo.setPartnerId(partnerId);
//            List<String> fences = partnerFenceService.findFences(partnerId);
//            if(CollUtil.isNotEmpty(fences)){
//                otherUserInfo.setTownId(CollUtil.join(fences,","));
//            }
//        }
        otherUserService.add(otherUserInfo);
 
        //新增账户
        AccountInfo accountInfo = accountService.init(userGatherApply.getMobilePhone(), userId);
        accountService.add(accountInfo);
 
        //发送短信
//        Map<String,String> map=new HashMap<>();
//        map.put("other_number",password);
//        SmsUtil.sendPointByTemplate(userGatherApply.getMobilePhone(),map,SmsConstant.OTHER_SUUCESS_TEMPLATE, 2);
        return userId;
 
    }
 
    /**
     * 审核成为废品站complaintFirstFlag
     * @param userGatherApply
     */
    private String auditScrapStore(UserGatherApply userGatherApply){
        UserScrapStore userScrapStore=new UserScrapStore();
        BeanUtil.copyProperties(userGatherApply,userScrapStore);
        String id=idUtils.generate("FP",6);
        userScrapStore.setId(id);
        userScrapStoreService.add(userScrapStore);
 
        //废品分类
        String itemTypes = userGatherApply.getItemTypes();
        if(StrUtil.isNotBlank(itemTypes)){
            List<SysEnvironmentalInfo> byIds = sysEnvironmentalInfoService.findByIds(itemTypes.split(","));
            List<SysItemUser> list=new ArrayList<>();
            for (SysEnvironmentalInfo byId : byIds) {
                SysItemUser sysItemUser=new SysItemUser();
                sysItemUser.setUserId(id);
                sysItemUser.setItemType(byId.getItemType());
                sysItemUser.setCreateTime(DateUtil.now());
                sysItemUser.setDelFlag(Constants.DEL_NOT_FLAG);
                list.add(sysItemUser);
            }
            if(CollUtil.isNotEmpty(list)) {
                sysItemUserMapper.insertList(list);
            }
        }
 
        //发送短信
//        SmsUtil.sendPoint(userGatherApply.getMobilePhone(), SmsConstant.SCRAP_SUCCESS);
 
        return id;
 
 
    }
 
    public void add(UserGatherApply userGatherApply){
        userGatherApply.setCreateTime(DateUtil.now());
        userGatherApplyMapper.insertSelective(userGatherApply);
    }
 
    public List<UserGatherApply> findByMobileAndType(String mobile,String type){
        UserGatherApply userGatherApply=new UserGatherApply();
        userGatherApply.setMobilePhone(mobile);
        userGatherApply.setType(type);
        return userGatherApplyMapper.select(userGatherApply);
    }
 
    public List<UserGatherApply> list(UserGatherApplyListParam userGatherApplyListParam) {
        Example example=new Example(UserGatherApply.class);
        Example.Criteria criteria = example.createCriteria();
        if(StrUtil.isNotBlank(userGatherApplyListParam.getStatus())){
            criteria.andEqualTo("status",userGatherApplyListParam.getStatus());
        }
 
 
        if(StrUtil.isNotBlank(userGatherApplyListParam.getFilterType())){
            criteria.andEqualTo("type",userGatherApplyListParam.getFilterType());
        }else {
            if(CommonEnum.app.getValue().equals(userGatherApplyListParam.getClientType())){
                criteria.andIn("type",CollUtil.newArrayList(UserEnum.回收员类型.getValue(),UserEnum.废品店类型.getValue()));
            }
        }
 
        if(StrUtil.isNotBlank(userGatherApplyListParam.getTownShipId())){
            criteria.andEqualTo("townshipId",userGatherApplyListParam.getTownShipId());
        }
 
        if(StrUtil.isNotBlank(userGatherApplyListParam.getRelationUserId())){
            criteria.andEqualTo("relationUserId",userGatherApplyListParam.getRelationUserId());
        }
 
        boolean createFlag=true;
        if(StrUtil.isNotBlank(userGatherApplyListParam.getExamineTimeStart())){
            createFlag=false;
            criteria.andCondition("DATE_FORMAT(examine_time,'%Y-%m-%d')>=DATE_FORMAT('"+userGatherApplyListParam.getExamineTimeStart()+"','%Y-%m-%d')");
        }
 
        if(StrUtil.isNotBlank(userGatherApplyListParam.getExamineTimeEnd())){
            createFlag=false;
            criteria.andCondition("DATE_FORMAT(examine_time,'%Y-%m-%d')<=DATE_FORMAT('"+userGatherApplyListParam.getExamineTimeEnd()+"','%Y-%m-%d')");
        }
 
        if(StrUtil.isNotBlank(userGatherApplyListParam.getCreateTimeStart())){
            criteria.andCondition("DATE_FORMAT(create_time,'%Y-%m-%d')>=DATE_FORMAT('"+userGatherApplyListParam.getCreateTimeStart()+"','%Y-%m-%d')");
        }
 
        if(StrUtil.isNotBlank(userGatherApplyListParam.getCreateTimeEnd())){
            criteria.andCondition("DATE_FORMAT(create_time,'%Y-%m-%d')<=DATE_FORMAT('"+userGatherApplyListParam.getCreateTimeEnd()+"','%Y-%m-%d')");
        }
 
        if(createFlag) {
            example.setOrderByClause("create_time desc");
        }else {
            example.setOrderByClause("examine_time desc");
        }
        List<UserGatherApply> userGatherApplies = userGatherApplyMapper.selectByExample(example);
 
        for (UserGatherApply userGatherApply : userGatherApplies) {
            String itemTypes = userGatherApply.getItemTypes();
            //分类
            if(StrUtil.isNotBlank(itemTypes)){
                String[] split = itemTypes.split(",");
                List<SysEnvironmentalInfo> byIds = sysEnvironmentalInfoService.findByIds(split);
                if(CollUtil.isNotEmpty(byIds)) {
                    userGatherApply.setItemTypeNames(byIds.stream().map(x->x.getTitle()).collect(Collectors.joining(",")));
                }
            }
            //所属区域名称
            if(StrUtil.isNotBlank(userGatherApply.getTownshipId())) {
                AddressLevelInfo addressLevelInfo = addressLevelService.find(userGatherApply.getProvinceId(), userGatherApply.getCityId(), userGatherApply.getTownshipId());
                if (addressLevelInfo != null) {
                    userGatherApply.setTownshipName(addressLevelInfo.getLevel3Name());
                }
            }
 
            //所属推广员名称
            if(StrUtil.isNotBlank(userGatherApply.getRelationUserId())){
                String name = otherUserService.findById(userGatherApply.getRelationUserId()).getName();
                userGatherApply.setRelationUserName(name);
            }
 
            //审核人名称
            //所属推广员名称
            if(StrUtil.isNotBlank(userGatherApply.getUpdateUserId())) {
                OtherUserInfo byId = otherUserService.findById(userGatherApply.getUpdateUserId());
                if(byId!=null){
                    userGatherApply.setUpdateUserName(byId.getName());
                }else {
                    CoreUser coreUser = coreUserMapper.selectByPrimaryKey(userGatherApply.getUpdateUserId());
                    if(coreUser!=null){
                        userGatherApply.setUpdateUserName(coreUser.getName());
                    }
                }
            }
 
            String mobilePhone = userGatherApply.getMobilePhone();
            UserGatherApply userGatherApply1=new UserGatherApply();
            userGatherApply1.setMobilePhone(mobilePhone);
            int i = userGatherApplyMapper.selectCount(userGatherApply1);
            if(i>1){
                userGatherApply.setIsApply(true);
            }else{
                userGatherApply.setIsApply(false);
            }
        }
 
        return userGatherApplies;
 
    }
 
 
    public UserGatherApply findById(Long id){
        return userGatherApplyMapper.selectByPrimaryKey(id);
    }
 
    public void audit(UserGatherAuditParam userGatherAuditParam) {
        Long id = userGatherAuditParam.getId();
        String status = userGatherAuditParam.getStatus();
        UserGatherApply userGatherApply=new UserGatherApply();
        BeanUtil.copyProperties(userGatherAuditParam,userGatherApply);
 
        //审核通过
        if(CommonEnum.已审核.getValue().equals(status)){
            UserGatherApply byId = findById(id);
            String type = byId.getType();
 
            if(UserEnum.回收员类型.getValue().equals(type)){
                String relationId=auditOtherUser(byId);
                userGatherApply.setRelationId(relationId);
            }else if(UserEnum.废品店类型.getValue().equals(type)){
                String relationId=auditScrapStore(byId);
                userGatherApply.setRelationId(relationId);
            }else if(UserEnum.集货员类型.getValue().equals(type)) {
                String relationId=auditGather(byId);
                userGatherApply.setRelationId(relationId);
            }
        }
        //更新审核状态
        userGatherApply.setExamineTime(DateUtil.now());
        userGatherApplyMapper.updateByPrimaryKeySelective(userGatherApply);
    }
}