zj
2024-06-03 4afe73cb84c5a609662b8b4ee20693de9b86b9a3
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package com.nq.service.impl;
 
 
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.nq.common.ServerResponse;
import com.nq.common.lanage.MessageLocaleResolver;
import com.nq.dao.StockIndexMapper;
import com.nq.pojo.StockIndex;
import com.nq.pojo.User;
import com.nq.service.IStockIndexService;
import com.nq.service.IStockOptionService;
import com.nq.service.IUserService;
import com.nq.utils.HttpClientRequest;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.redis.RedisShardedPoolUtils;
import com.nq.vo.stock.MarketVO;
import com.nq.vo.stockindex.StockIndexVO;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
 
@Service("iStockIndexService")
public class StockIndexServiceImpl implements IStockIndexService {
    private static final Logger log = LoggerFactory.getLogger(StockIndexServiceImpl.class);
 
 
    @Resource
    StockIndexMapper stockIndexMapper;
 
    @Autowired
    IUserService iUserService;
 
    @Autowired
    IStockOptionService iStockOptionService;
 
 
    public ServerResponse listByAdmin(Integer homeShow, Integer listShow, Integer transState, String indexCode, String indexName, int pageNum, int pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
 
        List<StockIndex> stockIndexList = this.stockIndexMapper.listByAdmin(homeShow, listShow, transState, indexCode, indexName);
        List<StockIndexVO> stockIndexVOS = Lists.newArrayList();
        for (StockIndex stockIndex : stockIndexList) {
            StockIndexVO stockIndexVO = assembleStockIndexVO(stockIndex);
            stockIndexVOS.add(stockIndexVO);
        }
        PageInfo pageInfo = new PageInfo(stockIndexList);
        pageInfo.setList(stockIndexVOS);
 
        return ServerResponse.createBySuccess(pageInfo);
    }
 
