<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
 <mapper namespace="com.iamberry.wechat.service.mapper.TemplateInfoMapper">
 
 
  <!-- palceInfo活动所有字段映射 -->
	<sql id="allField">
	 TEMPLATE_ID  templateId,
	 PLACE_ID   placeId,
	 TEMPLATE_IS_GLOGAL  isGlogal,
	 TEMPLATE_NAME  templateName,
	 TEMPLATE_CREATE_DATE  createDate,
	 TEMPLATE_COUNT  count,
	 TEMPLATE_STATE  state
	</sql>
 
     <!-- 用于下拉框选择 -->
    <select id="getTemplateListBySelect"  resultType="TemplateInfo">
     select  TEMPLATE_ID  templateId,TEMPLATE_NAME  templateName
     from tb_iamberry_template
     where TEMPLATE_STATE=1
     </select>
     
     <!-- 按条件获取模板列表 -->
     <select id="getTemplateList" resultType="TemplateInfo" parameterType="TemplateInfo">
     	SELECT  
     		<include refid="allField"/>,
     		(SELECT COUNT(FLOW_ID) FROM TB_IAMBERRY_TEMPLATE_FLOW where TEMPLATE_ID = templateId AND FLOW_STATE = 1) flowNum 
     	FROM 
     		TB_IAMBERRY_TEMPLATE 
     	<where>
     		<if test="templateName != null and templateName != ''">
     			TEMPLATE_NAME LIKE CONCAT('%',#{templateName},'%')
     		</if>
     	</where>
     	LIMIT ${page.recordBegin},${page.pageSize}
     </select>
     
     <!-- 根据条件获取模板表数据数量 -->
     <select id="getTemplateCount" resultType="int" parameterType="TemplateInfo">
     	SELECT  
     		COUNT(TEMPLATE_ID)
     	FROM 
     		TB_IAMBERRY_TEMPLATE 
     	<where>
     		<if test="templateName != null and templateName != ''">
     			TEMPLATE_NAME LIKE CONCAT('%',#{templateName},'%')
     		</if>
     	</where>
     </select>
 
 	<!-- 添加模板数据 -->
 	<insert id="addTemplateInfo" parameterType="TemplateInfo" useGeneratedKeys="true" keyProperty="templateId">
 		INSERT INTO TB_IAMBERRY_TEMPLATE 
 		(
 			PLACE_ID,
 			TEMPLATE_IS_GLOGAL,
 			TEMPLATE_NAME,
 			TEMPLATE_CREATE_DATE,
 			TEMPLATE_COUNT,
 			TEMPLATE_STATE,
 			TEMPLATE_CREATER 
 		)
 		VALUES 
 		(
 			#{placeId},
 			#{isGlogal},
 			#{templateName},
 			NOW(),
 			#{count},
 			#{state},
 			#{createrId}
 		)
 	</insert>
 	
 	<!-- 修改模板信息 -->
 	<update id="updateTemplateInfo" parameterType="TemplateInfo">
 		UPDATE 
 			TB_IAMBERRY_TEMPLATE 
 		SET 
 			TEMPLATE_IS_GLOGAL = #{isGlogal},
 			TEMPLATE_NAME = #{templateName},
 			TEMPLATE_COUNT = #{count},
 			TEMPLATE_STATE = #{state},
 			TEMPLATE_UPDATE_DATE = NOW() 
 		WHERE 
 			TEMPLATE_ID = #{templateId}
 	</update>
 
 	<!-- 根据模板id查询模板信息 -->
 	<select id="getTemplateInfoById" parameterType="TemplateInfo" resultType="TemplateInfo">
 			SELECT 
 				<include refid="allField"/> 
 			FROM 
 				TB_IAMBERRY_TEMPLATE 
 			WHERE 
 				TEMPLATE_ID = #{templateId}
 			<if test="state != null and state != ''">
	 			AND TEMPLATE_STATE = #{state} 
 			</if>
 	</select>
 
 
 </mapper>