<?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>
            <case value="3" resultType="com.xxfc.platform.order.pojo.order.OrderPageVO">
                <association column="detail_id" property="orderMemberDetail"
                             select="com.xxfc.platform.order.mapper.OrderMemberDetailMapper.selectByPrimaryKey"
                             javaType="com.xxfc.platform.order.entity.OrderMemberDetail"></association>
            </case>
        </discriminator>
    </resultMap>


    <resultMap type="com.xxfc.platform.order.pojo.order.OrderListVo" id="orderListMap">
        <result javaType="Integer" column="type" property="type"></result>
        <discriminator javaType="Integer" column="type">
            <case value="1" resultType="com.xxfc.platform.order.pojo.order.OrderListVo">
                <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.OrderListVo">
                <association column="detail_id" property="orderTourDetail"
                             select="com.xxfc.platform.order.mapper.OrderTourDetailMapper.selectByPrimaryKey"
                             javaType="com.xxfc.platform.order.entity.OrderTourDetail"></association>
            </case>
            <case value="3" resultType="com.xxfc.platform.order.pojo.order.OrderListVo">
                <association column="detail_id" property="orderMemberDetail"
                             select="com.xxfc.platform.order.mapper.OrderMemberDetailMapper.selectByPrimaryKey"
                             javaType="com.xxfc.platform.order.entity.OrderMemberDetail"></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>
            <if test="type != null">
                and type = #{type}
            </if>
            <if test="hasMemberRight != null">
                and has_member_right = #{hasMemberRight}
            </if>
            <if test="multiStatus != null">
                and
                (
                <foreach collection="multiStatus.split(',')" index="sIndex" item="statusEntity" separator=" or ">
                    status = #{statusEntity}
                </foreach>
                )
            </if>
            <if test="multiTypes != null">
                and
                (
                <foreach collection="multiTypes.split(',')" index="tIndex" item="typeEntity" separator=" or ">
                    type = #{typeEntity}
                </foreach>
                )
            </if>
        </where>
        order by crt_time desc
    </select>

    <select id="listOrder" parameterType="Map" resultMap="orderListMap">
        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
        LEFT JOIN order_member_detail m on m.order_id = b.id
        <where>
            <if test="crtUser != null">
                and b.crt_user = #{crtUser}
            </if>
            <if test="userIds != null and userIds.size() > 0">
                and b.user_id in
                <foreach collection="userIds" item="item"  open="("  separator="," close=")">
                    #{item}
                </foreach>

            </if>
            <if test="status != null">
                and b.status = #{status}
            </if>
            <if test="type != null">
                and b.type = #{type}
            </if>
            <if test="memberLevel != null and memberLevel != ''">
                and m.member_level = #{memberLevel}
            </if>
            <if test="no != null and no != '' ">
                and no like CONCAT ("%", #{no}, "%")
            </if>
            <if test="name != null">
                and b.name like CONCAT ("%", #{name}, "%")
            </if>
            <if test="startTime != null">
                and (r.start_time between #{startTime} and #{endTime}
                or t.start_time between #{startTime} and #{endTime})
            </if>
            <if test="vehicleIds != null and vehicleIds.size() > 0">
                AND r.vehicle_id IN <foreach collection="vehicleIds" item="vehicleId" open="(" close=")" separator=",">
                #{vehicleId}
            </foreach>
            </if>
            <if test="companyIds != null and companyIds.size > 0">
                and (r.start_company_id in
                <foreach collection="companyIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
                or t.start_company_id in
                <foreach collection="companyIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
                )
            </if>
            <if test="zoneIds != null and zoneIds.size > 0">
                and (r.start_zone_id in
                <foreach collection="zoneIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
                or t.start_zone_id in
                <foreach collection="zoneIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
                )
            </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="userIds != null and userIds.size > 0">
            and b.user_id in
            <foreach collection="userIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="status != null and status == -1">
            AND b.status = -1
            and b.refund_status in (0,2)
        </if>
        <if test="status != null and status != -1">
            and b.status = #{status}
        </if>
        <if test="no != null and no != ''">
            and no like CONCAT ("%", #{no}, "%")
        </if>
        <if test="startTime != null and status == 4">
            and r.start_time between #{startTime} and #{endTime}
        </if>
        <if test="startTime != null and (status == 5 || status == 6 || status == -1)">
            and r.end_time between #{startTime} and #{endTime}
        </if>
        <if test="companyIds != null and companyIds.size > 0">
            and r.start_company_id in
            <foreach collection="companyIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>

        </if>
        <if test="status == 4">
            order by r.start_time
        </if>
        <if test="status == 5 || status == -1">
            order by r.end_time
        </if>

    </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 *
        from base_order
        where no = #{no}
    </select>


</mapper>