<?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.iamberry.wechat.service.mapper.AgentMachineMapper">

	<sql id="agentMachineInfo">
		AGENT_MACHINE_ID agentMachineId, 
		AGENT_MACHINE_OPEN_ID agentMachineOpenId, 
		AGENT_MACHINE_TYPE agentMachineType,
		AGENT_MACHINE_STATUS agentMachineStatus, 
		AGENT_MACHINE_PRODUCT_ID agentMachineProductId, 
		AGENT_MACHINE_PRODUCT_NAME agentMachineProductName,
		AGENT_MACHINE_COLOR_ID agentMachineColorId, 
		AGENT_MACHINE_COLOR_NAME agentMachineColorName,
		AGENT_MACHINE_69CODE agentMachine69code,
		AGENT_MACHINE_ADDRESS_INFO agentMachineAddressInfo,
		AGENT_MACHINE_ADDRESS_NAME agentMachineAddressName,
		AGENT_MACHINE_ADDRESS_TEL agentMachineAddressTel,
		AGENT_MACHINE_POST_NAME agentMachinePostName,
		AGENT_MACHINE_POST_NUM agentMachinePostNum,
		AGENT_MACHINE_CREATE_DATE agentMachineCreateDate,
		AGENT_MACHINE_REMARK agentMachineRemark
	</sql>

	<!-- 新增物流信息 -->
	<insert id="insertAgentMachine" parameterType="AgentMachine" 
		keyProperty="agentMachineId" useGeneratedKeys="true">
 	  insert into 
 	  tb_iamberry_agent_machine(
		agent_machine_open_id,
		agent_machine_type,
		agent_machine_status,
		agent_machine_product_id,
		agent_machine_product_name,
		agent_machine_color_id,
		agent_machine_color_name,
		agent_machine_69code,
		agent_machine_address_info,
		agent_machine_address_name,
		agent_machine_address_tel,
		agent_machine_post_name,
		agent_machine_post_num,
		agent_machine_create_date,
		agent_machine_remark) 
      values(
	       #{agentMachineOpenId},
	       #{agentMachineType},
	       #{agentMachineStatus},
	       #{agentMachineProductId},
	       #{agentMachineProductName},
	       #{agentMachineColorId},
	       #{agentMachineColorName},
	       #{agentMachine69code},
	       #{agentMachineAddressInfo},
	       #{agentMachineAddressName},
	       #{agentMachineAddressTel},
	   	   #{agentMachinePostName},
	       #{agentMachinePostNum},
	       NOW(),
	       #{agentMachineRemark}
       ) 
 	</insert>
	
	
	<!-- 根据物流记录ID查询物流信息（代理商赠送机器记录） -->
	<select id="selectAgentMachine" parameterType="Integer" resultType="AgentMachine">
		SELECT 
			<include refid="agentMachineInfo" />
		FROM
			tb_iamberry_agent_machine
		WHERE
			agent_machine_id = #{agentMachineId}
	</select>
	
	<!-- 修改物流信息状态 -->
	<update id="updateAgentMachine" parameterType="AgentMachine">
		UPDATE 
			tb_iamberry_agent_machine 
		<set>
			<if test="agentMachineStatus != null">
				agent_machine_status = #{agentMachineStatus}, 
			</if>
			<if test="agentMachineProductId != null">
				agent_machine_product_id = #{agentMachineProductId}, 
			</if>
			<if test="agentMachineColorId != null">
				agent_machine_color_id = #{agentMachineColorId}, 
			</if>
			<if test="agentMachineAddressInfo != null">
				agent_machine_address_info = #{agentMachineAddressInfo}, 
			</if>
			<if test="agentMachineAddressName != null">
				agent_machine_address_name = #{agentMachineAddressName},
			</if>
			<if test="agentMachineAddressTel != null">
				agent_machine_address_tel = #{agentMachineAddressTel},
			</if>
			<if test="agentMachinePostName != null">
				agent_machine_post_name = #{agentMachinePostName},
			</if>
			<if test="agentMachinePostNum != null">
				agent_machine_post_num = #{agentMachinePostNum},
			</if>
			<if test="agentMachineRemark != null">
				agent_machine_remark = #{agentMachineRemark},
			</if>
			<if test="agentMachineColorId != null">
				agent_machine_color_id = #{agentMachineColorId},
			</if>
			<if test="agentMachineProductName != null">
				agent_machine_product_name = #{agentMachineProductName},
			</if>
			<if test="agentMachine69code != null">
				agent_machine_69code = #{agentMachine69code},
			</if>
			<if test="agentMachineProductId != null">
				agent_machine_product_id = #{agentMachineProductId},
			</if>
			<if test="agentMachineColorName != null">
				agent_machine_color_name = #{agentMachineColorName}
			</if>
			
		</set>
		<where>
			agent_machine_id = #{agentMachineId}
		</where>
	</update>
	
	<!-- 查询所有物流信息（代理商赠送机器记录） -->
	<select id="selectAllAgentMachine" parameterType="AgentMachine" resultType="AgentMachine">
		SELECT 
			<include refid="agentMachineInfo" />, I.AGENT_NICKNAME agentNameInfo
		FROM
			TB_IAMBERRY_AGENT_MACHINE M
		LEFT JOIN
			TB_IAMBERRY_AGENT_INFO I
		ON
			I.AGENT_OPEN_ID = M.AGENT_MACHINE_OPEN_ID
		<where>
			<if test="agentMachineStatus != null">
				and agent_machine_status = #{agentMachineStatus}
			</if>
			<if test="beginDate!=null and beginDate!=''">
			   and agent_machine_create_date &gt; #{beginDate}
			</if>
			<if test="endDate!=null and endDate!=''">
			  and agent_machine_create_date &lt; #{endDate}
			</if>  
		</where>
		 order by agent_machine_create_date desc
	    <if test="page!=null and page.pageNo>0 and page.pageSize>0 ">
	     limit ${page.pageNo},${page.pageSize}
	    </if>
	     <if test="page!=null and page.pageNo==0 and page.pageSize>0 ">
	     limit ${page.pageSize}
	    </if>  
	</select>
	<!-- 查询所有物流信息（代理商赠送机器记录）总数 -->
	<select id="selectAllAgentMachineCount" parameterType="AgentMachine" resultType="Integer">
		SELECT 
			count(*)
		FROM
			tb_iamberry_agent_machine
		<where>
			<if test="agentMachineStatus != null ">
				agent_machine_status = #{agentMachineStatus}
			</if>
		</where>
		
	</select>
	
	<!-- 根据openid查询所有物流信息（代理商赠送机器记录） -->
	<select id="selectAllAgentMachineById" parameterType="AgentMachine" resultType="AgentMachine">
		SELECT 
			<include refid="agentMachineInfo" />
		FROM
			tb_iamberry_agent_machine
		<where>
				agent_machine_open_id = #{agentMachineOpenId}
				and agent_machine_status in (2,3)
		</where>
		 order by agent_machine_create_date desc
	    <if test="page!=null and page.pageNo>0 and page.pageSize>0 ">
	     limit ${page.pageNo},${page.pageSize}
	    </if>
	     <if test="page!=null and page.pageNo==0 and page.pageSize>0 ">
	     limit ${page.pageSize}
	    </if>  
	</select>
	<!-- 根据openID查询所有物流信息（代理商赠送机器记录）总数 -->
	<select id="selectAllAgentMachineByIdCount" parameterType="AgentMachine" resultType="Integer">
		SELECT 
			count(*)
		FROM
			tb_iamberry_agent_machine
		<where>
			agent_machine_open_id = #{agentMachineOpenId}
		</where>
		
	</select>

	<!-- 根据物流记录ID与openid查询物流信息（确认该机器是否属于当前用户） -->
	<select id="selectAgentMachineByOpenId" parameterType="AgentMachine" resultType="Integer">
		SELECT
		COUNT(*)
		FROM
		tb_iamberry_agent_machine
		WHERE
		agent_machine_id = #{agentMachineId}
		AND
		agent_machine_open_id = #{agentMachineOpenId}
	</select>

</mapper>
