<?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.hm.TabHaobanHmClerkRelationMapper">

    <resultMap type="com.gic.haoban.manage.service.entity.hm.TabHaobanHmClerkRelation" id="TabHaobanHmClerkRelationMap">
        <result property="relationId" column="relation_id" jdbcType="INTEGER"/>
        <result property="hmId" column="hm_id" jdbcType="INTEGER"/>
        <result property="wxEnterpriseId" column="wx_enterprise_id" jdbcType="VARCHAR"/>
        <result property="enterpriseId" column="enterprise_id" jdbcType="VARCHAR"/>
        <result property="storeId" column="store_id" jdbcType="VARCHAR"/>
        <result property="storeName" column="store_name" jdbcType="VARCHAR"/>
        <result property="storeCode" column="store_code" jdbcType="VARCHAR"/>
        <result property="clerkId" column="clerk_id" jdbcType="VARCHAR"/>
        <result property="clerkName" column="clerk_name" jdbcType="VARCHAR"/>
        <result property="clerkCode" column="clerk_code" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="overFlag" column="over_flag" jdbcType="INTEGER"/>
        <result property="addNum" column="add_num" jdbcType="INTEGER"/>
        <result property="staffId" column="staff_id" jdbcType="VARCHAR"/>
        <result property="staffName" column="staff_name" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        relation_id,hm_id, wx_enterprise_id, enterprise_id, store_id,
         store_name, store_code, clerk_id, clerk_name, clerk_code, status,
         create_time, update_time, over_flag, add_num,staff_id,staff_name
    </sql>


    <!--新增所有列-->
    <insert id="insert" keyProperty="relationId" useGeneratedKeys="true">
        insert into tab_haoban_hm_clerk_relation(relation_id,hm_id, wx_enterprise_id, enterprise_id, store_id,
                                                 store_name, store_code, clerk_id, clerk_name, clerk_code, status,
                                                 create_time, update_time, over_flag, add_num,staff_id,staff_name)
        values (#{relationId},#{hmId},  #{wxEnterpriseId}, #{enterpriseId}, #{storeId}, #{storeName}, #{storeCode},
                #{clerkId}, #{clerkName}, #{clerkCode}, #{status}, #{createTime}, #{updateTime}, #{overFlag}, #{addNum},#{staffId},#{staffName})
    </insert>

    <!--查询单个-->
    <select id="queryByHmIdAndClerkId" resultMap="TabHaobanHmClerkRelationMap">
        select <include refid="Base_Column_List"></include>
        from tab_haoban_hm_clerk_relation
        where clerk_id = #{clerkId}
          and hm_id = #{hmId} and status = 1
        limit 1
    </select>

    <update id="updateAddNumById">
        update tab_haoban_hm_clerk_relation
        <set>
            add_num = add_num +1,
            <if test="overFlag != null">
                over_flag = #{overFlag},
            </if>
            update_time = now()
        </set>
        where relation_id = #{relationId}
    </update>


    <update id="resetAddNumAndOverFlag">
        update tab_haoban_hm_clerk_relation
        <set>
            add_num = 0,over_flag = 0,update_time = now()
        </set>
        where status = 1
    </update>

    <select id="queryByClerkId" resultMap="TabHaobanHmClerkRelationMap">
        select <include refid="Base_Column_List"></include>
        from tab_haoban_hm_clerk_relation
        where clerk_id = #{clerkId} and enterprise_id = #{enterpriseId} and status = 1
    </select>


    <select id="getNumByHmId" resultType="string">
        select clerk_id
        from tab_haoban_hm_clerk_relation
        where hm_id = #{hmId} and status = 1
    </select>


    <select id="getHmClerkByHmId" resultMap="TabHaobanHmClerkRelationMap">
        select <include refid="Base_Column_List"></include>
        from tab_haoban_hm_clerk_relation
        where hm_id = #{hmId} and status = 1
    </select>
    
    <select id="listHmClerkByHmIdList" resultMap="TabHaobanHmClerkRelationMap">
    	select <include refid="Base_Column_List" />
        from tab_haoban_hm_clerk_relation
        where hm_id in 
        <foreach item="id" index="index" collection="hmIdList" open="(" separator="," close=")">
            #{id}
        </foreach>
        and status = 1
    </select>

    <select id="getHmClerkByHmIdAndStaffId" resultMap="TabHaobanHmClerkRelationMap">
        select <include refid="Base_Column_List"></include>
        from tab_haoban_hm_clerk_relation
        where hm_id = #{hmId} and staff_id = #{staffId} and status = 1 limit 1
    </select>

    <update id="updateStatusById">
        update tab_haoban_hm_clerk_relation
        <set>
            status = #{status},update_time = now()
        </set>
        where relation_id = #{relationId}
    </update>

    <select id="queryByHmIdNotInClerkIds" resultMap="TabHaobanHmClerkRelationMap">
        select <include refid="Base_Column_List"></include>
        from tab_haoban_hm_clerk_relation
        where hm_id = #{hmId} and status = 1
          and clerk_id not in
        <foreach item="id" index="index" collection="clerkIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <update id="updateClerkNameByClerkId" parameterType="java.lang.String">
        update tab_haoban_hm_clerk_relation
        <set>
            clerk_name = #{clerkName},clerk_code = #{clerkCode},update_time = now()
        </set>
        where clerk_id = #{clerkId} and enterprise_id = #{enterpriseId}
    </update>

    <update id="updateStoreNameByStoreId" parameterType="java.lang.String">
        update tab_haoban_hm_clerk_relation
        <set>
            store_name = #{storeName},store_code = #{storeCode},update_time = now()
        </set>
        where store_id = #{storeId} and enterprise_id = #{enterpriseId}
    </update>
    
    <update id="updateByClerkChangeStore">
    	 update tab_haoban_hm_clerk_relation set  
           clerk_id = #{clerkId} , clerk_code=#{clerkCode} , clerk_name=#{clerkName} , store_id=#{storeId} , store_name = #{storeName}, store_code = #{storeCode} , update_time = now() 
        where clerk_id = #{oldClerkId} and enterprise_id = #{enterpriseId} and `status`  = 1
    </update>

</mapper>

