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

    <resultMap id="OrderMasterResult" type="com.ghy.order.domain.OrderMaster">
        <id property="id"                    column="id"/>
        <result property="code"              column="code"/>
        <result property="customerId"        column="customer_id"/>
        <result property="orderType"         column="order_type"/>
        <result property="orderStatus"       column="order_status"/>
        <result property="payType"           column="pay_type"/>
        <result property="payStatus"         column="pay_status"/>
        <result property="workerId"          column="worker_id"/>
        <result property="payTime"           column="pay_time"/>
        <result property="revTime"           column="rev_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="selectOrderMaster">
        SELECT id,
               code,
               customer_id,
               order_type,
               order_status,
               pay_type,
               pay_status,
               worker_id,
               pay_time,
               rev_time,
               create_by,
               create_time,
               remark
        FROM order_master
    </sql>

    <select id="selectOrderMasterList" parameterType="com.ghy.order.domain.OrderMaster" resultMap="OrderMasterResult">
        <include refid="selectOrderMaster"/>
        <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 orderType != 0">
                AND order_type = #{orderType}
            </if>
            <if test="orderStatus != null and orderStatus != 0">
                AND order_status = #{orderStatus}
            </if>
            <if test="payType != null and payType != 0">
                AND pay_type = #{payType}
            </if>
            <if test="payStatus != null and payStatus != 0">
                AND pay_status = #{payStatus}
            </if>
            <if test="workerId != null and workerId != 0">
                AND worker_id = #{workerId}
            </if>
        </where>
    </select>

    <select id="selectById" parameterType="long" resultMap="OrderMasterResult">
        <include refid="selectOrderMaster"/>
        <where>
            <if test="orderMasterId != null and orderMasterId != 0">
                AND id = #{orderMasterId}
            </if>
        </where>
    </select>

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

    <update id="updateOrderMaster" parameterType="com.ghy.order.domain.OrderMaster">
        UPDATE order_master
        <set>
            <if test="code != null and code != ''">code = #{code},</if>
            <if test="customerId != null and customerId != ''">customer_id = #{customerId},</if>
            <if test="orderType != null and orderType != ''">order_type = #{orderType},</if>
            <if test="orderStatus != null and orderStatus != ''">order_status = #{orderStatus},</if>
            <if test="payType != null and payType != ''">pay_type = #{payType},</if>
            <if test="payStatus != null and payStatus != ''">pay_status = #{payStatus},</if>
            <if test="workerId != null and workerId != ''">worker_id = #{workerId},</if>
            <if test="payTime != null and payTime != ''">pay_time = #{payTime},</if>
            <if test="revTime != null and revTime != ''">rev_time = #{revTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = SYSDATE()
        </set>
        WHERE id = #{id}
    </update>

    <insert id="insertOrderMaster" parameterType="com.ghy.order.domain.OrderMaster" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO order_master(
        <if test="code != null ">code,</if>
        <if test="customerId != null and customerId != ''">customer_id,</if>
        <if test="orderType != null and orderType != ''">order_type,</if>
        <if test="orderStatus != null and orderStatus != ''">order_status,</if>
        <if test="payType != null and payType != ''">pay_type,</if>
        <if test="payStatus != null and payStatus != ''">pay_status,</if>
        <if test="workerId != null and workerId != ''">worker_id,</if>
        <if test="payTime != null and payTime != ''">pay_time,</if>
        <if test="revTime != null and revTime != ''">rev_time,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )VALUES(
        <if test="code != null">#{code},</if>
        <if test="customerId != null and customerId != ''">#{customerId},</if>
        <if test="orderType != null and orderType != ''">#{orderType},</if>
        <if test="orderStatus != null and orderStatus != ''">#{orderStatus},</if>
        <if test="payType != null and payType != ''">#{payType},</if>
        <if test="payStatus != null and payStatus != ''">#{payStatus},</if>
        <if test="workerId != null and workerId != ''">#{workerId},</if>
        <if test="payTime != null and payTime != ''">#{payTime},</if>
        <if test="revTime != null and revTime != ''">#{revTime},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        SYSDATE()
        )
    </insert>

    <select id="checkOrderMasterCodeUnique" parameterType="String" resultMap="OrderMasterResult">
        <include refid="selectOrderMaster"/>
        WHERE `code` =#{orderMasterCode} LIMIT 1
    </select>

    <select id="selectByCode" parameterType="String" resultType="com.ghy.order.domain.OrderMaster">
        <include refid="selectOrderMaster"/>
        WHERE `code` = #{orderMasterCode}
    </select>

</mapper>
