UserServiceImpl.java 12.9 KB
Newer Older
licc's avatar
licc committed
1 2
package cn.wisenergy.service.app.impl;

3
import cn.wisenergy.common.utils.*;
4 5
import cn.wisenergy.mapper.*;
import cn.wisenergy.model.app.*;
6
import cn.wisenergy.model.app.UsersDto;
7 8
import cn.wisenergy.model.vo.AerialDeliveryVo;
import cn.wisenergy.model.vo.UserPoolVo;
9
import cn.wisenergy.service.Manager.RegistUserMoudleInitManager;
10
import cn.wisenergy.service.app.UserLevelService;
licc's avatar
licc committed
11
import cn.wisenergy.service.app.UserService;
12
import com.alibaba.fastjson.JSONObject;
licc's avatar
licc committed
13
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
14
import com.xxl.job.core.handler.annotation.XxlJob;
15
import lombok.Data;
licc's avatar
licc committed
16
import lombok.extern.slf4j.Slf4j;
17
import org.apache.commons.lang3.StringUtils;
18
import org.aspectj.weaver.ast.Var;
licc's avatar
licc committed
19 20
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
21
import org.springframework.transaction.annotation.Transactional;
m1991's avatar
m1991 committed
22
import org.springframework.util.CollectionUtils;
licc's avatar
licc committed
23

24 25
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
26
import java.util.*;
licc's avatar
licc committed
27 28

/**
licc's avatar
licc committed
29
 * @author 86187
licc's avatar
licc committed
30 31 32 33 34 35 36 37 38 39 40
 * @ Description: 用户接口实现
 * @ Author     : 86187
 * @ Date       : 2021/1/6 16:11
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UsersMapper, User> implements UserService {

    @Autowired
    private UsersMapper usersMapper;

m1991's avatar
m1991 committed
41 42 43
    @Autowired
    private RecommendUserMapper recommendUserMapper;

44 45
    @Autowired
    private TeamUserInfoMapper teamUserInfoMapper;
licc's avatar
licc committed
46

47
    @Autowired
48
    private RegistUserMoudleInitManager registUserMoudleInit;
49

m1991's avatar
m1991 committed
50 51 52
    @Autowired
    private RedisUtils redisUtils;

licc's avatar
licc committed
53 54
    private static final String INVITE_CODE = "1";

55 56
    @Override
    public User getById(String userId) {
licc's avatar
licc committed
57
        return usersMapper.getByUserId(userId);
licc's avatar
licc committed
58 59
    }

60 61
    /**
     * 获取登录用户
licc's avatar
licc committed
62
     *
63 64 65 66 67
     * @param token
     */
    @Override
    public UsersDto getLoginUser(String token) {
        UsersDto usersDto = null;
licc's avatar
licc committed
68
        String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, token);
69
        String value = redisUtils.getValue(key);
licc's avatar
licc committed
70
        if (!StringUtils.isBlank(value)) {
71 72 73 74 75 76
            usersDto = JSONObject.parseObject(value, UsersDto.class);
            log.info("获取到用户登录信息users:{}", value);
        }
        return usersDto;
    }

licc's avatar
licc committed
77
    @Override
78 79
    public R<Integer> getById1(String userId) {

80
        return R.ok(usersMapper.ByUserId(userId));
licc's avatar
licc committed
81 82
    }

liqin's avatar
liqin committed
83 84 85 86 87
    @Override
    public User getUserById(Long id) {
        return this.usersMapper.getUserById(id);
    }

88 89
    @Override
    public User getByUserId(String userId) {
90

codezwjava's avatar
codezwjava committed
91
        return usersMapper.getByUserId(userId);
92
    }
m1991's avatar
m1991 committed
93 94 95


    @Override
m1991's avatar
m1991 committed
96
    public User queryUsersByPhone(String userId) {
97
        //查询数据库,看看是否存在该用户
licc's avatar
licc committed
98
        return usersMapper.getByUserId(userId);
m1991's avatar
m1991 committed
99 100
    }

m1991's avatar
m1991 committed
101 102 103 104 105 106
    @Override
    public User queryUsersByOpenId(String id) {
        return null;
    }

    @Override
