<?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.hc.business.mapper.UserInvoicesMapper">

    <resultMap type="userInvoices" id="UserInvoicesMap">
        <result property="userInvoicesId" column="user_invoices_id" jdbcType="INTEGER"/>
        <result property="invoicesHead" column="invoices_head" jdbcType="VARCHAR"/>
        <result property="invoicesType" column="invoices_type" jdbcType="INTEGER"/>
        <result property="invoicesCode" column="invoices_code" jdbcType="VARCHAR"/>
        <result property="invoicesBank" column="invoices_bank" jdbcType="VARCHAR"/>
        <result property="invoicesUser" column="invoices_user" jdbcType="VARCHAR"/>
        <result property="invoicesNo" column="invoices_no" jdbcType="VARCHAR"/>
        <result property="invoicesPrice" column="invoices_price" jdbcType="NUMERIC"/>
        <result property="createTime" column="create_time" jdbcType="INTEGER"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="time" column="time" jdbcType="INTEGER"/>
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
        <result property="classId" column="class_id" jdbcType="INTEGER"/>
        <result property="remarks" column="remarks" jdbcType="VARCHAR"/>
        <result property="remarksSign" column="remarks_sign" jdbcType="VARCHAR"/>
        <result property="taxCode" column="tax_code" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        user_invoices_id,
           invoices_head,
           invoices_type,
           invoices_code,
           invoices_bank,
           invoices_user,
           invoices_no,
           invoices_price,
             invoices_address,
             invoices_tel,
           create_time,
           `status`,
           `time`,
           user_id,
           class_id,
           remarks,
           remarks_sign,
           tax_code,
            tax_no,
            tax_no_red,
            order_id,
            buyer_phone,
            buyer_email,
            buyer_linker,
            buyer_address,
            sync_order_no,
            sync_serial_num,
            sync_status,
            sync_msg,
            sync_url,
            sync_paper_url,
            sync_ofd_url,
            sync_url_red
    </sql>

    <select id="queryDetailByUserId" resultMap="UserInvoicesMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM user_invoices
        WHERE user_id = #{userId}
    </select>

    <select id="queryAll" resultMap="UserInvoicesMap">
        SELECT user_invoices.*
        FROM user_invoices
                 INNER JOIN orders o on user_invoices.user_invoices_id = o.user_invoices_id
        WHERE order_status = 1
    </select>

    <select id="queryByClassId" resultMap="UserInvoicesMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM user_invoices
        WHERE class_id = #{classId}
    </select>

    <select id="queryByOrderId" resultMap="UserInvoicesMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM user_invoices
        WHERE order_id = #{orderId}
        <if test="syncStatus != null">
            AND sync_status = #{syncStatus}
        </if>
    </select>

    <select id="queryByStatus" resultMap="UserInvoicesMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM user_invoices
        WHERE status = #{status}
    </select>

    <select id="queryBySyncSerialNum" resultMap="UserInvoicesMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM user_invoices
        WHERE sync_serial_num = #{syncSerialNum}
        LIMIT 1
    </select>

    <select id="queryByFPCode" resultMap="UserInvoicesMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM user_invoices
    WHERE tax_code = #{fpCode}
    LIMIT 1
</select>

    <select id="queryBySyncOrderNo" resultMap="UserInvoicesMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM user_invoices
        WHERE sync_order_no = #{syncOrderNo}
        LIMIT 1
    </select>

    <select id="queryAllInvoices" resultType="com.hc.business.dto.UserInvoiceDto">
        select
        b.order_no AS orderNo, -- 订单号
        b.create_time AS createTime,-- 支付日期
        c.class_name AS className, -- 培训班次
        b.total_amount AS totalAmount, -- 订单金额
        a.invoices_price AS invoicesPrice, -- 发票金额
        a.invoices_head AS invoicesHead, -- 发票抬头
        b.order_status AS orderStatus, -- 订单状态订单状态0123支付成功，退费，部分退费 1
        d.user_mobile AS userMobile, -- 联系人  1
        a.invoices_code AS invoicesCode, -- 税号 2
        a.invoices_bank AS invoicesBank, -- 开户行 2
        a.invoices_no AS invoicesNo, -- 账号 2
        a.invoices_address AS invoicesAddress, -- 地址 2
        a.invoices_tel AS invoicesTel, -- 电话 2
        a.buyer_linker AS buyerLinker, -- 收件人 2
        a.buyer_phone AS buyerPhone, -- 收件电话 2
        a.buyer_address AS buyerAddress -- 收件地址 2
        from user_invoices a
        join orders b on a.order_id = b.orders_id
        join class_config c on b.class_id=c.class_config_id
        join `user` d on d.user_id = b.user_id
        where b.is_de=1 and a.invoices_type =2
        <if test="orderNo != null and orderNo!=''" >
            AND b.order_no = #{orderNo}
        </if>
        <if test="begin != null">
            AND b.create_time  &gt;= #{begin}
        </if>
        <if test="end != null">
            AND b.create_time &lt;= #{end}
        </if>
        ORDER BY b.create_time DESC
    </select>
</mapper>