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


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.invoice.vo.InvoiceAmountRespVO;
import com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceDetailsPageReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.invoice.InvoiceRecordsDO;
import com.cf.imes.module.system.enums.pay.InvoiceStatusEnum;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface InvoiceRecordsMapper extends BaseMapperX<InvoiceRecordsDO> {


    default PageResult<InvoiceRecordsDO> selectInvoiceRecordsPage(InvoiceDetailsPageReqVO pageReqVO) {
        Integer status = pageReqVO.getStatus();
        LambdaQueryWrapperX<InvoiceRecordsDO> queryWrapperX = new LambdaQueryWrapperX<InvoiceRecordsDO>()
                .eq(InvoiceRecordsDO::getDeleted, false)
                .eqIfPresent(InvoiceRecordsDO::getOrganId, pageReqVO.getOrganId())
                .likeIfPresent(InvoiceRecordsDO::getInvoiceNo, pageReqVO.getInvoiceNo())
                .eqIfPresent(InvoiceRecordsDO::getStatus, status)
                .likeIfPresent(InvoiceRecordsDO::getCreator, pageReqVO.getCreator())
                .likeIfPresent(InvoiceRecordsDO::getInvoicePerson, pageReqVO.getInvoicePerson())
                .geIfPresent(InvoiceRecordsDO::getInvoiceAmount, pageReqVO.getInvoiceAmountStart())
                .leIfPresent(InvoiceRecordsDO::getInvoiceAmount, pageReqVO.getInvoiceAmountEnd())
                .betweenIfPresent(InvoiceRecordsDO::getCreateTime, pageReqVO.getCreateTime())
                .betweenIfPresent(InvoiceRecordsDO::getInvoiceTime, pageReqVO.getInvoiceTime());
        if (InvoiceStatusEnum.INVOICINGSUCCESSFUL.getCode().equals(status)) {
            // 已开票按开票时间
            queryWrapperX.orderByDesc(InvoiceRecordsDO::getInvoiceTime);
        } else {
            // 待开票按申请时间
            queryWrapperX.orderByDesc(InvoiceRecordsDO::getCreateTime);
        }
        return selectPage(pageReqVO, queryWrapperX);
    }

    /**
     * 查询管理端开票金额统计：待开票、已开票
     *
     * @return
     */
    InvoiceAmountRespVO getManageInvoiceAmountStatistics();
}