107 108
    public Map userByZx(String userId, String beInvitedCode) {
        //查询数据库,看看是否存在该用户
m1991's avatar
m1991 committed
109
        User byUserId = usersMapper.getByUserId(userId);
110
        if (null != byUserId) {
111
            R.error(1, "该用户已存在!请直接登录!");
112
            Map map = new HashMap();
113
            map.put("code", 1);
114
            map.put("msg", "该用户已存在!请直接登录!");
m1991's avatar
m1991 committed
115
            return map;
m1991's avatar
m1991 committed
116
        }
117 118


m1991's avatar
m1991 committed
119
        /**
120
         * 判断用户推荐人的邀请码是否为空,空的话填写1   || "" == beInvitedCode
m1991's avatar
m1991 committed
121
         */
licc's avatar
licc committed
122
        if (StringUtils.isBlank(beInvitedCode)) {
123
            //插入用户手机号与推荐人邀请码
licc's avatar
licc committed
124
            String inviteCode = "0";
125
            beInvitedCode = "1";
licc's avatar
licc committed
126
            int userLevel = 0;
127
            int frozen=0;
licc's avatar
licc committed
128
            String headImage = Constants.Common.HEAD_POTRAIT;
129
            usersMapper.insertbyint(userId, inviteCode, beInvitedCode, userLevel, headImage,frozen);
130
            //初始化六张表
131
            registUserMoudleInit.registUserMoudleInit(userId);
132
        } else {
133 134 135
            //插入用户手机号与推荐人邀请码
            String inviteCode = "0";
            int userLevel = 0;
136
            int frozen=0;
137
            String headImage = Constants.Common.HEAD_POTRAIT;
138
            usersMapper.insertbyint(userId, inviteCode, beInvitedCode, userLevel, headImage,frozen);
139 140
            //初始化六张表
            registUserMoudleInit.registUserMoudleInit(userId);
141
            //用户的被邀请码,查询到推荐人用户,根据推荐人用户的邀请码查询/修改
142
            User user = usersMapper.getByBeInvitedCode(beInvitedCode);
143

licc's avatar
licc committed
144
            //邀请人的被邀请码
145
            String byqm = user.getBeInvitedCode();
146

licc's avatar
licc committed
147
            //通过被邀请码,比对本人邀请码查询本人id
148
            String idb = String.valueOf(usersMapper.beInvitedCode1(beInvitedCode));
licc's avatar
licc committed
149

150
            //判断是否存在被邀请人的用户
151
            if (null == idb) {
152 153 154 155 156 157
                Map map = new HashMap();
                R.error(1, "邀请码填写无效!邀请人不存在,请重新填写!");
                map.put("code:", 1);
                map.put("msg:", "邀请码填写无效!邀请人不存在,请重新填写!");
                return map;
            }
158
            //推荐人的id
licc's avatar
licc committed
159
            int idbc = Integer.valueOf(idb);
160 161 162 163 164

            //获取注册用户的id
            User use=  usersMapper.getByUserId(userId);
            int ida =use.getId();

165
            //判断被邀请用户的创建时间是否比推荐人的用户时间晚
166
            if (idbc > ida) {
167 168 169 170
                Map map = new HashMap();
                R.error(1, "注册失败!邀请码无效,请重新填写!");
                map.put("code:", 1);
                map.put("msg:", "注册失败!邀请码无效,请重新填写!");
171 172
                return map;
            }
173

m1991's avatar
m1991 committed
174 175 176
            if (null == beInvitedCode || "" == beInvitedCode) {
                beInvitedCode = String.valueOf(1);
            }
m1991's avatar
m1991 committed
177
        }
178

m1991's avatar
m1991 committed
179
        //根据插入的用户手机号,查询用户唯一ID
180 181 182 183 184 185 186 187 188

        if(null== usersMapper.getuserIdById(userId)|| "".equals(usersMapper.getuserIdById(userId))){
            Map map =new HashMap();
            map.put("code",1);
            map.put("msg","该用户不存在!");
            return map;

        }
        long yqm=usersMapper.getuserIdById(userId);
m1991's avatar
m1991 committed
189
        //用户唯一ID调用生成6位邀请码
190
        String inviteCode = ShareCodeUtil.idToCode(yqm);
191
        Integer userLevel = 0;
m1991's avatar
m1991 committed
192
        //根据用户手机号,更新用户信息
193
//        usersMapper.edit1(userLevel,inviteCode,userId);
m1991's avatar
m1991 committed
194 195 196
        User byUserId1 = usersMapper.getByUserId(userId);
        byUserId1.setInviteCode(inviteCode);
        byUserId1.setUserLevel(0);
197
        byUserId1.setFrozen(0);
198
        usersMapper.updateById(byUserId1);
199

200
        String BYQM = usersMapper.BYQMById(userId);
m1991's avatar
m1991 committed
201

202 203 204 205
        if ("1".equals(BYQM)) {
            Map map = new HashMap();
            map.put("code", 0);
            map.put("msg", "注册成功!");
licc's avatar
licc committed
206
            String key = StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX, Constants.RedisKey.SMS_PRIFIX, userId, "0" + "");
207
            redisUtils.delete(key);
m1991's avatar
m1991 committed
208
            return map;
m1991's avatar
m1991 committed
209
        }
