<?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.ordercomponent.OrderComponentMapper">
    <select id="selectAllForFlatList"
            resultType="com.cf.imes.module.executor.dal.dataobject.ordercomponent.OrderComponentDO">
        SELECT component.id,
               component.level,
               component.name
        FROM (
            SELECT oc.id,
                   oc.level,
                   oc.name,
                   CASE
                       WHEN oc.pid = 0 AND oc.extra_process_status = 0 THEN 0
                       WHEN oc.pid = 0 AND oc.extra_process_status = 3 THEN 3
                       WHEN oc.process_status = 2 THEN 2
                       WHEN oc.process_status = 1
                            AND (oc.extra_process_status IS NULL OR oc.extra_process_status != 0) THEN 1
                       ELSE -1
                   END AS process_status
            FROM order_component oc
            WHERE oc.deleted = 0
              AND oc.order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        ) component
        <if test="processStatus != null and processStatus.size() > 0">
            WHERE component.process_status IN
            <foreach item="status" collection="processStatus" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>
        ORDER BY component.level DESC, component.id ASC
    </select>

    <select id="getOrderDetailComponentIds" resultType="java.lang.Long">
        select comp_id from order_item i
        where
        i.order_id = #{reqVO.orderId}
        <if test="reqVO.roomIds != null and reqVO.roomIds.size() > 0">
            AND i.room_id IN
            <foreach item="roomIdItem" collection="reqVO.roomIds" open="(" separator="," close=")">
                #{roomIdItem}
            </foreach>
        </if>
        <if test="reqVO.bodyIds != null and reqVO.bodyIds.size() > 0">
            AND i.body_id IN
            <foreach item="bodyIdItem" collection="reqVO.bodyIds" open="(" separator="," close=")">
                #{bodyIdItem}
            </foreach>
        </if>
        and i.comp_id is not null
    </select>

    <select id="getTopComponentListIn"
            resultType="com.cf.imes.module.executor.dal.dataobject.ordercomponent.OrderComponentDO">
        select distinct p.id, p.name
        from order_component p
        join order_component c
        on p.id = c.top_id
        where c.id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and p.deleted = #{deleted}
    </select>

    <select id="getOrderTopComponentList" resultType="com.cf.imes.module.executor.dal.dataobject.ordercomponent.OrderComponentDO">
        SELECT top.id, top.name
        FROM order_component top
        WHERE top.pid = 0
        <if test="name != null and name != ''">
            and top.name like CONCAT('%', #{name}, '%')
        </if>
        AND top.order_id in
        <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        AND EXISTS (
        SELECT 1
        FROM order_component oc
        JOIN order_item oi
        ON oi.comp_id = oc.id
        AND oi.order_id = oc.order_id
        AND oi.organ_id = oc.organ_id
        WHERE oc.top_id = top.id
        AND oc.order_id = top.order_id
        AND oc.organ_id = top.organ_id
        );
    </select>

    <select id="getOrderTopComponentListByAssemblyStatus"
            resultType="com.cf.imes.module.executor.dal.dataobject.ordercomponent.OrderComponentDO">
        SELECT * FROM (
        SELECT
        top.id,
        top.name,
        top.pid,
        top.cad_view_file_id,
        CASE
        <!-- 1. 不加工：extra_process_status = 0 -->
        WHEN top.pid = 0 AND top.extra_process_status = 0 THEN 0

        <!-- 2. 部分加工：extra_process_status = 3 -->
        WHEN top.pid = 0 AND top.extra_process_status = 3 THEN 3

        <!-- 3. 已加工：process_status = 2 -->
        WHEN top.pid = 0 AND top.process_status = 2 THEN 2

        <!-- 4. 未加工：process_status = 1 且 extra_process_status IS NULL -->
        WHEN top.pid = 0 AND top.process_status = 1 AND (top.extra_process_status IS NULL OR top.extra_process_status != 0) THEN 1

        ELSE -1
        END AS processStatus
        FROM order_component top
        WHERE top.pid = 0
        AND top.order_id IN
        <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        AND EXISTS (
        SELECT 1
        FROM order_component oc
        JOIN order_item oi
        ON oi.comp_id = oc.id
        AND oi.order_id = oc.order_id
        AND oi.organ_id = oc.organ_id
        WHERE oc.top_id = top.id
        AND oc.order_id = top.order_id
        AND oc.organ_id = top.organ_id
        )
        ) t
        <if test="assemblyStatusList != null and assemblyStatusList.size() > 0">
            WHERE t.processStatus IN
            <foreach item="status" collection="assemblyStatusList" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>
    </select>

    <select id="getTopIdsByComponentAndProcessStatus" resultType="java.lang.Long">
        SELECT DISTINCT oc.top_id
        FROM order_component oc
        WHERE oc.order_id IN
        <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        <if test="componentIds != null and componentIds.size() > 0">
            AND oc.top_id IN
            <foreach item="id" collection="componentIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="componentId != null or (name != null and name != '')">
            AND (
            <if test="componentId != null">
                oc.id = #{componentId}
            </if>
            <if test="componentId != null and name != null and name != ''">
                OR
            </if>
            <if test="name != null and name != ''">
                oc.name LIKE CONCAT('%', #{name}, '%')
            </if>
            )
        </if>
        <if test="processStatus != null and processStatus.size() > 0">
            AND oc.process_status IN
            <foreach item="status" collection="processStatus" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>
    </select>

    <select id="getComponentsByIdOrNameAndProcessStatus"
            resultType="com.cf.imes.module.executor.dal.dataobject.ordercomponent.OrderComponentDO">
        SELECT * FROM (
            SELECT oc.id,
                   oc.pid,
                   oc.name,
                   oc.width,
                   oc.height,
                   oc.depth,
                   CASE
                       WHEN oc.extra_process_status = 0 THEN 0
                       WHEN oc.pid = 0 AND oc.extra_process_status = 3 THEN 3
                       WHEN oc.process_status = 2 THEN 2
                       WHEN oc.process_status = 1
                            AND (oc.extra_process_status IS NULL OR oc.extra_process_status != 0) THEN 1
                       ELSE -1
                   END AS process_status
            FROM order_component oc
            WHERE oc.order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
            AND oc.deleted = 0
            <if test="componentIds != null and componentIds.size() > 0">
                AND oc.top_id IN
                <foreach item="id" collection="componentIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="componentId != null or (name != null and name != '')">
                AND (
                <if test="componentId != null">
                    oc.id = #{componentId}
                </if>
                <if test="componentId != null and name != null and name != ''">
                    OR
                </if>
                <if test="name != null and name != ''">
                    oc.name LIKE CONCAT('%', #{name}, '%')
                </if>
                )
            </if>
        ) component
        <if test="processStatus != null and processStatus.size() > 0">
            WHERE component.process_status IN
            <foreach item="status" collection="processStatus" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>
    </select>

    <select id="getComponentListOnTopLevel"
            resultType="com.cf.imes.module.executor.dal.dataobject.ordercomponent.OrderComponentDO">
        select * from order_component oc
        where oc.order_id = #{orderId}
        and oc.top_id = #{topId}
        and oc.deleted = #{deleted}
        and oc.level &gt; #{level}
    </select>

    <select id="selectOrderComponentPlateInPlan" resultType="boolean">
        select exists (
        select 1
        from order_item oi
        join order_plate op on oi.organ_id = op.organ_id and oi.order_id = op.order_id and oi.plate_id = op.id
        where
        oi.comp_id in
        <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
            #{componentId}
        </foreach>
        and oi.plate_id != 0
        and op.plan_id != 0
        )
    </select>

    <select id="selectByIdIgnoreDeleted"
            resultType="com.cf.imes.module.executor.dal.dataobject.ordercomponent.OrderComponentDO">
        select *
        from order_component
        where id = #{id}
    </select>

    <update id="updateOrderComponentsDeletedStatus">
        update order_component
        set deleted = #{delete}
        where
        order_id = #{orderId}
        and id in
        <foreach item="componentId" collection="componentIds" open="(" close=")" separator=",">
            #{componentId}
        </foreach>
        and deleted != #{delete}
    </update>

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