<?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.purchase.TagPurchaseDao">

    <sql id="columns">
        a.`id` AS "id",
		a.`data_time` AS "dataTime",
		a.`order_no` AS "orderNo",
		a.`receive_date` AS "receiveDate",
		a.`provider_id` AS "providerId",
		a.`tag_purchase_state_id` AS "tagPurchaseStateId",
		a.`buyer_id` AS "buyerId",
		a.`buyer_name` AS "buyerName",
		a.`create_time` AS "cTime",
		a.`update_time` AS "updateTime"
    </sql>

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

    <insert id="save">
        INSERT INTO
        tag_purchase
        (`id`,
        `data_time`,
        `order_no`,
        `receive_date`,
        `provider_id`,
        `tag_purchase_state_id`,
        `buyer_id`,
        `buyer_name`)
        VALUES
            (
            #{data.id},
            #{data.dataTime},
            #{data.orderNo},
            #{data.receiveDate},
            #{data.providerId},
            #{data.tagPurchaseStateId},
            #{data.buyerId},
            #{data.buyerName}
            )
    </insert>

    <update id="tagPurchaseStateIdUpdate">
        UPDATE
        tag_purchase a
        SET
        a.`tag_purchase_state_id` = #{tagPurchaseStateId}
        WHERE a.`id` IN
        <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

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

    <select id="findList" resultType="com.scm.contract.model.purchase.TagPurchase">
        SELECT
        <include refid="columns"/>
        FROM tag_purchase 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="tagPurchaseStateId != null and tagPurchaseStateId != ''">
                AND a.tag_purchase_state_id = #{tagPurchaseStateId}
            </if>
        </where>
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
        </choose>
    </select>

    <select id="getById" resultType="com.scm.contract.model.purchase.TagPurchase">
        SELECT
        <include refid="columns"/>
        FROM
            tag_purchase a
        WHERE
            `id` = #{id}
    </select>
    <select id="getByIds" resultType="com.scm.contract.model.purchase.TagPurchase">
        SELECT
        <include refid="columns"/>
        FROM
        tag_purchase a
        WHERE a.id IN
        <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="getMaxOrderNo" resultType="java.lang.String">
        SELECT
            MAX(`a`.`order_no`)
        FROM tag_purchase a
    </select>

    <update id="update">
        UPDATE
            tag_purchase
        SET
            `data_time` = #{dataTime},
            `receive_date` = #{receiveDate},
            `provider_id` = #{providerId},
            `tag_purchase_state_id` = #{tagPurchaseStateId}
        WHERE
            `id` = #{id}
    </update>

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