1
zj
2026-01-14 716f77637e6e324fd5865e82039c2c2ee3f94bfa
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
package com.nq.service.impl;
 
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nq.common.ServerResponse;
import com.nq.dao.AgentAgencyFeeMapper;
import com.nq.dao.AgentUserMapper;
import com.nq.dao.UserMapper;
import com.nq.dao.UserPositionMapper;
import com.nq.pojo.*;
import com.nq.service.IAgentAgencyFeeService;
import com.nq.service.IAgentUserService;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.redis.CookieUtils;
import com.nq.utils.redis.JsonUtil;
import com.nq.utils.redis.RedisShardedPoolUtils;
import com.nq.vo.agent.AgentAgencyFeeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
 
@Service("IAgentAgencyFeeService")
public class AgentAgencyFeeServiceImpl implements IAgentAgencyFeeService {
    @Resource
    private AgentAgencyFeeMapper agentAgencyFeeMapper;
 
    @Autowired
    IAgentUserService iAgentUserService;
 
    @Autowired
    AgentUserMapper agentUserMapper;
 
    @Autowired
    UserPositionMapper userPositionMapper;
 
    @Autowired
    UserMapper userMapper;
 
    @Override
    public int insert(AgentAgencyFee agentAgencyFee) {
        int ret = 0;
        // valid
        if (agentAgencyFee == null) {
            return 0;
        }
 
        ret = agentAgencyFeeMapper.insert(agentAgencyFee);
        return ret;
    }
 
    @Override
    public int update(AgentAgencyFee agentAgencyFee) {
        int ret = agentAgencyFeeMapper.update(agentAgencyFee);
        return ret>0 ? ret: 0;
    }
 
    /*获取收费比例
    * feeType:费用类型:1、入仓手续费,2、平仓手续费,3、延递费(留仓费),4、分红
    * */
    private  BigDecimal getScale(AgentUser agentUser, int feeType){
        BigDecimal scale = new BigDecimal(0);//递延费比例
        if(feeType == 1 || feeType == 2){
            scale = agentUser.getPoundageScale();
        } else if(feeType == 3){
            scale = agentUser.getDeferredFeesScale();
        } else if(feeType == 4){
            scale = agentUser.getReceiveDividendsScale();
        }
        return scale;
    }
 
