<?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.ProbationShopOrderMapper">
  <!-- 试用订单表 -->
 <!--  <sql id="allProbation">
		probation_orderid  probationOrderid,
		probation_product_id  probationProductId,
		probation_openid  probationOpenid,
		probation_yet_amount  probationYetAmount,
		probation_amount  probationAmount,
		probation_create_time  probationCreateTime,
		probation_post_num  probationPostNum,
		probation_post_firm  probationPostFirm,
		probation_status  probationStatus,
		probation_send_date  probationSendDate,
		probation_address_info  probationAddressInfo,
		probation_address_postnum  probationAddressPostnum,
		probation_address_tel  probationAddressTel,
		probation_address_name  probationAddressName,
		probation_admin_remark  probationAdminRemark,
		probation_user_remark  probationUserRemark,
		probation_transaction_id  probationTransactionId,
		probation_transaction_time  probationTransactionTime,
		probation_start_time  probationStartTime,
		probation_end_time  probationStartTime,
		probation_update_time  probationUpdateTime,
		probation_pause_start_time probationPauseStartTime,
		probation_pause_end_time probationPauseEndTime 
	</sql> -->
	
	<sql id="probationOrderDto">
		probation_orderid probationOrderid,
		probation_product_id  probationProductId,
		probation_openid  probationOpenid,
		probation_yet_amount  probationYetAmount,
		probation_receive_tel probationReceiveTel,
		probation_receive_name probationReceiveName,
		probation_address_info probationAddressInfo,
		probation_status  probationStatus,
		probation_user_remark probationUserRemark,
		probation_update_time probationUpdateTime,
		probation_create_time probationCreateTime,
		probation_start_time probationStartTime,
		probation_end_time probationEndTime,
		probation_pause_start_time probationPauseStartTime,
		probation_pause_end_time probationPauseEndTime,
		product_introduce_img productIntroduceImg,
		product_name productName,
		probation_pause_total_length probationPauseTotalLength,
		probation_color_type probationColorType 
	</sql>
	<!-- 订单奖励规则表 -->
	<sql id="probationAwardRelu">
		award_id awardId,
		award_num awardNum,
		award_info awardInfo,
		award_info_two awardInfoTwo,
		award_status awardStatus,
		award_create_time awardCreateTime
	</sql>
	
	<!-- 订单推送消息日志表 -->
	<sql id="probationNewLogs">
		logs_id logsId,
		logs_porderid logsPorderid,
		logs_time logsTime,
		logs_create_time logsCreateTime
	</sql>
	<!-- 订单推送验证码表 -->
	<sql id="probationCode">
		puser_code_id puserCodeId,
		puser_tel puserTel,
		puser_code puserCode,
		puser_date puserDate
	</sql>
	<!-- 试用人信息表 -->
	<sql id="probationInfo">
		puser_id puserId,
		puser_openid puserPpenid,
		puser_name puserName,
		puser_tel puserTel,
		puser_address  puserAddress,
		puser_send_name puserSend_name,
		puser_send_tel puserSendTel,
		puser_productid puserProductid,
		puser_receive_name puserReceiveName,
		puser_receive_tel puserReceiveTel,
		puser_create_time puserCreateTime
	</sql>
	
  <!-- 根据用户openID 查询会员试用订单 -->
  <select id="selectProbationOrderByOpenid" parameterType="String" resultType="ProbationOrderDto">
  		select <include refid="probationOrderDto"/> from tb_iamberry_probation_shop_order  o 
  		left join 
  		tb_iamberry_product_info f on o.probation_product_id = f.product_id
  		where 
  		o.probation_openid = #{openId}
  </select>
  
   <!-- 查询试用订单奖励规则 -->
  <select id="selectProbationAwardRelu"  resultType="ProbationAwardRelu">
  		select 
  			<include refid="probationAwardRelu"/> 
  		from tb_iamberry_probation_award_relu 
  		where award_status = 1
  		order by award_num
  </select>
  
  <!-- 查询所有试用开始订单 -->
  <select id="selectProbationStartOrder"  resultType="ProbationOrderDto">
  		select 
  			<include refid="probationOrderDto"/> 
  		from tb_iamberry_probation_shop_order o
  		left join tb_iamberry_product_info f on o.probation_product_id = f.product_id
  		where o.probation_status = 3
  </select>
  
  <!-- 根据订单ID查询 发放奖励阶段 -->
  <select id="selectProbationNewLogsByOrderId" parameterType="String" resultType="ProbationNewLogs">
  		select <include refid="probationNewLogs"/> from tb_iamberry_probation_new_logs 
  		where 
  		logs_porderid = #{logsOrderid}
  </select>
  
  <!-- 添加奖励推送日志 -->
  <insert id="insertProbationLogs" parameterType="ProbationNewLogs">
  		insert  into tb_iamberry_probation_new_logs 
		 		(logs_porderid,logs_time,logs_create_time)
		 values 
		  	( 
			  #{logsPorderid},#{logsTime},#{logsCreateTime}
		  	)
  </insert>
  <update id="updatePauseLogsDate" parameterType="ProbationNewLogs">
  		 		update  tb_iamberry_probation_new_logs 
  		 			set 
	                  	logs_time= #{logsTime}
	                where 
                		logs_porderid = #{logsPorderid} 
  </update>
  
   <!-- 根据手机号查询验证码信息 -->
  <select id="selectProbationCodeByTel" parameterType="String" resultType="ProbationCode">
  		select <include refid="probationCode"/> from tb_iamberry_probation_code  
  		where 
  		puser_tel = #{puserTel}
  </select>
   <!-- 添加奖励推送code -->
  <insert id="insertProbationCode" parameterType="ProbationCode">
  		insert  into tb_iamberry_probation_code 
		 		(puser_tel,puser_code,puser_date)
		 values 
		  	( 
			  #{puserTel},#{puserCode},#{puserDate}
		  	)
  </insert>
  <!-- 修改奖励推送code -->
  <update id="updatePauseCode" parameterType="ProbationCode">
  		 		update  tb_iamberry_probation_code  
  		 			set 
	                  	puser_code= #{puserCode},
	                  	puser_date= #{puserDate}
	                where 
                		puser_tel = #{puserTel} 
  </update>
  
  <!-- 根据用户手机号查询会员试用订单 -->
  <select id="selectProbationOrderByTel" parameterType="String" resultType="ProbationUserInfo">
  		select <include refid="probationInfo"/> from tb_iamberry_probation_userInfo 
  		where 
  		puser_tel = #{puserTel}
  </select>
   <!-- 根据商品id查询商品信息 -->
	<select id="selectProductInfo" parameterType="Integer" resultType="Product">
		SELECT 
			PRODUCT_NAME productName,
			COLOR_DISCOUNT productDiscount,
			COLOR_NAME productColor 
		FROM 
			TB_IAMBERRY_PRODUCT_INFO LEFT JOIN TB_IAMBERRY_PRODUCT_COLOR ON PRODUCT_ID = COLOR_PRODUCT_ID 
		WHERE 
			PRODUCT_ID = #{0} 
		AND 
			COLOR_ID = #{1} 
	</select>
	
	<!-- 添加试用订单信息 -->
	<insert id="addProbationOrder" parameterType="ProbationOrderDto" useGeneratedKeys="true">
		INSERT INTO TB_IAMBERRY_PROBATION_SHOP_ORDER
		(
			PROBATION_ORDERID,PROBATION_PRODUCT_ID,PROBATION_OPENID,PROBATION_YET_AMOUNT,PROBATION_AMOUNT,
			PROBATION_CREATE_TIME,PROBATION_STATUS,PROBATION_ADDRESS_INFO,PROBATION_RECEIVE_TEL,
			PROBATION_RECEIVE_NAME,PROBATION_COLOR_ID 
		) 
		VALUES 
		(
			#{probationOrderid},#{probationProductId},#{probationOpenid},#{probationYetAmount},#{probationAmount},NOW(),#{probationStatus},
			#{probationAddressInfo},#{probationReceiveTel},#{probationReceiveName},#{probationColorId}
		)
	</insert>
	
	<!-- 添加试用人信息 -->
	<insert id="addProbationUser" parameterType="ProbationUserDto" useGeneratedKeys="true" keyProperty="puserId">
		INSERT INTO TB_IAMBERRY_PROBATION_USERINFO
		(
			PUSER_OPENID,PUSER_NAME,PUSER_TEL,PUSER_ADDRESS,PUSER_SEND_NAME,PUSER_SEND_TEL,
			PUSER_PRODUCTID,PUSER_RECEIVE_NAME,PUSER_RECEIVE_TEL,PUSER_CREATE_TIME
		) 
		VALUES 
		(
			#{puserOpenid},#{puserName},#{puserTel},#{puserAddress},#{puserSendName},#{puserSendTel},
			#{puserProductid},#{puserNickName},#{puser},NOW()
		)
	</insert>
	
	<!-- 根据orderid查询一个试用订单 -->
	<select id="selectProbationOrderByOrderId" parameterType="String" resultType="ProbationOrderDto">
		SELECT 
			<include refid="probationOrderDto"/> 
		FROM TB_IAMBERRY_PROBATION_SHOP_ORDER O
		LEFT JOIN 
  		TB_IAMBERRY_PRODUCT_INFO F ON O.PROBATION_PRODUCT_ID = F.PRODUCT_ID
  		WHERE 
  		O.PROBATION_ORDERID = #{orderId}
	</select>
	
	<!-- 根据电话号码查询订单个数 -->
	<select id="selectProbationOrderCountByTel" parameterType="String" resultType="Integer">
		SELECT 
			COUNT(PROBATION_ORDERID)  
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER 
		WHERE 
			PROBATION_RECEIVE_TEL = #{0} 
		AND 
			<![CDATA[PROBATION_STATUS <> 0 ]]>
	</select>
	
	<!-- 添加订单信息 -->
	<insert id="insertSalesOrder" parameterType="Order">
		INSERT INTO 
		 	TB_IAMBERRY_SHOP_SALES_ORDER
		 		(
			 		SALES_ORDERID, SALES_OPENID, SALES_YET_AMOUNT, SALES_AMOUNT,
			 		SALES_CREATE_DATE,SALES_ADDRESS_INFO,
			 		SALES_ADDRESS_TEL, SALES_ADDRESS_NAME, SALES_USER_REMARK, SALES_STATUS, 
			 		SALES_PROXY_REWARD, SALES_TRANSACTION_ID,SALES_TRANSACTION_DATE,
			 		SALES_PORDERID, SALES_ORDER_TYPE, SALES_ADDRESS_POSTNUM, SALES_SEND_DATE,
			 		SALES_ADMIN_REMARK, SALES_COLOR_TYPE
				)
		 VALUES 
		  	( 
			  #{salesOrderid},  
			  #{salesOpenid},
			  #{salesYetAmount}, 
			  #{salesAmount},
			  NOW(),
			  #{salesAddressInfo},
			  #{salesAddressTel},
			  #{salesAddressName},
			  #{salesUserRemark},
			  #{salesStatus},
			  1,
			  #{salesTransactionId},
			  #{salesTransactionDate},
			  #{salesPorderid},
			  #{salesOrderType},
			  #{salesAddressPostnum},
			  #{salesSendDate},
			  #{salesAdminRemark},
			  #{salesColorType}
		  	)
	</insert>
	
	<!-- 查询VIP会员和金牌会员下线试用订单列表 -->
 	<select id="selectVipProbationList" resultType="ProbationOrderDto" parameterType="ProbationOrderDto">
 		SELECT 
			<include refid="probationOrderDto" />,
			pu.PUSER_NAME 'probationUserDto.puserName',
			pu.PUSER_TEL 'probationUserDto.puserTel' 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER o 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON o.PROBATION_OPENID = u.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON u.USER_DEALERS = p.PLACE_PIDS 
			LEFT JOIN TB_IAMBERRY_PROBATION_USERINFO pu ON o.PROBATION_RECEIVE_TEL = pu.PUSER_TEL 
			LEFT JOIN TB_IAMBERRY_PRODUCT_INFO d ON o.PROBATION_PRODUCT_ID = d.PRODUCT_ID 
		WHERE 
			p.PLACE_OPENID = #{probationOpenid} 
		AND 
			o.PROBATION_STATUS IN (1,2,3,4,5) 
		LIMIT ${page.recordBegin},${page.pageSize} 
 	</select>
 	
 	<!-- 查询门店和钻石会员下线试用订单列表 -->
 	<select id="selectStoreProbationList" resultType="ProbationOrderDto" parameterType="ProbationOrderDto">
 		SELECT 
			<include refid="probationOrderDto" />,
			pu.PUSER_NAME 'probationUserDto.puserName',
			pu.PUSER_TEL 'probationUserDto.puserTel' 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER o 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON o.PROBATION_OPENID = u.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON u.USER_DEALERS = p.PLACE_PIDS 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO i ON p.PLACE_PID = i.PLACE_ID 
			LEFT JOIN TB_IAMBERRY_PROBATION_USERINFO pu ON o.PROBATION_RECEIVE_TEL = pu.PUSER_TEL 
			LEFT JOIN TB_IAMBERRY_PRODUCT_INFO d ON o.PROBATION_PRODUCT_ID = d.PRODUCT_ID 
		WHERE 
			i.PLACE_OPENID = #{probationOpenid} 
		AND 
			o.PROBATION_STATUS in (1,2,3,4,5) 
		LIMIT ${page.recordBegin},${page.pageSize} 
 	</select>
 	
 	<!-- 查询合伙人下线试用订单列表 -->
 	<select id="selectPartnerProbationList" resultType="ProbationOrderDto" parameterType="ProbationOrderDto">
 		SELECT 
			<include refid="probationOrderDto" />,
			pu.PUSER_NAME 'probationUserDto.puserName',
			pu.PUSER_TEL 'probationUserDto.puserTel' 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER o 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON o.PROBATION_OPENID = u.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON u.USER_DEALERS = p.PLACE_PIDS 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO i ON p.PLACE_PID = i.PLACE_ID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO pi ON i.PLACE_PID = pi.PLACE_ID 
			LEFT JOIN TB_IAMBERRY_PROBATION_USERINFO pu ON o.PROBATION_RECEIVE_TEL = pu.PUSER_TEL 
			LEFT JOIN TB_IAMBERRY_PRODUCT_INFO d ON o.PROBATION_PRODUCT_ID = d.PRODUCT_ID 
		WHERE 
			pi.PLACE_OPENID = #{probationOpenid} 
		AND 
			o.PROBATION_STATUS in (1,2,3,4,5) 
		LIMIT ${page.recordBegin},${page.pageSize} 
 	</select>
 	
 	<!-- 查询VIP会员和金牌会员下线试用订单数量 -->
 	<select id="selectVipProbationNum" parameterType="String" resultType="Integer">
 		SELECT 
			COUNT(DISTINCT o.PROBATION_ORDERID) 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER o 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON o.PROBATION_OPENID = u.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON u.USER_DEALERS = p.PLACE_PIDS 
		WHERE 
			p.PLACE_OPENID = #{openId} 
		AND 
			o.PROBATION_STATUS IN (1,2,3,4,5)
 	</select>
 	
 	<!-- 查询门店和钻石会员下线试用订单数量 -->
 	<select id="selectStoreProbationNum" resultType="Integer" parameterType="String">
 		SELECT 
			COUNT(DISTINCT o.PROBATION_ORDERID) 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER o 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON o.PROBATION_OPENID = u.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON u.USER_DEALERS = p.PLACE_PIDS 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO i ON p.PLACE_PID = i.PLACE_ID 
		WHERE 
			i.PLACE_OPENID = #{openId} 
		AND 
			o.PROBATION_STATUS in (1,2,3,4,5)
 	</select>
 	
 	<!-- 查询合伙人下线试用订单数量 -->
 	<select id="selectPartnerProbationNum" resultType="Integer" parameterType="String">
 		SELECT 
			COUNT(DISTINCT o.PROBATION_ORDERID) 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER o 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON o.PROBATION_OPENID = u.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON u.USER_DEALERS = p.PLACE_PIDS 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO i ON p.PLACE_PID = i.PLACE_ID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO pi ON i.PLACE_PID = pi.PLACE_ID 
		WHERE 
			pi.PLACE_OPENID = #{openId} 
		AND 
			o.PROBATION_STATUS in (1,2,3,4,5)
 	</select>
  </mapper>