<?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.planitem.PlanItemMapper">


    <resultMap id="OrderPageMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.OrderRespVOCopy">

        <result property="orderId" column="orderId"/>
        <result property="status" column="status"/>
        <result property="defineId" column="defineId"/>
        <result property="customer" column="customer"/>
        <result property="address" column="address"/>
        <result property="deliveryDate" column="deliveryDate"/>
        <result property="orderDate" column="orderDate"/>
        <result property="num" column="num"/>
        <result property="area" column="area"/>
        <result property="orderType" column="orderType"/>

    </resultMap>

    <select id="selectOrderList" resultMap="OrderPageMap">
        SELECT
        o.id AS orderId,

        <if test="type != null and type == @com.cf.imes.module.executor.enums.OrderPlanQueryTypeEnum@ORDER_GROUP.getType()">
            o.plate_num - o.planned_plate_num AS num,
            o.area - o.planned_plate_area AS area,
        </if>

        <if test="type != null and type == @com.cf.imes.module.executor.enums.OrderPlanQueryTypeEnum@GOODS_GROUP.getType()">
            t.num,
            t.area,
        </if>

        o.parent_id AS parentId,
        o.order_type AS orderType,
        o.order_date AS orderDate,
        o.delivery_date AS deliveryDate,
        o.customer AS customer,
        o.address AS address,
        o.custom_order_no AS defineId,
        o.status AS status,
        o.phone_number,
        o.dealer,
        o.salesman,
        o.splitter,
        o.remark

        FROM orders o

        <if test="type != null and type == @com.cf.imes.module.executor.enums.OrderPlanQueryTypeEnum@GOODS_GROUP.getType()">
            LEFT JOIN (
            SELECT
            og.order_id,
            SUM(og.plate_num - og.planned_plate_num) AS num,
            SUM(og.area - og.planned_plate_area) AS area
            FROM order_goods og
            WHERE og.plate_num - og.planned_plate_num > 0

            <if test="orderGoodsIds != null and orderGoodsIds.size() > 0">
                AND og.id IN
                <foreach item="orderGoodsId"
                         collection="orderGoodsIds"
                         open="("
                         separator=","
                         close=")">
                    #{orderGoodsId}
                </foreach>
            </if>

            GROUP BY og.order_id
            ) t ON t.order_id = o.id
        </if>

        WHERE 1 = 1
        <if test="rootIds != null and rootIds.size() > 0">
            AND o.id IN
            <foreach item="rootId"
                     collection="rootIds"
                     open="("
                     separator=","
                     close=")">
                #{rootId}
            </foreach>
        </if>

        ORDER BY o.id DESC
    </select>

    <select id="selectRootOrderIdList" resultType="java.lang.Long">

        SELECT o.root_id
        FROM orders o
          ${ew.customSqlSegment}
          AND o.deleted = false
          AND IFNULL(o.plate_num,0) - IFNULL(o.planned_plate_num,0) > 0
        GROUP BY o.root_id
        ORDER BY o.root_id DESC

    </select>

    <select id="selectOrderChildList" resultMap="OrderPageMap">

        SELECT
        o.id AS orderId,

        <if test="type != null and type == @com.cf.imes.module.executor.enums.OrderPlanQueryTypeEnum@ORDER_GROUP.getType()">
            o.plate_num - o.planned_plate_num AS num,
            o.area - o.planned_plate_area AS area,
        </if>

        <if test="type != null and type == @com.cf.imes.module.executor.enums.OrderPlanQueryTypeEnum@GOODS_GROUP.getType()">
            t.num,
            t.area,
        </if>

        o.parent_id AS parentId,
        o.order_type AS orderType,
        o.order_date AS orderDate,
        o.delivery_date AS deliveryDate,
        o.customer AS customer,
        o.address AS address,
        o.custom_order_no AS defineId,
        o.status AS status

        FROM orders o

        <if test="type != null and type == @com.cf.imes.module.executor.enums.OrderPlanQueryTypeEnum@GOODS_GROUP.getType()">

            JOIN (
            SELECT
            og.order_id,
            SUM(og.plate_num - og.planned_plate_num) AS num,
            SUM(og.area - og.planned_plate_area) AS area
            FROM order_goods og
            WHERE og.plate_num - og.planned_plate_num > 0

            <if test="orderGoodsIds != null and orderGoodsIds.size() > 0">
                AND og.id IN
                <foreach item="orderGoodsId"
                         collection="orderGoodsIds"
                         open="("
                         separator=","
                         close=")">
                    #{orderGoodsId}
                </foreach>
            </if>

            GROUP BY og.order_id

            ) t ON t.order_id = o.id

        </if>

            ${ew.customSqlSegment}
            AND IFNULL(o.plate_num,0) - IFNULL(o.planned_plate_num,0) > 0
            AND  o.parent_id IN
            <foreach item="orderId"
                     collection="orderIds"
                     open="("
                     separator=","
                     close=")">
                #{orderId}
            </foreach>

        ORDER BY o.id DESC

    </select>



    <resultMap id="GoodsListMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.PlateInfoVO">

        <result property="planId" column="planId"/>
        <result property="material" column="material"/>
        <result property="width" column="width"/>
        <result property="height" column="height"/>
        <result property="thickness" column="thickness"/>
        <result property="color" column="color"/>
        <result property="brand" column="brand"/>
        <result property="count" column="count"/>
        <result property="area" column="area"/>
    </resultMap>


    <select id="selectOrderPlanGoodsList" resultMap="GoodsListMap" parameterType="java.lang.Long">

        select distinct
            op.plan_id as planId,
            og.material as material,
            og.thickness as thickness ,
            og.color as color

        from order_plate op
                  join order_goods og on op.organ_id = og.organ_id and op.goods_id = og.id

        where op.organ_id = #{organId}
        and op.order_id in
        <foreach collection="orderIds" item="orderId" open="(" close=")" separator=",">
            #{orderId}
        </foreach>
        and op.plan_id in
        <foreach collection="planIds" item="planId" open="(" close=")" separator=",">
            #{planId}
        </foreach>
        GROUP BY
            planId,
            material,
            thickness,
            color
    </select>


    <select id="selectPlateListByPlanId"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.PlateResList">


        select distinct op.id as plateId,
                        op.order_id,
                        op.plate_no ,
                        op.goods_id as id,
                        op.custom_plate_no,
                        op.name as plateName,
                        op.is_special_shaped,
                        op.is_sculpt,
                        op.area,
                        op.seal_left,
                        op.seal_right,
                        op.seal_up,
                        op.seal_down,
                        og.material,
                        og.color,
                        op.width,
                        op.height,
                        op.thickness,
                        op.texture,
                        op.is_row_hole,
                        op.remark,
                        op.plan_id,
                        oi.room_id,
                        oi.body_id,
                        ob.name as bodyName,
                        ob.room_name as roomName

        from order_plate op
                 join order_goods og on op.organ_id = og.organ_id and op.order_id = og.order_id and op.goods_id = og.id
                 join order_item oi on op.organ_id = oi.organ_id and op.order_id = oi.order_id and op.id = oi.plate_id
                 join order_body ob on op.organ_id = ob.organ_id and oi.order_id = ob.order_id and oi.body_id = ob.id
        where op.organ_id = #{organId} and op.plan_id = #{planId}

    </select>


    <resultMap id="OrderGoodsMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.OrderGoodsResp">

        <result property="goodsName" column="goodsName"/>
        <result property="material" column="material"/>
        <result property="color" column="color"/>
        <result property="texture" column="texture"/>
        <result property="thickness" column="thickness"/>
        <result property="brand" column="brand"/>
        <result property="num" column="num"/>
        <result property="area" column="area"/>

        <collection property="orderIds" ofType="com.cf.imes.module.executor.controller.admin.plan.bo.OrderIds" resultMap="orderIdsMap"/>

    </resultMap>

    <resultMap id="orderIdsMap" type="com.cf.imes.module.executor.controller.admin.plan.bo.OrderIds">

        <result property="orderId" column="orderId"/>
        <result property="goodsId" column="goodsId"/>

    </resultMap>


    <select id="selectNoPlanGoodsByRoomAndBodyIds" resultMap="OrderGoodsMap" parameterType="java.lang.Long">
        SELECT
            t.color,
            t.goodsName,
            t.material,
            t.thickness,
            t.brand,
            t.texture,
            COUNT(*) AS num,
            SUM(t.area) AS area

        FROM (
                 SELECT DISTINCT
                     op.id,
                     op.area,
                     og.color,
                     og.goods_name AS goodsName,
                     og.material,
                     og.thickness,
                     og.brand,
                     og.texture

                 FROM order_plate op

                          JOIN order_goods og
                               ON og.id = op.goods_id
                                   AND og.order_id = op.order_id
                                   AND og.organ_id = op.organ_id

                          JOIN order_item oi
                               ON oi.plate_id = op.id
                                   AND oi.order_id = op.order_id
                                   AND oi.organ_id = op.organ_id
                                   AND oi.source_id = 0

                     ${ew.customSqlSegment}

        AND op.plan_id = 0
             ) t

        GROUP BY
            t.color,
            t.goodsName,
            t.material,
            t.thickness,
            t.brand,
            t.texture
        ORDER BY t.thickness DESC
    </select>




    <select id="selectNoPlanGoodsList" resultMap="OrderGoodsMap" parameterType="java.lang.Long">


            select
            og.color as color,
            og.goods_name as goodsName,
            og.material as material,
            og.thickness as thickness,
            og.brand as brand,
            og.texture as texture,
            sum(og.plate_num - og.planned_plate_num) as num,
            sum(og.area - og.planned_plate_area) as area

        from orders o
                join order_goods og on o.id = og.order_id  and og.organ_id = #{organId}

            ${ew.customSqlSegment}
            and og.plate_num - og.planned_plate_num > 0
            and og.deleted = false
            group by og.color, og.goods_name, og.material, og.thickness, og.brand, og.texture
            order by thickness desc
    </select>






    <select id="selectNoOrderGoodsIdList" resultMap="OrderGoodsMap">

        select distinct
            og.color as color,
            og.goods_name as goodsName,
            og.material as material,
            og.thickness as thickness,
            og.brand as brand,
            og.texture as texture,

            og.goods_id as goodsId,
            og.order_id as orderId

        from orders o
                  join order_goods og on o.organ_id = og.organ_id and o.id = og.order_id
             <if test="type == @com.cf.imes.module.executor.enums.OrderPlanQueryTypeEnum@GOODS_ROOMBODY_QUERY.getType()">
                  join order_plate op on o.organ_id = op.organ_id and o.id = op.order_id and og.id = op.goods_id
                  join order_item oi on oi.organ_id = oi.organ_id and o.id = oi.order_id and op.id = oi.plate_id
             </if>
            ${ew.customSqlSegment}
            and og.plate_num - og.planned_plate_num > 0
            and og.deleted = false

    </select>



    <resultMap id="PlateDetailMap" type="com.cf.imes.module.executor.dal.dataobject.plate.PlateDetailDO">

        <result property="plateId" column="plateId"/>
        <result property="orderId" column="orderId"/>
        <result property="id" column="goodsId"/>
        <result property="goodsId" column="plateGoodsId"/>
        <result property="plateNo" column="plateNo"/>
        <result property="customPlateNo" column="customPlateNo"/>
        <result property="name" column="name"/>
        <result property="height" column="height"/>
        <result property="width" column="width"/>
        <result property="color" column="color"/>
        <result property="material" column="material"/>
        <result property="brand" column="brand"/>
        <result property="thickness" column="thickness"/>
        <result property="splitWidth" column="splitWidth"/>
        <result property="splitHeight" column="splitHeight"/>
        <result property="splitThickness" column="splitThickness"/>
        <result property="isSpecialShaped" column="isSpecialShaped"/>
        <result property="sealLeft" column="sealLeft"/>
        <result property="sealRight" column="sealRight"/>
        <result property="sealUp" column="sealUp"/>
        <result property="sealDown" column="sealDown"/>
        <result property="area" column="area"/>
        <result property="texture" column="texture"/>
        <result property="holeFace" column="holeFace"/>
        <result property="holeArrange" column="holeArrange"/>
        <result property="unregularPointCount" column="unregularPointCount"/>
        <result property="frontHoleCount" column="frontHoleCount"/>
        <result property="backHoleCount" column="backHoleCount"/>
        <result property="sideHoleCount" column="sideHoleCount"/>
        <result property="frontModelCount" column="frontModelCount"/>
        <result property="backModelCount" column="backModelCount"/>
        <result property="isDoor" column="isDoor"/>
        <result property="isArcAcross" column="isArcAcross"/>
        <result property="moduleTypeId" column="moduleTypeId"/>
        <result property="isSculpt" column="isSculpt"/>
        <result property="filterType" column="filterType"/>
        <result property="offsetX" column="offsetX"/>
        <result property="offsetY" column="offsetY"/>
        <result property="remark" column="remark"/>
        <result property="isCancel" column="isCancel"/>
        <result property="type" column="type"/>
        <result property="openDoorType" column="openDoorType"/>
        <result property="isRowHole" column="isRowHole"/>

        <result property="bodyId" column="bodyId"/>
        <result property="goodsName" column="goodsName"/>
        <result property="roomId" column="roomId"/>
        <result property="bodyName" column="bodyName"/>
        <result property="roomName" column="roomName"/>

        <result property="isSideSculpt" column="isSideSculpt"/>
        <result property="is2v" column="is2v"/>
        <result property="isSide2v" column="isSide2v"/>
        <result property="modelData" column="modelData" typeHandler="com.cf.imes.framework.mybatis.core.type.CompressStringTypeHandler"/>
        <result property="processGroupName" column="processGroupName"/>
    </resultMap>




    <select id="selectPlanPlateDetailList" resultMap="PlateDetailMap">
        SELECT
        ogs.goods_id as plateGoodsId,
        ogs.goods_name as goodsName,
        ogs.color as color,
        ogs.material as material,

        op.id as plateId,
        op.order_id as orderId,
        op.name as name,
        op.plate_no as plateNo,
        op.custom_plate_no as customPlateNo,
        op.type as type,
        op.goods_id as goodsId,
        op.width,
        op.height,
        op.thickness,
        op.split_width as splitWidth,
        op.split_height as splitHeight,
        op.split_thickness as splitThickness,
        op.seal_left as sealLeft,
        op.seal_right as sealRight,
        op.seal_up as sealUp,
        op.seal_down as sealDown,
        op.area as area,
        op.texture as texture,
        op.hole_face as holeFace,
        op.hole_arrange as holeArrange,
        op.is_door as isDoor,
        op.open_door_type as openDoorType,
        op.offset_x as offsetX,
        op.offset_y as offsetY,
        op.is_special_shaped as isSpecialShaped,
        op.is_sculpt as isSculpt,
        op.is_row_hole as isRowHole,
        op.remark as remark,
        op.model_data as modelData,
        oi.body_id as bodyId,
        oi.room_id as roomId,
        oi.group_id as groupId,
        og.group_type_id as groupTypeId,
        ob.name as bodyName,
        ob.room_name as roomName,
        og.name as processGroupName,
        og.group_type_name as processGroupTypeName

        FROM order_plate op

        JOIN order_goods ogs
        ON op.organ_id = ogs.organ_id
        AND op.order_id = ogs.order_id
        AND op.goods_id = ogs.id
        left join order_item oi on op.organ_id = oi.organ_id and op.order_id = oi.order_id and op.id = oi.plate_id
        join order_body ob on ob.organ_id = op.organ_id and ob.order_id = op.order_id and ob.id = oi.body_id
        left join order_group og on og.organ_id = op.organ_id and og.order_id = op.order_id and og.id = oi.group_id

        WHERE op.organ_id = #{organId}
        and op.order_id in
        <foreach collection="orderIds" item="orderId" open="(" close=")" separator=",">
            #{orderId}
        </foreach>
        AND op.deleted = false
        AND op.plan_id = #{planId}
        AND (
        (oi.group_id is not null and oi.group_id != 0)
        or
        (oi.group_id is null and oi.comp_id is null)
        )
    </select>

    <select id="selectIsCuttingPlate" resultType="java.lang.Long">

        select distinct
            op.id
        from order_plate op
        where op.organ_id = #{organId}
          and op.plan_id = #{planId}
          and op.is_cutted in (1,2)
            limit 1;


    </select>


    <select id="selectLabelOrderPlateDataByPlanId"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.LabelOrderPlateData">



        select distinct

            oi.body_id as bodyGroupNo,
            oi.room_id as roomGroupNo,
            ob.name as bodyName,
            ob.room_name as roomName,
            oi.package_id as packageNo,
            ogs.color as plateColor,
            ogs.material as plateMaterial,
            op.id as plateId,
            op.plan_id as planId,
            op.order_id as orderId,
            ogs.goods_name as goodsPlateName,
            ogs.raw_goods_id as rawGoodsId,
            op.name as plateName,
            op.plate_no as plateNo,
            op.custom_plate_no as customPlateNo,

            op.width as plateWidth,
            op.height as plateLength,
            op.thickness as plateThickness,
            ogs.width as goodsPlateWidth,
            ogs.height as goodsPlateLength,
            ogs.thickness as goodsPlateThickness,

            op.split_width as splitWidth,
            op.split_height as splitHeight,
            op.split_thickness as splitThickness,
            op.seal_left as sealLeft,
            op.seal_right as sealRight,
            op.seal_up as sealUp,
            op.seal_down as sealDown,
            op.remark as plateRemark

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

        where op.organ_id = #{organId} and op.plan_id = #{planId}



    </select>



    <select id="selectPlanGoodsList"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.SavePlanPlateList$GoodsItemList">


        select distinct

            og.id,
            og.goods_id,
            og.color,
            og.material,
            og.thickness

        from order_plan op
                 join order_goods og on op.organ_id = og.organ_id and op.order_id = og.order_id and op.goods_id = og.id

        where op.organ_id = #{organId}
          and op.plan_id = #{planId}


    </select>

    <select id="selectPlanPlateNumAndArea" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPlanPlateNumAndAreaRespVO">
        select count(op.id) as num, IFNULL(sum(op.area), 0) as area
        from order_plate op
        where op.plan_id = #{planId}
        and op.deleted = false
    </select>


    <select id="selectPlanPlateNumAndAreaGroup" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPlanPlateNumAndAreaRespVO">
        select op.plan_id as planId, count(op.id) as num, IFNULL(sum(op.area), 0) as area
        from order_plate op
        where op.plan_id in
        <foreach collection="planIds" item="planId" open="(" separator="," close=")">
            #{planId}
        </foreach>
        and op.deleted = false
        group by op.plan_id
    </select>
</mapper>