    private StockIndexVO assembleStockIndexVO(StockIndex stockIndex) {
        StockIndexVO stockIndexVO = new StockIndexVO();
 
        stockIndexVO.setId(stockIndex.getId());
        stockIndexVO.setIndexName(stockIndex.getIndexName());
        stockIndexVO.setIndexCode(stockIndex.getIndexCode());
        stockIndexVO.setIndexGid(stockIndex.getIndexGid());
        stockIndexVO.setHomeShow(stockIndex.getHomeShow());
        stockIndexVO.setListShow(stockIndex.getListShow());
        stockIndexVO.setTransState(stockIndex.getTransState());
        stockIndexVO.setDepositAmt(stockIndex.getDepositAmt());
        stockIndexVO.setTransFee(stockIndex.getTransFee());
        stockIndexVO.setEachPoint(stockIndex.getEachPoint());
        stockIndexVO.setMinNum(stockIndex.getMinNum());
        stockIndexVO.setMaxNum(stockIndex.getMaxNum());
        stockIndexVO.setAddTime(stockIndex.getAddTime());
        stockIndexVO.setTDesc(stockIndex.getTDesc());
 
 
 
        MarketVO marketVO = querySingleIndex(stockIndex.getIndexGid());
 
        stockIndexVO.setCurrentPoint(marketVO.getNowPrice());
        stockIndexVO.setFloatPoint(marketVO.getIncrease());
        stockIndexVO.setFloatRate(marketVO.getIncreaseRate());
        stockIndexVO.setType(marketVO.getType());
        return stockIndexVO;
    }
 
 
    public ServerResponse updateIndex(StockIndex stockIndex,HttpServletRequest request) {
        if (stockIndex.getId() == null) {
            return ServerResponse.createByErrorMsg("Modify id cannot be empty",request.getHeader(MessageLocaleResolver.LANG));
        }
 
        StockIndex dbindex = this.stockIndexMapper.selectByPrimaryKey(stockIndex.getId());
        dbindex.setHomeShow(stockIndex.getHomeShow());
        dbindex.setListShow(stockIndex.getListShow());
        dbindex.setTransState(stockIndex.getTransState());
        dbindex.setDepositAmt(stockIndex.getDepositAmt());
        dbindex.setTransFee(stockIndex.getTransFee());
        dbindex.setEachPoint(stockIndex.getEachPoint());
        dbindex.setMinNum(stockIndex.getMinNum());
        dbindex.setMaxNum(stockIndex.getMaxNum());
 
        int updateCount = this.stockIndexMapper.updateByPrimaryKey(dbindex);
        if (updateCount > 0) {
            return ServerResponse.createBySuccessMsg("Successfully modified",request.getHeader(MessageLocaleResolver.LANG));
        }
        return ServerResponse.createByErrorMsg("fail to edit",request.getHeader(MessageLocaleResolver.LANG));
    }
 
 
    public ServerResponse addIndex(StockIndex stockIndex,HttpServletRequest request) {
        log.info("name = {} code = {} gid = {}", new Object[]{stockIndex
                .getIndexName(), stockIndex.getIndexCode(), stockIndex.getIndexGid()});
        if (StringUtils.isBlank(stockIndex.getIndexName()) ||
                StringUtils.isBlank(stockIndex.getIndexCode()) ||
                StringUtils.isBlank(stockIndex.getIndexGid())) {
            return ServerResponse.createByErrorMsg("Parameter cannot be empty",request.getHeader(MessageLocaleResolver.LANG));
        }
 
        StockIndex nameIndex = this.stockIndexMapper.selectIndexByName(stockIndex.getIndexName());
        if (nameIndex != null) {
            return ServerResponse.createByErrorMsg("Index name already exists",request.getHeader(MessageLocaleResolver.LANG));
        }
        StockIndex codeIndex = this.stockIndexMapper.selectIndexByCode(stockIndex.getIndexCode());
        if (codeIndex != null) {
            return ServerResponse.createByErrorMsg("Index code already exists",request.getHeader(MessageLocaleResolver.LANG));
        }
 
        stockIndex.setAddTime(new Date());
        int insertCount = this.stockIndexMapper.insert(stockIndex);
 
        if (insertCount > 0) {
            return ServerResponse.createBySuccessMsg("Added successfully",request.getHeader(MessageLocaleResolver.LANG));
        }
        return ServerResponse.createByErrorMsg("add failed",request.getHeader(MessageLocaleResolver.LANG));
    }
 
 
    public ServerResponse queryHomeIndex() {
//        List<StockIndex> list = this.stockIndexMapper.queryHomeIndex();
//        List<StockIndexVO> stockIndexVOS = Lists.newArrayList();
//        for (StockIndex stockIndex : list) {
//            StockIndexVO stockIndexVO = assembleStockIndexVO(stockIndex);
//            int random = (int) (Math.random() * 3 + 1);
//            stockIndexVO.setRandom(random);
//            stockIndexVOS.add(stockIndexVO);
//        }
        String result = null;
        com.alibaba.fastjson2.JSONObject json = null;
        String url = PropertiesUtil.getProperty("home.index.recommend.url");
        try {
                result = HttpClientRequest.doGet(url);
                json = com.alibaba.fastjson2.JSONObject.parseObject(result);
                int random = (int) (Math.random() * 3 + 1);
                json.put("random",random);
        } catch (Exception e) {
            log.error("首页推荐出错", e);
        }
        return ServerResponse.createBySuccess(json);
    }
 
 
    public ServerResponse queryListIndex(HttpServletRequest request) {
        List<StockIndex> list = this.stockIndexMapper.queryListIndex();
        List<StockIndexVO> stockIndexVOS = Lists.newArrayList();
        User user = iUserService.getCurrentUser(request);
        for (StockIndex stockIndex : list) {
            StockIndexVO stockIndexVO = assembleStockIndexVO(stockIndex);
            //是否添加自選
            if (user == null) {
                stockIndexVO.setIsOption("0");
            } else {
                stockIndexVO.setIsOption(iStockOptionService.isMyOption(user.getId(), stockIndex.getIndexCode(),request));
            }
            stockIndexVOS.add(stockIndexVO);
        }
        return ServerResponse.createBySuccess(stockIndexVOS);
    }
 
 
    public ServerResponse queryTransIndex(Integer indexId,HttpServletRequest request) {
        StockIndex stockIndex = this.stockIndexMapper.selectByPrimaryKey(indexId);
        if (1 == stockIndex.getTransState().intValue()) {
            return ServerResponse.createBySuccessMsg("tradable",request.getHeader(MessageLocaleResolver.LANG));
        }
        return ServerResponse.createByErrorMsg("Not tradable",request.getHeader(MessageLocaleResolver.LANG));
    }
 
 
    public MarketVO querySingleIndex(String indexCode) {
        MarketVO marketVO = null;
        if (indexCode.contains("hk")||indexCode.contains("us")){
            String index = RedisShardedPoolUtils.get(indexCode, 3);
            JSONObject jsonObject = JSONObject.parseObject(index);
            marketVO = new MarketVO();
            marketVO.setName(jsonObject.getString("f14"));
            marketVO.setNowPrice(jsonObject.getString("f2"));
            marketVO.setIncrease(jsonObject.getString("f4"));
            marketVO.setIncreaseRate(jsonObject.getString("f3"));
            marketVO.setType(jsonObject.getString("f13"));
        }else {
            String market_url = PropertiesUtil.getProperty("sina.single.market.url");
 
            String result = null;
            try {
                market_url = market_url + indexCode;
                //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //System.out.print("指数请求开始,时间:"+sdf.format(new Date())+",market_url:"+market_url + "\n");
 
                result = HttpClientRequest.doGet(market_url);
                //System.out.print("指数请求结束,时间:"+sdf.format(new Date())+",result:"+result + "\n");
            } catch (Exception e) {
                log.error("获取 大盘指数 出错 e = {}", e);
            }
 
 
            try {
                if (StringUtils.isNotBlank(result)) {
                    result = result.substring(result.indexOf("\"") + 1, result.lastIndexOf("\""));
 
                    marketVO = new MarketVO();
                    if (result.contains(",")) {
                        String[] sh01_arr = result.split(",");
                        marketVO.setName(sh01_arr[0]);
                        marketVO.setNowPrice(sh01_arr[1]);
                        marketVO.setIncrease(sh01_arr[2]);
                        marketVO.setIncreaseRate(sh01_arr[3]);
                    }
                }
            } catch (Exception e) {
                log.error("转换大盘指数出错 str = {} ,  e = {}", result, e);
            }
        }
        return marketVO;
    }
 
 
    public StockIndex selectIndexById(Integer indexId) {
        return this.stockIndexMapper.selectByPrimaryKey(indexId);
    }
    //指数新闻
    @Override
    public ServerResponse queryIndexNews() {
        String url = PropertiesUtil.getProperty("hk.index.introduction.url");
        String result = HttpClientRequest.doGet(url);
        com.alibaba.fastjson2.JSONObject json = com.alibaba.fastjson2.JSONObject.parseObject(result);
        return ServerResponse.createBySuccess(json);
    }
 
