| | |
| | | import com.yami.trading.common.exception.BusinessException; |
| | | import com.yami.trading.common.web.ResultObject; |
| | | import com.yami.trading.security.common.util.SecurityUtils; |
| | | import com.yami.trading.service.contract.ContractOrderService; |
| | | import com.yami.trading.service.trader.TraderFollowUserService; |
| | | import com.yami.trading.service.trader.TraderService; |
| | | import com.yami.trading.service.user.UserService; |
| | |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.math.BigDecimal; |
| | | |
| | | /** |
| | | * 用户准备跟随交易员api接口 |
| | |
| | | |
| | | @Autowired |
| | | private UserService userService; |
| | | |
| | | @Autowired |
| | | private ContractOrderService contractOrderService; |
| | | |
| | | // private String trader_id; |
| | | // |
| | |
| | | @RequestMapping(action + "save.action") |
| | | public Object saveCreate(HttpServletRequest request) { |
| | | ResultObject resultObject = new ResultObject(); |
| | | String follow_type = request.getParameter("follow_type"); |
| | | String stop_loss = request.getParameter("stop_loss"); |
| | | String stop_profit = request.getParameter("stop_profit"); |
| | | String symbol = request.getParameter("symbol"); |
| | | String volume = request.getParameter("volume"); |
| | | String volume_max = request.getParameter("volume_max"); |
| | | String leverRate = request.getParameter("lever_rate"); |
| | | String trader_id = request.getParameter("trader_id"); |
| | | |
| | | String partyId = SecurityUtils.getCurrentUserId(); |
| | |
| | | entity.setPartyId(partyId); |
| | | entity.setUsername(user.getUserName()); |
| | | /** |
| | | * 跟单固定张数/固定比例---选择 1,固定张数量,固定比例 |
| | | * 当前仅支持固定币数量跟单 |
| | | */ |
| | | entity.setFollowType(follow_type); |
| | | entity.setStopLoss(Double.parseDouble(stop_loss)); |
| | | entity.setStopProfit(Double.parseDouble(stop_profit)); |
| | | entity.setFollowType(TraderFollowUser.FOLLOW_TYPE_FIXED); |
| | | entity.setStopLoss(0D); |
| | | entity.setStopProfit(0D); |
| | | entity.setSymbol(symbol); |
| | | entity.setVolume(Double.parseDouble(volume)); |
| | | entity.setVolumeMax(Double.parseDouble(volume_max)); |
| | | double investAmount = parsePositiveDouble(volume, "跟单投入币数量"); |
| | | entity.setVolume(investAmount); |
| | | entity.setVolumeMax(investAmount); |
| | | entity.setInvestAmount(BigDecimal.valueOf(investAmount)); |
| | | entity.setLeverRate(parsePositiveDoubleOrDefault(leverRate, 1D, "杠杆倍数")); |
| | | ensureTraderCannotFollow(partyId); |
| | | /** |
| | | * 状态 是否还在跟随状随 1,跟随,取消跟随 |
| | | * 状态 1-跟随中 |
| | | */ |
| | | entity.setState("1"); |
| | | entity.setState(TraderFollowUser.STATE_FOLLOWING); |
| | | |
| | | this.traderFollowUserService.save(entity, trader_id); |
| | | resultObject.setCode("0"); |
| | |
| | | @RequestMapping(action + "changeFollow.action") |
| | | public Object changeFollow(HttpServletRequest request) { |
| | | ResultObject resultObject = new ResultObject(); |
| | | String follow_type = request.getParameter("follow_type"); |
| | | String stop_loss = request.getParameter("stop_loss"); |
| | | String stop_profit = request.getParameter("stop_profit"); |
| | | String symbol = request.getParameter("symbol"); |
| | | String volume = request.getParameter("volume"); |
| | | String volume_max = request.getParameter("volume_max"); |
| | | String leverRate = request.getParameter("lever_rate"); |
| | | String trader_id = request.getParameter("trader_id"); |
| | | |
| | | String partyId = SecurityUtils.getCurrentUserId(); |
| | |
| | | // return resultObject; |
| | | // } |
| | | |
| | | User user = userService.getById(partyId); |
| | | // if (!party.getKyc_authority()) { |
| | | // resultObject.setCode("401"); |
| | | // resultObject.setMsg(error); |
| | |
| | | TraderFollowUser entity = this.traderFollowUserService.findByPartyIdAndTrader_partyId(partyId, |
| | | trader.getPartyId()); |
| | | /** |
| | | * 跟单固定张数/固定比例---选择 1,固定张数�?2,固定比�? |
| | | * 当前仅支持固定币数量跟单 |
| | | */ |
| | | entity.setFollowType(follow_type); |
| | | entity.setStopLoss(Double.parseDouble(stop_loss)); |
| | | entity.setStopProfit(Double.parseDouble(stop_profit)); |
| | | entity.setFollowType(TraderFollowUser.FOLLOW_TYPE_FIXED); |
| | | entity.setStopLoss(0D); |
| | | entity.setStopProfit(0D); |
| | | entity.setSymbol(symbol); |
| | | entity.setVolume(Double.parseDouble(volume)); |
| | | entity.setVolumeMax(Double.parseDouble(volume_max)); |
| | | double investAmount = parsePositiveDouble(volume, "跟单投入币数量"); |
| | | entity.setVolume(investAmount); |
| | | entity.setVolumeMax(investAmount); |
| | | entity.setInvestAmount(BigDecimal.valueOf(investAmount)); |
| | | entity.setLeverRate(parsePositiveDoubleOrDefault(leverRate, entity.getLeverRate() > 0 ? entity.getLeverRate() : 1D, "杠杆倍数")); |
| | | ensureTraderCannotFollow(partyId); |
| | | /** |
| | | * 状�?? 是否还在跟随状�?? 1,跟随�?2,取消跟�? |
| | | * 状态保持跟随中 |
| | | */ |
| | | entity.setState("1"); |
| | | entity.setState(TraderFollowUser.STATE_FOLLOWING); |
| | | |
| | | this.traderFollowUserService.update(entity); |
| | | resultObject.setCode("0"); |
| | |
| | | TraderFollowUser traderFollowUser = this.traderFollowUserService.findByPartyIdAndTrader_partyId(partyId, |
| | | trader.getPartyId().toString()); |
| | | if (traderFollowUser != null) { |
| | | this.traderFollowUserService.deleteCancel(traderFollowUser.getUuid()); |
| | | this.traderFollowUserService.cancelFollowAsync(traderFollowUser.getUuid(), contractOrderService); |
| | | } |
| | | |
| | | resultObject.setCode("0"); |
| | | resultObject.setMsg("已提交停止跟单请求"); |
| | | } catch (BusinessException e) { |
| | | resultObject.setCode(e.getSign() + ""); |
| | | resultObject.setMsg(e.getMessage()); |
| | |
| | | return resultObject; |
| | | } |
| | | |
| | | @RequestMapping(action + "status.action") |
| | | public Object status(HttpServletRequest request) { |
| | | ResultObject resultObject = new ResultObject(); |
| | | String trader_id = request.getParameter("trader_id"); |
| | | String partyId = SecurityUtils.getCurrentUserId(); |
| | | try { |
| | | Trader trader = traderService.findById(trader_id); |
| | | TraderFollowUser relation = trader == null ? null |
| | | : traderFollowUserService.findByPartyIdAndTrader_partyId(partyId, trader.getPartyId()); |
| | | resultObject.setCode("0"); |
| | | resultObject.setData(relation); |
| | | } catch (Exception e) { |
| | | resultObject.setCode("1"); |
| | | resultObject.setMsg("程序错误"); |
| | | logger.error("error:", e.fillInStackTrace()); |
| | | } |
| | | return resultObject; |
| | | } |
| | | |
| | | @RequestMapping(action + "list.action") |
| | | public Object list() { |
| | | ResultObject resultObject = new ResultObject(); |
| | | String partyId = SecurityUtils.getCurrentUserId(); |
| | | try { |
| | | resultObject.setCode("0"); |
| | | resultObject.setData(traderFollowUserService.findByPartyId(partyId)); |
| | | } catch (Exception e) { |
| | | resultObject.setCode("1"); |
| | | resultObject.setMsg("程序错误"); |
| | | logger.error("error:", e.fillInStackTrace()); |
| | | } |
| | | return resultObject; |
| | | } |
| | | |
| | | private double parsePositiveDouble(String value, String fieldName) { |
| | | double parsed; |
| | | try { |
| | | parsed = Double.parseDouble(value == null ? "" : value.trim()); |
| | | } catch (NumberFormatException ex) { |
| | | throw new BusinessException(1, fieldName + "格式不正确"); |
| | | } |
| | | if (parsed <= 0) { |
| | | throw new BusinessException(1, fieldName + "必须大于0"); |
| | | } |
| | | return parsed; |
| | | } |
| | | |
| | | private double parsePositiveDoubleOrDefault(String value, double defaultVal, String fieldName) { |
| | | if (value == null || value.trim().isEmpty()) { |
| | | return defaultVal; |
| | | } |
| | | return parsePositiveDouble(value, fieldName); |
| | | } |
| | | |
| | | private void ensureTraderCannotFollow(String partyId) { |
| | | Trader trader = traderService.findByPartyId(partyId); |
| | | if (trader != null && trader.getChecked() == 1) { |
| | | throw new BusinessException(1, "交易员身份用户不能跟单其他交易员"); |
| | | } |
| | | } |
| | | |
| | | } |