codezwjava's avatar
codezwjava committed
210 211 212

        //根据当前推荐人的邀请码查询 上级用户id
        String userIdByIntive = usersMapper.getUserByIntiveCode(beInvitedCode);
licc's avatar
licc committed
213 214 215
        if (null == userIdByIntive) {
            R.error(1, "用户推荐人邀请码不存在,请填写正确的邀请码");
        } else {
codezwjava's avatar
codezwjava committed
216 217 218
            //上级用户的直推信息 普通用户数量+1
            RecommendUser recommendUserMapperByUserId = recommendUserMapper.getByUserId(userIdByIntive);
            Integer normalUserNum = recommendUserMapperByUserId.getNormalUserNum();
licc's avatar
licc committed
219
            recommendUserMapperByUserId.setNormalUserNum(normalUserNum + 1);
codezwjava's avatar
codezwjava committed
220
            recommendUserMapper.updateById(recommendUserMapperByUserId);
m1991's avatar
m1991 committed
221
        }
codezwjava's avatar
codezwjava committed
222

223
        //递归向上修改团队用户信息表
codezwjava's avatar
codezwjava committed
224
        teamUserInfo(beInvitedCode);
225
        Map map = new HashMap();
226
        R.ok("注册成功!", 0);
227
        map.put("code:", 0);
228
        map.put("msg:", "注册成功!");
229 230
        return map;
    }
m1991's avatar
m1991 committed
231

232
    @Override
licc's avatar
licc committed
233
    public Integer getUserIdById(String userId) {
234
        return null;
m1991's avatar
m1991 committed
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

    @Override
    public R<AerialDeliveryVo> queryAerialDelivery() {
        log.info("shop-mall[]UserServiceImpl[]queryAerialDelivery[]input.method");
        //获取本月新注册用户
        Map<String, Object> map = new HashMap<>(4);
        map.put("createTime", new Date());
        List<User> list = usersMapper.getList(map);
        if (CollectionUtils.isEmpty(list)) {
            return R.ok(new AerialDeliveryVo());
        }

        AerialDeliveryVo aerialDeliveryVo = new AerialDeliveryVo();
        List<UserPoolVo> userPoolVoList = new ArrayList<>();
        for (User user : list) {
            UserPoolVo userPoolVo = new UserPoolVo();
            userPoolVo.setUserId(user.getUserId());
            userPoolVo.setHeadImage(user.getHeadImage());
            userPoolVo.setIntoTime(user.getCreateTime());
            userPoolVoList.add(userPoolVo);
        }
        aerialDeliveryVo.setTotal(list.size());
        aerialDeliveryVo.setUserPoolVos(userPoolVoList);
        return R.ok(aerialDeliveryVo);
    }

263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    @Override
    public R<Boolean> setHeadImage(String userId, String headImage) {
        log.info("shop-mall[]UserServiceImpl[]setHeadImage[]input.param.userId,headImage:" + userId, headImage);
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(headImage)) {
            return R.error("入参不能为空!");
        }

        //获取用户信息
        User user = usersMapper.getByUserId(userId);
        if (null == user) {
            return R.error("用户信息不存在!");
        }

        //添加头像
        user.setHeadImage(headImage);
        int count = usersMapper.edit(user);
        if (count == 0) {
            return R.error("设置头像失败!");
        }
        return R.ok(0, true);
    }

