<?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.UserChannelMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.UserChannel">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 07 15:11:33 CST 2021.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="uid" jdbcType="VARCHAR" property="uid" />
    <result column="aid" jdbcType="VARCHAR" property="aid" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="distribution_time" jdbcType="TIMESTAMP" property="distributionTime" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="recovery" jdbcType="INTEGER" property="recovery" />
    <result column="maid" jdbcType="VARCHAR" property="maid" />
    <result column="sign_time" jdbcType="TIMESTAMP" property="signTime" />
    <result column="province" jdbcType="VARCHAR" property="province" />
    <result column="city" jdbcType="VARCHAR" property="city" />
    <result column="society_tag" jdbcType="VARCHAR" property="societyTag" />
    <result column="contacts" jdbcType="VARCHAR" property="contacts" />
    <result column="contact_mobile" jdbcType="VARCHAR" property="contactMobile" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 07 15:11:33 CST 2021.
    -->
    id, `uid`, aid, `status`, distribution_time, create_time, remarks, `recovery`, maid, 
    sign_time, province, city, society_tag, contacts, contact_mobile
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 07 15:11:33 CST 2021.
    -->
    select 
    <include refid="Base_Column_List" />
    from user_channel
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 07 15:11:33 CST 2021.
    -->
    delete from user_channel
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.UserChannel">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 07 15:11:33 CST 2021.
    -->
    insert into user_channel (id, `uid`, aid, 
      `status`, distribution_time, create_time, 
      remarks, `recovery`, maid, 
      sign_time, province, city, 
      society_tag, contacts, contact_mobile
      )
    values (#{id,jdbcType=INTEGER}, #{uid,jdbcType=VARCHAR}, #{aid,jdbcType=VARCHAR}, 
      #{status,jdbcType=INTEGER}, #{distributionTime,jdbcType=TIMESTAMP}, #{createTime,jdbcType=TIMESTAMP}, 
      #{remarks,jdbcType=VARCHAR}, #{recovery,jdbcType=INTEGER}, #{maid,jdbcType=VARCHAR}, 
      #{signTime,jdbcType=TIMESTAMP}, #{province,jdbcType=VARCHAR}, #{city,jdbcType=VARCHAR}, 
      #{societyTag,jdbcType=VARCHAR}, #{contacts,jdbcType=VARCHAR}, #{contactMobile,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.UserChannel">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 07 15:11:33 CST 2021.
    -->
    insert into user_channel
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="uid != null">
        `uid`,
      </if>
      <if test="aid != null">
        aid,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="distributionTime != null">
        distribution_time,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="remarks != null">
        remarks,
      </if>
      <if test="recovery != null">
        `recovery`,
      </if>
      <if test="maid != null">
        maid,
      </if>
      <if test="signTime != null">
        sign_time,
      </if>
      <if test="province != null">
        province,
      </if>
      <if test="city != null">
        city,
      </if>
      <if test="societyTag != null">
        society_tag,
      </if>
      <if test="contacts != null">
        contacts,
      </if>
      <if test="contactMobile != null">
        contact_mobile,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="uid != null">
        #{uid,jdbcType=VARCHAR},
      </if>
      <if test="aid != null">
        #{aid,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="distributionTime != null">
        #{distributionTime,jdbcType=TIMESTAMP},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="recovery != null">
        #{recovery,jdbcType=INTEGER},
      </if>
      <if test="maid != null">
        #{maid,jdbcType=VARCHAR},
      </if>
      <if test="signTime != null">
        #{signTime,jdbcType=TIMESTAMP},
      </if>
      <if test="province != null">
        #{province,jdbcType=VARCHAR},
      </if>
      <if test="city != null">
        #{city,jdbcType=VARCHAR},
      </if>
      <if test="societyTag != null">
        #{societyTag,jdbcType=VARCHAR},
      </if>
      <if test="contacts != null">
        #{contacts,jdbcType=VARCHAR},
      </if>
      <if test="contactMobile != null">
        #{contactMobile,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.UserChannel">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 07 15:11:33 CST 2021.
    -->
    update user_channel
    <set>
      <if test="uid != null">
        `uid` = #{uid,jdbcType=VARCHAR},
      </if>
      <if test="aid != null">
        aid = #{aid,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=INTEGER},
      </if>
      <if test="distributionTime != null">
        distribution_time = #{distributionTime,jdbcType=TIMESTAMP},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="recovery != null">
        `recovery` = #{recovery,jdbcType=INTEGER},
      </if>
      <if test="maid != null">
        maid = #{maid,jdbcType=VARCHAR},
      </if>
      <if test="signTime != null">
        sign_time = #{signTime,jdbcType=TIMESTAMP},
      </if>
      <if test="province != null">
        province = #{province,jdbcType=VARCHAR},
      </if>
      <if test="city != null">
        city = #{city,jdbcType=VARCHAR},
      </if>
      <if test="societyTag != null">
        society_tag = #{societyTag,jdbcType=VARCHAR},
      </if>
      <if test="contacts != null">
        contacts = #{contacts,jdbcType=VARCHAR},
      </if>
      <if test="contactMobile != null">
        contact_mobile = #{contactMobile,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.UserChannel">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri May 07 15:11:33 CST 2021.
    -->
    update user_channel
    set `uid` = #{uid,jdbcType=VARCHAR},
      aid = #{aid,jdbcType=VARCHAR},
      `status` = #{status,jdbcType=INTEGER},
      distribution_time = #{distributionTime,jdbcType=TIMESTAMP},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      remarks = #{remarks,jdbcType=VARCHAR},
      `recovery` = #{recovery,jdbcType=INTEGER},
      maid = #{maid,jdbcType=VARCHAR},
      sign_time = #{signTime,jdbcType=TIMESTAMP},
      province = #{province,jdbcType=VARCHAR},
      city = #{city,jdbcType=VARCHAR},
      society_tag = #{societyTag,jdbcType=VARCHAR},
      contacts = #{contacts,jdbcType=VARCHAR},
      contact_mobile = #{contactMobile,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
   <update id="updateByUid" parameterType="com.goafanti.common.model.UserChannel">
    
    update user_channel
    <set>
      <if test="aid != null">
        aid = #{aid,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=INTEGER},
      </if>
      <if test="distributionTime != null">
        distribution_time = #{distributionTime,jdbcType=TIMESTAMP},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="recovery != null">
        `recovery` = #{recovery,jdbcType=INTEGER},
      </if>
      <if test="signTime != null ">
       sign_time = #{signTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where uid = #{uid,jdbcType=VARCHAR} and status in (1,2)
  </update>
  
    <insert id="insertBatch" parameterType="com.goafanti.user.bo.InputUserChannel">
       insert into user_channel ( `uid`, aid, 
      `status`,remarks,province,city,society_tag,contacts,contact_mobile)VALUES
      <foreach collection="list" index="index" item="item" separator=",">
      (#{item.uid,jdbcType=VARCHAR}, #{item.aid,jdbcType=VARCHAR},#{item.status},
      #{item.remarks},#{item.provinces},#{item.citys},#{item.societyTags},
      #{item.contacts},#{item.contactMobile})
    </foreach>
    </insert>
    
    <select id="selectByAidAndstatusList" resultType="com.goafanti.user.bo.OutUserChannel">
 	   select a.id,a.uid,b.nickname userName,a.aid,a.status,date_format(a.distribution_time,'%Y-%m-%d %H:%i:%S')distributionTimes,
 	date_format(a.create_time,'%Y-%m-%d %H:%i:%S')createTimes ,a.contacts ,a.contact_mobile contactMobile,a.remarks,
 	a.society_tag societyTag ,a.province,a.city from user_channel a
    left join user b on a.uid=b.id  
    where 1=1
    and a.status in (3,4,5,9,10,11,12,13)
    <if test="aid !=null">
    and a.aid= #{aid} 
    </if>
    order by  a.create_time desc
    <if test="page_sql != null">
    	${page_sql}
    </if>
    </select>
    
    <select id="selectByAidAndstatusCount" resultType="java.lang.Integer">
 	   select count(*) from user_channel a
    left join user b on a.uid=b.id  left join organization_identity c on a.uid =c.uid 
    left join district_glossory d1 on c.location_province =d1.id 
    left join district_glossory d2 on c.location_city =d2.id
    where 1=1 and a.status in (3,4,5,9,10,11,12,13)
    <if test="aid !=null">
    and a.aid= #{aid} 
    </if>
    </select>
    <update id="updateByuids">
     update user_channel set
     status =  #{status}
     <if test="type == 2">
     ,recovery=1
     </if>  
     <if test="date !=null">
     ,distribution_time= #{date}
     </if>
     <if test="maid !=null">
     ,maid= #{maid}
     </if>
	where 1=1
	<if test="type == 2">
    and status in (0,1,2) 
     </if>
     	<if test="type != 2">
    and status in (0,1,2,6) 
     </if>
	
	and  uid in( 
      <foreach collection="list" index="index" item="item" separator=",">
      #{item}
    </foreach>
      )
    </update>
    <delete id="deleteByIds">
    delete from user_channel where id in (
      <foreach collection="list" index="index" item="item" separator=",">
      #{item}
    </foreach>
    )
    </delete>
   <delete id="deleteUser">
        delete user_channel ,user , organization_identity ,organization_contact_book 
	    from user_channel ,user , organization_identity ,organization_contact_book
	    where user_channel.id in (
	     <foreach collection="list" index="index" item="item" separator=",">
      #{item}
    </foreach>
    ) 
	    and user_channel.uid =user.id and user.status =1
	    and user.id=organization_identity.uid and user.id=organization_contact_book.uid 
     
    </delete>
    
    <select id="checkRecovery" resultType="java.lang.String">
    select recovery from user_channel where uid in (
      <foreach collection="list" index="index" item="item" separator=",">
      #{item}
    </foreach>
    )
    </select>
</mapper>