<?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.HmLinkMapper">
  <resultMap type="com.gic.haoban.manage.service.entity.hm.TabHmLink" id="result-map-tabHaobanHmLink">
      <result column="link_id"  property="linkId"/>
      <result column="link_code"  property="linkCode"/>
      <result column="link_type"  property="linkType"/>
      <result column="wx_enterprise_id"  property="wxEnterpriseId"/>
      <result column="enterprise_id"  property="enterpriseId"/>
      <result column="name"  property="name"/>
      <result column="remark"  property="remark"/>
      <result column="welcome_id"  property="welcomeId"/>
      <result column="page_id"  property="pageId"/>
      <result column="creator_id"  property="creatorId"/>
      <result column="creator_name"  property="creatorName"/>
      <result column="modifier_id"  property="modifierId"/>
      <result column="modifier_name"  property="modifierName"/>
      <result column="create_time"  property="createTime"/>
      <result column="update_time"  property="updateTime"/>
      <result column="store_rule_json"  property="storeRuleJson"/>
      <result column="custom_rule_json"  property="customRuleJson"/>
      <result column="member_label_id"  property="memberLabelId"/>
      <result column="status_flag"  property="statusFlag"/>
      <result column="short_code" property="shortCode" />
      <result column="other_chat_hm_id" property="otherChatHmId"/>
  </resultMap>
  <sql id="Base_Column_List">
	  link_id,
	  link_code,
	  link_type,
	  wx_enterprise_id,
	  enterprise_id,
	  name,
	  remark,
	  welcome_id,
	  page_id,
	  creator_id,
	  creator_name,
	  modifier_id,
	  modifier_name,
	  create_time,
	  update_time,
	  store_rule_json,
	  custom_rule_json,
	  member_label_id,
	  status_flag,short_code , other_chat_hm_id
  </sql>
  <!-- ===================== 新增 ======================== -->
  <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.hm.TabHmLink">
  <![CDATA[
  INSERT INTO tab_haoban_hm_link(
	  link_id,
	  link_code,
	  link_type,
	  wx_enterprise_id,
	  enterprise_id,
	  name,
	  remark,
	  welcome_id,
	  page_id,
	  creator_id,
	  creator_name,
	  modifier_id,
	  modifier_name,
	  create_time,
	  update_time,
	  store_rule_json,
	  custom_rule_json,
	  member_label_id,
	  status_flag , short_code , other_chat_hm_id
  )VALUES(
	  #{linkId,jdbcType=BIGINT},
	  #{linkCode,jdbcType=VARCHAR},
	  #{linkType,jdbcType=INTEGER},
	  #{wxEnterpriseId,jdbcType=CHAR},
	  #{enterpriseId,jdbcType=CHAR},
	  #{name,jdbcType=VARCHAR},
	  #{remark,jdbcType=VARCHAR},
	  #{welcomeId,jdbcType=VARCHAR},
	  #{pageId,jdbcType=BIGINT},
	  #{creatorId,jdbcType=VARCHAR},
	  #{creatorName,jdbcType=VARCHAR},
	  #{modifierId,jdbcType=VARCHAR},
	  #{modifierName,jdbcType=VARCHAR},
	  now(),
	  now(),
	  #{storeRuleJson,jdbcType=VARCHAR},
	  #{customRuleJson,jdbcType=VARCHAR},
	  #{memberLabelId,jdbcType=VARCHAR},
	  #{statusFlag,jdbcType=INTEGER} , #{shortCode} , #{otherChatHmId}
  )
  ]]>
  </insert>

   <!-- ==================更新 ========== -->
  <update id="update" parameterType="com.gic.haoban.manage.service.entity.hm.TabHmLink">
	  <![CDATA[
		  UPDATE tab_haoban_hm_link SET
			  name=#{name,jdbcType=VARCHAR},
			  remark=#{remark,jdbcType=VARCHAR},
			  welcome_id=#{welcomeId,jdbcType=VARCHAR},
			  page_id=#{pageId,jdbcType=BIGINT},
			  modifier_id=#{modifierId,jdbcType=VARCHAR},
			  modifier_name=#{modifierName,jdbcType=VARCHAR},
			  update_time=now(),
			  store_rule_json=#{storeRuleJson,jdbcType=VARCHAR},
			  custom_rule_json=#{customRuleJson,jdbcType=VARCHAR},
			  member_label_id=#{memberLabelId,jdbcType=VARCHAR} , other_chat_hm_id = #{otherChatHmId}
		  WHERE link_id = #{linkId}
	  ]]>
  </update>

  <!-- ============ 查询=============-->
  <select id="selectById" parameterType="long" resultMap="result-map-tabHaobanHmLink">
  SELECT <include refid="Base_Column_List" /> FROM tab_haoban_hm_link WHERE link_id = #{linkId} and status_flag = 1
  </select>

  <select id="selectByShortCode" resultMap="result-map-tabHaobanHmLink">
    SELECT <include refid="Base_Column_List" /> FROM tab_haoban_hm_link WHERE   short_code=#{shortCode} and status_flag = 1
  </select>

  <select id="listForSms"  resultMap="result-map-tabHaobanHmLink">
  	select <include refid="Base_Column_List" /> from tab_haoban_hm_link where wx_enterprise_id=#{wxEnterpriseId} and enterprise_id=#{enterpriseId}
  	<if test="null != linkName">
  		and (name like '%${linkName}%' or short_code like '%${linkName}%')
  	</if>
  	<if test="null != linkType">
  		and link_type = #{linkType}
  	</if>
  	<if test="null != creatorIdList and creatorIdList.size>0">
  		and creator_id in 
  		<foreach collection="creatorIdList" open="(" close=")" index="index" item="id" separator=",">
			#{id}  			
  		</foreach>
  	</if>
  	and status_flag = 1 order by create_time desc
  	limit 20
  </select>


   <select id="queryPageRelationList" resultMap="result-map-tabHaobanHmLink">
       select <include refid="Base_Column_List"/>
       from tab_haoban_hm_link where  page_id = #{pageId} and enterprise_id=#{enterpriseId}
	<if test="searchText != null and searchText != '' ">
		and ( link_code = #{searchText} or name like CONCAT('%', #{searchText},'%'))
	</if>
   </select>

</mapper> 