<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
 <mapper namespace="com.iamberry.wechat.service.mapper.PlaceInfoMapper">
	<!-- palceInfo活动所有字段映射 -->
	<sql id="palceInfoAllField">
		PLACE_ID id,
		PLACE_SUPPER_ID supperId,
		PLACE_NAME  name,
		PLACE_ADDR   addr,
		PLACE_USERNAME  userName,
		PLACE_TEL  tel,
		PLACE_AMOUNT  amount,
		PLACE_CREATE_DATE  createDate,
		PLACE_STATUS  status,
		PLACE_OPENID  openid,
		PLACE_ROLE_ID roleId,
		PLACE_PID  placePid ,
		PLACE_PIDS  placeIds ,
		PLACE_AREA areaName,
		PLACE_INIT_CODE  initCode,
		PLACE_REMARK  remark
	</sql>

	<!-- 商户申请修改信息页面 -->
	<sql id="applyLogsAllField">
		APPLY_ID applyID, APPLY_OPENID applyOpenID, APPLY_TO_PLACEID applyToPlaceID,
		APPLY_NAME applyName, APPLY_USERNAME applyUserName, APPLY_TEL applyTel,
		APPLY_AREA applyArea, APPLY_DATE applyDate, APPLY_STATUS applyStatus, APPLY_ADDR applyAddr
	</sql>

	<select id="getOnePlaceInfo" parameterType="integer" resultType="PlaceInfo">
		SELECT
		<include refid="palceInfoAllField"/>,
		PLACE_PIDS  placeIds ,
		PLACE_HAS_QRCODE hasQrcode,
		PLACE_TEMPLATE_ID templateId,
		(select role_name from tb_iamberry_place_role where role_id=roleId )  as roleName,
		(select template_name from tb_iamberry_template where template_id=templateId) templateName
        FROM tb_iamberry_place_info WHERE place_id = #{placeId} and PLACE_STATUS=1
	</select>
	<select id="getOnePlaceInfo2" parameterType="string" resultType="PlaceInfo">
        SELECT
			<include refid="palceInfoAllField"/>,
		PLACE_PIDS  placeIds ,
		PLACE_HAS_QRCODE hasQrcode,
		PLACE_TEMPLATE_ID templateId,
		PLACE_INFO_STATUS placeInfoStus,
		ROLE_NAME roleName
		FROM TB_IAMBERRY_PLACE_INFO
		JOIN TB_IAMBERRY_PLACE_ROLE   ON ROLE_ID = PLACE_ROLE_ID
		WHERE PLACE_OPENID=#{openid}  AND PLACE_STATUS=1
	</select>
	<select id="getOnePlaceInfoByInitcode"  resultType="PlaceInfo">
		SELECT
		<include refid="palceInfoAllField"/>,
		PLACE_PIDS  placeIds ,
		PLACE_HAS_QRCODE hasQrcode,
		PLACE_TEMPLATE_ID templateId
        FROM tb_iamberry_place_info
        WHERE
        PLACE_STATUS=1
        <if test="initCode!=null and initCode!=''">
         and  PLACE_INIT_CODE=#{initCode}
        </if>
        <if test="id!=null and id!=''">
          and PLACE_ID=${id}
        </if>
        <if test="name!=null and name!=''">
          and PLACE_Name=#{name}
        </if>
	</select>

	<select id="getPlaceInfosForDownLoad"  resultType="PlaceInfo">
	  select
		PLACE_ID id,
		PLACE_NAME  name,
		PLACE_ADDR   addr,
		PLACE_USERNAME  userName,
		PLACE_TEL  tel,
		PLACE_ROLE_ID roleId,
		PLACE_PID  placePid ,
		PLACE_AREA areaName,
		PLACE_HAS_QRCODE hasQrcode,
		PLACE_INIT_CODE  initCode,
		PLACE_REMARK  remark
	   FROM tb_iamberry_place_info
        WHERE
        PLACE_STATUS=1
        <if test="beginNum!=null and beginNum!=''">
         <![CDATA[ and  PLACE_ID >= #{beginNum}  ]]>
        </if>
        <if test="num!=null and num!=''">
          limit #{num}
        </if>
	</select>

	<select  id="getUnderling" parameterType="PlaceInfo" resultType="string">
	 select  t.PLACE_OPENID  openid
	 from tb_iamberry_place_info t
	 <where>
	   t.PLACE_PID=(select PLACE_ID from tb_iamberry_place_info where PLACE_OPENID=#{openid})
	   <if test="roleId>0" >
		and t.PLACE_ROLE_ID=${roleId}
	   </if>
	   and t.PLACE_OPENID is not null
	 </where>
	</select>

	<select id="selectAll" resultType="PlaceInfo" parameterType="PlaceInfo">
		select
		<include refid="palceInfoAllField"/>,
		(select role_name from tb_iamberry_place_role where role_id=roleId )  as roleName,
		(SELECT Q.QRCODE_URL FROM TB_IAMBERRY_USER_QRCODE Q WHERE (Q.QRCODE_REPLACE_OPENID = id OR Q.QRCODE_REPLACE_OPENID = openid) AND Q.QRCODE_STATUS = 1 limit 1) qrcodeUrl
        from tb_iamberry_place_info
		<where>
		    PLACE_STATUS=1
			<if test="name!=null and name!=''">
			  and PLACE_NAME like CONCAT('%',#{name},'%')
			</if>
			<if test="userName!=null and userName!=''" >
			    and PLACE_USERNAME like CONCAT('%',#{userName},'%')
			</if>
			<if test="roleId!=null and roleId!=''" >
			    and PLACE_ROLE_ID=#{roleId}
			</if>
			<if test="tel!=null and tel!=''">
				AND PLACE_TEL LIKE CONCAT('%',#{tel},'%')
			</if>
			<if test="userNickname != null and userNickname != ''">
				AND PLACE_OPENID IN (SELECT T.USER_OPENID FROM TB_IAMBERRY_USER_USERINFO T WHERE T.USER_NICKNAME LIKE CONCAT('%',#{userNickname},'%'))
			</if>
			<if test="placePid!=null and placePid!=''" >
			    and PLACE_PID=#{placePid}
			</if>
			<if test="supperId>0" >
			   and PLACE_SUPPER_ID =#{supperId}
			</if>
			<if test="parentName!=null and parentName!=''" >
			   and PLACE_PID in
                (select PLACE_ID from tb_iamberry_place_info where
                  PLACE_NAME like CONCAT('%',#{parentName},'%')
                )
			</if>
			<if test="initCode!=null and initCode!=''" >
			    and UPPER(PLACE_INIT_CODE) like  CONCAT('%',UPPER(#{initCode}),'%')
			</if>
	    </where>
	    order by place_create_date desc
	    <if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
	     limit ${page.recordBegin},${page.pageSize}
	    </if>
	     <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
	     limit ${page.pageSize}
	    </if>
	</select>

	<!-- 公司分销下线用户 -  个人代理个数 -->
	<select id="getDistributorNumber"  resultType="integer" parameterType="PlaceInfo">
		SELECT count(*)
		FROM tb_iamberry_place_info TIPI
		<where>
			PLACE_STATUS=1
			<if test="name!=null and name!=''">
				and PLACE_NAME like CONCAT('%',#{name},'%')
			</if>
			<if test="userName!=null and userName!=''" >
				and PLACE_USERNAME like CONCAT('%',#{userName},'%')
			</if>
			<if test="roleId!=null and roleId!=''" >
				and PLACE_ROLE_ID=#{roleId}
			</if>
			<if test="tel!=null and tel!=''">
				AND PLACE_TEL LIKE CONCAT('%',#{tel},'%')
			</if>
			<if test="userNickname != null and userNickname != ''">
				AND PLACE_OPENID IN (SELECT T.USER_OPENID FROM TB_IAMBERRY_USER_USERINFO T WHERE T.USER_NICKNAME LIKE CONCAT('%',#{userNickname},'%'))
			</if>
			<if test="placePid!=null and placePid!=''" >
				and PLACE_PID=#{placePid}
			</if>
			<if test="supperId>0" >
				and PLACE_SUPPER_ID =#{supperId}
			</if>
			<if test="initCode!=null and initCode!=''" >
				and UPPER(PLACE_INIT_CODE) like  CONCAT('%',UPPER(#{initCode}),'%')
			</if>
			<if test="openid==1" >
				AND IFNULL(PLACE_OPENID,"") != ""
			</if>
			<if test="openid==2" >
				AND IFNULL(PLACE_OPENID,"") = ""
			</if>

		</where>
	</select>

	<!-- 公司分销下线用户 -  个人代理 -->
    <select id="getDistributor" resultType="PlaceInfo" parameterType="PlaceInfo">
        select
        	<include refid="palceInfoAllField"/>,
			TIUU.user_head  AS  userHead,TIUU.user_nickname AS userNickname
        from tb_iamberry_place_info
		LEFT JOIN tb_iamberry_user_userinfo TIUU ON tb_iamberry_place_info.place_openid = TIUU.user_openid
        <where>
            PLACE_STATUS=1
            <if test="name!=null and name!=''">
                and PLACE_NAME like CONCAT('%',#{name},'%')
            </if>
            <if test="userName!=null and userName!=''" >
                and PLACE_USERNAME like CONCAT('%',#{userName},'%')
            </if>
            <if test="roleId!=null and roleId!=''" >
                and PLACE_ROLE_ID=#{roleId}
            </if>
            <if test="tel!=null and tel!=''">
                AND PLACE_TEL LIKE CONCAT('%',#{tel},'%')
            </if>
            <if test="userNickname != null and userNickname != ''">
                AND PLACE_OPENID IN (SELECT T.USER_OPENID FROM TB_IAMBERRY_USER_USERINFO T WHERE T.USER_NICKNAME LIKE CONCAT('%',#{userNickname},'%'))
            </if>
            <if test="placePid!=null and placePid!=''" >
                and PLACE_PID=#{placePid}
            </if>
            <if test="supperId>0" >
                and PLACE_SUPPER_ID =#{supperId}
            </if>
            <if test="initCode!=null and initCode!=''" >
                and UPPER(PLACE_INIT_CODE) like  CONCAT('%',UPPER(#{initCode}),'%')
            </if>
			<if test="initCode==1" >
				and UPPER(PLACE_INIT_CODE) like  CONCAT('%',UPPER(#{initCode}),'%')
			</if>
			<if test="openid==1" >
				and PLACE_OPENID is not null
			</if>
			<if test="openid==2" >
				and PLACE_OPENID is null
			</if>
        </where>
        order by place_create_date desc
        <if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
            limit ${page.recordBegin},${page.pageSize}
        </if>
        <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
            limit ${page.pageSize}
        </if>
    </select>


	<select id="selectAllByParent" resultType="PlaceInfo" parameterType="PlaceInfo">
		select
		<include refid="palceInfoAllField"/>,
		(select role_name from tb_iamberry_place_role where role_id=roleId )  as roleName,
		(SELECT Q.QRCODE_URL FROM TB_IAMBERRY_USER_QRCODE Q WHERE (Q.QRCODE_REPLACE_OPENID = id OR Q.QRCODE_REPLACE_OPENID = openid) AND Q.QRCODE_STATUS = 1 limit 1) qrcodeUrl
        from tb_iamberry_place_info
		<where>
		    PLACE_STATUS=1
			<if test="name!=null and name!=''">
			  and PLACE_NAME like CONCAT('%',#{name},'%')
			</if>

			<if test="roleId!=null and templateId == 23 and roleId == 49" >
			    and PLACE_ROLE_ID= 51
			</if>
			<if test="roleId!=null and templateId == 22 and roleId == 49" >
			    and PLACE_ROLE_ID= 48
			</if>
			<if test="roleId!=null and templateId == 21 and roleId == 49" >
			    and PLACE_ROLE_ID= 48
			</if>
			<if test="roleId!=null and templateId == 23 and roleId == 51" >
			    and PLACE_ROLE_ID= 48
			</if>
			<if test="roleId!=null and roleId == 48" >
			    and PLACE_ROLE_ID= 47
			</if>
			<if test="roleId!=null and roleId == 52" >
			    and PLACE_ROLE_ID= 53
			</if>
	    </where>
	    order by place_create_date desc
	    <if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
	     limit ${page.recordBegin},${page.pageSize}
	    </if>
	     <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
	     limit ${page.pageSize}
	    </if>
	</select>

	<select id="getAllByParentCount"  parameterType="PlaceInfo" resultType="integer">
	 select count(PLACE_ID) from tb_iamberry_place_info
	 	<where>
	 	    PLACE_STATUS=1
			<if test="name!=null and name!=''">
			  and PLACE_NAME like CONCAT('%',#{name},'%')
			</if>
			<if test="roleId!=null and templateId == 23 and roleId == 49" >
			    and PLACE_ROLE_ID= 51
			</if>
			<if test="roleId!=null and templateId == 22 and roleId == 49" >
			    and PLACE_ROLE_ID= 48
			</if>
			<if test="roleId!=null and templateId == 21 and roleId == 49" >
			    and PLACE_ROLE_ID= 48
			</if>
			<if test="roleId!=null and templateId == 23 and roleId == 51" >
			    and PLACE_ROLE_ID= 48
			</if>
			<if test="roleId!=null and roleId == 48" >
			    and PLACE_ROLE_ID= 47
			</if>
			<if test="roleId!=null and roleId == 52" >
			    and PLACE_ROLE_ID= 53
			</if>
	    </where>
	</select>

	<!-- 根据条件获取placeId -->
	<select id="getPlaceId" resultType="PlaceInfo" parameterType="PlaceInfo">
		SELECT
			PLACE_ID id,
			PLACE_OPENID openid,
			(SELECT Q.QRCODE_URL FROM TB_IAMBERRY_USER_QRCODE Q WHERE (Q.QRCODE_REPLACE_OPENID = id OR Q.QRCODE_REPLACE_OPENID = openid) AND Q.QRCODE_STATUS = 1 limit 1) qrcodeUrl
		FROM
			TB_IAMBERRY_PLACE_INFO
		<where>
		    PLACE_STATUS=1
			<if test="name!=null and name!=''">
			  and PLACE_NAME like CONCAT('%',#{name},'%')
			</if>
			<if test="userName!=null and userName!=''" >
			    and PLACE_USERNAME like CONCAT('%',#{userName},'%')
			</if>
			<if test="roleId!=null and roleId!=''" >
			    and PLACE_ROLE_ID=#{roleId}
			</if>
			<if test="placePid!=null and placePid!=''" >
			    and PLACE_PID=#{placePid}
			</if>
			<if test="supperId>0" >
			   and PLACE_SUPPER_ID =#{supperId}
			</if>
			<if test="parentName!=null and parentName!=''" >
			   and PLACE_PID in
                (select PLACE_ID from tb_iamberry_place_info where
                  PLACE_NAME like CONCAT('%',#{parentName},'%')
                )
			</if>
	    </where>
	    order by place_create_date desc
	</select>

	<select id="getAllCount" resultType="integer">
	 select count(PLACE_ID) from tb_iamberry_place_info
	 	<where>
	 	    PLACE_STATUS=1
			<if test="name!=null and name!=''">
			  and PLACE_NAME like CONCAT('%',#{name},'%')
			</if>
			<if test="userName!=null and userName!=''" >
			   and PLACE_USERNAME like CONCAT('%',#{userName},'%')
			</if>
			<if test="supperId!=null and supperId!=''" >
			   and PLACE_SUPPER_ID =${supperId}
			</if>
			<if test="roleId!=null and roleId!=''" >
			    and PLACE_ROLE_Id=#{roleId}
			</if>
			<if test="placePid!=null and placePid!=''" >
			    and PLACE_PID=#{placePid}
			</if>
			<if test="parentName!=null and parentName!=''" >
			   and PLACE_PID in
                (select PLACE_ID from tb_iamberry_place_info where
                  PLACE_NAME like CONCAT('%',#{parentName},'%')
                )
			</if>
	    </where>
	</select>

	<select id="testNameExist"  resultType="Integer">
	 select count(PLACE_ID) from tb_iamberry_place_info
	 where
	  PLACE_NAME= #{name}
	  and PLACE_STATUS=1
	</select>

 	<insert id="insert" >
 	  insert into 
 	  tb_iamberry_place_info(
		PLACE_SUPPER_ID ,
		PLACE_NAME ,
		PLACE_ADDR ,
		PLACE_USERNAME  ,
		PLACE_TEL ,
		PLACE_AMOUNT ,
		PLACE_CREATE_DATE ,
		PLACE_STATUS ,
		PLACE_OPENID ,
		PLACE_ROLE_ID ,
		PLACE_PID ,
		PLACE_HAS_QRCODE ,
		PLACE_TEMPLATE_ID ,
		PLACE_AREA ,
		PLACE_INIT_CODE,
		PLACE_REMARK) 
      values(#{supperId},#{name},#{addr},#{userName},#{tel},#{amount},
      #{createDate},#{status},#{openid},${roleId},${placePid},#{hasQrcode},#{templateId},#{areaName},
      #{initCode},#{remark})
 	</insert>
 	<!-- 插入对象，并返回带有主键的对象 -->
 	<insert id="insertObj" useGeneratedKeys="true" keyProperty="id" >
 	   <selectKey keyProperty="id" resultType="int">
            select LAST_INSERT_ID()
        </selectKey>
 	  insert into
 	  tb_iamberry_place_info(
		PLACE_SUPPER_ID ,
		PLACE_NAME ,
		PLACE_ADDR ,
		PLACE_USERNAME  ,
		PLACE_TEL ,
		PLACE_AMOUNT ,
		PLACE_CREATE_DATE ,
		PLACE_STATUS ,
		PLACE_OPENID ,
		PLACE_ROLE_ID ,
		PLACE_PID ,
		PLACE_HAS_QRCODE ,
		PLACE_TEMPLATE_ID ,
		PLACE_AREA ,
		PLACE_INIT_CODE,
		PLACE_REMARK,
		PLACE_INFO_STATUS)
      values(#{supperId},#{name},#{addr},#{userName},#{tel},#{amount},
      #{createDate},#{status},#{openid},${roleId},${placePid},#{hasQrcode},#{templateId},#{areaName},
      #{initCode},#{remark},#{placeInfoStus})
 	</insert>

     <update id="update" >
	     update tb_iamberry_place_info
	     <set>
	     	<if test="templateId!=null and templateId!=''">
	          PLACE_TEMPLATE_ID =#{templateId},
	        </if>
	        <if test="name!=null and name!=''">
	          PLACE_NAME =#{name},
	        </if>
	        <if test="addr!=null and addr!=''">
	          PLACE_ADDR =#{addr},
	        </if>
	        <if test="userName!=null and userName!=''">
	          PLACE_USERNAME =#{userName},
	        </if>
	        <if test="tel!=null and tel!=''">
	          PLACE_TEL =#{tel},
	        </if>
	        <if test="amount!=null and amount!=''">
	          PLACE_AMOUNT =#{amount},
	        </if>
	        <if test="status!=null and status!=''">
	          PLACE_STATUS =#{status},
	        </if>
	        <if test="openid!=null and openid!=''">
	          PLACE_OPENID =#{openid},
	        </if>
	         <if test="roleId!=null and roleId!=''">
	          PLACE_ROLE_ID =#{roleId},
	        </if>
	        <if test="placePid!=null and placePid!=''">
	          PLACE_PID =#{placePid},
	        </if>
	        <if test="placeIds!=null and placeIds!=''">
	          PLACE_PIDS =#{placeIds},
	        </if>
	        <if test="areaName!=null and areaName!=''">
	          PLACE_AREA =#{areaName},
	        </if>
	         <if test="hasQrcode!=null and hasQrcode!=''">
	          PLACE_HAS_QRCODE =#{hasQrcode},
	        </if>
	         <if test="remark!=null and remark!=''">
	          PLACE_REMARK =#{remark}
	        </if>
	     </set>
	      where PLACE_ID = #{id}
     </update>

 	<update id="batchUpdate"  parameterType="java.util.List">
      <foreach collection="list" item="item" index="index" separator=";" open="" close="">
			 update tb_iamberry_place_info
	     <set>
	        <if test="item.name!=null and item.name!=''">
	          PLACE_NAME =#{item.name,jdbcType=VARCHAR},
	        </if>
	        <if test="item.addr!=null and item.addr!=''">
	          PLACE_ADDR =#{item.addr,jdbcType=VARCHAR},
	        </if>
	        <if test="item.userName!=null and item.userName!=''">
	          PLACE_USERNAME =#{item.userName,jdbcType=VARCHAR},
	        </if>
	        <if test="item.tel!=null and item.tel!=''">
	          PLACE_TEL =#{item.tel,jdbcType=VARCHAR},
	        </if>
	        <if test="item.areaName!=null and item.areaName!=''">
	          PLACE_AREA =#{item.areaName,jdbcType=VARCHAR},
	        </if>
	         <if test="item.hasQrcode!=null and item.hasQrcode!=''">
	          PLACE_HAS_QRCODE =#{item.hasQrcode,jdbcType=INTEGER},
	        </if>
	         <if test="item.remark!=null and item.remark!=''">
	          PLACE_REMARK =#{item.remark,jdbcType=VARCHAR}
	        </if>
	     </set>
	      where PLACE_ID = #{item.id,jdbcType=INTEGER}
	 </foreach>
    </update>

	<delete id="delete" parameterType="string">
		delete from tb_iamberry_place_info  
		where PLACE_ID = #{appid}
	</delete>

 	<!-- 根据code获取代理商信息 -->
 	<select id="selectOnePlaceInfoByCode" parameterType="String" resultType="PlaceInfo">
		SELECT 
			PLACE_ID id, PLACE_OPENID openid, 
            (select r.role_name from tb_iamberry_place_role r where r.role_id=place_role_id )  as roleName
        FROM 
        	TB_IAMBERRY_PLACE_INFO 
        WHERE 
        	PLACE_INIT_CODE = #{code}
 	</select>

	<!-- 更新微代理的微信openid -->
	<update id="updateOpenidById" parameterType="PlaceInfo">
	     UPDATE 
	     	TB_IAMBERRY_PLACE_INFO
	     SET 
	     	PLACE_OPENID = #{openid}
	     WHERE
	     	PLACE_ID = #{id}
	</update>
	<!-- 根据ID，获取顶级代理商ID -->
	<select id="selectPlaceInfobyId" parameterType="Integer" resultType="PlaceInfo">
		SELECT 
			PLACE_ID id, PLACE_PIDS placeIds,PLACE_TEMPLATE_ID templateId, 
			PLACE_OPENID openid, PLACE_ROLE_ID roleId,PLACE_HAS_QRCODE hasQrcode
        FROM 
        	TB_IAMBERRY_PLACE_INFO 
        WHERE 
        	PLACE_ID = #{id}
	</select>

	<!-- 根据用户openid，获取对应的代理商ID -->
	<select id="selectPlaceIdByOpenId" parameterType="String" resultType="Integer">
		SELECT 
			PLACE_ID id
        FROM 
        	TB_IAMBERRY_PLACE_INFO 
        WHERE 
        	PLACE_OPENID = #{openid}
	</select>

	<!-- 根据代理商ID,获取完善信息的状态 -->
	<select id="selectPlaceInfoStatusByPlaceId" parameterType="Integer" resultType="Integer">
		SELECT 
			PLACE_INFO_STATUS
        FROM 
        	TB_IAMBERRY_PLACE_INFO
        WHERE 
        	PLACE_ID = #{id}
	</select>

	<!-- 获取某一页的商户申请记录 -->
	<select id="selectApplyPlaceLogs" parameterType="PaperBean" resultType="ApplyPlaceLogs">
		SELECT
			<include refid="applyLogsAllField"/>
		FROM
			TB_IAMBERRY_PLACE_APPLY_LOGS
		ORDER BY
			APPLY_ID
		LIMIT
			#{minNum}, #{maxNum}
	</select>

	<!-- 获取待处理的总记录条数 -->
	<select id="getCount" resultType="int">
		SELECT
			COUNT(APPLY_ID)
		FROM
			TB_IAMBERRY_PLACE_APPLY_LOGS
	</select>

	<!-- 修改状态 -->
	<update id="updateApplyStatus" parameterType="ApplyPlaceLogs">
		UPDATE
			TB_IAMBERRY_PLACE_APPLY_LOGS
		SET
			APPLY_STATUS = #{applyStatus}
		WHERE
			APPLY_ID = #{applyID}
	</update>

	<!-- 获取某一个商户最近的申请记录 -->
	<select id="selectApplyLogsByPlaceID" parameterType="int" resultType="ApplyPlaceLogs">
		SELECT
			<include refid="applyLogsAllField"/>
		FROM
			TB_IAMBERRY_PLACE_APPLY_LOGS
		WHERE
			APPLY_TO_PLACEID = #{applyToPlaceID}
		ORDER BY
			APPLY_ID DESC
		LIMIT
			0, 1
	</select>

	<!-- 获取某一个商户最近的申请记录 -->
	<select id="selectApplyLogsByOpenID" parameterType="String" resultType="ApplyPlaceLogs">
		SELECT
			<include refid="applyLogsAllField"/>
		FROM
			TB_IAMBERRY_PLACE_APPLY_LOGS
		WHERE
			APPLY_OPENID = #{applyOpenID}
		ORDER BY
			APPLY_ID DESC
		LIMIT
			0, 1
	</select>

	<!-- 根据apply_id获取申请记录 -->
	<select id="selectApplyLogsByID" parameterType="int" resultType="ApplyPlaceLogs">
		SELECT
			<include refid="applyLogsAllField"/>
		FROM
			TB_IAMBERRY_PLACE_APPLY_LOGS
		WHERE
			APPLY_ID = #{applyID}
	</select>

	<!-- 修改代理商信息 -->
	<update id="updatePlaceInfo" parameterType="PlaceInfo">
		UPDATE
			TB_IAMBERRY_PLACE_INFO
		SET
			PLACE_NAME = #{name},
			PLACE_AREA = #{areaName},
			PLACE_USERNAME = #{userName},
			PLACE_TEL = #{tel},
			PLACE_INFO_STATUS = 2,
			PLACE_ADDR = #{addr}
		WHERE
			PLACE_ID = #{id}
	</update>

	<!-- 保存申请记录信息 -->
	<insert id="insertApplyLogs" parameterType="ApplyPlaceLogs"
		useGeneratedKeys="true" keyProperty="applyID">
		INSERT INTO
			TB_IAMBERRY_PLACE_APPLY_LOGS
				(
					APPLY_OPENID , APPLY_TO_PLACEID ,
					APPLY_NAME , APPLY_USERNAME , APPLY_TEL ,
					APPLY_AREA , APPLY_DATE, APPLY_STATUS, APPLY_ADDR  
				)
		VALUES
			(
				#{applyOpenID}, #{applyToPlaceID}, #{applyName}, 
				#{applyUserName}, #{applyTel}, #{applyArea}, NOW(), 
				1, #{applyAddr}	
			)
	</insert>

	<!-- 获取商户详情 -->
	<select id="selectPlaceInfoDetailByOpenId" resultType="PlaceInfo" parameterType="String">
        SELECT 
			p.PLACE_ID id,
			u.USER_HEAD userHead,
			u.USER_NICKNAME userNickname,
			(select role_name from tb_iamberry_place_role where role_id=Place_role_id )  as roleName,
			p.PLACE_USERNAME  userName,
			p.PLACE_TEL  tel,
			p.PLACE_NAME  name,
			p.PLACE_AREA areaName,
			p.PLACE_ADDR   addr,
			p.PLACE_INFO_STATUS placeInfoStus
		FROM TB_IAMBERRY_PLACE_INFO p JOIN tb_iamberry_user_userinfo u ON p.place_openid=u.user_openid
		WHERE p.PLACE_OPENID = #{openid} and p.PLACE_STATUS=1
	</select>


	<!-- 我要代理申请信息页面 -->
	<sql id="applyAgentAllField">
	user_id userId,
	user_name userName,
	user_tel userTel,
	user_mail userMail,
	user_city userCity,
	user_tc  userTc,
	user_date userDate
	</sql>
	<insert id="addApplyAgent" parameterType="ApplyUserInfo">
	insert into user_info 
	(
	user_name,
	user_tel,
	user_mail,
	user_city,
	user_tc,
	user_date
	) value (
	#{userName},
	#{userTel},
	#{userMail},
	#{userCity},
	#{userTc},
	#{userDate}
	)
	</insert>
	<select id="selectApplyAgentByTel" parameterType="String" resultType="Integer">
		SELECT count(*) from user_info where user_tel = #{userTel}
	</select>
	<!-- 分页查询代理申请记录信息表数据 -->
 	<select id="getApplyAgentList" parameterType="ApplyUserInfo" resultType="ApplyUserInfo">
 		SELECT
 			<include refid="applyAgentAllField"/>
 		FROM
 			user_info
 		<where>
 			<if test="userName != null and userName != ''">
 				user_name like CONCAT('%',#{userName},'%')
 				or
 				user_tel like CONCAT('%',#{userName},'%')
 			</if>
 		</where>
 		LIMIT ${page.recordBegin},${page.pageSize}
 	</select>
 	<!-- 分页查询代理申请记录信息表数据条数 -->
 	<select id="getApplyAgentCount" parameterType="ApplyUserInfo" resultType="Integer">
 		SELECT
 			COUNT(user_id) Num
 		FROM
 			user_info
 		<where>
 			<if test="userName != null and userName != ''">
 				user_name like CONCAT('%',#{userName},'%')
 				or
 				user_tel like CONCAT('%',#{userName},'%')
 			</if>
 		</where>
 	</select>

 	<!-- 通过placepids查询商户信息 -->
 	<select id="selectPlaceByPlacePids" resultType="PlaceInfo" parameterType="Integer">
 		SELECT
 			<include refid="palceInfoAllField" />
 		FROM
 			TB_IAMBERRY_PLACE_INFO
 		WHERE
 			PLACE_PIDS = #{placeIds}
 		AND
 			PLACE_STATUS = 1
 	</select>

	<!-- 查询分销员商户购买商品的下线人数 -->
	<select id="getPlaceSalesUser" parameterType="PlaceInfo" resultType="Integer">
		SELECT
			COUNT(DISTINCT U.USER_ID)
		FROM
			TB_IAMBERRY_USER_USERINFO U
		LEFT JOIN
			TB_IAMBERRY_SHOP_SALES_ORDER O ON U.USER_OPENID = O.SALES_OPENID
		WHERE
			 O.SALES_STATUS IN (2,5,12)
			<!-- 查询个人分销员 -->
			<if test="roleId == 55">
				 AND U.USER_DEALERS = #{placeIds}
			</if>
			<!-- 查询公司分销员 -->
			<if test="roleId == 54">
				AND U.USER_DEALERS IN (SELECT
										  P.PLACE_PIDS
									  FROM
										TB_IAMBERRY_PLACE_INFO P
									  WHERE
										P.PLACE_PID = #{id})
			</if>
	</select>

	<!-- 查询分销员下线总人数 -->
	<select id="getPlaceUser" parameterType="PlaceInfo" resultType="Integer">
		SELECT
			COUNT(DISTINCT U.USER_ID)
		FROM
			TB_IAMBERRY_USER_USERINFO U
		<where>
			<!-- 查询个人分销员 -->
			<if test="roleId == 55">
				U.USER_DEALERS = #{placeIds}
			</if>
			<!-- 查询公司分销员 -->
			<if test="roleId == 54">
				U.USER_DEALERS IN (SELECT
									  P.PLACE_PIDS
								  FROM
									  TB_IAMBERRY_PLACE_INFO P
								  WHERE
									  P.PLACE_PID = #{id})
			</if>
		</where>
	</select>

	<!-- 查询商户的待返和已返金额 -->
	<!-- 添加 左链接 tb_iamberry_shop_sales_order 是为了过滤订单不为2,5,12的返利 -->
	<select id="listPlaceReback" parameterType="PlaceInfo" resultType="RebackInfo">
		SELECT
			REBACK_ID id,
			REBACK_TO_OPENID toOpenid,
			REBACK_FROM_OPENID fromOpenid,
			REBACK_ORDERID orderId,
			REBACK_USER_TYPE userType,
			REBACK_MONEY money,
			REBACK_CREATE_DATE createDate,
			REBACK_UPDATE_DATE updateDate,
			REBACK_FINISH_DATE finishDate,
			REBACK_STATUS status
		FROM
			TB_IAMBERRY_REBACK
		LEFT JOIN tb_iamberry_shop_sales_order TISSO ON TB_IAMBERRY_REBACK.REBACK_ORDERID = TISSO.sales_orderid
		WHERE
			REBACK_STATUS IN (1,2)
			AND TISSO.sales_status in (2,5,12)
		AND
			REBACK_USER_ID = #{id}
		ORDER BY REBACK_CREATE_DATE DESC
		<if test="page!=null and page.pageSize>0 ">
			LIMIT ${page.recordBegin},${page.pageSize}
		</if>
	</select>

	<!-- 查询商户已激活下线信息 -->
	<select  id="listActivatePlace" parameterType="PlaceInfo" resultType="PlaceInfo">
		select  <include refid="palceInfoAllField" />
		from tb_iamberry_place_info t
		<where>
			t.PLACE_PID = #{placePid}
			and t.PLACE_OPENID is not null
		</where>
	</select>

	<select id="getIsQrCode" parameterType="String" resultType="Integer">
		SELECT COUNT(*) from tb_iamberry_user_qrcode where qrcode_openid = #{id}
	</select>
 </mapper>