<?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.sckw.mine.mapper.MineOrderMapper">

    <select id="mineOrderPageList" parameterType="com.sckw.mine.entity.req.MineOrderPageListParam"
            resultType="com.sckw.mine.entity.res.MineOrderPageListRes">

        select
               o.id,
        o.mine_order_no,
        o.order_source,
        o.cg_company_id,
        o.cg_company,
        o.cg_company_concat,
        o.cg_company_name,
        o.fk_dept_id,
        o.fk_dept,
        o.sk_dept_id,
        o.sk_dept,
        o.customer_order_time,
        o.delivery_cutoff_time,
        o.is_transport,
        o.work_flow,
        o.status,
        o.mineral_aggregate_id,
        o.create_by,
        o.update_by,
        o.create_time,
        o.update_time,
        m.mine_vol,
        m.purchase_num,
        m.mine_weight,
        n.transport_order_no

        from kw_business_mine_order o
        left join kw_business_transport_order_no n on n.mine_order_id = o.id
        left join kw_business_mine m on m.mine_order_id = o.id
        <where>
         o.del_flag = 0
            <if test="param.mineOrderNo != '' and param.mineOrderNo != null">
                and o.mine_order_no like concat('%',#{param.mineOrderNo},'%')
            </if>
            <if test="param.orderSource != '' and param.orderSource != null">
                and o.order_source  = #{param.orderSource}
            </if>
            <if test="param.startTime != '' and param.startTime != null">
                and o.create_time  >=  #{param.startTime}
            </if>
            <if test="param.endTime != '' and param.endTime != null">
                and o.create_time  &lt; #{param.endTime}
            </if>
            <if test="param.tenantId != '' and param.tenantId != null">
                and o.tenant_id  = #{param.tenantId}
            </if>
        </where>
        order by o.create_time desc
     </select>
    <select id="selectOrderById" resultType="com.sckw.mine.entity.res.MineOrderDetailRes">
        select
        o.id,
        o.mine_order_no,
        o.order_source,
        o.cg_company_id,
        o.cg_company,
        o.cg_company_concat,
        o.cg_company_name,
        o.fk_dept_id,
        o.fk_dept,
        o.sk_dept_id,
        o.sk_dept,
        o.customer_order_time,
        o.delivery_cutoff_time,
        o.is_transport,
        o.work_flow,
        o.status,
        o.mineral_aggregate_id,
        o.create_by,
        o.update_by,
        o.create_time,
        o.update_time,
        m.mine_vol,
        m.purchase_num,
        m.mine_weight,
        n.transport_order_no

        from kw_business_mine_order o
        left join kw_business_transport_order_no n on n.mine_order_id = o.id
        left join kw_business_mine m on m.mine_order_id = o.id
        where o.del_flag = 0 and o.id = #{orderId} and o.del_flag = 0
    </select>


    <select id="countByStatus" resultType="java.util.Map">
        SELECT `status`, COUNT(*) AS count FROM kw_business_mine_order
        <where>
            del_flag = 0
            <if test="tenantId != '' and tenantId != null">
                and tenant_id  = #{tenantId}
            </if>
        </where>
         GROUP BY `status`
    </select>


</mapper>