| | |
| | | |
| | | import java.text.DecimalFormat; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import kernel.util.StringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import kernel.util.Arith; |
| | | import kernel.util.ThreadUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.util.ObjectUtils; |
| | | import project.contract.ContractLock; |
| | | import project.contract.ContractOrder; |
| | | import project.contract.ContractOrderService; |
| | | import project.contract.ContractRedisKeys; |
| | | import project.data.DataService; |
| | | import project.data.model.Realtime; |
| | | import project.redis.RedisHandler; |
| | | import project.syspara.SysparaService; |
| | | import project.wallet.AssetService; |
| | | import project.wallet.Wallet; |
| | | import project.wallet.WalletRedisKeys; |
| | | import project.wallet.WalletService; |
| | | import project.wallet.consumer.WalletMessage; |
| | | |
| | | public class ContractOrderCalculationServiceImpl implements ContractOrderCalculationService { |
| | | private static final Logger logger = LoggerFactory.getLogger(ContractOrderCalculationServiceImpl.class); |
| | | private ContractOrderService contractOrderService; |
| | | private DataService dataService; |
| | | private WalletService walletService; |
| | | private RedisHandler redisHandler; |
| | | private AssetService assetService; |
| | | public final static String STATE_SUBMITTED = "submitted"; |
| | | public final static String STATE_CREATED = "created"; |
| | | /** |
| | |
| | | order.setProfit(Arith.add(0.0D, Math.abs(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 contractProfit = null == contractAssetsProfit ? 0.000D : contractAssetsProfit; |
| | | logger.info("----------contractProfit-------"+contractProfit); |
| | | if(contractProfit <= 0){ |
| | | redisHandler.setSync("MONEY_CONTRACT_PROFIT_"+order.getPartyId().toString(), contractProfit); |
| | | } |
| | | logger.info("----------1111111111111-------"); |
| | | //修改余额,每次修改取平仓前余额减去浮动亏损 |
| | | 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()))); |
| | | } |
| | | 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; |
| | |
| | | return; |
| | | } |
| | | } |
| | | logger.info("order_close_line_type:"+order_close_line_type); |
| | | if (order_close_line_type == 1) { |
| | | /** |
| | | * 收益 |
| | |
| | | |
| | | } |
| | | } else { |
| | | logger.info("---------进入单个持仓----------------"); |
| | | logger.info("---------order.getProfit()----------------"+order.getProfit()); |
| | | logger.info("---------order.getDeposit()----------------"+order.getDeposit()); |
| | | logger.info("---------order_close_line----------------"+order_close_line); |
| | | if (order.getProfit() < 0 && (Arith.div(order.getDeposit(), Math.abs(order.getProfit())) <= Arith |
| | | .div(order_close_line, 100))) { |
| | | logger.info("---------进入强平了----------------"); |
| | | /** |
| | | * 低于系统默认平仓线,进行强平 |
| | | */ |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrder_no()); |
| | | return; |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | |
| | | this.order_close_line_type = order_close_line_type; |
| | | } |
| | | |
| | | public void setRedisHandler(RedisHandler redisHandler) { |
| | | this.redisHandler = redisHandler; |
| | | } |
| | | |
| | | public void setAssetService(AssetService assetService) { |
| | | this.assetService = assetService; |
| | | } |
| | | |
| | | |
| | | } |