package com.bizzan.bitrade.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@ApiModel("仓位")
@Entity
@Data
public class MemberContractPosition implements Serializable {
	private static final Long serialVersionUID = 103L;

	@GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long id;
	private Long memberId;

	@ApiModelProperty("杠杆")
	private BigDecimal leverage;
	@ApiModelProperty("总仓位")
	private BigDecimal totalPosition;
	@ApiModelProperty("开仓价格")
	private BigDecimal openPrice;
	@ApiModelProperty("已平仓部分开仓价格")
	private BigDecimal closedOpenPrice;
	@ApiModelProperty("平仓价格")
	private BigDecimal closePrice;
	@ApiModelProperty("保证金")
	private BigDecimal principalAmount;
	@ApiModelProperty("总保证金 计算收益率")
	private BigDecimal totalPrincipalAmount;
	@ApiModelProperty("冻结仓位")
	private BigDecimal frozenPosition;
	@ApiModelProperty("已平仓仓位")
	private BigDecimal closedPosition;
	@Enumerated(EnumType.ORDINAL)
	@ApiModelProperty("仓位类型 INTEGRAL合仓 SEPARATE分仓")
	private ContractPositionType type;
	@ApiModelProperty("资金类型 USDT U本位 COIN币")
	private ContractMoneyType moneyType;
	@Enumerated(EnumType.ORDINAL)
	@ApiModelProperty("仓位方向 BUY多 SELL空")
	private ContractOrderDirection direction;
	@ApiModelProperty("已平仓仓位收益")
	private BigDecimal usdtProfit;
	private BigDecimal shareNumber;
	@ApiModelProperty("状态 STARTED未平仓 PARTLYDONE部分平仓 DONE全部平仓")
	@Enumerated(EnumType.ORDINAL)
	private ContractPositionStatus status;
	
	@ManyToOne
    @JoinColumn(name = "contract_id")
	private ContractCoin coin;

	@ApiModelProperty("创建时间")
	private Date createTime;
	@ApiModelProperty("平均开仓时间")
	private Date openTime;
	@ApiModelProperty("最近开仓时间")
	private Date latestOpenTime;
	@ApiModelProperty("平仓时间")
	private Date closeTime;

	@ApiModelProperty("开仓类型 0市价委托 1限价委托 2计划委托")
	private Integer openType;
	@ApiModelProperty("平仓类型 CLOSE平仓 MARKET_CLOSE市价全平 ONE_KEY_CLOSE一键平仓 REVERSE_OPEN反向开仓 CUT止盈止损 BLAST爆仓 ADMIN_CLOSE管理员强平")
	private CloseType closeType;
	
	@ApiModelProperty("委托数量精度 客户端使用此精度限制小数位")
	private Integer decimalScale;
	
	@ApiModelProperty("手续费")
	private BigDecimal commissionFee;
	
	private BigDecimal blastedAmount;
	
	private Integer isLock;
	
	private Integer violateTime;
	
	private BigDecimal currentPrice;
	
	private Long followOrderId;
	private Long followPositionId;
	private Long followMemberId;
	@ApiModelProperty("分润比例")
	private Integer dividendPercent;
	private Integer isSpecial;

	@ApiModelProperty("关联体验金券ID，NULL=普通仓位，非NULL=体验金仓位")
	private Long experienceRecordId;
	
	@ApiModelProperty("当前仓位")
	public BigDecimal getCurrentPosition() {
		if(closedPosition == null)
			return totalPosition;
		return totalPosition.subtract(closedPosition).stripTrailingZeros();
	}

	@ApiModelProperty("当前可用仓位")
	public BigDecimal getAvailablePosition() {
		if(frozenPosition == null)
			return getCurrentPosition();
		return getCurrentPosition().subtract(frozenPosition).stripTrailingZeros();
	}

	@ApiModelProperty("当前盈利")
	public BigDecimal getCurrentProfit() {
		if(currentPrice == null)
			return BigDecimal.ZERO;
		BigDecimal profit;
		if(this.moneyType == ContractMoneyType.COIN)
			profit = currentPrice.subtract(openPrice).multiply(getCurrentPosition());
		else {
			profit = currentPrice.subtract(openPrice).multiply(getCurrentPosition()).multiply(shareNumber).divide(openPrice,8,RoundingMode.HALF_UP);
		}
		
		if(direction == ContractOrderDirection.SELL) {
			profit = BigDecimal.ZERO.subtract(profit);
		}
		return profit;
	}
	
	@ApiModelProperty("当前仓位开仓金额")
	public BigDecimal getCurrentOpenValue() {
		if(moneyType == ContractMoneyType.COIN)
			return getCurrentPosition().multiply(openPrice);
		else
			return getCurrentPosition().multiply(shareNumber);
	}
	
	@ApiModelProperty("当前仓位市值")
	public BigDecimal getCurrentMarketValue() {
		if(currentPrice == null)
			return BigDecimal.ZERO;
		
		if(moneyType == ContractMoneyType.COIN)
			return getCurrentPosition().multiply(currentPrice);
		else
			return getCurrentPosition().multiply(shareNumber).multiply(currentPrice).divide(openPrice, 8, RoundingMode.HALF_UP);
	}
	
	@ApiModelProperty("当前仓位保证金")
	public BigDecimal getCurrentMargin() {
		return this.getCurrentMarketValue().divide(this.getLeverage(), 8, RoundingMode.HALF_UP);
	}

	@ApiModelProperty("仓位类型展示标签：全仓 / 分仓 / 体验金")
	public String getPositionTypeLabel() {
		if (this.experienceRecordId != null) return "体验金";
		if (ContractPositionType.SEPARATE.equals(this.type)) return "分仓";
		return "全仓";
	}
}
