<?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
        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="orderId" column="orderId"/>
        <result property="partsId" column="partsId"/>
        <result property="name" column="name"/>
        <result property="type" column="type"/>
        <result property="remark" column="remark"/>
        <result property="isComposite" column="isComposite"/>
        <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"/>

        <collection property="count" ofType="java.math.BigDecimal" column="count"/>
        <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="selectParts"
            parameterType="java.lang.Long"
            resultMap="partsMap">


        select distinct
            oi.num as `count`,
            op.order_id as orderId,
            op.id as partsId,
            op.name as name,
            op.model as model,
            op.spec as spec,
            op.unit as unit,
            op.brand as brand,
            op.material as material,
            op.factory as factory,
            op.type as type,
            op.remark as remark,
            op.is_composite as isComposite,
            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
                 left 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 oi.package_id = #{packageId}


    </select>





    <resultMap id="PartsListMap" type="com.cf.imes.module.executor.controller.admin.manage.pack.vo.OrderPartsRespVO">

        <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"/>

        <collection property="count" ofType="java.math.BigDecimal" column="count"/>
        <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="selectPartsList"
            parameterType="java.lang.Long"
            resultMap="PartsListMap">


        select distinct
            oi.num as `count`,
            oi.room_id as roomId,
            oi.body_id as bodyId,
            oi.group_id as groupId,
            op.id as partsId,
            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 as category,
            op.subparts as subparts,
            op.type

        from order_parts op
                 left 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.order_id = #{orderId} and op.deleted = false and op.type !=  '封边条' and oi.package_id = 0

    </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>
</mapper>