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

import cn.hutool.core.util.ObjectUtil;
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.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceAmountAndAvailablePageReqVO;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceAmountRespVO;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.PurchaseRecordPageReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.purchase.PurchaseRecordDO;
import com.cf.imes.module.system.enums.pay.InvoiceStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购买记录 Mapper
 */
@Mapper
public interface PurchaseRecordMapper extends BaseMapperX<PurchaseRecordDO> {
    /**
     * 查询机构下可开票的金额
     *
     * @param organId
     * @return
     */
    BigDecimal getInvoicingAmount(Long organId);

    /**
     * 分页查询
     *
     * @param pageReqVO
     * @return
     */
    default PageResult<PurchaseRecordDO> selectPage(PurchaseRecordPageReqVO pageReqVO) {
        return selectPage(pageReqVO, new LambdaQueryWrapperX<PurchaseRecordDO>()
                .eq(PurchaseRecordDO::getDeleted, false)
                .eqIfPresent(PurchaseRecordDO::getOrganId, pageReqVO.getOrganId())
                .likeIfPresent(PurchaseRecordDO::getProductName, pageReqVO.getProductName())
                .eqIfPresent(PurchaseRecordDO::getPurchaseDuration, pageReqVO.getPurchaseDuration())
                .betweenIfPresent(PurchaseRecordDO::getCreateTime, pageReqVO.getCreateTime())
                .likeIfPresent(PurchaseRecordDO::getCreator, pageReqVO.getCreator())
                .orderByDesc(PurchaseRecordDO::getCreateTime));

    }

    /**
     * 分页查询可开票记录
     *
     * @param pageReqVO
     * @return
     */
    default PageResult<PurchaseRecordDO> selectPage(InvoiceAmountAndAvailablePageReqVO pageReqVO) {
        return selectPage(pageReqVO, new LambdaQueryWrapperX<PurchaseRecordDO>()
                .betweenIfPresent(PurchaseRecordDO::getCreateTime, pageReqVO.getCreateTime())
                .geIfPresent(PurchaseRecordDO::getTotalAmount, ObjectUtil.isNotNull(pageReqVO.getInvoiceAmount()) ? pageReqVO.getInvoiceAmount()[0] : null)
                .leIfPresent(PurchaseRecordDO::getTotalAmount, ObjectUtil.isNotNull(pageReqVO.getInvoiceAmount()) ? pageReqVO.getInvoiceAmount()[1] : null)
                .eq(PurchaseRecordDO::getOrganId, SecurityFrameworkUtils.getUserOrganId())
                .eq(PurchaseRecordDO::getDeleted, false)
                .in(PurchaseRecordDO::getIsInvocing, InvoiceStatusEnum.INVOICABLE.getCode(), InvoiceStatusEnum.INVOICINGFAILED.getCode())
                .orderByDesc(PurchaseRecordDO::getCreateTime)
        );

    }

    /**
     * 查询机构下开票金额统计：可开票、开票中、已开票
     * @param organId
     * @return
     */
    InvoiceAmountRespVO getOrgInvoiceAmountStatistics(Long organId);

    /**
     * 查询可开票的金额
     *
     * @param organId
     */
    BigDecimal getInvoicableAmount(Long organId, @Param("ids") List<Long> ids);
}
