<?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.boco.nbd.wios.manage.mapper.def.ReportMapper">

    <resultMap id="BaseResultMap" type="com.boco.nbd.wios.manage.entity.bo.ReportOrderDTO">
        <result column="supplier_id" jdbcType="INTEGER" property="supplierId" />
        <result column="supplier_name" jdbcType="VARCHAR" property="supplierName" />
        <result column="region_id" jdbcType="INTEGER" property="regionId" />
        <result column="region_name" jdbcType="VARCHAR" property="regionName" />
        <result column="count_total" jdbcType="INTEGER" property="countTotal" />
        <result column="count_unfinished" jdbcType="INTEGER" property="countUnfinished" />
        <result column="count_finished" jdbcType="INTEGER" property="countFinished" />
        <result column="count_closed" jdbcType="INTEGER" property="countClosed" />
        <result column="avg_install_time" jdbcType="DECIMAL" property="avgInstallTime" />
        <result column="finish_ratio" jdbcType="DECIMAL" property="finishRatio" />
    </resultMap>


    <sql id="reportOrderWhere">
            <if test="supplierIds != null">
                and od.install_supplier_id in ( ${supplierIds} )
            </if>
            <if test="regionId != null">
                and od.region_id = #{regionId,jdbcType=INTEGER}
            </if>
            <if test="vehicleModel != null and vehicleModel != ''" >
                and od.vehicle_model like  CONCAT(CONCAT('%', #{vehicleModel,jdbcType=VARCHAR}),'%')
            </if>
            <!-- <if test="beginTime != null  and beginTime != ''" >
                and od.survey_reserve_time <![CDATA[ >= ]]> #{beginTime,jdbcType=VARCHAR}
            </if>
            <if test="endTime != null  and endTime != ''" >
                and od.survey_reserve_time <![CDATA[ <= ]]> #{endTime,jdbcType=VARCHAR}
            </if> -->
    </sql>


    <select id="countOrderGroupBySupplierAndRegion"  resultMap="BaseResultMap" parameterType="com.boco.nbd.wios.manage.entity.bo.ReportOrderBo">
        select
        od.install_supplier_id as supplier_id, ts.name as supplier_name,
        <if test="regionId == null">
            od.region_id, tr.name as region_name,
        </if>
         count(1) as count_total,
        sum(case od.status when 64 then 1 else 0 end) as count_closed,
        sum(case when od.install_finish_time is null then (case od.status when 64 then 0 else 1 end)  else 0 end) as count_unfinished,
        sum(case when od.install_finish_time is null then 0 else (case od.status when 64 then 0 else 1 end)  end) as count_finished
        from  t_order od
        join t_supplier ts on od.install_supplier_id = ts.id
        join t_region tr on od.region_id = tr.id
        where 1=1
        <if test="beginTime != null  and beginTime != ''" >
            and od.dispatch_time <![CDATA[ >= ]]> #{beginTime,jdbcType=VARCHAR}
        </if>
        <if test="endTime != null  and endTime != ''" >
            and od.dispatch_time <![CDATA[ <= ]]> #{endTime,jdbcType=VARCHAR}
        </if>
        <if test="supplierIds != null">
            and od.install_supplier_id in ( ${supplierIds} )
        </if>
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and tr.id_tree like #{regionIdTreeLike}
        </if>
        <if test="vehicleModel != null and vehicleModel != ''" >
            and od.vehicle_model like  CONCAT(CONCAT('%', #{vehicleModel,jdbcType=VARCHAR}),'%')
        </if>
        group by od.install_supplier_id
        <if test="regionId == null" >
            ,od.region_id, tr.name
        </if>
        ORDER BY
		od.region_id+0 asc,
		od.install_supplier_id+0 asc
    </select>

    <select id="countOrderGroupBySupplier"  resultMap="BaseResultMap" parameterType="com.boco.nbd.wios.manage.entity.bo.ReportOrderBo">
        select
        od.install_supplier_id as supplier_id, ts.name as supplier_name,
        count(1) as count_total,
        sum(case od.status when 64 then 1 else 0 end) as count_closed,
        sum(case when od.install_finish_time is null then 1 else 0 end) as count_unfinished,
        sum(case when od.install_finish_time is null then 0 else 1 end) as count_finished
        from  t_order od
        join t_supplier ts on od.install_supplier_id = ts.id
        <if test="beginTime != null  and beginTime != ''" >
        	JOIN t_operate_log log on log.third_party_id=od.id and log.name=10
        </if>
        where 1=1
        <if test="beginTime != null  and beginTime != ''" >
            and log.create_time <![CDATA[ >= ]]> #{beginTime,jdbcType=VARCHAR}
        </if>
        <if test="endTime != null  and endTime != ''" >
            and log.create_time <![CDATA[ <= ]]> #{endTime,jdbcType=VARCHAR}
        </if>
        <include refid="reportOrderWhere" />
        group by od.install_supplier_id,ts.name
    </select>
    <select id="calcOrderFinishedTime"  resultMap="BaseResultMap" parameterType="com.boco.nbd.wios.manage.entity.bo.ReportOrderBo">
        select od.install_supplier_id as supplier_id,
        avg(timestampdiff(hour, ol.create_time, od.install_finish_time)) as avg_install_time
        from  t_order od
        join t_operate_log ol on od.id = ol.third_party_id and ol.name=7
        <if test="beginTime != null  and beginTime != ''" >
        	JOIN t_operate_log log on log.third_party_id=od.id and log.name=10
        </if>
        where 1 = 1
        <if test="beginTime != null  and beginTime != ''" >
            and log.create_time <![CDATA[ >= ]]> #{beginTime,jdbcType=VARCHAR}
        </if>
        <if test="endTime != null  and endTime != ''" >
            and log.create_time <![CDATA[ <= ]]> #{endTime,jdbcType=VARCHAR}
        </if>
        <include refid="reportOrderWhere" />
        group by od.install_supplier_id
    </select>
    <select id="orderResponseToAdmin" resultType="com.boco.nbd.wios.manage.entity.bo.ReportOrderResponseDTO">
        SELECT
        convert(ifnull(
        sum(
        timestampdiff(
        SECOND,
        od.dispatch_time,
        od.create_time
        )/3600
        ),
        0
        ) / COUNT(1),decimal(10,2)) as order_response,
        COUNT(od.id) cnt,
        sp.id as supplier_id,sp.NAME as supplier_name
        FROM
        t_supplier sp
        left join
        (
        select o.id,o.install_supplier_id,o.dispatch_time,ol.create_time from t_order o
        LEFT JOIN t_region r ON r.id = o.region_id
        LEFT JOIN t_operate_log ol ON o.id = ol.third_party_id
        where ol.NAME = '13'
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and r.id_tree like #{regionIdTreeLike}
        </if>
        <if test="oemId != null  and oemId != ''" >
            and o.oem_id = #{oemId,jdbcType=VARCHAR}
        </if>
        <if test="dispatchBeginTime != null  and dispatchBeginTime != ''" >
            and o.dispatch_time &gt;= #{dispatchBeginTime,jdbcType=VARCHAR}
        </if>
        <if test="dispatchEndTime != null  and dispatchEndTime != ''" >
            and o.dispatch_time &lt;= #{dispatchEndTime,jdbcType=VARCHAR}
        </if>
        ) od ON od.install_supplier_id = sp.id
        WHERE 1=1
        <if test="supplierId != null  and supplierId != ''" >
            and sp.id in
            <foreach collection="supplierId.split(',')" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY od.install_supplier_id,sp.NAME
    </select>
    <select id="surveyResponseToAdmin" resultType="com.boco.nbd.wios.manage.entity.bo.ReportOrderResponseDTO">
        SELECT
        convert(ifnull(
        sum(
        timestampdiff(
        SECOND,
        od.dispatch_time,
        od.survey_finish_time
        )/3600
        ),
        0
        ) / 24 / count(1),decimal(10,2)) as order_response,
        COUNT(od.id) cnt,
        sp.id as supplier_id,sp.NAME as supplier_name
        FROM
        t_supplier sp
        left join
        (
        select o.id,o.install_supplier_id,o.dispatch_time,o.survey_finish_time from t_order o
        LEFT JOIN t_region r ON r.id = o.region_id
        where o.survey_finish_time is not null
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and r.id_tree like #{regionIdTreeLike}
        </if>
        <if test="oemId != null  and oemId != ''" >
            and o.oem_id = #{oemId,jdbcType=VARCHAR}
        </if>
        <if test="dispatchBeginTime != null  and dispatchBeginTime != ''" >
            and o.dispatch_time &gt;= #{dispatchBeginTime,jdbcType=VARCHAR}
        </if>
        <if test="dispatchEndTime != null  and dispatchEndTime != ''" >
            and o.dispatch_time &lt;= #{dispatchEndTime,jdbcType=VARCHAR}
        </if>
        ) od ON od.install_supplier_id = sp.id
        WHERE
        1=1
        <if test="supplierId != null  and supplierId != ''" >
            and sp.id in
            <foreach collection="supplierId.split(',')" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY od.install_supplier_id,sp.NAME
    </select>
    <select id="installResponseToAdmin" resultType="com.boco.nbd.wios.manage.entity.bo.ReportOrderResponseDTO">
        SELECT
        convert(ifnull(
        sum(
        timestampdiff(
        SECOND,
        od.dispatch_time,
        od.install_finish_time
        )/3600
        ),
        0
        ) / 24 / count(1),decimal(10,2)) as order_response,
        COUNT(od.id) cnt,
        sp.id as supplier_id,sp.NAME as supplier_name
        FROM
        t_supplier sp
        left join
        (
        select o.id,o.install_supplier_id,o.dispatch_time,o.install_finish_time from t_order o
        LEFT JOIN t_region r ON r.id = o.region_id
        where o.install_finish_time is not null
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and r.id_tree like #{regionIdTreeLike}
        </if>
        <if test="oemId != null  and oemId != ''" >
            and o.oem_id = #{oemId,jdbcType=VARCHAR}
        </if>
        <if test="dispatchBeginTime != null  and dispatchBeginTime != ''" >
            and o.dispatch_time &gt;= #{dispatchBeginTime,jdbcType=VARCHAR}
        </if>
        <if test="dispatchEndTime != null  and dispatchEndTime != ''" >
            and o.dispatch_time &lt;= #{dispatchEndTime,jdbcType=VARCHAR}
        </if>
        ) od ON od.install_supplier_id = sp.id
        WHERE
        1=1
        <if test="supplierId != null  and supplierId != ''" >
            and sp.id in
            <foreach collection="supplierId.split(',')" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY od.install_supplier_id,sp.NAME
    </select>
    <select id="installEfficientToAdmin" resultType="com.boco.nbd.wios.manage.entity.bo.ReportOrderResponseDTO">
        SELECT
        convert(ifnull(
        sum(
        CASE
        WHEN od.`status` IN (46, 49, 52, 61) THEN
        1
        ELSE
        0
        END
        ),
        0
        ) / count(1)*100,decimal(10,2)) as order_response,
        COUNT(od.id) cnt,
        sp.id as supplier_id,sp.NAME as supplier_name
        FROM
        t_supplier sp
        left join
        (
        select o.id,o.install_supplier_id,o.dispatch_time,o.status from t_order o
        LEFT JOIN t_region r ON r.id = o.region_id
        where o.STATUS != 64
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and r.id_tree like #{regionIdTreeLike}
        </if>
        <if test="oemId != null  and oemId != ''" >
            and o.oem_id = #{oemId,jdbcType=VARCHAR}
        </if>
        <if test="dispatchBeginTime != null  and dispatchBeginTime != ''" >
            and o.dispatch_time &gt;= #{dispatchBeginTime,jdbcType=VARCHAR}
        </if>
        <if test="dispatchEndTime != null  and dispatchEndTime != ''" >
            and o.dispatch_time &lt;= #{dispatchEndTime,jdbcType=VARCHAR}
        </if>
        ) od ON od.install_supplier_id = sp.id
        WHERE
        1=1
        <if test="supplierId != null  and supplierId != ''" >
            and sp.id in
            <foreach collection="supplierId.split(',')" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY od.install_supplier_id,sp.NAME
    </select>
    <select id="reportPassToAdmin" resultType="com.boco.nbd.wios.manage.entity.bo.ReportOrderResponseDTO">
        SELECT
        ifnull(od.order_response,0) as order_response,
        od.cnt,
        sp.id as supplier_id,sp.NAME as supplier_name
        FROM
        t_supplier sp
        left join
        (
        select
        o.install_supplier_id,
        convert((count(DISTINCT o.id) - count(DISTINCT l2.third_party_id) ) / count(DISTINCT o.id)*100,decimal(10,2)) as order_response,
        count(DISTINCT o.id) cnt
        from t_order o
        LEFT JOIN t_region r ON r.id = o.region_id
        LEFT JOIN t_operate_log l1 ON o.id = l1.third_party_id
        LEFT JOIN t_operate_log l2 ON l2.third_party_id = l1.third_party_id
        <if test="type == 1">
        and l2.`NAME` = '28'
        WHERE l1.`NAME` in( '25','28')
        </if>
        <if test="type == 2">
        and l2.`NAME` = '55'
        WHERE l1.`NAME` in( '52','55')
        </if>
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and r.id_tree like #{regionIdTreeLike}
        </if>
        <if test="oemId != null  and oemId != ''" >
            and o.oem_id = #{oemId,jdbcType=VARCHAR}
        </if>
        <if test="dispatchBeginTime != null  and dispatchBeginTime != ''" >
            and l1.create_time &gt;= #{dispatchBeginTime,jdbcType=VARCHAR}
        </if>
        <if test="dispatchEndTime != null  and dispatchEndTime != ''" >
            and l1.create_time &lt;= #{dispatchEndTime,jdbcType=VARCHAR}
        </if>
        GROUP BY
        o.install_supplier_id
        ) od ON od.install_supplier_id = sp.id
        WHERE 1=1
        <if test="supplierId != null  and supplierId != ''" >
            and sp.id in
            <foreach collection="supplierId.split(',')" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ORDER BY od.install_supplier_id,sp.NAME
    </select>
    <select id="orderResponseToSupplier" resultType="com.boco.nbd.wios.manage.entity.bo.ReportOrderResponseDTO">
        SELECT
        convert(ifnull(
        sum(
        timestampdiff(
        SECOND,
        od.dispatch_time,
        ol.create_time
        )/3600
        ),
        0
        ) / COUNT(1),decimal(10,2)) as order_response,
        DATE_FORMAT(ol.create_time,'%Y-%m') as month
        FROM
        t_order od
        LEFT JOIN t_region r ON r.id = od.region_id
        LEFT JOIN t_operate_log ol ON od.id = ol.third_party_id
        WHERE
        od.install_supplier_id = #{supplierId,jdbcType=VARCHAR} AND ol.NAME = '13'
        <if test="dispatchBeginTime != null  and dispatchBeginTime != ''" >
            and DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{dispatchBeginTime,jdbcType=VARCHAR}
        </if>
        <if test="dispatchEndTime != null  and dispatchEndTime != ''" >
            and DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;= #{dispatchEndTime,jdbcType=VARCHAR}
        </if>
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and r.id_tree like #{regionIdTreeLike}
        </if>
        <if test="oemId != null  and oemId != ''" >
            and od.oem_id = #{oemId,jdbcType=VARCHAR}
        </if>
        GROUP BY DATE_FORMAT(od.dispatch_time,'%Y-%m')
    </select>
    <select id="surveyResponseToSupplier" resultType="com.boco.nbd.wios.manage.entity.bo.ReportOrderResponseDTO">
        SELECT
        convert(ifnull(
        sum(
        timestampdiff(
        SECOND,
        od.dispatch_time,
        od.survey_finish_time
        )/3600
        ),
        0
        ) / 24 / count(1),decimal(10,2)) as order_response,
        DATE_FORMAT(od.dispatch_time,'%Y-%m') as month
        FROM
        t_order od
        LEFT JOIN t_region r ON r.id = od.region_id
        WHERE od.survey_finish_time is not null
        and od.install_supplier_id = #{supplierId,jdbcType=VARCHAR}
        <if test="dispatchBeginTime != null  and dispatchBeginTime != ''" >
            and DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{dispatchBeginTime,jdbcType=VARCHAR}
        </if>
        <if test="dispatchEndTime != null  and dispatchEndTime != ''" >
            and DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;= #{dispatchEndTime,jdbcType=VARCHAR}
        </if>
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and r.id_tree like #{regionIdTreeLike}
        </if>
        <if test="oemId != null  and oemId != ''" >
            and od.oem_id = #{oemId,jdbcType=VARCHAR}
        </if>
        GROUP BY DATE_FORMAT(od.dispatch_time,'%Y-%m')
    </select>
    <select id="installResponseToSupplier" resultType="com.boco.nbd.wios.manage.entity.bo.ReportOrderResponseDTO">
        SELECT
        convert(ifnull(
        sum(
        timestampdiff(
        SECOND,
        od.dispatch_time,
        od.install_finish_time
        )/3600
        ),
        0
        ) / 24 / count(1),decimal(10,2)) as order_response,
        DATE_FORMAT(od.dispatch_time,'%Y-%m') as month
        FROM
        t_order od
        LEFT JOIN t_region r ON r.id = od.region_id
        WHERE od.install_finish_time is not null
        and od.install_supplier_id = #{supplierId,jdbcType=VARCHAR}
        <if test="dispatchBeginTime != null  and dispatchBeginTime != ''" >
            and DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{dispatchBeginTime,jdbcType=VARCHAR}
        </if>
        <if test="dispatchEndTime != null  and dispatchEndTime != ''" >
            and DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;= #{dispatchEndTime,jdbcType=VARCHAR}
        </if>
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and r.id_tree like #{regionIdTreeLike}
        </if>
        <if test="oemId != null  and oemId != ''" >
            and od.oem_id = #{oemId,jdbcType=VARCHAR}
        </if>
        GROUP BY DATE_FORMAT(od.dispatch_time,'%Y-%m')
    </select>
    <select id="installEfficientToSupplier" resultType="com.boco.nbd.wios.manage.entity.bo.ReportOrderResponseDTO">
        SELECT
        convert(ifnull(
        sum(
        CASE
        WHEN od.`status` IN (46, 49, 52, 61) THEN
        1
        ELSE
        0
        END
        ),
        0
        ) / count(1)*100,decimal(10,2)) as order_response,
        DATE_FORMAT(od.dispatch_time,'%Y-%m') as month
        FROM
        t_order od
        LEFT JOIN t_region r ON r.id = od.region_id
        WHERE
        od.STATUS != 64
        and od.install_supplier_id = #{supplierId,jdbcType=VARCHAR}
        <if test="dispatchBeginTime != null  and dispatchBeginTime != ''" >
            and DATE_FORMAT(od.dispatch_time,'%Y-%m') &gt;= #{dispatchBeginTime,jdbcType=VARCHAR}
        </if>
        <if test="dispatchEndTime != null  and dispatchEndTime != ''" >
            and DATE_FORMAT(od.dispatch_time,'%Y-%m') &lt;= #{dispatchEndTime,jdbcType=VARCHAR}
        </if>
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and r.id_tree like #{regionIdTreeLike}
        </if>
        <if test="oemId != null  and oemId != ''" >
            and od.oem_id = #{oemId,jdbcType=VARCHAR}
        </if>
        GROUP BY DATE_FORMAT(od.dispatch_time,'%Y-%m')
    </select>
    <select id="reportPassToSupplier" resultType="com.boco.nbd.wios.manage.entity.bo.ReportOrderResponseDTO">
        SELECT
        convert((count(DISTINCT od.id) - count(DISTINCT l2.third_party_id) ) / count(DISTINCT od.id)*100,decimal(10,2)) as order_response,
        DATE_FORMAT(l1.create_time,'%Y-%m') as month
        FROM
        t_order od
        LEFT JOIN t_region r ON r.id = od.region_id
        LEFT JOIN t_operate_log l1 ON od.id = l1.third_party_id
        LEFT JOIN t_operate_log l2 ON l2.third_party_id = l1.third_party_id
        <if test="type == 1">
        and l2.`NAME` = '28'
        WHERE l1.`NAME` in( '25','28')
        </if>
        <if test="type == 2">
        and l2.`NAME` = '55'
        WHERE l1.`NAME` in( '52','55')
        </if>
        and od.install_supplier_id = #{supplierId,jdbcType=VARCHAR}
        <if test="dispatchBeginTime != null  and dispatchBeginTime != ''" >
            and DATE_FORMAT(l1.create_time,'%Y-%m') &gt;= #{dispatchBeginTime,jdbcType=VARCHAR}
        </if>
        <if test="dispatchEndTime != null  and dispatchEndTime != ''" >
            and DATE_FORMAT(l1.create_time,'%Y-%m') &lt;= #{dispatchEndTime,jdbcType=VARCHAR}
        </if>
        <if test="regionIdTree != null  and regionIdTree != ''" >
            <bind name="regionIdTreeLike" value="regionIdTree + '%'"/>
            and r.id_tree like #{regionIdTreeLike}
        </if>
        <if test="oemId != null  and oemId != ''" >
            and od.oem_id = #{oemId,jdbcType=VARCHAR}
        </if>
        GROUP BY DATE_FORMAT(l1.create_time,'%Y-%m')
    </select>
</mapper>