package com.yolo.keyboard.dal.mysql.tenantcommission;

import com.yolo.keyboard.controller.admin.tenantcommission.vo.KeyboardTenantCommissionPageReqVO;
import com.yolo.keyboard.dal.dataobject.tenantcommission.KeyboardTenantCommissionDO;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.mybatis.core.mapper.BaseMapperX;
import com.yolo.keyboard.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.ibatis.annotations.Mapper;

/**
 * 租户分成记录 Mapper
 *
 * @author ziin
 */
@Mapper
public interface KeyboardTenantCommissionMapper extends BaseMapperX<KeyboardTenantCommissionDO> {

    /**
     * 根据交易ID查询分成记录
     */
    default KeyboardTenantCommissionDO selectByTransactionId(String transactionId) {
        return selectOne(KeyboardTenantCommissionDO::getTransactionId, transactionId);
    }

    /**
     * 根据内购记录ID查询分成记录
     */
    default KeyboardTenantCommissionDO selectByPurchaseRecordId(Integer purchaseRecordId) {
        return selectOne(KeyboardTenantCommissionDO::getPurchaseRecordId, purchaseRecordId);
    }

    /**
     * 根据内购记录ID和租户ID查询分成记录
     * 用于检查某个租户是否已经对某条内购记录计算过分成
     */
    default KeyboardTenantCommissionDO selectByPurchaseRecordIdAndTenantId(Integer purchaseRecordId, Long tenantId) {
        return selectOne(new LambdaQueryWrapperX<KeyboardTenantCommissionDO>()
                .eq(KeyboardTenantCommissionDO::getPurchaseRecordId, purchaseRecordId)
                .eq(KeyboardTenantCommissionDO::getTenantId, tenantId));
    }

    default PageResult<KeyboardTenantCommissionDO> selectPage(KeyboardTenantCommissionPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<KeyboardTenantCommissionDO>()
                .eqIfPresent(KeyboardTenantCommissionDO::getTenantId, reqVO.getTenantId())
                .eqIfPresent(KeyboardTenantCommissionDO::getPurchaseRecordId, reqVO.getPurchaseRecordId())
                .eqIfPresent(KeyboardTenantCommissionDO::getTransactionId, reqVO.getTransactionId())
                .eqIfPresent(KeyboardTenantCommissionDO::getInviteeUserId, reqVO.getInviteeUserId())
                .eqIfPresent(KeyboardTenantCommissionDO::getInviterUserId, reqVO.getInviterUserId())
                .eqIfPresent(KeyboardTenantCommissionDO::getPurchaseAmount, reqVO.getPurchaseAmount())
                .eqIfPresent(KeyboardTenantCommissionDO::getCommissionRate, reqVO.getCommissionRate())
                .eqIfPresent(KeyboardTenantCommissionDO::getCommissionAmount, reqVO.getCommissionAmount())
                .eqIfPresent(KeyboardTenantCommissionDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(KeyboardTenantCommissionDO::getPurchaseTime, reqVO.getPurchaseTime())
                .eqIfPresent(KeyboardTenantCommissionDO::getSettledAt, reqVO.getSettledAt())
                .eqIfPresent(KeyboardTenantCommissionDO::getBalanceTransactionId, reqVO.getBalanceTransactionId())
                .eqIfPresent(KeyboardTenantCommissionDO::getCreatedAt, reqVO.getCreatedAt())
                .eqIfPresent(KeyboardTenantCommissionDO::getUpdatedAt, reqVO.getUpdatedAt())
                .eqIfPresent(KeyboardTenantCommissionDO::getRemark, reqVO.getRemark())
                .orderByDesc(KeyboardTenantCommissionDO::getId));
    }
}
