<?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.orderItem.OrderItemMapper">


    <select id="selectPlatesDetailByOrderId" resultType="com.cf.imes.module.executor.controller.admin.plate.vo.PlateRespVO">
        SELECT DISTINCT i.order_id, p.id, p.plate_no, g.material, p.name, p.area, p.seal_left, p.seal_down,
        p.seal_right, p.seal_up, p.texture, p.is_special_shaped, p.is_sculpt,p.is_side_sculpt,p.is_2v, p.is_side_2v, p.unregular_point_count, p.front_hole_count,
        p.back_hole_count, p.side_hole_count,p.width,p.height,p.thickness,p.remark,g.color,p.is_row_hole, p.custom_plate_no, p.cad_view_id, p.cad_data
        FROM order_item i
        RIGHT JOIN `order_plate` p ON i.plate_id = p.id
        LEFT JOIN `order_goods` g ON g.id = p.goods_id
        LEFT JOIN `order_group` gp ON gp.id = i.group_id
        WHERE i.order_id = #{orderId} AND i.organ_id = #{organId} AND p.deleted = #{deleted}
        <if test="roomId != null and roomId.size() > 0">
            AND i.room_id IN
            <foreach item="roomIdItem" collection="roomId" open="(" separator="," close=")">
                #{roomIdItem}
            </foreach>
        </if>
        <if test="bodyId != null and bodyId.size() > 0">
            AND i.body_id IN
            <foreach item="bodyIdItem" collection="bodyId" open="(" separator="," close=")">
                #{bodyIdItem}
            </foreach>
        </if>
        <if test="groupId != null and groupId.size() > 0">
            AND i.group_id IN
            <foreach item="groupIdItem" collection="groupId" open="(" separator="," close=")">
                #{groupIdItem}
            </foreach>
        </if>
        <if test="groupName != null">
            AND gp.name = #{groupName}
        </if>
    </select>

    <select id="selectPartsDetailByOrderId" resultType="com.cf.imes.module.executor.controller.admin.orderParts.vo.OrderPartsRespVO">
        SELECT i.order_id, p.model, p.spec, p.name, SUM(i.num) AS num, p.unit, p.brand, p.factory, p.remark,p.type,p.price,
        p.is_composite,p.create_time, p.color, p.category, p.length, p.width, p.thickness, p.material, p.goods_id
        FROM order_item i
        RIGHT JOIN `order_parts` p ON i.organ_id = p.organ_id AND i.order_id = p.order_id AND i.parts_id = p.id
        RIGHT JOIN `order_body` ob ON i.organ_id = ob.organ_id AND i.order_id = ob.order_id AND i.body_id = ob.id
        WHERE i.organ_id = #{organId} AND i.order_id = #{orderId} AND ob.deleted = #{deleted} AND (i.group_id is null or i.group_id = 0)
        <if test="roomId != null and roomId.size() > 0">
            AND i.room_id IN
            <foreach item="roomIdItem" collection="roomId" open="(" separator="," close=")">
                #{roomIdItem}
            </foreach>
        </if>
        <if test="bodyId != null and bodyId.size() > 0">
            AND i.body_id IN
            <foreach item="bodyIdItem" collection="bodyId" open="(" separator="," close=")">
                #{bodyIdItem}
            </foreach>
        </if>
        <if test="groupId != null and groupId.size() > 0">
            AND i.group_id IN
            <foreach item="groupIdItem" collection="groupId" open="(" separator="," close=")">
                #{groupIdItem}
            </foreach>
        </if>
        <if test="types != null and types.size() > 0">
            AND p.category IN
            <foreach item="type" collection="types" open="(" separator="," close=")">
                #{type}
            </foreach>
        </if>
        <if test="name != null">
            AND p.name = #{name}
        </if>
        GROUP BY
        i.order_id,