liqin's avatar
liqin committed
286
    @Override
287 288 289 290 291 292 293 294 295 296 297 298
    public R<Boolean> fillInInviteCode(String userId, String inviteCode) {
        log.info("shop-mall[]UserServiceImpl[]fillInInviteCode[]input.param.userId,inviteCode:" + userId, inviteCode);
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(inviteCode)) {
            return R.error("入参不能为空!");
        }

        //1、获取用户信息
        User user = usersMapper.getByUserId(userId);
        if (null == user) {
            return R.error("用户信息不存在!");
        }

licc's avatar
licc committed
299
        //判断用户是否有邀请码 1:无
licc's avatar
licc committed
300 301
        if (!INVITE_CODE.equals(user.getBeInvitedCode())) {
            return R.error("尊敬的用户您好,您的推荐人邀请码已存在,不能修改!");
licc's avatar
licc committed
302 303
        }

304 305 306 307 308 309 310
        //2、根据邀请码,获取邀请人信息
        User userCode = usersMapper.queryByInviteCode(inviteCode);
        if (null == userCode) {
            return R.error("邀请人信息不存在!");
        }

        //3、判断邀请人的注册时间是否大于当前用户
licc's avatar
licc committed
311 312
        if (user.getCreateTime().getTime() < userCode.getCreateTime().getTime()) {
            return R.error("请填写注册时间早于自己的用户邀请码!");
313 314
        }

licc's avatar
licc committed
315 316 317 318 319
        //4、判断用户推荐人邀请码是否为自己的邀请码
        if (user.getUserId().equals(userCode.getUserId())) {
            return R.error("推荐人不能是自己!");
        }

320 321 322 323 324 325 326 327 328
        //4、设置用户被邀请码
        user.setBeInvitedCode(inviteCode);
        int count = usersMapper.edit(user);
        if (count == 0) {
            return R.ok(1, false);
        }
        return R.ok(0, true);
    }

codezwjava's avatar
codezwjava committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    //团队用户数据维护
    public void teamUserInfo(String beInvitedCode) {
        //根据邀请码查询上级用户的id
        String teamUserId = usersMapper.getUserByIntiveCode(beInvitedCode);
        //查询上级用户的团队信息表
        TeamUserInfo teamUserInfoByUserId = teamUserInfoMapper.getByUserId(teamUserId);
        //上级用户团队信息普通用户数量+1
        Integer normalUserNum = teamUserInfoByUserId.getNormalUserNum();
        teamUserInfoByUserId.setNormalUserNum(normalUserNum + 1);
        teamUserInfoMapper.updateById(teamUserInfoByUserId);
        //根据上级用户id查询上级用户的邀请码
        User teamUser = usersMapper.getByUserId(teamUserId);
        String teamUserBeInvitedCode = teamUser.getBeInvitedCode();
        //递归实现上级所有的用户普通数量 + 1
        //若团队用户的被邀请码为 1 时 则没有上级用户用户
344
        if (!"1".equals(teamUserBeInvitedCode)) {
codezwjava's avatar
codezwjava committed
345
            teamUserInfo(teamUserBeInvitedCode);
licc's avatar
licc committed
346
        } else {
codezwjava's avatar
codezwjava committed
347
            return;
348 349
        }
    }
licc's avatar
licc committed
350

m1991's avatar
m1991 committed
351 352
    /**
     * 用户登出
licc's avatar
licc committed
353
     *
m1991's avatar
m1991 committed
354 355 356 357 358 359
     * @param token
     * @return
     */
    @Override
    public int logout(String token) {
        int succ = 0;
360 361
        String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, token);
        redisUtils.delete(key);
licc's avatar
licc committed
362
        if (redisUtils.getValue(key) == null) {
m1991's avatar
m1991 committed
363 364 365 366
            succ = 1;
        }
        return succ;
    }
licc's avatar
licc committed
367
}