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

    <!--    查询订单    -->
    <select id="getOrderList" resultType="com.xmomen.module.order.model.OrderModel" parameterType="com.xmomen.module.order.model.OrderQuery">
        SELECT
        torp.REF_VALUE as couponNumber,
        sp.SHOW_VALUE AS orderTypeDesc,
        ssp.SHOW_VALUE AS orderStatusDesc,
        paymentp.SHOW_VALUE AS paymentModeDesc,
        paymentp2.`SHOW_VALUE` as otherPaymentModeDesc,
        osp.SHOW_VALUE AS orderSourceDesc,
        st.EXECUTOR_ID AS packingTaskUserId,
        st.CREATE_TIME as packingTaskCreateTime,
        st.START_TIME as packingTaskStartTime,
        st.FINISH_TIME as packingTaskEndTime,
        u.realname AS packingTaskUsername,
        st.TASK_STATUS as packingTaskStatus,
        stsdp.show_value as packingTaskStatusDesc,
        ce.express_name,
        users.realname as manager_name,
        createUser.realname as create_user,
        pay2.`AMOUNT` AS otherPayAmount,
        receiving_code.ref_value as receivingCode,
        tb.*
        FROM
        tb_order tb

        LEFT JOIN sys_users createUser ON createUser.id = tb.create_user_id

        LEFT JOIN sys_dictionary s on s.DICTIONARY_CODE='ORDER_TYPE'
        left join sys_dictionary_parameter sp on sp.SYS_DICTIONARY_ID=s.ID and tb.ORDER_TYPE = sp.REAL_VALUE

        LEFT JOIN sys_dictionary ss on ss.DICTIONARY_CODE='ORDER_STATUS'
        left join sys_dictionary_parameter ssp on ssp.SYS_DICTIONARY_ID=ss.ID and tb.ORDER_STATUS = ssp.REAL_VALUE

        LEFT JOIN sys_dictionary payment on payment.DICTIONARY_CODE='PAYMENT_MODE'
        left join sys_dictionary_parameter paymentp on paymentp.SYS_DICTIONARY_ID=payment.ID and tb.PAYMENT_MODE = paymentp.REAL_VALUE

        LEFT JOIN sys_dictionary payment2 on payment2.DICTIONARY_CODE='PAYMENT_MODE'
        left join sys_dictionary_parameter paymentp2 on paymentp2.SYS_DICTIONARY_ID=payment2.ID and tb.`OTHER_PAYMENT_MODE` = paymentp2.REAL_VALUE

        LEFT JOIN sys_dictionary oss on oss.DICTIONARY_CODE='ORDER_SOURCE'
        left join sys_dictionary_parameter osp on osp.SYS_DICTIONARY_ID=oss.ID and tb.ORDER_SOURCE = osp.REAL_VALUE

        left join `tb_trade_record` pay2 on pay2.`TRADE_NO` = tb.`ORDER_NO` and pay2.`TRADE_TYPE` = tb.`OTHER_PAYMENT_MODE`

        LEFT JOIN tb_order_relation tor ON tor.ORDER_NO = tb.ORDER_NO AND tor.REF_TYPE = 'ORDER_PACKING_TASK_RELATION'
        LEFT JOIN tb_order_relation torp ON torp.ORDER_NO = tb.ORDER_NO AND torp.REF_TYPE = 'ORDER_PAY_RELATION'
        LEFT JOIN sys_task st on st.ID= tor.REF_VALUE
        LEFT JOIN sys_users u ON u.id = st.EXECUTOR_ID
        LEFT JOIN sys_dictionary stsd on stsd.DICTIONARY_CODE='JOB_STATUS'
        left join sys_dictionary_parameter stsdp on stsdp.SYS_DICTIONARY_ID=stsd.ID and st.TASK_STATUS = stsdp.REAL_VALUE
        left join cd_express ce on tb.despatch_express_id = ce.id

        left join sys_users users on users.id = tb.MANAGER_ID

        left join tb_order_ref receiving_code on receiving_code.order_no = tb.order_no and receiving_code.ref_type = 'SHOU_HUO_NO'
        <where>
            tb.order_status != 0
            <if test="orderStatus">
                AND tb.order_status = #{orderStatus}
            </if>
            <if test="keyword">
                AND (tb.order_no LIKE CONCAT('%', #{keyword}, '%')
                or tb.consignee_phone LIKE CONCAT('%', #{keyword}, '%')
                or tb.consignee_name like CONCAT('%', #{keyword}, '%')
                )
            </if>
            <if test="couponNumber">
                AND torp.REF_VALUE = #{couponNumber}
            </if>
            <if test="id">
                AND tb.id = #{id}
            </if>
            <if test="managerId">
                AND tb.MANAGER_ID = #{managerId}
            </if>
            <if test="consigneeName">
                AND tb.consignee_name LIKE CONCAT('%', #{consigneeName}, '%')
            </if>
            <if test="orderNo">
                AND tb.ORDER_NO = #{orderNo}
            </if>
            <if test="orderNos">
                AND tb.ORDER_NO IN
                <foreach collection="orderNos" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="packingTaskStatus">
                AND st.TASK_STATUS = #{packingTaskStatus}
            </if>
            <if test="packingTaskUserId">
                AND st.EXECUTOR_ID = #{packingTaskUserId}
            </if>
            <if test="isHasPackingTaskUserId">
                AND st.EXECUTOR_ID IS NOT NULL
            </if>
            <if test="hasNoShowCancel">
                AND tb.order_status != 9
            </if>
            <if test="showDespatch == 1">
                AND tb.despatch_express_id is not null
            </if>
            <if test="showDespatch == 0">
                AND tb.despatch_express_id is null
            </if>
            <if test="packingTaskCreateTimeStart">
                <![CDATA[
                  AND DATE_FORMAT(st.CREATE_TIME ,'%Y-%m-%d')>= #{packingTaskCreateTimeStart}
                ]]>
            </if>
            <if test="packingTaskCreateTimeEnd">
                <![CDATA[
                  AND DATE_FORMAT(st.CREATE_TIME ,'%Y-%m-%d')<= #{packingTaskCreateTimeEnd}
                ]]>
            </if>
            <if test="orderCreateTimeStart">
                <![CDATA[
                  AND DATE_FORMAT(tb.CREATE_TIME ,'%Y-%m-%d')>= #{orderCreateTimeStart}
                ]]>
            </if>
            <if test="orderCreateTimeEnd">
                <![CDATA[
                  AND DATE_FORMAT(tb.CREATE_TIME ,'%Y-%m-%d')<= #{orderCreateTimeEnd}
                ]]>
            </if>
            <if test="appointmentTimeStart">
                <![CDATA[
                  AND DATE_FORMAT(tb.APPOINTMENT_TIME ,'%Y-%m-%d') >= #{appointmentTimeStart}
                ]]>
            </if>
            <if test="appointmentTimeEnd">
                <![CDATA[
                  AND DATE_FORMAT(tb.APPOINTMENT_TIME ,'%Y-%m-%d') <= #{appointmentTimeEnd}
                ]]>
            </if>
            <if test="createUserId">
                AND tb.create_user_id = #{createUserId}
            </if>
            <if test="despatchExpressId">
                AND tb.DESPATCH_EXPRESS_ID = #{despatchExpressId}
            </if>
            <if test="isTwoSend">
                AND tb.is_two_send = #{isTwoSend}
            </if>
        </where>
        order by tb.CREATE_TIME desc
    </select>

    <select id="getOrderReportList" resultType="com.xmomen.module.report.model.OrderReport" parameterType="com.xmomen.module.order.model.OrderQuery">
        SELECT
        tb.create_time,
        torp.REF_VALUE as couponNumber,
        cc.`COMPANY_NAME`,
        users.realname as managerName,
        tb.order_no,
        tb.`APPOINTMENT_TIME`,
        tb.`CONSIGNEE_NAME`,
        tb.`CONSIGNEE_PHONE`,
        tb.`CONSIGNEE_ADDRESS`,
        tb.`TOTAL_AMOUNT`,
        paymentp.SHOW_VALUE AS paymentModeDesc,
        paymentp2.`SHOW_VALUE` as otherPaymentModeDesc,
        pay.`AMOUNT` payAmount,
        pay2.`AMOUNT` AS otherPayAmount
        FROM
        tb_order tb
        LEFT JOIN sys_dictionary s on s.DICTIONARY_CODE='ORDER_TYPE'
        left join sys_dictionary_parameter sp on sp.SYS_DICTIONARY_ID=s.ID and tb.ORDER_TYPE = sp.REAL_VALUE

        LEFT JOIN sys_dictionary ss on ss.DICTIONARY_CODE='ORDER_STATUS'
        left join sys_dictionary_parameter ssp on ssp.SYS_DICTIONARY_ID=ss.ID and tb.ORDER_STATUS = ssp.REAL_VALUE

        LEFT JOIN sys_dictionary payment on payment.DICTIONARY_CODE='PAYMENT_MODE'
        left join sys_dictionary_parameter paymentp on paymentp.SYS_DICTIONARY_ID=payment.ID and tb.PAYMENT_MODE = paymentp.REAL_VALUE

        LEFT JOIN sys_dictionary payment2 on payment2.DICTIONARY_CODE='PAYMENT_MODE'
        left join sys_dictionary_parameter paymentp2 on paymentp2.SYS_DICTIONARY_ID=payment2.ID and tb.`OTHER_PAYMENT_MODE` = paymentp2.REAL_VALUE

        left join `tb_trade_record` pay on pay.`TRADE_NO` = tb.`ORDER_NO` and (pay.`TRADE_TYPE` = 'CARD' || pay.`TRADE_TYPE` = 'COUPON')
        left join `tb_trade_record` pay2 on pay2.`TRADE_NO` = tb.`ORDER_NO` and pay2.`TRADE_TYPE` = tb.`OTHER_PAYMENT_MODE`

        LEFT JOIN sys_dictionary oss on oss.DICTIONARY_CODE='ORDER_SOURCE'
        left join sys_dictionary_parameter osp on osp.SYS_DICTIONARY_ID=oss.ID and tb.ORDER_SOURCE = osp.REAL_VALUE

        LEFT JOIN tb_order_relation tor ON tor.ORDER_NO = tb.ORDER_NO AND tor.REF_TYPE = 'ORDER_PACKING_TASK_RELATION'
        LEFT JOIN tb_order_relation torp ON torp.ORDER_NO = tb.ORDER_NO AND torp.REF_TYPE = 'ORDER_PAY_RELATION'
        LEFT JOIN sys_task st on st.ID= tor.REF_VALUE
        LEFT JOIN sys_users u ON u.id = st.EXECUTOR_ID
        LEFT JOIN sys_dictionary stsd on stsd.DICTIONARY_CODE='JOB_STATUS'
        left join sys_dictionary_parameter stsdp on stsdp.SYS_DICTIONARY_ID=stsd.ID and st.TASK_STATUS = stsdp.REAL_VALUE
        left join cd_express ce on tb.despatch_express_id = ce.id
        left join `tb_trade_record` ttr on ttr.`ID` = torp.`REF_VALUE`
        left join sys_users users on users.id = tb.MANAGER_ID
        left join cd_company cc on cc.`ID` = tb.`COMPANY_ID`
        <where>
            tb.order_status != 0
            <if test="orderStatus">
                AND tb.order_status = #{orderStatus}
            </if>
            <if test="keyword">
                AND (tb.order_no LIKE CONCAT('%', #{keyword}, '%')
                or tb.consignee_phone LIKE CONCAT('%', #{keyword}, '%')
                or tb.consignee_name like CONCAT('%', #{keyword}, '%')
                )
            </if>
            <if test="id">
                AND tb.id = #{id}
            </if>
            <if test="managerId">
                AND tb.MANAGER_ID = #{managerId}
            </if>
            <if test="consigneeName">
                AND tb.consignee_name LIKE CONCAT('%', #{consigneeName}, '%')
            </if>
            <if test="orderNo">
                AND tb.ORDER_NO = #{orderNo}
            </if>
            <if test="orderNos">
                AND tb.ORDER_NO IN
                <foreach collection="orderNos" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="packingTaskStatus">
                AND st.TASK_STATUS = #{packingTaskStatus}
            </if>
            <if test="packingTaskUserId">
                AND st.EXECUTOR_ID = #{packingTaskUserId}
            </if>
            <if test="isHasPackingTaskUserId">
                AND st.EXECUTOR_ID IS NOT NULL
            </if>
            <if test="hasNoShowCancel">
                AND tb.order_status != 9
            </if>
            <if test="packingTaskCreateTimeStart">
                <![CDATA[
                  AND DATE_FORMAT(st.CREATE_TIME ,'%Y-%m-%d')>= #{packingTaskCreateTimeStart}
                ]]>
            </if>
            <if test="packingTaskCreateTimeEnd">
                <![CDATA[
                  AND DATE_FORMAT(st.CREATE_TIME ,'%Y-%m-%d')<= #{packingTaskCreateTimeEnd}
                ]]>
            </if>
            <if test="orderCreateTimeStart">
                <![CDATA[
                  AND DATE_FORMAT(tb.CREATE_TIME ,'%Y-%m-%d')>= #{orderCreateTimeStart}
                ]]>
            </if>
            <if test="orderCreateTimeEnd">
                <![CDATA[
                  AND DATE_FORMAT(tb.CREATE_TIME ,'%Y-%m-%d')<= #{orderCreateTimeEnd}
                ]]>
            </if>
            <if test="createUserId">
                AND tb.create_user_id = #{createUserId}
            </if>
            <if test="despatchExpressId">
                AND tb.DESPATCH_EXPRESS_ID = #{despatchExpressId}
            </if>
        </where>
        order by tb.CREATE_TIME desc
    </select>

    <select id="getPurchaseList" resultType="com.xmomen.module.order.model.PurchaseModel" parameterType="java.util.HashMap">
        SELECT
        p.id as purchaseId,
        p.PURCHASE_CODE,
        p.PURCHASE_STATUS,
        stsdp.show_value as purchaseStatusDesc,
        p.ITEM_CODE,
        i.ITEM_NAME,
        i.PRICING_MANNER AS itemUnit,
        i.id as cdItemId,
        i.sell_Unit,
        i.spec,
        p.TOTAL AS totalItemQty,
        p.TOTAL_WEIGHT as totalWeight,
        p.DISTRIBUTE_VALUE,
        p.PACKAGE_VALUE,
        p.PACKING_VALUE,
        p.create_Date
        FROM
        tb_purchase p
        LEFT JOIN cd_item i
        ON i.ITEM_CODE = p.ITEM_CODE
        LEFT JOIN sys_dictionary stsd on stsd.DICTIONARY_CODE='PURCHASE_STATUS'
        left join sys_dictionary_parameter stsdp on stsdp.SYS_DICTIONARY_ID=stsd.ID and p.PURCHASE_STATUS = stsdp.REAL_VALUE
        <where>
            <if test="keyword">
                AND ( i.ITEM_NAME LIKE CONCAT('%', #{keyword}, '%')
                OR p.ITEM_CODE LIKE CONCAT('%', #{keyword}, '%') )
            </if>
            <if test="purchaseStatus != null">
                AND p.PURCHASE_STATUS = #{purchaseStatus}
            </if>
            <if test="startTime">
                <![CDATA[
                  AND DATE_FORMAT(p.CREATE_DATE ,'%Y-%m-%d')>= #{startTime}
                ]]>
            </if>
            <if test="endTime">
                <![CDATA[
                  AND DATE_FORMAT(p.CREATE_DATE ,'%Y-%m-%d')<= #{endTime}
                ]]>
            </if>
            <if test="isDistribute == 1">
                AND (p.TOTAL - p.DISTRIBUTE_VALUE) > 0
                AND p.PURCHASE_STATUS = 1
            </if>
            <if test="sellUnit != '' and sellUnit != null">
                AND i.SELL_UNIT = #{sellUnit}
            </if>
        </where>
        ORDER BY p.create_date desc,i.sell_Unit
    </select>

    <select id="getOrderPurchaseList" resultType="com.xmomen.module.order.model.OrderPurchaseModel" parameterType="java.util.HashMap">
        select temp.ORDER_NO,
        sum(temp.TOTAL_ITEM_QTY) total_item_qty,
        temp.ITEM_ID,
        temp.ITEM_CODE,
        temp.ITEM_UNIT,
        SUM(temp.TOTAL_WEIGHT) AS TOTAL_WEIGHT
        from (SELECT
        o.ORDER_NO,
        SUM(i.ITEM_QTY) AS TOTAL_ITEM_QTY,
        i.ITEM_ID,
        i.ITEM_CODE,
        i.ITEM_UNIT,
        SUM(i.ITEM_QTY * m.spec) AS TOTAL_WEIGHT
        FROM
        tb_order_item i
        LEFT JOIN tb_order o
        ON o.ORDER_NO = i.ORDER_NO
        left join cd_item m
        ON m.item_code = i.item_code
        <where>
            o.ORDER_STATUS = 1
            and m.ITEM_TYPE=1
            <if test="startTime">
                <![CDATA[
                  AND DATE_FORMAT(o.APPOINTMENT_TIME ,'%Y-%m-%d') >= #{startTime}
                ]]>
            </if>
            <if test="endTime">
                <![CDATA[
                  AND DATE_FORMAT(o.APPOINTMENT_TIME ,'%Y-%m-%d') <= #{endTime}
                ]]>
            </if>
        </where>
        GROUP BY o.ORDER_NO,i.item_id
        UNION ALL
        SELECT
        o.ORDER_NO,
        SUM(i.ITEM_QTY * item_ref.REF_COUNT) AS TOTAL_ITEM_QTY,
        sub_item.id itme_id,
        sub_item.ITEM_CODE,
        sub_item.PRICING_MANNER ITEM_UNIT,
        SUM(i.ITEM_QTY * item_ref.REF_COUNT * sub_item.spec) AS TOTAL_WEIGHT
        FROM
        tb_order_item i
        LEFT JOIN tb_order o ON o.ORDER_NO = i.ORDER_NO
        left join cd_item m ON m.item_code = i.item_code
        left join cd_item_ref item_ref ON item_ref.CD_ITEM_ID = m.ID and item_ref.REF_TYPE='CHILD_ITEM'
        left join cd_item sub_item on sub_item.id = item_ref.REF_VALUE
        <where>
            o.ORDER_STATUS = 1
            and m.ITEM_TYPE=2
            <if test="startTime">
                <![CDATA[
                  AND DATE_FORMAT(o.APPOINTMENT_TIME ,'%Y-%m-%d') >= #{startTime}
                ]]>
            </if>
            <if test="endTime">
                <![CDATA[
                  AND DATE_FORMAT(o.APPOINTMENT_TIME ,'%Y-%m-%d') <= #{endTime}
                ]]>
            </if>
        </where>
        GROUP BY o.ORDER_NO,sub_item.id
        )temp
        GROUP BY temp.ORDER_NO,temp.item_id
    </select>

    <select id="queryPackingModel" resultType="com.xmomen.module.order.model.PackingModel" parameterType="com.xmomen.module.order.model.PackingQuery">
        SELECT
        p.ID,
        p.PACKING_NO,
        p.PACKING_TIME,
        p.PACKING_STATUS,
        tor.ORDER_NO,
        sdp.SHOW_VALUE AS PACKING_STATUS_desc
        FROM
        tb_packing p
        LEFT JOIN sys_dictionary sd
        ON sd.DICTIONARY_CODE = 'PACKING_STATUS'
        LEFT JOIN sys_dictionary_parameter sdp
        ON sd.ID = sdp.SYS_DICTIONARY_ID
        AND sdp.REAL_VALUE = p.PACKING_STATUS
        LEFT JOIN tb_order_relation tor
        ON tor.REF_TYPE = 'ORDER_PACKING_RELATION'
        AND tor.REF_VALUE = p.PACKING_NO
        <where>
            <if test="orderNo">
                AND tor.ORDER_NO = #{orderNo}
            </if>
            <if test="packingNo">
                AND p.PACKING_NO = #{packingNo}
            </if>
            <if test="packingStatus">
                AND p.PACKING_STATUS = #{packingStatus}
            </if>
        </where>
    </select>

    <select id="queryCountPackingOrderItemModel" resultType="com.xmomen.module.order.model.PackingOrderItemModel" parameterType="com.xmomen.module.order.model.PackingOrderQuery">
        select
        tt.ITEM_CODE,
        tt.ITEM_NAME,
        sum(ITEM_QTY) as itemQty,
        sum(packedItemQty) as packedItemQty,
        CASE
        WHEN sum(packedItemQty) = 0
        THEN '未开始'
        WHEN sum(packedItemQty) = sum(ITEM_QTY)
        THEN '已完成'
        ELSE '待完成'
        END AS packing_status_desc
        from (SELECT
        oi.`ITEM_CODE`,
        oi.ITEM_NAME,
        oi.ITEM_QTY,
        COUNT(pr.ID) AS packedItemQty
        FROM
        tb_order_item oi
        LEFT JOIN tb_order o
        ON o.ORDER_NO = oi.ORDER_NO
        LEFT JOIN tb_packing_record pr
        ON pr.ORDER_ITEM_ID = oi.ID
        LEFT JOIN tb_packing p
        ON p.ID = pr.PACKING_ID
        <where>
            <if test="orderId">
                AND o.id = #{orderId}
            </if>
            <if test="orderNo">
                AND o.ORDER_NO = #{orderNo}
            </if>
            <if test="itemCode">
                AND oi.ITEM_CODE = #{itemCode}
            </if>
            <if test="orderNos">
                AND oi.ORDER_NO IN
                <foreach collection="orderNos" close=")" open="(" separator="," item="listItem" index="">
                    #{listItem}
                </foreach>
            </if>
            <if test="orderItemId">
                AND oi.id = #{orderItemId}
            </if>
        </where>
        GROUP BY oi.id) as tt group by tt.ITEM_CODE
        order by sum(ITEM_QTY) - sum(packedItemQty)
    </select>

    <select id="queryPackingOrderItemModel" resultType="com.xmomen.module.order.model.PackingOrderModel" parameterType="com.xmomen.module.order.model.PackingOrderQuery">
        SELECT
        oi.id AS orderItemId,
        p.PACKING_NO,
        oi.ITEM_NAME,
        oi.ITEM_CODE,
        o.ORDER_NO,
        oi.ITEM_QTY,
        COUNT(pr.ID) AS packedItemQty,
        CASE
        WHEN COUNT(pr.ID) = 0
        THEN '未开始'
        WHEN COUNT(pr.ID) = oi.item_qty
        THEN '已完成'
        ELSE '待完成'
        END AS packing_status_desc
        FROM
        tb_order_item oi
        LEFT JOIN tb_order o
        ON o.ORDER_NO = oi.ORDER_NO
        LEFT JOIN tb_packing_record pr
        ON pr.ORDER_ITEM_ID = oi.ID
        LEFT JOIN tb_packing p
        ON p.ID = pr.PACKING_ID
        <where>
            <if test="orderId">
                AND o.id = #{orderId}
            </if>
            <if test="orderNo">
                AND o.ORDER_NO = #{orderNo}
            </if>
            <if test="itemCode">
                AND oi.ITEM_CODE = #{itemCode}
            </if>
            <if test="orderNos">
                AND oi.ORDER_NO IN
                <foreach collection="orderNos" close=")" open="(" separator="," item="listItem" index="">
                    #{listItem}
                </foreach>
            </if>
            <if test="orderItemId">
                AND oi.id = #{orderItemId}
            </if>
            <if test="keyword">
                AND p.PACKING_NO LIKE CONCAT('%', #{keyword}, '%') OR oi.ITEM_NAME LIKE CONCAT('%', #{keyword}, '%')
            </if>
        </where>
        GROUP BY oi.ID
    </select>

    <select id="queryPackingRecordModel" resultType="com.xmomen.module.order.model.PackingRecordModel" parameterType="com.xmomen.module.order.model.PackingRecordQuery">
        SELECT
        oi.ITEM_NAME,
        tp.PACKING_NO,
        pr.*
        FROM
        tb_packing_record pr
        LEFT JOIN tb_order_item oi
        ON oi.ID = pr.ORDER_ITEM_ID
        left join tb_packing tp
        on tp.ID = pr.PACKING_ID
        <where>
            <if test="id">
                AND pr.ID = #{id}
            </if>
            <if test="orderItemId">
                AND pr.ORDER_ITEM_ID = #{orderItemId}
            </if>
            <if test="keyword">
                AND ( pr.UPC LIKE CONCAT('%', #{keyword}, '%') OR oi.ITEM_NAME LIKE CONCAT('%', #{keyword}, '%') )
            </if>
        </where>
    </select>

    <select id="countPackingTask" resultType="com.xmomen.module.order.model.PackingTaskCount" parameterType="java.util.HashMap">
    <![CDATA[
         select t.actorId,
                t.actorName,
                MAX(CASE WHEN t.type = 'TOTAL' then t.number else 0 END) as totalCount,
                MAX(CASE WHEN t.type = 'READY' then t.number else 0 END) as readyCount,
                MAX(CASE WHEN t.type = 'EXECUTING' then t.number else 0 END) as executingCount,
                MAX(CASE WHEN t.type = 'FINISHED' then t.number else 0 END) as finishedCount
                from (
                -- 待操作
                SELECT
                    su.id AS actorId,
                    su.realName AS actorName,
                    'TOTAL' as type,
                    count(st.id) as number
                FROM
                    sys_users su
                LEFT JOIN sys_users_roles sur ON sur.USER_ID = su.ID
                LEFT JOIN sys_roles sr ON sr.id = sur.ROLE_ID
                left join sys_task st on st.EXECUTOR_ID = su.id
                 and st.TASK_STATUS IN (0,1,2)
                WHERE
                    sr.ROLE = #{roleType} group by su.id

                UNION ALL
                -- 待操作
                SELECT
                    su.id AS actorId,
                    su.realName AS actorName,
                    'READY' as type,
                    count(st.id) as number
                FROM
                    sys_users su
                LEFT JOIN sys_users_roles sur ON sur.USER_ID = su.ID
                LEFT JOIN sys_roles sr ON sr.id = sur.ROLE_ID
                left join sys_task st on st.EXECUTOR_ID = su.id
                 and st.TASK_STATUS=0
                WHERE
                    sr.ROLE = #{roleType} group by su.id

                union alL
                    -- 操作中
                SELECT
                    su.id AS actorId,
                    su.realName AS actorName,
                    'EXECUTING' as type,
                    count(st.id) as number
                FROM
                    sys_users su
                LEFT JOIN sys_users_roles sur ON sur.USER_ID = su.ID
                LEFT JOIN sys_roles sr ON sr.id = sur.ROLE_ID
                left join sys_task st on st.EXECUTOR_ID = su.id
                 and st.TASK_STATUS=1
                WHERE
                    sr.ROLE = #{roleType} group by su.id

                union all
                    -- 已完成
                SELECT
                    su.id AS actorId,
                    su.realName AS actorName,
                    'FINISHED' as type,
                    count(st.id) as number
                FROM
                    sys_users su
                LEFT JOIN sys_users_roles sur ON sur.USER_ID = su.ID
                LEFT JOIN sys_roles sr ON sr.id = sur.ROLE_ID
                left join sys_task st on st.EXECUTOR_ID = su.id
                 and st.TASK_STATUS=2
                WHERE
                    sr.ROLE = #{roleType} group by su.id
                    ) t group by actorId



        ]]>
    </select>


    <!--    查询订单    -->
    <select id="getPackageTaskList" resultType="com.xmomen.module.order.model.OrderModel" parameterType="com.xmomen.module.order.model.OrderQuery">
        SELECT
        torp.REF_VALUE as couponNumber,
        sp.SHOW_VALUE AS orderTypeDesc,
        ssp.SHOW_VALUE AS orderStatusDesc,
        paymentp.SHOW_VALUE AS paymentModeDesc,
        osp.SHOW_VALUE AS orderSourceDesc,
        st.EXECUTOR_ID AS packingTaskUserId,
        st.CREATE_TIME as packingTaskCreateTime,
        st.START_TIME as packingTaskStartTime,
        st.FINISH_TIME as packingTaskEndTime,
        u.realname AS packingTaskUsername,
        st.TASK_STATUS as packingTaskStatus,
        stsdp.show_value as packingTaskStatusDesc,
        ce.express_name,
        users.realname as manager_name,
        pay2.`AMOUNT` AS otherPayAmount,
        tb.*
        FROM
        tb_order tb
        LEFT JOIN sys_dictionary s on s.DICTIONARY_CODE='ORDER_TYPE'
        left join sys_dictionary_parameter sp on sp.SYS_DICTIONARY_ID=s.ID and tb.ORDER_TYPE = sp.REAL_VALUE

        LEFT JOIN sys_dictionary ss on ss.DICTIONARY_CODE='ORDER_STATUS'
        left join sys_dictionary_parameter ssp on ssp.SYS_DICTIONARY_ID=ss.ID and tb.ORDER_STATUS = ssp.REAL_VALUE

        LEFT JOIN sys_dictionary payment on payment.DICTIONARY_CODE='PAYMENT_MODE'
        left join sys_dictionary_parameter paymentp on paymentp.SYS_DICTIONARY_ID=payment.ID and tb.PAYMENT_MODE = paymentp.REAL_VALUE

        LEFT JOIN sys_dictionary oss on oss.DICTIONARY_CODE='ORDER_SOURCE'
        left join sys_dictionary_parameter osp on osp.SYS_DICTIONARY_ID=oss.ID and tb.ORDER_SOURCE = osp.REAL_VALUE

        left join `tb_trade_record` pay2 on pay2.`TRADE_NO` = tb.`ORDER_NO` and pay2.`TRADE_TYPE` = tb.`OTHER_PAYMENT_MODE`

        LEFT JOIN tb_order_relation tor ON tor.ORDER_NO = tb.ORDER_NO AND tor.REF_TYPE = 'ORDER_PACKING_TASK_RELATION'
        LEFT JOIN tb_order_relation torp ON torp.ORDER_NO = tb.ORDER_NO AND torp.REF_TYPE = 'ORDER_PAY_RELATION'
        LEFT JOIN sys_task st on st.ID= tor.REF_VALUE
        LEFT JOIN sys_users u ON u.id = st.EXECUTOR_ID
        LEFT JOIN sys_dictionary stsd on stsd.DICTIONARY_CODE='JOB_STATUS'
        left join sys_dictionary_parameter stsdp on stsdp.SYS_DICTIONARY_ID=stsd.ID and st.TASK_STATUS = stsdp.REAL_VALUE
        left join cd_express ce on tb.despatch_express_id = ce.id

        left join sys_users users on users.id = tb.MANAGER_ID
        <where>
            tb.order_status != 0
            <if test="orderStatus">
                AND tb.order_status = #{orderStatus}
            </if>
            <if test="keyword">
                AND (tb.order_no LIKE CONCAT('%', #{keyword}, '%')
                or tb.consignee_phone LIKE CONCAT('%', #{keyword}, '%')
                or tb.consignee_name like CONCAT('%', #{keyword}, '%')
                )
            </if>
            <if test="id">
                AND tb.id = #{id}
            </if>
            <if test="managerId">
                AND tb.MANAGER_ID = #{managerId}
            </if>
            <if test="consigneeName">
                AND tb.consignee_name LIKE CONCAT('%', #{consigneeName}, '%')
            </if>
            <if test="orderNo">
                AND tb.ORDER_NO = #{orderNo}
            </if>
            <if test="orderNos">
                AND tb.ORDER_NO IN
                <foreach collection="orderNos" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="packingTaskStatus">
                AND st.TASK_STATUS = #{packingTaskStatus}
            </if>
            <if test="showPackingAssigns == 1">
                AND st.TASK_STATUS is not null
            </if>
            <if test="showPackingAssigns == 0">
                AND st.TASK_STATUS is null
            </if>
            <if test="packingTaskUserId">
                AND st.EXECUTOR_ID = #{packingTaskUserId}
            </if>
            <if test="isHasPackingTaskUserId">
                AND st.EXECUTOR_ID IS NOT NULL
            </if>
            <if test="hasNoShowCancel">
                AND tb.order_status != 9
            </if>
            <if test="packingTaskCreateTimeStart">
                <![CDATA[
                  AND DATE_FORMAT(st.CREATE_TIME ,'%Y-%m-%d')>= #{packingTaskCreateTimeStart}
                ]]>
            </if>
            <if test="packingTaskCreateTimeEnd">
                <![CDATA[
                  AND DATE_FORMAT(st.CREATE_TIME ,'%Y-%m-%d')<= #{packingTaskCreateTimeEnd}
                ]]>
            </if>
            <if test="appointmentTimeStart">
                <![CDATA[
                  AND DATE_FORMAT(tb.APPOINTMENT_TIME ,'%Y-%m-%d')>= #{appointmentTimeStart}
                ]]>
            </if>
            <if test="appointmentTimeEnd">
                <![CDATA[
                  AND DATE_FORMAT(tb.APPOINTMENT_TIME ,'%Y-%m-%d')<= #{appointmentTimeEnd}
                ]]>
            </if>
            <if test="createUserId">
                AND tb.create_user_id = #{createUserId}
            </if>
        </where>
        order by st.TASK_STATUS,tb.order_no desc
    </select>

    <select id="countBatchOrder" resultType="java.lang.Integer" parameterType="java.lang.String">
    	select count(1) from tb_order where batch_no =#{batchNo}
    </select>


</mapper>
