<?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.SalesLogMapper">
	
	<!-- 根据产品类型和销售人查询会员销售记录 -->
	<select id="getSalesInfo" parameterType="SalesLog" resultType="SalesLog">
		SELECT 
			SALES_ID salesId,
			SALES_NUM salesNum,
			SALES_PRODUCT_TYPE salesProductType,
			SALES_USER_OPENID salesUserOpenId,
			SALES_PLACEID salesPlaceId 
		FROM 
			TB_IAMBERRY_MEMBER_SALES 
		WHERE 
			SALES_PRODUCT_TYPE = #{salesProductType}
		AND 
			SALES_PLACEID = #{salesPlaceId}
	</select>
	
	<!-- 新增会员销售记录表记录 -->
	<insert id="insertSalesLog" parameterType="SalesLog" useGeneratedKeys="true" keyProperty="salesId">
		INSERT INTO TB_IAMBERRY_MEMBER_SALES
		(
			SALES_USER_OPENID,
			SALES_PLACEID,
			SALES_PRODUCT_TYPE,
			SALES_NUM,
			SALES_CREATE_TIME 
		) 
		VALUES 
		(
			#{salesUserOpenId},
			#{salesPlaceId},
			#{salesProductType},
			#{salesNum},
			NOW()
		)
	</insert>
	
	<!-- 修改会员销售记录表记录 -->
	<update id="updateSalesLog" parameterType="SalesLog">
		UPDATE 
			TB_IAMBERRY_MEMBER_SALES 
		SET 
			SALES_NUM = (SALES_NUM + #{salesNum}) 
		WHERE 
			SALES_PRODUCT_TYPE = #{salesProductType} 
		<if test="salesUserOpenId != null and salesUserOpenId != ''">
			AND SALES_USER_OPENID = #{salesUserOpenId} 
		</if>
		<if test="salesPlaceId != null and salesPlaceId != ''">
			AND SALES_PLACEID = #{salesPlaceId} 
		</if>
	</update>
</mapper>