CoinController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package top.lvzhiqiang.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.springframework.web.bind.annotation.*;
  4. import org.springframework.web.multipart.MultipartFile;
  5. import top.lvzhiqiang.config.InitRunner;
  6. import top.lvzhiqiang.dto.R;
  7. import top.lvzhiqiang.entity.CoinApiConfig;
  8. import top.lvzhiqiang.entity.CoinWatchlist;
  9. import top.lvzhiqiang.entity.FileImage;
  10. import top.lvzhiqiang.enumeration.ResultCodeEnum;
  11. import top.lvzhiqiang.exception.BusinessException;
  12. import top.lvzhiqiang.exception.ParameterException;
  13. import top.lvzhiqiang.mapper.CoinMapper;
  14. import top.lvzhiqiang.service.CoinApiConfigService;
  15. import top.lvzhiqiang.service.CoinService;
  16. import top.lvzhiqiang.util.DateUtils;
  17. import top.lvzhiqiang.util.FtpUtil;
  18. import top.lvzhiqiang.util.StringUtils;
  19. import javax.annotation.Resource;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.math.BigDecimal;
  23. import java.math.RoundingMode;
  24. import java.time.LocalDate;
  25. import java.util.List;
  26. /**
  27. * Coin Controller
  28. *
  29. * @author lvzhiqiang
  30. * 2023/9/5 15:23
  31. */
  32. @RestController
  33. @RequestMapping("/coin")
  34. public class CoinController {
  35. @Resource
  36. private CoinService coinService;
  37. @Resource
  38. private CoinApiConfigService coinApiConfigService;
  39. @Resource
  40. private CoinMapper coinMapper;
  41. /**
  42. * 获取API配置信息
  43. *
  44. * @author lvzhiqiang
  45. * 2023/9/5 15:23
  46. */
  47. @PostMapping("/findApiConfig")
  48. public List<CoinApiConfig> findApiConfig() {
  49. List<CoinApiConfig> all = coinApiConfigService.findByParams(null, null, null, null, 1);
  50. return all;
  51. }
  52. /**
  53. * 主查询
  54. *
  55. * @author lvzhiqiang
  56. * 2023/9/5 15:23
  57. */
  58. @PostMapping("/mainSearch")
  59. public Object mainSearch(@RequestBody JSONObject params) throws Exception {
  60. if (!params.containsKey("nameEn")) {
  61. throw new ParameterException("参数错误!");
  62. }
  63. return coinService.mainSearch(params);
  64. }
  65. @GetMapping("/orderDetail/{trackingNo}")
  66. public String orderDetail(@PathVariable String trackingNo) {
  67. if (StringUtils.isEmpty(trackingNo)) {
  68. return "跟单号为空!";
  69. }
  70. return coinService.orderDetail(trackingNo);
  71. }
  72. @GetMapping("/orderDetail2/{orderId}/{symbol}")
  73. public String orderDetail2(@PathVariable String orderId, @PathVariable String symbol) {
  74. if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(symbol)) {
  75. return "订单ID何币对名称为空!";
  76. }
  77. return coinService.orderDetail2(orderId, symbol);
  78. }
  79. @GetMapping("/watchlistDetail/{symbol}/{operationType}")
  80. public Object watchlistDetail(@PathVariable String symbol, @PathVariable String operationType) {
  81. if (StringUtils.isEmpty(symbol)) {
  82. throw new ParameterException("symbol不能为空!");
  83. }
  84. if (StringUtils.isEmpty(operationType)) {
  85. throw new ParameterException("operationType不能为空!");
  86. }
  87. return R.ok().data(coinService.watchlistDetail(symbol, operationType));
  88. }
  89. @PostMapping("/watchlistUpdate")
  90. public Object watchlistUpdate(String symbol, String remark) {
  91. if (StringUtils.isEmpty(symbol)) {
  92. throw new ParameterException("symbol不能为空!");
  93. }
  94. if (StringUtils.isEmpty(remark)) {
  95. throw new ParameterException("remark不能为空!");
  96. }
  97. return R.ok().data(coinService.watchlistUpdate(symbol, remark));
  98. }
  99. /**
  100. * 上传图片
  101. *
  102. * @author lvzhiqiang
  103. * 2023/12/29 10:26
  104. */
  105. @RequestMapping("/InsertOrUpdateImg")
  106. @ResponseBody
  107. public R InsertOrUpdateImg(@RequestParam("file") MultipartFile file, String remark, Long categoryId, String id) {
  108. if (StringUtils.isEmpty(id) && (file == null || file.getSize() == 0)) {
  109. throw new ParameterException("文件为空!");
  110. }
  111. if (categoryId == null) {
  112. throw new ParameterException("categoryId为空!");
  113. }
  114. String imageUrl = "";
  115. String imageSize = "";
  116. if (StringUtils.isEmpty(id)) {
  117. try {
  118. // 1、给上传的图片生成新的文件名
  119. // 1.1获取原始文件名
  120. String oldName = file.getOriginalFilename();
  121. // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀
  122. String newName = FtpUtil.genImageName();
  123. newName = newName + oldName.substring(oldName.lastIndexOf("."));
  124. // 2、把图片上传到图片服务器
  125. // 2.1获取上传的io流
  126. InputStream input = file.getInputStream();
  127. // 2.2调用FtpUtil工具类进行上传
  128. boolean result = FtpUtil.uploadFile(newName, input);
  129. if (result) {
  130. //返回给前端图片访问路径
  131. imageUrl = LocalDate.now().format(DateUtils.dateFormatter_) + "/" + newName;
  132. imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
  133. FileImage fileImage = new FileImage();
  134. fileImage.setOldName(oldName);
  135. fileImage.setNewName(newName);
  136. fileImage.setSize(imageSize);
  137. fileImage.setPath(imageUrl);
  138. fileImage.setRemark(remark);
  139. fileImage.setCategoryId(categoryId);
  140. coinMapper.insertFileImage(fileImage);
  141. }
  142. } catch (IOException e) {
  143. e.printStackTrace();
  144. }
  145. JSONObject result = new JSONObject();
  146. result.put("imageUrl", FtpUtil.getBaseUrl() + imageUrl);
  147. result.put("imageSize", imageSize);
  148. return R.ok().data(result);
  149. } else {
  150. FileImage fileImage = coinMapper.findFileImageById(Long.valueOf(id));
  151. if (fileImage == null) {
  152. throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "ID 不存在!");
  153. }
  154. fileImage.setCategoryId(categoryId);
  155. if (StringUtils.isNotEmpty(remark)) {
  156. fileImage.setRemark(remark);
  157. }
  158. if (file != null && file.getSize() > 0) {
  159. try {
  160. String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue();
  161. boolean flag = FtpUtil.delFile(ftpBasePath + fileImage.getPath());
  162. if (flag) {
  163. String oldName = file.getOriginalFilename();
  164. String newName = FtpUtil.genImageName();
  165. newName = newName + oldName.substring(oldName.lastIndexOf("."));
  166. InputStream input = file.getInputStream();
  167. // 2.2调用FtpUtil工具类进行上传
  168. boolean result = FtpUtil.uploadFile(newName, input);
  169. if (result) {
  170. imageUrl = LocalDate.now().format(DateUtils.dateFormatter_) + "/" + newName;
  171. imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
  172. fileImage.setOldName(oldName);
  173. fileImage.setNewName(newName);
  174. fileImage.setSize(imageSize);
  175. fileImage.setPath(imageUrl);
  176. } else {
  177. throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "上传新文件失败!");
  178. }
  179. } else {
  180. throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "删除旧文件失败!");
  181. }
  182. } catch (Exception e) {
  183. e.printStackTrace();
  184. return R.error().message(e.getMessage());
  185. }
  186. }
  187. coinMapper.updateFileImage(fileImage);
  188. return R.ok().data("success");
  189. }
  190. }
  191. @RequestMapping("/deleteImgs/{imageId}")
  192. @ResponseBody
  193. public R deleteImgs(@PathVariable Long imageId) {
  194. if (imageId == null) {
  195. throw new ParameterException("imageId为空!");
  196. }
  197. FileImage fileImage = coinMapper.findFileImageById(imageId);
  198. if (fileImage == null) {
  199. throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "ID 不存在!");
  200. }
  201. try {
  202. String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue();
  203. boolean flag = FtpUtil.delFile(ftpBasePath + fileImage.getPath());
  204. if (flag) {
  205. coinMapper.deleteFileImageById(imageId);
  206. return R.ok();
  207. } else {
  208. return R.error().message("删除失败");
  209. }
  210. } catch (Exception e) {
  211. e.printStackTrace();
  212. return R.error().message(e.getMessage());
  213. }
  214. }
  215. /**
  216. * insertOrUpdateWatchlist
  217. *
  218. * @author lvzhiqiang
  219. * 2024/1/20 15:42
  220. */
  221. @RequestMapping("/insertOrUpdateWatchlist")
  222. @ResponseBody
  223. public R insertOrUpdateWatchlist(String symbol, String trackCategory, String issuingDate, Long cmcId, String coingeckoId, Integer filterFlag, String crudType) {
  224. if (StringUtils.isEmpty(crudType)) {
  225. throw new ParameterException("crudType为空!");
  226. }
  227. if (StringUtils.isEmpty(symbol)) {
  228. throw new ParameterException("symbol为空!");
  229. }
  230. if ("1".equals(crudType)) {
  231. // 新增
  232. if (StringUtils.isEmpty(trackCategory) || StringUtils.isEmpty(issuingDate) || null == cmcId || StringUtils.isEmpty(coingeckoId) || null == filterFlag) {
  233. throw new ParameterException("参数为空!");
  234. }
  235. CoinWatchlist coinWatchlist = new CoinWatchlist();
  236. coinWatchlist.setSymbol(symbol);
  237. coinWatchlist.setTrackCategory(trackCategory);
  238. coinWatchlist.setIssuingDate(LocalDate.parse(issuingDate, DateUtils.dateFormatter));
  239. coinWatchlist.setCmcId(cmcId);
  240. coinWatchlist.setCoingeckoId(coingeckoId);
  241. coinWatchlist.setFilterFlag(filterFlag);
  242. coinMapper.insertOrUpdateWatchlist(coinWatchlist);
  243. } else if ("2".equals(crudType)) {
  244. // 修改
  245. CoinWatchlist coinWatchlist = coinMapper.findWatchlistBySymbol(symbol);
  246. if (coinWatchlist == null) {
  247. throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "symbol 不存在!");
  248. }
  249. if (StringUtils.isNotEmpty(trackCategory)) {
  250. coinWatchlist.setTrackCategory(trackCategory);
  251. }
  252. if (StringUtils.isNotEmpty(issuingDate)) {
  253. coinWatchlist.setIssuingDate(LocalDate.parse(issuingDate, DateUtils.dateFormatter));
  254. }
  255. if (null != cmcId) {
  256. coinWatchlist.setCmcId(cmcId);
  257. }
  258. if (null != filterFlag) {
  259. coinWatchlist.setFilterFlag(filterFlag);
  260. }
  261. coinMapper.insertOrUpdateWatchlist(coinWatchlist);
  262. }
  263. return R.ok().data("success");
  264. }
  265. }