<?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.pack.OrderPackageMapper">


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

        UPDATE order_package

        SET deleted = true

        WHERE organ_id = #{organId}
        and order_id = #{orderId}
        and id in
        <foreach item="packIds" collection="packIds" open="(" separator="," close=")">
            #{packIds}
        </foreach>


    </update>

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

        <result property="id" column="orderId"/>
        <collection property="plateIdList" ofType="java.lang.Long" column="plateId"/>

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

    </resultMap>


    <select id="selectPackPage" parameterType="java.util.List" resultMap="PackMap">



        select
        order_id as orderId,
        id as packId

        from order_package

        where
        organ_id = #{organId}

        and status !=0

        and deleted = false

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

    </select>



    <select id="selectPackPieceCount"
            parameterType="java.util.List"
            resultMap="PackMap">

        select distinct
        op.order_id as orderId,
        oi.plate_id  as plateId

        from order_package op
        left join order_item oi on op.organ_id = oi.organ_id and op.order_id = oi.order_id and op.id = oi.package_id

        where

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

        and op.type = 0
        and op.status in (1,2,3)
        and oi.package_id != 0
        and oi.plate_id != 0;



    </select>




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

        UPDATE order_package
        SET deleted = true
        WHERE organ_id = #{organId} and order_id = #{orderId} and id = #{packageId}

    </update>



    <select id="selectByOrderNo"
            resultType="com.cf.imes.module.executor.controller.admin.manage.query.vo.pack.OrderPackageVO">

        select distinct
            op.id,
            op.order_id,
            op.package_no,
            op.type,
            op.status,
            op.organ_id,
            op.creator,
            op.remark as packRemark,
            op.updater,
            op.update_time,
            op.create_time,
            o.custom_order_no,
            o.dealer,
            o.customer,
            o.address,
            o.remark as orderRemark

        from orders o
                 left join order_package op on  o.organ_id = op.organ_id and o.id = op.order_id

            ${ew.customSqlSegment}

    </select>


    <select id="selectPackListByOrderId"
            resultType="com.cf.imes.module.executor.controller.admin.manage.query.vo.pack.OrderPackageVO">


        select distinct
            id,
            order_id,
            package_no,
            type,
            status,
            organ_id,
            creator,
            remark as packRemark,
            updater,
            update_time,
            create_time

        from order_package

        where organ_id = #{organId} and deleted = false  and order_id =  #{orderId}

    </select>





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

        <result property="orderNo" column="orderNo"/>

        <collection property="packageInfos" ofType="com.cf.imes.module.executor.controller.admin.order.vo.order.LabelOrderPackData$PackageInfo">

            <result property="packageNo" column="packageNo"/>
            <result property="packageId" column="packageId"/>

        </collection>

    </resultMap>



    <select id="selectLabelOrderPackData"
            parameterType="java.lang.Long"
            resultMap="OrderPackDataMap">


        select distinct
            package_no as packageNo,
            id as packageId,
            order_id as orderNo

        from order_package

        where organ_id = #{organId}
          and deleted = false
          and id in
        <foreach item="packIds" collection="packIds" open="(" separator="," close=")">
            #{packIds}
        </foreach>
        and status = 1


    </select>




    <select id="selectPartPack"
            parameterType="java.lang.Long"
            resultMap="OrderPackDataMap">


        select distinct
            package_no as packageNo,
            id as packageId,
            order_id as orderNo

        from order_package

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


    </select>

</mapper>