package top.lvzhiqiang.controller; import com.alibaba.fastjson.JSONObject; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import top.lvzhiqiang.config.InitRunner; import top.lvzhiqiang.dto.R; import top.lvzhiqiang.entity.CoinApiConfig; import top.lvzhiqiang.entity.CoinWatchlist; import top.lvzhiqiang.entity.FileImage; 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.util.DateUtils; import top.lvzhiqiang.util.FtpUtil; import top.lvzhiqiang.util.StringUtils; import javax.annotation.Resource; import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import java.math.RoundingMode; 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() { List all = coinApiConfigService.findByParams(null, null, null, null, 1); 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); } @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) { if (StringUtils.isEmpty(symbol)) { throw new ParameterException("symbol不能为空!"); } if (StringUtils.isEmpty(remark)) { throw new ParameterException("remark不能为空!"); } return R.ok().data(coinService.watchlistUpdate(symbol, remark)); } /** * 上传图片 * * @author lvzhiqiang * 2023/12/29 10:26 */ @RequestMapping("/InsertOrUpdateImg") @ResponseBody public R InsertOrUpdateImg(@RequestParam("file") MultipartFile file, String remark, Long categoryId, String id) { if (StringUtils.isEmpty(id) && (file == null || file.getSize() == 0)) { throw new ParameterException("文件为空!"); } if (categoryId == null) { throw new ParameterException("categoryId为空!"); } String imageUrl = ""; String imageSize = ""; if (StringUtils.isEmpty(id)) { try { // 1、给上传的图片生成新的文件名 // 1.1获取原始文件名 String oldName = file.getOriginalFilename(); // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀 String newName = FtpUtil.genImageName(); newName = newName + oldName.substring(oldName.lastIndexOf(".")); // 2、把图片上传到图片服务器 // 2.1获取上传的io流 InputStream input = file.getInputStream(); // 2.2调用FtpUtil工具类进行上传 boolean result = FtpUtil.uploadFile(newName, input); if (result) { //返回给前端图片访问路径 imageUrl = LocalDate.now().format(DateUtils.dateFormatter_) + "/" + newName; imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB"); FileImage fileImage = new FileImage(); fileImage.setOldName(oldName); fileImage.setNewName(newName); fileImage.setSize(imageSize); fileImage.setPath(imageUrl); fileImage.setRemark(remark); fileImage.setCategoryId(categoryId); coinMapper.insertFileImage(fileImage); } } catch (IOException e) { e.printStackTrace(); } JSONObject result = new JSONObject(); result.put("imageUrl", FtpUtil.getBaseUrl() + imageUrl); result.put("imageSize", imageSize); return R.ok().data(result); } else { FileImage fileImage = coinMapper.findFileImageById(Long.valueOf(id)); if (fileImage == null) { throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "ID 不存在!"); } fileImage.setCategoryId(categoryId); if (StringUtils.isNotEmpty(remark)) { fileImage.setRemark(remark); } if (file != null && file.getSize() > 0) { try { String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue(); boolean flag = FtpUtil.delFile(ftpBasePath + fileImage.getPath()); if (flag) { String oldName = file.getOriginalFilename(); String newName = FtpUtil.genImageName(); newName = newName + oldName.substring(oldName.lastIndexOf(".")); InputStream input = file.getInputStream(); // 2.2调用FtpUtil工具类进行上传 boolean result = FtpUtil.uploadFile(newName, input); if (result) { imageUrl = LocalDate.now().format(DateUtils.dateFormatter_) + "/" + newName; imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB"); fileImage.setOldName(oldName); fileImage.setNewName(newName); fileImage.setSize(imageSize); fileImage.setPath(imageUrl); } else { throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "上传新文件失败!"); } } else { throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "删除旧文件失败!"); } } catch (Exception e) { e.printStackTrace(); return R.error().message(e.getMessage()); } } coinMapper.updateFileImage(fileImage); return R.ok().data("success"); } } @RequestMapping("/deleteImgs/{imageId}") @ResponseBody public R deleteImgs(@PathVariable Long imageId) { if (imageId == null) { throw new ParameterException("imageId为空!"); } FileImage fileImage = coinMapper.findFileImageById(imageId); if (fileImage == null) { throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "ID 不存在!"); } try { String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue(); boolean flag = FtpUtil.delFile(ftpBasePath + fileImage.getPath()); if (flag) { coinMapper.deleteFileImageById(imageId); return R.ok(); } else { return R.error().message("删除失败"); } } catch (Exception e) { e.printStackTrace(); return R.error().message(e.getMessage()); } } /** * insertOrUpdateWatchlist * * @author lvzhiqiang * 2024/1/20 15:42 */ @RequestMapping("/insertOrUpdateWatchlist") @ResponseBody public R insertOrUpdateWatchlist(String symbol, String trackCategory, String issuingDate, Long cmcId, String coingeckoId, Integer filterFlag, String crudType) { if (StringUtils.isEmpty(crudType)) { throw new ParameterException("crudType为空!"); } if (StringUtils.isEmpty(symbol)) { throw new ParameterException("symbol为空!"); } if ("1".equals(crudType)) { // 新增 if (StringUtils.isEmpty(trackCategory) || StringUtils.isEmpty(issuingDate) || null == cmcId || StringUtils.isEmpty(coingeckoId) || null == filterFlag) { throw new ParameterException("参数为空!"); } CoinWatchlist coinWatchlist = new CoinWatchlist(); coinWatchlist.setSymbol(symbol); coinWatchlist.setTrackCategory(trackCategory); coinWatchlist.setIssuingDate(LocalDate.parse(issuingDate, DateUtils.dateFormatter)); coinWatchlist.setCmcId(cmcId); coinWatchlist.setCoingeckoId(coingeckoId); coinWatchlist.setFilterFlag(filterFlag); coinMapper.insertOrUpdateWatchlist(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(issuingDate)) { coinWatchlist.setIssuingDate(LocalDate.parse(issuingDate, DateUtils.dateFormatter)); } if (null != cmcId) { coinWatchlist.setCmcId(cmcId); } if (null != filterFlag) { coinWatchlist.setFilterFlag(filterFlag); } coinMapper.insertOrUpdateWatchlist(coinWatchlist); } return R.ok().data("success"); } }