<?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.manage.dal.mysql.pack.OrderPackageMapper">

    <resultMap id="PackMap" type="com.cf.imes.module.manage.controller.admin.pack.vo.PackRespVO">

        <result property="packCount" column="numberOfPackages"/>
        <result property="packPieceCount" column="packagedBoard"/>
    </resultMap>


    <select id="selectPackPage" resultType="com.cf.imes.module.manage.controller.admin.pack.vo.PackRespVO"
            parameterType="java.util.List"
            resultMap="PackMap">


        select
        count(DISTINCT oi.package_id ) as numberOfPackages,
        count(DISTINCT oi.plate_id) as packagedBoard

        from order_package opn
            join order_item oi on opn.id = oi.package_id
        WHERE opn.order_id IN
        <foreach item="id" collection="collect" open="(" separator="," close=")">
            #{id}
        </foreach>
          and opn.status &lt;&gt; 0;
    </select>







    <resultMap id="PlateDetailsMap" type="com.cf.imes.module.manage.controller.admin.pack.vo.PlateDetailsRespVO">
        <result property="roomName" column="roomName"/>
        <result property="cabinetName" column="bodyName"/>
        <result property="plateNo" column="palteId"/>
        <result property="plateName" column="plateName"/>
        <result property="material" column="plateMaterial"/>
        <result property="color" column="palteColor"/>
        <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"/>
        <result property="processGroup" column="name"/>
    </resultMap>
    <select id="selectMissingBoard"
            parameterType="java.lang.Long"
            resultMap="PlateDetailsMap">

        select
        ob.room_name as roomName,
        ob.name as bodyName,
        op.id as palteId,
        op.name as plateName,
        ogs.material as plateMaterial,
        ogs.color as palteColor,
        op.height as plateHeight,
        op.width as plateWidth,
        op.thickness as plateThickness,
        op.area as plateArea,
        op.texture as texture,
        op.remark as remark,
        op.is_special_shaped as specialShaped,
        op.is_sculpt as profiling,

        CASE WHEN op.front_hole_count = 0 or op.back_model_count = 0  or op.side_hole_count = 0  THEN false ELSE true END AS rowHole

        from order_plate op
        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

        where
            oi.order_id = #{orderNo}
        and
            oi.organ_id = #{organId}
        and
            op.id not in (
        select oi2.plate_id
        from order_item oi2
        where  oi2.order_id = #{orderNo} and oi2.package_id &lt;&gt; '');

    </select>


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

        UPDATE order_package
        SET deleted = true
        WHERE id = #{packageId} AND package_no = #{packageNo};

    </update>



    <select id="selectByOrderNo"
            resultType="com.cf.imes.module.manage.dal.dataobject.query.OrderPackageDO"
            parameterType="map">

        select distinct
        opn.*
        from order_package opn
        join order_item oi on opn.id = oi.package_id and opn.organ_id = oi.organ_id
        left join order_plate op on oi.plate_id = op.id and oi.organ_id = op.organ_id

        <where>

            <if test="reqVO.orderId !=null">
                    and opn.order_id = #{reqVO.orderId}
            </if>

            <if test="reqVO.plateNo !=null  ">
                    and op.plate_no = #{reqVO.plateNo}
            </if>

            <if test="reqVO.packStatus !=null ">
                and opn.status= #{reqVO.packStatus}
            </if>

            <if test="reqVO.packageNo !=null and reqVO.packageNo != '' ">
                and opn.package_no= #{reqVO.packageNo}
            </if>

             and opn.organ_id = #{organId}
        </where>


    </select>


</mapper>