<?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.apply.LabelApplicationDetailDao">

    <sql id="columns">
        a.`id` AS "id",
        a.`label_application_id` AS "labelApplicationId",
		a.`tag_type_id` AS "tagTypeId",
		a.`materiel_id` AS "materielId",
		a.`number` AS "number",
		a.`unit_price` AS "unitPrice",
		a.`materiel_code` AS "materielCode",
		a.`materiel_describe` AS "materielDescribe",
		a.`goods_code` AS "goodsCode",
		a.`goods_name` AS "goodsName",
		a.`minimum_packing_number` AS "minimumPackingNumber",
		a.`minimum_packing_unit` AS "minimumPackingUnit",
        a.`is_print` AS isPrint,
        a.`print_status` AS printStatus,
        a.`expected_receipt_date` AS expectedReceiptDate,
        a.`applicant_name` AS applicantName,
        a.`delivery_method_id` AS deliveryMethodId,
        a.`delivery_order_no` AS deliveryOrderNo,
        a.`recipient` AS recipient,
        a.`address` AS address,
        a.`phone` AS phone,
        a.`mail` AS mail,
        a.`is_send` AS isSend,
        a.`print_time` = "printTime"
    </sql>

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

    <insert id="saveList">
        INSERT INTO
        label_application_detail
        (`id`,
        `label_application_id`,
        `tag_type_id`,
        `materiel_id`,
        `number`,
        `unit_price`,
        `materiel_code`,
        `materiel_describe`,
        `goods_code`,
        `goods_name`,
        `minimum_packing_number`,
        `minimum_packing_unit`,
        `is_print`,
        `print_status`,
        `expected_receipt_date`,
        `applicant_name`,
        `delivery_method_id`,
        `delivery_order_no`,
        `recipient`,
        `address`,
        `phone`,
        `mail`,
        `is_send`
        )
        VALUES
        <foreach item="detail" index="index" collection="details" separator=",">
            (
            #{detail.id},
            #{detail.labelApplicationId},
            #{detail.tagTypeId},
            #{detail.materielId},
            #{detail.number},
            #{detail.unitPrice},
            #{detail.materielCode},
            #{detail.materielDescribe},
            #{detail.goodsCode},
            #{detail.goodsName},
            #{detail.minimumPackingNumber},
            #{detail.minimumPackingUnit},
            #{detail.isPrint},
            #{detail.printStatus},
            #{detail.expectedReceiptDate},
            #{detail.applicantName},
            #{detail.deliveryMethodId},
            #{detail.deliveryOrderNo},
            #{detail.recipient},
            #{detail.address},
            #{detail.phone},
            #{detail.mail},
            #{detail.isSend}
            )
        </foreach>
    </insert>

    <update id="updatePrintBash">
        <foreach item="detail" index="index" collection="details" separator=";">
            UPDATE
            label_application_detail
            SET
            <if test="detail.printTime != null">
                `print_time` = #{detail.printTime},
            </if>
            `is_print` = #{detail.isPrint},
            `print_status` = #{detail.printStatus}
            WHERE `id` = #{detail.id}
        </foreach>
    </update>

    <update id="updateSendBash">
        <foreach item="detail" index="index" collection="details" separator=";">
            UPDATE
            label_application_detail
            SET
            `delivery_method_id` = #{detail.deliveryMethodId},
            `delivery_order_no` = #{detail.deliveryOrderNo},
            `recipient` = #{detail.recipient},
            `is_send` = #{detail.isSend}
            WHERE `id` = #{detail.id}
        </foreach>
    </update>

    <delete id="deleteListByLabelApplicationId">
        DELETE FROM label_application_detail WHERE `label_application_id` = #{labelApplicationId};
    </delete>


    <select id="getListByLabelApplicationId" resultType="com.scm.contract.model.apply.LabelApplicationDetail">
        SELECT
        <include refid="columns"/>
        FROM label_application_detail a
        <include refid="joins"/>
        WHERE a.label_application_id = #{labelApplicationId}
    </select>
    <select id="getListByLabelApplicationIds" resultType="com.scm.contract.model.apply.LabelApplicationDetail">
        SELECT
        <include refid="columns"/>
        FROM label_application_detail a
        <include refid="joins"/>
        WHERE a.label_application_id IN
        <foreach item="id" index="index" collection="labelApplicationIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="getPrinting" resultType="com.scm.contract.model.apply.LabelApplicationDetail">
        SELECT
        <include refid="columns"/>
        FROM label_application_detail a
        <include refid="joins"/>
        WHERE a.is_print = #{isPrint}
    </select>
    <select id="listByDetailIds" resultType="com.scm.contract.model.apply.LabelApplicationDetail">
        SELECT
        <include refid="columns"/>
        FROM label_application_detail a
        <include refid="joins"/>
        WHERE a.`id` IN
        <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="findList" resultType="com.scm.contract.model.apply.LabelApplicationDetail">
        SELECT
        <include refid="columns"/>
        FROM label_application_detail a
        <include refid="joins"/>
        <where>
            <if test="id != null and id != ''">
                AND a.id = #{id}
            </if>
            <if test="isPrint != null">
                AND a.is_print = #{isPrint}
            </if>
            <if test="printStatus != null and printStatus != ''">
                AND a.print_status = #{printStatus}
            </if>
        </where>
    </select>

    <select id="getById" resultType="com.scm.contract.model.apply.LabelApplicationDetail">
        SELECT
        <include refid="columns"/>
        FROM label_application_detail a
        <include refid="joins"/>
        WHERE a.`id` = #{id}
    </select>

</mapper>