    //美股港股指数定时任务
    @Override
    public void otherIndexTask() {
        String[] keys = {"hk", "us"};
        //当前毫秒时间戳
        long now = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        for (String key : keys) {
            String url = null;
            if ("hk".equals(key)) {
                url = PropertiesUtil.getProperty("hk.index.url") + now;
            }else {
                url = PropertiesUtil.getProperty("us.index.url") + now;
            }
 
            String result = HttpClientRequest.doGet(url);
            JSONObject json = JSONObject.parseObject(result);
            //取出data里的diff数组
            ArrayList diff = (ArrayList) json.getJSONObject("data").get("diff");
 
            for (int i = 0; i < diff.size(); i++) {
                JSONObject data = (JSONObject) diff.get(i);
                String stockCode = data.getString("f12");
                StockIndex stock = stockIndexMapper.selectIndexByCode(stockCode);
            if (stock == null) {
                stock = new StockIndex();
//                String spell = GetPyByChinese.converterToFirstSpell(data.getString("f14"));
                stock.setIndexCode(data.getString("f12"));
                stock.setIndexName(data.getString("f14"));
                stock.setListShow(1);
                stock.setIndexGid(key+data.getString("f12"));
                stock.setHomeShow(1);
                stock.setTransState(1);
                stock.setAddTime(new Date());
                stock.setDepositAmt(10000);
                stock.setTransFee(200);
                stock.setMaxNum(100);
                stock.setMinNum(1);
                stock.setEachPoint(300);
                stockIndexMapper.insert(stock);
                sb.append("新增股票:").append(stock.getIndexName()).append(stock.getIndexCode()).append("/n");
            }
 
            }
            log.info(key + "股新增股票:{}", sb.toString());
        }
    }
 
    @Override
    public ServerResponse queryListIndexByCode(List<String> code) {
        List<StockIndexVO> stockIndexVOS = Lists.newArrayList();
        for (String s : code) {
            StockIndex stockIndex = stockIndexMapper.selectIndexByCode(s);
            if (stockIndex == null) {
                return ServerResponse.createByErrorMsg("股票代码不存在","en");
            }
            StockIndexVO stockIndexVO = assembleStockIndexVO(stockIndex);
            stockIndexVOS.add(stockIndexVO);
        }
        return ServerResponse.createBySuccess(stockIndexVOS);
    }
}