<?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.ghy.order.mapper.OrderTemplateMapper">

    <resultMap id="ColumnMap" type="com.ghy.order.domain.OrderTemplate">
        <id property="id"                               column="id"/>
        <result property="userId"                       column="user_id"/>
        <result property="templateName"                 column="template_name"/>
        <result property="goodsBrand"                   column="goods_brand"/>
        <result property="goodsSpecification"           column="goods_specification"/>
        <result property="videoUrl"                     column="video_url"/>
        <result property="imageUrl"                     column="image_url"/>
        <result property="addressId"                    column="address_id"/>
        <result property="streetId"                     column="street_id"/>
        <result property="fullAddress"                  column="full_address"/>
        <result property="customerName"                 column="customer_name"/>
        <result property="customerPhone"                column="customer_phone"/>
        <result property="orderMode"                    column="order_mode"/>
        <result property="price"                        column="price"/>
        <result property="agencyFund"                   column="agency_fund"/>
        <result property="informationFee"               column="information_fee"/>
        <result property="bonus"                        column="bonus"/>
        <result property="logisticsCode"                column="logistics_code"/>
        <result property="needWagon"                    column="need_wagon"/>
        <result property="needCarry"                    column="need_carry"/>
        <result property="floor"                        column="floor"/>
        <result property="createBy"                     column="create_by"/>
        <result property="createTime"                   column="create_time"/>
        <result property="updateBy"                     column="update_by"/>
        <result property="updateTime"                   column="update_time"/>
        <result property="remark"                       column="remark"/>
    </resultMap>

    <update id="update" parameterType="com.ghy.order.domain.OrderTemplate">
        UPDATE order_template
        <set>
            <if test="templateName != null">template_name = #{templateName},</if>
            <if test="goodsBrand != null">goods_brand = #{goodsBrand},</if>
            <if test="goodsSpecification != null">goods_specification = #{goodsSpecification},</if>
            <if test="videoUrl != null">video_url = #{videoUrl},</if>
            <if test="imageUrl != null">image_url = #{imageUrl},</if>
            <if test="addressId != null">address_id = #{addressId},</if>
            <if test="streetId != null">street_id = #{streetId},</if>
            <if test="fullAddress != null">full_address = #{fullAddress},</if>
            <if test="customerName != null">customer_name = #{customerName},</if>
            <if test="customerPhone != null">customer_phone = #{customerPhone},</if>
            <if test="orderMode != null">order_mode = #{orderMode},</if>
            <if test="price != null">price = #{price},</if>
            <if test="agencyFund != null">agency_fund = #{agencyFund},</if>
            <if test="informationFee != null ">information_fee = #{informationFee},</if>
            <if test="bonus != null">bonus = #{bonus},</if>
            <if test="logisticsCode != null">logistics_code = #{logisticsCode},</if>
            <if test="needWagon != null">need_wagon = #{needWagon},</if>
            <if test="needCarry != null">need_carry = #{needCarry},</if>
            <if test="floor != null">floor = #{floor},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = SYSDATE()
        </set>
        WHERE id = #{id}
    </update>

    <insert id="insert" parameterType="com.ghy.order.domain.OrderTemplate" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO order_template(
            user_id,
            <if test="templateName != null">template_name,</if>
            <if test="goodsBrand != null ">goods_brand,</if>
            <if test="goodsSpecification != null ">goods_specification,</if>
            <if test="videoUrl != null ">video_url,</if>
            <if test="imageUrl != null ">image_url,</if>
            <if test="addressId != null ">address_id,</if>
            <if test="streetId != null ">street_id,</if>
            <if test="fullAddress != null ">full_address,</if>
            <if test="customerName != null ">customer_name,</if>
            <if test="customerPhone != null ">customer_phone,</if>
            <if test="orderMode != null">order_mode,</if>
            <if test="price != null">price,</if>
            <if test="agencyFund != null">agency_fund,</if>
            <if test="informationFee != null">information_fee,</if>
            <if test="bonus != null">bonus,</if>
            <if test="logisticsCode != null">logistics_code,</if>
            <if test="needWagon != null">need_wagon,</if>
            <if test="needCarry != null">need_carry,</if>
            <if test="floor != null ">floor,</if>
            create_time
        )VALUES(
            #{userId},
            <if test="templateName != null ">#{templateName},</if>
            <if test="goodsBrand != null">#{goodsBrand},</if>
            <if test="goodsSpecification != null">#{goodsSpecification},</if>
            <if test="videoUrl != null ">#{videoUrl},</if>
            <if test="imageUrl != null">#{imageUrl},</if>
            <if test="addressId != null">#{addressId},</if>
            <if test="streetId != null">#{streetId},</if>
            <if test="fullAddress != null">#{fullAddress},</if>
            <if test="customerName != null">#{customerName},</if>
            <if test="customerPhone != null">#{customerPhone},</if>
            <if test="orderMode != null">#{orderMode},</if>
            <if test="price != null">#{price},</if>
            <if test="agencyFund != null">#{agencyFund},</if>
            <if test="informationFee != null">#{informationFee},</if>
            <if test="bonus != null">#{bonus},</if>
            <if test="logisticsCode != null">#{logisticsCode},</if>
            <if test="needWagon != null">#{needWagon},</if>
            <if test="needCarry != null">#{needCarry},</if>
            <if test="floor != null">#{floor},</if>
            SYSDATE()
        )
    </insert>

    <select id="select" parameterType="com.ghy.order.domain.OrderTemplate" resultMap="ColumnMap">
        <include refid="selectColumns"/>
        <where>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
        </where>
    </select>

    <select id="selectById" parameterType="long" resultMap="ColumnMap">
        <include refid="selectColumns"/> WHERE id = #{id}
    </select>

    <select id="selectByIds" resultMap="ColumnMap">
        <include refid="selectColumns"/> WHERE id IN
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <delete id="delete" parameterType="Long">
        DELETE FROM order_template WHERE id = #{id}
    </delete>

    <delete id="batchDelete">
        DELETE FROM order_template WHERE id IN
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <sql id="selectColumns">
        SELECT id, user_id, template_name,goods_brand, goods_specification, video_url, image_url,
               address_id, street_id, full_address, customer_name, customer_phone, order_mode,
               price, agency_fund, information_fee, bonus, logistics_code, need_wagon, need_carry, floor,
               create_by, create_time, update_by, update_time, remark
        FROM order_template
    </sql>

<!-- order_template_goods SQL -->

    <insert id="insertGoods">
        INSERT INTO order_template_goods(order_template_id, goods_id, goods_name, dept_goodscategory_id, number)
        VALUES(#{orderTemplateId}, #{goodsId}, #{goodsName}, #{deptGoodsCategoryId}, #{number})
    </insert>

    <delete id="deleteGoods" parameterType="Long">
        DELETE FROM order_template_goods WHERE order_template_id = #{orderTemplateId}
    </delete>

    <select id="selectGoods" resultType="com.ghy.order.domain.OrderTemplateGoods">
        SELECT order_template_id AS orderTemplateId,
               goods_id AS goodsId,
               goods_name AS goodsName,
               dept_goodscategory_id AS deptGoodsCategoryId,
               number
        FROM order_template_goods
        WHERE order_template_id = #{orderTemplateId}
    </select>
</mapper>
