<?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.tmerclub.cloud.order.mapper.OrderInvoiceMapper">
  <resultMap id="orderInvoiceMap" type="com.tmerclub.cloud.order.model.OrderInvoice">
    <id column="order_invoice_id" property="orderInvoiceId" />
    <result column="order_id" property="orderId"/>
    <result column="shop_id" property="shopId"/>
    <result column="invoice_type" property="invoiceType"/>
    <result column="invoice_context" property="invoiceContext"/>
    <result column="invoice_state" property="invoiceState"/>
    <result column="header_type" property="headerType"/>
    <result column="header_name" property="headerName"/>
    <result column="invoice_tax_number" property="invoiceTaxNumber"/>
    <result column="file_id" property="fileId"/>
    <result column="application_time" property="applicationTime"/>
    <result column="upload_time" property="uploadTime"/>
    <result column="status" property="orderStatus"/>
  </resultMap>
  <resultMap id="orderInvoiceVOMap" type="com.tmerclub.cloud.order.vo.OrderInvoiceVO">
    <id column="order_invoice_id" property="orderInvoiceId" />
    <result column="order_id" property="orderId"/>
    <result column="invoice_type" property="invoiceType"/>
    <result column="header_type" property="headerType"/>
    <result column="invoice_context" property="invoiceContext"/>
    <result column="invoice_state" property="invoiceState"/>
    <result column="header_name" property="headerName"/>
    <result column="invoice_tax_number" property="invoiceTaxNumber"/>
    <result column="file_id" property="fileId"/>
    <result column="application_time" property="applicationTime"/>
    <result column="upload_time" property="uploadTime"/>
    <result column="shop_id" property="shopId"/>
    <result column="shop_name" property="shopName"/>
    <result column="all_count" property="count"/>
    <result column="actual_total" property="amount"/>
    <result column="status" property="orderState"/>
    <result column="order_create_time" property="orderCreateTime"/>
    <result column="user_id" property="userId"/>
    <collection property="orderItemVOList" ofType="com.tmerclub.cloud.order.vo.MyOrderItemVO">
      <id column="order_item_id" property="orderItemId"/>
      <result column="spu_id" property="spuId"/>
      <result column="spu_name" property="spuName"/>
      <result column="sku_name" property="skuName"/>
      <result column="count" property="count"/>
      <result column="pic" property="pic"/>
      <result column="price" property="price"/>
      <result column="refund_status" property="refundStatus"/>
      <result column="activity_id" property="activityId"/>
      <result column="activity_Type" property="activityType"/>
    </collection>
  </resultMap>
  <sql id="Vo_Column_List">
    `order_invoice_id`,`order_id`,`shop_id`,`supplier_id`,`invoice_type`,`invoice_context`,`header_type`,`invoice_state`,`header_name`,`invoice_tax_number`,`file_id`,`application_time`,`upload_time`
  </sql>
  <sql id="Join_Column_List">
    oi.`order_invoice_id`,
    oi.`order_id`,
    oi.`shop_id`,
    oi.`supplier_id`,
    oi.`invoice_type`,
    oi.`invoice_context`,
    oi.`header_type`,
    oi.`invoice_state`,
    oi.`header_name`,
    oi.`invoice_tax_number`,
    oi.`file_id`,
    oi.`application_time`,
    oi.`upload_time`
  </sql>
  <select id="getByOrderInvoiceId" resultMap="orderInvoiceMap">
    select oi.`order_invoice_id`,oi.`order_id`,oi.`shop_id`, oi.`supplier_id`, oi.`invoice_type`,oi.`invoice_context`,oi.`header_type`,
           oi.`invoice_state`,oi.`header_name`,oi.`invoice_tax_number`,oi.`file_id`,oi.`application_time`,oi.`upload_time`,o.`status`
    from order_invoice oi
    left join `order` o on oi.order_id = o.order_id
    where oi.order_invoice_id = #{orderInvoiceId}
  </select>
  <insert id="save">
    insert into order_invoice (`order_invoice_id`,`order_id`,`shop_id`,`supplier_id`,`invoice_type`,`invoice_context`,`invoice_state`,`header_type`,`header_name`,`invoice_tax_number`,`file_id`,`application_time`,`upload_time`)
    values (#{orderInvoice.orderInvoiceId},#{orderInvoice.orderId},#{orderInvoice.shopId},#{orderInvoice.supplierId},#{orderInvoice.invoiceType},#{orderInvoice.invoiceContext},#{orderInvoice.invoiceState},#{orderInvoice.headerType},#{orderInvoice.headerName},#{orderInvoice.invoiceTaxNumber},#{orderInvoice.fileId},#{orderInvoice.applicationTime},#{orderInvoice.uploadTime});
  </insert>

  <insert id="saveBatch">
    insert into order_invoice (`order_invoice_id`,`order_id`,`shop_id`,`supplier_id`,`invoice_type`,`invoice_context`,`invoice_state`,`header_type`,`header_name`,`invoice_tax_number`,`file_id`,`application_time`,`upload_time`) values
    <foreach collection="orderInvoices" item="orderInvoice" separator=",">
       (#{orderInvoice.orderInvoiceId},#{orderInvoice.orderId},#{orderInvoice.shopId},#{orderInvoice.supplierId},#{orderInvoice.invoiceType},#{orderInvoice.invoiceContext},#{orderInvoice.invoiceState},#{orderInvoice.headerType},#{orderInvoice.headerName},#{orderInvoice.invoiceTaxNumber},#{orderInvoice.fileId},#{orderInvoice.applicationTime},#{orderInvoice.uploadTime})
    </foreach>
  </insert>

  <update id="update">
    update order_invoice
    <set>
      <if test="orderInvoice.invoiceType != null">
        `invoice_type` = #{orderInvoice.invoiceType},
      </if>
      <if test="orderInvoice.invoiceContext != null">
        `invoice_context` = #{orderInvoice.invoiceContext},
      </if>
      <if test="orderInvoice.headerType != null">
        `header_type` = #{orderInvoice.headerType},
      </if>
      <if test="orderInvoice.invoiceState != null">
        `invoice_state` = #{orderInvoice.invoiceState},
      </if>
      <if test="orderInvoice.headerName != null">
        `header_name` = #{orderInvoice.headerName},
      </if>
      <if test="orderInvoice.invoiceTaxNumber != null">
        `invoice_tax_number` = #{orderInvoice.invoiceTaxNumber},
      </if>
      <if test="orderInvoice.fileId != null">
        `file_id` = #{orderInvoice.fileId},
      </if>
      <if test="orderInvoice.applicationTime != null">
        `application_time` = #{orderInvoice.applicationTime},
      </if>
      <if test="orderInvoice.uploadTime != null">
        `upload_time` = #{orderInvoice.uploadTime},
      </if>
    </set>
    where order_invoice_id = #{orderInvoice.orderInvoiceId}
  </update>
  <delete id="deleteById">
    delete from order_invoice where order_invoice_id = #{orderInvoiceId}
  </delete>
  <select id="getById" resultMap="orderInvoiceVOMap">
    SELECT oiv.* ,o.`actual_total`, o.`status`,o.`all_count`,o.`create_time` AS order_create_time,
           o.`shop_name`,o.`shop_id`,IFNULL(oil.`spu_name`,oi.`spu_name`) AS spu_name,o.`user_id`,oi.spu_id,
           IFNULL(oil.sku_name,oi.sku_name) AS sku_name,oi.`count`,oi.`order_item_id`,oi.`pic`,oi.`price`,oi.`refund_status`,oi.`activity_id`,oi.`activity_Type`
    FROM order_invoice oiv
           LEFT JOIN `order` o ON oiv.`order_id` = o.`order_id`
           LEFT JOIN `order_item` oi ON oiv.`order_id` = oi.`order_id`
           LEFT JOIN `order_item_lang` oil ON oi.`order_item_id` = oil.`order_item_id` AND oil.lang = #{lang}
    WHERE oiv.`order_invoice_id` = #{orderInvoiceId}
  </select>
  <select id="isUpload" resultType="int">
    select count(order_invoice_id)
    from order_invoice
    where order_id = #{orderId}
      and invoice_state = 2
      and upload_time is not null
      <if test="shopId != null">
        AND shop_id = #{shopId}
      </if>
      <if test="supplierId != null">
        AND supplier_id = #{supplierId}
        AND shop_id = 0
      </if>
  </select>
  <select id="getByOrderId" resultType="java.lang.Long">
    select
      order_invoice_id
    from
      order_invoice
    where
      order_id = #{orderId}
      <if test="shopId != null">
        AND shop_id = #{shopId}
      </if>
      <if test="supplierId != null">
        AND supplier_id = #{supplierId}
        AND shop_id = 0
      </if>
  </select>
  <select id="listByOrderIds" resultType="com.tmerclub.cloud.order.model.OrderInvoice">
    select
      <include refid="Vo_Column_List"/>
    from
      order_invoice
    where
      order_id in
    <foreach collection="orderIds" item="orderId" open="(" close=")" separator=",">
      #{orderId}
    </foreach>
  </select>

  <delete id="deleteBatch">
    delete from order_invoice where order_id in
    <foreach collection="orderIds" item="orderId" open="(" close=")" separator=",">
      #{orderId}
    </foreach>
  </delete>

  <select id="getEsOrderInvoice" resultType="com.tmerclub.cloud.api.order.bo.EsOrderInvoiceBO">
    select <include refid="Join_Column_List" />,o.status orderStatus
    from order_invoice oi
    left join `order` o on o.order_id = oi.order_id
    where oi.order_invoice_id = #{orderInvoiceId}
  </select>

  <select id="listEsOrderInvoiceByOrderId" resultType="com.tmerclub.cloud.api.order.bo.EsOrderInvoiceBO">
    SELECT
      <include refid="Join_Column_List" />,
      o.status orderStatus
    FROM
      order_invoice oi
      LEFT JOIN `order` o ON o.order_id = oi.order_id
    WHERE
      o.order_id = #{orderId}
  </select>
</mapper>
