<?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.MachineNumberMapper">
	<!-- 添加日志记录-->
<insert id="addNumberLogs" parameterType="MachineNumberLogs">
	INSERT INTO tb_iamberry_user_machine_number_logs
	(logs_batch,logs_number,logs_remark,logs_create_time)
	VALUES
	(#{logsBatch},#{logsNumber},#{logsRemark},NOW())
</insert>
	<!-- 添加机器编号，二维码-->
	<insert id="addMachineNumber" parameterType="MachineNumber">
		INSERT INTO tb_iamberry_user_machine_number
		(number_qrcode,number_barcode,number_batch,number_status,number_create_time)
		VALUES
		(#{numberQrcode},#{numberBarcode},#{numberBatch},1,NOW())
	</insert>

	<sql id="numberLogs">
		nl.logs_id logsId,
		nl.logs_batch logsBatch,
		nl.logs_number logsNumber,
		nl.logs_remark logsRemark,
		nl.logs_create_time logsCreateTime
	</sql>

	<sql id="warrantyCardInfo">
		number_id cardId,
		number_barcode cardBarcode,
		number_sales_time cardSaleTime,
		user_email userEmail,
		user_name userName,
		number_watero cardWatero,
		number_province cardProvince,
		number_city cardCity,
		number_district cardDistrict
	</sql>

	<!-- 统计批次及已完成条数-->
	<select id="listNumberLogs" parameterType="Page" resultType="MachineNumberLogs">
		SELECT
		<include refid="numberLogs"/>,count(mn.number_batch) as qrCodeNumber,count(mn.number_batch) as completedNumber
		FROM
			tb_iamberry_user_machine_number_logs nl
		LEFT JOIN tb_iamberry_user_machine_number mn ON nl.logs_batch = mn.number_batch
		<where>
			<if test="params.logsBatch!=null and params.logsBatch!='' ">
			 AND	number_batch = #{params.logsBatch}
			</if>
			<if test="params.beginDate!=null and params.beginDate!=''">
				<![CDATA[ AND DATE(number_create_time) >= #{params.beginDate} ]]>
			</if>
			<if test="params.endDate!=null and params.endDate!=''">
				<![CDATA[ AND DATE(number_create_time) <=  #{params.endDate} ]]>
			</if>
		</where>
		group by nl.logs_batch
		Order By nl.logs_create_time Desc
		LIMIT #{pageNo}, #{pageSize}
	</select>
	<!-- 统计批次及已完成条数总条数-->
	<select id="listNumberLogsCount" parameterType="Page" resultType="Integer">
	SELECT COUNT(*) from  (
			SELECT
			<include refid="numberLogs"/>,count(mn.number_batch) as qrCodeNumber
			FROM
			tb_iamberry_user_machine_number_logs nl
			LEFT JOIN tb_iamberry_user_machine_number mn ON nl.logs_batch = mn.number_batch
			<where>
				<if test="params.logsBatch!=null and params.logsBatch!='' ">
					AND	number_batch = #{params.logsBatch}
				</if>
				<if test="params.beginDate!=null and params.beginDate!=''">
					<![CDATA[ AND DATE(logs_create_time) >= #{params.beginDate} ]]>
				</if>
				<if test="params.endDate!=null and params.endDate!=''">
					<![CDATA[ AND DATE(logs_create_time) <=  #{params.endDate} ]]>
				</if>
			</where>
			group by nl.logs_batch) as counts
	</select>

	<!--查询生产批次二维码列表-->
	<select id="listNumberQrCode" parameterType="Page" resultType="MachineNumber">
		select * from tb_iamberry_user_machine_number
		<where>
			<if test="params.logsBatch!=null and params.logsBatch!='' ">
				AND	number_batch = #{params.logsBatch}
			</if>
		</where>
		<if test="pageNo!=null and pageSize!=null and pageSize > 0 ">
		LIMIT #{pageNo}, #{pageSize}
		</if>
	</select>

	<!--查询生产批次二维码列表总数-->
	<select id="listNumberQrCodeCount" parameterType="Page" resultType="Integer">
		select
			count(*)
	  	from
			tb_iamberry_user_machine_number
		<where>
			<if test="params.logsBatch!=null and params.logsBatch!='' ">
				number_batch = #{params.logsBatch}
			</if>
		</where>

	</select>

	<!-- 根据条件查询机器编号集合,可分页 -->
	<select id="listMachineNumber" parameterType="MachineNumber" resultType="MachineNumber">
		select
			*
		from
		tb_iamberry_user_machine_number
		<where>
			<if test="numberId != null and numberId != '' ">
				number_id = #{numberId}
			</if>
			<if test="numberOpenId != null and numberOpenId != '' ">
				and number_open_id = #{numberOpenId}
			</if>
			<if test="numberBarcode != null and numberBarcode != '' ">
				and number_barcode = #{numberBarcode}
			</if>
		</where>
		<if test="page!=null and page.pageSize>0 ">
			LIMIT ${page.recordBegin},${page.pageSize}
		</if>
	</select>

	<!-- 根据条件查询质保卡集合,可分页 -->
	<select id="listWarrantyCard" parameterType="WarrantyCard" resultType="WarrantyCard">
		select
			<include refid="warrantyCardInfo" />
		from
		tb_iamberry_user_machine_number
		LEFT JOIN tb_iamberry_user_userinfo ON number_open_id = user_openid
		<where>
			number_status = 1
			and number_open_id is not null
			<if test="cardBarcode != null and cardBarcode != '' ">
				and number_barcode like CONCAT('%',#{cardBarcode},'%')
			</if>
			<if test="userName != null and userName != '' ">
				and user_name like CONCAT('%',#{userName},'%')
			</if>
			<if test="userEmail != null and userEmail != '' ">
				and user_email like CONCAT('%',#{userEmail},'%')
			</if>
		</where>
		order by number_id DESC
		<if test="page!=null and page.pageSize>0 ">
			LIMIT ${page.recordBegin},${page.pageSize}
		</if>
	</select>

	<select id="WarrantyCardCount" parameterType="WarrantyCard" resultType="Integer">
		select
			COUNT(number_id)
		from
		tb_iamberry_user_machine_number
		LEFT JOIN tb_iamberry_user_userinfo ON number_open_id = user_openid
		<where>
			number_status = 1
			and number_open_id is not null
			<if test="cardBarcode != null and cardBarcode != '' ">
				and number_barcode like CONCAT('%',#{cardBarcode},'%')
			</if>
			<if test="userName != null and userName != '' ">
				and user_name like CONCAT('%',#{userName},'%')
			</if>
			<if test="userEmail != null and userEmail != '' ">
				and user_email like CONCAT('%',#{userEmail},'%')
			</if>
		</where>
	</select>

	<!-- 修改机器编号信息 -->
	<update id="updateMachineNumber" parameterType="MachineNumber">
		update
			tb_iamberry_user_machine_number
		<set>
			<if test="numberOpenId != null">
				number_open_id = #{numberOpenId},
			</if>
			<if test="numberSalesTime != null">
				number_sales_time = #{numberSalesTime},
			</if>
			<if test="numberSubTime != null">
				number_sub_time = #{numberSubTime},
			</if>
		</set>
		<where>
			number_barcode = #{numberBarcode}
		</where>
	</update>

	<!-- 查询赠品信息列表 -->
	<select id="listUserGift" parameterType="MachineGift" resultType="MachineGift">
		select
		 *
		from
			tb_iamberry_user_machine_gift
		where
			gift_status = 1
		and
			gift_type = #{giftType}
	</select>

	<!--修改状态为已完成-->
	<update id="updateIsprInt" parameterType="String">
		UPDATE tb_iamberry_user_machine_number SET number_is_print = 2 WHERE number_id = #{numberId}
	</update>
	
	<select id="selectQrcodeLogsByBatch" parameterType="String" resultType="Integer" >
		SELECT COUNT(*) FROM tb_iamberry_user_machine_number_logs where logs_batch = #{logsBatch}
	</select>

	<select id="qrcodProgressUpdate"  resultType="MachineNumberLogs">
		SELECT
		nl.logs_batch logsBatch,nl.logs_number logsNumber,count(mn.number_batch) as qrCodeNumber,count(mn.number_batch) as completedNumber
		FROM
		tb_iamberry_user_machine_number_logs nl
		LEFT JOIN tb_iamberry_user_machine_number mn ON nl.logs_batch = mn.number_batch
		<where>
			number_batch in
			<foreach collection="array" item="item" index="index" open="(" separator="," close=")">
				#{item}
			</foreach>
		</where>
		group by nl.logs_batch
	</select>

	<!-- 添加机器编号，二维码及用户信息-->
	<insert id="addMachineNumberAndUser" parameterType="MachineNumber">
		INSERT INTO tb_iamberry_user_machine_number
		(number_open_id,number_qrcode,number_barcode,number_batch,number_sales_time,number_status,number_create_time,number_sub_time)
		VALUES
		(#{numberOpenId},#{numberQrcode},#{numberBarcode},#{numberBatch},#{numberSalesTime},1,NOW(),#{numberSubTime})
	</insert>

	<insert id="addMachineNumberList" parameterType="ArrayList">
		<selectKey resultType="int" keyProperty="numberId" order="AFTER">
			SELECT
			LAST_INSERT_ID()
		</selectKey>
		INSERT INTO tb_iamberry_user_machine_number
		(
			number_open_id,number_qrcode,number_barcode,number_batch,number_sales_time,
			number_status,number_create_time,number_sub_time,number_watero,
			number_province,number_city,number_district
		)
		VALUES
		<foreach collection="list" item="item" index="index" separator="," >
			(
			#{item.numberOpenId},#{item.numberQrcode},#{item.numberBarcode},#{item.numberBatch},
			#{item.numberSalesTime},#{item.numberStatus},NOW(),NOW(),#{item.numberWatero},
			#{item.numberProvince},#{item.numberCity},#{item.numberDistrict}
			)
		</foreach>
	</insert>
</mapper>
