<?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 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.unregular_point_count, p.front_hole_count,
        p.back_hole_count, p.side_hole_count,p.width,p.height,p.thickness,gp.name
        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 i.room_id = #{roomId}
        </if>
        <if test="bodyId != null">
            AND i.body_id = #{bodyId}
        </if>
        <if test="groupId != null">
            AND i.group_id = #{groupId}
        </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.id, p.model, p.spec, p.name,i.num, p.unit, p.brand, p.factory, p.remark,p.type,p.price,
        p.is_composite,p.create_time
        FROM order_item i
        RIGHT JOIN `order_parts` p ON i.parts_id = p.id
        WHERE i.order_id = #{orderId} AND i.organ_id = #{organId} AND p.deleted = #{deleted}
        <if test="roomId != null">
            AND i.room_id = #{roomId}
        </if>
        <if test="bodyId != null">
            AND i.body_id = #{bodyId}
        </if>
        <if test="name != null">
            AND p.name = #{name}
        </if>
    </select>

    <select id="selectPartsByOrderId"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPlatesDetailRespVO">
        SELECT i.order_id, i.room_id, i.body_id, i.parts_id, p.`name` as part_name, i.num as plate_num
        FROM `order_item` i
                 RIGHT JOIN `order_parts` p ON i.parts_id = p.id
        WHERE i.order_id = #{orderId} AND i.organ_id = #{organId}  AND p.deleted = #{deleted}
    </select>



    <resultMap id="PlateListMap" type="com.cf.imes.module.executor.controller.admin.plate.vo.PlateRespVO">

        <result property="color" column="color"/>
        <result property="material" column="material"/>
        <result property="bodyName" column="bodyName"/>
        <result property="roomName" column="roomName"/>
        <result property="itemId" column="itemId"/>
        <result property="processGroupId" column="processGroupId"/>
        <result property="brand" column="brand"/>
        <result property="hasTexture" column="hasTexture"/>
        <result property="bigPlateLength" column="bigPlateLength"/>
        <result property="bigPlateWidth" column="bigPlateWidth"/>
        <result property="spec" column="spec"/>

    </resultMap>
    <select id="selectPlateList"
            resultMap="PlateListMap"
            parameterType="map">

        select distinct
        op.*,
        og.color as color,
        og.material as material,
        ob.name as bodyName,
        ob.room_name as roomName,
        oi.id as itemId,
        oi.group_id as processGroupId,
        og.brand as brand,
        (case
        when p.texture = '' then  '是'
        else  '否' end ) as hasTexture,
        og.height as bigPlateLength,
        og.width as bigPlateWidth,
        og.spec as spec
        from order_item oi
            join order_plate op on oi.plate_id = op.id and oi.organ_id = op.organ_id
            join order_body ob on oi.body_id = ob.id and oi.organ_id = ob.organ_id
            join order_goods og on op.goods_id = og.id and op.organ_id = og.organ_id
            join plate p on og.goods_id = p.id and og.organ_id = p.organ_id
        <where>
                oi.organ_id = #{organId}
            <if test="reqVO.orderId != null and reqVO.orderId != ''">
                AND oi.order_id = #{reqVO.orderId}
            </if>
            <if test="reqVO.bodyId != null and reqVO.bodyId != ''">
                AND oi.body_id = #{reqVO.bodyId}
            </if>
            <if test="reqVO.plateId != null and reqVO.plateId != ''">
                AND oi.plate_id = #{reqVO.plateId}
            </if>

        </where>

    </select>




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

        <result property="bigPlateNum" column="bigPlateNum"/>
        <result property="planStatus" column="planStatus"/>
        <result property="remainPlateNum" column="remainPlateNum"/>
    </resultMap>
    <select id="selectPlateNumList"
            resultMap="PlateNumListMap"
            resultType="java.lang.Long">

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

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


    <resultMap id="PackListMap" type="com.cf.imes.module.executor.controller.admin.plan.saveOptimize.PrintOrderPackReqVO">

        <result property="orderId" column="orderId"/>
        <result property="plateId" column="plateId"/>
        <result property="packId" column="packId"/>
        <result property="packNo" column="packNo"/>
        <result property="packNum" column="packNum"/>

    </resultMap>
    <select id="selectPackList"
            parameterType="java.lang.Long"
            resultMap="PackListMap">


        select distinct oi.order_id as orderId,
                        oi.plate_id as plateId,
                        op.id as packId,
                        op.package_no as packNo,
                        (case when op.status !=0  then count(op.id) end) as packNum
        from order_item oi
                 left join order_package op on oi.order_id = op.order_id and oi.organ_id = op.organ_id

        where  oi.parts_id = '' and oi.organ_id = #{organId}
          and oi.order_id in

        <foreach item="orderIds" collection="orderIds" open="(" separator="," close=")">
            #{orderIds}
        </foreach>
        group by oi.order_id,oi.plate_id,op.id,op.package_no;




    </select>





</mapper>