yanghb
2023-04-10 b2f678ad387ca24e05a11100ea4583f0f2f730f0
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.UserUserMapper;
import com.stylefeng.guns.modular.system.model.UserInfo;
import com.stylefeng.guns.modular.system.model.UserTaxiCard;
import com.stylefeng.guns.modular.system.model.UserUser;
import com.stylefeng.guns.modular.system.service.IUserInfoService;
import com.stylefeng.guns.modular.system.service.IUserTaxiCardService;
import com.stylefeng.guns.modular.system.service.IUserUserService;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.warpper.BaseWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
public class UserUserServiceImpl extends ServiceImpl<UserUserMapper, UserUser> implements IUserUserService {
 
    @Autowired
    private IUserInfoService userInfoService;
 
    @Autowired
    private IUserTaxiCardService userTaxiCardService;
 
    @Override
    public List<BaseWarpper> getUserUserList(Integer uid) throws Exception {
        List<UserUser> userUsers = this.selectList(new EntityWrapper<UserUser>().eq("userId", uid).orderBy("id"));
        List<BaseWarpper> list = new ArrayList<>();
        userUsers.forEach(userUser -> {
            BaseWarpper baseWarpper = new BaseWarpper();
            baseWarpper.setId(userUser.getId());
            baseWarpper.setName(userUser.getCallName());
            baseWarpper.setContent(userUser.getPhone());
            list.add(baseWarpper);
        });
        return list;
    }
 
    @Override
    public ResultUtil addUserUser(Integer uid, String content) throws Exception {
        int nu = userTaxiCardService.selectCount(new EntityWrapper<UserTaxiCard>().eq("userId", uid));
        if(nu == 0){
            return ResultUtil.error("您暂未购买打车卡");
        }
        if(ToolUtil.isNotEmpty(content)){
            JSONArray jsonArray = JSON.parseArray(content);
            int num = 0;
            for(int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Integer id = jsonObject.getInteger("id");
                if(null == id){
                    num++;
                }
            }
            int number = this.selectCount(new EntityWrapper<UserUser>().eq("userId", uid));
            int n = 15 - number;
            if(n > 0 && n < num){
                return ResultUtil.error("您还能添加" + n + "个亲密账户");
            }
            if(n == 0){
                return ResultUtil.error("您已超出绑定上限15人");
            }
 
            for(int i = 0; i < jsonArray.size(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Integer id = jsonObject.getInteger("id");
                String phone = jsonObject.getString("phone");
                String name = jsonObject.getString("name");
                UserInfo userInfo = userInfoService.selectOne(new EntityWrapper<UserInfo>().eq("phone", phone).ne("flag", 3));
                if(null != userInfo && userInfo.getId().compareTo(uid) == 0){
                    return ResultUtil.error("不能绑定自己");
                }
                if(null == id){
                    if(null != userInfo){
                        int i1 = this.selectCount(new EntityWrapper<UserUser>().eq("userId", uid).eq("bindUserId", userInfo.getId()));
                        if(i1 > 0){
                            return ResultUtil.error(phone + "号码已经绑定了");
                        }
                    }
                    if(null == userInfo){
                        return ResultUtil.error(phone + "该手机还未注册账户");
                    }
 
                    UserUser userUser = new UserUser();
                    userUser.setUserId(uid);
                    userUser.setBindUserId(userInfo.getId());
                    userUser.setCallName(name);
                    userUser.setPhone(phone);
                    this.insert(userUser);
                }else{
                    UserUser userUser = this.selectById(id);
                    userUser.setPhone(phone);
                    userUser.setCallName(name);
                    this.updateById(userUser);
                }
            }
        }
        return ResultUtil.success();
    }
 
    @Override
    public ResultUtil unbundleUserUser(Integer id) throws Exception {
        this.deleteById(id);
        return ResultUtil.success();
    }
}