<?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.com.ty.lift.ud.payment.mapper.UserBillMapper" >
  <resultMap id="BaseResultMap" type="cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntity" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="amount" property="amount" jdbcType="DECIMAL" />
    <result column="balance" property="balance" jdbcType="DECIMAL" />
    <result column="type" property="type" jdbcType="INTEGER" />
    <result column="flow_num" property="flowNum" jdbcType="VARCHAR" />
    <result column="descr" property="descr" jdbcType="VARCHAR" />
    <result column="create_by" property="createBy" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_by" property="updateBy" jdbcType="VARCHAR" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 23 14:55:35 CST 2020.
    -->
    id, user_id, amount, balance, type, flow_num, descr, create_by, create_time, update_by, 
    update_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 23 14:55:35 CST 2020.
    -->
    select 
    <include refid="Base_Column_List" />
    from user_bill
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 23 14:55:35 CST 2020.
    -->
    delete from user_bill
    where id = #{id,jdbcType=INTEGER}
  </delete>
<!--   <insert id="insert" parameterType="UserBillEntity" > -->
<!--     insert into user_bill (id, user_id, amount,  -->
<!--       balance, type, flow_num,  -->
<!--       descr, create_by, create_time,  -->
<!--       update_by, update_time) -->
<!--     values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{amount,jdbcType=DECIMAL},  -->
<!--       #{balance,jdbcType=DECIMAL}, #{type,jdbcType=INTEGER}, #{flowNum,jdbcType=VARCHAR},  -->
<!--       #{descr,jdbcType=VARCHAR}, #{createBy,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},  -->
<!--       #{updateBy,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP}) -->
<!--   </insert> -->
  <insert id="insertSelective" parameterType="cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntity" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 23 14:55:35 CST 2020.
    -->
    insert into user_bill
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="amount != null" >
        amount,
      </if>
      <if test="balance != null" >
        balance,
      </if>
      <if test="type != null" >
        type,
      </if>
      <if test="flowNum != null" >
        flow_num,
      </if>
      <if test="descr != null" >
        descr,
      </if>
      <if test="createBy != null" >
        create_by,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updateBy != null" >
        update_by,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="amount != null" >
        #{amount,jdbcType=DECIMAL},
      </if>
      <if test="balance != null" >
        #{balance,jdbcType=DECIMAL},
      </if>
      <if test="type != null" >
        #{type,jdbcType=INTEGER},
      </if>
      <if test="flowNum != null" >
        #{flowNum,jdbcType=VARCHAR},
      </if>
      <if test="descr != null" >
        #{descr,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null" >
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null" >
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntity" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 23 14:55:35 CST 2020.
    -->
    update user_bill
    <set >
      <if test="userId != null" >
        user_id = #{userId,jdbcType=INTEGER},
      </if>
      <if test="amount != null" >
        amount = #{amount,jdbcType=DECIMAL},
      </if>
      <if test="balance != null" >
        balance = #{balance,jdbcType=DECIMAL},
      </if>
      <if test="type != null" >
        type = #{type,jdbcType=INTEGER},
      </if>
      <if test="flowNum != null" >
        flow_num = #{flowNum,jdbcType=VARCHAR},
      </if>
      <if test="descr != null" >
        descr = #{descr,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null" >
        create_by = #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null" >
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntity" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 23 14:55:35 CST 2020.
    -->
    update user_bill
    set user_id = #{userId,jdbcType=INTEGER},
      amount = #{amount,jdbcType=DECIMAL},
      balance = #{balance,jdbcType=DECIMAL},
      type = #{type,jdbcType=INTEGER},
      flow_num = #{flowNum,jdbcType=VARCHAR},
      descr = #{descr,jdbcType=VARCHAR},
      create_by = #{createBy,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_by = #{updateBy,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
  
  <select id="selectByQuery" resultType="cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntity" >
    select t.*,users.name as userName,users.type as rote,users.avatar_url as avatarUrl,users.mobile  from user_bill as t left join user_info as users on t.user_id = users.user_id
    <where>
        ${ew.sqlSegment}
    </where>
     limit #{page.pageIndex},#{page.pageSize}
  </select>
  <select id="countByQuery" resultType="java.lang.Integer">
    select count(0)  from user_bill as t left join user_info as users on t.user_id = users.user_id
    <where>
        ${ew.sqlSegment}
    </where>
  </select>
  
    <select id="sumByType" resultType="java.lang.Double">
    select IFNULL(sum(amount),0)  from user_bill t where type = #{type} and ${ew.sqlSegment}
  </select>

  <select id="getDetail" resultType="cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntityVo">
    select
      bill.id id,
      bill.user_id userId,
      bill.amount amount,
      bill.balance balance,
      bill.type type,
      bill.flow_num flowNum,
      bill.create_time createTime,
      bill.descr descr,
      CONCAT("支付给",info.name) targetDecr
    from user_bill bill
    left join user_info info on info.user_id = bill.target_user_id
    where id = #{id}
  </select>
</mapper>