1
zyy
2 days ago a8e2dbcd82859a3f972192c60b95cba4defe1738
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
package com.yami.trading.api.controller.trader;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.trading.bean.contract.domain.ContractOrder;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.trader.domain.Trader;
import com.yami.trading.bean.trader.domain.TraderFollowUser;
import com.yami.trading.bean.trader.domain.TraderUser;
import com.yami.trading.common.constants.Constants;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.common.web.ResultObject;
import com.yami.trading.security.common.util.SecurityUtils;
import com.yami.trading.service.contract.ContractOrderService;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.trader.TraderFollowUserOrderService;
import com.yami.trading.service.trader.TraderFollowUserService;
import com.yami.trading.service.trader.TraderService;
import com.yami.trading.service.trader.TraderUserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * 用户api接口,累计数�?
 */
@RestController
@CrossOrigin
@RequestMapping()
public class ApiTraderUserController {
    /**
     * 
     */
    private static final long serialVersionUID = -3217562997540384508L;
    /**
     * 交易员api接口
     */
    private static Log logger = LogFactory.getLog(ApiTraderUserController.class);
    
    @Autowired
    private TraderService traderService;
    
    @Autowired
    private TraderUserService traderUserService;
    
    @Autowired
    private TraderFollowUserService traderFollowUserService;
    
    @Autowired
    private TraderFollowUserOrderService traderFollowUserOrderService;
    
    @Autowired
    private ContractOrderService contractOrderService;
    
//    private String id;
//    private String orderBy_type;
//    private String symbol;
 
    @Autowired
    private ItemService itemService;
 
    /**
     * 查询类型 orders 我的跟单 ,trader 我的交易
     */
//    private String type;
    /**
     * 我的跟单查询类型�? orders 当前委托�? ,hisorders 历史委托�?
     */
//    private String type_order;
 
//    private String name;
 
//    private int page_no;
    
    private final String action = "/api/traderUser!";
 
    @RequestMapping(action + "home.action")
    public Object home() {
        ResultObject resultObject = new ResultObject();
        String partyId = SecurityUtils.getCurrentUserId();
        Map<String, Object> map = new HashMap<>();
        int followNumber = 0;
        BigDecimal followAmount = BigDecimal.ZERO;
        BigDecimal followProfit = BigDecimal.ZERO;
        double totalProfit = 0d;
        // 正在进行中的跟单
        List<ContractOrder> contractOrderServiceList = contractOrderService.selectContractOrderByUserIdAndFollowAndState(partyId, ContractOrder.ORDER_FOLLOW, ContractOrder.STATE_SUBMITTED);
        TraderUser traderUser = traderUserService.saveTraderUserByPartyId(partyId);
        if(null != contractOrderServiceList) {
            followNumber = contractOrderServiceList.size(); // 跟单中的订单数
            followAmount = contractOrderServiceList.stream().map(ContractOrder::getDeposit).reduce(BigDecimal.ZERO, BigDecimal::add); // 跟单USDT
            followProfit = contractOrderServiceList.stream().map(ContractOrder::getProfit).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        if(null != traderUser) {
            totalProfit = traderUser.getProfit();
        }
        map.put("followNumber", followNumber);
        map.put("followAmount", followAmount);
        map.put("followProfit", followProfit);
        map.put("totalProfit", totalProfit);
        resultObject.setCode("0");
        resultObject.setMsg("请求成功");
        resultObject.setData(map);
        return resultObject;
    }
 
    @RequestMapping(action + "get.action")
    public Object get(HttpServletRequest request) {
        ResultObject resultObject = new ResultObject();
        String type = request.getParameter("type");
        String page_no = request.getParameter("page_no");
        try {
            if (StringUtils.isNullOrEmpty(page_no)) {
                page_no = "1";
            }
            if (!StringUtils.isInteger(page_no)) {
                throw new YamiShopBindException("页码不是整数");
            }
            if (Integer.valueOf(page_no).intValue() <= 0) {
                throw new YamiShopBindException("页码不能小于等于0");
            }
            Page<T> page = new Page<>(1, 1000000);
 
            String partyId = SecurityUtils.getCurrentUserId();
 
            TraderUser data = traderUserService.saveTraderUserByPartyId(partyId);
 
            resultObject.setData(bulidData(data, type, page));
            resultObject.setCode("0");
        } catch (BusinessException e) {
            resultObject.setCode("1");
            resultObject.setMsg(e.getMessage());
        } catch (Exception e) {
            resultObject.setCode("1");
            resultObject.setMsg("程序错误");
            logger.error("error:", e);
        }
 
        return resultObject;
 
    }
 
    private Map<String, Object> bulidData(TraderUser entity, String type, Page page) throws ParseException {
 
        List<Map<String, Object>> trader_order = new ArrayList<Map<String, Object>>();
        List<TraderFollowUser> follow_users = new ArrayList<TraderFollowUser>();
 
        List<Map<String, Object>> follow_traders = new ArrayList<Map<String, Object>>();
        follow_users = traderFollowUserService.findByPartyId(entity.getPartyId());
        double folllow_trader_num = 0;
        if (follow_users != null) {
            folllow_trader_num = follow_users.size();
        }
 
        /**
         * 跟随的交易员
         */
        if ("trader".equals(type)) {
            if (follow_users != null) {
                for (TraderFollowUser user : follow_users) {
                    Trader trader = traderService.findByPartyId(user.getTraderPartyId());
                    Item item = itemService.findBySymbol(trader.getSymbols());
                    Map<String, Object> follow_trader = new HashMap<String, Object>();
                    follow_trader.put("profit", user.getProfit());
                    follow_trader.put("profitRation", BigDecimal.valueOf(user.getProfit()).divide(BigDecimal.valueOf(user.getAmountSum()), RoundingMode.HALF_UP));
                    follow_trader.put("amountSum", user.getAmountSum());
                    follow_trader.put("username", trader.getName());
                    String path = Constants.WEB_URL + "/public/showimg!showImg.action?imagePath=" + trader.getImg();
                    follow_trader.put("img", path);
                    follow_trader.put("id", trader.getUuid());
                    follow_trader.put("followState", "1");
                    follow_trader.put("followType", user.getFollowType());
                    follow_trader.put("volume", user.getVolume());
                    follow_trader.put("volumeMax", user.getVolumeMax());
                    follow_trader.put("followNow", trader.getFollowerNow());
                    follow_trader.put("followMax", trader.getFollowerMax());
                    follow_trader.put("symbols", item.getName());
                    follow_traders.add(follow_trader);
                }
            }
        }
        if ("orders".equals(type)) {
            trader_order = this.traderFollowUserOrderService.getPaged(page, entity.getPartyId(), ContractOrder.STATE_SUBMITTED);
        } else if("hisorders".equals(type)) {
            trader_order = this.traderFollowUserOrderService.getPaged(page, entity.getPartyId(), ContractOrder.STATE_CREATED);
        }
 
        Map<String, Object> map = new HashMap<String, Object>();
 
        map.put("orders", trader_order);
        map.put("traders", follow_traders);
        map.put("folllow_trader_num", folllow_trader_num);
 
        map.put("id", entity.getUuid());
 
        map.put("name", entity.getName());
        map.put("profit", entity.getProfit());
        map.put("amount_sum", entity.getAmountSum());
        Date date_now = new Date();// 取时�?
        int days = daysBetween(entity.getCreateTime(), date_now);
        if (days < 0) {
            days = 0;
        }
        map.put("create_days", days);
 
        return map;
 
    }
 
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
 
        return Integer.parseInt(String.valueOf(between_days));
    }
 
}