<?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.bonus.zlpt.order.mapper.OrderInfoMapper">
    
    <resultMap type="com.bonus.zlpt.common.core.domain.order.OrderInfo" id="OrderInfoResult">
        <result property="orderId"    column="order_id"    />
        <result property="pId"    column="p_id"    />
        <result property="code"    column="code"    />
        <result property="time"    column="time"    />
        <result property="endTime"    column="end_time"    />
        <result property="deposit"    column="deposit"    />
        <result property="cost"    column="cost"    />
        <result property="payType"    column="pay_type"    />
        <result property="supplier"    column="supplier"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="orderUser"    column="order_user"    />
        <result property="orderCompany"    column="order_company"    />
    </resultMap>

    <sql id="selectOrderInfoVo">
        select order_id, p_id, code, time, end_time, deposit, cost, pay_type, supplier, order_status, reject_reason, order_user, order_company from ma_order_info
    </sql>

    <select id="selectOrderInfoList" parameterType="com.bonus.zlpt.common.core.domain.order.vo.OrderInfoVo" resultType="com.bonus.zlpt.common.core.domain.order.vo.OrderInfoVo">
        select o.*,d.phone as order_phone,d.ma_id,co.company_name as supplier_company
        from ma_order_info o
        left join ma_order_details d on o.order_id=d.order_id
        left join bm_company_info co on o.supplier=co.company_id
        <where>  
            <if test="orderId != null "> and o.order_id = #{orderId}</if>
            <if test="pId != null "> and o.p_id = #{pId}</if>
            <if test="code != null  and code != ''"> and o.code = #{code}</if>
            <if test="time != null  and time != ''"> and o.time = #{time}</if>
            <if test="deposit != null "> and o.deposit = #{deposit}</if>
            <if test="cost != null "> and o.cost = #{cost}</if>
            <if test="payType != null  and payType != ''"> and o.pay_type = #{payType}</if>
            <if test="supplier != null  and supplier != ''"> and o.supplier = #{supplier}</if>
            <if test="orderStatus != null  and orderStatus != ''"> and o.order_status = #{orderStatus}</if>
            <if test="orderUser != null "> and o.order_user = #{orderUser}</if>
            <if test="orderCompany != null  and orderCompany != ''"> and o.order_company = #{orderCompany}</if>
        </where>
    </select>
    
    <select id="selectOrderInfoByOrderId" parameterType="Long" resultType="com.bonus.zlpt.common.core.domain.order.vo.OrderInfoVo">
        select d.*,o.code,o.time,o.order_status,o.reject_reason,s.file_url as contract_url
        from ma_order_info o
                 left join ma_order_details d on d.order_id = o.order_id
                 left join sys_file_info s on s.model_id = o.order_id
        where s.dic_id = 21 and o.order_id = #{orderId}
    </select>
        
    <insert id="insertOrderInfo" parameterType="com.bonus.zlpt.common.core.domain.order.vo.OrderInfoVo">
        insert into ma_order_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="pId != null">p_id,</if>
            <if test="code != null">code,</if>
            <if test="time != null">time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="deposit != null">deposit,</if>
            <if test="cost != null">cost,</if>
            <if test="payType != null">pay_type,</if>
            <if test="supplier != null">supplier,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="rejectReason != null">reject_reason,</if>
            <if test="orderUser != null">order_user,</if>
            <if test="orderCompany != null">order_company,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="pId != null">#{pId},</if>
            <if test="code != null">#{code},</if>
            <if test="time != null">#{time},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="deposit != null">#{deposit},</if>
            <if test="cost != null">#{cost},</if>
            <if test="payType != null">#{payType},</if>
            <if test="supplier != null">#{supplier},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="rejectReason != null">#{rejectReason},</if>
            <if test="orderUser != null">#{orderUser},</if>
            <if test="orderCompany != null">#{orderCompany},</if>
         </trim>
    </insert>

    <insert id="insertOrderDetails" parameterType="com.bonus.zlpt.common.core.domain.order.vo.OrderInfoVo">
        insert into ma_order_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="needCompany != null">need_company,</if>
            <if test="planStartTime != null">plan_start_time,</if>
            <if test="isMachinist != null">is_machinist,</if>
            <if test="addressId != null">address_id,</if>
            <if test="address != null">address,</if>
            <if test="duration != null">duration,</if>
            <if test="durationType != null">duration_type,</if>
            <if test="invoiceType != null">invoice_type,</if>
            <if test="description != null">description,</if>
            <if test="maId != null">ma_id,</if>
            <if test="leaseType != null">lease_type,</if>
            <if test="leasePrice != null">lease_price,</if>
            <if test="machinistPrice != null">machinist_price,</if>
            <if test="orderContract != null">order_contract,</if>
            <if test="machinistName != null">machinist_name,</if>
            <if test="phone != null">phone,</if>
            <if test="logisticsPhone != null">logistics_phone,</if>
            <if test="realStartTime != null">real_start_time,</if>
            <if test="renterName != null">renter_name,</if>
            <if test="tenantName != null">tenant_name,</if>
            <if test="entryAttachment != null">entry_attachment,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="needCompany != null">#{needCompany},</if>
            <if test="planStartTime != null">#{planStartTime},</if>
            <if test="isMachinist != null">#{isMachinist},</if>
            <if test="addressId != null">#{addressId},</if>
            <if test="address != null">#{address},</if>
            <if test="duration != null">#{duration},</if>
            <if test="durationType != null">#{durationType},</if>
            <if test="invoiceType != null">#{invoiceType},</if>
            <if test="description != null">#{description},</if>
            <if test="maId != null">#{maId},</if>
            <if test="leaseType != null">#{leaseType},</if>
            <if test="leasePrice != null">#{leasePrice},</if>
            <if test="machinistPrice != null">#{machinistPrice},</if>
            <if test="orderContract != null">#{orderContract},</if>
            <if test="machinistName != null">#{machinistName},</if>
            <if test="phone != null">#{phone},</if>
            <if test="logisticsPhone != null">#{logisticsPhone},</if>
            <if test="realStartTime != null">#{realStartTime},</if>
            <if test="renterName != null">#{renterName},</if>
            <if test="tenantName != null">#{tenantName},</if>
            <if test="entryAttachment != null">#{entryAttachment},</if>
        </trim>
    </insert>

    <update id="updateOrderInfo" parameterType="com.bonus.zlpt.common.core.domain.order.vo.OrderInfoVo">
        update ma_order_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="pId != null">p_id = #{pId},</if>
            <if test="code != null">code = #{code},</if>
            <if test="time != null">time = #{time},</if>
            <if test="endTime != null">time = #{endTime},</if>
            <if test="deposit != null">deposit = #{deposit},</if>
            <if test="cost != null">cost = #{cost},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="supplier != null">supplier = #{supplier},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="rejectReason != null">reject_reason = #{rejectReason},</if>
            <if test="orderUser != null">order_user = #{orderUser},</if>
            <if test="orderCompany != null">order_company = #{orderCompany},</if>
        </trim>
        where order_id = #{orderId}
    </update>

    <update id="updateOrderDetails" parameterType="com.bonus.zlpt.common.core.domain.order.vo.OrderInfoVo">
        update ma_order_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="needCompany != null">need_company = #{needCompany},</if>
            <if test="planStartTime != null">plan_start_time = #{planStartTime},</if>
            <if test="isMachinist != null">is_machinist = #{isMachinist},</if>
            <if test="addressId != null">address_id = #{addressId},</if>
            <if test="address != null">address = #{address},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="durationType != null">duration_type = #{durationType},</if>
            <if test="invoiceType != null">invoice_type = #{invoiceType},</if>
            <if test="description != null">description = #{description},</if>
            <if test="maId != null">ma_id = #{maId},</if>
            <if test="leaseType != null">lease_type = #{leaseType},</if>
            <if test="leasePrice != null">lease_price = #{leasePrice},</if>
            <if test="machinistPrice != null">machinist_price = #{machinistPrice},</if>
            <if test="orderContract != null">order_contract = #{orderContract},</if>
            <if test="machinistName != null">machinist_name = #{machinistName},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="logisticsPhone != null">logistics_phone = #{logisticsPhone},</if>
            <if test="realStartTime != null">real_start_time = #{realStartTime},</if>
            <if test="renterName != null">renter_name = #{renterName},</if>
            <if test="tenantName != null">tenant_name = #{tenantName},</if>
            <if test="entryAttachment != null">entry_attachment = #{entryAttachment},</if>
        </trim>
        where order_id = #{orderId}
    </update>

    <delete id="deleteOrderInfoByOrderId" parameterType="Long">
        delete from ma_order_info where order_id = #{orderId}
    </delete>

    <delete id="deleteOrderInfoByOrderIds" parameterType="String">
        delete from ma_order_info where order_id in 
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </delete>

    <select id="selectLastInsertId" resultType="Long">
        SELECT LAST_INSERT_ID()
    </select>

    <select id="count" parameterType="Long" resultType="Long">
        SELECT count(*) from ma_order_info where order_user = #{userId}
    </select>
</mapper>