--         p.id,
        p.model,
        p.spec,
        p.name,
        p.unit,
        p.brand,
        p.factory,
        p.remark,
        p.type,
        p.price,
        p.is_composite,
        p.create_time,
        p.color,
        p.category,
        p.length,
        p.width,
        p.thickness,
        p.material,
        p.goods_id
    </select>

    <select id="selectPartsByOrderId"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPlatesDetailRespVO">
        SELECT DISTINCT i.order_id, i.room_id, i.body_id, i.parts_id, p.`name` as part_name, SUM(i.num) AS plate_num, p.unit
        <choose>
            <when test="deleted != null and deleted == @com.cf.imes.framework.common.enums.DeletedCodeEnum@DELETED.getStatus()">
                FROM `order_item_temp` i
            </when>
            <otherwise>
                FROM `order_item` i
            </otherwise>
        </choose>
                 RIGHT JOIN `order_parts` p ON i.organ_id = p.organ_id AND i.order_id = p.order_id AND i.parts_id = p.id
                 RIGHT JOIN `order_body` ob ON i.organ_id = ob.organ_id AND i.order_id = ob.order_id AND i.body_id = ob.id
        WHERE i.organ_id = #{organId}  AND i.order_id = #{orderId} AND  ob.deleted = #{deleted} AND ((i.group_id is null and i.comp_id is null) or i.group_id = 0) AND p.category in
        <foreach item="type" collection="types" open="(" separator="," close=")">
            #{type}
        </foreach>
        GROUP BY
            i.order_id,
            i.room_id,
            i.body_id,
            i.parts_id,
            p.name,
            p.unit
    </select>




    <delete id="deleteByPlateId" parameterType="java.lang.Long">


        delete from order_item  where plate_id in
        <foreach item="id" collection="deletePlateIds" open="(" separator="," close=")">
            #{id}
        </foreach>


    </delete>

    <update id="deletePackList" parameterType="java.lang.Long">

        UPDATE order_item

        SET package_id = 0, pack_num = 0, heap_num = 0, heap_split_status = ${@com.cf.imes.module.executor.enums.heapsplit.HeapSplitStatusEnum@NO.status}

        WHERE organ_id = #{organId}
        and order_id = #{orderId}
        and  package_id in
        <foreach item="packId" collection="packIds" open="(" separator="," close=")">
            #{packId}
        </foreach>
        and source_id = 0

    </update>


    <select id="selectGoodsByOrderId"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderGoodsRespVO">
        SELECT
            i.order_id,
            i.room_id,
            i.body_id,
            p.goods_id,
            g.goods_name,
            COUNT(DISTINCT p.id) AS totalPlates,
            SUM(p.area) AS totalArea
        FROM
            order_item i
                RIGHT JOIN
            order_plate p ON i.organ_id = p.organ_id AND i.order_id = p.order_id AND i.plate_id = p.id
                RIGHT JOIN
            order_goods g ON i.organ_id = g.organ_id AND i.order_id = g.order_id AND p.goods_id = g.id
        WHERE
            i.order_id = #{orderId} AND i.organ_id = #{organId}  AND p.deleted = #{deleted}
        GROUP BY
            i.order_id, i.room_id, i.body_id, p.goods_id, g.goods_name
    </select>


    <select id="selectGoodsDetailByOrderId" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderGoodsDetailRespVO">
        SELECT
        g.id,
        g.order_id,
        g.brand,
        g.goods_id,
        g.goods_name,
        g.material,
        g.texture,
        g.color,
        g.color_black,
        g.width,
        g.height,
        g.thickness,
        g.remark,
        g.spec,
        SUM(sub.totalPlates) AS number,
        SUM(sub.totalArea) AS area

        FROM (
        SELECT
        i.room_id,
        i.body_id,
        i.group_id,
        i.organ_id,
        i.order_id,
        p.goods_id,
        COUNT(p.id) AS totalPlates,
        SUM(p.area) AS totalArea
        FROM
        order_item i
        RIGHT JOIN
        order_plate p ON i.organ_id = p.organ_id AND i.order_id = p.order_id AND i.plate_id = p.id
        WHERE
        i.organ_id = #{organId} AND i.order_id = #{orderId} AND p.deleted = #{deleted}
        <if test="roomId != null and roomId.size() > 0">
            AND i.room_id IN
            <foreach item="roomIdItem" collection="roomId" open="(" separator="," close=")">
                #{roomIdItem}
            </foreach>
        </if>
        <if test="bodyId != null and bodyId.size() > 0">
            AND i.body_id IN
            <foreach item="bodyIdItem" collection="bodyId" open="(" separator="," close=")">
                #{bodyIdItem}
            </foreach>
        </if>
        <if test="groupId != null and groupId.size() > 0">
            AND i.group_id IN
            <foreach item="groupIdItem" collection="groupId" open="(" separator="," close=")">
                #{groupIdItem}
            </foreach>
        </if>
        <if test="goodsId != null and goodsId.size() > 0">
            AND p.goods_id IN
            <foreach item="goodIdItem" collection="goodsId" open="(" separator="," close=")">
                #{goodIdItem}
            </foreach>
        </if>

        GROUP BY
        i.room_id, i.body_id, i.group_id, p.goods_id, i.organ_id, i.order_id
        ) sub
        LEFT JOIN
        order_goods g ON sub.organ_id = g.organ_id AND sub.order_id = g.order_id AND sub.goods_id = g.id
        GROUP BY
        g.id, g.order_id, g.brand, g.goods_id, g.goods_name, g.material, g.texture, g.color, g.color_black, g.width, g.height, g.thickness, g.remark, g.spec;
    </select>


    <select id="countPartsNotId" resultType="java.lang.Integer">

        SELECT COUNT(i.id)
        FROM order_item i
        LEFT JOIN order_body ob
        ON i.organ_id = ob.organ_id
        AND i.order_id = ob.order_id
        AND i.body_id = ob.id
        WHERE i.organ_id = #{organId}
        AND i.order_id = #{orderId}

        <if test="bodyId != null and bodyId.size() > 0">
            AND i.body_id NOT IN
            <foreach item="bodyIdItem" collection="bodyId" open="(" separator="," close=")">
                #{bodyIdItem}
            </foreach>
        </if>

        AND ob.deleted = #{deleted}
        AND i.parts_id = #{goodsId};
    </select>



    <select id="selectExistOrderPlan" resultType="java.lang.Integer">
        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.organ_id = #{organId}
        and oi.order_id = #{orderId}
        <if test="bodyId != null">
            and oi.body_id = #{bodyId}
        </if>
        <if test="roomIds != null and roomIds.size() > 0">
            and oi.room_id in
            <foreach item="roomId" collection="roomIds" open="(" separator="," close=")">
                #{roomId}
            </foreach>
        </if>
        and op.plan_id != 0
        limit 1
    </select>




    <resultMap id="PlateDetailsMap" type="com.cf.imes.module.executor.controller.admin.manage.pack.vo.PlateDetailsRespVO">
        <result property="packageId" column="packageId"/>
        <result property="roomName" column="roomName"/>
        <result property="cabinetName" column="bodyName"/>
        <result property="plateId" column="plateId"/>
        <result property="plateNo" column="plateNo"/>
        <result property="orderId" column="orderId"/>
        <result property="plateName" column="plateName"/>
        <result property="material" column="plateMaterial"/>
        <result property="color" column="plateColor"/>
        <result property="width" column="plateWidth"/>
        <result property="height" column="plateHeight"/>
        <result property="thickness" column="plateThickness"/>
        <result property="area" column="plateArea"/>
        <result property="texture" column="texture"/>
        <result property="specialShaped" column="specialShaped"/>
        <result property="profiling" column="profiling"/>
        <result property="rowHole" column="rowHole"/>
        <result property="remark" column="remark"/>

        <collection property="processGroupNameList" javaType="java.util.List"  ofType="com.cf.imes.module.executor.controller.admin.manage.pack.vo.GroupNameList">
            <result property="name" column="name"/>
        </collection>

    </resultMap>




    <select id="selectPrintPlateList"
            parameterType="java.lang.Long"
            resultMap="PlateDetailsMap">

        select DISTINCT
            op.plate_no as plateId,
            op.height as plateHeight,
            op.width as plateWidth,
            op.thickness as plateThickness,
            op.name as plateName,
            og.material as plateMaterial,
            og.color as plateColor,
            ob.name as bodyName,
            ob.room_name as roomName

        from order_item oi
                 join order_body ob on oi.body_id = ob.id and oi.organ_id = ob.organ_id
                 join order_plate op on oi.plate_id = op.id and oi.organ_id = op.organ_id
                 join order_goods og on op.goods_id = og.id and op.organ_id = og.organ_id

        where oi.order_id = #{orderNo} and oi.package_id = #{packageNo} and oi.organ_id = #{organId} ;
    </select>



    <update id="deleteOrderPack" parameterType="java.lang.Long">

        UPDATE order_item

        SET package_id = 0

        WHERE organ_id = #{organId} and order_id = #{orderId} and  package_id = #{packId} ;

    </update>




    <delete id="deleteOtherParts"
            parameterType="java.lang.Long">

        DELETE FROM order_item

        WHERE organ_id = #{organId}
          and order_id = #{orderId}
          and parts_id = #{partsId}

    </delete>




    <delete id="deleteParts" parameterType="java.lang.Long">

        delete from order_item

        where organ_id = #{organId}
        and order_id = #{orderId}
        and package_id  = #{packageId}
        and parts_id in
        <foreach item="partsIds" collection="partsIds" open="(" separator="," close=")">
            #{partsIds}
        </foreach>

    </delete>



    <update id="insertPartsId" parameterType="java.lang.Long">

        update order_item oi

        set package_id = #{packId}

        where organ_id = #{organId}
        and order_id = #{orderId}
        and parts_id in
        <foreach item="id" collection="addPartsIdList" open="(" separator="," close=")">
            #{id}
        </foreach>

    </update>



    <update id="updateParts">

        update order_item

        set package_id = 0

        where organ_id = #{organId}
          and order_id = #{orderId}
          and package_id = #{packageId}
          and parts_id = #{partsId};

    </update>





    <resultMap id="PlateDetialsMap" type="com.cf.imes.module.executor.controller.admin.manage.process.vo.PlateDetails">

        <result property="plateId" column="plateId"/>
        <result property="plateNo" column="plateNo"/>
        <result property="width" column="width"/>
        <result property="length" column="length"/>
        <result property="thickness" column="thickness"/>


    </resultMap>

    <select id="selectOrderPackPartsIds"
            resultType="com.cf.imes.module.executor.controller.admin.manage.pack.vo.PlateAndPartIdList">


        select distinct oi.parts_id

        from  order_item oi
                  join order_parts op on oi.organ_id = op.organ_id and oi.order_id = op.order_id and oi.parts_id = op.id

        where oi.organ_id = #{organId} and oi.order_id = #{orderId} and oi.package_id = 0 and op.type != '封边条'

    </select>


    <select id="selectOrderPackPlateIds"
            resultType="com.cf.imes.module.executor.controller.admin.manage.pack.vo.PlateAndPartIdList">


        select distinct plate_id

        from order_item

        where organ_id = #{organId}
          and order_id = #{orderId}
          and package_id = 0
          and plate_id !=0

    </select>

    <select id="selectOrdersViewPlatesPage" resultType="com.cf.imes.module.executor.dal.dataobject.orderItem.OrdersViewPlatesPageDO">
        SELECT
        p.plate_no,
        p.name,
        p.width,
        p.height,
        p.thickness,
        g.material,
        g.color,
        g.brand,
        p.custom_plate_no,
        p.cad_plate_no AS customNumber,
        p.id,
        i.body_id,
        i.cad_view_id
        <choose>
            <when test="cadView">
                , p.seal_left, p.seal_down, p.seal_right, p.seal_up
            </when>
            <otherwise>
                , p.seal_left, p.seal_down, p.seal_right, p.seal_up,
                p.area, p.texture, p.is_special_shaped, p.is_sculpt,
                p.is_row_hole, p.remark
            </otherwise>
        </choose>
        <if test="componentIds != null and componentIds.size() > 0">
            , i.comp_id
        </if>
        FROM (
        SELECT
        plate_id,
        cad_view_id,
        MIN(body_id) AS body_id
        <if test="componentIds != null and componentIds.size() > 0">
            , MIN(comp_id) AS comp_id
        </if>
        <choose>
            <when test="deleted">
                FROM order_item_temp
            </when>
            <otherwise>
                FROM order_item
            </otherwise>
        </choose>
        WHERE plate_id != 0
        <if test="bodyIds != null and bodyIds.size() > 0">
            AND body_id IN
            <foreach item="bodyId" collection="bodyIds" open="(" separator="," close=")">
                #{bodyId}
            </foreach>
        </if>
        <if test="groupIds != null and groupIds.size() > 0">
            AND group_id IN
            <foreach item="groupId" collection="groupIds" open="(" separator="," close=")">
                #{groupId}
            </foreach>
        </if>
        <if test="orderIds != null and orderIds.size() > 0">
            AND order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </if>
        <if test="componentIds != null and componentIds.size() > 0">
            AND comp_id IN
            <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                #{componentId}
            </foreach>
        </if>
        GROUP BY plate_id, cad_view_id
        ) i
        INNER JOIN order_plate p ON p.id = i.plate_id
        INNER JOIN order_goods g ON g.id = p.goods_id
        WHERE p.deleted = #{deleted}
    </select>

    <select id="selectOrdersViewPlateIdsPage" resultType="java.lang.Long">
        SELECT
        i.plate_id
        <choose>
            <when test="deleted">
                FROM order_item_temp i
            </when>
            <otherwise>
                FROM order_item i
            </otherwise>
        </choose>
        WHERE i.plate_id != 0 and source_id = 0
        <if test="bodyIds != null and bodyIds.size() > 0">
            AND body_id IN
            <foreach item="bodyId" collection="bodyIds" open="(" separator="," close=")">
                #{bodyId}
            </foreach>
        </if>
        <if test="groupIds != null and groupIds.size() > 0">
            AND group_id IN
            <foreach item="groupId" collection="groupIds" open="(" separator="," close=")">
                #{groupId}
            </foreach>
        </if>
        <if test="orderIds != null and orderIds.size() > 0">
            AND order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </if>
        <if test="componentIds != null and componentIds.size() > 0">
            AND comp_id IN
            <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                #{componentId}
            </foreach>
        </if>
        GROUP BY i.plate_id
        ORDER BY MAX(i.id) DESC
    </select>

    <select id="selectOrdersViewPartsPage" resultType="com.cf.imes.module.executor.dal.dataobject.orderItem.OrdersViewPartsPageDO">
        SELECT
        op.id,
        op.type,
        op.name,
        op.material,
        op.color,
        op.spec,
        op.unit,
        op.factory,
        op.width,
        op.length,
        op.thickness,
        op.brand,
        i.body_id,
        i.cad_view_id,
        op.remark
        <if test="componentIds != null and componentIds.size() > 0">
            , i.comp_id
        </if>
        FROM (
        SELECT
        parts_id,
        cad_view_id,
        MIN(body_id) AS body_id
        <if test="componentIds != null and componentIds.size() > 0">
            , MIN(comp_id) AS comp_id
        </if>
        <choose>
            <when test="deleted">
                FROM order_item_temp
            </when>
            <otherwise>
                FROM order_item
            </otherwise>
        </choose>
        WHERE plate_id = 0
        <if test="orderIds != null and orderIds.size() > 0">
            AND order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </if>
        <if test="bodyIds != null and bodyIds.size() > 0">
            AND body_id IN
            <foreach item="bodyId" collection="bodyIds" open="(" separator="," close=")">
                #{bodyId}
            </foreach>
        </if>
        <if test="groupIds != null and groupIds.size() > 0">
            AND group_id IN
            <foreach item="groupId" collection="groupIds" open="(" separator="," close=")">
                #{groupId}
            </foreach>
        </if>
        <if test="componentIds != null and componentIds.size() > 0">
            AND comp_id IN
            <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                #{componentId}
            </foreach>
        </if>
        GROUP BY parts_id, cad_view_id
        ) i
        INNER JOIN order_parts op
        ON op.id = i.parts_id
        WHERE op.deleted = false
        <if test="cadView">
            AND op.name != '封边条' AND op.name != '三合一'
        </if>
        <if test="name != null">
            AND op.name = #{name}
        </if>
        ORDER BY op.id, op.type
    </select>

    <select id="selectOrdersViewPartsIdsPage" resultType="com.cf.imes.module.executor.dal.dataobject.orderItem.OrdersViewPartsPageDO">
        SELECT
        t.parts_id as id,
        t.cad_view_id,
        op.type,
        op.name,
        op.material,
        op.color,
        op.model,
        op.spec,
        op.unit,
        op.brand,
        op.factory,
        op.remark,
        op.width,
        op.length,
        op.thickness
        FROM (
        SELECT
        i.parts_id,
        i.cad_view_id
        <choose>
            <when test="deleted">
                FROM order_item_temp i
            </when>
            <otherwise>
                FROM order_item i
            </otherwise>
        </choose>
        WHERE i.plate_id = 0
        AND i.source_id = 0

        <if test="orderIds != null and orderIds.size() > 0">
            AND i.order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </if>

        <if test="bodyIds != null and bodyIds.size() > 0">
            AND i.body_id IN
            <foreach item="bodyId" collection="bodyIds" open="(" separator="," close=")">
                #{bodyId}
            </foreach>
        </if>

        <choose>
            <when test="groupIds != null and groupIds.size() > 0">
                AND i.group_id IN
                <foreach item="groupId" collection="groupIds" open="(" separator="," close=")">
                    #{groupId}
                </foreach>
            </when>
            <otherwise>
                <if test="componentIds == null or componentIds.size() == 0">
                    AND ((i.group_id is null and i.comp_id is null) or i.group_id = 0)
                </if>
            </otherwise>
        </choose>

        <if test="componentIds != null and componentIds.size() > 0">
            AND i.comp_id IN
            <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                #{componentId}
            </foreach>
        </if>

        GROUP BY
        i.parts_id,
        i.cad_view_id
        ) t
        JOIN order_parts op
        ON op.id = t.parts_id
        WHERE op.deleted = false
        <if test="orderIds != null and orderIds.size() > 0">
            AND op.order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </if>

        <if test="name != null and name != ''">
            AND op.name = #{name}
        </if>

        ORDER BY op.type, op.id
    </select>

    <select id="selectOrdersViewPartsList"
            resultType="com.cf.imes.module.executor.dal.dataobject.orderItem.OrdersViewPartsPageDO">
        SELECT
        i.order_id AS orderId,
        i.parts_id AS id,
        i.body_id AS bodyId,
        ob.name AS bodyName,
        ob.room_name AS roomName,
        <choose>
            <when test="componentIds != null and componentIds.size() > 0">
                oc.cad_view_file_id AS cadViewFileId,
            </when>
            <otherwise>
                ob.cad_view_file_id AS cadViewFileId,
            </otherwise>
        </choose>
        i.cad_view_id AS cadViewId,

        i.group_id AS groupId,
        i.comp_id AS compId,
        i.num AS num,

        og.name AS processGroupName,
        oc.name AS componentName,

        op.type,
        op.name,
        op.material,
        op.color,
        op.model,
        op.spec,
        op.unit,
        op.brand,
        op.factory,
        op.remark,
        op.width,
        op.length,
        op.thickness
        <choose>
            <when test="deleted">
                FROM order_item_temp i
            </when>
            <otherwise>
                FROM order_item i
            </otherwise>
        </choose>
        JOIN order_parts op
        ON op.organ_id = i.organ_id
        AND op.order_id = i.order_id
        AND op.id = i.parts_id
        AND op.deleted = false
        LEFT JOIN order_body ob
        ON ob.organ_id = i.organ_id
        AND ob.order_id = i.order_id
        AND ob.id = i.body_id
        LEFT JOIN order_group og
        ON og.organ_id = i.organ_id
        AND og.order_id = i.order_id
        AND og.id = i.group_id
        LEFT JOIN order_component oc
        ON oc.organ_id = i.organ_id
        AND oc.order_id = i.order_id
        AND oc.id = i.comp_id
        WHERE i.plate_id = 0
        AND i.source_id = 0
        AND i.parts_id != 0
        <if test="orderIds != null and orderIds.size() > 0">
            AND i.order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </if>

        <if test="bodyIds != null and bodyIds.size() > 0">
            AND i.body_id IN
            <foreach item="bodyId" collection="bodyIds" open="(" separator="," close=")">
                #{bodyId}
            </foreach>
        </if>

        <choose>
            <when test="groupIds != null and groupIds.size() > 0">
                AND i.group_id IN
                <foreach item="groupId" collection="groupIds" open="(" separator="," close=")">
                    #{groupId}
                </foreach>
            </when>
            <otherwise>
                <if test="componentIds == null or componentIds.size() == 0">
                    AND ((i.group_id is null and i.comp_id is null) or i.group_id = 0)
                </if>
            </otherwise>
        </choose>

        <if test="componentIds != null and componentIds.size() > 0">
            AND i.comp_id IN
            <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                #{componentId}
            </foreach>
        </if>

        <if test="name != null and name != ''">
            AND op.name = #{name}
        </if>

        ORDER BY
        op.type,
        op.id
    </select>

    <select id="selectComponentCorrelationPlateIds" resultType="java.lang.String">
        SELECT DISTINCT op.plate_no
        FROM order_item oi
        JOIN order_plate op
        ON op.id = oi.plate_id
        AND op.order_id = oi.order_id
        AND op.deleted = 0
        WHERE oi.plate_id != 0
        <if test="orderIds != null and orderIds.size() > 0">
            AND oi.order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </if>
        <if test="(componentIds != null and componentIds.size() > 0) or (groupIds != null and groupIds.size() > 0)">
            AND (
            <if test="componentIds != null and componentIds.size() > 0">
                oi.comp_id IN
                <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                    #{componentId}
                </foreach>
            </if>
            <if test="componentIds != null and componentIds.size() > 0 and groupIds != null and groupIds.size() > 0">
                OR
            </if>
            <if test="groupIds != null and groupIds.size() > 0">
                oi.group_id IN
                <foreach item="groupId" collection="groupIds" open="(" separator="," close=")">
                    #{groupId}
                </foreach>
            </if>
            )
        </if>
    </select>

    <select id="selectComponentCorrelationPartsIds" resultType="java.lang.String">
        SELECT oi.parts_id
        FROM order_item oi
        WHERE oi.parts_id != 0
        <if test="orderIds != null and orderIds.size() > 0">
            AND oi.order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </if>
        <if test="(componentIds != null and componentIds.size() > 0) or (groupIds != null and groupIds.size() > 0)">
            AND (
            <if test="componentIds != null and componentIds.size() > 0">
                oi.comp_id IN
                <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                    #{componentId}
                </foreach>
            </if>

            <if test="componentIds != null and componentIds.size() > 0 and groupIds != null and groupIds.size() > 0">
                OR
            </if>
            <if test="groupIds != null and groupIds.size() > 0">
                oi.group_id IN
                <foreach item="groupId" collection="groupIds" open="(" separator="," close=")">
                    #{groupId}
                </foreach>
            </if>
            )
        </if>
        AND EXISTS (
        SELECT 1
        FROM order_parts op
        WHERE op.id = oi.parts_id
        AND op.order_id = oi.order_id
        AND op.deleted = false
        )
        GROUP BY oi.parts_id
    </select>

    <select id="selectOrderViewPlates" resultType="com.cf.imes.module.executor.dal.dataobject.orderItem.OrdersViewPlatesPageDO">
        SELECT
            op.plate_no,
            op.cad_plate_no,
            op.name,
            op.height,
            op.width,
            op.thickness,
            ob.name AS bodyName,
            ob.room_name AS roomName,
            t.body_id AS bodyId,
            t.cad_view_id
            <if test="componentIds != null and componentIds.size() > 0">
                , t.comp_id
            </if>
        FROM order_plate op
        JOIN (
            SELECT plate_id,
            body_id,
            MIN(cad_view_id) AS cad_view_id
            <if test="componentIds != null and componentIds.size() > 0">
                , MIN(comp_id) AS comp_id
            </if>
            FROM order_item
            WHERE order_id = #{orderId} and plate_id != 0 and source_id = 0
            <if test="bodyIds != null and bodyIds.size() > 0">
                AND body_id IN
                <foreach item="bodyId" collection="bodyIds" open="(" separator="," close=")">
                    #{bodyId}
                </foreach>
            </if>
            <if test="componentIds != null and componentIds.size() > 0">
                AND comp_id IN
                <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                    #{componentId}
                </foreach>
            </if>
            GROUP BY body_id, plate_id
        ) t ON t.plate_id = op.id
        JOIN order_body ob ON ob.id = t.body_id
        <if test="bodyIds != null and bodyIds.size() > 0">
            AND ob.id IN
            <foreach item="bodyId" collection="bodyIds" open="(" separator="," close=")">
                #{bodyId}
            </foreach>
        </if>
    </select>

    <select id="selectOrderViewParts" resultType="com.cf.imes.module.executor.dal.dataobject.orderItem.OrdersViewPartsPageDO">
        SELECT
        op.id,
        op.name,
        op.model,
        op.spec,
        op.brand,
        op.factory,
        op.color,
        op.width,
        op.length,
        op.thickness,
        ob.name AS bodyName,
        ob.room_name AS roomName,
        ob.id AS bodyId,
        t.num,
        t.cad_view_id
        <if test="componentIds != null and componentIds.size() > 0">
            , t.comp_id
        </if>
        FROM order_parts op
        JOIN (
            SELECT parts_id,
            body_id,
            cad_view_id,
            <choose>
                <when test="componentIds != null and componentIds.size() > 0">
                    MIN(num) AS num,
                    MIN(comp_id) AS comp_id
                    FROM order_item
                    WHERE order_id = #{orderId} and parts_id != 0 and source_id = 0
                    AND comp_id IN
                    <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                        #{componentId}
                    </foreach>
                </when>
                <otherwise>
                    SUM(num) AS num
                    FROM order_item
                    WHERE order_id = #{orderId} and parts_id != 0 and source_id = 0
                    and ((group_id is null and comp_id is null) or group_id = 0)
                </otherwise>
            </choose>
            <if test="bodyIds != null and bodyIds.size() > 0">
                AND body_id IN
                <foreach item="bodyId" collection="bodyIds" open="(" separator="," close=")">
                    #{bodyId}
                </foreach>
            </if>
            GROUP BY body_id, parts_id, cad_view_id
        ) t ON t.parts_id = op.id
        JOIN order_body ob ON ob.order_id = op.order_id and ob.id = t.body_id
        <if test="bodyIds != null and bodyIds.size() > 0">
            AND ob.id IN
            <foreach item="bodyId" collection="bodyIds" open="(" separator="," close=")">
                #{bodyId}
            </foreach>
        </if>
    </select>

    <select id="selectOrdersPrintPlatesList" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPrintDetailRespVO">
        SELECT
        p.order_id,
        p.plate_no,
        p.name as plateName,
        p.width,
        p.height,
        p.thickness,
        p.split_width,
        p.split_height,
        p.split_thickness,
        p.custom_plate_no,
        p.id as plateId,
        p.remark,
        g.material,
        g.brand,
        g.color,
        ob.room_name AS roomName,
        ob.name AS bodyName
        FROM (
        SELECT
        plate_id,
        MIN(body_id) AS body_id
        FROM order_item
        WHERE plate_id != 0 and source_id = 0
        <if test="orderIds != null and orderIds.size() > 0">
            AND order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </if>
        <if test="componentIds != null and componentIds.size() > 0">
            AND comp_id IN
            <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                #{componentId}
            </foreach>
        </if>
        GROUP BY plate_id
        ) i
        INNER JOIN order_plate p ON p.id = i.plate_id
        INNER JOIN order_goods g ON g.id = p.goods_id
        INNER JOIN order_body ob ON ob.id = i.body_id
        WHERE p.deleted = false
    </select>

    <select id="selectOrdersPrintPartsList" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPrintDetailRespVO">
        SELECT
        op.order_id,
        op.id as partsId,
        op.type,
        op.name as partsName,
        op.material,
        op.color,
        op.model,
        op.spec,
        op.unit,
        op.brand,
        op.factory,
        op.remark,
        ob.room_name AS roomName,
        ob.name AS bodyName,
        i.num
        FROM (
        SELECT
        parts_id,
        MIN(body_id) AS body_id,
        SUM(num) AS num
        FROM order_item
        WHERE plate_id = 0 and source_id = 0
        <if test="orderIds != null and orderIds.size() > 0">
            AND order_id IN
            <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </if>
        <if test="componentIds != null and componentIds.size() > 0">
            AND comp_id IN
            <foreach item="componentId" collection="componentIds" open="(" separator="," close=")">
                #{componentId}
            </foreach>
        </if>
        GROUP BY parts_id
        ) i
        INNER JOIN order_parts op
        ON op.id = i.parts_id
        INNER JOIN order_body ob ON ob.id = i.body_id
        WHERE op.deleted = false
        ORDER BY op.id, op.type
    </select>

    <select id="selectOrderItemForTemp"
            resultType="com.cf.imes.module.executor.dal.dataobject.orderItem.OrderItemDO">

        SELECT *
        FROM order_item
        WHERE id > #{lastId}
        AND order_id = #{orderId}
        AND organ_id = #{organId}
        AND source_id = 0

        <if test="bodyIds != null and bodyIds.size() > 0">
            AND body_id IN
            <foreach item="bodyId"
                     collection="bodyIds"
                     open="("
                     separator=","
                     close=")">
                #{bodyId}
            </foreach>
        </if>

        <if test="compIds != null and compIds.size() > 0">
            AND comp_id IN
            <foreach item="compId"
                     collection="compIds"
                     open="("
                     separator=","
                     close=")">
                #{compId}
            </foreach>
        </if>

        ORDER BY id
        LIMIT #{pageSize}

    </select>

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

    <select id="selectAssemblyComponentPrintPlates"
            resultType="com.cf.imes.module.executor.dal.dataobject.orderItem.AssemblyComponentChildPrintItemDO">

        SELECT DISTINCT
        oc.id AS compId,
        p.id AS id,
        p.name AS name,
        p.height AS length,
        p.width AS width,
        p.thickness AS thickness,
        1 AS quantity,
        ob.room_name AS roomName,
        ob.name AS bodyName
        FROM order_component oc
        INNER JOIN order_item i
        ON i.comp_id = oc.id
        AND i.order_id = oc.order_id
        AND i.organ_id = oc.organ_id
        AND i.body_id = oc.body_id
        INNER JOIN order_plate p
        ON p.id = i.plate_id
        AND p.order_id = i.order_id
        AND p.organ_id = i.organ_id
        AND p.deleted = FALSE
        LEFT JOIN order_body ob
        ON ob.id = oc.body_id
        AND ob.order_id = oc.order_id
        AND ob.organ_id = oc.organ_id
        WHERE oc.deleted = FALSE
        AND i.plate_id != 0
        AND i.source_id = 0
        AND oc.id IN
        <foreach collection="componentIds" item="componentId" open="(" separator="," close=")">
            #{componentId}
        </foreach>
        AND oc.order_id IN
        <foreach collection="orderIds" item="orderId" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        ORDER BY oc.id, p.id
    </select>

    <select id="selectAssemblyComponentPrintParts"
            resultType="com.cf.imes.module.executor.dal.dataobject.orderItem.AssemblyComponentChildPrintItemDO">

        SELECT
        oc.id AS compId,
        oc.body_id AS bodyId,
        op.id AS id,
        op.name AS name,
        op.length AS length,
        op.width AS width,
        op.thickness AS thickness,
        i.num AS quantity,
        ob.room_name AS roomName,
        ob.name AS bodyName
        FROM order_component oc
        INNER JOIN order_item i
        ON i.comp_id = oc.id
        AND i.order_id = oc.order_id
        AND i.organ_id = oc.organ_id
        AND i.body_id = oc.body_id
        INNER JOIN order_parts op
        ON op.id = i.parts_id
        AND op.order_id = i.order_id
        AND op.organ_id = i.organ_id
        AND op.deleted = FALSE
        LEFT JOIN order_body ob
        ON ob.id = oc.body_id
        AND ob.order_id = oc.order_id
        AND ob.organ_id = oc.organ_id
        WHERE oc.deleted = FALSE
        AND i.plate_id = 0
        AND i.parts_id != 0
        AND i.source_id = 0
        AND oc.id IN
        <foreach collection="componentIds" item="componentId" open="(" separator="," close=")">
            #{componentId}
        </foreach>
        AND oc.order_id IN
        <foreach collection="orderIds" item="orderId" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        ORDER BY oc.id, oc.body_id, op.id
    </select>
</mapper>