<?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.veneer.VeneerManageMapper">

    <resultMap id="OrderMap"
               type="com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManageOrderRespVO">
        <result property="orderId" column="orderId"/>
        <result property="parentId" column="parentId"/>
        <result property="orderType" column="orderType"/>
        <result property="customOrderNo" column="customOrderNo"/>
        <result property="customer" column="customer"/>
        <result property="address" column="address"/>
        <result property="orderDate" column="orderDate"/>
        <result property="num" column="num"/>
        <result property="area" column="area"/>
    </resultMap>

    <resultMap id="GoodsMap"
               type="com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManageGoodsRespVO">
        <result property="goodsId" column="goodsId"/>
        <result property="goodsName" column="goodsName"/>
        <result property="material" column="material"/>
        <result property="color" column="color"/>
        <result property="thickness" column="thickness"/>
        <result property="num" column="num"/>
        <result property="area" column="area"/>
    </resultMap>

    <resultMap id="PlateVeneerMap"
               type="com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManagePlateRespVO">
        <id property="id" column="id"/>
        <result property="veneerNo" column="veneerNo"/>
        <result property="plateNo" column="plateNo"/>
        <result property="plateName" column="plateName"/>
        <result property="length" column="length"/>
        <result property="width" column="width"/>
        <result property="thickness" column="thickness"/>
        <result property="planId" column="planId"/>
        <result property="sourceVeneerNo" column="sourceVeneerNo"/>
        <result property="veneerName" column="veneerName"/>
        <result property="productLevel" column="productLevel"/>
        <result property="color" column="color"/>
        <result property="material" column="material"/>
    </resultMap>

    <resultMap id="UnplannedPlateMap"
               type="com.cf.imes.module.executor.controller.admin.veneer.vo.UnplannedVeneerPlateRespVO"
               autoMapping="true">
        <id property="id" column="id"/>
        <id property="plateId" column="plateId"/>
        <result property="modelData" column="modelData"
                typeHandler="com.cf.imes.framework.mybatis.core.type.CompressStringTypeHandler"/>
    </resultMap>

    <select id="selectRootOrderIdList" resultType="java.lang.Long">
        SELECT o.root_id
        FROM orders o
        WHERE o.deleted = false
          AND IFNULL(o.decomposed_veneer_plate_num, 0)
              &gt; IFNULL(o.planned_veneer_plate_num, 0)
        <if test="req.orderId != null and req.orderId != ''">
            AND CAST(o.id AS CHAR) LIKE CONCAT('%', #{req.orderId}, '%')
        </if>
        <if test="req.customOrderNo != null and req.customOrderNo != ''">
            AND o.custom_order_no LIKE CONCAT('%', #{req.customOrderNo}, '%')
        </if>
        <if test="req.orderType != null">
            AND o.order_type = #{req.orderType}
        </if>
        <if test="req.customer != null and req.customer != ''">
            AND o.customer LIKE CONCAT('%', #{req.customer}, '%')
        </if>
        <if test="req.address != null and req.address != ''">
            AND o.address LIKE CONCAT('%', #{req.address}, '%')
        </if>
        <if test="orderDateTime != null and orderDateTime.length == 2">
            AND o.order_date BETWEEN #{orderDateTime[0]} AND #{orderDateTime[1]}
        </if>
        GROUP BY o.root_id
        ORDER BY o.root_id DESC
    </select>

    <select id="selectOrderList" resultMap="OrderMap">
        SELECT o.id AS orderId,
               o.parent_id AS parentId,
               o.order_type AS orderType,
               o.custom_order_no AS customOrderNo,
               o.customer,
               o.address,
               o.order_date AS orderDate,
               o.plate_num AS num,
               o.area AS area
        FROM orders o
        WHERE o.deleted = false
          AND o.id IN
        <foreach collection="rootIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        ORDER BY o.id DESC
    </select>

    <select id="selectOrderChildList" resultMap="OrderMap">
        SELECT o.id AS orderId,
               o.parent_id AS parentId,
               o.order_type AS orderType,
               o.custom_order_no AS customOrderNo,
               o.customer,
               o.address,
               o.order_date AS orderDate,
               o.plate_num AS num,
               o.area AS area
        FROM orders o
        WHERE o.deleted = false
          AND IFNULL(o.decomposed_veneer_plate_num, 0)
              &gt; IFNULL(o.planned_veneer_plate_num, 0)
          AND o.parent_id IN
        <foreach collection="rootIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        <if test="req.orderId != null and req.orderId != ''">
            AND CAST(o.id AS CHAR) LIKE CONCAT('%', #{req.orderId}, '%')
        </if>
        <if test="req.customOrderNo != null and req.customOrderNo != ''">
            AND o.custom_order_no LIKE CONCAT('%', #{req.customOrderNo}, '%')
        </if>
        <if test="req.orderType != null">
            AND o.order_type = #{req.orderType}
        </if>
        <if test="req.customer != null and req.customer != ''">
            AND o.customer LIKE CONCAT('%', #{req.customer}, '%')
        </if>
        <if test="req.address != null and req.address != ''">
            AND o.address LIKE CONCAT('%', #{req.address}, '%')
        </if>
        <if test="orderDateTime != null and orderDateTime.length == 2">
            AND o.order_date BETWEEN #{orderDateTime[0]} AND #{orderDateTime[1]}
        </if>
        ORDER BY o.id DESC
    </select>

    <select id="selectGoodsPage" resultMap="GoodsMap">
        SELECT og.goods_id AS goodsId,
               og.goods_name AS goodsName,
               og.material,
               og.color,
               og.thickness,
               SUM(og.plate_num) AS num,
               SUM(og.area) AS area
        FROM orders o
        JOIN order_goods og ON og.order_id = o.id
        WHERE o.deleted = false
          AND o.status IN (1, 2, 3)
          AND og.deleted = false
          AND IFNULL(og.decomposed_veneer_plate_num, 0)
              &gt; IFNULL(og.planned_veneer_plate_num, 0)
        <if test="req.goodsName != null and req.goodsName != ''">
            AND og.goods_name LIKE CONCAT('%', #{req.goodsName}, '%')
        </if>
        <if test="req.goodsId != null and req.goodsId != ''">
            AND og.goods_id LIKE CONCAT('%', #{req.goodsId}, '%')
        </if>
        <if test="req.material != null and req.material != ''">
            AND og.material LIKE CONCAT('%', #{req.material}, '%')
        </if>
        <if test="req.color != null and req.color != ''">
            AND og.color LIKE CONCAT('%', #{req.color}, '%')
        </if>
        <if test="req.thickness != null">
            AND og.thickness = #{req.thickness}
        </if>
        GROUP BY og.goods_id, og.goods_name, og.material, og.color, og.thickness
        ORDER BY og.thickness DESC
    </select>

    <select id="selectUnplannedRootOrderIdList" resultType="java.lang.Long">
        SELECT o.root_id
        FROM orders o
        WHERE o.deleted = false
          AND IFNULL(o.plate_num, 0) &gt; IFNULL(o.planned_veneer_plate_num, 0)
          AND EXISTS (
              SELECT 1
              FROM order_plate_veneer opv
              WHERE opv.order_id = o.id
                AND opv.deleted = false
                AND opv.plan_id = 0
          )
        <if test="req.orderId != null and req.orderId != ''">
            AND CAST(o.id AS CHAR) LIKE CONCAT('%', #{req.orderId}, '%')
        </if>
        <if test="req.customOrderNo != null and req.customOrderNo != ''">
            AND o.custom_order_no LIKE CONCAT('%', #{req.customOrderNo}, '%')
        </if>
        <if test="req.orderType != null">
            AND o.order_type = #{req.orderType}
        </if>
        <if test="req.customer != null and req.customer != ''">
            AND o.customer LIKE CONCAT('%', #{req.customer}, '%')
        </if>
        <if test="req.address != null and req.address != ''">
            AND o.address LIKE CONCAT('%', #{req.address}, '%')
        </if>
        <if test="orderDateTime != null and orderDateTime.length == 2">
            AND o.order_date BETWEEN #{orderDateTime[0]} AND #{orderDateTime[1]}
        </if>
        GROUP BY o.root_id
        ORDER BY o.root_id DESC
    </select>

    <select id="selectUnplannedGoodsPage" resultMap="GoodsMap">
        SELECT og.goods_id AS goodsId,
               og.goods_name AS goodsName,
               og.material,
               og.color,
               og.thickness,
               SUM(og.plate_num) AS num,
               SUM(og.area) AS area
        FROM orders o
        JOIN order_goods og ON og.order_id = o.id
        WHERE o.deleted = false
          AND o.status IN (1, 2, 3)
          AND og.deleted = false
          AND IFNULL(og.plate_num, 0) &gt; IFNULL(og.planned_veneer_plate_num, 0)
          AND EXISTS (
              SELECT 1
              FROM order_plate_veneer opv
              WHERE opv.order_id = og.order_id
                AND opv.order_goods_id = og.id
                AND opv.deleted = false
                AND opv.plan_id = 0
          )
        <if test="req.goodsName != null and req.goodsName != ''">
            AND og.goods_name LIKE CONCAT('%', #{req.goodsName}, '%')
        </if>
        <if test="req.goodsId != null and req.goodsId != ''">
            AND og.goods_id LIKE CONCAT('%', #{req.goodsId}, '%')
        </if>
        <if test="req.material != null and req.material != ''">
            AND og.material LIKE CONCAT('%', #{req.material}, '%')
        </if>
        <if test="req.color != null and req.color != ''">
            AND og.color LIKE CONCAT('%', #{req.color}, '%')
        </if>
        <if test="req.thickness != null">
            AND og.thickness = #{req.thickness}
        </if>
        GROUP BY og.goods_id, og.goods_name, og.material, og.color, og.thickness
        ORDER BY og.thickness DESC
    </select>

    <select id="selectUnplannedOrderChildList" resultMap="OrderMap">
        SELECT o.id AS orderId,
               o.parent_id AS parentId,
               o.order_type AS orderType,
               o.custom_order_no AS customOrderNo,
               o.customer,
               o.address,
               o.order_date AS orderDate,
               o.plate_num AS num,
               o.area AS area
        FROM orders o
        WHERE o.deleted = false
          AND IFNULL(o.plate_num, 0) &gt; IFNULL(o.planned_veneer_plate_num, 0)
          AND o.parent_id IN
        <foreach collection="rootIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
          AND EXISTS (
              SELECT 1
              FROM order_plate_veneer opv
              WHERE opv.order_id = o.id
                AND opv.deleted = false
                AND opv.plan_id = 0
          )
        <if test="req.orderId != null and req.orderId != ''">
            AND CAST(o.id AS CHAR) LIKE CONCAT('%', #{req.orderId}, '%')
        </if>
        <if test="req.customOrderNo != null and req.customOrderNo != ''">
            AND o.custom_order_no LIKE CONCAT('%', #{req.customOrderNo}, '%')
        </if>
        <if test="req.orderType != null">
            AND o.order_type = #{req.orderType}
        </if>
        <if test="req.customer != null and req.customer != ''">
            AND o.customer LIKE CONCAT('%', #{req.customer}, '%')
        </if>
        <if test="req.address != null and req.address != ''">
            AND o.address LIKE CONCAT('%', #{req.address}, '%')
        </if>
        <if test="orderDateTime != null and orderDateTime.length == 2">
            AND o.order_date BETWEEN #{orderDateTime[0]} AND #{orderDateTime[1]}
        </if>
        ORDER BY o.id DESC
    </select>

    <sql id="PlateVeneerColumns">
        opv.id,
        opv.veneer_no AS veneerNo,
        opv.plate_no AS plateNo,
        opv.plate_name AS plateName,
        opv.plate_length AS length,
        opv.plate_width AS width,
        opv.plate_thickness AS thickness,
        opv.plan_id AS planId,
        opv.source_veneer_no AS sourceVeneerNo,
        opv.veneer_name AS veneerName,
        opv.product_level AS productLevel,
        opv.color,
        opv.material
    </sql>

    <select id="selectOrderPlateList" resultMap="PlateVeneerMap">
        SELECT
        <include refid="PlateVeneerColumns"/>
        FROM order_plate_veneer opv
        WHERE opv.deleted = false
          AND opv.plan_id = 0
          AND opv.order_id IN
        <foreach collection="req.orderIds" item="orderId" open="(" close=")" separator=",">
            #{orderId}
        </foreach>
        <if test="(req.roomIds != null and req.roomIds.size() > 0)
                or (req.bodyIds != null and req.bodyIds.size() > 0)">
            AND EXISTS (
                SELECT 1
                FROM order_item oi
                WHERE oi.order_id = opv.order_id
                  AND oi.plate_id = opv.order_plate_id
                  AND oi.plate_id != 0
                <if test="req.roomIds != null and req.roomIds.size() > 0">
                    AND oi.room_id IN
                    <foreach collection="req.roomIds" item="roomId" open="(" close=")" separator=",">
                        #{roomId}
                    </foreach>
                </if>
                <if test="req.bodyIds != null and req.bodyIds.size() > 0">
                    AND oi.body_id IN
                    <foreach collection="req.bodyIds" item="bodyId" open="(" close=")" separator=",">
                        #{bodyId}
                    </foreach>
                </if>
            )
        </if>
        ORDER BY opv.order_plate_id DESC, opv.veneer_side ASC
    </select>

    <select id="selectGoodsPlateList" resultMap="PlateVeneerMap">
        SELECT
        <include refid="PlateVeneerColumns"/>
        FROM order_plate_veneer opv
        JOIN order_goods og ON og.order_id = opv.order_id
                           AND og.id = opv.order_goods_id
        WHERE opv.deleted = false
          AND opv.plan_id = 0
          AND og.deleted = false
          AND og.goods_id IN
        <foreach collection="goodsIds" item="goodsId" open="(" close=")" separator=",">
            #{goodsId}
        </foreach>
        ORDER BY opv.order_plate_id DESC, opv.veneer_side ASC
    </select>

    <sql id="UnplannedPlateColumns">
        opv.id,
        opv.order_id AS orderId,
        opv.order_goods_id AS orderGoodsId,
        opv.veneer_side AS veneerSide,
        opv.plate_surface AS plateSurface,
        opv.veneer_no AS veneerNo,
        opv.outer_margin AS outerMargin,
        opv.source_veneer_id AS sourceVeneerId,
        opv.source_veneer_no AS sourceVeneerNo,
        opv.veneer_name AS veneerName,
        opv.product_level AS productLevel,
        opv.color AS veneerColor,
        opv.material AS veneerMaterial,
        op.id AS plateId,
        og.goods_id AS goodsId,
        og.goods_name AS goodsName,
        op.plate_no AS plateNo,
        op.custom_plate_no AS customPlateNo,
        op.name,
        op.name AS plateName,
        og.color,
        og.brand,
        og.material,
        op.height,
        op.height AS length,
        op.width,
        op.thickness,
        op.split_width AS splitWidth,
        op.split_height AS splitHeight,
        op.split_thickness AS splitThickness,
        op.is_special_shaped AS isSpecialShaped,
        op.seal_left AS sealLeft,
        op.seal_right AS sealRight,
        op.seal_up AS sealUp,
        op.seal_down AS sealDown,
        op.reserved_edge_left AS reservedEdgeLeft,
        op.reserved_edge_right AS reservedEdgeRight,
        op.reserved_edge_up AS reservedEdgeUp,
        op.reserved_edge_down AS reservedEdgeDown,
        op.area,
        op.texture,
        op.hole_face AS holeFace,
        op.hole_arrange AS holeArrange,
        op.unregular_point_count AS unregularPointCount,
        op.front_hole_count AS frontHoleCount,
        op.back_hole_count AS backHoleCount,
        op.side_hole_count AS sideHoleCount,
        op.front_model_count AS frontModelCount,
        op.back_model_count AS backModelCount,
        op.is_door AS isDoor,
        op.is_arc_across AS isArcAcross,
        op.module_type_id AS moduleTypeId,
        op.offset_x AS offsetX,
        op.offset_y AS offsetY,
        op.remark,
        op.type,
        opg.name AS processGroupName,
        op.open_door_type AS openDoorType,
        op.is_row_hole AS isRowHole,
        op.is_sculpt AS isSculpt,
        op.is_side_sculpt AS isSideSculpt,
        op.is_2v AS is2v,
        op.is_side_2v AS isSide2v,
        CASE WHEN op.front_model_count &gt; 0 AND op.back_model_count &gt; 0
            THEN 1 ELSE 0 END AS doubleSideModel,
        CASE WHEN op.front_hole_count &gt; 0 AND op.back_hole_count &gt; 0
            THEN 1 ELSE 0 END AS doubleSideHole,
        CASE WHEN op.front_model_count &gt; 0 THEN 1 ELSE 0 END AS frontModel,
        CASE WHEN op.back_model_count &gt; 0 THEN 1 ELSE 0 END AS backModel,
        CASE WHEN op.front_hole_count &gt; 0 THEN 1 ELSE 0 END AS frontHole,
        CASE WHEN op.back_hole_count &gt; 0 THEN 1 ELSE 0 END AS backHole,
        op.is_side_sculpt AS sideModel,
        CASE WHEN op.side_hole_count &gt; 0 THEN 1 ELSE 0 END AS sideHole,
        op.model_data AS modelData
    </sql>

    <select id="selectUnplannedPlateList" resultMap="UnplannedPlateMap">
        SELECT
        <include refid="UnplannedPlateColumns"/>
        FROM order_plate_veneer opv
        JOIN order_plate op ON op.order_id = opv.order_id
                           AND op.id = opv.order_plate_id
        JOIN order_goods og ON og.order_id = opv.order_id
                           AND og.id = opv.order_goods_id
                           AND og.id = op.goods_id
        JOIN order_item oi ON oi.order_id = op.order_id
                          AND oi.plate_id = op.id
                          AND oi.plate_id != 0
        LEFT JOIN order_group opg ON opg.order_id = op.order_id
                                 AND opg.id = oi.group_id
        WHERE opv.deleted = false
          AND opv.plan_id = 0
          AND op.deleted = false
          AND og.deleted = false
        <choose>
            <when test="req.orderIds != null and req.orderIds.size() > 0">
                AND opv.order_id IN
                <foreach collection="req.orderIds" item="orderId" open="(" close=")" separator=",">
                    #{orderId}
                </foreach>
            </when>
            <otherwise>
                AND og.goods_id IN
                <foreach collection="req.goodsIds" item="goodsId" open="(" close=")" separator=",">
                    #{goodsId}
                </foreach>
            </otherwise>
        </choose>
        <if test="req.veneerNo != null and req.veneerNo != ''">
            AND opv.veneer_no LIKE CONCAT('%', #{req.veneerNo}, '%')
        </if>
        <if test="req.plateNo != null and req.plateNo != ''">
            AND opv.plate_no LIKE CONCAT('%', #{req.plateNo}, '%')
        </if>
        <if test="req.sourceVeneerNo != null and req.sourceVeneerNo != ''">
            AND opv.source_veneer_no LIKE CONCAT('%', #{req.sourceVeneerNo}, '%')
        </if>
        <if test="req.material != null and req.material != ''">
            AND opv.material LIKE CONCAT('%', #{req.material}, '%')
        </if>
        <if test="req.color != null and req.color != ''">
            AND opv.color LIKE CONCAT('%', #{req.color}, '%')
        </if>
        <if test="req.plateName != null and req.plateName != ''">
            AND opv.plate_name LIKE CONCAT('%', #{req.plateName}, '%')
        </if>
        <if test="req.length != null and req.length.length == 2">
            AND opv.plate_length BETWEEN #{req.length[0]} AND #{req.length[1]}
        </if>
        <if test="req.width != null and req.width.length == 2">
            AND opv.plate_width BETWEEN #{req.width[0]} AND #{req.width[1]}
        </if>
        <if test="req.thickness != null and req.thickness.length == 2">
            AND opv.plate_thickness BETWEEN #{req.thickness[0]} AND #{req.thickness[1]}
        </if>
        <if test="req.roomIds != null and req.roomIds.size() > 0">
            AND oi.room_id IN
            <foreach collection="req.roomIds" item="roomId" open="(" close=")" separator=",">
                #{roomId}
            </foreach>
        </if>
        <if test="req.bodyIds != null and req.bodyIds.size() > 0">
            AND oi.body_id IN
            <foreach collection="req.bodyIds" item="bodyId" open="(" close=")" separator=",">
                #{bodyId}
            </foreach>
        </if>
        ORDER BY op.id DESC, opv.veneer_side, oi.group_id
    </select>

    <select id="selectRemovedPlateVeneerList"
            resultType="com.cf.imes.module.executor.dal.dataobject.veneer.OrderPlateVeneerDO">
        SELECT opv.id,
               opv.order_id AS orderId,
               opv.order_goods_id AS orderGoodsId,
               opv.order_plate_id AS orderPlateId
        FROM order_plate_veneer opv
        WHERE opv.deleted = true
          AND opv.id IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <update id="softDeletePendingPlateVeneer">
        UPDATE order_plate_veneer
        SET deleted = true,
            update_time = CURRENT_TIMESTAMP
        WHERE deleted = false
          AND plan_id = 0
          AND id IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <select id="selectRemainingPlateIdList" resultType="java.lang.Long">
        SELECT opv.order_plate_id
        FROM order_plate_veneer opv
        WHERE opv.deleted = false
          AND opv.order_plate_id IN
        <foreach collection="plateIds" item="plateId" open="(" close=")" separator=",">
            #{plateId}
        </foreach>
    </select>

    <update id="restorePlateUndecomposed">
        UPDATE order_plate
        SET is_decomposed = false,
            update_time = CURRENT_TIMESTAMP
        WHERE deleted = false
          AND is_decomposed = true
          AND id IN
        <foreach collection="plateIds" item="plateId" open="(" close=")" separator=",">
            #{plateId}
        </foreach>
    </update>
</mapper>
