<?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
        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
        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) 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

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


    </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="selectOrderPlanList" resultType="java.lang.Long">



        select distinct
            op.plan_id

        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 oi.room_id in
                <foreach item="roomIds" collection="roomIds" open="(" separator="," close=")">
                    #{roomIds}
                </foreach>
            </if>
            and op.plan_id != 0

        </where>


    </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="insertPackId" parameterType="java.lang.Long">

        update order_item

        set package_id = #{packId}

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

    </update>



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

        update order_item oi

        set package_id = 0

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


    </update>



    <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="insertParts" 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="addPartsList" open="(" separator="," close=")">
            #{id}
        </foreach>

    </update>


    <update id="deletePartsList">

        update order_item oi

        set package_id = 0

        where
        organ_id = #{organId}
        and order_id = #{orderId}
        and package_id = #{packId}
        and parts_id in
        <foreach item="id" collection="deletePartsList" 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>
</mapper>