1
zj
2024-06-13 a4662cc65a02f258062bf6cc392ceb1017db9292
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
package com.yami.trading.service.trader.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.trading.bean.trader.domain.Trader;
import com.yami.trading.bean.trader.domain.TraderFollowUser;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.util.Arith;
import com.yami.trading.dao.trader.TraderFollowUserMapper;
import com.yami.trading.service.trader.TraderFollowUserService;
import com.yami.trading.service.trader.TraderService;
import com.yami.trading.service.trader.TraderUserService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
 
@Service
public class TraderFollowUserServiceImpl implements TraderFollowUserService {
    @Resource
    private TraderService traderService;
    @Resource
    private TraderUserService traderUserService;
 
    @Resource
    private TraderFollowUserMapper traderFollowUserMapper;
 
    public List<Map<String, Object>> getPaged(Page pageparam, String partyId, String profit) {
 
//        StringBuffer queryString = new StringBuffer("");
//        queryString.append(" SELECT * FROM ");
//        queryString.append(" T_TRADER_FOLLOW_USER ");
//        queryString.append(" where 1=1 ");
//
//        Map<String, Object> parameters = new HashMap();
//
//        queryString.append(" and TRADER_PARTY_ID = :partyId");
//        parameters.put("partyId", partyId);
 
//        if (!StringUtils.isNullOrEmpty(profit)) {
//            queryString.append(" and PROFIT >= 0 ");
//        }
//
//        queryString.append(" order by PROFIT desc ");
        Page page = traderFollowUserMapper.selectPage(pageparam, Wrappers.<TraderFollowUser>lambdaQuery().eq(TraderFollowUser::getTraderPartyId, partyId).ge(TraderFollowUser::getProfit, 0).orderByDesc(TraderFollowUser::getProfit));
//        Page page = this.pagedQueryDao.pagedQuerySQL(pageNo, pageSize, queryString.toString(), parameters);
        List<Map<String, Object>> data = this.bulidData(page.getRecords());
        return data;
    }
 
    private List<Map<String, Object>> bulidData(List<TraderFollowUser> traderFollowUsers) {
        List<Map<String, Object>> result_traders = new ArrayList();
        DecimalFormat df2 = new DecimalFormat("#.##");
        df2.setRoundingMode(RoundingMode.FLOOR);// 向下取整
        if (traderFollowUsers == null) {
            return result_traders;
        }
        for (int i = 0; i < traderFollowUsers.size(); i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            TraderFollowUser entity = traderFollowUsers.get(i);
            map.put("name", entity.getUsername());
            map.put("profit", df2.format(entity.getProfit()));
            map.put("amount_sum", df2.format(entity.getAmountSum()));
 
            result_traders.add(map);
        }
 
        return result_traders;
 
    }
 
    @Override
    public void save(TraderFollowUser entity, String trader_id) {
        if (entity.getVolume() % 1 != 0 || entity.getVolume() <= 0 || entity.getVolumeMax() % 1 != 0) {
            throw new BusinessException(1, "跟单参数输入错误");
        }
        if (entity.getFollowType() == "1" && (entity.getVolume() > 3000 || entity.getVolume() < 1)) {
            throw new BusinessException(1, "跟单参数输入错误");
        }
        if (entity.getFollowType() == "2" && (entity.getVolume() > 5 || entity.getVolume() < 1)) {
            throw new BusinessException(1, "跟单倍数输入错误");
        }
        Trader trader = this.traderService.findById(trader_id);
        if (trader == null) {
            throw new BusinessException(1, "交易员不存在");
        }
        if ("0".equals(trader.getState())) {
            throw new BusinessException(1, "交易员未开启带单");
        }
        if (findByStateAndPartyId(entity.getPartyId(), trader.getPartyId(), "1") != null) {
            throw new BusinessException(1, "用户已跟随交易员");
        }
        if (Arith.sub(trader.getFollowerMax(), trader.getFollowerNow()) < 1) {
            throw new BusinessException(1, "交易员跟随人数已满");
        }
        if (entity.getPartyId().equals(trader.getPartyId())) {
            throw new BusinessException(1, "交易员不能跟随自己");
        }
        Trader trader_user = this.traderService.findByPartyId(entity.getPartyId());
        if (trader_user != null) {
            throw new BusinessException(1, "交易员无法跟随另一个交易员");
        }
        // 跟单固定张数/固定比例---选择 1,固定张数,2,固定比例
        if (trader.getFollowVolumnMin() > 0) {
            switch (entity.getFollowType()) {
            case "1":
                if (entity.getVolume() < trader.getFollowVolumnMin()) {
                    throw new BusinessException(1, "跟单参数输入错误");
                }
                if (entity.getVolumeMax() < trader.getFollowVolumnMin()) {
                    throw new BusinessException(1, "跟单参数输入错误");
                }
                break;
            case "2":
                throw new BusinessException(1, "交易员已设置最小下单数,无法通过固定比例跟单");
            default:
                break;
            }
        }
 
        entity.setTraderPartyId(trader.getPartyId());
        entity.setCreateTime(new Date());
 
        trader.setFollowerNow((int) Arith.add(trader.getFollowerNow(), 1));
        trader.setFollowerSum((int) Arith.add(trader.getFollowerSum(), 1));
        traderService.update(trader);
        /**
         * 创建累计用户跟随累计表
         */
        traderUserService.saveTraderUserByPartyId(entity.getPartyId());
 
//        ApplicationUtil.executeSaveOrUpdate(entity);
        traderFollowUserMapper.insert(entity);
 
    }
 
