package com.bizzan.bitrade.service;

import com.bizzan.bitrade.dao.ContractCoinRepository;
import com.bizzan.bitrade.dao.MemberContractWalletDao;
import com.bizzan.bitrade.dao.MemberContractWalletPositionDao;
import com.bizzan.bitrade.entity.ContractCoin;
import com.bizzan.bitrade.entity.Member;
import com.bizzan.bitrade.entity.MemberContractWallet;
import com.bizzan.bitrade.entity.MemberContractWalletPosition;
import com.bizzan.bitrade.service.Base.BaseService;
import com.bizzan.bitrade.util.MessageResult;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class MemberContractWalletPositionService extends BaseService {
    @Autowired
    private MemberContractWalletPositionDao memberContractWalletPositionDao;

    @Autowired
    private ContractCoinRepository contractCoinRepository;

    public MemberContractWalletPosition findOne(Long id) {
        return memberContractWalletPositionDao.findOne(id);
    }

    public MemberContractWalletPosition findByMemberIdAndContractCoin(Long memberId, ContractCoin contractCoin) {
        return memberContractWalletPositionDao.findByMemberIdAndContractCoin(memberId, contractCoin);
    }
    public Page<MemberContractWalletPosition> findAll(Predicate predicate, Pageable pageable) {
        return memberContractWalletPositionDao.findAll(predicate, pageable);
    }
    public MemberContractWalletPosition save(MemberContractWalletPosition memberContractWalletPosition){
        return memberContractWalletPositionDao.saveAndFlush(memberContractWalletPosition);
    }

    public List<MemberContractWalletPosition> findAllByMember(Member member) {
        return memberContractWalletPositionDao.findAllByMemberId(member.getId());
    }

    public List<MemberContractWalletPosition> findAllByMemberId(Long memberId) {
        return memberContractWalletPositionDao.findAllByMemberId(memberId);
    }


    /**
     * 增加做多保证金（与余额有关）
     * @param walletId
     * @param amount
     */
    public void increaseUsdtBuyPrincipalAmount(Long walletId, BigDecimal amount) {
        memberContractWalletPositionDao.increaseUsdtBuyPrincipalAmount(walletId, amount);
    }

    /**
     * 增加做多保证金（与冻结余额有关）
     * @param walletId
     * @param amount
     */
    public void increaseUsdtBuyPrincipalAmountWithFrozen(Long walletId, BigDecimal amount) {
        memberContractWalletPositionDao.increaseUsdtBuyPrincipalAmountWithFrozen(walletId, amount);
    }

    /**
     * 增加做空保证金（与余额有关）
     * @param walletId
     * @param amount
     */
    public void increaseUsdtSellPrincipalAmount(Long walletId, BigDecimal amount) {
        memberContractWalletPositionDao.increaseUsdtSellPrincipalAmount(walletId, amount);
    }

    /**
     * 增加做空保证金（与冻结余额有关）
     * @param walletId
     * @param amount
     */
    public void increaseUsdtSellPrincipalAmountWithFrozen(Long walletId, BigDecimal amount) {
        memberContractWalletPositionDao.increaseUsdtSellPrincipalAmountWithFrozen(walletId, amount);
    }

    /**
     * 减少做多保证金（与余额有关）
     * @param walletId
     * @param amount
     */
    public int decreaseUsdtBuyPrincipalAmount(Long walletId, BigDecimal amount) {
       return memberContractWalletPositionDao.decreaseUsdtBuyPrincipalAmount(walletId, amount);
    }
//
//    /**
//     * 减少做多保证金（与余额有关）
//     * @param walletId
//     * @param amount
//     */
//    public void decreaseUsdtBuyPrincipalAmountWithoutBalance(Long walletId, BigDecimal amount) {
//        memberContractWalletDao.decreaseUsdtBuyPrincipalAmountWithoutBalance(walletId, amount);
//    }
//
    /**
     * 减少做空保证金（与余额有关）
     * @param walletId
     * @param amount
     */
    public int decreaseUsdtSellPrincipalAmount(Long walletId, BigDecimal amount) {
        return memberContractWalletPositionDao.decreaseUsdtSellPrincipalAmount(walletId, amount);
    }
//
//    /**
//     * 减少做空保证金（与余额有关）
//     * @param walletId
//     * @param amount
//     */
//    public void decreaseUsdtSellPrincipalAmountWithoutBalance(Long walletId, BigDecimal amount) {
//        memberContractWalletDao.decreaseUsdtSellPrincipalAmountWithoutBalance(walletId, amount);
//    }


    /**
     * 更新开仓价格和持仓量（多仓）
     * @param walletId
     * @param avaPrice
     * @param volume
     */
    public void updateUsdtBuyPriceAndPosition(Long walletId, BigDecimal avaPrice, BigDecimal volume) {
        memberContractWalletPositionDao.updateUsdtBuyPriceAndPosition(walletId, avaPrice, volume);
    }

    /**
     * 更新开仓价格和持仓量（空仓）
     * @param walletId
     * @param avaPrice
     * @param volume
     */
    public void updateUsdtSellPriceAndPosition(Long walletId, BigDecimal avaPrice, BigDecimal volume) {
        memberContractWalletPositionDao.updateUsdtSellPriceAndPosition(walletId, avaPrice, volume);
    }
//
//    public void decreaseUsdtSellPositionAndPrincipalAmount(Long walletId, BigDecimal volume, BigDecimal principalAmount) {
//        memberContractWalletDao.decreaseUsdtSellPositionAndPrincipalAmount(walletId, volume, principalAmount);
//    }

    public void decreaseUsdtFrozenSellPositionAndPrincipalAmount(Long walletId, BigDecimal volume, BigDecimal principalAmount) {
        memberContractWalletPositionDao.decreaseUsdtFrozenSellPositionAndPrincipalAmount(walletId, volume, principalAmount);
    }

    public void decreaseUsdtBuyPositionAndPrincipalAmount(Long walletId, BigDecimal volume, BigDecimal principalAmount) {
        memberContractWalletPositionDao.decreaseUsdtBuyPositionAndPrincipalAmount(walletId, volume, principalAmount);
    }
    public void decreaseUsdtFrozenBuyPositionAndPrincipalAmount(Long walletId, BigDecimal volume, BigDecimal principalAmount) {
        memberContractWalletPositionDao.decreaseUsdtFrozenBuyPositionAndPrincipalAmount(walletId, volume, principalAmount);
    }
    public void freezeUsdtSellPosition(Long walletId, BigDecimal volume) {
        memberContractWalletPositionDao.freezeUsdtSellPosition(walletId, volume);
    }
    public void thrawUsdtSellPosition(Long walletId, BigDecimal volume) {
        memberContractWalletPositionDao.thrawUsdtSellPosition(walletId, volume);
    }
    public void freezeUsdtBuyPosition(Long walletId, BigDecimal volume) {
        memberContractWalletPositionDao.freezeUsdtBuyPosition(walletId, volume);
    }
    public void thrawUsdtBuyPosition(Long walletId, BigDecimal volume) {
        memberContractWalletPositionDao.thrawUsdtBuyPosition(walletId, volume);
    }
    public void modifyUsdtBuyLeverage(Long walletId, BigDecimal leverage) {
        memberContractWalletPositionDao.modifyUsdtBuyLeverage(walletId, leverage);
    }

    public void modifyUsdtSellLeverage(Long walletId, BigDecimal leverage) {
        memberContractWalletPositionDao.modifyUsdtSellLeverage(walletId, leverage);
    }


    public List<MemberContractWalletPosition> findAllNeedSync(Long memberId) {
        return memberContractWalletPositionDao.findAllNeedSync(memberId);
    }
    public List<MemberContractWalletPosition> findAllNeedSyn(ContractCoin contractCoin) {
        return memberContractWalletPositionDao.findAllNeedSyn(contractCoin.getId());
    }

    public void blastBuy(Long walletId) {
        memberContractWalletPositionDao.blastBuy(walletId);
    }

    public void blastSell(Long walletId) {
        memberContractWalletPositionDao.blastSell(walletId);
    }

    public void updateShareNumber(Long walletId, BigDecimal shareNumber) {
        memberContractWalletPositionDao.updateShareNumber(walletId, shareNumber);
    }

    public void increaseUsdtProfit(Long walletId, BigDecimal pL) {
        memberContractWalletPositionDao.increaseUsdtProfit(walletId, pL);
    }

    public void increaseUsdtLoss(Long walletId, BigDecimal pL) {
        memberContractWalletPositionDao.increaseUsdtLoss(walletId, pL);
    }
    //合约调整倍率
    public void modifyUsdtBuyAndSellLeverage(Long id, BigDecimal leverage) {
        memberContractWalletPositionDao.updateUsdtBuyAndSellLeverage(id,leverage);
    }
}
