<?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.cf.imes.module.executor.dal.mysql.orderBody.OrderBodyMapper">

    <update id="updatePlateNumById" >
        update `order_body` set plate_num = #{plateNum} where id = #{orderId}
    </update>

    <delete id="deleteAllByOrderId">
        DELETE order_parts, order_module_extra, order_plate, order_item, order_group, order_body
            FROM order_body
            LEFT JOIN order_group ON order_group.body_id = order_body.id
            LEFT JOIN order_item ON order_item.body_id = order_body.id
            LEFT JOIN order_plate ON order_plate.id = order_item.plate_id
            LEFT JOIN order_parts ON order_parts.id = order_item.parts_id
            LEFT JOIN order_module_extra ON order_module_extra.body_id = order_body.id
            WHERE order_body.id = #{bodyId} AND order_body.order_id = #{orderId};
    </delete>

    <select id="selectModuleByOrderId" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderModuleRespVO">
        SELECT b.order_id, b.room_id,b.id AS body_id, g.id as group_id, g.name as group_name, g.group_type_id,g.group_type_name,
               g.plate_num AS group_num,b.plate_num AS body_num
        FROM `order_body` b
                 LEFT JOIN `order_group` g ON b.organ_id = g.organ_id AND b.order_id = g.order_id AND b.id = g.body_id
        WHERE b.organ_id = #{organId} and b.order_id = #{orderId} and  b.deleted = #{deleted}
    </select>

    <select id="getOrderBodyByOrderId" resultType="com.cf.imes.module.executor.dal.dataobject.orderBody.OrderBodyDO">
        SELECT b.id, b.order_id, b.room_id, b.name, b.width, b.height, b.depth, b.filename, b.remark, b.creator, b.create_time, b.updater, b.update_time, b.deleted, b.plate_num
        FROM `order_body` b
        WHERE b.order_id = #{orderId} and b.organ_id = #{organId}
    </select>



    <select id="selectRoomAndBody"
            parameterType="java.lang.Long"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.product.OrderRoomBodyRespVO">


        select id as bodyId,
               name as bodyName,
               room_id as roomId,
               room_name as roomName

        from order_body

        where organ_id = #{organId}

          and order_id in

            <foreach item="orderIds" collection="orderIds" open="(" separator="," close=")">
                 #{orderIds}
            </foreach>



    </select>


    <select id="selectPlanBodyRoomDataSource" resultType="java.util.Map">


        select distinct ${filed} from order_body
                                 where organ_id = #{organId}
                                   and deleted = false
                                   and id  in
                        <foreach item="bodyIds" collection="bodyIds" open="(" separator="," close=")">
                                    #{bodyIds}
                        </foreach>


    </select>

    <select id="selectPlateNumAndArea" resultType="com.cf.imes.module.executor.dal.dataobject.orderBody.OrderBodyPlateNumDO">
        SELECT
        oi.body_id AS order_body_id,
        COUNT(*) AS num
        <choose>
            <when test="delete">
                FROM order_item_temp oi
            </when>
            <otherwise>
                FROM order_item oi
            </otherwise>
        </choose>
        JOIN order_plate op
        ON op.organ_id = oi.organ_id
        AND op.order_id = oi.order_id
        AND op.id = oi.plate_id
        AND op.deleted = #{delete}
        WHERE oi.body_id IN
        <foreach collection="orderBodyIds" item="orderBodyId" open="(" close=")" separator=",">
            #{orderBodyId}
        </foreach>
        GROUP BY oi.body_id
    </select>

    <delete id="physicalDeleteByOrderIds">
        delete from order_body
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>