<?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.PartGoodsRespVO">
        SELECT distinct 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.id as bodyId, c.room_id, c.name as bodyName, c.room_name
        FROM order_parts a
                 LEFT JOIN order_item b ON a.id = b.parts_id and a.organ_id = b.organ_id
                 LEFT JOIN order_body c on b.body_id = c.id and b.organ_id = c.organ_id and c.order_id = b.order_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.is_composite,
               a.remark,
               SUM(b.num) AS num,
               c.name          as body_name,
               c.id            as body_id,
               c.room_id,
               c.room_name

        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}
        GROUP BY
            a.id,
            a.goods_id,
            a.name,
            a.material,
            a.type,
            a.model,
            a.spec,
            a.brand,
            a.factory,
            a.unit,
            a.is_composite,
            a.remark,
            c.id,
            c.name,
            c.room_id,
            c.room_name;
    </select>

    <select id="selectPartAll"
            resultType="com.cf.imes.module.executor.controller.admin.orderParts.vo.PartGoodsRespVO">
        SELECT DISTINCT
--             a.id,
            a.name,
            a.brand,
            a.model,
            a.spec,
            a.unit,
--             a.price,
            a.remark,
            SUM(b.num) AS 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}
        GROUP BY a.name, a.brand, a.model, a.spec, a.unit, a.remark
    </select>


    <update id="updateDeletedById">
        UPDATE order_parts
        SET deleted = #{deleted}
        WHERE
        id IN
        <foreach collection="partsIds" item="partsId" open="(" separator="," close=")">
            #{partsId}
        </foreach>
        AND
        organ_id = #{organId}
         and order_id = #{orderId}
    </update>


    <update id="updateDeletedParts">
        UPDATE order_parts
        SET deleted = true
        WHERE
        organ_id = #{organId}
        and order_id = #{orderId}
        and  id IN
        <foreach collection="partsIds" item="partsId" open="(" separator="," close=")">
            #{partsId}
        </foreach>
    </update>


    <update id="updateNoDeletedParts">
        UPDATE order_parts
        SET deleted = false
        WHERE
        organ_id = #{organId}
        and order_id = #{orderId}
        and  id IN
        <foreach collection="partsIds" item="partsId" open="(" separator="," close=")">
            #{partsId}
        </foreach>
    </update>





    <delete id="deletedById">
        DELETE FROM order_parts
        WHERE id IN
        <foreach collection="partsIds" item="partsId" open="(" separator="," close=")">
            #{partsId}
        </foreach>
        AND organ_id = #{organId}
    </delete>

    <resultMap id="OrderPartsMap" type="com.cf.imes.module.executor.controller.admin.plan.saveOptimize.PrintOrderPartsRespVO">

        <result property="partsNum" column="partsNum"/>
        <result property="bodyId" column="bodyId"/>
        <result property="roomId" column="roomId"/>
        <result property="bodyName" column="bodyName"/>
        <result property="roomName" column="roomName"/>
        <result property="id" column="id"/>
        <result property="spec" column="spec"/>
        <result property="brand" column="brand"/>
        <result property="factory" column="factory"/>
        <result property="model" column="model"/>
        <result property="name" column="name"/>
        <result property="orderId" column="orderId"/>
        <result property="type" column="type"/>
        <result property="partsColor" column="partsColor"/>
        <result property="partsWidth" column="partsWidth"/>
        <result property="partsLength" column="partsLength"/>
        <result property="partsThickness" column="partsThickness"/>
        <result property="category" column="category"/>
        <result property="subparts" column="subparts"/>

        <collection property="groupId" ofType="java.lang.Long" column="groupId"/>

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

        select distinct oi.group_id as groupId,
                        oi.num as partsNum,
                        oi.body_id as bodyId,
                        oi.room_id as roomId,
                        ob.name as bodyName,
                        ob.room_name as roomName,
                        op.id as id,
                        op.spec as spec,
                        op.brand as brand,
                        op.factory as factory,
                        op.model as model ,
                        op.name as name,
                        op.order_id as orderId,
                        op.type as type,
                        op.color as partsColor,
                        op.width as partsWidth,
                        op.length as partsLength,
                        op.thickness as partsThickness,
                        op.category as category,
                        op.subparts as subparts
        from order_parts op
             join order_item oi on op.organ_id = oi.organ_id and op.order_id  = oi.order_id and op.id = oi.parts_id
             join order_body ob on oi.organ_id = ob.organ_id and oi.order_id = ob.order_id and oi.body_id = ob.id

        where op.organ_id = #{organId}
            and op.deleted = false
            and op.order_id  in
        <foreach item="orderIds" collection="orderIds" open="(" separator="," close=")">
            #{orderIds}
        </foreach>


    </select>


    <select id="selectPartsIdListByBodyId" resultType="java.lang.Long">
        SELECT parts_id FROM order_item_temp
        WHERE body_id IN
        <foreach item="bodyId" collection="bodyIds" open="(" separator="," close=")">
            #{bodyId}
        </foreach>
        AND organ_id = #{organId} and order_id = #{orderId}
    </select>


    <select id="countPartsByOrderId" resultType="java.lang.Integer">
        SELECT count(*) FROM order_plate
        WHERE order_id = #{orderId} AND organ_id = #{organId} AND deleted = #{deleted}
    </select>



    <select id="selectPlanPartsDataSource" resultType="java.util.Map">


        select distinct ${filed} from order_parts where
                                      organ_id = #{organId}

                                    and deleted = false

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


    </select>

    <select id="selectOrderPartsListByBody" resultType="com.cf.imes.module.executor.controller.admin.orderParts.vo.OrderPartsPrintRespVO">
        SELECT DISTINCT a.id,
                        a.goods_id,
                        a.name,
                        a.material,
                        a.category,
                        a.model,
                        a.spec,
                        a.brand,
                        a.factory,
                        a.unit,
                        a.remark,
                        a.price,
                        a.price * b.num as amount,
                        b.num

        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}
          and b.room_id = #{roomId}
          and b.body_id = #{bodyId}
    </select>

    <resultMap id="partsMap" type="com.cf.imes.module.executor.controller.admin.manage.query.vo.pack.PackageDetailsRespVO">
        <result property="packageNo" column="packageId"/>
        <result property="orderId" column="orderId"/>
        <result property="roomName" column="roomName"/>
        <result property="cabinetName" column="bodyName"/>
        <result property="roomId" column="roomId"/>
        <result property="bodyId" column="bodyId"/>
        <result property="processGroup" column="processGroup"/>
        <result property="partsId" column="partsId"/>
        <result property="type" column="type"/>
        <result property="isComposite" column="isComposite"/>
        <result property="partsName" column="name"/>
        <result property="model" column="model"/>
        <result property="spec" column="spec"/>
        <result property="unit" column="unit"/>
        <result property="brand" column="brand"/>
        <result property="factory" column="factory"/>
        <result property="remark" column="remark"/>
        <result property="partsColor" column="partsColor"/>
        <result property="partsWidth" column="partsWidth"/>
        <result property="partsLength" column="partsLength"/>
        <result property="partsThickness" column="partsThickness"/>
        <result property="category" column="category"/>
        <result property="subparts" column="subparts"/>
        <result property="componentName" column="componentName"/>
        <result property="packNum" column="packNum"/>

        <result property="multiNum" column="multiNum"/>
        <result property="multiSort" column="multiSort"/>
    </resultMap>

    <select id="selectPackedParts"
            parameterType="java.lang.Long"
            resultMap="partsMap">
        SELECT
        ob.room_name        AS roomName,
        ob.name             AS bodyName,
        ob.room_id          AS roomId,
        ob.id               AS bodyId,

        oi.pack_num         AS packNum,
        oi.package_id       AS packageId,

        ob.multi_num        AS multiNum,
        ob.multi_sort       AS multiSort,

        op.id               AS partsId,
        op.order_id         AS orderId,

        op.name,
        op.model,
        op.spec,
        op.unit,
        op.brand,
        op.factory,
        op.remark,
        op.color            AS partsColor,
        op.width            AS partsWidth,
        op.length           AS partsLength,
        op.thickness        AS partsThickness,
        op.category,
        op.subparts,
        op.type

        FROM order_item oi

        JOIN order_parts op
        ON oi.parts_id = op.id
        AND oi.organ_id = op.organ_id
        AND oi.order_id = op.order_id

        LEFT JOIN order_body ob
        ON oi.organ_id = ob.organ_id
        AND oi.order_id = ob.order_id
        AND oi.body_id = ob.id

        WHERE
        oi.package_id = #{packageNo}
        AND oi.parts_id != 0

        AND op.deleted = false
        AND op.type != '封边条'

        AND oi.order_id IN
        <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        order by partsId
    </select>

    <select id="selectPackedPartsGroupAndComponent"
            parameterType="java.lang.Long"
            resultMap="partsMap">
        SELECT
        oi.parts_id AS partsId,
        oi.room_id AS roomId,
        oi.body_id AS bodyId,

        oi.group_id,
        oi.comp_id,

        og.name AS processGroup,
        oc.name AS componentName

        FROM order_item oi

        LEFT JOIN order_group og
        ON oi.organ_id = og.organ_id
        AND oi.order_id = og.order_id
        AND oi.group_id = og.id

        LEFT JOIN order_component oc
        ON oi.organ_id = oc.organ_id
        AND oi.order_id = oc.order_id
        AND oi.comp_id = oc.id

        WHERE ((
        oi.group_id IS NOT NULL
        AND oi.group_id != 0
        )
        OR oi.comp_id IS NOT NULL)

        AND oi.order_id IN
        <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        AND oi.parts_id IN
        <foreach item="partsId" collection="partsIds" open="(" separator="," close=")">
            #{partsId}
        </foreach>
    </select>

    <resultMap id="PartsListMap" type="com.cf.imes.module.executor.controller.admin.manage.pack.vo.OrderPartsRespVO">
        <result property="packageId" column="packageId"/>
        <result property="orderId" column="orderId"/>
        <result property="roomName" column="roomName"/>
        <result property="cabinetName" column="bodyName"/>
        <result property="roomId" column="roomId"/>
        <result property="bodyId" column="bodyId"/>
        <result property="partsId" column="partsId"/>
        <result property="type" column="type"/>
        <result property="isComposite" column="isComposite"/>
        <result property="name" column="name"/>
        <result property="model" column="model"/>
        <result property="spec" column="spec"/>
        <result property="unit" column="unit"/>
        <result property="brand" column="brand"/>
        <result property="factory" column="factory"/>
        <result property="remark" column="remark"/>
        <result property="partsColor" column="partsColor"/>
        <result property="partsWidth" column="partsWidth"/>
        <result property="partsLength" column="partsLength"/>
        <result property="partsThickness" column="partsThickness"/>
        <result property="category" column="category"/>
        <result property="subparts" column="subparts"/>
        <result property="componentName" column="componentName"/>
        <result property="processGroup" column="processGroup"/>

        <result property="heapSplitStatus" column="heapSplitStatus"/>
        <result property="heapNum" column="heapNum"/>

        <result property="multiNum" column="multiNum"/>
        <result property="multiSort" column="multiSort"/>

    </resultMap>
    <select id="selectOrdersPartsList"
            parameterType="java.lang.Long"
            resultMap="PartsListMap">
        SELECT
        ob.room_name        AS roomName,
        ob.name             AS bodyName,
        ob.room_id          AS roomId,
        ob.id               AS bodyId,
        og.name             AS processGroup,
        oi.group_id          AS groupId,
        oc.name             AS componentName,
        (oi.num - oi.pack_num) AS num,
        ob.multi_num        AS multiNum,
        ob.multi_sort       AS multiSort,
        op.id               AS partsId,
        op.order_id         AS orderId,
        op.name,
        op.model,
        op.spec,
        op.unit,
        op.brand,
        op.factory,
        op.remark,
        op.color            AS partsColor,
        op.width            AS partsWidth,
        op.length           AS partsLength,
        op.thickness        AS partsThickness,
        op.category,
        op.subparts,
        op.type
        FROM order_item oi

        JOIN order_parts op
        ON oi.parts_id = op.id

        LEFT JOIN order_group og
        ON oi.organ_id = og.organ_id
        AND oi.order_id = og.order_id
        AND oi.group_id = og.id

        LEFT JOIN order_body ob
        ON oi.organ_id = ob.organ_id
        AND oi.order_id = ob.order_id
        AND oi.body_id = ob.id

        LEFT JOIN order_component oc
        ON oi.organ_id = oc.organ_id
        AND oi.order_id = oc.order_id
        AND oi.comp_id = oc.id

        WHERE
        oi.parts_id != 0
        AND oi.package_id = 0
        AND oi.source_id = 0
        AND op.deleted = false
        AND op.type != '封边条'
        AND oi.order_id IN
        <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        order by partsId
    </select>

    <select id="selectOrdersHeapSplitPartsList"
            parameterType="java.lang.Long"
            resultMap="PartsListMap">
        SELECT
        ob.room_name        AS roomName,
        ob.name             AS bodyName,
        ob.room_id          AS roomId,
        ob.id               AS bodyId,

        oi.heap_split_status AS heapSplitStatus,
        oi.group_id          AS groupId,

        og.name             AS processGroup,
        oc.name             AS componentName,

        ob.multi_num        AS multiNum,
        ob.multi_sort       AS multiSort,

        CASE
            WHEN oi.heap_split_status = 1 THEN oi.heap_num
            ELSE (oi.num - oi.heap_num)
        END AS num,
        oi.heap_num         AS heapNum,

        op.id               AS partsId,
        op.order_id         AS orderId,

        op.name,
        op.model,
        op.spec,
        op.unit,
        op.brand,
        op.factory,
        op.remark,
        op.color            AS partsColor,
        op.width            AS partsWidth,
        op.length           AS partsLength,
        op.thickness        AS partsThickness,
        op.category,
        op.subparts,
        op.type

        FROM order_item oi

        JOIN order_parts op
        ON oi.parts_id = op.id
        AND oi.organ_id = op.organ_id
        AND oi.order_id = op.order_id

        LEFT JOIN order_group og
        ON oi.organ_id = og.organ_id
        AND oi.order_id = og.order_id
        AND oi.group_id = og.id

        LEFT JOIN order_body ob
        ON oi.organ_id = ob.organ_id
        AND oi.order_id = ob.order_id
        AND oi.body_id = ob.id

        LEFT JOIN order_component oc
        ON oi.organ_id = oc.organ_id
        AND oi.order_id = oc.order_id
        AND oi.comp_id = oc.id

        WHERE
        oi.parts_id != 0

        AND (
        oi.source_id = 0
        OR (oi.source_id != 0 AND oi.heap_split_status = 1)
        )

        AND op.deleted = false
        AND op.type != '封边条'

        AND oi.order_id IN
        <foreach item="orderId" collection="orderIds" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        order by partsId
    </select>

    <resultMap id="LabelPartsMap" type="com.cf.imes.module.executor.controller.admin.order.vo.order.LabelOrderPartsData">

        <result property="partsId" column="partsId"/>
        <result property="orderNo" column="orderNo"/>
        <result property="partsName" column="partsName"/>
        <result property="model" column="model"/>
        <result property="factory" column="factory"/>
        <result property="brand" column="brand"/>
        <result property="spec" column="spec"/>
        <result property="partsUnit" column="partsUnit"/>
        <result property="partsType" column="partsType"/>

        <collection property="num" ofType="java.math.BigDecimal" column="num"/>
        <collection property="roomId" ofType="java.lang.Long" column="roomId"/>
        <collection property="bodyId" ofType="java.lang.Long" column="bodyId"/>
        <collection property="groupId" ofType="java.lang.Long" column="groupId"/>

    </resultMap>

    <select id="selectLabelOrderPartsData"
            parameterType="java.lang.Long"
            resultMap="LabelPartsMap">


        select distinct
            oi.num as num,
            oi.room_id as roomId,
            oi.body_id as bodyId,
            oi.group_id as groupId,
            op.id as partsId,
            op.order_id as orderNo,
            op.name as partsName,
            op.model as model,
            op.factory as factory,
            op.brand as brand,
            op.spec as spec,
            op.unit as partsUnit,
            op.type as partsType

        from order_parts op
                 join order_item oi on op.organ_id = oi.organ_id and op.order_id  = oi.order_id and op.id = oi.parts_id

        where op.organ_id = #{organId}
          and op.deleted = false
          and op.order_id in
        <foreach item="orderIds" collection="orderIds" open="(" separator="," close=")">
            #{orderIds}
        </foreach>

    </select>


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


        update order_parts op

        set op.deleted = true
        where
        op.organ_id = #{organId}
        and op.order_id = #{orderId}
        and op.deleted = false
        and op.id in
        <foreach item="id" collection="partsIds" open="(" separator="," close=")">
            #{id}
        </foreach>


    </update>


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

        UPDATE order_parts op

        SET op.deleted = true

        WHERE op.organ_id = #{organId}
          and op.order_id = #{orderId}
          and op.id = #{partsId};

    </update>

    <delete id="physicalDeleteByOrderIds">
        delete from order_parts
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>