<?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.community.huiju.dao.TpBillInvoiceMapper" >
  <resultMap id="BaseResultMap" type="com.community.huiju.model.TpBillInvoice" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="community_id" property="communityId" jdbcType="INTEGER" />
    <result column="bill_id" property="billId" jdbcType="INTEGER" />
    <result column="building_owner_info_id" property="buildingOwnerInfoId" jdbcType="INTEGER" />
    <result column="ta_user_id" property="taUserId" jdbcType="INTEGER" />
    <result column="bill_invoice_explain" property="billInvoiceExplain" jdbcType="VARCHAR" />
    <result column="pay_price" property="payPrice" jdbcType="INTEGER" />
    <result column="bill_statement_id" property="billStatementId" jdbcType="INTEGER" />
    <result column="bill_status" property="billStatus" jdbcType="CHAR" />
    <result column="pay_name" property="payName" jdbcType="VARCHAR" />
    <result column="pay_date" property="payDate" jdbcType="TIMESTAMP" />
    <result column="create_user" property="createUser" jdbcType="INTEGER" />
    <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
    <result column="update_user" property="updateUser" jdbcType="INTEGER" />
    <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />
    <result column="pay_type" property="payType" jdbcType="CHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, community_id, bill_id, building_owner_info_id, ta_user_id, bill_invoice_explain, 
    pay_price, bill_statement_id, bill_status, pay_name, pay_date, create_user, create_date, 
    update_user, update_date, pay_type
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from tp_bill_invoice
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tp_bill_invoice
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.community.huiju.model.TpBillInvoice" >
    insert into tp_bill_invoice (id, community_id, bill_id, 
      building_owner_info_id, ta_user_id, bill_invoice_explain, 
      pay_price, bill_statement_id, bill_status, 
      pay_name, pay_date, create_user, 
      create_date, update_user, update_date, pay_type
      )
    values (#{id,jdbcType=INTEGER}, #{communityId,jdbcType=INTEGER}, #{billId,jdbcType=INTEGER}, 
      #{buildingOwnerInfoId,jdbcType=INTEGER}, #{taUserId,jdbcType=INTEGER}, #{billInvoiceExplain,jdbcType=VARCHAR}, 
      #{payPrice,jdbcType=INTEGER}, #{billStatementId,jdbcType=INTEGER}, #{billStatus,jdbcType=CHAR}, 
      #{payName,jdbcType=VARCHAR}, #{payDate,jdbcType=TIMESTAMP}, #{createUser,jdbcType=INTEGER}, 
      #{createDate,jdbcType=TIMESTAMP}, #{updateUser,jdbcType=INTEGER}, #{updateDate,jdbcType=TIMESTAMP},
      #{payType,jdbcType=CHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.community.huiju.model.TpBillInvoice" >
    insert into tp_bill_invoice
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="communityId != null" >
        community_id,
      </if>
      <if test="billId != null" >
        bill_id,
      </if>
      <if test="buildingOwnerInfoId != null" >
        building_owner_info_id,
      </if>
      <if test="taUserId != null" >
        ta_user_id,
      </if>
      <if test="billInvoiceExplain != null" >
        bill_invoice_explain,
      </if>
      <if test="payPrice != null" >
        pay_price,
      </if>
      <if test="billStatementId != null" >
        bill_statement_id,
      </if>
      <if test="billStatus != null" >
        bill_status,
      </if>
      <if test="payName != null" >
        pay_name,
      </if>
      <if test="payDate != null" >
        pay_date,
      </if>
      <if test="createUser != null" >
        create_user,
      </if>
      <if test="createDate != null" >
        create_date,
      </if>
      <if test="updateUser != null" >
        update_user,
      </if>
      <if test="updateDate != null" >
        update_date,
      </if>
      <if test="payType != null">
          pay_type,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="communityId != null" >
        #{communityId,jdbcType=INTEGER},
      </if>
      <if test="billId != null" >
        #{billId,jdbcType=INTEGER},
      </if>
      <if test="buildingOwnerInfoId != null" >
        #{buildingOwnerInfoId,jdbcType=INTEGER},
      </if>
      <if test="taUserId != null" >
        #{taUserId,jdbcType=INTEGER},
      </if>
      <if test="billInvoiceExplain != null" >
        #{billInvoiceExplain,jdbcType=VARCHAR},
      </if>
      <if test="payPrice != null" >
        #{payPrice,jdbcType=INTEGER},
      </if>
      <if test="billStatementId != null" >
        #{billStatementId,jdbcType=INTEGER},
      </if>
      <if test="billStatus != null" >
        #{billStatus,jdbcType=CHAR},
      </if>
      <if test="payName != null" >
        #{payName,jdbcType=VARCHAR},
      </if>
      <if test="payDate != null" >
        #{payDate,jdbcType=TIMESTAMP},
      </if>
      <if test="createUser != null" >
        #{createUser,jdbcType=INTEGER},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null" >
        #{updateUser,jdbcType=INTEGER},
      </if>
      <if test="updateDate != null" >
        #{updateDate,jdbcType=TIMESTAMP},
      </if>
      <if test="payType != null">
        #{payType,jdbcType=CHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.community.huiju.model.TpBillInvoice" >
    update tp_bill_invoice
    <set >
      <if test="communityId != null" >
        community_id = #{communityId,jdbcType=INTEGER},
      </if>
      <if test="billId != null" >
        bill_id = #{billId,jdbcType=INTEGER},
      </if>
      <if test="buildingOwnerInfoId != null" >
        building_owner_info_id = #{buildingOwnerInfoId,jdbcType=INTEGER},
      </if>
      <if test="taUserId != null" >
        ta_user_id = #{taUserId,jdbcType=INTEGER},
      </if>
      <if test="billInvoiceExplain != null" >
        bill_invoice_explain = #{billInvoiceExplain,jdbcType=VARCHAR},
      </if>
      <if test="payPrice != null" >
        pay_price = #{payPrice,jdbcType=INTEGER},
      </if>
      <if test="billStatementId != null" >
        bill_statement_id = #{billStatementId,jdbcType=INTEGER},
      </if>
      <if test="billStatus != null" >
        bill_status = #{billStatus,jdbcType=CHAR},
      </if>
      <if test="payName != null" >
        pay_name = #{payName,jdbcType=VARCHAR},
      </if>
      <if test="payDate != null" >
        pay_date = #{payDate,jdbcType=TIMESTAMP},
      </if>
      <if test="createUser != null" >
        create_user = #{createUser,jdbcType=INTEGER},
      </if>
      <if test="createDate != null" >
        create_date = #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null" >
        update_user = #{updateUser,jdbcType=INTEGER},
      </if>
      <if test="updateDate != null" >
        update_date = #{updateDate,jdbcType=TIMESTAMP},
      </if>
      <if test="payType != null">
        pay_type = #{payType,jdbcType=CHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.community.huiju.model.TpBillInvoice" >
    update tp_bill_invoice
    set community_id = #{communityId,jdbcType=INTEGER},
      bill_id = #{billId,jdbcType=INTEGER},
      building_owner_info_id = #{buildingOwnerInfoId,jdbcType=INTEGER},
      ta_user_id = #{taUserId,jdbcType=INTEGER},
      bill_invoice_explain = #{billInvoiceExplain,jdbcType=VARCHAR},
      pay_price = #{payPrice,jdbcType=INTEGER},
      bill_statement_id = #{billStatementId,jdbcType=INTEGER},
      bill_status = #{billStatus,jdbcType=CHAR},
      pay_name = #{payName,jdbcType=VARCHAR},
      pay_date = #{payDate,jdbcType=TIMESTAMP},
      create_user = #{createUser,jdbcType=INTEGER},
      create_date = #{createDate,jdbcType=TIMESTAMP},
      update_user = #{updateUser,jdbcType=INTEGER},
      update_date = #{updateDate,jdbcType=TIMESTAMP},
      pay_type = #{payType,jdbcType=CHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="getBillsList" resultType="map">
      SELECT
          i.id,
          i.bill_invoice_explain as billInvoiceExplain,
          i.pay_price AS payPrice,
          i.ta_user_id AS taUserId,
          i.bill_status AS billStatus,
          b.bill_name AS billName,
          b.bill_explain AS billExplain,
          b.end_date AS endDate,
          b.create_date AS createDate,
          i.out_trade_no AS outTradeNo
      FROM
          tp_bill_invoice i
          LEFT JOIN tp_bill b ON b.id = i.bill_id
          AND b.community_id = #{communityId,jdbcType=INTEGER}
      WHERE
          i.community_id = #{communityId,jdbcType=INTEGER}
          <if test="payType == 0">
            AND i.bill_status = 0
          </if>
          <if test="payType == 1">
            AND (i.bill_status = 1 or i.bill_status = 2)
          </if>
          and i.status=1
          AND i.building_owner_info_id = #{buildingOwnerInfoId,jdbcType=INTEGER}
  </select>

  <select id="getBillInvoiceDetail" resultType="map">
    SELECT
        i.id,
        i.bill_invoice_explain AS billInvoiceExplain,
        i.pay_price AS payPrice,
        i.ta_user_id AS taUserId,
        i.bill_status AS billStatus,
        b.bill_name AS billName,
        b.bill_explain AS billExplain,
        b.end_date AS endDate,
        s.id AS billStatementId,
        s.pay_name AS payName,
        s.pay_remark AS payRemark,
        s.pay_type AS payType,
        s.create_time AS createTime,
        i.out_trade_no AS outTradeNo
    FROM
        tp_bill_invoice i
        LEFT JOIN tp_bill b ON b.id = i.bill_id
        AND b.community_id = #{communityId,jdbcType=INTEGER}
        LEFT JOIN tp_bill_statement s ON i.bill_statement_id = s.id
        AND s.community_id = #{communityId,jdbcType=INTEGER}
    WHERE
        i.id = #{billInvoiceId,jdbcType=INTEGER}
    AND i.community_id = #{communityId,jdbcType=INTEGER}
  </select>

  <select id="selectByIdAndBuildingOwnerInfoId" resultType="com.community.huiju.model.TpBillInvoice" >
    SELECT
        t.id,
        t.community_id AS communityId,
        t.bill_id AS billId,
        t.building_owner_info_id AS buildingOwnerInfoId,
        t.ta_user_id AS taUserId,
        t.bill_invoice_explain AS billInvoiceExplain,
        t.pay_price AS payPrice,
        t.bill_statement_id AS billStatementId,
        t.bill_status AS billStatus,
        t.pay_name AS payName,
        t.out_trade_no AS outTradeNo,
        b.bill_name as billName
    FROM
	    tp_bill_invoice t
	LEFT JOIN tp_bill b ON t.bill_id = b.id and b.community_id = #{communityId,jdbcType=INTEGER}
    where t.id = #{id,jdbcType=INTEGER}
    and t.community_id = #{communityId,jdbcType=INTEGER}
    and t.status = 1
    and t.building_owner_info_id = #{buildingOwnerInfoId,jdbcType=INTEGER}
  </select>

  <select id="selectByOutTradeNo" resultType="com.community.huiju.model.TpBillInvoice" >
  SELECT
        t.id,
        t.community_id AS communityId,
        t.bill_id AS billId,
        t.building_owner_info_id AS buildingOwnerInfoId,
        t.ta_user_id AS taUserId,
        t.bill_invoice_explain AS billInvoiceExplain,
        t.pay_price AS payPrice,
        t.bill_statement_id AS billStatementId,
        t.bill_status AS billStatus,
        t.pay_name AS payName,
        t.out_trade_no AS outTradeNo,
        b.bill_name as billName
    FROM
	    tp_bill_invoice t
	LEFT JOIN tp_bill b ON t.bill_id = b.id
	where t.out_trade_no = #{outTradeNo,jdbcType=VARCHAR}
	and t.status = 1
  </select>

    <select id="selectByBuildingOwnerInfoId" parameterType="integer" resultMap="BaseResultMap" >
        select
        <include refid="Base_Column_List"/>
        from tp_bill_invoice
        where bill_statement_id = #{buildingOwnerInfoId,jdbcType=INTEGER}
    </select>

    <update id="updateBeach" parameterType="map" >
        update tp_bill_invoice
        set ta_user_id = #{map.taUserId}
        where id in
      <foreach collection="map.list" item="item" index="index" separator="," open="(" close=")">
          #{item.id}
      </foreach>
    </update>

</mapper>