<?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.scm.contract.dao.print.PrintCodeDao">

	<sql id="columns">
		a.`id` AS "id",
		a.`detail_id` AS "detailId",
		a.`enterprise_code` AS "enterpriseCode",
		a.`materiel_code` AS "materielCode",
		a.`materiel_describe` AS "materielDescribe",
		a.`goods_code` AS "goodsCode",
		a.`goods_name` AS "goodsName",
		a.`provider_code` AS "providerCode",
		a.`provider_name` AS "providerName",
		a.`tag_type` AS "tagType",
		a.`minimum_packing_number` AS "minimumPackingNumber",
		a.`qr_code` AS "qrCode",
		a.`serial_number` AS "serialNumber",
		a.`verification_code` AS "verificationCode",
		a.`is_deliver_goods` AS "isDeliverGoods"
	</sql>

	<sql id="joins">
	</sql>

	<insert id="saveList">
		INSERT INTO
		print_code
		(
		`id`,
		`detail_id`,
		`enterprise_code`,
		`materiel_code`,
		`materiel_describe`,
		`goods_code`,
		`goods_name`,
		`provider_code`,
		`provider_name`,
		`tag_type`,
		`minimum_packing_number`,
		`qr_code`,
		`serial_number`,
		`verification_code`,
		`is_deliver_goods`
		)
		VALUES
		<foreach item="data" index="index" collection="printCodes" separator=",">
			(
			#{data.id},
			#{data.detailId},
			#{data.enterpriseCode},
			#{data.materielCode},
			#{data.materielDescribe},
			#{data.goodsCode},
			#{data.goodsName},
			#{data.providerCode},
			#{data.providerName},
			#{data.tagType},
			#{data.minimumPackingNumber},
			#{data.qrCode},
			#{data.serialNumber},
			#{data.verificationCode},
			#{data.isDeliverGoods}
			)
		</foreach>
	</insert>

	<update id="updateIsDeliverGoods">
		UPDATE
			print_code
		SET
			`is_deliver_goods` = #{isDeliverGoods}
		WHERE
		    `id` IN
		<foreach item="printCodeId" index="index" collection="printCodeIds" open="(" separator="," close=")">
			#{printCodeId}
		</foreach>
	</update>

	<update id="updateBash">
		<foreach item="data" index="index" collection="printCodes" separator=";">
			UPDATE
			    print_code
			SET
			`detail_id` = #{data.detailId},
			`enterprise_code` = #{data.enterpriseCode},
			`materiel_code` = #{data.materielCode},
			`materiel_describe` = #{data.materielDescribe},
			`goods_code` = #{data.goodsCode},
			`goods_name` = #{data.goodsName},
			`provider_code` = #{data.providerCode},
			`provider_name` = #{data.providerName},
			`tag_type` = #{data.tagType},
			`minimum_packing_number` = #{data.minimumPackingNumber},
			`qr_code` = #{data.qrCode},
			`serial_number` = #{data.serialNumber},
			`verification_code` = #{data.verificationCode},
			`is_deliver_goods` = #{data.isDeliverGoods}
			WHERE
			`id` = #{data.id}
		</foreach>
	</update>

	<select id="getListByDetailIds" resultType="com.scm.contract.model.print.PrintCode">
		SELECT
		<include refid="columns"/>
		FROM print_code a
		<include refid="joins"/>
		WHERE a.`detail_id` IN
		<foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
			#{id}
		</foreach>
	</select>

	<select id="getListByDetailId" resultType="com.scm.contract.model.print.PrintCode">
		SELECT
		<include refid="columns"/>
		FROM print_code a
		<include refid="joins"/>
		WHERE a.`detail_id` = #{detailId}
	</select>

	<select id="getMaxSerialNumber" resultType="com.scm.contract.model.print.PrintCode">
		SELECT
			`goods_code` AS goodsCode,
			`minimum_packing_number` AS minimumPackingNumber,
			CONCAT(`goods_code`, `minimum_packing_number`) AS test,
			MAX( `serial_number` ) AS serialNumber
		FROM
			`print_code`
		WHERE
			`serial_number` LIKE CONCAT(YEAR(now()), '%')
		GROUP BY
			CONCAT(`goods_code`, `minimum_packing_number`);
	</select>
	<select id="getSerialNumberRange" resultType="com.scm.contract.model.print.SerialNumberRange">
		SELECT
			`detail_id` AS `id`,
			MIN(`serial_number`) AS `min`,
			MAX(`serial_number`) AS `max`
		FROM
			`print_code`
		WHERE
			detail_id IN
		<foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
			#{id}
		</foreach>
		GROUP BY
			`detail_id`
	</select>

	<select id="getListByIds" resultType="com.scm.contract.model.print.PrintCode">
		SELECT
		<include refid="columns"/>
		FROM print_code a
		<include refid="joins"/>
		WHERE a.`id` IN
		<foreach item="id" index="index" collection="printCodeIds" open="(" separator="," close=")">
			#{id}
		</foreach>
	</select>

	<select id="getListByEpcs" resultType="com.scm.contract.model.print.PrintCode">
		SELECT
		<include refid="columns"/>
		FROM print_code a
		<include refid="joins"/>
		WHERE a.`qr_code` IN
		<foreach item="epc" index="index" collection="epcs" open="(" separator="," close=")">
			#{epc}
		</foreach>
	</select>


</mapper>
