<?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"/>


    </resultMap>

    <select id="selectOrderList" resultMap="OrderPageMap">
        select
            <if test="type != null and type == @com.cf.imes.module.executor.enums.OrderPlanQueryTypeEnum@ORDER_GROUP.getType()">
                distinct
            </if>
            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()">
                sum(og.plate_num - og.planned_plate_num) as num,
                sum(og.area - og.planned_plate_area) as area,
            </if>

            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 order_goods og on og.organ_id = o.organ_id and og.order_id = o.id
        </if>

            ${ew.customSqlSegment}
            and o.plate_num - o.planned_plate_num > 0
        <if test="type != null and type == @com.cf.imes.module.executor.enums.OrderPlanQueryTypeEnum@GOODS_GROUP.getType()">
            and og.plate_num - og.planned_plate_num > 0
            group by orderId,orderDate,deliveryDate,customer,address,defineId,status
        </if>

        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="selectOrderPlanPlateList" resultMap="GoodsListMap" parameterType="java.lang.Long">

        select
            op.plan_id as planId,
            og.width as width,
            og.height as height,
            og.material as material,
            og.thickness as thickness ,
            og.color as color,
            og.brand as brand,
            COUNT(op.id) AS count,
            SUM(op.area) AS area

        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.plan_id in
        <foreach collection="planIds" item="planIds" open="(" close=")" separator=",">
            #{planIds}
        </foreach>
        GROUP BY
            planId,
            width,
            height,
            material,
            thickness,
            color,
            brand
    </select>


    <select id="selectPlateListByPlateIds"
            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,
                        op.is_side_sculpt as isSideSculpt,
                        op.is_2v as is2v,
                        op.is_side_2v as isSide2v

        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

            ${ew.customSqlSegment}

        limit #{pageNo},#{pageSize};


    </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>







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


        select count(distinct op.id)

        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

            ${ew.customSqlSegment}


    </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"/>

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

        from order_item oi
                join order_plate op on op.order_id = oi.order_id and op.organ_id = oi.organ_id and op.id = oi.plate_id
                join order_goods og on og.order_id = oi.order_id and og.organ_id = oi.organ_id and og.id = op.goods_id
            ${ew.customSqlSegment}
            and oi.plate_id != 0
            and og.plate_num - og.planned_plate_num > 0
            and og.deleted = false
    </select>




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


            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

        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
        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.controller.admin.plan.vo.PlateDetailRespVO">

        <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="hasHole" column="hasHole"/>

        <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="hasHole" column="hasHole"/>

        <result property="isSideSculpt" column="isSideSculpt"/>
        <result property="is2v" column="is2v"/>
        <result property="isSide2v" column="isSide2v"/>

        <collection property="processGroupLists" javaType="java.util.List"   ofType="com.cf.imes.module.executor.controller.admin.plan.bo.ProcessGroupList"  >

            <result property="groupId" column="groupId"/>
            <result property="groupName" column="groupName"/>
        </collection >
    </resultMap>




    <select id="selectPlanPlateDetailList" resultMap="PlateDetailMap">



        select distinct

            oi.body_id as bodyId,
            oi.group_id as groupId,
            oi.room_id as roomId,
            og.name as groupName,
            ob.name as bodyName,
            ob.room_name as roomName,
            ogs.goods_id as plateGoodsId,
            ogs.goods_name as goodsName,
            ogs.brand as brand,
            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 as width,
            op.height as height,
            op.thickness as thickness,
            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.open_door_type as openDoorType,
            op.is_special_shaped as isSpecialShaped,
            op.is_sculpt as isSculpt,
            op.remark as remark

        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.id = oi.plate_id
                 left join order_group og on op.organ_id = og.organ_id and op.order_id = og.order_id and oi.group_id = og.id
                 left join order_body ob on op.organ_id = ob.organ_id and op.order_id = ob.order_id and oi.body_id = ob.id

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


    </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>


</mapper>