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

    <resultMap id="OrderDetailResult" type="com.ghy.order.domain.OrderDetail">
        <id property="id"                    column="id"/>
        <result property="code"              column="code"/>
        <result property="orderMasterId"     column="order_master_id"/>
        <result property="orderMasterCode"   column="order_master_code"/>
        <result property="customerId"        column="customer_id"/>
        <result property="orderType"         column="order_type"/>
        <result property="orderStatus"       column="order_status"/>
        <result property="workerId"          column="worker_id"/>
        <result property="revTime"           column="rev_time"/>
        <result property="workBeginTime"     column="work_begin_time"/>
        <result property="workFinishTime"    column="work_finish_time"/>
        <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>

    <sql id="selectOrderDetail">
        SELECT id,
               code,
               order_master_id,
               order_master_code,
               customer_id,
               order_type,
               order_status,
               worker_id,
               rev_time,
               work_begin_time,
               work_finish_time,
               create_by,
               create_time,
               remark
        FROM order_detail
    </sql>

    <select id="selectOrderDetailList" parameterType="com.ghy.order.domain.OrderDetail" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail"/>
        <where>
            <if test="code != null and code != ''">
                AND code LIKE concat('%', #{code}, '%')
            </if>
            <if test="customerId != null and customerId != 0">
                AND customer_id = #{customerId}
            </if>
            <if test="orderType != null">
                AND order_type = #{orderType}
            </if>
            <if test="orderStatus != null">
                AND order_status = #{orderStatus}
            </if>
            <if test="orderMasterCode != null and orderMasterCode != 0">
                AND order_detail_code = #{orderMasterCode}
            </if>
            <if test="workerId != null and workerId != 0">
                AND worker_id = #{workerId}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectById" parameterType="long" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail"/> WHERE id = #{orderDetailId}
    </select>


    <select id="selectByOrderMasterId" parameterType="long" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail"/> WHERE order_master_id = #{orderMasterId}
    </select>

    <delete id="deleteOrderDetailByIds" parameterType="Long">
        DELETE FROM order_detail WHERE id IN
        <foreach collection="array" item="orderDetailId" open="(" separator="," close=")">
            #{orderDetailId}
        </foreach>
    </delete>

    <update id="updateOrderDetail" parameterType="com.ghy.order.domain.OrderDetail">
        UPDATE order_detail
        <set>
            <if test="code != null and code != ''">code = #{code},</if>
            <if test="orderMasterId != null and orderMasterId != 0">order_master_id = #{orderMasterId},</if>
            <if test="orderMasterCode != null and orderMasterCode != ''">order_master_code = #{orderMasterCode},</if>
            <if test="customerId != null and customerId != 0">customer_id = #{customerId},</if>
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="workerId != null and workerId != 0">worker_id = #{workerId},</if>
            <if test="revTime != null">rev_time = #{revTime},</if>
            <if test="workBeginTime != null">work_begin_time = #{workBeginTime},</if>
            <if test="workFinishTime != null">work_finish_time = #{workFinishTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = SYSDATE()
        </set>
        WHERE id = #{id}
    </update>

    <insert id="insertOrderDetail" parameterType="com.ghy.order.domain.OrderDetail" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO order_detail(
        <if test="code != null and code != ''">code,</if>
        <if test="orderMasterId != null and orderMasterId != 0">order_master_id,</if>
        <if test="orderMasterCode != null and orderMasterCode != ''">order_master_code,</if>
        <if test="customerId != null and customerId != 0">customer_id,</if>
        <if test="orderType != null">order_type,</if>
        <if test="orderStatus != null">order_status,</if>
        <if test="workerId != null and workerId != 0">worker_id,</if>
        <if test="revTime != null">rev_time,</if>
        <if test="workBeginTime != null">work_begin_time,</if>
        <if test="workFinishTime != null">work_finish_time,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )VALUES(
        <if test="code != null and code != ''">#{code},</if>
        <if test="orderMasterId != null and orderMasterId != 0">#{orderMasterId},</if>
        <if test="orderMasterCode != null and orderMasterCode != ''">#{orderMasterCode},</if>
        <if test="customerId != null and customerId != 0">#{customerId},</if>
        <if test="orderType != null">#{orderType},</if>
        <if test="orderStatus != null">#{orderStatus},</if>
        <if test="workerId != null and workerId != 0">#{workerId},</if>
        <if test="revTime != null">#{revTime},</if>
        <if test="workBeginTime != null">#{workBeginTime},</if>
        <if test="workFinishTime != null">#{workFinishTime},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        SYSDATE()
        )
    </insert>

    <select id="checkOrderDetailCodeUnique" parameterType="String" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail"/>
        WHERE `code` =#{orderDetailCode} LIMIT 1
    </select>

    <select id="getByOrderIdList" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail" />
        <where>
            and order_master_id in
            <foreach item="item" index="orderIdList" collection="orderIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>

</mapper>