package com.hfkj.service.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.common.utils.IDGenerator;
import com.hfkj.dao.BsTradeOrderProfitSharingMapper;
import com.hfkj.entity.*;
import com.hfkj.service.*;
import com.hfkj.sysenum.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @className: BsTradeOrderProfitSharingServiceImpl
 * @author: HuRui
 * @date: 2023/2/22
 **/
@Service("tradeOrderProfitSharingService")
public class BsTradeOrderProfitSharingServiceImpl implements BsTradeOrderProfitSharingService {

    @Resource
    private BsTradeOrderProfitSharingMapper tradeOrderProfitSharingMapper;

    @Resource
    private BsTradeOrderProfitSharingReceiverService tradeOrderProfitSharingReceiverService;

    @Resource
    private BsTradeOrderService tradeOrderService;

    @Resource
    private BsMerPlatformNoRateService merPlatformNoRateService;

    @Resource
    private BsTradeTakeConfigService tradeTakeConfigService;

    @Override
    public void editData(BsTradeOrderProfitSharing profitSharing) {
        if (profitSharing.getId() == null) {
            profitSharing.setCreateTime(new Date());
            tradeOrderProfitSharingMapper.insert(profitSharing);
        } else {
            tradeOrderProfitSharingMapper.updateByPrimaryKeySelective(profitSharing);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void createProfitSharing(BsTradeOrder tradeOrder) {
        if (tradeOrder.getProfitSharingStatus() != null && tradeOrder.getProfitSharingStatus().equals(true)) {
            BsTradeOrderProfitSharing tradeOrderProfitSharing = new BsTradeOrderProfitSharing();
            tradeOrderProfitSharing.setCmdType(ProfitSharingCmdTypeEnum.CMD_TYPE_1.getNumber());
            tradeOrderProfitSharing.setProfitSharingOrderNo("1530"+ DateUtil.format(new Date(), DateUtil.YMDHMS) + IDGenerator.nextId(10));
            tradeOrderProfitSharing.setTradeOrderId(tradeOrder.getId());
            tradeOrderProfitSharing.setTradeOrderNo(tradeOrder.getOutTradeNo());
            tradeOrderProfitSharing.setPlatformType(tradeOrder.getPlatformType());
            tradeOrderProfitSharing.setPlatformMerNo(tradeOrder.getPlatformMerNo());
            tradeOrderProfitSharing.setPlatformTradeNo(tradeOrder.getPlatformTradeNo());
            tradeOrderProfitSharing.setPlatformAppid(tradeOrder.getPlatformAppid());
            tradeOrderProfitSharing.setStatus(ProfitSharingStatusEnum.status0.getNumber());
            editData(tradeOrderProfitSharing);

            // 比例
            BsMerPlatformNoRate rate = merPlatformNoRateService.getDetail(tradeOrder.getMerId(), PlatformTypeEnum.getDataByNumber(tradeOrder.getPlatformType()).getNumber(), null);
            if (rate == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置比例");
            }

            // 渠道平台手续费
            BigDecimal platformServiceCharge = null;
            if (tradeOrder.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {
                platformServiceCharge = new BigDecimal("0.000");
            } else if (tradeOrder.getPlatformType().equals(PlatformTypeEnum.type4.getNumber())) {
                platformServiceCharge = new BigDecimal("0.002");
            }

            // 交易金额 * 渠道平台收取手续费率 = 渠道平台手续费 （四舍五入向上取取整）
            BigDecimal platformServiceChargeAmount = tradeOrder.getTradeActualAmount().multiply(platformServiceCharge).setScale(2, BigDecimal.ROUND_HALF_DOWN);

            // 自建平台手续费
            BigDecimal platformServiceAmount = tradeOrder.getTradeAmount().multiply(rate.getRatePct()).setScale(2, BigDecimal.ROUND_HALF_DOWN);

            // 无任何优惠
            if (tradeOrder.getStoreDiscountSatisfy().equals(false) && tradeOrder.getUserDiscountId() == null) {
                // 自建平台手续费 - 渠道平台手续费 = 利润
                BigDecimal totalProfit = platformServiceAmount.subtract(platformServiceChargeAmount);
                tradeOrderProfitSharing.setProfitSharingAmoun(totalProfit);
                // 剩余利润
                BigDecimal surplusProfit = totalProfit;
                // 运营商配置
                BsTradeTakeConfig operatingTakeConfig = tradeTakeConfigService.getTradeTakeConfig(
                        tradeOrder.getCompanyId(),
                        PlatformTypeEnum.getDataByNumber(tradeOrder.getPlatformType()),
                        ProfitSharingReceiverRoleEnum.role1,
                        2L,
                        null);
                // 代理商配置
                BsTradeTakeConfig agentTakeConfig = tradeTakeConfigService.getTradeTakeConfig(
                        tradeOrder.getCompanyId(),
                        PlatformTypeEnum.getDataByNumber(tradeOrder.getPlatformType()),
                        ProfitSharingReceiverRoleEnum.role2,
                        null,
                        tradeOrder.getAgentId());

                for (ProfitSharingReceiverObjectType receiverObjectType : ProfitSharingReceiverObjectType.getDataList()) {
                    if (receiverObjectType.getType().equals(ProfitSharingReceiverObjectType.operating.getType())) {
                        // 运营商抽成
                        BigDecimal operatingAmount;
                        if (agentTakeConfig != null) {
                            operatingAmount = surplusProfit.multiply(operatingTakeConfig.getReceiverRatio()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        } else {
                            operatingAmount = surplusProfit;
                        }
                        BsTradeOrderProfitSharingReceiver profitSharingReceiver = new BsTradeOrderProfitSharingReceiver();
                        profitSharingReceiver.setTradeOrderProfitSharingId(tradeOrderProfitSharing.getId());
                        profitSharingReceiver.setTradeOrderId(tradeOrder.getId());
                        profitSharingReceiver.setTradeOrderNo(tradeOrder.getOutTradeNo());
                        profitSharingReceiver.setProfitSharingAmount(surplusProfit);
                        profitSharingReceiver.setRatio(operatingTakeConfig.getReceiverRatio());
                        profitSharingReceiver.setReceiverObjectType(ProfitSharingReceiverObjectType.operating.getType());
                        profitSharingReceiver.setReceiverObjectId(operatingTakeConfig.getReceiverObjectId().toString());
                        profitSharingReceiver.setReceiverAccount(operatingTakeConfig.getReceiverAccount());
                        profitSharingReceiver.setReceiverName(operatingTakeConfig.getReceiverName());
                        profitSharingReceiver.setReceiverAmount(operatingAmount);
                        tradeOrderProfitSharingReceiverService.editData(profitSharingReceiver);

                        surplusProfit = surplusProfit.subtract(operatingAmount);

                    } else if (receiverObjectType.getType().equals(ProfitSharingReceiverObjectType.company.getType())) {
                        // 公司抽成
                        BigDecimal companyAmount;
                        if (agentTakeConfig != null) {
                            companyAmount = surplusProfit.multiply(agentTakeConfig.getReceiverRatio()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        } else {
                            companyAmount = new BigDecimal("0");
                        }
                        BsTradeOrderProfitSharingReceiver profitSharingReceiver = new BsTradeOrderProfitSharingReceiver();
                        profitSharingReceiver.setTradeOrderProfitSharingId(tradeOrderProfitSharing.getId());
                        profitSharingReceiver.setTradeOrderId(tradeOrder.getId());
                        profitSharingReceiver.setTradeOrderNo(tradeOrder.getOutTradeNo());
                        profitSharingReceiver.setProfitSharingAmount(surplusProfit);
                        profitSharingReceiver.setRatio(agentTakeConfig!=null?agentTakeConfig.getReceiverRatio():new BigDecimal("0"));
                        profitSharingReceiver.setReceiverObjectType(ProfitSharingReceiverObjectType.company.getType());
                        profitSharingReceiver.setReceiverObjectId(tradeOrder.getCompanyId()!=null?tradeOrder.getCompanyId().toString():null);
                        profitSharingReceiver.setReceiverAccount(agentTakeConfig!=null?agentTakeConfig.getReceiverAccount():null);
                        profitSharingReceiver.setReceiverName(agentTakeConfig!=null?agentTakeConfig.getReceiverName():null);
                        profitSharingReceiver.setReceiverAmount(companyAmount);
                        tradeOrderProfitSharingReceiverService.editData(profitSharingReceiver);

                        surplusProfit = surplusProfit.subtract(companyAmount);

                    } else if (receiverObjectType.getType().equals(ProfitSharingReceiverObjectType.agent.getType())) {
                        // 代理商抽成
                        BigDecimal agentAmount = surplusProfit;

                        BsTradeOrderProfitSharingReceiver profitSharingReceiver = new BsTradeOrderProfitSharingReceiver();
                        profitSharingReceiver.setTradeOrderProfitSharingId(tradeOrderProfitSharing.getId());
                        profitSharingReceiver.setTradeOrderId(tradeOrder.getId());
                        profitSharingReceiver.setTradeOrderNo(tradeOrder.getOutTradeNo());
                        profitSharingReceiver.setProfitSharingAmount(surplusProfit);
                        profitSharingReceiver.setRatio(agentTakeConfig!=null?agentTakeConfig.getSubReceiverRatio(): new BigDecimal("0"));
                        profitSharingReceiver.setReceiverObjectType(ProfitSharingReceiverObjectType.agent.getType());
                        profitSharingReceiver.setReceiverObjectId(tradeOrder.getAgentId().toString());
                        profitSharingReceiver.setReceiverAccount(agentTakeConfig!=null?agentTakeConfig.getSubReceiverAccount():null);
                        profitSharingReceiver.setReceiverName(agentTakeConfig!=null?agentTakeConfig.getSubReceiverName():null);
                        profitSharingReceiver.setReceiverAmount(agentAmount);
                        tradeOrderProfitSharingReceiverService.editData(profitSharingReceiver);
                    }
                }
            }

            // 门店优惠
            if (tradeOrder.getStoreDiscountSatisfy().equals(true)) {
                // 运营商配置
                BsTradeTakeConfig operatingTakeConfig = tradeTakeConfigService.getTradeTakeConfig(
                        tradeOrder.getCompanyId(),
                        PlatformTypeEnum.getDataByNumber(tradeOrder.getPlatformType()),
                        ProfitSharingReceiverRoleEnum.role1,
                        2L,
                        null);
                // 代理商配置
                BsTradeTakeConfig agentTakeConfig = tradeTakeConfigService.getTradeTakeConfig(
                        tradeOrder.getCompanyId(),
                        PlatformTypeEnum.getDataByNumber(tradeOrder.getPlatformType()),
                        ProfitSharingReceiverRoleEnum.role2,
                        null,
                        tradeOrder.getAgentId());

                BigDecimal totalProfit = new BigDecimal("0");
                BigDecimal surplusProfit = new BigDecimal("0");
                if (tradeOrder.getStoreDiscountType().equals(StoreDiscountActivityTypeEnum.type1.getNumber())
                    || tradeOrder.getStoreDiscountType().equals(StoreDiscountActivityTypeEnum.type2.getNumber())) {
                   // 优惠成本价 - 优惠金额 = 利润
                    totalProfit = tradeOrder.getStoreDiscountFinalCostPrice().subtract(tradeOrder.getStoreDiscountActualPrice());
                    // 剩余利润
                    surplusProfit = totalProfit;

                } else if (tradeOrder.getStoreDiscountType().equals(StoreDiscountActivityTypeEnum.type3.getNumber())) {
                    // 实际交易金额 - 平台交易成本价 = 利润
                    totalProfit = tradeOrder.getStoreDiscountProfitPrice();
                    // 剩余利润
                    surplusProfit = totalProfit;
                }
                tradeOrderProfitSharing.setProfitSharingAmoun(totalProfit);

                for (ProfitSharingReceiverObjectType receiverObjectType : ProfitSharingReceiverObjectType.getDataList()) {
                    if (receiverObjectType.getType().equals(ProfitSharingReceiverObjectType.operating.getType())) {
                        // 运营商抽成
                        BigDecimal operatingAmount;
                        if (agentTakeConfig != null) {
                            operatingAmount = surplusProfit.multiply(operatingTakeConfig.getReceiverRatio()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        } else {
                            operatingAmount = surplusProfit;
                        }
                        BsTradeOrderProfitSharingReceiver profitSharingReceiver = new BsTradeOrderProfitSharingReceiver();
                        profitSharingReceiver.setTradeOrderProfitSharingId(tradeOrderProfitSharing.getId());
                        profitSharingReceiver.setTradeOrderId(tradeOrder.getId());
                        profitSharingReceiver.setTradeOrderNo(tradeOrder.getOutTradeNo());
                        profitSharingReceiver.setProfitSharingAmount(surplusProfit);
                        profitSharingReceiver.setRatio(operatingTakeConfig.getReceiverRatio());
                        profitSharingReceiver.setReceiverObjectType(ProfitSharingReceiverObjectType.operating.getType());
                        profitSharingReceiver.setReceiverObjectId(operatingTakeConfig.getReceiverObjectId().toString());
                        profitSharingReceiver.setReceiverAccount(operatingTakeConfig.getReceiverAccount());
                        profitSharingReceiver.setReceiverName(operatingTakeConfig.getReceiverName());
                        profitSharingReceiver.setReceiverAmount(operatingAmount);
                        tradeOrderProfitSharingReceiverService.editData(profitSharingReceiver);

                        surplusProfit = surplusProfit.subtract(operatingAmount);

                    } else if (receiverObjectType.getType().equals(ProfitSharingReceiverObjectType.company.getType())) {
                        // 公司抽成
                        BigDecimal companyAmount;
                        if (agentTakeConfig != null) {
                            companyAmount = surplusProfit.multiply(agentTakeConfig.getReceiverRatio()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        } else {
                            companyAmount = new BigDecimal("0");
                        }
                        BsTradeOrderProfitSharingReceiver profitSharingReceiver = new BsTradeOrderProfitSharingReceiver();
                        profitSharingReceiver.setTradeOrderProfitSharingId(tradeOrderProfitSharing.getId());
                        profitSharingReceiver.setTradeOrderId(tradeOrder.getId());
                        profitSharingReceiver.setTradeOrderNo(tradeOrder.getOutTradeNo());
                        profitSharingReceiver.setProfitSharingAmount(surplusProfit);
                        profitSharingReceiver.setRatio(agentTakeConfig!=null?agentTakeConfig.getReceiverRatio():new BigDecimal("0"));
                        profitSharingReceiver.setReceiverObjectType(ProfitSharingReceiverObjectType.company.getType());
                        profitSharingReceiver.setReceiverObjectId(tradeOrder.getCompanyId()!=null?tradeOrder.getCompanyId().toString():null);
                        profitSharingReceiver.setReceiverAccount(agentTakeConfig!=null?agentTakeConfig.getReceiverAccount():null);
                        profitSharingReceiver.setReceiverName(agentTakeConfig!=null?agentTakeConfig.getReceiverName():null);
                        profitSharingReceiver.setReceiverAmount(companyAmount);
                        tradeOrderProfitSharingReceiverService.editData(profitSharingReceiver);

                        surplusProfit = surplusProfit.subtract(companyAmount);

                    } else if (receiverObjectType.getType().equals(ProfitSharingReceiverObjectType.agent.getType())) {
                        // 代理商抽成
                        BigDecimal agentAmount = surplusProfit;

                        BsTradeOrderProfitSharingReceiver profitSharingReceiver = new BsTradeOrderProfitSharingReceiver();
                        profitSharingReceiver.setTradeOrderProfitSharingId(tradeOrderProfitSharing.getId());
                        profitSharingReceiver.setTradeOrderId(tradeOrder.getId());
                        profitSharingReceiver.setTradeOrderNo(tradeOrder.getOutTradeNo());
                        profitSharingReceiver.setProfitSharingAmount(surplusProfit);
                        profitSharingReceiver.setRatio(agentTakeConfig!=null?agentTakeConfig.getSubReceiverRatio(): new BigDecimal("0"));
                        profitSharingReceiver.setReceiverObjectType(ProfitSharingReceiverObjectType.agent.getType());
                        profitSharingReceiver.setReceiverObjectId(tradeOrder.getAgentId().toString());
                        profitSharingReceiver.setReceiverAccount(agentTakeConfig!=null?agentTakeConfig.getSubReceiverAccount():null);
                        profitSharingReceiver.setReceiverName(agentTakeConfig!=null?agentTakeConfig.getSubReceiverName():null);
                        profitSharingReceiver.setReceiverAmount(agentAmount);
                        tradeOrderProfitSharingReceiverService.editData(profitSharingReceiver);
                    }
                }

            }

            // 用户优惠券优惠
            if (tradeOrder.getUserDiscountId() != null) {
                // 运营商配置
                BsTradeTakeConfig operatingTakeConfig = tradeTakeConfigService.getTradeTakeConfig(
                        tradeOrder.getCompanyId(),
                        PlatformTypeEnum.getDataByNumber(tradeOrder.getPlatformType()),
                        ProfitSharingReceiverRoleEnum.role1,
                        2L,
                        null);
                // 代理商配置
                BsTradeTakeConfig agentTakeConfig = tradeTakeConfigService.getTradeTakeConfig(
                        tradeOrder.getCompanyId(),
                        PlatformTypeEnum.getDataByNumber(tradeOrder.getPlatformType()),
                        ProfitSharingReceiverRoleEnum.role2,
                        null,
                        tradeOrder.getAgentId());

                BigDecimal totalProfit = new BigDecimal("0");
                BigDecimal surplusProfit = new BigDecimal("0");
                if (tradeOrder.getStoreDiscountType().equals(StoreDiscountActivityTypeEnum.type1.getNumber())
                        || tradeOrder.getStoreDiscountType().equals(StoreDiscountActivityTypeEnum.type2.getNumber())) {
                    // 优惠成本价 - 优惠金额 = 利润
                    totalProfit = tradeOrder.getStoreDiscountActualPrice().subtract(tradeOrder.getStoreDiscountActualPrice());
                    // 剩余利润
                    surplusProfit = totalProfit;

                } else if (tradeOrder.getStoreDiscountType().equals(StoreDiscountActivityTypeEnum.type3.getNumber())) {
                    // 平台交易成本价
                    BigDecimal finalCostPrice = tradeOrder.getTradeAmount().multiply(tradeOrder.getStoreDiscountFinalCostPrice()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                    // 实际交易金额 - 平台交易成本价 = 利润
                    totalProfit = tradeOrder.getTradeActualAmount().subtract(finalCostPrice);
                    // 剩余利润
                    surplusProfit = totalProfit;
                }

                tradeOrderProfitSharing.setProfitSharingAmoun(totalProfit);
                for (ProfitSharingReceiverObjectType receiverObjectType : ProfitSharingReceiverObjectType.getDataList()) {
                    if (receiverObjectType.getType().equals(ProfitSharingReceiverObjectType.operating.getType())) {
                        // 运营商抽成
                        BigDecimal operatingAmount;
                        if (agentTakeConfig != null) {
                            operatingAmount = surplusProfit.multiply(operatingTakeConfig.getReceiverRatio()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        } else {
                            operatingAmount = surplusProfit;
                        }
                        BsTradeOrderProfitSharingReceiver profitSharingReceiver = new BsTradeOrderProfitSharingReceiver();
                        profitSharingReceiver.setTradeOrderProfitSharingId(tradeOrderProfitSharing.getId());
                        profitSharingReceiver.setTradeOrderId(tradeOrder.getId());
                        profitSharingReceiver.setTradeOrderNo(tradeOrder.getOutTradeNo());
                        profitSharingReceiver.setProfitSharingAmount(surplusProfit);
                        profitSharingReceiver.setRatio(operatingTakeConfig.getReceiverRatio());
                        profitSharingReceiver.setReceiverObjectType(ProfitSharingReceiverObjectType.operating.getType());
                        profitSharingReceiver.setReceiverObjectId(operatingTakeConfig.getReceiverObjectId().toString());
                        profitSharingReceiver.setReceiverAccount(operatingTakeConfig.getReceiverAccount());
                        profitSharingReceiver.setReceiverName(operatingTakeConfig.getReceiverName());
                        profitSharingReceiver.setReceiverAmount(operatingAmount);
                        tradeOrderProfitSharingReceiverService.editData(profitSharingReceiver);

                        surplusProfit = surplusProfit.subtract(operatingAmount);

                    } else if (receiverObjectType.getType().equals(ProfitSharingReceiverObjectType.company.getType())) {
                        // 公司抽成
                        BigDecimal companyAmount;
                        if (agentTakeConfig != null) {
                            companyAmount = surplusProfit.multiply(agentTakeConfig.getReceiverRatio()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        } else {
                            companyAmount = new BigDecimal("0");
                        }
                        BsTradeOrderProfitSharingReceiver profitSharingReceiver = new BsTradeOrderProfitSharingReceiver();
                        profitSharingReceiver.setTradeOrderProfitSharingId(tradeOrderProfitSharing.getId());
                        profitSharingReceiver.setTradeOrderId(tradeOrder.getId());
                        profitSharingReceiver.setTradeOrderNo(tradeOrder.getOutTradeNo());
                        profitSharingReceiver.setProfitSharingAmount(surplusProfit);
                        profitSharingReceiver.setRatio(agentTakeConfig!=null?agentTakeConfig.getReceiverRatio():new BigDecimal("0"));
                        profitSharingReceiver.setReceiverObjectType(ProfitSharingReceiverObjectType.company.getType());
                        profitSharingReceiver.setReceiverObjectId(tradeOrder.getCompanyId()!=null?tradeOrder.getCompanyId().toString():null);
                        profitSharingReceiver.setReceiverAccount(agentTakeConfig!=null?agentTakeConfig.getReceiverAccount():null);
                        profitSharingReceiver.setReceiverName(agentTakeConfig!=null?agentTakeConfig.getReceiverName():null);
                        profitSharingReceiver.setReceiverAmount(companyAmount);
                        tradeOrderProfitSharingReceiverService.editData(profitSharingReceiver);

                        surplusProfit = surplusProfit.subtract(companyAmount);

                    } else if (receiverObjectType.getType().equals(ProfitSharingReceiverObjectType.agent.getType())) {
                        // 代理商抽成
                        BigDecimal agentAmount = surplusProfit;

                        BsTradeOrderProfitSharingReceiver profitSharingReceiver = new BsTradeOrderProfitSharingReceiver();
                        profitSharingReceiver.setTradeOrderProfitSharingId(tradeOrderProfitSharing.getId());
                        profitSharingReceiver.setTradeOrderId(tradeOrder.getId());
                        profitSharingReceiver.setTradeOrderNo(tradeOrder.getOutTradeNo());
                        profitSharingReceiver.setProfitSharingAmount(surplusProfit);
                        profitSharingReceiver.setRatio(agentTakeConfig!=null?agentTakeConfig.getSubReceiverRatio(): new BigDecimal("0"));
                        profitSharingReceiver.setReceiverObjectType(ProfitSharingReceiverObjectType.agent.getType());
                        profitSharingReceiver.setReceiverObjectId(tradeOrder.getAgentId().toString());
                        profitSharingReceiver.setReceiverAccount(agentTakeConfig!=null?agentTakeConfig.getSubReceiverAccount():null);
                        profitSharingReceiver.setReceiverName(agentTakeConfig!=null?agentTakeConfig.getSubReceiverName():null);
                        profitSharingReceiver.setReceiverAmount(agentAmount);
                        tradeOrderProfitSharingReceiverService.editData(profitSharingReceiver);
                    }
                }

            }

            editData(tradeOrderProfitSharing);
        }
    }

    @Override
    public BsTradeOrderProfitSharing getProfitSharingByTradeOrderId(ProfitSharingCmdTypeEnum profitSharingCmdType, Long tradeOrderId) {
        BsTradeOrderProfitSharingExample example = new BsTradeOrderProfitSharingExample();
        BsTradeOrderProfitSharingExample.Criteria criteria = example.createCriteria()
                .andCmdTypeEqualTo(profitSharingCmdType.getNumber())
                .andTradeOrderIdEqualTo(tradeOrderId);

        List<BsTradeOrderProfitSharing> list = tradeOrderProfitSharingMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsTradeOrderProfitSharing getDetailByProfitSharingOrderNo(String profitSharingOrderNo) {
        BsTradeOrderProfitSharingExample example = new BsTradeOrderProfitSharingExample();
        example.createCriteria().andProfitSharingOrderNoEqualTo(profitSharingOrderNo);
        List<BsTradeOrderProfitSharing> list = tradeOrderProfitSharingMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsTradeOrderProfitSharing> getNeedProfitSharingOrder() {
        return tradeOrderProfitSharingMapper.getNeedProfitSharingOrder();
    }

    @Override
    public List<BsTradeOrderProfitSharing> getNeedFinishProfitSharingOrder() {
        return tradeOrderProfitSharingMapper.getNeedFinishProfitSharingOrder();
    }
}
