<?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.UserBusinessMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.UserBusiness">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 05 10:50:19 CST 2018.
    -->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="uid" jdbcType="VARCHAR" property="uid" />
    <result column="aid" jdbcType="VARCHAR" property="aid" />
    <result column="follow_situation" jdbcType="INTEGER" property="followSituation" />
    <result column="customer_status" jdbcType="INTEGER" property="customerStatus" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="business_project_id" jdbcType="VARCHAR" property="businessProjectId" />
  </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 Sat May 05 10:50:19 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 Sat May 05 10:50:19 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 Sat May 05 10:50:19 CST 2018.
    -->
    id, uid, aid, follow_situation, customer_status, create_time, update_time, remarks, 
    business_project_id
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.UserBusinessExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 05 10:50:19 CST 2018.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from user_business
    <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 Sat May 05 10:50:19 CST 2018.
    -->
    select 
    <include refid="Base_Column_List" />
    from user_business
    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 Sat May 05 10:50:19 CST 2018.
    -->
    delete from user_business
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.UserBusinessExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 05 10:50:19 CST 2018.
    -->
    delete from user_business
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.UserBusiness">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 05 10:50:19 CST 2018.
    -->
    insert into user_business (id, uid, aid, 
      follow_situation, customer_status, create_time, 
      update_time, remarks, business_project_id
      )
    values (#{id,jdbcType=VARCHAR}, #{uid,jdbcType=VARCHAR}, #{aid,jdbcType=VARCHAR}, 
      #{followSituation,jdbcType=INTEGER}, #{customerStatus,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, 
      #{updateTime,jdbcType=TIMESTAMP}, #{remarks,jdbcType=VARCHAR}, #{businessProjectId,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.UserBusiness">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 05 10:50:19 CST 2018.
    -->
    insert into user_business
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="uid != null">
        uid,
      </if>
      <if test="aid != null">
        aid,
      </if>
      <if test="followSituation != null">
        follow_situation,
      </if>
      <if test="customerStatus != null">
        customer_status,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="remarks != null">
        remarks,
      </if>
      <if test="businessProjectId != null">
        business_project_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="uid != null">
        #{uid,jdbcType=VARCHAR},
      </if>
      <if test="aid != null">
        #{aid,jdbcType=VARCHAR},
      </if>
      <if test="followSituation != null">
        #{followSituation,jdbcType=INTEGER},
      </if>
      <if test="customerStatus != null">
        #{customerStatus,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="businessProjectId != null">
        #{businessProjectId,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.goafanti.common.model.UserBusinessExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 05 10:50:19 CST 2018.
    -->
    select count(*) from user_business
    <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 Sat May 05 10:50:19 CST 2018.
    -->
    update user_business
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </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.followSituation != null">
        follow_situation = #{record.followSituation,jdbcType=INTEGER},
      </if>
      <if test="record.customerStatus != null">
        customer_status = #{record.customerStatus,jdbcType=INTEGER},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.remarks != null">
        remarks = #{record.remarks,jdbcType=VARCHAR},
      </if>
      <if test="record.businessProjectId != null">
        business_project_id = #{record.businessProjectId,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 Sat May 05 10:50:19 CST 2018.
    -->
    update user_business
    set id = #{record.id,jdbcType=VARCHAR},
      uid = #{record.uid,jdbcType=VARCHAR},
      aid = #{record.aid,jdbcType=VARCHAR},
      follow_situation = #{record.followSituation,jdbcType=INTEGER},
      customer_status = #{record.customerStatus,jdbcType=INTEGER},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      remarks = #{record.remarks,jdbcType=VARCHAR},
      business_project_id = #{record.businessProjectId,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.UserBusiness">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 05 10:50:19 CST 2018.
    -->
    update user_business
    <set>
      <if test="uid != null">
        uid = #{uid,jdbcType=VARCHAR},
      </if>
      <if test="aid != null">
        aid = #{aid,jdbcType=VARCHAR},
      </if>
      <if test="followSituation != null">
        follow_situation = #{followSituation,jdbcType=INTEGER},
      </if>
      <if test="customerStatus != null">
        customer_status = #{customerStatus,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="businessProjectId != null">
        business_project_id = #{businessProjectId,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.UserBusiness">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sat May 05 10:50:19 CST 2018.
    -->
    update user_business
    set uid = #{uid,jdbcType=VARCHAR},
      aid = #{aid,jdbcType=VARCHAR},
      follow_situation = #{followSituation,jdbcType=INTEGER},
      customer_status = #{customerStatus,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      remarks = #{remarks,jdbcType=VARCHAR},
      business_project_id = #{businessProjectId,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  <select id="selectBusinessGlossory" resultType="com.goafanti.customer.bo.BusinessListBo">
  	select id as businessGlossoryId,name as businessName from business_glossory
  </select>
  
  <select id="judgeBusiness" resultType="java.lang.Integer">
  	select count(0) from user_business where
  	 uid = #{uid,jdbcType=VARCHAR} and customer_status  != 6 
  	 <if test="businessProjectId != null and businessProjectId != ''">
  	  	and business_project_id = #{businessProjectId,jdbcType=VARCHAR}
  	 </if>
  	 <if test="aid != null and aid != ''">
  	 	and aid = #{aid,jdbcType=VARCHAR}
  	 </if>
  </select>
 
  <select id="selectBusinessByUAid" parameterType="java.lang.String" resultType="com.goafanti.customer.bo.BusinessListBo">
	select 
		a.id as businessId,
		a.uid,
		a.business_project_id as businessProjectId,
		a.follow_situation as followSituation,
		a.customer_status as customerStatus,
		a.remarks,
		b.bname as businessProjectName,
		c.cname as businessvarietiesName
	from user_business a
	left join business_project b on a.business_project_id = b.id
	left join business_category c on b.cid = c.id
	where  a.uid = #{uid,jdbcType=VARCHAR} and a.aid = #{aid,jdbcType=VARCHAR} and  a.customer_status != 6
	order by a.create_time
  </select>
  
  <select id="selectLockedBusinessByUAid" parameterType="java.lang.String" resultType="com.goafanti.customer.bo.BusinessListBo">
	select 
		x.id as businessId,
		x.uid,
		5 as followSituation,
		x.customer_status as customerStatus,
		y.business_project_id as businessProjectId,
		y.bname as businessProjectName,
		y.cname as businessvarietiesName
	from(
		select 
			id,
			uid,
			business_project_id,
			follow_situation,
			customer_status,
			remarks
		from user_business
		where uid = #{uid} and aid = #{aid} and customer_status != 6
	)x right join(
		select 
			a.business_project_id,
			b.bname,
			c.cname
		from user_lock_release a
		left join business_project b on a.business_project_id = b.id
		left join business_category c on b.cid = c.id
		where a.uid = #{uid} and a.aid = #{aid} and a.type = 1 and a.status = 0	
	)y on x.business_project_id = y.business_project_id
  </select>
  
  <select id="selectFollowCountByUAid" parameterType="java.lang.String" resultType="java.lang.Integer">
  	select count(0) from user_follow where uid = #{uid,jdbcType=VARCHAR} and aid = #{aid,jdbcType=VARCHAR}
  </select>
  
  
  <select id="findBusinessDetail" parameterType="java.lang.String" resultType="com.goafanti.customer.bo.BussinessFollowBo">
	select
		a.id as businessId,
		a.uid,
		a.business_project_id as businessProjectId,
		a.create_time as createTime,
		a.follow_situation as followSituation,
		a.customer_status as customerStatus,
		a.remarks,
		b.identify_name as identifyName,
		c.name as adminName,
		d.bname as businessProjectName
	from
		user_business a left join user b on
		a.uid = b.id left join admin c on
		a.aid = c.id left join business_project d on
		a.business_project_id = d.id
	where a.id = #{businessId,jdbcType=VARCHAR}
  </select>
  
  <select id="findFollowOneBusiness" parameterType="java.lang.String" resultType="com.goafanti.customer.bo.BussinessFollowBo">
	select
		a.customer_status as customerStatus,
		a.follow_situation as followSituation,
		a.remarks,
		a.business_project_id as businessProjectId,
		b.id as followId,
		b.create_time as createTime,
		b.result,
		b.contact_type as contactType,
		d.name as contacts,
		d.mobile as contactMobile,
		b.create_time as followTime,
		e.name as adminName,
		f.identify_name as identifyName,
		g.bname as businessProjectName
	from
		user_follow_business a 
	left join user_follow b on a.follow_id = b.id
	left join user_business c on a.business_id = c.id
	left join organization_contact_book d on b.ocb_id = d.id
	left join admin e on b.aid = e.id
	left join user f on b.uid = f.id
	left join business_project g on a.business_project_id = g.id
	where a.id = #{ufbId,jdbcType=VARCHAR}
  </select>
  <update id="updateDimissionTransfer" parameterType="java.lang.String">
  update  user_business 
	set aid= #{transferId,jdbcType=VARCHAR}
	where aid= #{aid,jdbcType=VARCHAR}
  </update>
  <select id="selectBusinessProjectByUAPid" resultType="com.goafanti.customer.bo.BusinessListBo">
  select 
		a.id as businessId,
		a.uid,
		a.business_project_id as businessProjectId,
		a.follow_situation as followSituation,
		a.customer_status as customerStatus,
		a.remarks,
		b.bname as businessProjectName,
		c.cname as businessvarietiesName
	from user_business a
	left join business_project b on a.business_project_id = b.id
	left join business_category c on b.cid = c.id
	where  a.uid = #{uid,jdbcType=VARCHAR} and a.aid = #{aid,jdbcType=VARCHAR} 
	and a.business_project_id= #{pid} and  a.customer_status != 6
  </select>
  <select id="checkLock" resultType="java.lang.Integer">
	select count(*) from user_business 
	where aid= #{aid} and uid= #{uid} and business_project_id = #{pid}
	
  </select>
</mapper>