<?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.plate.PlateMapper">


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

        select a.id as plateId, a.name, a.width, a.height, a.thickness, a.area,
        b.custom_order_no, b.customer, b.address, b.remark, b.id as orderId,
        c.goods_name, c.material, c.color, a.goods_id,oi.num,a.is_special_shaped as specialShaped,a.is_sculpt as profiling

        from order_plate a
        LEFT JOIN order_item oi on a.id = oi.plate_id and a.order_id = oi.order_id and a.organ_id = oi.organ_id
        LEFT JOIN `orders` b on a.order_id = b.id and a.organ_id = b.organ_id
        LEFT JOIN order_goods c on a.goods_id = c.id and a.organ_id = c.organ_id
        left join order_plan_item opi on oi.id = opi.item_id and oi.organ_id = opi.organ_id
                <where>
                    a.organ_id = #{organId}
                    and a.is_cancel = 0
                    and opi.item_id is null
                    <if test="orderId !=null">
                       and a.order_id = #{orderId}
                    </if>

                </where>


    </select>

    <select id="selectPlateByPlanId"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.PlateResList">
        select a.order_id, a.plate_no , a.name as plateName,a.is_special_shaped,a.is_sculpt,a.area,a.seal_left,a.seal_right,a.seal_up,a.seal_down, e.material, e.color, a.width, a.height, a.thickness, b.room_id, b.body_id
        from order_plate a
                 LEFT JOIN order_item b ON a.id = b.plate_id AND a.order_id = b.order_id and a.organ_id = b.organ_id
                 LEFT JOIN order_goods e ON e.id = a.goods_id AND e.order_id = a.order_id and e.organ_id = a.organ_id
                 LEFT JOIN order_plan_item p ON b.id = p.item_id and b.organ_id = p.organ_id
            ${ew.customSqlSegment}


    </select>
    <select id="selectPlateList"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.PlateParam">
        select a.id, a.plate_no, a.height length, a.width, a.texture, a.hole_arrange, a.hole_face,  if ( (count(a.unregular_point_count) = 0), 1, 0) as  isRect
        from order_plate a
        left join order_item b on a.id = b.plate_id
        where b.plan_id = #{planId}
        group by a.id

    </select>

    <select id="selectProductList"
            resultType="com.cf.imes.module.executor.controller.admin.plate.vo.PlateRespVO">
        SELECT a.id, a.plate_no, a.height, a.thickness, a.width, a.name, a.area, a.seal_left, a.seal_right,
               a.seal_up, a.seal_down, a.texture, a.hole_face, a.hole_arrange, a.unregular_point_count, a.front_hole_count, a.back_hole_count,
               a.side_hole_count, a.front_model_count, a.back_model_count, a.remark, c.material, c.color
        FROM order_plate a
                 LEFT JOIN order_goods c ON a.goods_id = c.id AND a.order_id = c.order_id and a.organ_id = c.organ_id
                 LEFT JOIN order_item b ON a.id = b.plate_id and a.organ_id = b.organ_id
        WHERE b.organ_id = #{organId} and  b.order_id = #{orderId}
          AND (#{roomId} IS NULL OR b.room_id = #{roomId})
          AND (#{bodyId} IS NULL OR b.body_id = #{bodyId})
        GROUP BY a.id, c.material, c.color;
    </select>

    <select id="selectBatchOrderIdsAndGoodsIds" resultType="java.lang.Long">
        select oi.id from order_plate op
        join order_item oi on op.id = oi.plate_id and op.order_id = oi.order_id and op.organ_id = oi.organ_id
        where  op.organ_id = #{organId}  and  op.order_id in
        <foreach collection="itemList" item="item" open="(" close=")" separator=",">
            (#{item.orderId})
        </foreach>
        and
        op.goods_id in
        <foreach collection="itemList" item="item" open="(" close=")" separator=",">
            (#{item.goodsId})
        </foreach>

    </select>





<!--    <resultMap id="PlateNumListMap" type="com.cf.imes.module.executor.controller.admin.plate.vo.PlateDetailRespVO">-->

<!--&lt;!&ndash;        <result property="bigPlateNum" column="bigPlateNum"/>&ndash;&gt;-->
<!--        <result property="planStatus" column="planStatus"/>-->
<!--        <result property="remainPlateNum" column="remainPlateNum"/>-->
<!--    </resultMap>-->
<!--    <select id="selectPlateNumList"-->
<!--            resultMap="PlateNumListMap"-->
<!--            parameterType="com.cf.imes.module.executor.controller.admin.plate.vo.PlateDetailReqVO">-->

<!--&#45;&#45;         select count(distinct og.id) as bigPlateNum,-->
<!--&#45;&#45;                op.status as planStatus,-->
<!--&#45;&#45;                orp.count as remainPlateNum-->
<!--&#45;&#45;         from order_goods og-->
<!--&#45;&#45;                  left join order_plan op on og.order_id = op.order_nos-->
<!--&#45;&#45;                  left join order_remain_plate orp on op.id = orp.plan_id-->
<!--&#45;&#45;         where order_id = #{orderId}-->
<!--&#45;&#45;         group by planStatus,remainPlateNum;-->
<!--    select distinct-->
<!--        op.status as planStatus,-->
<!--        sum(orp.count ) as remainPlateNum-->
<!--    from order_plan op-->
<!--            join order_plan_item opi on op.id = opi.plan_id-->
<!--            join order_item oi on oi.id = opi.item_id-->
<!--            join order_remain_plate orp on op.id = orp.plan_id-->
<!--    where oi.order_id = #{orderId}-->
<!--    group by planStatus;-->

<!--    </select>-->




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

        <result property="itemId" column="itemId"/>
        <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="processGroupId" column="processGroupId"/>
        <result property="processGroupName" column="processGroupName"/>
        <result property="hasHole" column="hasHole"/>

    </resultMap>
    <select id="selectPlateDetialList"
            resultMap="PlateDetialMap"
            resultType="java.lang.Long">

        select distinct op.*,
                        oi.id as itemId,
                        ogs.goods_name as goodsName,
                        ob.id as bodyId,
                        ob.room_id as roomId,
                        ob.name as bodyName,
                        ob.room_name as roomName,
                        og.id as processGroupId,
                        og.name as processGroupName,
                        CASE WHEN op.unregular_point_count = 0 and op.front_hole_count  = 0  and op.back_hole_count = 0 and op.side_hole_count = 0 THEN false ELSE true END AS hasHole
        from order_plate op
                 left join order_goods ogs on op.goods_id = ogs.id and op.organ_id = ogs.organ_id
                 join order_item oi on op.id = oi.plate_id and op.organ_id = oi.organ_id
                 join order_body ob on oi.body_id = ob.id and oi.organ_id = ob.organ_id
                 left join order_group og on oi.group_id = og.id and oi.organ_id = og.organ_id
        where op.order_id =#{orderId} and op.organ_id = #{organId}

    </select>


    <select id="selectPlateDetialListByIds"
            resultMap="PlateDetialMap"
            resultType="java.lang.Long">

        select distinct op.*,
                        oi.id as itemId,
                        ogs.goods_name as goodsName,
                        ob.name as bodyName,
                        ob.room_name as roomName,
                        og.name as processGroupName,
                        CASE WHEN op.unregular_point_count = 0 and op.front_hole_count  = 0  and op.back_hole_count = 0 and op.side_hole_count = 0 THEN false ELSE true END AS hasHole
        from order_plate op
            left join order_goods ogs on op.goods_id = ogs.id and op.organ_id = ogs.organ_id
            join order_item oi on op.id = oi.plate_id and op.organ_id = oi.organ_id
            join order_body ob on oi.body_id = ob.id and oi.organ_id = ob.organ_id
            left join order_group og on oi.group_id = og.id and oi.organ_id = og.organ_id

        where op.organ_id = #{organId} and op.order_id in

        <foreach collection="orderIds" item="orderIds" open="(" close=")" separator=",">

            #{orderIds}

        </foreach>

    </select>


    <select id="selectPlateGoodsList" resultType="com.cf.imes.module.executor.controller.admin.plate.vo.PlateGoodsRespVO">

        select op.id,
               op.name,
               op.goods_id,
               ogs.goods_name,
               op.front_hole_count,
               op.back_hole_count,
               op.side_hole_count,
               op.unregular_point_count,
               op.front_model_count,
               op.back_model_count,
               op.hole_arrange,
               op.hole_face,
               op.texture,
               op.type,
               op.open_door_type,
               op.filter_type,
               op.seal_left,
               op.seal_right,
               op.seal_up,
               op.seal_down,
               op.offset_x,
               op.offset_y,
               op.is_arc_across,
               op.is_door,
               op.is_cancel,
               op.remark,
               op.plate_no,
               op.split_height,
               op.split_width,
               op.split_thickness,
               op.module_type_id,
               op.height,
               op.width,
               op.thickness,
               op.area,
               op.is_special_shaped,
               op.is_sculpt,
               ogs.price,
               ogs.brand,
               ogs.height    as goods_height,
               ogs.width     as goods_width,
               ogs.thickness as goods_thickness,
               ogs.color,
               ogs.material,
               ogs.spec,
               bdy.id as body_id,
               bdy.`name` as body_name,
               bdy.room_id,
               bdy.room_name
        from order_plate op
                 left join order_goods ogs on op.goods_id = ogs.id
                 left join order_item its on op.id = its.plate_id
                 left join order_body bdy on bdy.id = its.body_id
                 left join order_goods gods on gods.id = op.goods_id
        where op.order_id = #{orderId}
          and op.organ_id = #{organId}
          and op.deleted = #{deleted}
    </select>

    <select id="selectPlateGoodsListSummary" resultType="com.cf.imes.module.executor.controller.admin.plate.vo.PlateGoodsRespVO">

        select
               op.name,
               op.open_door_type,
               op.height,
               op.width,
               op.thickness,
               op.area,
               ogs.thickness as goods_thickness,
               ogs.color,
               ogs.material,
               ogs.spec,
               bdy.id as body_id,
               bdy.`name` as body_name,
               bdy.room_id,
               bdy.room_name
        from order_plate op
                 left join order_goods ogs on op.goods_id = ogs.id
                 left join order_item its on op.id = its.plate_id
                 left join order_body bdy on bdy.id = its.body_id
                 left join order_goods gods on gods.id = op.goods_id
        where op.order_id = #{orderId}
          and op.organ_id = #{organId}
          and op.deleted = #{deleted}
    </select>

    <update id="updateDeletedById">
        UPDATE order_plate
        SET deleted = #{deleted}
        WHERE id IN (
            SELECT plate_id FROM order_item
            WHERE body_id = #{bodyId} AND organ_id = #{organId}
        )
    </update>
    <select id="countPlateByOrderId" resultType="java.lang.Integer">
        SELECT count(*) FROM order_plate
        WHERE order_id = #{orderId} AND organ_id = #{organId} AND deleted = #{deleted}
    </select>

    <select id="selectPlateSize" resultType="java.math.BigDecimal">


        select sum(area) from order_plate where order_id = #{orderId};


    </select>

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


        select a.order_id, a.plate_no , a.name as plateName,a.is_special_shaped,a.is_sculpt,a.area,a.seal_left,a.seal_right,a.seal_up,a.seal_down, e.material, e.color, a.width, a.height, a.thickness, b.room_id, b.body_id
        from order_plan_item p
                 left join order_item b on p.item_id = b.id and b.organ_id = p.organ_id
                 left join order_plate a on b.plate_id = a.id and b.order_id = a.order_id and b.organ_id = a.organ_id
                 left join order_goods e on a.goods_id = e.id and a.order_id = e.order_id and a.organ_id = e.organ_id

        where p.plan_id = #{planId} and p.organ_id = #{organId} ;



    </select>


</mapper>