<?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.UserLockReleaseMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.UserLockRelease">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 07 09:56:32 CST 2018.
    -->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="uid" jdbcType="VARCHAR" property="uid" />
    <result column="aid" jdbcType="VARCHAR" property="aid" />
    <result column="business_project_id" jdbcType="VARCHAR" property="businessProjectId" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="lock_time" jdbcType="DATE" property="lockTime" />
    <result column="release_time" jdbcType="DATE" property="releaseTime" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
  </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 Mon May 07 09:56:32 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 Mon May 07 09:56:32 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 Mon May 07 09:56:32 CST 2018.
    -->
    id, type, uid, aid, business_project_id, status, lock_time, release_time, order_no
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.UserLockReleaseExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 07 09:56:32 CST 2018.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from user_lock_release
    <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 Mon May 07 09:56:32 CST 2018.
    -->
    select
    <include refid="Base_Column_List" />
    from user_lock_release
    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 Mon May 07 09:56:32 CST 2018.
    -->
    delete from user_lock_release
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.UserLockReleaseExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 07 09:56:32 CST 2018.
    -->
    delete from user_lock_release
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.UserLockRelease">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 07 09:56:32 CST 2018.
    -->
    insert into user_lock_release (id, type, uid,
      aid, business_project_id, status,
      lock_time, release_time, order_no
      )
    values (#{id,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, #{uid,jdbcType=VARCHAR},
      #{aid,jdbcType=VARCHAR}, #{businessProjectId,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
      #{lockTime,jdbcType=DATE}, #{releaseTime,jdbcType=DATE}, #{orderNo,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.UserLockRelease">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 07 09:56:32 CST 2018.
    -->
    insert into user_lock_release
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="type != null">
        type,
      </if>
      <if test="uid != null">
        uid,
      </if>
      <if test="aid != null">
        aid,
      </if>
      <if test="businessProjectId != null">
        business_project_id,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="lockTime != null">
        lock_time,
      </if>
      <if test="releaseTime != null">
        release_time,
      </if>
      <if test="orderNo != null">
        order_no,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="uid != null">
        #{uid,jdbcType=VARCHAR},
      </if>
      <if test="aid != null">
        #{aid,jdbcType=VARCHAR},
      </if>
      <if test="businessProjectId != null">
        #{businessProjectId,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="lockTime != null">
        #{lockTime,jdbcType=DATE},
      </if>
      <if test="releaseTime != null">
        #{releaseTime,jdbcType=DATE},
      </if>
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.goafanti.common.model.UserLockReleaseExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 07 09:56:32 CST 2018.
    -->
    select count(*) from user_lock_release
    <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 Mon May 07 09:56:32 CST 2018.
    -->
    update user_lock_release
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.type != null">
        type = #{record.type,jdbcType=INTEGER},
      </if>
      <if test="record.uid != null">
        uid = #{record.uid,jdbcType=VARCHAR},
      </if>
      <if test="record.aid != null">
        aid = #{record.aid,jdbcType=VARCHAR},
      </if>
      <if test="record.businessProjectId != null">
        business_project_id = #{record.businessProjectId,jdbcType=VARCHAR},
      </if>
      <if test="record.status != null">
        status = #{record.status,jdbcType=INTEGER},
      </if>
      <if test="record.lockTime != null">
        lock_time = #{record.lockTime,jdbcType=DATE},
      </if>
      <if test="record.releaseTime != null">
        release_time = #{record.releaseTime,jdbcType=DATE},
      </if>
      <if test="record.orderNo != null">
        order_no = #{record.orderNo,jdbcType=VARCHAR},
      </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 Mon May 07 09:56:32 CST 2018.
    -->
    update user_lock_release
    set id = #{record.id,jdbcType=VARCHAR},
      type = #{record.type,jdbcType=INTEGER},
      uid = #{record.uid,jdbcType=VARCHAR},
      aid = #{record.aid,jdbcType=VARCHAR},
      business_project_id = #{record.businessProjectId,jdbcType=VARCHAR},
      status = #{record.status,jdbcType=INTEGER},
      lock_time = #{record.lockTime,jdbcType=DATE},
      release_time = #{record.releaseTime,jdbcType=DATE},
      order_no = #{record.orderNo,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.UserLockRelease">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 07 09:56:32 CST 2018.
    -->
    update user_lock_release
    <set>
      <if test="type != null">
        type = #{type,jdbcType=INTEGER},
      </if>
      <if test="uid != null">
        uid = #{uid,jdbcType=VARCHAR},
      </if>
      <if test="aid != null">
        aid = #{aid,jdbcType=VARCHAR},
      </if>
      <if test="businessProjectId != null">
        business_project_id = #{businessProjectId,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="lockTime != null">
        lock_time = #{lockTime,jdbcType=DATE},
      </if>
      <if test="releaseTime != null">
        release_time = #{releaseTime,jdbcType=DATE},
      </if>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.UserLockRelease">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon May 07 09:56:32 CST 2018.
    -->
    update user_lock_release
    set type = #{type,jdbcType=INTEGER},
      uid = #{uid,jdbcType=VARCHAR},
      aid = #{aid,jdbcType=VARCHAR},
      business_project_id = #{businessProjectId,jdbcType=VARCHAR},
      status = #{status,jdbcType=INTEGER},
      lock_time = #{lockTime,jdbcType=DATE},
      release_time = #{releaseTime,jdbcType=DATE},
      order_no = #{orderNo,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <delete id="deleteLockByOrder">
  	delete from user_lock_release where order_no = #{orderNo,jdbcType=VARCHAR} and business_project_id = #{businessProjectId,jdbcType=VARCHAR}
  </delete>
  <update id="updateUserLock">
  	update user_lock_release set status = #{newStatus} where uid = #{uid} and aid = #{aid} and type = 0 and status = #{oldStatus}
  </update>

  <update id="updateUsersLock">
  	update user_lock_release set status = #{newStatus}
  	where aid = #{aid} and type = 0 and status = #{oldStatus}
  	and uid in
  	<foreach item="item" collection="uList" separator="," open="(" close=")" index="">
       #{item}
     </foreach>
  </update>

  <update id="updateUserTypeLock">
  	update user_lock_release set status = #{newStatus} , release_time= #{date}
  	where uid = #{uid} and aid = #{aid} and type = 0 and type = #{type}
  </update>

  <update id="updateUserTypeLockList">
  	update user_lock_release set status = #{newStatus} , release_time= #{date}
  	where aid = #{aid} and type = 0 and type = #{type} and uid in
	<foreach item="item" collection="list" separator="," open="(" close=")" index="">
       #{item}
    </foreach>
  </update>

  <select id="selectWaitReleaseCustomer" resultType="com.goafanti.customer.bo.LockingReleaseBo">
	select
		t0.id, t0.uid, t0.aid, t0.lock_time lockTime,u.new_channel newChannel,
		t1.last_follow_time lastFollowTime,u.nickname userName ,um.channel_type channelType
	from
		(select id,uid,aid,lock_time from user_lock_release where aid = #{aid} and type = 0 and status = 0)t0
	left join
		(select uid,aid,max(create_time) as last_follow_time from user_follow where aid = #{aid} group by uid)t1
	on t0.uid = t1.uid left join user u on t0.uid=u.id  left join user_mid um on t0.uid=um.uid
	where u.channel=0
	 and datediff(now(),if(t0.lock_time &lt; t1.last_follow_time,t1.last_follow_time,t0.lock_time))>30
  </select>

  <select id="selectWaitReleaseBusiness"  resultType="com.goafanti.customer.bo.LockingReleaseBo">
	select
		t0.id,
		t0.uid,
		t0.aid,
		t0.lock_time,
		t1.last_follow_time
	from
		(select id,uid,aid,lock_time from user_lock_release t0 where aid = #{aid} and type = 1 and status = 0)t0
	left join
		(select uid,aid,max(create_time) as last_follow_time from user_follow where aid = #{aid} group by uid)t1
	on t0.uid = t1.uid
	where datediff(t1.last_follow_time,lock_time)>360 or datediff(now(),t0.lock_time)>900
  </select>


  <update id="updatePendingReleaseLock" parameterType="java.lang.String">
  	update user_lock_release set status = 1 where id in (${lockIds})
  </update>

  <select id="selectPendinglockUserList" resultType="com.goafanti.customer.bo.LockingReleaseBo">
  	select id,uid,type  from user_lock_release where status=1
  </select>

  <update id="updateProject" parameterType="java.lang.String">
  	update user_lock_release set status = 2,release_time=now()
  	where uid= #{uid,jdbcType=VARCHAR}
	and aid= #{aid,jdbcType=VARCHAR}
	and business_project_id= #{businessProjectId,jdbcType=VARCHAR}
  </update>
  <select id="checkTask" resultType="com.goafanti.customer.bo.LockingReleaseBo">
  	select
		 id, type, uid, aid, business_project_id as  businessProjectId, status
	 from user_lock_release where type =1
	and status=0
	and uid=#{buyerId,jdbcType=VARCHAR}
	and business_project_id=#{commodityId,jdbcType=VARCHAR}
  </select>
  <update id="updatePrivateBusinessTransfer">
 	update user_lock_release set aid=#{inputId} where uid= #{uid} and business_project_id= #{pid} and aid= #{aid};
  </update>

  <select id="checkUserProject" resultType="com.goafanti.customer.bo.LockingReleaseBo">
  	select
		 id, type, uid, aid, business_project_id as  businessProjectId, status
	 from user_lock_release where type =1
	and status=0
	and uid=#{uid,jdbcType=VARCHAR}
	and aid=#{aid,jdbcType=VARCHAR}
	and business_project_id=#{pid,jdbcType=VARCHAR}
  </select>

  <select id="selectUserDays" resultType="com.goafanti.common.bo.userDaysBo">
    select  t0.uid, t0.aid, u.nickname name ,u.channel ,0 type,u.new_channel newChannel
	from
		(select id,uid,aid,lock_time from user_lock_release where aid = #{aid} and type = 0 and status = 0)t0
	left join
		(select uid,aid,max(create_time) as last_follow_time from user_follow where aid = #{aid} group by uid)t1
	on t0.uid = t1.uid
	left join user u on t0.uid=u.id
	where u.channel=0 and u.new_channel=0
	and datediff(now(),if(t0.lock_time &lt; t1.last_follow_time,t1.last_follow_time,t0.lock_time)) &gt; 15
	UNION
	  select  t0.uid, t0.aid, u.nickname name ,u.channel ,0 type,u.new_channel newChannel
	from
		(select id,uid,aid,lock_time from user_lock_release where aid = #{aid} and type = 0 and status = 0)t0
	left join
		(select uid,aid,max(create_time) as last_follow_time from user_follow where aid = #{aid} group by uid)t1
	on t0.uid = t1.uid
	left join user u on t0.uid=u.id
	where u.channel=1
	and  datediff(now(),t0.lock_time)> #{y}
  </select>

    <select id="selectChannelUserDays" resultType="com.goafanti.common.bo.userDaysBo">
	select  t0.uid, t0.aid, u.nickname name,c.recovery
	from (select id,uid,aid,lock_time from user_lock_release where type = 0 and status = 0  and aid=#{aid} )t0
	left join user  u on t0.uid=u.id
	left join user_channel c on u.id=c.uid
	where u.channel =1 and c.status &lt; 3
	and datediff(now(),t0.lock_time) &gt; #{days}
  </select>

  <select id="selectChannelNotFollow" resultType="com.goafanti.common.bo.userDaysBo">
  	   select  t0.uid, t0.aid, u.nickname name ,u.channel
	from
		(select id,uid,aid,lock_time from user_lock_release where aid = #{aid} and type = 0 and status = 0)t0
	left join
		(select uid,aid,max(create_time) as last_follow_time from user_follow where aid = #{aid} group by uid)t1
	on t0.uid = t1.uid
	left join user u on t0.uid=u.id
	where 1=1 and u.channel=1
	and datediff(now(),if(t0.lock_time &lt; t1.last_follow_time,t1.last_follow_time,t0.lock_time)) &gt; #{days}
  </select>

  <insert id="insertList" >
      insert into user_lock_release (id, type, uid,
      aid, business_project_id, status,
      lock_time, release_time, order_no
      )
    values
    <foreach item="item" collection="list" separator="," index="">
       	(#{item.id,jdbcType=VARCHAR}, #{item.type,jdbcType=INTEGER}, #{item.uid,jdbcType=VARCHAR},
      #{item.aid,jdbcType=VARCHAR}, #{item.businessProjectId,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER},
      #{item.lockTime,jdbcType=DATE}, #{item.releaseTime,jdbcType=DATE}, #{item.orderNo,jdbcType=VARCHAR})
     </foreach>
  </insert>

  <select id="checkLock" resultType="java.lang.Integer">
   	select count(*) from user_lock_release
	where aid= #{aid} and uid= #{uid} and business_project_id = #{pid}
  </select>

  <update id="updateReleaseList" >
  update user_lock_release
  	set release_time =now(),status =2
  	where id  in
  	<foreach item="item" collection="list" separator="," open="(" close=")" index="">
       #{item}
     </foreach>
  </update>
  <select id="selectByUidGetCount" resultType="java.lang.Integer">
  select count(*) from user_lock_release
  	where `type` =1 and status=0 and aid = #{aid} and uid= #{uid}
  </select>
  <update id="updateReleaseUser">
  update t_order_new a ,user b ,user_business  c
	set b.share_type =1,b.transfer_time =now(),b.sign_bills=0,
	c.customer_status =6,c.follow_situation =8
	where a.order_no = #{orderNo} and a.buyer_id =b.id
	and a.buyer_id =c.uid and a.salesman_id =c.aid and c.follow_situation =5
  </update>

  <update id="updateByAidAndUid">
  	update user_lock_release
	set status =2,release_time =now()
	where aid= #{aid} and uid= #{uid} and `type` =0
  </update>
  <select id="selectByids" resultType="java.lang.String">
  select uid from user_lock_release
  where uid in(
   <foreach item="item" collection="list" separator="," index="">
       	#{item}
     </foreach>
  )
  </select>
  <insert id="insertChannelList">
      INSERT INTO user_lock_release
(id, `type`, uid, aid, status, lock_time)
VALUES
  <foreach item="item" collection="list" separator="," index="">
      (uuid(), 0, #{item}, #{aid},  0, now())
     </foreach>
  </insert>
  <update id="updateChannelList">
  update user_lock_release set status =2 ,release_time =now()
	where status=0 and type=0 and uid in (
	 <foreach item="item" collection="list" separator="," index="">
       	#{item}
     </foreach>
	)
  </update>
</mapper>
