<?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.order.OrderMapper">

    <resultMap id="OrderPageMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.OrderRespVOCopy">


        <result property="orderId" column="orderId"/>
        <result property="status" column="status"/>
        <result property="defineId" column="defineId"/>
        <result property="customer" column="customer"/>
        <result property="address" column="address"/>
        <result property="deliveryDate" column="deliveryDate"/>
        <result property="orderDate" column="orderDate"/>
        <result property="num" column="num"/>
        <result property="area" column="area"/>


<!--        <collection property="platePages" ofType="com.cf.imes.module.executor.controller.admin.plan.vo.PlatePage" resultMap="PlatePageMap"/>-->


    </resultMap>

    <resultMap id="PlatePageMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.PlatePage">


        <result property="plateId" column="plateId"/>
        <result property="area" column="area"/>


    </resultMap>



    <select id="selectOrderPage" resultMap="OrderPageMap">

        select distinct
            o.id as orderId,
            o.order_date as orderDate,
            o.delivery_date as deliveryDate,
            o.customer as customer,
            o.address as address,
            o.custom_order_no as defineId,
            o.status as status

        from orders o
                 join order_goods og on o.organ_id = og.organ_id  and o.id = og.order_id and og.plan_id = 0  and o.deleted = og.deleted
                 join order_plate op on o.organ_id = op.organ_id  and o.deleted = op.deleted and o.id = op.order_id and op.is_optimized = false and og.id = op.goods_id

        ${ew.customSqlSegment}


    </select>


