<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yc.ship.module.trade.dal.mysql.invoice.InvoiceMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="queryInvoiceOrder"  resultType="com.yc.ship.module.trade.dal.dataobject.invoice.InvoiceDO">
        SELECT ti.*
        FROM trade_invoice ti INNER JOIN trade_invoice_detail tid on ti.id = tid.invoice_id
        and ti.deleted =0
        where tid.order_no = #{orderNo}
    </select>

    <select id="queryInvoicePage"  resultType="com.yc.ship.module.trade.controller.admin.invoice.vo.InvoiceRespVO">
        SELECT ti.*,GROUP_CONCAT(tid.order_no) orderNos
        FROM trade_invoice ti INNER JOIN trade_invoice_detail tid on ti.id = tid.invoice_id
        where ti.deleted =0
        <if test="param.fpqqlsh != null and param.fpqqlsh != ''">
            AND ti.fpqqlsh = #{param.fpqqlsh}
        </if>
        <if test="param.invoiceStatus != null and param.invoiceStatus != ''">
            AND ti.invoice_status = #{param.invoiceStatus}
        </if>
        <if test="param.buyerTaxCode != null and param.buyerTaxCode != ''">
            AND ti.buyer_tax_code = #{param.buyerTaxCode}
        </if>
        <if test="param.buyerName != null and param.buyerName != ''">
            AND ti.buyer_name like concat('%',#{param.buyerName}, '%')
        </if>
        <if test="param.orderNo != null and param.orderNo != ''">
            AND tid.order_no = #{param.orderNo}
        </if>
        <if test="param.buyerTel != null and param.buyerTel != ''">
            AND ti.buyer_tel = #{param.buyerTel}
        </if>
        <if test="param.buyerEmail != null and param.buyerEmail != ''">
            AND ti.buyer_email = #{param.buyerEmail}
        </if>
        <if test="param.beginCKprq != null and param.beginCKprq != ''">
            AND ti.create_time >= #{param.beginCKprq}
        </if>
        <if test="param.endCKprq != null and param.endCKprq != ''">
            AND ti.create_time <![CDATA[ <= ]]> #{param.endCKprq}
        </if>
        <if test="param.storeId != null and param.storeId != ''">
            AND ti.store_id = #{param.storeId}
        </if>
        <if test="param.agencyId != null and param.agencyId != ''">
            AND ti.agency_id = #{param.agencyId}
        </if>
        GROUP BY ti.id
        order by ti.create_time desc
    </select>
</mapper>