<?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.StaffDepartmentRelatedMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanStaffDepartmentRelated">
        <id column="staff_department_related_id" property="staffDepartmentRelatedId" jdbcType="VARCHAR"/>
        <result column="staff_id" property="staffId" jdbcType="VARCHAR"/>
        <result column="phone_number" property="phoneNumber" jdbcType="VARCHAR"/>
        <result column="nation_code" property="nationCode" jdbcType="VARCHAR"/>
        <result column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR"/>
        <result column="department_id" property="departmentId" jdbcType="VARCHAR"/>
        <result column="status_flag" property="statusFlag" jdbcType="INTEGER"/>
        <result column="clerk_code" property="clerkCode" jdbcType="VARCHAR"/>
        <result column="staff_name" property="staffName" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    <sql id="Base_Column_List">
        staff_department_related_id, staff_id, phone_number, nation_code,  department_id,
    status_flag, clerk_code, create_time, update_time,wx_enterprise_id,staff_name
    </sql>
    
    <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.TabHaobanStaffDepartmentRelated">
        insert into tab_haoban_staff_department_related (staff_department_related_id, staff_id,
                                                         phone_number, nation_code, 
                                                         department_id, status_flag, clerk_code,
                                                         create_time, update_time, wx_enterprise_id, staff_name)
        values (#{staffDepartmentRelatedId,jdbcType=VARCHAR}, #{staffId,jdbcType=VARCHAR},
                #{phoneNumber,jdbcType=VARCHAR}, #{nationCode,jdbcType=VARCHAR}, 
                #{departmentId,jdbcType=VARCHAR}, #{statusFlag,jdbcType=INTEGER}, #{clerkCode,jdbcType=VARCHAR},
                #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, #{wxEnterpriseId}, #{staffName})
    </insert>
    
    <update id="updateByPrimaryKeySelective"
            parameterType="com.gic.haoban.manage.service.entity.TabHaobanStaffDepartmentRelated">
        update tab_haoban_staff_department_related
        <set>
            <if test="staffId != null">
                staff_id = #{staffId,jdbcType=VARCHAR},
            </if>
            <if test="phoneNumber != null">
                phone_number = #{phoneNumber,jdbcType=VARCHAR},
            </if>
            <if test="nationCode != null">
                nation_code = #{nationCode,jdbcType=VARCHAR},
            </if>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="departmentId != null">
                department_id = #{departmentId,jdbcType=VARCHAR},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="clerkCode != null">
                clerk_code = #{clerkCode,jdbcType=VARCHAR},
            </if>
            <if test="staffName != null">
                staff_name = #{staffName,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where staff_department_related_id = #{staffDepartmentRelatedId,jdbcType=VARCHAR}
    </update>

    <select id="listByDepartmentId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff_department_related
        where department_id = #{departmentId,jdbcType=VARCHAR}
        and status_flag = 1
    </select>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff_department_related
        where staff_department_related_id = #{staffDepartmentRelatedId,jdbcType=VARCHAR}
        and status_flag = 1

    </select>

    <select id="listStaffDepartmentByStaffId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff_department_related
        where staff_id = #{staffId,jdbcType=VARCHAR}
        and status_flag = 1
        group by department_id
    </select>

    <update id="delByStaffid">
        update tab_haoban_staff_department_related
        set status_flag = 0,
            update_time = now()
        where staff_id = #{staffId,jdbcType=VARCHAR}
    </update>


<!--    <select id="getDepartmentIdAndStaffId" resultMap="BaseResultMap" parameterType="java.lang.String">-->
<!--        select-->
<!--        <include refid="Base_Column_List"/>-->
<!--        from tab_haoban_staff_department_related-->
<!--        where staff_id = #{staffId,jdbcType=VARCHAR}-->
<!--        and department_id = #{departmentId}-->
<!--        and status_flag = 1-->
<!--        limit 1-->
<!--    </select>-->

<!--    <select id="getDepartmentIdAndCode" resultMap="BaseResultMap" parameterType="java.lang.String">-->
<!--        select-->
<!--        <include refid="Base_Column_List"/>-->
<!--        from tab_haoban_staff_department_related-->
<!--        where clerk_code = #{clerkCode,jdbcType=VARCHAR}-->
<!--        and department_id = #{departmentId}-->
<!--        and status_flag = 1-->
<!--    </select>-->


    <select id="countByDepartmentIds" resultType="java.lang.Integer">
        select
        count(*)
        from tab_haoban_staff_department_related
        where department_id in
        <foreach collection="departmentIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
        and status_flag = 1
    </select>

    <select id="listStaffDepartmentByStaffIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff_department_related
        where staff_id in
        <foreach collection="staffIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
        and status_flag = 1
    </select>

    <select id="listByDepartmentIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff_department_related
        where department_id in
        <foreach collection="departmentIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
        and status_flag = 1
    </select>

    <update id="cleanStaffDepartment">
        update tab_haoban_staff_department_related
        set
        status_flag = 0,
        update_time = now()
        where wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 1
        and staff_id in
        <foreach collection="staffIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <select id="listByWxEnterpriseIdAndStaffId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_staff_department_related
        where status_flag = 1
        and wx_enterprise_id = #{wxEnterpriseId} and staff_id = #{staffId}
    </select>	
    
    
    <select id="listNoDepartStaff" resultType="String">
    	SELECT distinct(a.staff_id) FROM `tab_haoban_staff` a LEFT JOIN `tab_haoban_staff_department_related` b ON a.`staff_id` = b.`staff_id` AND b.`status_flag` = 1 
		WHERE 
		 a.create_time >= #{addDate}
		 <if test="null != wxEnterpriseId">
		 	and a.wx_enterprise_id = #{wxEnterpriseId} 
		 </if>
		  AND a.status_flag = 1 AND b.`staff_department_related_id` IS NULL 
    </select>
  
</mapper>