<?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.TabHaobanStoreRelationMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabStoreRelation">
        <id column="store_relation_id" jdbcType="VARCHAR" property="storeRelationId"/>
        <result column="wx_enterprise_id" jdbcType="VARCHAR" property="wxEnterpriseId"/>
        <result column="store_id" jdbcType="VARCHAR" property="storeId"/>
        <result column="store_name" jdbcType="VARCHAR" property="storeName"/>
        <result column="store_group_id" jdbcType="VARCHAR" property="storeGroupId"/>
        <result column="enterprise_id" jdbcType="VARCHAR" property="enterpriseId"/>
        <result column="status_flag" jdbcType="INTEGER" property="statusFlag"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        store_relation_id, wx_enterprise_id, store_id, store_group_id, enterprise_id, status_flag, store_name,
    create_time, update_time
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_store_relation
        where store_relation_id = #{storeRelationId,jdbcType=VARCHAR}
    </select>
    
    
    <insert id="insertBatch">
        insert into tab_haoban_store_relation (store_relation_id, wx_enterprise_id,
        store_id,store_name, store_group_id, enterprise_id,
        status_flag, create_time, update_time
        )
        values
        <foreach collection="list" separator="," item="item">
            (#{item.storeRelationId,jdbcType=VARCHAR}, #{item.wxEnterpriseId,jdbcType=VARCHAR},
            #{item.storeId,jdbcType=VARCHAR},#{item.storeName,jdbcType=VARCHAR}, #{item.storeGroupId,jdbcType=VARCHAR},
            #{item.enterpriseId,jdbcType=VARCHAR},
            1, now(), now()
            )
        </foreach>
    </insert>

    <select id="searchStoreRange" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_store_relation
        where enterprise_id = #{enterpriseId,jdbcType=VARCHAR}
        and wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        <if test="search!=null">
            and store_name like concat("%",#{search},"%")
        </if>
        and status_flag=1
    </select>

    <select id="listStoreIdByWxEnterpriseId" resultType="String">
        select
        store_id
        from tab_haoban_store_relation
        where
        wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
        <if test="enterpriseId!=null">
            and enterprise_id = #{enterpriseId,jdbcType=VARCHAR}
        </if>
        and status_flag=1
    </select>

    <select id="listStoreRange" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_store_relation
        where enterprise_id = #{enterpriseId,jdbcType=VARCHAR}
        and status_flag=1
    </select>

    <update id="deleteStoreRalationByStoreIds">
        update tab_haoban_store_relation
        set
        status_flag = 0,
        update_time = now()
        where enterprise_id=#{enterpriseId}
        and store_id in
        <foreach collection="storeIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        and status_flag=1
    </update>

    <update id="updateStoreName">
        update tab_haoban_store_relation
        set store_name  = #{storeName},
            update_time = now()
        where enterprise_id = #{enterpriseId}
          and store_id = #{storeId}
          and status_flag = 1
    </update>

    <select id="listStoreIdByStoreIdWxEnterpriseId" resultType="String">
        select store_id
        from tab_haoban_store_relation
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
          and enterprise_id = #{enterpriseId,jdbcType=VARCHAR}
          and store_id = #{storeId}
          and status_flag = 1
    </select>

    <select id="listStoreGroupIdByWxEnterpriseId" resultType="String">
        select store_group_id
        from tab_haoban_store_relation
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
          and enterprise_id = #{enterpriseId,jdbcType=VARCHAR}
          and status_flag = 1 group by store_group_id
    </select>
    
    <select id="listStoreIdByName" resultType="String">
        select store_id
        from tab_haoban_store_relation
        where wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR}
          and enterprise_id = #{enterpriseId,jdbcType=VARCHAR}
          and store_name like CONCAT('%',#{storeName},'%')
          and status_flag = 1
    </select>

</mapper>