package com.cf.imes.module.system.dal.mysql.funds.products;

import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.system.controller.admin.funds.products.vo.productDetails.ProductDetailsPageReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.products.ProductsDetailDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 产品明细表 product_details Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface ProductsDetailMapper extends BaseMapperX<ProductsDetailDO> {

    /**
     * 分页查询
     *
     * @param reqVO
     * @return
     */
    default PageResult<ProductsDetailDO> selectPage(ProductDetailsPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ProductsDetailDO>()
                .eqIfPresent(ProductsDetailDO::getProductId, reqVO.getProductId())
                .eqIfPresent(ProductsDetailDO::getDuration, reqVO.getDuration())
                .eqIfPresent(ProductsDetailDO::getDiscount, reqVO.getDiscount())
                .eqIfPresent(ProductsDetailDO::getDeleted, false)
                .orderByAsc(ProductsDetailDO::getDuration)
        );
    }

    /**
     * 查询产品id下相同时长、时长单位的数量
     *
     * @param productId
     * @param productDetailId
     * @param duration
     * @param durationUnit
     * @return
     */
    default int selectCountByDuration(Long productId, Long productDetailId, Integer duration, Integer durationUnit) {
        LambdaQueryWrapperX<ProductsDetailDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(ProductsDetailDO::getProductId, productId)
                .eq(ProductsDetailDO::getDeleted, false)
                .eq(ProductsDetailDO::getDuration, duration)
                .eq(ProductsDetailDO::getDurationUnit, durationUnit)
                .neIfPresent(ProductsDetailDO::getId, productDetailId);
        return Math.toIntExact(selectCount(queryWrapper));
    }
}
