<?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.xxfc.platform.order.mapper.BaseOrderMapper">

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.xxfc.platform.order.entity.BaseOrder" id="baseOrderMap">
        <result property="id" column="id"/>
        <result property="no" column="no"/>
        <result property="type" column="type"/>
        <result property="detailId" column="detail_id"/>
        <result property="status" column="status"/>
        <result property="productAmount" column="product_amount"/>
        <result property="orderAmount" column="order_amount"/>
        <result property="detailJson" column="detail_json"/>
        <result property="thirdType" column="third_type"/>
        <result property="outTradeNo" column="out_trade_no"/>
        <result property="crtTime" column="crt_time"/>
        <result property="crtUser" column="crt_user"/>
        <result property="crtName" column="crt_name"/>
        <result property="crtHost" column="crt_host"/>
        <result property="updTime" column="upd_time"/>
        <result property="updUser" column="upd_user"/>
        <result property="updName" column="upd_name"/>
        <result property="updHost" column="upd_host"/>
    </resultMap>

    <resultMap type="com.xxfc.platform.order.pojo.order.OrderPageVO" id="orderPageMap">
        <result javaType="Integer" column="type" property="type"></result>
        <discriminator javaType="Integer" column="type">
            <case value="1" resultType="com.xxfc.platform.order.pojo.order.OrderPageVO">
                <association column="{id=detail_id}" property="orderRentVehicleDetail" select="com.xxfc.platform.order.mapper.OrderRentVehicleDetailMapper.pageByParm" javaType="com.xxfc.platform.order.entity.OrderRentVehicleDetail"></association>
            </case>
            <case value="2" resultType="com.xxfc.platform.order.pojo.order.OrderPageVO">
                <association column="detail_id" property="orderTourDetail" select="com.xxfc.platform.order.mapper.OrderTourDetailMapper.selectByPrimaryKey" javaType="com.xxfc.platform.order.entity.OrderTourDetail"></association>
            </case>
        </discriminator>
    </resultMap>
    
    <select id="pageByParm" parameterType="Map" resultMap="orderPageMap">
        select *
            from base_order
        <where>
            <if test="crtUser != null">
                and crt_user = #{crtUser}
            </if>
            <if test="crtCompanyId != null">
                and crt_company_id = #{crtCompanyId}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="no != null">
                and no = #{no}
            </if>
        </where>
        order by crt_time desc
    </select>

    <select id="listOrder" parameterType="Map" resultMap="orderPageMap">
        select b.*
        from base_order b
        LEFT JOIN order_rent_vehicle_detail r on r.order_id = b.id
        LEFT JOIN order_tour_detail t on t.order_id = b.id
        <where>
            <if test="crtUser != null">
                and b.crt_user = #{crtUser}
            </if>
            <if test="status != null">
                and b.status = #{status}
            </if>
             <if test="type != null">
                and b.type = #{type}
            </if>
            <if test="no != null">
                and no like CONCAT ("%", #{no}, "%")
            </if>
            <if test="startTime != null">
                and r.start_time between  #{startTime}  and  #{endTime}
                or t.start_time between  #{startTime}  and  #{endTime}
            </if>
            <if test="startCompanyId != null">
                and r.start_company_id = #{startCompanyId}
                or t.start_company_id = #{startCompanyId}
            </if>
            <if test="startZoneId != null">
                and r.start_zone_id = #{startZoneId}
                or t.start_zone_id = #{startZoneId}
            </if>
        </where>
        order by b.crt_time desc
    </select>

    <select id="getRentVehicle" parameterType="Map" resultMap="orderPageMap">
        select b.*
        from base_order b
        LEFT JOIN order_rent_vehicle_detail r on r.order_id = b.id
        where b.type = 1
            <if test="userId != null">
                and b.user_id = #{userId}
            </if>
            <if test="status != null">
                and b.status = #{status}
            </if>
            <if test="no != null">
                and no like CONCAT ("%", #{no}, "%")
            </if>
            <if test="startTime != null">
                and r.start_time between  #{startTime}  and  #{endTime}
            </if>
            <if test="startCompanyId != null">
                and r.start_company_id = #{startCompanyId}
            </if>
        order by b.crt_time desc
    </select>

    <select id="getTourList" parameterType="Map" resultMap="orderPageMap">
        select b.*
        from base_order b
        LEFT JOIN order_tour_detail r on r.order_id = b.id
        where b.type = 2
            <if test="userId != null">
                and b.user_id = #{userId}
            </if>
            <if test="status != null">
                and b.status = #{status}
            </if>
            <if test="no != null">
                and no like CONCAT ("%", #{no}, "%")
            </if>
            <if test="startTime != null">
                and r.start_time between  #{startTime}  and  #{endTime}
            </if>
            <if test="startCompanyId != null">
                and r.start_company_id = #{startCompanyId}
            </if>
        order by b.crt_time desc
    </select>

    <select id="getOrderDetail" parameterType="java.lang.String" resultMap="orderPageMap">
        select b.*
        from base_order b
        where no = #{no}
    </select>

</mapper>