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

    <sql id="columns">
        a.`id` AS "id",
		a.`data_time` AS "dataTime",
		a.`provider_id` AS "providerId",
		a.`order_no` AS "orderNo",
		a.`tag_apply_state_id` AS "tagApplyStateId",
		a.`operator_id` AS "operatorId",
		a.`operator_name` AS "operatorName",
		a.`create_time` AS "cTime",
		a.`update_time` AS "updateTime"
    </sql>

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

    <insert id="saveList">
        INSERT INTO
        label_application
        (`id`,
        `data_time`,
        `provider_id`,
        `order_no`,
        `tag_apply_state_id`,
        `operator_id`,
        `operator_name`
        )
        VALUES
        <foreach item="labelApplication" index="index" collection="labelApplications" separator=",">
            (
            #{labelApplication.id},
            #{labelApplication.dataTime},
            #{labelApplication.providerId},
            #{labelApplication.orderNo},
            #{labelApplication.tagApplyStateId},
            #{labelApplication.operatorId},
            #{labelApplication.operatorName}
            )
        </foreach>
    </insert>

    <update id="tagApplyStateIdUpdate">
        UPDATE
        label_application a
        SET
        a.`tag_apply_state_id` = #{tagApplyStateId}
        WHERE a.`id` IN
        <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="update">
        UPDATE
            label_application
        SET `data_time`= #{dataTime},
            `tag_apply_state_id`= #{tagApplyStateId}
        WHERE `id` = #{id}
    </update>
    <update id="updateListById">
        <foreach item="labelApplication" index="index" collection="labelApplications" separator=";">
            UPDATE
            label_application
            SET `data_time`= #{labelApplication.dataTime},
            `tag_apply_state_id`= #{labelApplication.tagApplyStateId},
            `order_no` = #{labelApplication.orderNo}
            WHERE `id` = #{labelApplication.id}
        </foreach>
    </update>

    <select id="get" resultType="com.scm.contract.model.apply.LabelApplication">
        SELECT
        <include refid="columns"/>
        FROM label_application a
        <include refid="joins"/>
        WHERE a.id = #{id}
    </select>

    <select id="findList" resultType="com.scm.contract.model.apply.LabelApplication">
        SELECT
        <include refid="columns"/>
        FROM label_application a
        <include refid="joins"/>
        <where>
            <if test="id != null and id != ''">
                AND a.id = #{id}
            </if>
            <if test="beginTime != null and endTime != null ">
                AND a.data_time BETWEEN #{beginTime} AND #{endTime}
            </if>
            <if test="providerId != null and providerId != ''">
                AND a.provider_id = #{providerId}
            </if>
            <if test="tagApplyStateId != null and tagApplyStateId != ''">
                AND a.tag_apply_state_id = #{tagApplyStateId}
            </if>
            <if test="tagApplyStateIds != null and tagApplyStateIds.size() >=0">
                AND a.tag_apply_state_id IN
                <foreach item="id" index="index" collection="tagApplyStateIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
        </choose>
    </select>

    <select id="getListByIds" resultType="com.scm.contract.model.apply.LabelApplication">
        SELECT
        <include refid="columns"/>
        FROM label_application a
        <include refid="joins"/>
        WHERE a.id IN
        <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="getById" resultType="com.scm.contract.model.apply.LabelApplication">
        SELECT
        <include refid="columns"/>
        FROM label_application a
        <include refid="joins"/>
        WHERE a.id = #{id}
    </select>

    <select id="getMaxOrderNo" resultType="java.lang.String">
        SELECT
            MAX(`order_no`)
        FROM label_application
    </select>


    <delete id="delete">
        DELETE
        FROM label_application
        WHERE id = #{id}
    </delete>

</mapper>
