<?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="cn.superdesk.uniorder.dao.mapper.thirdpart.qchjc.invoice.QchjcInvoiceApplyRelationEntityMapper">
  <resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.invoice.QchjcInvoiceApplyRelationEntity">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="qchjc_invoice_apply_id" jdbcType="BIGINT" property="qchjcInvoiceApplyId" />
    <result column="invoice_apply_sn" jdbcType="VARCHAR" property="invoiceApplySn" />
    <result column="goods_quantity" jdbcType="INTEGER" property="goodsQuantity" />
    <result column="invoice_apply_amount" jdbcType="DECIMAL" property="invoiceApplyAmount" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, qchjc_invoice_apply_id, invoice_apply_sn, goods_quantity, invoice_apply_amount
  </sql>
  <select id="selectByExample" parameterType="cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.invoice.QchjcInvoiceApplyRelationEntityExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from qchjc_invoice_apply_relation
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from qchjc_invoice_apply_relation
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from qchjc_invoice_apply_relation
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.invoice.QchjcInvoiceApplyRelationEntityExample">
    delete from qchjc_invoice_apply_relation
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.invoice.QchjcInvoiceApplyRelationEntity">
    insert into qchjc_invoice_apply_relation (id, qchjc_invoice_apply_id, invoice_apply_sn, 
      goods_quantity, invoice_apply_amount)
    values (#{id,jdbcType=BIGINT}, #{qchjcInvoiceApplyId,jdbcType=BIGINT}, #{invoiceApplySn,jdbcType=VARCHAR}, 
      #{goodsQuantity,jdbcType=INTEGER}, #{invoiceApplyAmount,jdbcType=DECIMAL})
  </insert>
  <insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.invoice.QchjcInvoiceApplyRelationEntity">
    insert into qchjc_invoice_apply_relation
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="qchjcInvoiceApplyId != null">
        qchjc_invoice_apply_id,
      </if>
      <if test="invoiceApplySn != null">
        invoice_apply_sn,
      </if>
      <if test="goodsQuantity != null">
        goods_quantity,
      </if>
      <if test="invoiceApplyAmount != null">
        invoice_apply_amount,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="qchjcInvoiceApplyId != null">
        #{qchjcInvoiceApplyId,jdbcType=BIGINT},
      </if>
      <if test="invoiceApplySn != null">
        #{invoiceApplySn,jdbcType=VARCHAR},
      </if>
      <if test="goodsQuantity != null">
        #{goodsQuantity,jdbcType=INTEGER},
      </if>
      <if test="invoiceApplyAmount != null">
        #{invoiceApplyAmount,jdbcType=DECIMAL},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.invoice.QchjcInvoiceApplyRelationEntityExample" resultType="java.lang.Long">
    select count(*) from qchjc_invoice_apply_relation
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update qchjc_invoice_apply_relation
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.qchjcInvoiceApplyId != null">
        qchjc_invoice_apply_id = #{record.qchjcInvoiceApplyId,jdbcType=BIGINT},
      </if>
      <if test="record.invoiceApplySn != null">
        invoice_apply_sn = #{record.invoiceApplySn,jdbcType=VARCHAR},
      </if>
      <if test="record.goodsQuantity != null">
        goods_quantity = #{record.goodsQuantity,jdbcType=INTEGER},
      </if>
      <if test="record.invoiceApplyAmount != null">
        invoice_apply_amount = #{record.invoiceApplyAmount,jdbcType=DECIMAL},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update qchjc_invoice_apply_relation
    set id = #{record.id,jdbcType=BIGINT},
      qchjc_invoice_apply_id = #{record.qchjcInvoiceApplyId,jdbcType=BIGINT},
      invoice_apply_sn = #{record.invoiceApplySn,jdbcType=VARCHAR},
      goods_quantity = #{record.goodsQuantity,jdbcType=INTEGER},
      invoice_apply_amount = #{record.invoiceApplyAmount,jdbcType=DECIMAL}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.invoice.QchjcInvoiceApplyRelationEntity">
    update qchjc_invoice_apply_relation
    <set>
      <if test="qchjcInvoiceApplyId != null">
        qchjc_invoice_apply_id = #{qchjcInvoiceApplyId,jdbcType=BIGINT},
      </if>
      <if test="invoiceApplySn != null">
        invoice_apply_sn = #{invoiceApplySn,jdbcType=VARCHAR},
      </if>
      <if test="goodsQuantity != null">
        goods_quantity = #{goodsQuantity,jdbcType=INTEGER},
      </if>
      <if test="invoiceApplyAmount != null">
        invoice_apply_amount = #{invoiceApplyAmount,jdbcType=DECIMAL},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.superdesk.uniorder.dao.entity.thirdpart.qchjc.invoice.QchjcInvoiceApplyRelationEntity">
    update qchjc_invoice_apply_relation
    set qchjc_invoice_apply_id = #{qchjcInvoiceApplyId,jdbcType=BIGINT},
      invoice_apply_sn = #{invoiceApplySn,jdbcType=VARCHAR},
      goods_quantity = #{goodsQuantity,jdbcType=INTEGER},
      invoice_apply_amount = #{invoiceApplyAmount,jdbcType=DECIMAL}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="selectInvoiceApplyResByQchjcApplyId"
          resultMap="BaseResultMap">
    SELECT
      invoice_apply_sn,goods_quantity,invoice_apply_amount
    FROM
      invoice_apply
    WHERE
        id IN (
        SELECT
          invoice_apply_id
        FROM
          invoice_apply_x_goods
        WHERE
            unified_order_goods_id IN ( SELECT unified_id FROM qchjc_order_goods WHERE qch_customer_invoice_apply_id = #{id} ))
  </select>
  <select id="selectQchjcApplyResByInvoiceSn"
          resultType="cn.superdesk.uniorder.dto.vo.thirdpart.qchjc.invoice.QchjcApplyResVo">
    SELECT
      t2.invoice_apply_sn as invoiceApplySn,
      t2.goods_quantity as orderCount,
      t2.invoice_apply_amount as applyAmount
    FROM
      qchjc_invoice_apply t1
        LEFT JOIN qchjc_invoice_apply_relation t2 on t1.id = t2.qchjc_invoice_apply_id
    WHERE
      t1.invoice_sn = #{invoiceSn}
  </select>
</mapper>