package cn.newfeifan.mall.module.distri.dal.mysql.ptprofitlog;


import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.newfeifan.mall.framework.mybatis.core.mapper.BaseMapperX;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofitlog.PtProfitLogDO;
import org.apache.ibatis.annotations.Mapper;
import cn.newfeifan.mall.module.distri.controller.app.ptprofitlog.vo.*;

import java.util.Arrays;
import java.util.List;

import static cn.newfeifan.mall.module.distri.constant.DistriConstants.PT_ID;
import static cn.newfeifan.mall.module.distri.enums.CaclEnum.*;
import static cn.newfeifan.mall.module.distri.enums.CaclEnum.GROSS_PROFIT_ANCESTOR_QUOTA_PERC_EXCEED_MAXIMUM_LIMIT;

/**
 * 平台利润记录 Mapper
 *
 * @author 非繁人
 */
@Mapper
public interface PtProfitLogMapper extends BaseMapperX<PtProfitLogDO> {

    default PageResult<PtProfitLogDO> selectPage(PtProfitLogPageReqVO reqVO) {
        //用户只能查看这些类型
        List<Integer> profitStatus;

        LambdaQueryWrapperX<PtProfitLogDO> queryWrapper = new LambdaQueryWrapperX<PtProfitLogDO>()
                .eqIfPresent(PtProfitLogDO::getAmount, reqVO.getAmount())
                .eqIfPresent(PtProfitLogDO::getAfterAmount, reqVO.getAfterAmount())
                .eqIfPresent(PtProfitLogDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(PtProfitLogDO::getPercentTemplate, reqVO.getPercentTemplate())
                .eqIfPresent(PtProfitLogDO::getUserId, reqVO.getUserId())
                .orderByDesc(PtProfitLogDO::getCreateTime)
                .orderByDesc(PtProfitLogDO::getId);

        if (!reqVO.getIsFreeze()) {
            //可用
            profitStatus = Arrays.asList(
                    RECOMMENDED_PERSON_QUOTA.getType(),
                    DIRECT_REFERRAL_QUOTA.getType(),
                    SMALL_QUOTA_CRASH_SETTLE.getType(),
                    ORDER_PAY_INTEGRAL.getType(),
                    ORDER_REFUND_INTEGRAL.getType(),
                    ORDER_CANCEL_BY_USER_REFUND_INTEGRAL.getType(),
                    ORDER_CANCEL_BY_SYSTEM_REFUND_INTEGRAL.getType(),
                    MANUAL_RETURN_INTEGRAL.getType(),
                    WITHDRAWAL.getType(),
                    WITHDRAWAL_ERROR.getType()
            );
//            queryWrapper.ne(PtProfitLogDO::getAmount,0);
        } else {
            //冻结
            profitStatus = Arrays.asList(
                    RECOMMENDED_PERSON_QUOTA.getType(),
                    DIRECT_REFERRAL_QUOTA.getType(),
                    SMALL_QUOTA_CRASH.getType(),
                    ORDER_PAY_INTEGRAL_ANCESTER.getType(),
                    ORDER_REFUND_INTEGRAL_CONSUMER.getType(),
                    ORDER_REFUND_INTEGRAL_ANCESTER.getType(),
                    ORDER_PAY_INTEGRAL_CONSUMER.getType());
//            queryWrapper.ne(PtProfitLogDO::getFreezeAmount,0);
        }

        if (!reqVO.getIsFreeze() && reqVO.getUserId().equals(PT_ID)) {
            //平台账号可用
            profitStatus = Arrays.asList(
                    PLATFORM_TOTAL_ADD.getType(),
                    PLATFORM_SERVICE_FEE.getType(),
                    PLATFORM_REVENUE.getType(),
                    SMALL_QUOTA_CRASH_SETTLE.getType(),
                    AFTER_CRASH_CALC_PT_TOTAL_QUOTA.getType(),
                    AFTER_CRASH_CALC_PT_TOTAL_GROSS_QUOTA.getType(),
                    GROSS_PROFIT_BONUS_QUOTA_PERC_EXCEED_MAXIMUM_LIMIT.getType(),
                    GROSS_PROFIT_ANCESTOR_QUOTA_PERC_EXCEED_MAXIMUM_LIMIT.getType(),
                    MANUAL_RETURN_INTEGRAL.getType(),
                    WITHDRAWAL.getType(),
                    WITHDRAWAL_ERROR.getType()
            );
            reqVO.setUserId(null);
        } else if (reqVO.getIsFreeze() && reqVO.getUserId().equals(PT_ID)) {
            profitStatus = Arrays.asList(
                    RECOMMENDED_PERSON_QUOTA.getType(),
                    DIRECT_REFERRAL_QUOTA.getType(),
                    SMALL_QUOTA_CRASH.getType(),
                    ORDER_PAY_INTEGRAL.getType(),
                    ORDER_REFUND_INTEGRAL.getType(),
                    ORDER_CANCEL_BY_USER_REFUND_INTEGRAL.getType(),
                    ORDER_CANCEL_BY_SYSTEM_REFUND_INTEGRAL.getType()
            );
//            reqVO.setUserId(null);
        }

        queryWrapper.inIfPresent(PtProfitLogDO::getProfitStatus, profitStatus);

        return selectPage(reqVO, queryWrapper);
    }

    default PageResult<PtProfitLogDO> getMaxAmountPage(PtProfitLogPageReqVO pageReqVO) {
        List<Integer> profitStatus = Arrays.asList(
                DIRECT_REFERRAL_QUOTA.getType(),
                RECOMMENDED_PERSON_QUOTA.getType(),
                SMALL_QUOTA_CRASH.getType(),
                ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
                ORDER_REFUND_ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
                MANUAL_RETURN_INTEGRAL.getType());
        return selectPage(pageReqVO, new LambdaQueryWrapperX<PtProfitLogDO>()
                .eqIfPresent(PtProfitLogDO::getUserId, pageReqVO.getUserId())
                .inIfPresent(PtProfitLogDO::getProfitStatus, profitStatus)
                .orderByDesc(PtProfitLogDO::getCreateTime)
                .orderByDesc(PtProfitLogDO::getId)
        );
    }
}