Helius
2020-06-15 0169b3c71efc064577a45c5058d458c9ef1cb94a
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
package com.xcong.excoin.modules.agent.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcong.excoin.common.entity.FebsConstant;
import com.xcong.excoin.common.entity.QueryRequest;
import com.xcong.excoin.common.exception.FebsException;
import com.xcong.excoin.common.utils.Md5Util;
import com.xcong.excoin.modules.agent.entity.AgentFriendRelationEntity;
import com.xcong.excoin.modules.agent.entity.MemberEntity;
import com.xcong.excoin.modules.agent.mapper.AgentFriendRelationMapper;
import com.xcong.excoin.modules.agent.mapper.MemberMapper;
import com.xcong.excoin.modules.agent.pojo.AgentUser;
import com.xcong.excoin.modules.agent.service.IAgentService;
import com.xcong.excoin.system.entity.User;
import com.xcong.excoin.system.entity.UserRole;
import com.xcong.excoin.system.mapper.UserMapper;
import com.xcong.excoin.system.mapper.UserRoleMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.management.Agent;
 
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
/**
 * @author wzy
 * @date 2020-06-11
 **/
@Slf4j
@RequiredArgsConstructor
@Service
public class AgentServiceImpl implements IAgentService {
 
    private final UserMapper userMapper;
 
    private final MemberMapper memberMapper;
 
    private final UserRoleMapper userRoleMapper;
 
    private final AgentFriendRelationMapper agentFriendRelationMapper;
 
    @Override
    public IPage<AgentUser> findAgentList(AgentUser agentUser, QueryRequest request) {
        Page<AgentUser> page = new Page<>(request.getPageNum(), request.getPageSize());
        return userMapper.selectAgentUserList(page, agentUser);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addAgent(AgentUser agentUser, User user) {
        AgentFriendRelationEntity agentFriendRelation = new AgentFriendRelationEntity();
        String refererId = "";
        int level = 0;
        if (FebsConstant.USER_TYPE_ADMIN.equals(user.getType())) {
            refererId = FebsConstant.DEFAULT_REFERER_ID;
            level = 1;
        } else {
            refererId = user.getInviteId();
            AgentFriendRelationEntity friendRelationEntity = agentFriendRelationMapper.selectAgentFriendRelationByUserId(user.getUserId());
            if (agentUser.getReturnRatio().compareTo(friendRelationEntity.getReturnRatio()) > 0) {
                throw new FebsException("返佣比例需小于自己的返佣比例");
            }
            level++;
        }
        MemberEntity memberEntity = memberMapper.selectMemberByInviteIdAndRefererId(agentUser.getInviteId(), refererId);
        if (memberEntity == null) {
            throw new FebsException("UID不存在或不是本用户下级代理");
        }
 
        User exsit = userMapper.selectUserByInviteId(agentUser.getInviteId());
        if (exsit != null) {
            throw new FebsException("该用户已存在");
        }
 
        User addUser = new User();
        addUser.setUsername(agentUser.getAccount());
        addUser.setStatus(User.STATUS_VALID);
        addUser.setAvatar(User.DEFAULT_AVATAR);
        addUser.setTheme(User.THEME_WHITE);
        addUser.setIsTab(User.TAB_OPEN);
        addUser.setType(FebsConstant.USER_TYPE_AGENT);
        addUser.setSystem(FebsConstant.SYSTEM_AGENT);
        addUser.setCreateTime(new Date());
        addUser.setMobile(agentUser.getTelphone());
        addUser.setInviteId(agentUser.getInviteId());
        addUser.setAgentName(agentUser.getName());
        addUser.setPassword(Md5Util.encrypt(addUser.getUsername(), User.DEFAULT_PASSWORD));
        userMapper.insert(addUser);
 
        UserRole userRole = new UserRole();
        userRole.setUserId(addUser.getUserId());
        userRole.setRoleId(83L);
        userRoleMapper.insert(userRole);
 
        agentFriendRelation.setInviteId(agentUser.getInviteId());
        agentFriendRelation.setRefererId(refererId);
        agentFriendRelation.setRefererIds(memberEntity.getRefererIds());
        agentFriendRelation.setMemberId(memberEntity.getId());
        agentFriendRelation.setReturnRatio(agentUser.getReturnRatio());
        agentFriendRelation.setUserId(addUser.getUserId());
        agentFriendRelation.setCreateBy(user.getUsername());
        agentFriendRelation.setCreateTime(new Date());
        agentFriendRelation.setUpdateBy(user.getUsername());
        agentFriendRelation.setUpdateTime(new Date());
        agentFriendRelation.setLevelId(level);
        agentFriendRelation.setFeeIsSelf(2);
        agentFriendRelationMapper.insert(agentFriendRelation);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delAgent(String[] ids) {
        List<String> list = Arrays.asList(ids);
        userMapper.deleteBatchIds(list);
        agentFriendRelationMapper.delete(new QueryWrapper<AgentFriendRelationEntity>().lambda().in(AgentFriendRelationEntity::getUserId, list));
        userRoleMapper.delete(new QueryWrapper<UserRole>().lambda().in(UserRole::getUserId, list));
    }
 
    @Override
    public void resetPwd(Long id) {
        User user = userMapper.selectById(id);
        user.setPassword(Md5Util.encrypt(user.getUsername(), User.DEFAULT_PASSWORD));
        userMapper.updateById(user);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editAgent(AgentUser agentUser, User user) {
        if (!FebsConstant.USER_TYPE_ADMIN.equals(user.getType())) {
            AgentFriendRelationEntity friendRelationEntity = agentFriendRelationMapper.selectAgentFriendRelationByUserId(user.getUserId());
            if (agentUser.getReturnRatio().compareTo(friendRelationEntity.getReturnRatio()) > 0) {
                throw new FebsException("返佣比例需小于上级的返佣比例");
            }
        }
 
        User editUser = new User();
        editUser.setUserId(agentUser.getId());
        editUser.setMobile(agentUser.getTelphone());
        editUser.setAgentName(agentUser.getName());
        userMapper.updateById(editUser);
 
        AgentFriendRelationEntity relationEntity = new AgentFriendRelationEntity();
        relationEntity.setUserId(agentUser.getId());
        relationEntity.setReturnRatio(agentUser.getReturnRatio());
        agentFriendRelationMapper.updateByUserId(relationEntity);
    }
}