<?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.orderParts.OrderPartsMapper">

    <select id="selectProductList"
            resultType="com.cf.imes.module.executor.controller.admin.orderParts.vo.OrderPartsRespVO">
        SELECT a.id, a.goods_id, a.name, a.material, a.type, a.model, a.spec, a.brand, a.factory,
               a.unit, a.price, a.is_composite, a.remark, b.num
        FROM order_parts a
                 LEFT JOIN order_item b ON a.id = b.parts_id and a.organ_id = b.organ_id
        WHERE b.organ_id =#{organId} and  b.order_id = #{orderId}
          AND (#{roomId} IS NULL OR b.room_id = #{roomId})
          AND (#{bodyId} IS NULL OR b.body_id = #{bodyId})

    </select>

    <select id="selectPartDetail"
            resultType="com.cf.imes.module.executor.controller.admin.orderParts.vo.PartGoodsRespVO">
        SELECT a.id,
               a.goods_id,
               a.name,
               a.material,
               a.type,
               a.model,
               a.spec,
               a.brand,
               a.factory,
               a.unit,
               a.price,
               a.is_composite,
               a.remark,
               b.num,
               c.name          as body_name,
               c.id            as body_id,
               c.room_id,
               c.room_name,
               b.num * a.price AS total_price -- 添加计算总金额的列
        FROM order_parts a
                 LEFT JOIN order_item b ON a.id = b.parts_id
                 LEFT JOIN order_body c on c.id = b.body_id
        WHERE b.order_id = #{orderId}
          and b.organ_id = #{organId}
          and a.deleted = #{deleted}
    </select>

    <select id="selectPartAll"
            resultType="com.cf.imes.module.executor.controller.admin.orderParts.vo.PartGoodsRespVO">
        SELECT
            a.name,
            a.brand,
            a.model,
            a.spec,
            a.unit,
            a.price,
            a.remark,
            b.num
        FROM
            order_parts a
                LEFT JOIN
            order_item b ON a.id = b.parts_id
        WHERE
            b.order_id = #{orderId}
          AND b.organ_id = #{organId}
          AND a.deleted = #{deleted}
    </select>


    <update id="updateDeletedById">
        UPDATE order_parts
        SET deleted = #{deleted}
        WHERE id IN (
            SELECT parts_id FROM order_item
            WHERE body_id = #{bodyId} AND organ_id = #{organId}
        )
    </update>


    <select id="selectPartList"
            resultType="com.cf.imes.module.executor.controller.admin.plan.saveOptimize.PrintOrderPartsRespVO">

        select distinct oi.num as partsNum,
                        ob.name as bodyName,
                        ob.room_name as roomName,
                        op.id,
                        op.spec,
                        op.brand,
                        op.factory,
                        op.model,
                        op.name,
                        op.order_id
        from order_parts op
                 join order_item oi on op.id = oi.parts_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 op.organ_id = #{organId}
          and  op.order_id  in

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


    </select>








</mapper>