<?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.goafanti.common.dao.JtVoucherDetailMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.JtVoucherDetail">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="voucher_id" jdbcType="VARCHAR" property="voucherId" />
    <result column="use_uid" jdbcType="VARCHAR" property="useUid" />
    <result column="use_time" jdbcType="TIMESTAMP" property="useTime" />
    <result column="receive_time" jdbcType="TIMESTAMP" property="receiveTime" />
    <result column="status" jdbcType="INTEGER" property="status" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    <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">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    <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">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    id, voucher_id, use_uid, use_time, receive_time, status
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.JtVoucherDetailExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from jt_voucher_detail
    <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.String" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    select 
    <include refid="Base_Column_List" />
    from jt_voucher_detail
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    delete from jt_voucher_detail
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.JtVoucherDetailExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    delete from jt_voucher_detail
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.JtVoucherDetail">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    insert into jt_voucher_detail (id, voucher_id, use_uid, 
      use_time, receive_time, status
      )
    values (#{id,jdbcType=VARCHAR}, #{voucherId,jdbcType=VARCHAR}, #{useUid,jdbcType=VARCHAR}, 
      #{useTime,jdbcType=TIMESTAMP}, #{receiveTime,jdbcType=TIMESTAMP}, #{status,jdbcType=INTEGER}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.JtVoucherDetail">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    insert into jt_voucher_detail
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="voucherId != null">
        voucher_id,
      </if>
      <if test="useUid != null">
        use_uid,
      </if>
      <if test="useTime != null">
        use_time,
      </if>
      <if test="receiveTime != null">
        receive_time,
      </if>
      <if test="status != null">
        status,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="voucherId != null">
        #{voucherId,jdbcType=VARCHAR},
      </if>
      <if test="useUid != null">
        #{useUid,jdbcType=VARCHAR},
      </if>
      <if test="useTime != null">
        #{useTime,jdbcType=TIMESTAMP},
      </if>
      <if test="receiveTime != null">
        #{receiveTime,jdbcType=TIMESTAMP},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.goafanti.common.model.JtVoucherDetailExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    select count(*) from jt_voucher_detail
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    update jt_voucher_detail
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.voucherId != null">
        voucher_id = #{record.voucherId,jdbcType=VARCHAR},
      </if>
      <if test="record.useUid != null">
        use_uid = #{record.useUid,jdbcType=VARCHAR},
      </if>
      <if test="record.useTime != null">
        use_time = #{record.useTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.receiveTime != null">
        receive_time = #{record.receiveTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.status != null">
        status = #{record.status,jdbcType=INTEGER},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    update jt_voucher_detail
    set id = #{record.id,jdbcType=VARCHAR},
      voucher_id = #{record.voucherId,jdbcType=VARCHAR},
      use_uid = #{record.useUid,jdbcType=VARCHAR},
      use_time = #{record.useTime,jdbcType=TIMESTAMP},
      receive_time = #{record.receiveTime,jdbcType=TIMESTAMP},
      status = #{record.status,jdbcType=INTEGER}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.JtVoucherDetail">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    update jt_voucher_detail
    <set>
      <if test="voucherId != null">
        voucher_id = #{voucherId,jdbcType=VARCHAR},
      </if>
      <if test="useUid != null">
        use_uid = #{useUid,jdbcType=VARCHAR},
      </if>
      <if test="useTime != null">
        use_time = #{useTime,jdbcType=TIMESTAMP},
      </if>
      <if test="receiveTime != null">
        receive_time = #{receiveTime,jdbcType=TIMESTAMP},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.JtVoucherDetail">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Oct 16 15:59:34 CST 2018.
    -->
    update jt_voucher_detail
    set voucher_id = #{voucherId,jdbcType=VARCHAR},
      use_uid = #{useUid,jdbcType=VARCHAR},
      use_time = #{useTime,jdbcType=TIMESTAMP},
      receive_time = #{receiveTime,jdbcType=TIMESTAMP},
      status = #{status,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <select id="selectVoucherDetailList" resultType="com.goafanti.voucher.bo.JtVoucherDetailListBo">
  	select
		b.id, a.name, a.money, b.receive_time as receiveTime, a.duration_day as durationDay,
		IF(date_add(b.receive_time, interval a.duration_day day) &lt; now(),2,b.status) as status,
		a.source, b.use_time as useTime, c.mobile as userMobile,
		date_format(b.receive_time,'%Y-%m-%d %H:%I:%S')as starTime,
		date_format(date_add(b.receive_time, interval a.duration_day day),'%Y-%m-%d %H:%I:%S')as endTime
		from  jt_voucher_detail b
		left join jt_voucher a on a.id=b.voucher_id
		left join user c on b.use_uid=c.id
		where 1=1
	<if test="uid != null">
		and  b.use_uid  = #{uid,jdbcType=VARCHAR}
	</if>	
	<if test="name != null">
		and  a.name like CONCAT('%',#{name,jdbcType=VARCHAR},'%')
	</if>  
	<if test="status != null">
	  and IF(date_add(b.receive_time, interval a.duration_day day) &lt; now(),2,b.status)  = #{status,jdbcType=INTEGER}
	</if>
	<if test="userMobile != null">
		and  c.mobile = #{userMobile,jdbcType=VARCHAR}
	</if>
	<if test="source != null">
		and  a.source = #{source,jdbcType=INTEGER}
	</if>
	
	<if test="sort == 0">
		order by a.money 
	</if>
	<if test="sort == 1">
		order by b.receive_time
	</if>
	<if test="sort == 2">
		order by endTime
	</if>
	<if test="sortType == 1">
	 desc
	 </if>
  	<if test="page_sql!=null">
		${page_sql}
	</if>
  </select>
  
  <select id="selectVoucherDetailCount" resultType="java.lang.Integer">
  	 select	count(*)  from jt_voucher_detail b
		left join jt_voucher a on a.id=b.voucher_id
		left join user c on b.use_uid=c.id
	where 1=1
	<if test="uid != null">
		and  b.use_uid  = #{uid,jdbcType=VARCHAR}
	</if>	
	<if test="name != null">
		and  a.name like CONCAT('%',#{name,jdbcType=VARCHAR},'%')
	</if>  
	<if test="status != null">
	    and IF(date_add(b.receive_time, interval a.duration_day day) &lt; now(),2,b.status)  = #{status,jdbcType=INTEGER}
	</if>
	<if test="userMobile != null">
		and  c.mobile = #{userMobile,jdbcType=VARCHAR}
	</if>
	<if test="source != null">
		and  a.source = #{source,jdbcType=INTEGER}
	</if>
  </select>
  <select id="getTokenVoucher" resultType="java.lang.String">
  select b.id from jt_voucher_detail a 
    left join jt_voucher b on a.voucher_id = b.id
    where b.source=4 
    and a.use_uid= #{uid,jdbcType=VARCHAR}
  </select>
</mapper>