<?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.yc.ship.module.trade.dal.mysql.order.TradeOrderRoomModelMapper">


    <select id="selectListByOrderId" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderRoomModelDO">
        select t1.id,t1.order_id,t1.order_no,t1.room_model_id,t2.short_name room_model_name,t1.floor,t1.original_price,t1.discount,t1.unit_price,t1.real_money,t1.use_room_num,t1.country_plus_amount,t1.country_plus_num,t1.adult_take_num,t1.adult_plus_num,t1.child_non_take_num,t1.child_take_num,t1.child_plus_num,t1.baby_non_take_num,t1.baby_take_num,t1.baby_plus_num,t1.floor_price,t1.floor_num,t1.room_model_type_id,t1.tenant_id,t1.creator,t1.create_time,t1.update_time,t1.deleted,t1.room_index_id from trade_order_room_model t1 inner join resource_room_model t2 on t1.room_model_id = t2.id
        where t1.deleted =0 and t1.order_id = #{orderId}
    </select>

    <select id="selectListByOrderIds" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderRoomModelDO">
        select t1.id,t1.order_id,t1.order_no,t1.room_model_id,t2.short_name room_model_name,t1.floor,t1.original_price,t1.discount,t1.unit_price,t1.real_money,t1.use_room_num,t1.country_plus_amount,t1.country_plus_num,t1.adult_take_num,t1.adult_plus_num,t1.child_non_take_num,t1.child_take_num,t1.child_plus_num,t1.baby_non_take_num,t1.baby_take_num,t1.baby_plus_num,t1.floor_price,t1.floor_num,t1.room_model_type_id,t1.tenant_id,t1.creator,t1.create_time,t1.update_time,t1.deleted,t1.room_index_id from trade_order_room_model t1 inner join resource_room_model t2 on t1.room_model_id = t2.id
        where t1.deleted =0 and t1.order_id in
        <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
            #{item}
        </foreach>

    </select>
    <select id="selectOrderShareStatsByOrderIds"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.OrderShareVO">
        select t1.order_id,if(t2.gender=0,'女','男') gender,count(t2.id) num
        from trade_order_room_model t1
            LEFT JOIN trade_visitor t2
                on t1.room_index_id = t2.room_index_id
        where t1.use_room_num = 0.5 and t1.deleted = 0 and t2.deleted = 0
        and t1.order_id in
        <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
            #{item}
        </foreach>
        GROUP BY t1.order_id,t2.gender
    </select>
    <select id="selectOrderShareStatsByOrderIds1"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.OrderShareVO">
        select if(t2.gender=0,'女','男') gender,count(t2.id) num
        from trade_order_room_model t1
        LEFT JOIN trade_visitor t2
        on t1.room_index_id = t2.room_index_id
        where t1.use_room_num = 0.5 and t1.deleted = 0 and t2.deleted = 0
        and t1.order_id in
        <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
            #{item}
        </foreach>
        GROUP BY t2.gender
    </select>
    <select id="selectRoomDesc" resultType="com.yc.ship.module.trade.controller.app.otc.vo.AppRoomDescVO">
        SELECT
            t1.order_id,
            t4.floor,
            t2.NAME room_model_name,
            t3.room_num
        FROM
            ( SELECT room_index_id, order_id, IFNULL( final_room_id, init_room_id ) room_id, room_model_id, floor FROM trade_visitor where order_id in
             <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                 #{item}
             </foreach>
            <if test="keyword != null and keyword != ''">
                and (mobile = #{keyword} or credential_no = #{keyword})
            </if>
             GROUP BY room_index_id ) t1
                LEFT JOIN trade_order_room_model t4 on t1.room_index_id = t4.room_index_id
                LEFT JOIN resource_room_model t2 ON t4.room_model_id = t2.id
                LEFT JOIN resource_room t3 ON t1.room_id = t2.id
    </select>
    <select id="selectOrderRoomModelStatsByOrderIds"
            resultType="com.yc.ship.module.trade.controller.admin.order.vo.order.OrderTotalRoomModelVO">
        SELECT
            a1.room_model_id,
            a10.name room_model_name,
        CONCAT(a1.floor,'F') floor,
            ifnull(a11.adultTakeNum,0) adultTakeNum,
        ifnull(a5.adultShareNum,0) adultShareNum,
        ifnull(a2.adultAloneNum,0) adultAloneNum,
        ifnull(a3.adultPlusNum,0) adultPlusNum,
        ifnull(a3.childNonTakeNum,0) childNonTakeNum,
        ifnull(a3.childTakeNum,0) childTakeNum,
        ifnull(a3.childPlusNum,0) childPlusNum,
        ifnull(a3.babyNonTakeNum,0) babyNonTakeNum,
        ifnull(a4.totalNum,0) totalNum,
        ifnull(a6.leaveRoomNum,0) leaveRoomNum,
        ifnull(a7.lockRoomNum,0) lockRoomNum,
        ifnull(a8.shareNum,0) shareNum,
        ifnull(a9.distributorNum,0) distributorNum
        FROM
            ( SELECT room_model_id, floor FROM trade_order_room_model WHERE deleted = 0 and order_id in
              <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                  #{item}
              </foreach>
            GROUP BY room_model_id, floor ) a1
                LEFT JOIN (
                SELECT
                    room_model_id,
                    floor,
                    sum( adult_take_num ) adultAloneNum
                FROM
                    trade_order_room_model
                WHERE
                    deleted = 0
                  AND use_room_num = 1
                  AND adult_take_num = 1
                and order_id in
                <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                    #{item}
                </foreach>
                GROUP BY
                    room_model_id,
                    floor
            ) a2 ON a1.room_model_id = a2.room_model_id
                AND a1.floor = a2.floor
                LEFT JOIN (
                SELECT
                    room_model_id,
                    floor,
                    sum( adult_plus_num ) adultPlusNum,
                    sum( child_non_take_num ) childNonTakeNum,
                    sum( child_take_num ) childTakeNum,
                    sum( child_plus_num ) childPlusNum,
                    sum( baby_non_take_num ) babyNonTakeNum
                FROM
                    trade_order_room_model
                WHERE
                    deleted = 0
                and order_id in
                <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                    #{item}
                </foreach>
                GROUP BY
                    room_model_id,
                    floor
            ) a3 ON a1.room_model_id = a3.room_model_id
                AND a1.floor = a3.floor
                LEFT JOIN (
                SELECT
                    room_model_id,
                    floor,
                    sum( adult_take_num + adult_plus_num + child_non_take_num + child_take_num + child_plus_num + baby_non_take_num ) totalNum
                FROM
                    trade_order_room_model
                WHERE
                    deleted = 0
                and order_id in
                <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                    #{item}
                </foreach>
                GROUP BY
                    room_model_id,
                    floor
            ) a4 ON a1.room_model_id = a4.room_model_id
                AND a1.floor = a4.floor
                LEFT JOIN ( SELECT room_model_id, floor, sum( adult_take_num ) adultShareNum
                FROM trade_order_room_model
                WHERE deleted = 0 AND use_room_num = 0.5
                and order_id in
                <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                    #{item}
                </foreach>
                GROUP BY room_model_id, floor ) a5 ON a1.room_model_id = a5.room_model_id and a1.floor = a5.floor
                LEFT JOIN (
                SELECT
                room_model_id,
                floor,
                sum( adult_take_num ) adultTakeNum
                FROM
                trade_order_room_model
                WHERE
                deleted = 0
                AND use_room_num = 1
                and adult_take_num > 1
                AND order_id IN
                <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                    #{item}
                </foreach>
                GROUP BY
                room_model_id,
                floor
                ) a11 ON a1.room_model_id = a11.room_model_id
                AND a1.floor = a11.floor
                LEFT JOIN (
                SELECT
                    room_model_id,
                    floor,
                    sum( use_room_num ) leaveRoomNum
                FROM
                    trade_order_room_model
                WHERE
                order_id IN ( SELECT id FROM trade_order WHERE order_status = 14 AND deleted = 0 )
                and order_id in
                <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                    #{item}
                </foreach>
                AND deleted = 0
                GROUP BY
                    room_model_id,
                    floor
            ) a6 ON a1.room_model_id = a6.room_model_id
                AND a1.floor = a6.floor
                LEFT JOIN (
                SELECT
                    room_model_id,
                    floor,
                    sum( use_room_num ) lockRoomNum
                FROM
                    trade_order_room_model
                WHERE
                        order_id IN ( SELECT id FROM trade_order WHERE order_status IN ( 1, 6 ) AND deleted = 0 )
                  AND deleted = 0
                and order_id in
                <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                    #{item}
                </foreach>
                GROUP BY
                    room_model_id,
                    floor
            ) a7 ON a1.room_model_id = a7.room_model_id
                AND a1.floor = a7.floor
                LEFT JOIN (
                SELECT
                    room_model_id,
                    floor,
                    sum( real_num + virtual_num ) shareNum
                FROM
                    product_voyage_stock_detail_log
                WHERE
                    deleted = 0 and (real_num>0 or virtual_num > 0)
                  AND stock_type = 1
                and order_id in
                <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                    #{item}
                </foreach>
                GROUP BY
                    room_model_id,
                    floor
            ) a8 ON a1.room_model_id = a8.room_model_id
                AND a1.floor = a8.floor
                LEFT JOIN (
                SELECT
                    room_model_id,
                    floor,
                    sum( real_num + virtual_num ) distributorNum
                FROM
                    product_voyage_stock_detail_log
                WHERE
                    deleted = 0
                  AND stock_type = 2  and (real_num>0 or virtual_num > 0)
                and order_id in
                <foreach item="item" collection="orderIds" separator="," open="(" close=")" index="index">
                    #{item}
                </foreach>
                GROUP BY
                    room_model_id,
                    floor
            ) a9 ON a1.room_model_id = a9.room_model_id
                AND a1.floor = a9.floor
                LEFT JOIN resource_room_model a10 on a1.room_model_id = a10.id
        ORDER BY a1.room_model_id asc,a1.floor asc
    </select>


</mapper>