CoinController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package top.lvzhiqiang.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.springframework.web.bind.annotation.*;
  4. import top.lvzhiqiang.dto.R;
  5. import top.lvzhiqiang.entity.CoinApiConfig;
  6. import top.lvzhiqiang.entity.CoinWatchlist;
  7. import top.lvzhiqiang.enumeration.ResultCodeEnum;
  8. import top.lvzhiqiang.exception.BusinessException;
  9. import top.lvzhiqiang.exception.ParameterException;
  10. import top.lvzhiqiang.mapper.CoinMapper;
  11. import top.lvzhiqiang.service.CoinApiConfigService;
  12. import top.lvzhiqiang.service.CoinService;
  13. import top.lvzhiqiang.service.impl.CoinServiceImpl;
  14. import top.lvzhiqiang.util.DateUtils;
  15. import top.lvzhiqiang.util.StringUtils;
  16. import javax.annotation.Resource;
  17. import java.time.LocalDate;
  18. import java.util.List;
  19. /**
  20. * Coin Controller
  21. *
  22. * @author lvzhiqiang
  23. * 2023/9/5 15:23
  24. */
  25. @RestController
  26. @RequestMapping("/coin")
  27. public class CoinController {
  28. @Resource
  29. private CoinService coinService;
  30. @Resource
  31. private CoinApiConfigService coinApiConfigService;
  32. @Resource
  33. private CoinMapper coinMapper;
  34. /**
  35. * 获取API配置信息
  36. *
  37. * @author lvzhiqiang
  38. * 2023/9/5 15:23
  39. */
  40. @PostMapping("/findApiConfig")
  41. public List<CoinApiConfig> findApiConfig(String userName) {
  42. if (userName == null) {
  43. throw new ParameterException("用户名为空!");
  44. }
  45. List<CoinApiConfig> all = coinApiConfigService.findByParams(null, null, null, null, 1, userName);
  46. return all;
  47. }
  48. /**
  49. * 主查询
  50. *
  51. * @author lvzhiqiang
  52. * 2023/9/5 15:23
  53. */
  54. @PostMapping("/mainSearch")
  55. public Object mainSearch(@RequestBody JSONObject params) throws Exception {
  56. if (!params.containsKey("nameEn")) {
  57. throw new ParameterException("参数错误!");
  58. }
  59. return coinService.mainSearch(params);
  60. }
  61. @PostMapping("/getCurrentHoldingTotalAmout")
  62. public Object getCurrentHoldingTotalAmout(@RequestBody JSONObject params) throws Exception {
  63. if (!params.containsKey("nameEn")) {
  64. throw new ParameterException("参数错误!");
  65. }
  66. return coinService.getCurrentHoldingTotalAmout(params);
  67. }
  68. @GetMapping("/orderDetail/{trackingNo}")
  69. public String orderDetail(@PathVariable String trackingNo) {
  70. if (StringUtils.isEmpty(trackingNo)) {
  71. return "跟单号为空!";
  72. }
  73. return coinService.orderDetail(trackingNo);
  74. }
  75. @GetMapping("/orderDetail2/{orderId}/{symbol}")
  76. public String orderDetail2(@PathVariable String orderId, @PathVariable String symbol) {
  77. if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(symbol)) {
  78. return "订单ID何币对名称为空!";
  79. }
  80. return coinService.orderDetail2(orderId, symbol);
  81. }
  82. @GetMapping("/watchlistDetail/{symbol}/{operationType}")
  83. public Object watchlistDetail(@PathVariable String symbol, @PathVariable String operationType) {
  84. if (StringUtils.isEmpty(symbol)) {
  85. throw new ParameterException("symbol不能为空!");
  86. }
  87. if (StringUtils.isEmpty(operationType)) {
  88. throw new ParameterException("operationType不能为空!");
  89. }
  90. return R.ok().data(coinService.watchlistDetail(symbol, operationType));
  91. }
  92. @PostMapping("/watchlistUpdate")
  93. public Object watchlistUpdate(String symbol, String remark, String score) {
  94. if (StringUtils.isEmpty(symbol)) {
  95. throw new ParameterException("symbol不能为空!");
  96. }
  97. if (StringUtils.isEmpty(remark) && StringUtils.isEmpty(score)) {
  98. throw new ParameterException("remark和score不能同时为空!");
  99. }
  100. return R.ok().data(coinService.watchlistUpdate(symbol, remark, score));
  101. }
  102. @GetMapping("/mainSearchDetail/{nameEn}/{id}")
  103. public Object mainSearchDetail(@PathVariable String nameEn, @PathVariable String id) {
  104. return coinService.mainSearchDetail(nameEn, id);
  105. }
  106. /**
  107. * insertOrUpdateWatchlist
  108. *
  109. * @author lvzhiqiang
  110. * 2024/1/20 15:42
  111. */
  112. @RequestMapping("/insertOrUpdateWatchlist")
  113. @ResponseBody
  114. public R insertOrUpdateWatchlist(String symbol, String trackCategory, String trackCategory2, String issuingDate, Long cmcId, String coingeckoId, String coingeckoUrl, String feixiaohaoUrl, Integer filterFlag, String crudType, String userName) {
  115. if (StringUtils.isEmpty(crudType)) {
  116. throw new ParameterException("crudType为空!");
  117. }
  118. if (StringUtils.isEmpty(userName)) {
  119. throw new ParameterException("userName为空!");
  120. } else {
  121. JSONObject coinUser = coinMapper.findUserByUsername(userName);
  122. if (coinUser == null) {
  123. throw new ParameterException("用户不存在!");
  124. }
  125. }
  126. if (StringUtils.isEmpty(symbol)) {
  127. throw new ParameterException("symbol为空!");
  128. }
  129. if ("1".equals(crudType)) {
  130. // 新增
  131. if (StringUtils.isEmpty(issuingDate) || null == cmcId || StringUtils.isEmpty(coingeckoId) || null == filterFlag) {
  132. throw new ParameterException("参数为空!");
  133. }
  134. CoinWatchlist coinWatchlist = new CoinWatchlist();
  135. coinWatchlist.setSymbol(symbol.trim());
  136. coinWatchlist.setTrackCategory(StringUtils.isEmpty(trackCategory) ? "" : trackCategory);
  137. coinWatchlist.setTrackCategory2(StringUtils.isEmpty(trackCategory2) ? "" : trackCategory2);
  138. coinWatchlist.setIssuingDate(LocalDate.parse(issuingDate, DateUtils.dateFormatter));
  139. coinWatchlist.setCmcId(cmcId);
  140. coinWatchlist.setCoingeckoId(coingeckoId);
  141. coinWatchlist.setCoingeckoUrl(coingeckoUrl);
  142. coinWatchlist.setFeixiaohaoUrl(feixiaohaoUrl);
  143. coinWatchlist.setFilterFlag(filterFlag);
  144. coinMapper.insertOrUpdateWatchlist(coinWatchlist);
  145. // init data
  146. coinService.initWatchlist(coinWatchlist);
  147. } else if ("2".equals(crudType)) {
  148. // 修改
  149. CoinWatchlist coinWatchlist = coinMapper.findWatchlistBySymbol(symbol);
  150. if (coinWatchlist == null) {
  151. throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "symbol 不存在!");
  152. }
  153. if (StringUtils.isNotEmpty(trackCategory)) {
  154. coinWatchlist.setTrackCategory(trackCategory);
  155. }
  156. if (StringUtils.isNotEmpty(trackCategory2)) {
  157. coinWatchlist.setTrackCategory2(trackCategory2);
  158. }
  159. if (StringUtils.isNotEmpty(issuingDate)) {
  160. coinWatchlist.setIssuingDate(LocalDate.parse(issuingDate, DateUtils.dateFormatter));
  161. }
  162. if (null != cmcId) {
  163. coinWatchlist.setCmcId(cmcId);
  164. }
  165. if (StringUtils.isNotEmpty(coingeckoId)) {
  166. coinWatchlist.setCoingeckoId(coingeckoId);
  167. }
  168. if (StringUtils.isNotEmpty(coingeckoUrl)) {
  169. coinWatchlist.setCoingeckoUrl(coingeckoUrl);
  170. }
  171. if (StringUtils.isNotEmpty(feixiaohaoUrl)) {
  172. coinWatchlist.setFeixiaohaoUrl(feixiaohaoUrl);
  173. }
  174. if (null != filterFlag) {
  175. coinWatchlist.setFilterFlag(filterFlag);
  176. }
  177. coinMapper.insertOrUpdateWatchlist(coinWatchlist);
  178. }
  179. return R.ok().data("success");
  180. }
  181. @PostMapping("/serverMonitorAlarm")
  182. public void serverMonitorAlarm(@RequestBody JSONObject params) {
  183. StringBuffer sb = new StringBuffer();
  184. params.forEach((k, v) -> {
  185. sb.append(k).append(":").append(v).append("\n");
  186. });
  187. if (sb.length() <= 0) {
  188. return;
  189. }
  190. sb.insert(0, "服务器监控告警\n");
  191. coinService.monitorAlarm(sb.delete(sb.length() - 1, sb.length()).toString(), CoinServiceImpl.JOB_ALARM_MODE_CHAT_BOT);
  192. }
  193. @PostMapping("/login")
  194. public Object login(String username, String password) {
  195. if (StringUtils.isEmpty(username)) {
  196. throw new ParameterException("用户名不能为空!");
  197. }
  198. if (StringUtils.isEmpty(password)) {
  199. throw new ParameterException("密码不能为空");
  200. }
  201. return coinService.login(username, password);
  202. }
  203. }