| | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.yami.trading.bean.contract.domain.ContractOrder; |
| | | import com.yami.trading.bean.contract.dto.TraderOwnClosedAggDTO; |
| | | import com.yami.trading.bean.trader.domain.Trader; |
| | | import com.yami.trading.common.constants.Constants; |
| | | import com.yami.trading.common.util.Arith; |
| | | import com.yami.trading.common.util.StringUtils; |
| | | import com.yami.trading.dao.trader.TraderMapper; |
| | | import com.yami.trading.service.contract.ContractOrderService; |
| | | import com.yami.trading.service.trader.TraderService; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | public class TraderServiceImpl implements TraderService { |
| | | |
| | | @Resource |
| | | private TraderMapper traderMapper; |
| | | @Resource |
| | | @Lazy |
| | | private ContractOrderService contractOrderService; |
| | | |
| | | public Trader findById(String id) { |
| | | |
| | |
| | | |
| | | public Trader findByPartyId(String partyId) { |
| | | LambdaQueryWrapper<Trader> lambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | lambdaQueryWrapper.eq(Trader::getPartyId, partyId); |
| | | Trader trader = traderMapper.selectOne(lambdaQueryWrapper); |
| | | return trader; |
| | | lambdaQueryWrapper.eq(Trader::getPartyId, partyId) |
| | | .eq(Trader::getDelFlag, 0) |
| | | .orderByDesc(Trader::getChecked) |
| | | .orderByDesc(Trader::getCreateTime) |
| | | .last("limit 1"); |
| | | return traderMapper.selectOne(lambdaQueryWrapper); |
| | | } |
| | | |
| | | @Override |
| | |
| | | // lambdaQueryWrapper.orderByDesc(Trader::getCreate_time); |
| | | // } |
| | | lambdaQueryWrapper.eq(Trader::getDelFlag, 0); |
| | | lambdaQueryWrapper.eq(Trader::getChecked, 1); |
| | | lambdaQueryWrapper.eq(Trader::getState, "1"); |
| | | lambdaQueryWrapper.orderByDesc(Trader::getCreateTime); |
| | | |
| | | IPage<Trader> page = traderMapper.selectPage(pageparam, new LambdaQueryWrapper<>()); |
| | | IPage<Trader> page = traderMapper.selectPage(pageparam, lambdaQueryWrapper); |
| | | |
| | | // Page page = this.pagedQueryDao.pagedQuerySQL(pageNo, pageSize, queryString.toString(), parameters); |
| | | List<Map<String, Object>> data = this.bulidData(page.getRecords()); |
| | |
| | | if (traders == null) { |
| | | return result_traders; |
| | | } |
| | | List<String> partyIds = traders.stream() |
| | | .filter(Objects::nonNull) |
| | | .map(Trader::getPartyId) |
| | | .filter(pid -> !StringUtils.isNullOrEmpty(pid)) |
| | | .collect(Collectors.toList()); |
| | | Map<String, TraderOwnClosedAggDTO> closedAggByParty = contractOrderService.mapClosedTraderOwnAggByPartyIds(partyIds); |
| | | for (int i = 0; i < traders.size(); i++) { |
| | | Map<String, Object> map = new HashMap<String, Object>(); |
| | | // Trader entity = BeanUtil.mapToBean(traders.get(i), Trader.class, true); |
| | |
| | | df2.format(Arith.add(entity.getOrderAmount(), entity.getDeviationOrderAmount()))); |
| | | |
| | | // map.put("symbol_name", "BTC/USDT;ETH/USDT"); |
| | | map.put("profit", df2.format(Arith.add(entity.getProfit(), entity.getDeviationProfit()))); |
| | | TraderOwnClosedAggDTO closedAgg = closedAggByParty.get(entity.getPartyId()); |
| | | double closedProfitSum = 0D; |
| | | double closedMarginSum = 0D; |
| | | if (closedAgg != null) { |
| | | if (closedAgg.getClosedProfitSum() != null) { |
| | | closedProfitSum = closedAgg.getClosedProfitSum().doubleValue(); |
| | | } |
| | | if (closedAgg.getClosedMarginSum() != null) { |
| | | closedMarginSum = closedAgg.getClosedMarginSum().doubleValue(); |
| | | } |
| | | } |
| | | double historyProfit = Arith.add(closedProfitSum, entity.getDeviationProfit()); |
| | | double historyAmountBasis = closedMarginSum > 0D |
| | | ? Arith.add(closedMarginSum, entity.getDeviationOrderAmount()) |
| | | : Arith.add(entity.getOrderAmount(), entity.getDeviationOrderAmount()); |
| | | map.put("profit", df2.format(historyProfit)); |
| | | |
| | | map.put("order_profit", (int) Arith.add(entity.getOrderProfit(), entity.getDeviationOrderProfit())); |
| | | |
| | |
| | | Arith.mul(entity.getProfitRatio(), 100)))); |
| | | |
| | | map.put("profit_share_ratio", df2.format(Arith.mul(entity.getProfitShareRatio(), 100))); |
| | | map.put("follow_commission_type", |
| | | com.yami.trading.bean.trader.FollowCommissionType.normalizeOrLegacy(entity.getFollowCommissionType())); |
| | | map.put("follow_commission_monthly_amount", |
| | | entity.getFollowCommissionMonthlyAmount() == null ? "0" |
| | | : entity.getFollowCommissionMonthlyAmount().stripTrailingZeros().toPlainString()); |
| | | map.put("follow_commission_daily_pct", df2.format(Arith.mul(entity.getFollowCommissionDailyPct(), 100))); |
| | | |
| | | // 累计收益/收益率:历史=合约表已平仓全品种盈亏+偏差,分母优先已平仓保证金合计(与当前持仓 deposit 一致)+偏差;再加实时持仓 |
| | | double openProfit = 0D; |
| | | double openDeposit = 0D; |
| | | int openPositionCount = 0; |
| | | List<ContractOrder> openOrders = contractOrderService.findSubmittedTraderOwn(entity.getPartyId(), ""); |
| | | if (openOrders != null && !openOrders.isEmpty()) { |
| | | openPositionCount = openOrders.size(); |
| | | for (ContractOrder one : openOrders) { |
| | | // submitted 持仓利润实时值在缓存中,先包裹后再聚合,口径与详情页一致 |
| | | contractOrderService.wrapProfit(one); |
| | | openProfit = Arith.add(openProfit, one.getProfit() == null ? 0D : one.getProfit().doubleValue()); |
| | | openDeposit = Arith.add(openDeposit, one.getDeposit() == null ? 0D : one.getDeposit().doubleValue()); |
| | | } |
| | | } |
| | | double totalProfit = Arith.add(historyProfit, openProfit); |
| | | double totalRatio = 0D; |
| | | double totalAmount = Arith.add(historyAmountBasis, openDeposit); |
| | | if (totalAmount > 0D) { |
| | | totalRatio = Arith.mul(Arith.div(totalProfit, totalAmount), 100); |
| | | } |
| | | map.put("history_profit", df2.format(historyProfit)); |
| | | double historyProfitRatioOnly = 0D; |
| | | if (historyAmountBasis > 0D) { |
| | | historyProfitRatioOnly = Arith.mul(Arith.div(historyProfit, historyAmountBasis), 100); |
| | | } else { |
| | | historyProfitRatioOnly = Arith.add(Arith.mul(entity.getDeviationProfitRatio(), 100), |
| | | Arith.mul(entity.getProfitRatio(), 100)); |
| | | } |
| | | map.put("history_profit_ratio", df2.format(historyProfitRatioOnly)); |
| | | map.put("open_profit", df2.format(openProfit)); |
| | | map.put("open_deposit", df2.format(openDeposit)); |
| | | map.put("open_position_count", openPositionCount); |
| | | map.put("total_profit", df2.format(totalProfit)); |
| | | map.put("total_profit_ratio", df2.format(totalRatio)); |
| | | |
| | | result_traders.add(map); |
| | | } |