package top.lvzhiqiang.controller; import com.alibaba.fastjson.JSONObject; import org.springframework.web.bind.annotation.*; import top.lvzhiqiang.dto.R; import top.lvzhiqiang.entity.CoinApiConfig; import top.lvzhiqiang.entity.CoinWatchlist; import top.lvzhiqiang.enumeration.ResultCodeEnum; import top.lvzhiqiang.exception.BusinessException; import top.lvzhiqiang.exception.ParameterException; import top.lvzhiqiang.mapper.CoinMapper; import top.lvzhiqiang.service.CoinApiConfigService; import top.lvzhiqiang.service.CoinService; import top.lvzhiqiang.service.impl.CoinServiceImpl; import top.lvzhiqiang.util.DateUtils; import top.lvzhiqiang.util.StringUtils; import javax.annotation.Resource; import java.time.LocalDate; import java.util.List; /** * Coin Controller * * @author lvzhiqiang * 2023/9/5 15:23 */ @RestController @RequestMapping("/coin") public class CoinController { @Resource private CoinService coinService; @Resource private CoinApiConfigService coinApiConfigService; @Resource private CoinMapper coinMapper; /** * 获取API配置信息 * * @author lvzhiqiang * 2023/9/5 15:23 */ @PostMapping("/findApiConfig") public List findApiConfig(String userName) { if (userName == null) { throw new ParameterException("用户名为空!"); } List all = coinApiConfigService.findByParams(null, null, null, null, 1, userName); return all; } /** * 主查询 * * @author lvzhiqiang * 2023/9/5 15:23 */ @PostMapping("/mainSearch") public Object mainSearch(@RequestBody JSONObject params) throws Exception { if (!params.containsKey("nameEn")) { throw new ParameterException("参数错误!"); } return coinService.mainSearch(params); } @PostMapping("/getCurrentHoldingTotalAmout") public Object getCurrentHoldingTotalAmout(@RequestBody JSONObject params) throws Exception { if (!params.containsKey("nameEn")) { throw new ParameterException("参数错误!"); } return coinService.getCurrentHoldingTotalAmout(params); } @GetMapping("/orderDetail/{trackingNo}") public String orderDetail(@PathVariable String trackingNo) { if (StringUtils.isEmpty(trackingNo)) { return "跟单号为空!"; } return coinService.orderDetail(trackingNo); } @GetMapping("/orderDetail2/{orderId}/{symbol}") public String orderDetail2(@PathVariable String orderId, @PathVariable String symbol) { if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(symbol)) { return "订单ID何币对名称为空!"; } return coinService.orderDetail2(orderId, symbol); } @GetMapping("/watchlistDetail/{symbol}/{operationType}") public Object watchlistDetail(@PathVariable String symbol, @PathVariable String operationType) { if (StringUtils.isEmpty(symbol)) { throw new ParameterException("symbol不能为空!"); } if (StringUtils.isEmpty(operationType)) { throw new ParameterException("operationType不能为空!"); } return R.ok().data(coinService.watchlistDetail(symbol, operationType)); } @PostMapping("/watchlistUpdate") public Object watchlistUpdate(String symbol, String remark, String score) { if (StringUtils.isEmpty(symbol)) { throw new ParameterException("symbol不能为空!"); } if (StringUtils.isEmpty(remark) && StringUtils.isEmpty(score)) { throw new ParameterException("remark和score不能同时为空!"); } return R.ok().data(coinService.watchlistUpdate(symbol, remark, score)); } @GetMapping("/mainSearchDetail/{nameEn}/{id}") public Object mainSearchDetail(@PathVariable String nameEn, @PathVariable String id) { return coinService.mainSearchDetail(nameEn, id); } /** * insertOrUpdateWatchlist * * @author lvzhiqiang * 2024/1/20 15:42 */ @RequestMapping("/insertOrUpdateWatchlist") @ResponseBody 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) { if (StringUtils.isEmpty(crudType)) { throw new ParameterException("crudType为空!"); } if (StringUtils.isEmpty(userName)) { throw new ParameterException("userName为空!"); } else { JSONObject coinUser = coinMapper.findUserByUsername(userName); if (coinUser == null) { throw new ParameterException("用户不存在!"); } } if (StringUtils.isEmpty(symbol)) { throw new ParameterException("symbol为空!"); } if ("1".equals(crudType)) { // 新增 if (StringUtils.isEmpty(issuingDate) || null == cmcId || StringUtils.isEmpty(coingeckoId) || null == filterFlag) { throw new ParameterException("参数为空!"); } CoinWatchlist coinWatchlist = new CoinWatchlist(); coinWatchlist.setSymbol(symbol.trim()); coinWatchlist.setTrackCategory(StringUtils.isEmpty(trackCategory) ? "" : trackCategory); coinWatchlist.setTrackCategory2(StringUtils.isEmpty(trackCategory2) ? "" : trackCategory2); coinWatchlist.setIssuingDate(LocalDate.parse(issuingDate, DateUtils.dateFormatter)); coinWatchlist.setCmcId(cmcId); coinWatchlist.setCoingeckoId(coingeckoId); coinWatchlist.setCoingeckoUrl(coingeckoUrl); coinWatchlist.setFeixiaohaoUrl(feixiaohaoUrl); coinWatchlist.setFilterFlag(filterFlag); coinMapper.insertOrUpdateWatchlist(coinWatchlist); // init data coinService.initWatchlist(coinWatchlist); } else if ("2".equals(crudType)) { // 修改 CoinWatchlist coinWatchlist = coinMapper.findWatchlistBySymbol(symbol); if (coinWatchlist == null) { throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "symbol 不存在!"); } if (StringUtils.isNotEmpty(trackCategory)) { coinWatchlist.setTrackCategory(trackCategory); } if (StringUtils.isNotEmpty(trackCategory2)) { coinWatchlist.setTrackCategory2(trackCategory2); } if (StringUtils.isNotEmpty(issuingDate)) { coinWatchlist.setIssuingDate(LocalDate.parse(issuingDate, DateUtils.dateFormatter)); } if (null != cmcId) { coinWatchlist.setCmcId(cmcId); } if (StringUtils.isNotEmpty(coingeckoId)) { coinWatchlist.setCoingeckoId(coingeckoId); } if (StringUtils.isNotEmpty(coingeckoUrl)) { coinWatchlist.setCoingeckoUrl(coingeckoUrl); } if (StringUtils.isNotEmpty(feixiaohaoUrl)) { coinWatchlist.setFeixiaohaoUrl(feixiaohaoUrl); } if (null != filterFlag) { coinWatchlist.setFilterFlag(filterFlag); } coinMapper.insertOrUpdateWatchlist(coinWatchlist); } return R.ok().data("success"); } @PostMapping("/serverMonitorAlarm") public void serverMonitorAlarm(@RequestBody JSONObject params) { StringBuffer sb = new StringBuffer(); params.forEach((k, v) -> { sb.append(k).append(":").append(v).append("\n"); }); if (sb.length() <= 0) { return; } sb.insert(0, "服务器监控告警\n"); coinService.monitorAlarm(sb.delete(sb.length() - 1, sb.length()).toString(), CoinServiceImpl.JOB_ALARM_MODE_CHAT_BOT); } @PostMapping("/login") public Object login(String username, String password) { if (StringUtils.isEmpty(username)) { throw new ParameterException("用户名不能为空!"); } if (StringUtils.isEmpty(password)) { throw new ParameterException("密码不能为空"); } return coinService.login(username, password); } }