<?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.TabHaobanHmQrcodeMapper">

    <resultMap type="com.gic.haoban.manage.service.entity.hm.TabHaobanHmQrcode" id="TabHaobanHmQrcodeMap">
        <result property="hmId" column="hm_id" jdbcType="INTEGER"/>
        <result property="hmCode" column="hm_code" jdbcType="VARCHAR"/>
        <result property="hmType" column="hm_type" jdbcType="INTEGER"/>
        <result property="wxEnterpriseId" column="wx_enterprise_id" jdbcType="VARCHAR"/>
        <result property="enterpriseId" column="enterprise_id" jdbcType="VARCHAR"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="creatorId" column="creator_id" jdbcType="VARCHAR"/>
        <result property="creatorName" column="creator_name" jdbcType="VARCHAR"/>
        <result property="modifierId" column="modifier_id" jdbcType="VARCHAR"/>
        <result property="modifierName" column="modifier_name" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="memberLabelId" column="member_label_id" jdbcType="VARCHAR"/>
        <result property="passFlag" column="pass_flag" jdbcType="INTEGER"/>
        <result property="welcomeId" column="welcome_id" jdbcType="VARCHAR"/>
        <result property="statusFlag" column="status_flag" jdbcType="INTEGER"/>
        <result property="addNum" column="add_num" jdbcType="INTEGER"/>
        <result property="overFlag" column="over_flag" jdbcType="INTEGER"/>
        <result property="wxConfigId" column="wx_config_id" jdbcType="VARCHAR"/>
        <result property="wxQrcode" column="wx_qrcode" 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="hmUserNum" column="hm_user_num" jdbcType="INTEGER"/>
        <result property="hmGroupId" column="hm_group_id" jdbcType="INTEGER"/>
        <result property="staffId" column="staff_id" jdbcType="VARCHAR"/>
        <result property="staffName" column="staff_name" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        hm_id, hm_code, hm_type, wx_enterprise_id, enterprise_id, name, creator_id,
         creator_name, modifier_id, modifier_name, create_time, update_time,
         member_label_id, pass_flag, welcome_id, status_flag, add_num, over_flag,
         wx_config_id, wx_qrcode, store_id, store_name, store_code,
         clerk_id, clerk_name, clerk_code, hm_user_num, hm_group_id,staff_id,staff_name
    </sql>

    <!--查询单个-->
    <select id="queryById" resultMap="TabHaobanHmQrcodeMap">
        select
        <include refid="Base_Column_List"></include>
        from tab_haoban_hm_qrcode
        where hm_id = #{hmId}
    </select>
	
	 <select id="queryByIdList" resultMap="TabHaobanHmQrcodeMap">
        select
        <include refid="Base_Column_List"></include>
        from tab_haoban_hm_qrcode
        where hm_id in 
         <foreach collection="hmIds" open="(" close=")" separator="," item="hmId">
                #{hmId}
         </foreach>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="hmId" useGeneratedKeys="true">
        insert into tab_haoban_hm_qrcode(hm_id, hm_code, hm_type, wx_enterprise_id, enterprise_id, name, creator_id,
                                         creator_name, modifier_id, modifier_name, create_time, update_time,
                                         member_label_id, pass_flag, welcome_id, status_flag, add_num, over_flag,
                                         wx_config_id, wx_qrcode, store_id, store_name, store_code,
                                         clerk_id, clerk_name, clerk_code, hm_user_num, hm_group_id,staff_id,staff_name)
        values (#{hmId}, #{hmCode}, #{hmType}, #{wxEnterpriseId}, #{enterpriseId}, #{name}, #{creatorId},
                #{creatorName},
                #{modifierId}, #{modifierName}, #{createTime}, #{updateTime}, #{memberLabelId}, #{passFlag},
                #{welcomeId}, #{statusFlag}, #{addNum}, #{overFlag}, #{wxConfigId}, #{wxQrcode},
                #{storeId}, #{storeName}, #{storeCode}, #{clerkId}, #{clerkName}, #{clerkCode}, #{hmUserNum},
                #{hmGroupId},#{staffId},#{staffName})
    </insert>

    <insert id="insertBatch" keyProperty="hmId" useGeneratedKeys="true">
        insert into tab_haoban_hm_qrcode(hm_id,hm_code, hm_type, wx_enterprise_id, enterprise_id, name, creator_id,
        creator_name, modifier_id, modifier_name, create_time, update_time, member_label_id, pass_flag, welcome_id,
        status_flag, add_num, over_flag, wx_config_id, wx_qrcode, store_id, store_name, store_code,
        clerk_id, clerk_name, clerk_code, hm_user_num, hm_group_id,staff_id,staff_name)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.hmId},#{entity.hmCode}, #{entity.hmType}, #{entity.wxEnterpriseId}, #{entity.enterpriseId},
            #{entity.name},
            #{entity.creatorId}, #{entity.creatorName}, #{entity.modifierId}, #{entity.modifierName},
            #{entity.createTime}, #{entity.updateTime}, #{entity.memberLabelId}, #{entity.passFlag},
            #{entity.welcomeId}, #{entity.statusFlag}, #{entity.addNum}, #{entity.overFlag}, #{entity.wxConfigId},
            #{entity.wxQrcode}, #{entity.storeId}, #{entity.storeName}, #{entity.storeCode},
            #{entity.clerkId}, #{entity.clerkName}, #{entity.clerkCode}, #{entity.hmUserNum}, #{entity.hmGroupId},
            #{entity.staffId},#{entity.staffName})
        </foreach>
    </insert>


    <!--通过主键修改数据-->
    <update id="update">
        update tab_haoban_hm_qrcode
        <set>
            <if test="hmCode != null and hmCode != ''">
                hm_code = #{hmCode},
            </if>
            <if test="hmType != null">
                hm_type = #{hmType},
            </if>
            <if test="wxEnterpriseId != null and wxEnterpriseId != ''">
                wx_enterprise_id = #{wxEnterpriseId},
            </if>
            <if test="enterpriseId != null and enterpriseId != ''">
                enterprise_id = #{enterpriseId},
            </if>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="creatorId != null and creatorId != ''">
                creator_id = #{creatorId},
            </if>
            <if test="creatorName != null and creatorName != ''">
                creator_name = #{creatorName},
            </if>
            <if test="modifierId != null and modifierId != ''">
                modifier_id = #{modifierId},
            </if>
            <if test="modifierName != null and modifierName != ''">
                modifier_name = #{modifierName},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="memberLabelId != null">
                member_label_id = #{memberLabelId},
            </if>
            <if test="passFlag != null">
                pass_flag = #{passFlag},
            </if>
            <if test="welcomeId != null">
                welcome_id = #{welcomeId},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag},
            </if>
            <if test="addNum != null">
                add_num = #{addNum},
            </if>
            <if test="overFlag != null">
                over_flag = #{overFlag},
            </if>
            <if test="wxConfigId != null and wxConfigId != ''">
                wx_config_id = #{wxConfigId},
            </if>
            <if test="wxQrcode != null and wxQrcode != ''">
                wx_qrcode = #{wxQrcode},
            </if>
            <if test="storeId != null and storeId != ''">
                store_id = #{storeId},
            </if>
            <if test="storeName != null and storeName != ''">
                store_name = #{storeName},
            </if>
            <if test="storeCode != null and storeCode != ''">
                store_code = #{storeCode},
            </if>
            <if test="clerkId != null and clerkId != ''">
                clerk_id = #{clerkId},
            </if>
            <if test="clerkName != null and clerkName != ''">
                clerk_name = #{clerkName},
            </if>
            <if test="clerkCode != null and clerkCode != ''">
                clerk_code = #{clerkCode},
            </if>
            <if test="hmUserNum != null">
                hm_user_num = #{hmUserNum},
            </if>
            <if test="hmGroupId != null">
                hm_group_id = #{hmGroupId},
            </if>
            <if test="staffId != null">
                staff_id = #{staffId},
            </if>
            <if test="staffName != null">
                staff_name = #{staffName},
            </if>
        </set>
        where hm_id = #{hmId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from tab_haoban_hm_qrcode
        where hm_id = #{hmId}
    </delete>


    <update id="updateStatusById">
        update tab_haoban_hm_qrcode
        <set>
            status_flag = #{statusFlag},
            update_time = now()
        </set>
        where hm_id = #{hmId}
    </update>

    <select id="listByStoreId" resultMap="TabHaobanHmQrcodeMap">
        select
        <include refid="Base_Column_List"></include>
        from tab_haoban_hm_qrcode
        where store_id = #{storeId} and status_flag in (1,3) and wx_enterprise_id = #{wxEnterpriseId}
        <if test="overFlag != null">
            and over_flag = #{overFlag}
        </if>
        <if test="hmType != null">
            and hm_type = #{hmType}
        </if>
    </select>

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

    <update id="resetAddNumAndOverFlag">
        update tab_haoban_hm_qrcode
        <set>
            add_num = 0,over_flag = 0,update_time = now()
        </set>
        where status_flag in (1,3)
    </update>


    <select id="queryByClerkId" resultMap="TabHaobanHmQrcodeMap">
        select
        <include refid="Base_Column_List"></include>
        from tab_haoban_hm_qrcode
        where clerk_id = #{clerkId} and status_flag in (1,3) and wx_enterprise_id = #{wxEnterpriseId} limit 1
    </select>

    <select id="queryByClerkIdAndOverFlag" resultMap="TabHaobanHmQrcodeMap">
        select
        <include refid="Base_Column_List"></include>
        from tab_haoban_hm_qrcode
        where clerk_id = #{clerkId} and status_flag = 1
        <if test="null != wxEnterpriseId">
	        and wx_enterprise_id = #{wxEnterpriseId}
        </if>
        <if test="hmType != null">
            and hm_type = #{hmType}
        </if>
        limit 1
    </select>

    <select id="queryByStaffIdAndOverFlag" resultType="Long">
        select
        hm_id as hmId
        from tab_haoban_hm_qrcode
        where staff_id = #{staffId} and status_flag = 1
        <if test="null != wxEnterpriseId">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
        <if test="hmType != null">
            and hm_type = #{hmType}
        </if>
    </select>


    <select id="getHmStoreByEnterpriseId" resultMap="TabHaobanHmQrcodeMap">
        select store_id storeId,
               MIN(over_flag) overFlag
        from tab_haoban_hm_qrcode
        where 1 
         <if test="null != wxEnterpriseId">
          and	wx_enterprise_id = #{wxEnterpriseId}
         </if>
         and enterprise_id = #{enterpriseId}  and status_flag = 1
          and store_id is not null
          GROUP BY store_id
    </select>
    
     <select id="getHmStoreForWxa" resultType="String">
        select distinct(store_id) 
        from tab_haoban_hm_qrcode 
        where 1
        <if test="null != wxEnterpriseId">
        	and wx_enterprise_id = #{wxEnterpriseId}
        </if>
        and enterprise_id = #{enterpriseId} and status_flag = 1 and over_flag = 0 and store_id is not null
    </select>
    
	<select id="listHmIdForIndex" resultType="String" parameterType="com.gic.haoban.manage.api.dto.qdto.hm.HmQrcodeListQDTO">
		select distinct(a.hm_id) 
        from tab_haoban_hm_qrcode a left join tab_haoban_hm_clerk_relation b on a.hm_id = b.hm_id and b.status = 1 
        where a.enterprise_id = #{enterpriseId} and a.status_flag != 0
        <if test="wxEnterpriseId != null and wxEnterpriseId != ''">
            and a.wx_enterprise_id = #{wxEnterpriseId}
        </if>
        <if test="null != hmSelect and '' != hmSelect ">
        	and (a.hm_id=#{hmSelect} or a.hm_code=#{hmSelect} or a.name like #{hmSelectLike} or a.creator_name like #{hmSelectLike} )
        </if>
        <if test="null != clerkSelect and '' != clerkSelect">
        	and ( a.clerk_id = #{clerkSelect}  or  b.clerk_id = #{clerkSelect} )
        </if>
        <if test="null != storeSelect and '' != storeSelect">
        	and ( a.store_id = #{storeSelect} or b.store_id = #{storeSelect} )
        </if>
        <if test="storeId != null and storeId.size > 0">
            and ( a.store_id  IN
		            <foreach collection="storeId" item="id" index="index" open="(" close=")" separator=",">
		                #{id}
		            </foreach> 
            	  or b.store_id  IN
		            <foreach collection="storeId" item="id" index="index" open="(" close=")" separator=",">
		                #{id}
		            </foreach>
            )
        </if>
        <if test="null != statusFlagInt and statusFlagInt !=10">
            and a.status_flag = #{statusFlagInt}
        </if>
        <if test="null != statusFlagInt and statusFlagInt ==10">
            and a.status_flag in (1,3)
        </if>
        <if test="null != hmTypeInt">
        	and a.hm_type=#{hmTypeInt}  
        </if>
        <if test="null != hmGroupId">
        	and a.hm_group_id=#{hmGroupId}  
        </if>
        <if test="null != startTime">
        	and a.create_time >=#{startTime} and a.create_Time <![CDATA[<=]]>  #{endTime}
        </if>
	
	</select>
	
    <select id="getGroupIdNumByEnterpriseId" resultType="com.gic.haoban.manage.service.pojo.bo.hm.HmQrcodeGroupNumBO">
        select hm_group_id as hmGroupId,
               COUNT(hm_id) as num
        from tab_haoban_hm_qrcode
        where status_flag != 0
          and enterprise_id = #{enterpriseId}
        <if test="wxEnterpriseId != null and wxEnterpriseId != ''">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
        <if test="groupIds != null and groupIds.size > 0">
            and hm_group_id IN
            <foreach collection="groupIds" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="hmIds != null and hmIds.size > 0">
            and hm_id in
            <foreach collection="hmIds" open="(" close=")" separator="," item="hmId">
                #{hmId}
            </foreach>
        </if>
        GROUP BY hm_group_id
    </select>

    <select id="getTotalByEnterpriseId" resultType="int">
        select COUNT(*)
        from tab_haoban_hm_qrcode
        where status_flag != 0
          and enterprise_id = #{enterpriseId}
        <if test="wxEnterpriseId != null and wxEnterpriseId != ''">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
        <if test="hmIds != null and hmIds.size > 0">
            and hm_id in
            <foreach collection="hmIds" open="(" close=")" separator="," item="hmId">
                #{hmId}
            </foreach>
        </if>
    </select>


    <update id="batchUpdateQrCodeGroup">
        update tab_haoban_hm_qrcode
        set hm_group_id = #{groupId},
            update_time = now()
        where hm_id in
        <foreach collection="ids" open="(" close=")" item="id" separator=",">
            #{id}
        </foreach>
    </update>
    
    <select id="getCanAddCount" resultType="int">
    	select count(*) from 
    	<include refid="canAddClerkSql" />
    </select>
    
    <select id="listCanAddClerk" resultType="com.gic.haoban.manage.service.pojo.bo.hm.HmQrcodeClerkBO">
    	select a.clerk_id clerkId,
    	       a.store_id storeId,
    	       a.staff_id staffId
    	from
    	<include refid="canAddClerkSql" />
    </select>
    
    <sql id="canAddClerkSql">
		tab_haoban_staff_clerk_relation a  LEFT JOIN tab_haoban_hm_qrcode b  ON a.clerk_id = b.clerk_id and b.hm_type = 1 and b.status_flag in (1,3)
			left join tab_haoban_staff c on a.staff_id = c.staff_id 
		WHERE  a.enterprise_id = #{enterpriseId} AND a.status_flag = 1 AND a.open_concat_flag = 1 and c.status_flag = 1 
		<if test="null != storeIdList">
			 and a.store_id in 
			<foreach collection="storeIdList" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
		</if>
		and a.wx_enterprise_id = #{wxEnterpriseId}
		and b.hm_id is null
	</sql>
	
	<select id="listHasQrcodeClerk" resultType="java.lang.String">
		select clerk_id from tab_haoban_hm_qrcode where enterprise_id=#{enterpriseId}  
			 and clerk_id in 
			<foreach collection="clerkIdList" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
            and hm_type = 1 and status_flag in (1,3)
	</select>

    <select id="listClerkQrcode" resultType="com.gic.haoban.manage.api.dto.hm.ClerkQrcodeDTO">
        select clerk_id clerkId ,  wx_qrcode qwQrcode from tab_haoban_hm_qrcode where enterprise_id=#{enterpriseId}
        and clerk_id in
        <foreach collection="clerkIdList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        and hm_type = 1 and status_flag in (1,3)
    </select>


    <select id="getClerkQrcode" resultType="java.lang.String">
		select wx_qrcode from tab_haoban_hm_qrcode where enterprise_id=#{enterpriseId}  
			 and clerk_id = #{clerkId} 
            and hm_type = 1 and status_flag = 1 limit 1
	</select>
	
    <update id="updateClerkNameByClerkId" parameterType="java.lang.String">
         update tab_haoban_hm_qrcode
         <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_qrcode
        <set>
            store_name = #{storeName},store_code = #{storeCode},update_time = now()
        </set>
        where store_id = #{storeId} and enterprise_id = #{enterpriseId}
    </update>

</mapper>

