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;

import java.math.BigDecimal;

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

    default PageResult<ProductsDetailDO> selectPage(ProductDetailsPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ProductsDetailDO>()
                .eqIfPresent(ProductsDetailDO::getPurchaseType, reqVO.getPurchaseType())
                .eqIfPresent(ProductsDetailDO::getProductId, reqVO.getProductId())
                .eqIfPresent(ProductsDetailDO::getDuration, reqVO.getDuration())
                .eqIfPresent(ProductsDetailDO::getGiftDuration, reqVO.getGiftDuration())
                .eqIfPresent(ProductsDetailDO::getOutput, reqVO.getOutput())
                .eqIfPresent(ProductsDetailDO::getGiftOutput, reqVO.getGiftOutput())
                .eqIfPresent(ProductsDetailDO::getOriginalPrice, reqVO.getOriginalPrice())
                .eqIfPresent(ProductsDetailDO::getDiscount, reqVO.getDiscount())
                .eqIfPresent(ProductsDetailDO::getCurrentPrice, reqVO.getCurrentPrice())
                .eqIfPresent(ProductsDetailDO::getIsDiscounted, reqVO.getIsDiscounted())
        );
    }


    // 时长查重，添加productId为查询条件
    default int selectCountByDuration(Long productId, Long productDetailId, String 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));
    }



    // 产量查重
    default int selectCountByOutput(BigDecimal output, Long productId) {
        LambdaQueryWrapperX<ProductsDetailDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(ProductsDetailDO::getProductId, productId)
                    .eq(ProductsDetailDO::getDeleted, false)
                    .eq(ProductsDetailDO::getDuration, output);
        return Math.toIntExact(selectCount(queryWrapper));

    }
}
