<?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.TemplateFlowInfoMapper">
 	<sql id="flowAllField">
 		FLOW_ID flowId,
 		TEMPLATE_ID templateId,
 		FLOW_LEVEL flowLevel,
 		FLOW_SKIP_STEP flowSkipStep,
 		FLOW_MONEY flowMoney,
 		FLOW_ROLE_ID flowRoleId,
 		FLOW_TYPE flowType,
 		FLOW_CREATE_DATE flowCreateDate,
 		FLOW_UPDATE_DATE flowUpdateDate,
 		FLOW_STATE flowState,
 		FLOW_NO flowNo,
 		FLOW_PRODUCT_TYPE flowProductType
 	</sql>
 	<select id="getTemplateFlowInfo" parameterType="java.util.Map" resultType="TemplateFlowInfo">
 		SELECT 
 			<include refid="flowAllField"/>
 		FROM 
 			TB_IAMBERRY_TEMPLATE_FLOW 
 		WHERE 
 			TEMPLATE_ID = #{templateId} 
 		AND 
 			FLOW_LEVEL = #{flowLevel} 
 		AND 
 			FLOW_TYPE = #{flowType} 
 		AND 
 			FLOW_PRODUCT_TYPE = #{flowProductType} 
 		<if test="flowRoleId!=null">
     		AND FLOW_ROLE_ID = #{flowRoleId} 
 		</if>
 		LIMIT 0,1
 	</select>
 	
 	<select id="getTemplateFlowInfos" parameterType="TemplateFlowInfo" resultType="TemplateFlowInfo">
 		SELECT 
 			<include refid="flowAllField"/>,
 			FLOW_PRODUCT_TYPE flowProductType
 		FROM 
 			TB_IAMBERRY_TEMPLATE_FLOW 
 		WHERE 
 			TEMPLATE_ID = #{templateId} 
 		AND 
 			FLOW_LEVEL = #{flowLevel} 
 		AND 
 			FLOW_TYPE = #{flowType} 
 		<if test="flowRoleId!=null">
     		AND FLOW_ROLE_ID = #{flowRoleId} 
 		</if>
 		AND 
 		   FLOW_PRODUCT_TYPE in ${flowProductType}
 	</select>
 	
 	<select id="getFlowInfoListByTemplateId" parameterType="Integer" resultType="TemplateFlowInfo">
 	   SELECT 
 			<include refid="flowAllField"/>
 		FROM 
 			TB_IAMBERRY_TEMPLATE_FLOW 
 		 WHERE 
 			TEMPLATE_ID = #{templateId} 
 		AND 
 			FLOW_STATE = 1 
 	</select>
 	
 	 	
 	<insert id="addTemplateFlowList" parameterType="java.util.List">  
    	INSERT INTO TB_IAMBERRY_TEMPLATE_FLOW 
    	(
    		TEMPLATE_ID,
    		FLOW_LEVEL,
    		FLOW_SKIP_STEP,
    		FLOW_MONEY,
    		FLOW_ROLE_ID,
    		FLOW_TYPE,
    		FLOW_CREATE_DATE,
    		FLOW_STATE,
    		FLOW_NO,
    		FLOW_PRODUCT_TYPE
    	)  
    	VALUES   
	    <foreach collection="list" item="item" index="index" separator="," >
	        (#{item.templateId},#{item.flowLevel},#{item.flowSkipStep},#{item.flowMoney},
	        #{item.flowRoleId},#{item.flowType},NOW(),#{item.flowState},#{item.flowNo}, #{item.flowProductType})
	    </foreach>  
	</insert>
	
	<!-- 批量修改返利模板数据 -->
	<update id="updateTemplateFlowInfo"  parameterType="java.util.List">
	          <foreach collection="list" item="item" index="index" separator=";" open="" close="">
	                UPDATE  TB_IAMBERRY_TEMPLATE_FLOW SET 
	                  	FLOW_MONEY= #{item.flowMoney,jdbcType=INTEGER},
	                  	FLOW_LEVEL = #{item.flowLevel,jdbcType=INTEGER},
	                  	FLOW_ROLE_ID = #{item.flowRoleId,jdbcType=VARCHAR},
	                  	FLOW_TYPE = #{item.flowType,jdbcType=INTEGER},
	                  	FLOW_UPDATE_DATE = NOW(),
	                  	FLOW_STATE = #{item.flowState,jdbcType=INTEGER} 
	                WHERE 
                		TEMPLATE_ID = #{item.templateId,jdbcType=INTEGER} 
	                AND 
	                	FLOW_NO = #{item.flowNo,jdbcType=VARCHAR}
	                AND
	                	FLOW_PRODUCT_TYPE = #{item.flowProductType,jdbcType=INTEGER} 
	         </foreach>
	 </update>
 	
 	<!-- 根据模板ID获取已经保存的数据 -->
 	<select id="getTypeId" parameterType="java.lang.Integer" resultType="int">
 		SELECT DISTINCT FLOW_PRODUCT_TYPE FROM TB_IAMBERRY_TEMPLATE_FLOW WHERE TEMPLATE_ID = #{tid} AND NOT (FLOW_PRODUCT_TYPE IS NULL);
 	</select>
 	
 </mapper>