<?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.TabHaobanClerkMainStoreRelatedMapper" >
  <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanClerkMainStoreRelated" >
    <id column="clerk_main_store_related_id" property="clerkMainStoreRelatedId" jdbcType="VARCHAR" />
    <result column="staff_id" property="staffId" jdbcType="VARCHAR" />
    <result column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR" />
    <result column="store_id" property="storeId" jdbcType="VARCHAR" />
    <result column="main_store_flag" property="mainStoreFlag" jdbcType="INTEGER" />
    <result column="fresh_friend_flag" property="freshFriendFlag" 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" />
  </resultMap>
  <sql id="Base_Column_List" >
    clerk_main_store_related_id, staff_id, wx_enterprise_id, store_id, main_store_flag, fresh_friend_flag,
    status_flag, create_time, update_time
  </sql>
  
  
  <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.TabHaobanClerkMainStoreRelated" >
    insert into tab_haoban_clerk_main_store_related (clerk_main_store_related_id, staff_id,
    wx_enterprise_id, store_id, main_store_flag, fresh_friend_flag,
      status_flag, create_time, update_time
      )
    values (#{clerkMainStoreRelatedId,jdbcType=VARCHAR}, #{staffId,jdbcType=VARCHAR},
    #{wxEnterpriseId,jdbcType=VARCHAR}, #{storeId,jdbcType=VARCHAR}, #{mainStoreFlag,jdbcType=INTEGER},
    #{freshFriendFlag},
      #{statusFlag,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
      )
  </insert>
  
  <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanClerkMainStoreRelated" >
    update tab_haoban_clerk_main_store_related
    <set >
      <if test="staffId != null" >
        staff_id = #{staffId,jdbcType=VARCHAR},
      </if>
      <if test="wxEnterpriseId != null" >
        wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
      </if>
      <if test="storeId != null" >
        store_id = #{storeId,jdbcType=VARCHAR},
      </if>
      <if test="mainStoreFlag != null" >
        main_store_flag = #{mainStoreFlag,jdbcType=INTEGER},
      </if>
      <if test="freshFriendFlag != null">
        fresh_friend_flag = #{freshFriendFlag,jdbcType=INTEGER},
      </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>
    </set>
    where clerk_main_store_related_id = #{clerkMainStoreRelatedId,jdbcType=VARCHAR}
  </update>
  
  
  <select id="selectByWxEnterpriseIdAndStoreId" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from tab_haoban_clerk_main_store_related
    where staff_id = #{staffId,jdbcType=VARCHAR}
    and wx_enterprise_id = #{wxEnterpriseId}
    and status_flag = 1
    <if test="null != storeIdList">
     and store_id IN
    <foreach collection="storeIdList" open="(" close=")" separator="," item="item">
      #{item}
    </foreach>
    </if>
    limit 1
  </select>


  <update id="delMainStore">
    update tab_haoban_clerk_main_store_related
    set
      status_flag = 0,
      update_time = now()
    where
       staff_id=#{staffId}
       and store_id=#{storeId}
       and wx_enterprise_id = #{wxEnterpriseId}
       and status_flag = 1
  </update>
  
   <update id="delStaffMainStore">
    update tab_haoban_clerk_main_store_related
    set
      status_flag = 0,
      update_time = now()
    where
       staff_id=#{staffId}
       and wx_enterprise_id = #{wxEnterpriseId}
       and status_flag = 1
  </update>
  
  <update id="delByStoreIds">
    update tab_haoban_clerk_main_store_related
    set
    status_flag = 0,
    update_time = now()
    where status_flag = 1
    and store_id IN
    <foreach collection="storeIds" open="(" close=")" separator="," item="item">
      #{item}
    </foreach>
  </update>

  <select id="getLastNoStatusByStaffId" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from tab_haoban_clerk_main_store_related
    where wx_enterprise_id = #{wxEnterpriseId}
    and staff_id = #{staffId}
    order by create_time desc limit 1
  </select>

  <select id="listByStaffIdList" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from tab_haoban_clerk_main_store_related
    where  staff_id in
    <foreach collection="list"  open="(" close=")" separator="," item="item">
      #{item}
    </foreach>
     and status_flag = 1
  </select>
</mapper>