<?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">
    <select id="selectPackPage"
            parameterType="java.util.List"
            resultType="com.cf.imes.module.executor.controller.admin.manage.pack.vo.PackRespVO">
        select
        order_id as id,
        COUNT(*) AS packCount
        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>
        GROUP BY order_id
    </select>



    <select id="selectPackPieceCount"
            parameterType="java.util.List"
            resultType="com.cf.imes.module.executor.controller.admin.manage.pack.vo.PackRespVO">

        select
        op.order_id as id,
        COUNT(DISTINCT oi.plate_id) AS packPieceCount
        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.package_no = 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.status in (1,2,3)
        and oi.package_id != 0
        and oi.plate_id != 0
        GROUP BY op.order_id;
    </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="getPackageNoInOrderIds" resultType="com.cf.imes.module.executor.controller.admin.manage.query.vo.pack.OrderPackageVO">
        SELECT package_no, order_id, status
        FROM order_package
        WHERE deleted = false
        AND order_id IN
        <foreach collection="orderIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
</mapper>