<?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.ReprintCodeDetailDao">

	<sql id="columns">
		a.`id` AS "id",
		a.`reprint_code_id` AS "reprintCodeId",
		a.`print_code_id` AS "printCodeId",
		a.`old_serial_number` AS "oldSerialNumber",
		a.`old_qr_code` AS "oldQrCode",
		a.`new_serial_number` AS "newSerialNumber",
		a.`new_qr_code` AS "newQrCode"
	</sql>

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

	<insert id="saveBash">
		INSERT INTO
		reprint_code_detail
		(
		`id`,
		`reprint_code_id`,
		`print_code_id`,
		`old_serial_number`,
		`old_qr_code`,
		`new_serial_number`,
		`new_qr_code`
		)
		VALUES
		<foreach item="data" index="index" collection="reprintCodeDetails" separator=",">
			(
			#{data.id},
			#{data.reprintCodeId},
			#{data.printCodeId},
			#{data.oldSerialNumber},
			#{data.oldQrCode},
			#{data.newSerialNumber},
			#{data.newQrCode}
			)
		</foreach>
	</insert>
	<delete id="deleteByIds">
		DELETE
		FROM reprint_code_detail
		WHERE `id` IN
		<foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>
	<delete id="deleteByReprintCodeId">
		DELETE
		FROM reprint_code_detail
		WHERE `reprint_code_id` = #{reprintCodeId}
	</delete>

	<select id="getSerialNumberRange" resultType="com.scm.contract.model.print.SerialNumberRange">
		SELECT
			`reprint_code_id` AS `id`,
			MIN(`new_serial_number`) AS `min`,
			MAX(`new_serial_number`) AS `max`
		FROM
			`reprint_code_detail`
		WHERE
			reprint_code_id IN
		<foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
			#{id}
		</foreach>
		GROUP BY
			`reprint_code_id`
	</select>

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


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

</mapper>
