<?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.VeneerPlanMapper">

    <select id="selectPlanPage"
            resultType="com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanPageRespVO">
        SELECT ovp.id,
               ovp.sort,
               ovp.process_id AS processId,
               ovp.process_name AS processName,
               CONCAT_WS('-', NULLIF(v.material, ''), NULLIF(v.color, ''),
                         CONCAT(CAST(v.thickness AS CHAR), 'mm')) AS veneerInfo,
               ovp.status,
               ovp.is_scheduled AS isScheduled,
               ovp.veneer_num AS veneerNum,
               ovp.veneer_area AS veneerArea,
               ovp.create_time AS createTime,
               ovp.produce_time AS produceTime
        FROM order_veneer_plan ovp
        JOIN veneers v ON v.id = ovp.source_veneer_id
                       AND v.organ_id = ovp.organ_id
                       AND v.deleted = false
        WHERE ovp.deleted = false
        <if test="req.id != null and req.id != ''">
            AND CAST(ovp.id AS CHAR) LIKE CONCAT('%', #{req.id}, '%')
        </if>
        <if test="req.processName != null and req.processName != ''">
            AND ovp.process_name LIKE CONCAT('%', #{req.processName}, '%')
        </if>
        <if test="req.status != null and req.status.size() > 0">
            AND ovp.status IN
            <foreach collection="req.status" item="status" open="(" close=")" separator=",">
                #{status}
            </foreach>
        </if>
        ORDER BY ovp.id DESC
    </select>

    <select id="selectPlanSortList"
            resultType="com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanSortRespVO">
        SELECT ovp.id,
               ovp.sort,
               ovp.process_id AS processId,
               ovp.process_name AS processName,
               CONCAT_WS('-', NULLIF(v.material, ''), NULLIF(v.color, ''),
                         CONCAT(CAST(v.thickness AS CHAR), 'mm')) AS veneerInfo,
               ovp.status,
               ovp.is_scheduled AS isScheduled,
               ovp.veneer_num AS veneerNum,
               ovp.create_time AS createTime,
               ovp.produce_time AS produceTime
        FROM order_veneer_plan ovp
        JOIN veneers v ON v.id = ovp.source_veneer_id
                       AND v.organ_id = ovp.organ_id
                       AND v.deleted = false
        WHERE ovp.deleted = false
          AND ovp.status != #{openedStatus}
        ORDER BY ovp.sort, ovp.id
    </select>

    <update id="batchUpdatePlanSort">
        UPDATE order_veneer_plan
        SET sort = CASE id
        <foreach collection="sortList" item="item">
            WHEN #{item.planId} THEN #{item.sort}
        </foreach>
        END,
            version = version + 1,
            update_time = CURRENT_TIMESTAMP
        WHERE deleted = false
          AND status != #{openedStatus}
          AND id IN
        <foreach collection="sortList" item="item" open="(" close=")" separator=",">
            #{item.planId}
        </foreach>
    </update>

    <select id="selectPlannedVeneerList"
            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,
               opv.source_veneer_id AS sourceVeneerId
        FROM order_plate_veneer opv
        WHERE opv.deleted = false
          AND opv.plan_id = #{planId}
          AND opv.id IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        ORDER BY opv.id
    </select>

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

    <select id="selectPlanStatistics"
            resultType="com.cf.imes.module.executor.dal.dataobject.veneer.VeneerPlanStatisticsDO">
        SELECT COUNT(*) AS veneerNum,
               COALESCE(ROUND(SUM(
                   (opv.plate_length + opv.outer_margin * 2)
                   * (opv.plate_width + opv.outer_margin * 2) / 1000000
               ), 4), 0) AS veneerArea
        FROM order_plate_veneer opv
        WHERE opv.deleted = false
          AND opv.plan_id = #{planId}
    </select>

    <delete id="physicalDeleteEmptyPlan">
        DELETE FROM order_veneer_plan
        WHERE deleted = false
          AND id = #{planId}
          AND status NOT IN (#{openingStatus}, #{openedStatus})
          AND version = #{version}
    </delete>

    <select id="selectPlanPlateList"
            resultType="com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanPlateRespVO">
        SELECT 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.source_veneer_no AS sourceVeneerNo,
               opv.veneer_name AS veneerName,
               opv.product_level AS productLevel,
               opv.color,
               opv.material
        FROM order_plate_veneer opv
        WHERE opv.deleted = false
          AND opv.plan_id = #{planId}
        ORDER BY opv.order_plate_id DESC, opv.veneer_side
    </select>

    <select id="selectPendingVeneerList"
            resultType="com.cf.imes.module.executor.dal.dataobject.veneer.OrderPlateVeneerDO">
        SELECT opv.id,
               opv.organ_id AS organId,
               opv.order_id AS orderId,
               opv.order_goods_id AS orderGoodsId,
               opv.order_plate_id AS orderPlateId,
               opv.plate_no AS plateNo,
               opv.plate_length AS plateLength,
               opv.plate_width AS plateWidth,
               opv.outer_margin AS outerMargin,
               opv.source_veneer_id AS sourceVeneerId,
               opv.source_veneer_no AS sourceVeneerNo
        FROM order_plate_veneer opv
        WHERE opv.deleted = false
          AND opv.plan_id = 0
          AND opv.id IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        ORDER BY opv.id
    </select>

    <update id="updateVeneerPlanned">
        UPDATE order_plate_veneer
        SET plan_id = #{planId},
            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="selectOrderPlannedVeneerPlateCount"
            resultType="com.cf.imes.module.executor.dal.dataobject.veneer.VeneerPlannedPlateCountDO">
        SELECT opv.order_id AS targetId,
               COUNT(DISTINCT opv.order_plate_id) AS plannedVeneerPlateNum
        FROM order_plate_veneer opv
        WHERE opv.deleted = false
          AND opv.plan_id != 0
          AND opv.order_id IN
        <foreach collection="orderIds" item="orderId" open="(" close=")" separator=",">
            #{orderId}
        </foreach>
          AND NOT EXISTS (
              SELECT 1
              FROM order_plate_veneer pending
              WHERE pending.order_id = opv.order_id
                AND pending.order_plate_id = opv.order_plate_id
                AND pending.deleted = false
                AND pending.plan_id = 0
          )
        GROUP BY opv.order_id
    </select>

    <select id="selectGoodsPlannedVeneerPlateCount"
            resultType="com.cf.imes.module.executor.dal.dataobject.veneer.VeneerPlannedPlateCountDO">
        SELECT opv.order_goods_id AS targetId,
               COUNT(DISTINCT opv.order_plate_id) AS plannedVeneerPlateNum
        FROM order_plate_veneer opv
        WHERE opv.deleted = false
          AND opv.plan_id != 0
          AND opv.order_goods_id IN
        <foreach collection="orderGoodsIds" item="orderGoodsId" open="(" close=")" separator=",">
            #{orderGoodsId}
        </foreach>
          AND NOT EXISTS (
              SELECT 1
              FROM order_plate_veneer pending
              WHERE pending.order_id = opv.order_id
                AND pending.order_goods_id = opv.order_goods_id
                AND pending.order_plate_id = opv.order_plate_id
                AND pending.deleted = false
                AND pending.plan_id = 0
          )
        GROUP BY opv.order_goods_id
    </select>

    <select id="selectPlanAffectedScope"
            resultType="com.cf.imes.module.executor.dal.dataobject.veneer.OrderPlateVeneerDO">
        SELECT DISTINCT opv.order_id AS orderId,
                        opv.order_goods_id AS orderGoodsId
        FROM order_plate_veneer opv
        WHERE opv.deleted = false
          AND opv.plan_id IN
        <foreach collection="planIds" item="planId" open="(" close=")" separator=",">
            #{planId}
        </foreach>
    </select>

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

    <delete id="physicalDeletePlan">
        DELETE FROM order_veneer_plan
        WHERE deleted = false
          AND status NOT IN (#{openingStatus}, #{openedStatus})
          AND id IN
        <foreach collection="planIds" item="planId" open="(" close=")" separator=",">
            #{planId}
        </foreach>
    </delete>

</mapper>
