<?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.OrderAccountMapper">
    <select id="getOrderAccountByOrderType" parameterType="com.xxfc.platform.order.pojo.Term"
            resultType="com.xxfc.platform.order.pojo.account.OrderAccountDTO">
        SELECT
        <if test="subdivide !=null and subdivide ==1">
            date( FROM_UNIXTIME( a.crt_time / 1000 ) ) as oneDay,
        </if>
        a.*
        <if test="orderType != null and orderType == 1">
            , v.start_company_id as companyId
        </if>
        <if test="orderType != null and orderType == 2">
            , t.start_company_id as companyId
        </if>
        FROM
        order_account a
        LEFT JOIN base_order b ON a.order_id = b.id
        <if test="orderType != null and orderType==1">
            LEFT JOIN order_rent_vehicle_detail v ON b.id = v.order_id
        </if>
        <if test="orderType != null and orderType==2">
            LEFT JOIN order_tour_detail t ON b.id = t.order_id
        </if>
        WHERE
        a.account_status=1
        AND
        b.type = #{orderType}
        <if test="startTime != null">
            AND a.crt_time <![CDATA[>= ]]>  #{startTime}
        </if>
        <if test="endTime != null">
            AND a.crt_time &gt; endTime
        </if>
        <if test="companyIds != null and  companyIds.size() != 0 ">
            <if test="orderType != null and orderType == 1">
                AND v.start_company_id in
                <foreach collection="companyIds" index="index" item="companyId" open="(" separator="," close=")">
                    #{companyId}
                </foreach>
            </if>
            <if test="orderType != null and orderType == 2">
                AND t.start_company_id in
                <foreach collection="companyIds" index="index" item="companyId" open="(" separator="," close=")">
                    #{companyId}
                </foreach>
            </if>
        </if>
        <if test="subdivide !=null and subdivide ==1">
            AND date( FROM_UNIXTIME( a.crt_time / 1000 ) ) = date( DATE_SUB( now( ), INTERVAL #{day} DAY ) )
        </if>
    </select>
    <select id="outStatisticalData" resultType="com.xxfc.platform.order.pojo.ReturnOrderAmount">
        select oa.crt_time as time ,oupc.no,oupc.type,oa.account_detail as accountDetail ,oupc.realname,oupc.username,oa.account_type
        from order_account oa
        left join order_user_position_company oupc ON oa.order_id=oupc.id
        WHERE oa.account_type!=101
        <if test="startTime != null">
            and oa.crt_time <![CDATA[>=]]>  #{startTime}
        </if>
        <if test="endTime != null">
            and oa.crt_time <![CDATA[<=]]>  #{endTime}
        </if>
        <if test="queryCriteria.jurisdiction != null and queryCriteria.jurisdiction.size() !=0 ">
            and oupc.company_id in
            <foreach collection="queryCriteria.jurisdiction" index="index" item="companyId" open="(" separator="," close=")">
                #{companyId}
            </foreach>
        </if>
        <if test="queryCriteria.orderType != null">
            and oupc.type = #{queryCriteria.orderType}
        </if>
        order by oa.crt_time DESC
    </select>

    <select id="selectOrderAccountByOrderTypeAndStartTimeAndEndTime"
            resultType="com.xxfc.platform.order.pojo.account.OrderAccountBo">
        SELECT
            oa.*,
            bo.id,
            bo.`status`,
            bo.order_origin,
            bo.pay_way,
            bo.has_pay,
            IFNULL(brvd.start_company_id,IFNULL(otd.start_company_id,bo.parent_user_company_id)) AS `companyId`,
            brvd.cost_detail AS `costDetail`,
            brvd.damage_safe AS `damageSafe`,
            omd.member_level AS `memberLevel`

        FROM
            `order_account` AS `oa`
            LEFT JOIN `base_order` AS `bo` ON bo.id = oa.order_id
            LEFT  JOIN `order_rent_vehicle_detail` AS `brvd` ON brvd.order_id=oa.order_id
            LEFT JOIN `order_tour_detail` AS `otd` ON otd.order_id=oa.order_id
            LEFT JOIN `order_member_detail` AS `omd` ON omd.order_id=oa.order_id
            WHERE bo.type=#{orderType} AND (`oa`.account_status=1 or(bo.pay_way=0 AND bo.has_pay=1 AND oa.account_status=0)) AND oa.`crt_time` BETWEEN #{startTime} AND #{endTime}
    </select>

    <select id="selectByDateAndStatffIds" resultType="com.xxfc.platform.order.pojo.account.OrderAccountBo">
        oa.*,
        bo.id,
        bo.`status`,
        bo.order_origin,
        bo.pay_way,
        bo.has_pay,
        bo.parent_user_id as `userId`,
        bo.parent_position_id as `postionId`,
        IFNULL(brvd.start_company_id,IFNULL(otd.start_company_id,bo.parent_user_company_id)) AS `companyId`,
        brvd.cost_detail_extend AS `costDetailExtend`,
        brvd.damage_safe AS `damageSafe`,
        omd.member_level AS `memberLevel`

        FROM
        `order_account` AS `oa`
        LEFT JOIN `base_order` AS `bo` ON bo.id = oa.order_id
        LEFT  JOIN `order_rent_vehicle_detail` AS `brvd` ON brvd.order_id=oa.order_id
        LEFT JOIN `order_tour_detail` as `otd` ON otd.order_id=oa.order_id
        LEFT JOIN `order_member_detail` AS `omd` ON omd.order_id=oa.order_id
        WHERE  `oa`.account_status=1
        AND oa.`crt_time` BETWEEN #{startDate} AND #{endDate}
        <if test="staffUserIds!=null and staffUserIds.size>0">
            and bo.id IN <foreach collection="staffUserIds" item="staffId" open="(" close=")" separator=",">
            #{staffId}
        </foreach>
        </if>

    </select>
</mapper>