    @Override
    public void save(TraderFollowUser entity) {
        traderFollowUserMapper.insert(entity);
    }
 
    @Override
    public void update(TraderFollowUser entity) {
        if (entity.getVolume() % 1 != 0 || entity.getVolume() <= 0 || entity.getVolumeMax() % 1 != 0) {
            throw new BusinessException(1, "跟单参数输入错误");
        }
        if (entity.getFollowType() == "1" && (entity.getVolume() > 3000 || entity.getVolume() < 1)) {
            throw new BusinessException(1, "跟单参数输入错误");
        }
        if (entity.getFollowType() == "2" && (entity.getVolume() > 5 || entity.getVolume() < 1)) {
            throw new BusinessException(1, "跟单倍数输入错误");
        }
 
//        ApplicationUtil.executeUpdate(entity);
        traderFollowUserMapper.updateById(entity);
    }
 
    @Override
    public void deleteCancel(String id) {
        TraderFollowUser entity = findById(id);
        /**
         * 将旧的交易员跟随用户-1
         */
        Trader trader_before = this.traderService.findByPartyId(entity.getTraderPartyId().toString());
        trader_before.setFollowerNow((int) Arith.sub(trader_before.getFollowerNow(), 1));
        this.traderService.update(trader_before);
 
        if (entity != null) {
//            ApplicationUtil.executeDelete(entity);
            traderFollowUserMapper.deleteById(entity);
        }
 
    }
 
    public List<TraderFollowUser> findByStateAndPartyId(String partyId, String trader_partyId, String state) {
        List<TraderFollowUser> list = traderFollowUserMapper.selectList(Wrappers.<TraderFollowUser>lambdaQuery().eq(TraderFollowUser::getPartyId, partyId).eq(TraderFollowUser::getTraderPartyId, trader_partyId).eq(TraderFollowUser::getState, state));
//        List<TraderFollowUser> list = ApplicationUtil.executeSelect(TraderFollowUser.class,
//                " WHERE PARTY_ID = ? AND TRADER_PARTY_ID = ?  AND STATE = ?  ",
//                new Object[] { partyId, trader_partyId, state });
        if (list.size() > 0)
            return list;
        return null;
    }
 
    public List<TraderFollowUser> findByTrader_partyId(String trader_partyId) {
        List<TraderFollowUser> list = traderFollowUserMapper.selectList(Wrappers.<TraderFollowUser>lambdaQuery().eq(TraderFollowUser::getTraderPartyId, trader_partyId));
//        List<TraderFollowUser> list = ApplicationUtil.executeSelect(TraderFollowUser.class, " WHERE TRADER_PARTY_ID = ? ",
//                new Object[] { trader_partyId });
        if (list.size() > 0)
            return list;
        return null;
    }
 
    public List<TraderFollowUser> findByPartyId(String partyId) {
        List<TraderFollowUser> list = traderFollowUserMapper.selectList(Wrappers.<TraderFollowUser>lambdaQuery().eq(TraderFollowUser::getPartyId, partyId));
//        List<TraderFollowUser> list = ApplicationUtil.executeSelect(TraderFollowUser.class, " WHERE PARTY_ID = ? ",
//                new Object[] { partyId });
        if (list.size() > 0)
            return list;
        return null;
    }
 
    public TraderFollowUser findByPartyIdAndTrader_partyId(String partyId, String trader_partyId) {
        List<TraderFollowUser> list = traderFollowUserMapper.selectList(Wrappers.<TraderFollowUser>lambdaQuery().eq(TraderFollowUser::getPartyId, partyId).eq(TraderFollowUser::getTraderPartyId, trader_partyId));
//        List<TraderFollowUser> list = ApplicationUtil.executeSelect(TraderFollowUser.class,
//                " WHERE PARTY_ID= ? and TRADER_PARTY_ID = ? ",
//                new Object[] { partyId, trader_partyId });
        if (list.size() > 0)
            return list.get(0);
        return null;
    }
 
    public TraderFollowUser findById(String id) {
//        return ApplicationUtil.executeGet(id, TraderFollowUser.class);
        TraderFollowUser traderFollowUser = traderFollowUserMapper.selectById(id);
        return traderFollowUser;
    }
 
}