<?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.query.OrderItemMapper">


    <resultMap id="orderItemMap" type="com.cf.imes.module.manage.controller.admin.query.vo.pack.PackageDetailsRespVO">
        <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="selectByPackageId"
            parameterType="java.lang.Long"
            resultMap="orderItemMap">

        select distinct
            og.name as name,
            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_item oi
                 join order_plate op on oi.plate_id = op.id and oi.organ_id = op.organ_id
                 join order_goods ogs on op.goods_id = ogs.id and op.organ_id = ogs.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 ob.organ_id = og.organ_id

        where
        oi.package_id =#{packageId} and oi.organ_id = #{organId}

    </select>



    <!--  查看板材信息的组合名称-->
    <select id="selectCombinationName"
            resultType="com.cf.imes.module.manage.controller.admin.query.vo.productionStatus.OrderModuleVO">
        <![CDATA[
        select CONCAT(ob.room_name,'_',ob.name) as name,ob.id as bodyId from order_body ob where order_id =#{orderNo};
        ]]>
    </select>


    <!--  查看板材信息分页-->
    <resultMap id="PlateMap" type="com.cf.imes.module.manage.controller.admin.query.vo.productionStatus.OrderPlateRespVO">
        <result property="id" column="palteId"/>
        <result property="name" column="plateName"/>
        <result property="color" column="palteColor"/>
        <result property="width" column="plateWidth"/>
        <result property="height" column="plateHeight"/>
        <result property="thickness" column="plateThickness"/>
        <result property="material" column="plateMaterial"/>
    </resultMap>
    <select id="selectPlateList"
            resultMap="PlateMap"
            parameterType="com.cf.imes.module.manage.controller.admin.query.vo.productionStatus.PlatePageVO">

        select
            op.id as palteId,
            op.name as plateName,
            p.material as plateMaterial,
            p.color as palteColor,
            p.height as plateHeight,
            p.width as plateWidth,
            p.thickness as plateThickness
        from order_item oi
                 join order_plate op on oi.plate_id = op .id
                 join plate p on op.goods_id = p.goods_id

        <where>
            <if test="bodyId != null">
                and oi.body_id = #{bodyId}
            </if>

            <if test="groupId != null">
                 and oi.group_id = #{groupId}
            </if>

        </where>

    </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="plateId"/>
        <result property="plateName" column="plateName"/>
        <result property="material" column="plateMaterial"/>
        <result property="color" column="plateColor"/>
        <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="selectOrderPlateList"
            parameterType="java.lang.Long"
            resultMap="PlateDetailsMap" >
        select distinct
            og.name as name,
            ob.room_name as roomName,
            ob.name as bodyName,
            op.id as plateId,
            op.name as plateName,
            ogs.material as plateMaterial,
            ogs.color as plateColor,
            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_item oi
                 join order_plate op on oi.plate_id = op.id and oi.organ_id = op.organ_id
                 join order_goods ogs on op.goods_id = ogs.id and op.organ_id = ogs.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 ob.organ_id = og.organ_id
        where
            oi.order_id = #{orderNo} and oi.organ_id = #{organId}
    </select>








    <select id="selectPrintPlateList"
            parameterType="java.lang.Long"
            resultMap="PlateDetailsMap">

        select
            op.plate_no as plateId,
            op.height as plateHeight,
            op.width as plateWidth,
            op.thickness as plateThickness,
            op.name as plateName,
            og.material as plateMaterial,
            og.color as plateColor,
            ob.name as bodyName,
            ob.room_name as roomName

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

        where oi.order_id = #{orderNo} and oi.package_id = #{packageNo} and oi.organ_id = #{organId} ;
    </select>



    <update id="deleteOrderPack">

        UPDATE order_item oi
            JOIN order_package  opn ON oi.package_id = opn.id
            SET oi.package_id = 0, opn.deleted = true
        WHERE opn.id = #{reqVO.packageId} AND opn.package_no = #{reqVO.packageNo};

    </update>




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

        DELETE FROM order_item
        WHERE id IN (
            SELECT id FROM (
                               SELECT oi.id
                               FROM order_item oi
                               WHERE oi.parts_id = #{partsId}
                           ) AS subquery
        );

    </update>




    <delete id="deleteParts">

        delete from order_item
        where package_id  = #{reqVO.packageId}
            and order_id in
            (select order_id from order_package where id = #{reqVO.packageId}  and package_no = #{reqVO.packageNo} );

    </delete>




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

        update order_item oi

        set package_id = #{packId}

        where plate_id in
        <foreach item="id" collection="plateIdList" open="(" separator="," close=")">
            #{id}
        </foreach>

    </update>



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

        update order_item oi

        set package_id = 0

        where package_id = #{packId} and plate_id in
        <foreach item="id" collection="deletePlateIdList" open="(" separator="," close=")">
            #{id}
        </foreach>


    </update>



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

        update order_item oi

        set package_id = #{packId}

        where parts_id in

        <foreach item="id" collection="addPartsIdList" open="(" separator="," close=")">
            #{id}
        </foreach>

    </update>


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

        update order_item oi

        set package_id = #{packId}

        where order_id = #{orderNo} and
            parts_id in
        <foreach item="id" collection="addPartsList" open="(" separator="," close=")">
            #{id}
        </foreach>

    </update>


    <update id="deletePartsList">

        update order_item oi

        set package_id = 0

        where package_id = #{packId} and order_id = #{orderNo} plate_id in
        <foreach item="id" collection="deletePartsList" open="(" separator="," close=")">
            #{id}
        </foreach>

    </update>



    <select id="selectBodyName" resultType="java.lang.String" parameterType="java.lang.Long">


        select ob.room_name
        from order_item oi
                 join order_body ob on oi.body_id = ob.id
        where oi.plate_id = #{plateId};

    </select>




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

        <result property="plateNum" column="plateNum"/>
        <result property="plateProcessedNum" column="plateProcessedNum"/>

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

        select

            count(case
                      when oi.package_id !=0 and oi.parts_id = 0 then oi.plate_id end )as plateProcessedNum,

            count(
                    case
                        when oi.plate_id !=0   then oi.plate_id end
                )as plateNum
        from order_item oi
        where oi.order_id = #{orderId};


    </select>



    <resultMap id="PlateDetialsMap" type="com.cf.imes.module.manage.controller.admin.process.vo.PlateDetails">

            <result property="plateId" column="plateId"/>
            <result property="plateNo" column="plateNo"/>
            <result property="width" column="width"/>
            <result property="length" column="length"/>
            <result property="thickness" column="thickness"/>


    </resultMap>

    <select id="selectByOrderId"
            resultMap="PlateDetialsMap"
            parameterType="map">


        select distinct
        op.id as plateId,
        op.plate_no as plateNo,
        op.width as width,
        op.height as length,
        op.thickness as thickness
        from order_item oi

        join order_plate op on oi.plate_id = op.id and oi.organ_id = op.organ_id

        where oi.organ_id = #{organId}  and oi.order_id = #{reqVO.orderId}  and oi.body_id = #{reqVO.bodyId}


    </select>



    <select id="selectPlateListByBodyIds"
            resultMap="PlateDetialsMap"
            parameterType="java.lang.Long">


        select distinct
            op.id as plateId,
            op.plate_no as plateNo,
            op.width as width,
            op.height as length,
            op.thickness as thickness
        from order_item oi

                 join order_plate op on oi.plate_id = op.id and oi.organ_id = op.organ_id

        where oi.organ_id = #{organId}  and oi.order_id = #{reqVO.orderId}  and
              oi.body_id in

        <foreach item="id" collection="collect" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>


    <select id="selectOrdersPlateList"
            parameterType="java.lang.Long"
            resultMap="PlateDetailsMap">


        select distinct
            og.name as name,
            ob.room_name as roomName,
            ob.name as bodyName,
            op.id as plateId,
            op.name as plateName,
            ogs.material as plateMaterial,
            ogs.color as plateColor,
            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_item oi
                 join order_plate op on oi.plate_id = op.id and oi.organ_id = op.organ_id
                 join order_goods ogs on op.goods_id = ogs.id and op.organ_id = ogs.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 ob.organ_id = og.organ_id
        where
            oi.organ_id = #{userOrganId} and

            oi.order_id in

        <foreach item="orderIds" collection="orderIds" open="(" separator="," close=")">
            #{orderIds}
        </foreach>


    </select>


</mapper>