    /*
    * 代理费收入,考虑多级代理的问题
    * feeType:费用类型:1、入仓手续费,2、平仓手续费,3、延递费(留仓费),4、分红
    * positionSn:入仓单号
    * 调用关键词:核算代理收入
    * */
    @Override
    public int AgencyFeeIncome(int feeType,String positionSn) {
        int ret = 0;
        int businessId = 0;
        int agentId = 0;
        int level = 0;//代理级别
        BigDecimal totalMoney = new BigDecimal(0);
        BigDecimal oneProfitMoney = new BigDecimal(0);//一级利润
        BigDecimal upProfitMoney = new BigDecimal(0);//上级利润
        BigDecimal selfProfitMoney = new BigDecimal(0);//上级利润
        BigDecimal downProfitMoney = new BigDecimal(0);//下级利润
        BigDecimal scale = new BigDecimal(0);//递延费比例
        String remarks = "";
        int downAgentId = 0;
        AgentUser agentUser = null;
        UserPosition userPosition = null;
        userPosition = userPositionMapper.findPositionBySn(positionSn);
        agentUser = agentUserMapper.findAgentByAgentId(userPosition.getAgentId());
        if(agentUser == null){
            return 0;
        }
        agentId = agentUser.getId();
        level = agentUser.getAgentLevel();
        businessId = userPosition.getId();
        if(feeType == 1){
            totalMoney = userPosition.getOrderFee();
            remarks = "【入仓收入】入仓手续费总额:"+userPosition.getOrderFee()+",单号:"+userPosition.getPositionSn();
        } else if(feeType == 2){
            //平仓浮动总盈亏负数,代理利润增加,否则减少
            totalMoney = userPosition.getAllProfitAndLose().multiply(new BigDecimal(-1));
            remarks = "【平仓收入】平仓手续费总额:"+userPosition.getOrderFee()+",单号:"+userPosition.getPositionSn();
        } else if(feeType == 3){
            totalMoney = userPosition.getOrderStayFee();
            remarks = "【递延费收入】递延费总额:"+userPosition.getOrderFee()+",单号:"+userPosition.getPositionSn();
        }else if(feeType == 4){
            totalMoney = userPosition.getAllProfitAndLose().multiply(new BigDecimal(-1));
            remarks = "【分红收入】分红总额:"+userPosition.getOrderFee()+",单号:"+userPosition.getPositionSn();
        }
        //金额为0不计算分红直接跳出
        if(totalMoney.compareTo(new BigDecimal(0))<=0){
            return -1;
        }
        //模拟用户下单不计算分红
        User user = userMapper.selectById(userPosition.getUserId());
        if(user.getAccountType() != 0 || user.getIsLock() != 0 || user.getIsActive() != 2){
            return -2;
        }
        List<AgentUser> agentlist = iAgentUserService.getAgentSuperiorList(agentUser.getId());
        if(agentlist != null && agentlist.size()>0){
            if(agentlist.size() == 1){//一级代理的会员
                //一级代理利润
                AgentAgencyFee agentAgencyFee = new AgentAgencyFee();
                scale = getScale(agentUser,feeType);
                selfProfitMoney = totalMoney.multiply(scale).setScale(4, 4);
                agentAgencyFee.setAgentId(agentUser.getId());
                agentAgencyFee.setStatus(1);
                agentAgencyFee.setAymentType(1);
                agentAgencyFee.setBusinessId(businessId);
                agentAgencyFee.setFeeType(feeType);
                agentAgencyFee.setTotalMoney(totalMoney);
                agentAgencyFee.setProfitMoney(selfProfitMoney);
                agentAgencyFee.setRemarks(remarks);
                saveAgencyFee(agentAgencyFee);
                //总代理利润
                agentUser = agentUserMapper.findAgentByAgentId(agentUser.getParentId());
                AgentAgencyFee totalAgent = new AgentAgencyFee();
                totalAgent.setAgentId(agentUser.getId());
                totalAgent.setStatus(1);
                totalAgent.setAymentType(1);
                totalAgent.setBusinessId(businessId);
                totalAgent.setFeeType(feeType);
                upProfitMoney = totalMoney.subtract(selfProfitMoney);
                totalAgent.setTotalMoney(totalMoney);
                totalAgent.setProfitMoney(upProfitMoney);
                totalAgent.setRemarks(remarks);
                saveAgencyFee(totalAgent);
            } else if(agentlist.size()>1) {//二级代理以上会员
                for (int i = 1; i < agentlist.size(); i++) {
                    AgentUser model = agentlist.get(i);
                    //一级代理,要把总代理的收入算出来
                    if (i == 1) {
                        //一级代理利润
                        AgentAgencyFee agentAgencyFee = new AgentAgencyFee();
                        if (agentlist.size() == 2) {
                            AgentUser selfAgentUser = agentUserMapper.findAgentByAgentId(agentId);
                            scale = getScale(model,feeType);
                            oneProfitMoney = totalMoney.multiply(scale).setScale(4, 4);
                            scale = getScale(selfAgentUser,feeType);
                            downProfitMoney = oneProfitMoney.multiply(scale).setScale(4, 4);
                            selfProfitMoney = oneProfitMoney.subtract(downProfitMoney);
 
                        } else {
                            AgentUser modeldown = agentlist.get(i + 1);
                            scale = getScale(model,feeType);
                            oneProfitMoney = totalMoney.multiply(scale).setScale(4, 4);
                            scale = getScale(modeldown,feeType);
                            downProfitMoney = oneProfitMoney.multiply(scale).setScale(4, 4);
                            selfProfitMoney = oneProfitMoney.subtract(downProfitMoney);
                        }
                        agentAgencyFee.setAgentId(model.getId());
                        agentAgencyFee.setStatus(1);
                        agentAgencyFee.setAymentType(1);
                        agentAgencyFee.setBusinessId(businessId);
                        agentAgencyFee.setFeeType(feeType);
                        agentAgencyFee.setTotalMoney(totalMoney);
                        agentAgencyFee.setProfitMoney(selfProfitMoney);
                        agentAgencyFee.setRemarks(remarks);
                        //增加一级代理利润
                        saveAgencyFee(agentAgencyFee);
                        //总代理利润
                        agentUser = agentUserMapper.findAgentByAgentId(model.getParentId());
                        AgentAgencyFee totalAgent = new AgentAgencyFee();
                        totalAgent.setAgentId(agentUser.getId());
                        totalAgent.setStatus(1);
                        totalAgent.setAymentType(1);
                        totalAgent.setBusinessId(businessId);
                        totalAgent.setFeeType(feeType);
                        upProfitMoney = totalMoney.subtract(oneProfitMoney);
                        totalAgent.setTotalMoney(totalMoney);
                        totalAgent.setProfitMoney(upProfitMoney);
                        totalAgent.setRemarks(remarks);
                        saveAgencyFee(totalAgent);
                        upProfitMoney = downProfitMoney;
                        ret = ret +1 ;
                    } else {
                        //二级以下代理利润
                        AgentAgencyFee agentAgencyFee = new AgentAgencyFee();
                        if (i == (level-1)) {//倒数第二级,自己就是下级
                            AgentUser selfAgentUser = agentUserMapper.findAgentByAgentId(agentId);
                            scale = getScale(selfAgentUser,feeType);
                            downProfitMoney = upProfitMoney.multiply(scale).setScale(4, 4);
                            selfProfitMoney = upProfitMoney.subtract(downProfitMoney);
                        } else {
                            AgentUser modeldown = agentlist.get(i + 1);
                            scale = getScale(modeldown,feeType);
                            downProfitMoney = upProfitMoney.multiply(scale).setScale(4, 4);
                            selfProfitMoney = upProfitMoney.subtract(downProfitMoney);
                        }
                        agentAgencyFee.setAgentId(model.getId());
                        agentAgencyFee.setStatus(1);
                        agentAgencyFee.setAymentType(1);
                        agentAgencyFee.setBusinessId(businessId);
                        agentAgencyFee.setFeeType(feeType);
                        agentAgencyFee.setTotalMoney(totalMoney);
                        agentAgencyFee.setProfitMoney(selfProfitMoney);
                        agentAgencyFee.setRemarks(remarks);
                        saveAgencyFee(agentAgencyFee);
                        upProfitMoney = downProfitMoney;
                        ret = ret +1 ;
                    }
                }
                //最后一级的利润
                AgentAgencyFee agentAgencyFee = new AgentAgencyFee();
                agentAgencyFee.setAgentId(agentId);
                agentAgencyFee.setStatus(1);
                agentAgencyFee.setAymentType(1);
                agentAgencyFee.setBusinessId(businessId);
                agentAgencyFee.setFeeType(feeType);
                agentAgencyFee.setTotalMoney(totalMoney);
                agentAgencyFee.setProfitMoney(downProfitMoney);
                agentAgencyFee.setRemarks(remarks);
                saveAgencyFee(agentAgencyFee);
            }
        }
        return ret;
    }
 
