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.admin.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.COMMISSION_TRANSITION;

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

    default PageResult<PtProfitLogDO> selectPage(PtProfitLogPageReqVO reqVO) {
        //用户只能查看这些类型
        List<Integer> 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(),
//                ORDER_PAY_INTEGRAL_ANCESTER.getType(),
                ORDER_REFUND_INTEGRAL_CONSUMER.getType(),
                ORDER_REFUND_INTEGRAL_ANCESTER.getType(),
//                ORDER_PAY_INTEGRAL_CONSUMER.getType(),
                SMALL_QUOTA_CRASH_SETTLE.getType(),
//                ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
                ORDER_REFUND_ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
                MANUAL_RETURN_INTEGRAL.getType(),
                WITHDRAWAL.getType(),
                WITHDRAWAL_ERROR.getType(),
                COMMISSION_TRANSITION.getType()
        );

        if(reqVO.getUserId() != null && 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(),
                    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(),
//                    ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
//                    ORDER_REFUND_ORDER_PAY_FREEZE_HIGH_QUOTA.getType(),
                    WITHDRAWAL.getType(),
                    WITHDRAWAL_ERROR.getType()
            );
//            reqVO.setUserId(null);
        }

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

        if(reqVO.getVisitor() != null && reqVO.getVisitor()){
            profitStatus = Arrays.asList(
                    RECOMMENDED_PERSON_QUOTA.getType(),
                    ORDER_REFUND_INTEGRAL_ANCESTER.getType(),
                    SMALL_QUOTA_CRASH_SETTLE.getType()
                    );
            queryWrapper.in(PtProfitLogDO::getProfitStatus,profitStatus);
            queryWrapper.isNull(PtProfitLogDO::getUserId);
        } else {
            queryWrapper.in(PtProfitLogDO::getProfitStatus, profitStatus);
        }
        return selectPage(reqVO, queryWrapper
        );
    }
}