| | |
| | | |
| | | import kernel.util.Arith; |
| | | import kernel.util.ThreadUtils; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.context.ApplicationContextAware; |
| | | import org.springframework.util.ObjectUtils; |
| | | import project.contract.ContractLock; |
| | | import project.contract.ContractOrder; |
| | |
| | | import project.wallet.WalletService; |
| | | import project.wallet.consumer.WalletMessage; |
| | | |
| | | public class ContractOrderCalculationServiceImpl implements ContractOrderCalculationService { |
| | | public class ContractOrderCalculationServiceImpl implements ContractOrderCalculationService, ApplicationContextAware { |
| | | private ApplicationContext applicationContext; |
| | | |
| | | private static final Logger logger = LoggerFactory.getLogger(ContractOrderCalculationServiceImpl.class); |
| | | private ContractOrderService contractOrderService; |
| | | private DataService dataService; |
| | | private WalletService walletService; |
| | | private RedisHandler redisHandler; |
| | | // private RedisHandler redisHandler; |
| | | private AssetService assetService; |
| | | public final static String STATE_SUBMITTED = "submitted"; |
| | | public final static String STATE_CREATED = "created"; |
| | |
| | | double amount = Arith.mul(div, Arith.sub(currentPrice, order.getTrade_avg_price())); |
| | | logger.info("---------盈亏金额:"+amount); |
| | | logger.info("---------盈亏:"+profit_loss); |
| | | RedisHandler redisHandler = getRedisHandler(); |
| | | if ("profit".equals(profit_loss)) { |
| | | /** |
| | | * 盈 正数 |
| | | */ |
| | | order.setProfit(Arith.add(0.0D, Math.abs(amount))); |
| | | |
| | | Object profit = redisHandler.get("MONEY_CONTRACT_PROFIT_" + order.getPartyId().toString()); |
| | | if(!ObjectUtils.isEmpty(profit)){ |
| | | if(Double.parseDouble(profit.toString()) < 0 ){ |
| | | |
| | | redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), 0); |
| | | |
| | | Object money = redisHandler.get("PARTY_ID_MONEY_" + order.getPartyId().toString()); |
| | | if(!ObjectUtils.isEmpty(money)){ |
| | | Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId().toString()); |
| | | wallet.setMoney(Double.parseDouble(money.toString())); |
| | | redisHandler.setSync(WalletRedisKeys.WALLET_PARTY_ID + wallet.getPartyId().toString(), wallet); |
| | | redisHandler.pushAsyn(WalletRedisKeys.WALLET_QUEUE_UPDATE, new WalletMessage(order.getPartyId().toString(), amount)); |
| | | } |
| | | } |
| | | } |
| | | } else if ("loss".equals(profit_loss)) { |
| | | order.setProfit(Arith.sub(0.0D,Math.abs(amount) )); |
| | | |
| | | //定义一个 总浮动亏损的值 |
| | | logger.info("----------fffffffffffffff-------"+redisHandler); |
| | | logger.info("----------ssssssssssss-------"+order.getPartyId().toString()); |
| | | Double contractAssetsProfit = (Double) this.redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString()); |
| | | logger.info("----------bbbbbbbbbbbbbbbbbb-------"+contractAssetsProfit); |
| | | Double contractAssetsProfit = (Double) redisHandler.get(ContractRedisKeys.CONTRACT_ASSETS_PROFIT_PARTY_ID + order.getPartyId().toString()); |
| | | double contractProfit = null == contractAssetsProfit ? 0.000D : contractAssetsProfit; |
| | | logger.info("----------contractProfit-------"+contractProfit); |
| | | if(contractProfit <= 0){ |
| | | redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), contractProfit); |
| | | List<ContractOrder> list = contractOrderService.findSubmitted(order.getPartyId().toString(), null, null); |
| | | double deposit = 0; |
| | | for (int i = 0; i < list.size(); i++) { |
| | | ContractOrder close_line = list.get(i); |
| | | deposit = Arith.add(deposit, close_line.getDeposit()); |
| | | } |
| | | logger.info("----------1111111111111-------"); |
| | | double profitt = Arith.add(contractProfit,deposit); |
| | | if(profitt <= 0){ |
| | | redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), profitt); |
| | | }else{ |
| | | redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), 0); |
| | | } |
| | | //修改余额,每次修改取平仓前余额减去浮动亏损 |
| | | Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId().toString()); |
| | | logger.info("----------wallet-------"+wallet); |
| | | Object money = redisHandler.get("PARTY_ID_MONEY_" + order.getPartyId().toString()); |
| | | Object profit = redisHandler.get("MONEY_CONTRACT_PROFIT_" + order.getPartyId().toString()); |
| | | logger.info("----------money-------"+money); |
| | | logger.info("----------profit-------"+profit); |
| | | if(!ObjectUtils.isEmpty(money) && !ObjectUtils.isEmpty(profit)){ |
| | | wallet.setMoney(Arith.sub(Double.parseDouble(money.toString()),Double.parseDouble(profit.toString()))); |
| | | double residueMoney = Arith.add(Double.parseDouble(money.toString()), Double.parseDouble(profit.toString())); |
| | | //如果当前余额减去浮动亏损小于0,将进入强平,这里不再进行余额更新 |
| | | if(residueMoney > 0){ |
| | | wallet.setMoney(residueMoney); |
| | | redisHandler.setSync(WalletRedisKeys.WALLET_PARTY_ID + wallet.getPartyId().toString(), wallet); |
| | | } |
| | | } |
| | | logger.info("----------2222222222222-------"); |
| | | redisHandler.setSync(WalletRedisKeys.WALLET_PARTY_ID + wallet.getPartyId().toString(), wallet); |
| | | logger.info("----------3333333333333333------"); |
| | | redisHandler.pushAsyn(WalletRedisKeys.WALLET_QUEUE_UPDATE, new WalletMessage(order.getPartyId().toString(), amount)); |
| | | logger.info("----------4444444444444-------"); |
| | | } |
| | | |
| | | double changeRatio; |
| | |
| | | profit = Arith.add(profit, Arith.add(close_line.getProfit(), close_line.getDeposit())); |
| | | } |
| | | Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId().toString()); |
| | | double totleMoney = wallet.getMoney(); |
| | | Object money = redisHandler.get("PARTY_ID_MONEY_" + order.getPartyId().toString()); |
| | | double totleMoney = 0; |
| | | if(ObjectUtils.isEmpty(money)){ |
| | | totleMoney = wallet.getMoney(); |
| | | }else{ |
| | | totleMoney = Double.parseDouble(money.toString()); |
| | | } |
| | | if (Arith.add(profit,totleMoney) <= 0) { |
| | | redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), 0); |
| | | /** |
| | | * 触发全仓强平 |
| | | */ |
| | |
| | | this.order_close_line_type = order_close_line_type; |
| | | } |
| | | |
| | | public void setRedisHandler(RedisHandler redisHandler) { |
| | | this.redisHandler = redisHandler; |
| | | public RedisHandler getRedisHandler() { |
| | | return applicationContext.getBean(RedisHandler.class); |
| | | } |
| | | |
| | | |
| | | public void setAssetService(AssetService assetService) { |
| | | this.assetService = assetService; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| | | this.applicationContext = applicationContext; |
| | | } |
| | | |
| | | } |