<?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.gic.haoban.manage.service.dao.mapper.StaffMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanStaff">
        <id column="staff_id" property="staffId" jdbcType="VARCHAR"/>
        <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR"/>
        <result column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR"/>
        <result column="phone_number" property="phoneNumber" jdbcType="VARCHAR"/>
        <result column="staff_name" property="staffName" jdbcType="VARCHAR"/>
        <result column="nation_code" property="nationCode" jdbcType="VARCHAR"/>
        <result column="nick_name" property="nickName" jdbcType="VARCHAR"/>
        <result column="sex" property="sex" jdbcType="INTEGER"/>
        <result column="head_img" property="headImg" jdbcType="VARCHAR"/>
        <result column="postion" property="postion" jdbcType="VARCHAR"/>
        <result column="active_flag" property="activeFlag" jdbcType="INTEGER"/>
        <result column="sort" property="sort" jdbcType="INTEGER"/>
        <result column="extend_postion" property="extendPostion" jdbcType="VARCHAR"/>
        <result column="sync_postion_flag" property="syncPostionFlag" jdbcType="INTEGER"/>
        <result column="super_manager_flag" property="superManagerFlag" jdbcType="INTEGER"/>
        <result column="status_flag" property="statusFlag" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="wx_open_user_id" property="wxOpenUseId" jdbcType="VARCHAR"/>
        <result column="wx_open_id" property="wxOpenId" jdbcType="VARCHAR"/>
        <result column="qr_code" property="qrCode" jdbcType="VARCHAR"/>
        <result column="add_num" property="addNum" jdbcType="INTEGER"/>
        <result column="open_concat_flag" property="openConcatFlag"/>
        <result column="active_time" property="activeTime" jdbcType="TIMESTAMP"/>
        <result column="expire_time" property="expireTime" jdbcType="TIMESTAMP"/>
        <result column="active_code" property="activeCode" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        staff_id
        , wx_user_id, phone_number, staff_name, nation_code, nick_name, sex, postion,
    active_flag, extend_postion, status_flag, create_time, update_time,wx_enterprise_id,head_img,
    sort,sync_postion_flag,super_manager_flag,wx_open_user_id,wx_open_id,qr_code,add_num,open_concat_flag,active_time,expire_time,active_code
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where staff_id = #{staffId,jdbcType=VARCHAR} and status_flag = 1
    </select>


    <update id="delOtherStaffByWxUserId">
        update tab_haoban_staff
        set status_flag = 0,
            update_time = now()
        where wx_enterprise_id = #{wxEnterpriseId}
          and wx_user_id = #{wxUserId}
          and status_flag = 1
          and staff_id != #{staffId}
    </update>

    <insert id="insertSelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanStaff">
        insert into tab_haoban_staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="staffId != null">
                staff_id,
            </if>
            <if test="wxUserId != null">
                wx_user_id,
            </if>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id,
            </if>
            <if test="phoneNumber != null">
                phone_number,
            </if>
            <if test="staffName != null">
                staff_name,
            </if>
            <if test="nationCode != null">
                nation_code,
            </if>
            <if test="nickName != null">
                nick_name,
            </if>
            <if test="sex != null">
                sex,
            </if>
            <if test="postion != null">
                postion,
            </if>
            <if test="activeFlag != null">
                active_flag,
            </if>
            <if test="extendPostion != null">
                extend_postion,
            </if>
            <if test="headImg != null">
                head_img,
            </if>
            <if test="statusFlag != null">
                status_flag,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="sort != null">
                sort,
            </if>
            <if test="syncPostionFlag != null">
                sync_postion_flag,
            </if>
            <if test="superManagerFlag != null">
                super_manager_flag,
            </if>
            <if test="wxOpenUseId != null">
                wx_open_user_id,
            </if>
            <if test="wxOpenId != null">
                wx_open_id,
            </if>
            <if test="qrCode != null">
                qr_code,
            </if>
            <if test="openConcatFlag != null">
                open_concat_flag,
            </if>
            <if test="addNum != null">
                add_num,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="staffId != null">
                #{staffId,jdbcType=VARCHAR},
            </if>
            <if test="wxUserId != null">
                #{wxUserId,jdbcType=VARCHAR},
            </if>
            <if test="wxEnterpriseId != null">
                #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="phoneNumber != null">
                #{phoneNumber,jdbcType=VARCHAR},
            </if>
            <if test="staffName != null">
                #{staffName,jdbcType=VARCHAR},
            </if>
            <if test="nationCode != null">
                #{nationCode,jdbcType=VARCHAR},
            </if>
            <if test="nickName != null">
                #{nickName,jdbcType=VARCHAR},
            </if>
            <if test="sex != null">
                #{sex,jdbcType=INTEGER},
            </if>
            <if test="postion != null">
                #{postion,jdbcType=VARCHAR},
            </if>
            <if test="activeFlag != null">
                #{activeFlag,jdbcType=INTEGER},
            </if>
            <if test="extendPostion != null">
                #{extendPostion,jdbcType=VARCHAR},
            </if>
            <if test="headImg != null">
                #{headImg,jdbcType=VARCHAR},
            </if>
            <if test="statusFlag != null">
                #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="sort != null">
                #{sort,jdbcType=INTEGER},
            </if>
            <if test="syncPostionFlag != null">
                #{syncPostionFlag},
            </if>
            <if test="superManagerFlag != null">
                #{superManagerFlag},
            </if>
            <if test="wxOpenUseId != null">
                #{wxOpenUseId},
            </if>
            <if test="wxOpenId != null">
                #{wxOpenId},
            </if>
            <if test="qrCode != null">
                #{qrCode},
            </if>
            <if test="openConcatFlag != null">
                #{openConcatFlag},
            </if>
            <if test="addNum != null">
                #{addNum}
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanStaff">
        update tab_haoban_staff
        <set>
            <if test="wxUserId != null">
                wx_user_id = #{wxUserId,jdbcType=VARCHAR},
            </if>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="phoneNumber != null">
                phone_number = #{phoneNumber,jdbcType=VARCHAR},
            </if>
            <if test="staffName != null">
                staff_name = #{staffName,jdbcType=VARCHAR},
            </if>
            <if test="nationCode != null">
                nation_code = #{nationCode,jdbcType=VARCHAR},
            </if>
            <if test="nickName != null">
                nick_name = #{nickName,jdbcType=VARCHAR},
            </if>
            <if test="sex != null and sex !=0 ">
                sex = #{sex,jdbcType=INTEGER},
            </if>
            <if test="postion != null">
                postion = #{postion,jdbcType=VARCHAR},
            </if>
            <if test="activeFlag != null">
                active_flag = #{activeFlag,jdbcType=INTEGER},
            </if>
            <if test="extendPostion != null">
                extend_postion = #{extendPostion,jdbcType=VARCHAR},
            </if>
            <if test="headImg != null">
                head_img = #{headImg,jdbcType=VARCHAR},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag,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="sort != null">
                sort = #{sort,jdbcType=INTEGER},
            </if>
            <if test="syncPostionFlag != null">
                sync_postion_flag = #{syncPostionFlag,jdbcType=INTEGER},
            </if>
            <if test="superManagerFlag != null">
                super_manager_flag = #{superManagerFlag,jdbcType=INTEGER},
            </if>
            <if test="wxOpenUseId != null">
                wx_open_user_id = #{wxOpenUseId},
            </if>
            <if test="wxOpenId != null">
                wx_open_id = #{wxOpenId},
            </if>
            <if test="qrCode != null">
                qr_code = #{qrCode},
            </if>
            <if test="addNum != null">
                add_num = #{addNum}
            </if>
        </set>
        where staff_id = #{staffId,jdbcType=VARCHAR}
    </update>

    <select id="selectByNationcodeAndPhoneNumber" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where nation_code = #{nationCode}
        and phone_number = #{phoneNumber}
        and wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 1
        limit 1
    </select>

    <select id="listByPhoneNumber" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where phone_number = #{phoneNumber}
        and status_flag = 1
    </select>

    <select id="pageStaff" parameterType="com.gic.haoban.manage.service.pojo.bo.StaffListBO" resultMap="BaseResultMap">
        select
        a.*
        <if test="departmentIds != null and departmentIds.size() > 0">
            , b.department_id departmentIds
        </if>
        from tab_haoban_staff a
        <if test="departmentIds != null and departmentIds.size() > 0">
            left join tab_haoban_staff_department_related b on a.staff_id = b.staff_id and b.status_flag = 1
        </if>
        where a.wx_enterprise_id = #{wxEnterpriseId} 
        <if test="occupy == null or 1!=occupy">
	        and a.status_flag = 1
        </if>
        <if test="keyWord != null and keyWord != ''">
            and (a.staff_name like CONCAT('%',#{keyWord},'%') or a.phone_number like CONCAT('%',#{keyWord},'%'))
        </if>
        <if test="activeFlag != null">
            and a.active_flag = #{activeFlag}
        </if>
        <if test="staffIds != null and staffIds.size() > 0 and relationFlag != null and relationFlag == 1 ">
            and a.staff_id IN
            <foreach collection="staffIds" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="staffIds != null and staffIds.size() > 0 and relationFlag != null and relationFlag == 0 ">
            and a.staff_id not in
            <foreach collection="staffIds" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
        
         <!--  其他占用  -->
        <if test="occupy != null and 1==occupy">
        	and a.status_flag = 0 and a.del_time >= DATE_FORMAT(NOW(),'%Y-%m-%d')  and a.expire_time >= DATE_FORMAT(NOW(),'%Y-%m-%d')
        </if>
        <!--   -->
        <if test="occupy != null and 2==occupy">
        	and ( a.expire_time is null   or a.expire_time <![CDATA[<]]> now() )
        </if>
        <!-- 	许可状态  0 未激活，1 正常有效，2 即将过期，3 已过期，4 查询可以续期的成员（即状态是1、2、3的成员） -->
        <if test="null != permitState and '' != permitState">
        	<if test="permitState==0">
        		and a.expire_time is null
        	</if>
        	<if test="permitState==1">
        		and  a.expire_time > now()  and a.expire_time >  DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-%d'),INTERVAL 30 DAY)
        	</if>
        	
        	<if test="permitState==2">
        		 and a.expire_time >  now() and a.expire_time <![CDATA[<=]]>  DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-%d'),INTERVAL 30 DAY)
        	</if>
        	
        	<if test="permitState==3">
        		 and a.expire_time <![CDATA[<]]> now()
        	</if>
            <if test="permitState==4">
                and a.expire_time is not null
            </if>
        </if>
        <if test="null != startTime and startTime !='' ">
            and a.expire_time <![CDATA[>=]]> #{startTime}
        </if>
        <if test="null != endTime and endTime !='' ">
            and a.expire_time <![CDATA[<=]]> #{endTime}
        </if>
        <if test="departmentIds != null and departmentIds.size() > 0">
            and b.department_id IN
            <foreach collection="departmentIds" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="departmentIds != null and departmentIds.size() > 0">
            group by a.staff_id
        </if>
        order by ${sortField} ${sortType}
    </select>
    <select id="listByIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where status_flag = 1
        and staff_id IN
        <foreach collection="staffIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </select>

    <select id="listByStaffIdList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where staff_id IN
        <foreach collection="staffIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </select>

    <select id="listLikeName" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where status_flag = 1 and wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        <if test="staffName != null and staffName != ''">
            and staff_name like CONCAT('%',#{staffName,jdbcType=VARCHAR},'%')
        </if>
    </select>

    <select id="selectByUserIdAndEnterpriseId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where (wx_user_id = #{userId,jdbcType=VARCHAR} or wx_open_user_id = #{userId,jdbcType=VARCHAR})
        and wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 1 limit 1
    </select>
    
    <select id="getDeleteUser" resultMap="BaseResultMap">
    	select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where (wx_user_id = #{userId,jdbcType=VARCHAR} or wx_open_user_id = #{userId,jdbcType=VARCHAR})
        and wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 0 order by create_time desc limit 1
    </select>
    

    <select id="listByUserIdsAndWxEnterpriseId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where status_flag = 1
        and wx_enterprise_id = #{wxEnterpriseId}
        and ( wx_user_id IN
        <foreach collection="userIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
        or
        wx_open_user_id IN
        <foreach collection="userIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
        )
    </select>

    <update id="cleanStaff" parameterType="com.gic.haoban.manage.service.entity.TabHaobanStaff">
        update tab_haoban_staff
        set
        status_flag = 0,
        update_time = now() , del_time = now()
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR} and status_flag = 1
        and staff_id in
        <foreach collection="staffIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

	<update id="delStaff">
        update tab_haoban_staff set
        status_flag = 0,
        update_time = now() , del_time = now()
        where staff_id = #{staffId} and status_flag = 1
    </update>

    <select id="listUserStaffId" resultType="string">
        select staff_id FROM tab_haoban_staff
        WHERE
        wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR} and status_flag = 1 and super_manager_flag is null
        and staff_id not in
        <foreach collection="staffIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="listNoStatusStaffIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        FROM tab_haoban_staff
        WHERE
        wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR} and super_manager_flag is null
        and staff_id in
        <foreach collection="staffIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getNoStatusByWxUserIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        FROM tab_haoban_staff
        WHERE
        wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        and super_manager_flag is null
        and (wx_user_id = #{wxUserId,jdbcType=VARCHAR} or wx_open_user_id = #{wxUserId,jdbcType=VARCHAR})
        order by update_time desc,create_time desc limit 1
    </select>

    <update id="updateOpenUserIdsByUserId" parameterType="com.gic.haoban.manage.service.entity.TabHaobanStaff">
        update tab_haoban_staff
        set wx_open_user_id = #{wxOpenUseId},
            update_time     = now()
        where wx_enterprise_id = #{wxEnterpriseId}
          and wx_user_id = #{wxUserId}
    </update>

    <select id="getQrCodeByStaffId" resultType="string">
        select qr_code from tab_haoban_staff where staff_id = #{staffId} and status_flag = 1
    </select>

    <update id="updateOpenIdByStaffId" parameterType="com.gic.haoban.manage.service.entity.TabHaobanStaff">
        update tab_haoban_staff
        set wx_open_id  = #{openId},
            update_time = now()
        where staff_id = #{staffId}
    </update>

    <update id="updateQrCodeByStaffId" parameterType="com.gic.haoban.manage.service.entity.TabHaobanStaff">
        update tab_haoban_staff
        set qr_code     = #{qrCode},
            update_time = now()
        where staff_id = #{staffId}
    </update>

    <update id="updateQrCodeByUserId" parameterType="com.gic.haoban.manage.service.entity.TabHaobanStaff">
        update tab_haoban_staff
        set qr_code     = #{qrCode},
            update_time = now()
        where wx_user_id = #{userId} or wx_open_user_id = #{userId}
    </update>

    <select id="selectByPhoneNumberAndEnterpriseId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where phone_number = #{phoneNumber}
        and wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 1 limit 1
    </select>

    <select id="listByWxEnterpriseId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff
        where wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 1
    </select>


    <update id="updateAddNumById" parameterType="java.lang.String">
        update tab_haoban_staff
        set add_num     = add_num + 1,
            update_time = now()
        where staff_id = #{staffId}
          and status_flag = 1
    </update>


    <update id="updateActiveStatusById">
        update tab_haoban_staff
        set
  			`active_time` = #{activeTime},
  			`expire_time` = #{expireTime},
    		`active_code` = #{activeCode},
            update_time   = now()
        where staff_id = #{staffId}
    </update>
    
    <update id="deleteActivieInfo">
    	 update tab_haoban_staff
        set
  			`active_time` = null,
  			`expire_time` = null ,
  			active_code = null ,
            update_time   = now()
        where wx_enterprise_id = #{wxEnterpriseId} and active_code is not null 
    </update>


    <update id="resetAddNum">
        update tab_haoban_staff
        <set>
            add_num = 0
        </set>
        where status_flag = 1
    </update>


    <select id="listBindStoreIds" resultType="com.gic.haoban.manage.api.dto.StaffDTO">
        select
        a.staff_id staffId,
        a.phone_number phoneNumber,
        a.staff_name staffName,
        a.head_img headImg,
        a.wx_enterprise_id wxEnterpriseId,
        b.clerk_id clerkId,
        b.clerk_code clerkCode,
        b.enterprise_id enterpriseId,
        b.store_id storeId
        from tab_haoban_staff a
        left join tab_haoban_staff_clerk_relation b on a.staff_id = b.staff_id
        where a.wx_enterprise_id = #{wxEnterpriseId}
        and a.status_flag = 1 and b.status_flag = 1
        <if test="null != storeIds and storeIds.size gt 0">
            and b.store_id in
            <foreach collection="storeIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        limit 100
    </select>
    
    
    <select id="listAllByPage" resultMap="BaseResultMap">
        select <include refid="Base_Column_List" /> from tab_haoban_staff where wx_enterprise_id = #{wxEnterpriseId} and status_flag = 1 order by staff_id
        limit ${startItem},${pageSize}
    </select>

    <update id="updateOpenConcatFlag">
    		update tab_haoban_staff set open_concat_flag = 1 where 
    		<if test="mixFlag==0">
    			wx_user_id in 
    		</if>
    		<if test="mixFlag==1">
	    		wx_open_user_id in 
    		</if>
    		<foreach collection="wxUserIdList" index="index" item="id" close=")" open="(" separator=",">
    			 #{id}
    		</foreach>
    		and status_flag = 1
    </update>

    <select id="getOccupyNum" resultType="java.lang.Integer">
        SELECT count(*) FROM tab_haoban_staff WHERE status_flag = 0 and del_time >= DATE_FORMAT(NOW(),'%Y-%m-%d') and expire_time >= DATE_FORMAT(NOW(),'%Y-%m-%d') and wx_enterprise_id = #{wxEnterpriseId}
    </select>

    <select id="getExpiringNum" resultType="java.lang.Integer">
        SELECT count(*) FROM tab_haoban_staff WHERE status_flag = 1 and expire_time > now() and expire_time <![CDATA[<=]]>  DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-%d'),INTERVAL 30 DAY) and wx_enterprise_id = #{wxEnterpriseId}
    </select>


    <update id="updateStaffInfoByStaffId">
        update tab_haoban_staff set
             active_time = #{activeTime},
             expire_time = #{expireTime},
             active_code = #{activeCode}
        where  staff_id= #{staffId}
    </update>

    <select id="queryStaffIdsWithEnterpriseId" resultType="java.lang.String">
        select staff_id
            from tab_haoban_staff
        where status_flag = 1
          and wx_enterprise_id in
          <foreach collection="wxEnterpriseIds" item="wxEnterpriseId" open="(" close=")" separator=",">
              #{wxEnterpriseId}
          </foreach>
    </select>
    <select id="getNumByActiveCode" resultType="java.lang.Integer">
        SELECT count(*) FROM tab_haoban_staff WHERE  wx_enterprise_id = #{wxEnterpriseId} and active_code = #{activeCode}
    </select>

</mapper>