<!--    select distinct
    o.id as orderId,
    o.order_date as orderDate,
    o.delivery_date as deliveryDate,
    o.customer as customer,
    o.address as address,
    o.custom_order_no as defineId,
    o.status as status,
    count( op.id) as num ,
    sum(op.area) as area

    from orders o

    left join order_goods og on o.organ_id = og.organ_id   and o.id = og.order_id

    left join order_plate op on  og.organ_id = op.organ_id and og.id = op.goods_id and og.deleted = op.deleted-->

    <select id="selectDynamicSqlString" resultType="java.util.Map">
        ${sqlStr}
    </select>


    <select id="selectOrderListByIds"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.OrderRespVOCopy">


        select distinct
        id as orderId,
        order_date as orderDate,
        delivery_date as deliveryDate,
        customer as customer,
        address as address,
        custom_order_no as defineId,
        status as status,
        create_time as createTime

        from orders

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

        order by orderId desc

    </select>

    <select id="selectOrderNear" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderOverdueRespVO">
        select
            o.id,
            o.api_order_id,
            o.customer,
            o.parent_no,
            o.organ_id,
            o.status,
            o.order_date,
            o.order_sort,
            o.delivery_date,
            o.order_type,
            o.custom_order_no,
            o.address,
            o.data_type,
            o.phone_number,
            o.dealer,
            o.dealer_phone_number,
            o.salesman,
            o.splitter,
            o.remark
        from orders o
        where o.organ_id = #{organId}
          and o.deleted = 0
          and o.status != 4
          and o.delivery_date = #{orderDate}
    </select>

    <select id="selectOrderOverdue" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderOverdueRespVO">
        select
            o.id,
            o.api_order_id,
            o.customer,
            o.parent_no,
            o.organ_id,
            o.status,
            o.order_date,
            o.delivery_date,
            o.order_type,
            o.order_sort,
            o.custom_order_no,
            o.data_type,
            o.address,
            o.phone_number,
            o.dealer,
            o.dealer_phone_number,
            o.salesman,
            o.splitter,
            o.remark,
            LPAD(FLOOR(TIMESTAMPDIFF(DAY, o.delivery_date, #{today})), 2, '0') AS beOverdue
        from orders o
        where o.organ_id = #{organId}
          and o.deleted = 0
          and o.status != 4
          and #{today} >= DATE(o.delivery_date)
    </select>

    <select id="selectOrderCountProducePeriod" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderCountProducePeriodRespVO">
        SELECT
        DATE(o.order_date) AS order_date,
        COUNT(o.id) AS count
        FROM
        orders o
        WHERE
        o.deleted = 0
        AND DATE(o.order_date) BETWEEN #{startTime} AND #{endTime}
        <if test="organId != null">
            AND o.organ_id = #{organId}
        </if>
        GROUP BY
        DATE(o.order_date)
        ORDER BY
        order_date;
    </select>


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


        select ${filed}  from orders
                         where   organ_id = #{organId}
                           and deleted = false
                           and id in
        <foreach collection="orderIds" item="orderIds" open="(" close=")" separator=",">
            #{orderIds}
        </foreach>

    </select>


    <resultMap id="OrderGoodsMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.OrderGoodsResp">

        <result property="width" column="width"/>
        <result property="height" column="height"/>
        <result property="thickness" column="thickness"/>
        <result property="goodsName" column="goods_name"/>
        <result property="material" column="material"/>
        <result property="color" column="color"/>
        <result property="texture" column="texture"/>
        <result property="brand" column="brand"/>
        <result property="spec" column="spec"/>
        <result property="remark" column="remark"/>
        <result property="goodsId" column="goodsId"/>

        <collection property="orderIds" ofType="com.cf.imes.module.executor.controller.admin.plan.bo.OrderIds" resultMap="OrderIdMap"/>

    </resultMap>


    <resultMap id="OrderIdMap" type="com.cf.imes.module.executor.controller.admin.plan.bo.OrderIds">

        <result property="orderId" column="orderId"/>
        <result property="id" column="id"/>
        <result property="goodsId" column="goodsId"/>

    </resultMap>


    <select id="selectGoodsListByGoodsId" resultMap="OrderGoodsMap">


        select distinct
            og.goods_id as goodsId,
            og.order_id as orderId,
            og.id as id
        from orders o

                 join order_goods og on o.organ_id = og.organ_id  and o.id = og.order_id and og.plan_id = 0  and o.deleted = og.deleted

                 join order_plate op  on o.organ_id = op.organ_id and o.deleted = op.deleted and o.id = op.order_id and op.is_optimized = false

            ${ew.customSqlSegment}


    </select>


    <select id="selectPlanOrderList"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.PlanOrderRespVO">


        select distinct
        o.id,
        op.plan_id,
        o.order_date,
        o.delivery_date,
        o.custom_order_no,
        o.customer,
        o.address,
        o.phone_number,
        o.dealer,
        o.dealer_phone_number,
        o.salesman,
        o.splitter,
        o.remark
        from orders o
        join order_plate op on o.organ_id = op.organ_id and o.id = op.order_id
        where op.organ_id = #{organId}
        and op.plan_id  in
        <foreach collection="planIds" item="ids" open="(" close=")" separator=",">
            #{ids}
        </foreach>
    </select>

    <select id="selectFilterOrderPagePlate"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.OrderRespVOCopy">

        select distinct
            o.id as orderId,
            o.order_date as orderDate,
            o.delivery_date as deliveryDate,
            o.customer as customer,
            o.address as address,
            o.custom_order_no as defineId,
            o.status as status

        from orders o
                 join order_goods og on o.organ_id = og.organ_id  and o.id = og.order_id and og.plan_id = 0  and o.deleted = og.deleted
                 join order_plate op on o.organ_id = op.organ_id  and o.deleted = op.deleted and o.id = op.order_id and op.is_optimized = false and og.id = op.goods_id

            ${ew.customSqlSegment}


    </select>






    <select id="selectFilterGoodsPlateList" resultMap="OrderIdMap">


        select distinct

            og.id as id,
            og.order_id as orderId,
            og.goods_id as goodsId

        from orders o

                 join order_goods og on o.organ_id = og.organ_id  and o.id = og.order_id and og.plan_id = 0  and o.deleted = og.deleted

                 join order_plate op  on o.organ_id = op.organ_id and o.deleted = op.deleted and o.id = op.order_id and op.is_optimized = false and og.id = op.goods_id

            ${ew.customSqlSegment}


    </select>



    <resultMap id="GoodsMap" type="com.cf.imes.module.executor.controller.admin.plan.vo.OrderGoodsResp">
        <result property="width" column="width"/>
        <result property="height" column="height"/>
        <result property="thickness" column="thickness"/>
        <result property="goodsName" column="goods_name"/>
        <result property="material" column="material"/>
        <result property="color" column="color"/>
        <result property="texture" column="texture"/>
        <result property="brand" column="brand"/>
        <result property="spec" column="spec"/>
        <result property="remark" column="remark"/>
        <result property="goodsId" column="goodsId"/>
    </resultMap>

    <select id="selectFilterGoodsPlatePage" resultMap="GoodsMap">


        select distinct

            og.goods_id as goodsId,
            og.goods_name,
            og.color,
            og.material,
            og.texture,
            og.thickness,
            og.width,
            og.height,
            og.brand,
            og.remark,
            og.spec

        from orders o

                 join order_goods og on o.organ_id = og.organ_id  and o.id = og.order_id and og.plan_id = 0  and o.deleted = og.deleted

                 join order_plate op  on o.organ_id = op.organ_id and o.deleted = op.deleted and o.id = op.order_id and op.is_optimized = false and og.id = op.goods_id

            ${ew.customSqlSegment}


    </select>

    <select id="selectFilterOrderPlateList" resultType="java.lang.Long">

        select distinct
            o.id as orderId

        from orders o
                 join order_goods og on o.organ_id = og.organ_id  and o.id = og.order_id and og.plan_id = 0  and o.deleted = og.deleted
                 join order_plate op on o.organ_id = op.organ_id  and o.deleted = op.deleted and o.id = op.order_id and op.is_optimized = false and og.id = op.goods_id

            ${ew.customSqlSegment}

    </select>



    <select id="selectNoPlanOrderPlateCount" resultType="java.lang.Long">


        select
            count(op.id)

        from orders o
                 left join order_plate op on o.id = op.order_id and op.organ_id = #{organId} and op.plan_id = 0
                 left join order_goods og on op.order_id = og.order_id and op.goods_id = og.id and og.organ_id = #{organId}

            ${ew.customSqlSegment}


    </select>



    <select id="selectNoPlanOrderPlateList"
            resultType="com.cf.imes.module.executor.controller.admin.plan.vo.PlateResList">


        select distinct
            o.id as orderId,
            op.id as plateId,
            op.plate_no as plateNo,
            op.goods_id as id,
            op.custom_plate_no as customPlateNo,
            og.material as material,
            og.color as color,
            og.goods_id as goodsId,
            og.brand as brand,
            op.name as plateName,
            op.height as height,
            op.width as width,
            op.thickness as thickness,
            op.texture as texture,
            op.area as area,
            op.seal_down as sealDown,
            op.seal_up as sealUp,
            op.seal_right as sealRight,
            op.seal_left as sealLeft,
            op.is_special_shaped as isSpecialShaped,
            op.is_sculpt as isSculpt,
            op.is_row_hole as isRowHole,
            op.remark as remark

        from orders o
                 left join order_plate op on o.id = op.order_id and op.organ_id = #{organId} and op.plan_id = 0
                 left join order_goods og on op.order_id = og.order_id and op.goods_id = og.id and og.organ_id = #{organId}

            ${ew.customSqlSegment}
        limit #{pageNo},#{pageSize};


    </select>



    <select id="selectOrderIdsTrue" resultMap="PackMap">

            SELECT
                id as orderNo,
                custom_order_no as customOrderNo,
                dealer as dealer,
                dealer_phone_number as dealerPhoneNumber,
                order_date as orderDate,
                create_time as createTime,
                delivery_date as deliveryDate,
                customer as customer,
                phone_number as phoneNumber,
                address as address,
                salesman as salesman,
                splitter as splitter,
                packaged as packaged
            FROM orders
            WHERE  organ_id = #{organId} and deleted = false and status in (1,2,3,4) and create_time &gt;= #{createTime}  and id != #{orderId}
            ORDER BY create_time
                LIMIT 1 ;


    </select>





    <select id="selectOrderIdsFalse" resultMap="PackMap">


        SELECT
            id as orderNo,
            custom_order_no as customOrderNo,
            dealer as dealer,
            dealer_phone_number as dealerPhoneNumber,
            order_date as orderDate,
            create_time as createTime,
            delivery_date as deliveryDate,
            customer as customer,
            phone_number as phoneNumber,
            address as address,
            salesman as salesman,
            splitter as splitter,
            packaged as packaged
        FROM orders
        WHERE   organ_id = #{organId} and deleted = false and status != 0 and create_time &lt;= #{createTime} and id != #{orderId}
        ORDER BY create_time desc
            LIMIT 1 ;


    </select>




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

        <result property="id" column="orderNo"/>
        <result property="customOrderNo" column="customOrderNo"/>
        <result property="dealer" column="dealer"/>
        <result property="dealerPhoneNumber" column="dealerPhoneNumber"/>
        <result property="orderDate" column="orderDate"/>
        <result property="deliveryDate" column="deliveryDate"/>
        <result property="packaged" column="packaged"/>
        <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="remark" column="remark"/>
        <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>



    <resultMap id="PrintMap" type="com.cf.imes.module.executor.controller.admin.manage.pack.vo.PrintOrderPackRespVO">
        <result property="customOrderNo" column="customOrderNo"/>
        <result property="dealer" column="dealer"/>
        <result property="orderId" 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>


    <select id="selectOrderIdList" resultMap="PackMap" parameterType="map">


        select
        id as orderNo,
        custom_order_no as customOrderNo,
        dealer as dealer,
        dealer_phone_number as dealerPhoneNumber,
        order_date as orderDate,
        create_time as createTime,
        delivery_date as deliveryDate,
        customer as customer,
        phone_number as phoneNumber,
        address as address,
        salesman as salesman,
        splitter as splitter,
        packaged as packaged,
        remark as remark

        from orders
        <where>

            organ_id =#{organId}
            and deleted = false
            and status in (1,2,3,4)

            <if test="pageReqVO.orderId !=null">
                and id like concat('%', #{pageReqVO.orderId},'%')
            </if>

            <if test="pageReqVO.status !=null ">
                and packaged = #{pageReqVO.status}
            </if>

            <if test="pageReqVO.customOrderNo !=null">
                and custom_order_no like concat('%',#{pageReqVO.customOrderNo},'%')
            </if>

            <if test="pageReqVO.customer !=null">
                and customer like concat('%', #{pageReqVO.customer},'%')
            </if>

            <if test="pageReqVO.address !=null">
                and address like concat('%', #{pageReqVO.address},'%')
            </if>

            <if test="pageReqVO.dealer !=null">
                and dealer like concat('%', #{pageReqVO.dealer},'%')
            </if>

        </where>

        order by create_time desc

    </select>

    <select id="selectLabelOrderData"
            resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.LabelOrderData">



        select distinct
        id as orderNo,
        custom_order_no as customOrderNo,
        status as status,
        dealer as dealer,
        customer as customer,
        phone_number as phoneNumber,
        address as address,
        salesman as salesman,
        splitter as splitter,
        order_date as orderDate,
        delivery_date as deliveryDate,
        remark as remark

        from orders

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


    </select>

    <select id="selectPlannedPlateNumAndArea" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPlanPlateNumAndAreaRespVO">
        select count(op.id) as num, IFNULL(sum(op.area), 0) as area
        from orders o
                 join order_plate op on o.organ_id = op.organ_id and o.id = op.order_id
        where o.id = #{orderId}
        and op.plan_id != 0
        and op.deleted = false
        and o.deleted = false
    </select>
</mapper>