<?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.manage.dal.mysql.query.ProductionStatusMapper">

<resultMap id="printOrderMap" type="com.cf.imes.module.manage.controller.admin.query.vo.pack.PrintOrderRespVO">

    <result property="orderNo" column="orderNo"/>
    <result property="customOrderNo" column="customOrderNo"/>
    <result property="dealer" column="dealer"/>
    <result property="address" column="address"/>
    <result property="customer" column="customer"/>
    <result property="customerPhone" column="customerPhone"/>
    <result property="packageQuantity" column="packageQuantity"/>

</resultMap>
    <select id="selectPrintOrder"
            parameterType="java.lang.Long"
            resultMap="printOrderMap">

        select
            o.id as orderNo ,
            o.custom_order_no as customOrderNo,
            o.dealer as dealer,
            o.address as address,
            o.customer as customer,
            o.phone_number as customerPhone,
            count(opn.order_id) as packageQuantity

        from `orders` o
            join order_package opn on o.id = opn.order_id and o.organ_id = opn.organ_id

        where o.id = #{orderNo} and opn.status &lt;&gt; 0 and o.organ_id = #{organId} ;

    </select>



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

        <result property="id" column="orderNo"/>
        <result property="customOrderNo" column="customOrderNo"/>
        <result property="dealer" column="dealer"/>
        <result property="deliveryDate" column="deliveryDate"/>
        <result property="status" column="status"/>
        <result property="customer" column="customer"/>
        <result property="address" column="address"/>
        <result property="phoneNumber" column="phoneNumber"/>
        <result property="salesman" column="salesman"/>
        <result property="splitter" column="splitter"/>
        <result property="createTime" column="createTime"/>
        <result property="packId" column="packId"/>

        <result property="packCount" column="packCount"/>
        <result property="totalPieceCount" column="totalPieceCount"/>
        <result property="packPieceCount" column="packPieceCount"/>

    </resultMap>

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


        SELECT distinct
        o.id as orderNo,
        o.custom_order_no as customOrderNo,
        o.dealer as dealer,
        o.create_time as createTime,
        o.delivery_date as deliveryDate,
        o.customer as customer,
        o.phone_number as phoneNumber,
        o.address as address,
        o.salesman as salesman,
        o.splitter as splitter,
        o.status as status


        FROM `orders` o
        join order_item oi on o.id = oi.order_id and o.organ_id = oi.organ_id

        WHERE
            o.organ_id = #{organId} and
            o.id in
                <foreach item="id" collection="collect" open="(" separator="," close=")">
                    #{id}
                </foreach>



    </select>


<!--    &#45;&#45;         count(distinct-->
<!--    &#45;&#45;         case-->
<!--    &#45;&#45;         when oi.package_id!=0 then oi.package_id end-->
<!--    &#45;&#45;         ) as packCount,-->
<!--    &#45;&#45;         count(distinct case-->
<!--    &#45;&#45;         when oi.package_id !=0 and oi.parts_id = 0 then oi.plate_id end )as packPieceCount,-->
<!--    &#45;&#45;         count(distinct-->
<!--    &#45;&#45;         case-->
<!--    &#45;&#45;         when oi.plate_id !=0   then oi.plate_id end-->
<!--    &#45;&#45;         )as totalPieceCount-->

<!--    &#45;&#45;         group by orderNo,customOrderNo,dealer,createTime,deliveryDate,customer,phoneNumber,address,salesman,splitter,status;-->



    <resultMap id="PrintMap" type="com.cf.imes.module.manage.controller.admin.pack.vo.PrintOrderPackRespVO">
        <result property="customOrderNo" column="customOrderNo"/>
        <result property="dealer" column="dealer"/>
        <result property="orderNo" column="orderNo"/>
        <result property="packNo" column="packNo"/>
        <result property="packNum" column="packNum"/>
        <result property="address" column="address"/>
    </resultMap>

    <select id="selectOrderPack"
            parameterType="java.lang.Long"
            resultMap="PrintMap">

        select o.custom_order_no as customOrderNo,
        o.dealer as dealer,
        opn.id as packNo ,
        o.id as orderNo,
        o.address as address,
        ((SELECT COUNT(t2.id) FROM order_package t2 WHERE t2.create_time &lt;= opn.create_time and t2.order_id =#{orderNo})) as packNum

        from `orders` o
        join order_package opn on o.id = opn.order_id and o.organ_id = opn.organ_id

        where opn.order_id =#{orderNo}  and opn.id = #{packageNo} and opn.status &lt;&gt; 0 and opn.organ_id = #{organId}
        order by  opn.create_time ;

    </select>


</mapper>