package cn.iocoder.yudao.module.bpm.dal.mysql.oa;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.invoice.BpmOAInvoicePageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.invoice.BpmOAInvoiceRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAContractDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAInvoiceDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Objects;

@Mapper
public interface BpmOAInvoiceMapper extends BaseMapperX<BpmOAInvoiceDO> {

    default PageResult<BpmOAInvoiceRespVO> selectPage(BpmOAInvoicePageReqVO pageReqVO,
                                                      List<Long> userIds) {

        MPJLambdaWrapperX<BpmOAInvoiceDO> query = new MPJLambdaWrapperX<>();
        query.selectAll(BpmOAInvoiceDO.class);
        query.selectAs(BpmOAContractDO::getContractName, BpmOAInvoiceRespVO::getContractName);
        query.selectAs(BpmOAContractDO::getCustomerName, BpmOAInvoiceRespVO::getCustomerName);
        query.selectAs(BpmOAContractDO::getContractNo, BpmOAInvoiceRespVO::getContractNo);
        query.leftJoin(BpmOAContractDO.class, "c", BpmOAContractDO::getId, BpmOAInvoiceDO::getContractId);
        query.apply(Objects.nonNull(pageReqVO.getContractName()), "c.contract_name LIKE CONCAT('%', {0}, '%')", pageReqVO.getContractName());
        query.apply(Objects.nonNull(pageReqVO.getCustomerName()), "c.customer_name LIKE CONCAT('%', {0}, '%')", pageReqVO.getCustomerName());
        query.in(BpmOAInvoiceDO::getUserId, userIds);
        query.orderByDesc(BpmOAInvoiceDO::getCreateTime);

        return selectJoinPage(pageReqVO, BpmOAInvoiceRespVO.class, query);
    }

    default List<BpmOAInvoiceRespVO> selectListByProcessInstanceIds(List<String> processInstanceIds) {

        MPJLambdaWrapperX<BpmOAInvoiceDO> query = new MPJLambdaWrapperX<>();
        query.selectAll(BpmOAInvoiceDO.class);
        query.selectAs(BpmOAContractDO::getContractName, BpmOAInvoiceRespVO::getContractName);
        query.selectAs(BpmOAContractDO::getCustomerName, BpmOAInvoiceRespVO::getCustomerName);
        query.leftJoin(BpmOAContractDO.class, BpmOAContractDO::getId, BpmOAInvoiceDO::getContractId);
        query.inIfPresent(BpmOAInvoiceDO::getProcessInstanceId, processInstanceIds);

        return selectJoinList(BpmOAInvoiceRespVO.class, query);
    }
}