    /*增加代理利润*/
    public int saveAgencyFee(AgentAgencyFee model){
        int k = 0;
        //添加利润明细
        k = insert(model);
        //修改代理账户余额
        AgentUser user = new AgentUser();
        user.setId(model.getAgentId());
        user.setTotalMoney(model.getProfitMoney());
        k = agentUserMapper.updateTotalMoney(user);
        return k;
    }
 
    /*查询代理利润明细列表
    * */
    public ServerResponse<PageInfo> getAgentAgencyFeeList(int pageNum, int pageSize, HttpServletRequest request) {
        Page<AgentAgencyFeeVO> page = PageHelper.startPage(pageNum, pageSize);
        AgentUser agentUser = getCurrentAgent(request);
        List<AgentAgencyFee> agentAgencyFees = this.agentAgencyFeeMapper.getAgentAgencyFeeList(agentUser.getId());
        PageInfo pageInfo = new PageInfo(page);
        pageInfo.setList(agentAgencyFees);
        return ServerResponse.createBySuccess(pageInfo);
    }
 
    public AgentUser getCurrentAgent(HttpServletRequest request) {
        String loginToken = CookieUtils.readLoginToken(request, PropertiesUtil.getProperty("agent.cookie.name"));
        String agentJson = RedisShardedPoolUtils.get(loginToken);
        AgentUser agentUser = (AgentUser) JsonUtil.string2Obj(agentJson, AgentUser.class);
       if (agentUser==null){
           return null;
       }
        return this.agentUserMapper.selectByPrimaryKey(agentUser.getId());
    }
}