<?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.flow.mapper.def.StatisticsMapper">

    <!--归档订单总数-->
    <select id="selectFiledOrderCount"  resultType="java.lang.Integer">
        select count(*) from t_order a where a.status=64 and a.type=1 and a.process_ins_id is not null and nodeFlag='199'
        <if test="nodeFlag != null ">
            and a.node_flag in
            <foreach collection="nodeFlag" item="nodeFlag" open="(" separator="," close=")">
                    #{nodeFlag}
            </foreach>
        </if>
        <if test="createTime != null and createTime != ''">
            <if test="timeType=='day'">
                <choose>
                    <when test="nodeName != null and nodeName != ''">
                        and exists (select 1 from ACT_HI_TASKINST b where a.process_ins_id = b.PROC_INST_ID_ and b.NAME_ = #{nodeName} and date_format(b.END_TIME_ ,'%Y-%m-%d')= #{createTime})
                    </when>
                    <otherwise>
                        and DATE_FORMAT(a.create_time,'%Y-%m-%d')=#{createTime}
                    </otherwise>
                </choose>

            </if>
            <if test="timeType=='month'">
                <choose>
                    <when test="nodeName != null and nodeName != ''">
                        and exists (select 1 from ACT_HI_TASKINST b where a.process_ins_id = b.PROC_INST_ID_ and b.NAME_ = #{nodeName} and date_format(b.END_TIME_ ,'%Y-%m')= #{createTime})
                    </when>
                    <otherwise>
                        and DATE_FORMAT(a.create_time,'%Y-%m')=#{createTime}
                    </otherwise>
                </choose>
            </if>
        </if>
        <if test="oemId != null and oemId != ''">
                and a.oem_id=#{oemId}
        </if>
        <if test="oemId != null and oemId != ''">
            and a.oem_id=#{oemId}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&lt;= #{endTime}
        </if>
    </select>


    <!--流程中订单总数-->
    <select id="selectFlowOrderCount"  resultType="java.lang.Integer">
        select count(*) from t_order a where a.status!=64 and a.type=1 and a.process_ins_id is not null
        <if test="nodeFlag != null ">
            and a.node_flag in
            <foreach collection="nodeFlag" item="nodeFlag" open="(" separator="," close=")">
                #{nodeFlag}
            </foreach>
        </if>
        <if test="createTime != null and createTime != ''">
            <if test="timeType=='day'">
                <choose>
                    <when test="nodeName != null and nodeName != ''">
                        and exists (select 1 from ACT_HI_TASKINST b where a.process_ins_id = b.PROC_INST_ID_ and b.NAME_ = #{nodeName} and date_format(b.END_TIME_ ,'%Y-%m-%d')= #{createTime})
                    </when>
                    <otherwise>
                        and DATE_FORMAT(a.create_time,'%Y-%m-%d')=#{createTime}
                    </otherwise>
                </choose>

            </if>
            <if test="timeType=='month'">
                <choose>
                    <when test="nodeName != null and nodeName != ''">
                        and exists (select 1 from ACT_HI_TASKINST b where a.process_ins_id = b.PROC_INST_ID_ and b.NAME_ = #{nodeName} and date_format(b.END_TIME_ ,'%Y-%m')= #{createTime})
                    </when>
                    <otherwise>
                        and DATE_FORMAT(a.create_time,'%Y-%m')=#{createTime}
                    </otherwise>
                </choose>
            </if>
        </if>
        <if test="oemId != null and oemId != ''">
            and a.oem_id=#{oemId}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&lt;= #{endTime}
        </if>
    </select>


    <resultMap id="StatisticsCountMap" type="java.util.HashMap">
        <result column="name" property="key" javaType="java.lang.String"/>
        <result column="num" property="value" javaType="java.lang.Integer"/>
    </resultMap>

    <!--根据节点查询订单总数-->
    <select id="getFlowOrderCount" resultType="java.lang.Integer">
        select  count(*)
          from  t_order
        where  type=1 and process_ins_id is not null  and (node_flag!=199 and status=64)=false
        <if test="oemId != null and oemId != ''">
            and oem_id=#{oemId}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&lt;= #{endTime}
        </if>
        <if test="nodeFlag != null ">
            and node_flag in
            <foreach collection="nodeFlag" item="nodeFlag" open="(" separator="," close=")">
                #{nodeFlag}
            </foreach>
        </if>
    </select>

    <!--区域根据节点查询订单总数-->
    <select id="getFlowOrderCountGroupArea" resultMap="StatisticsCountMap">
        select  area_name name,count(*) num
        from  t_order
        where  type=1 and process_ins_id is not null  and (node_flag!=199 and status=64)=false and area_id in (select id from t_region where parent_id=1)
        <if test="oemId != null and oemId != ''">
            and oem_id=#{oemId}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&lt;= #{endTime}
        </if>
        <if test="nodeFlag != null ">
            and node_flag in
            <foreach collection="nodeFlag" item="nodeFlag" open="(" separator="," close=")">
                #{nodeFlag}
            </foreach>
        </if>
        GROUP BY area_name
    </select>


    <!--派发订单总数-->
    <select id="getOrderCount" resultType="java.lang.Integer">
        select  count(*)
        from  t_order
        where  type=1 and process_ins_id is not null  and (node_flag!=199 and status=64)=false
        <if test="oemId != null and oemId != ''">
            and oem_id=#{oemId}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&lt;= #{endTime}
        </if>
    </select>

    <!--区域派发订单总数-->
    <select id="getOrderCountGroupArea" resultMap="StatisticsCountMap">
        select  area_name name,count(*) num
        from  t_order
        where  type=1 and process_ins_id is not null  and (node_flag!=199 and status=64)=false and area_id in (select id from t_region where parent_id=1)
        <if test="oemId != null and oemId != ''">
            and oem_id=#{oemId}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&lt;= #{endTime}
        </if>
        GROUP BY area_name
    </select>




    <!--省份订单总量-->
    <select id="selectProvinceOrderCount" resultType="com.boco.nbd.wios.flow.entity.vo.StatisticsProvinceVo">

        select a.province_id,a.province_name as provinceName ,count(*) as orderCount from t_order a where a.type=1 and (a.node_flag!=199 and a.status=64)=false and a.process_ins_id is not null and a.province_name is not null
        <if test="nodeFlag != null ">
            and a.node_flag in
            <foreach collection="nodeFlag" item="nodeFlag" open="(" separator="," close=")">
                #{nodeFlag}
            </foreach>
        </if>
        <if test="createTime != null and createTime != ''">
            <if test="timeType=='day'">
                <choose>
                    <when test="nodeName != null and nodeName != ''">
                        and exists (select 1 from ACT_HI_TASKINST b where a.process_ins_id = b.PROC_INST_ID_ and b.NAME_ = #{nodeName} and date_format(b.END_TIME_ ,'%Y-%m-%d')= #{createTime})
                    </when>
                    <otherwise>
                        and DATE_FORMAT(a.create_time,'%Y-%m-%d')=#{createTime}
                    </otherwise>
                </choose>

            </if>
            <if test="timeType=='month'">
                <choose>
                    <when test="nodeName != null and nodeName != ''">
                        and exists (select 1 from ACT_HI_TASKINST b where a.process_ins_id = b.PROC_INST_ID_ and b.NAME_ = #{nodeName} and date_format(b.END_TIME_ ,'%Y-%m')= #{createTime})
                    </when>
                    <otherwise>
                        and DATE_FORMAT(a.create_time,'%Y-%m')=#{createTime}
                    </otherwise>
                </choose>
            </if>
        </if>
        <if test="oemId != null and oemId != ''">
            and a.oem_id=#{oemId}
        </if>
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')&lt;= #{endTime}
        </if>
        GROUP BY a.province_name
    </select>

    <!--地市订单总量-->
    <select id="selectAreaOrderCount" resultType="java.util.HashMap">


        <choose>
            <when test='provinceId == "20" or provinceId == "42" or provinceId == "25" or provinceId == "31" '>
                select a.city_name as regionName ,count(*) as orderCount from t_order a where  a.type=1 and (a.node_flag!=199 and a.status=64)=false and a.process_ins_id is not null and a.city_name is not null and a.province_id=#{provinceId}
                <if test="nodeFlag != null ">
                    and a.node_flag in
                    <foreach collection="nodeFlag" item="nodeFlag" open="(" separator="," close=")">
                        #{nodeFlag}
                    </foreach>
                </if>
                <if test="createTime != null and createTime != ''">
                    <if test="timeType=='day'">
                        <choose>
                            <when test="nodeName != null and nodeName != ''">
                                and exists (select 1 from ACT_HI_TASKINST b where a.process_ins_id = b.PROC_INST_ID_ and b.NAME_ = #{nodeName} and date_format(b.END_TIME_ ,'%Y-%m-%d')= #{createTime})
                            </when>
                            <otherwise>
                                and DATE_FORMAT(a.create_time,'%Y-%m-%d')=#{createTime}
                            </otherwise>
                        </choose>

                    </if>
                    <if test="timeType=='month'">
                        <choose>
                            <when test="nodeName != null and nodeName != ''">
                                and exists (select 1 from ACT_HI_TASKINST b where a.process_ins_id = b.PROC_INST_ID_ and b.NAME_ = #{nodeName} and date_format(b.END_TIME_ ,'%Y-%m')= #{createTime})
                            </when>
                            <otherwise>
                                and DATE_FORMAT(a.create_time,'%Y-%m')=#{createTime}
                            </otherwise>
                        </choose>
                    </if>
                </if>
                <if test="oemId != null and oemId != ''">
                    and a.oem_id=#{oemId}
                </if>
                <if test="startTime != null and startTime != ''">
                    and DATE_FORMAT(create_time,'%Y-%m-%d')&gt;= #{startTime}
                </if>
                <if test="endTime != null and endTime != ''">
                    and DATE_FORMAT(create_time,'%Y-%m-%d')&lt;= #{endTime}
                </if>
                GROUP BY a.city_name
            </when>
            <otherwise>
                select a.region_name as regionName ,count(*) as orderCount from t_order a where  a.type=1 and (a.node_flag!=199 and a.status=64)=false and a.process_ins_id is not null and a.region_name is not null and a.province_id=#{provinceId}
                <if test="nodeFlag != null ">
                    and a.node_flag in
                    <foreach collection="nodeFlag" item="nodeFlag" open="(" separator="," close=")">
                        #{nodeFlag}
                    </foreach>
                </if>
                <if test="createTime != null and createTime != ''">
                    <if test="timeType=='day'">
                        <choose>
                            <when test="nodeName != null and nodeName != ''">
                                and exists (select 1 from ACT_HI_TASKINST b where a.process_ins_id = b.PROC_INST_ID_ and b.NAME_ = #{nodeName} and date_format(b.END_TIME_ ,'%Y-%m-%d')= #{createTime})
                            </when>
                            <otherwise>
                                and DATE_FORMAT(a.create_time,'%Y-%m-%d')=#{createTime}
                            </otherwise>
                        </choose>

                    </if>
                    <if test="timeType=='month'">
                        <choose>
                            <when test="nodeName != null and nodeName != ''">
                                and exists (select 1 from ACT_HI_TASKINST b where a.process_ins_id = b.PROC_INST_ID_ and b.NAME_ = #{nodeName} and date_format(b.END_TIME_ ,'%Y-%m')= #{createTime})
                            </when>
                            <otherwise>
                                and DATE_FORMAT(a.create_time,'%Y-%m')=#{createTime}
                            </otherwise>
                        </choose>
                    </if>
                </if>
                <if test="oemId != null and oemId != ''">
                    and a.oem_id=#{oemId}
                </if>
                <if test="startTime != null and startTime != ''">
                    and DATE_FORMAT(create_time,'%Y-%m-%d')&gt;= #{startTime}
                </if>
                <if test="endTime != null and endTime != ''">
                    and DATE_FORMAT(create_time,'%Y-%m-%d')&lt;= #{endTime}
                </if>
                GROUP BY a.region_name
            </otherwise>

        </choose>



    </select>

    <!--月度订单统计-->
    <select id="selectMonthOrderQuantity" resultType="java.util.HashMap">

        select date_format(create_time,'%Y-%m') as time,count(*) as orderCount

            FROM t_order

        where  type=1 and process_ins_id is not null

                and date_format(create_time,'%Y-%m')&gt;=#{startTime}

                and date_format(create_time,'%Y-%m')&lt;=#{endTime}

            <if test="nodeFlag != null ">
                and node_flag in
                <foreach collection="nodeFlag" item="nodeFlag" open="(" separator="," close=")">
                    #{nodeFlag}
                </foreach>
            </if>

            <if test="oemId != null and oemId != ''">
                and oem_id=#{oemId}
            </if>

        GROUP BY  date_format(create_time,'%Y-%m')

    </select>

    <!--查询节点信息-->
    <select id="getTaskInstByInstIdAndName" resultType="com.boco.nbd.wios.flow.entity.bo.TaskInstBO">

           select
                ID_ id,PROC_INST_ID_ procInstId,NAME_ name,START_TIME_ startTime,CLAIM_TIME_ claimTime,END_TIME_ endTime,DUE_DATE_ dueDate
           from ACT_HI_TASKINST

           <where>
               <if test="procInstId != null and procInstId != ''">
                   and  PROC_INST_ID_= #{procInstId}
               </if>
               <if test="nodeName != null and nodeName != ''">
                   and  NAME_ = #{nodeName}
               </if>
           </where>

           order by date_format(START_TIME_,'%Y-%m-%d %H:%i:%s') desc


    </select>


    <resultMap id="ScoreSupplierMap" type="java.util.HashMap">
        <result column="name" property="key" javaType="java.lang.String"/>
        <result column="num" property="value" javaType="java.lang.Integer"/>
    </resultMap>
    <!--根据服务商查询评分-->
    <select id="getScoreSupplierBySupplier" resultMap="ScoreSupplierMap">
        SELECT
            supplier_name name,
            FORMAT( AVG( final_score ), 2 )  num
        FROM
            t_score_supplier_v2
        <where>
            <if test="time != null and time != ''">
                and month = #{time}
            </if>
        </where>
        GROUP BY
            supplier_name

    </select>

    <!--根据区域查询评分-->
    <select id="getScoreSupplierByRegion" resultMap="ScoreSupplierMap">
        SELECT
            region_name name,
            FORMAT( AVG( final_score ), 2 ) num
        FROM
            t_score_supplier_v2
        <where>
            <if test="time != null and time != ''">
                and month = #{time}
            </if>
        </where>
        GROUP BY
	        region_name

    </select>


    <!--不满意订单数量统计-->
    <select id="getDissatisfiedOrderCount" resultType="java.lang.Integer">
        SELECT
        SUM( c.num ) num
        FROM
        (
            SELECT
                count(*) num
            FROM
                t_order a
                INNER JOIN t_order_score b ON a.id = b.order_id
                INNER JOIN ACT_HI_TASKINST d ON a.process_ins_id=d.PROC_INST_ID_
            WHERE
                a.type = 1
                AND a.process_ins_id IS NOT NULL
                AND ( a.node_flag != 199 AND a.STATUS = 64 )= FALSE
                AND ( a.know_after_sales_channel &lt;&gt; 0 OR a.know_after_sales_channel IS NULL )
                AND ( a.visit_content NOT IN ( 'APP', '预约充电', '充电慢', '墙盒功率' ) OR a.visit_content IS NULL )
                AND b.NAME = '充电评价'
                AND b.score &lt; 60
                AND a.node_flag &gt; 112
                AND left(d.NAME_,3) = '113'
                <if test="time != null and time != ''">
                AND DATE_FORMAT(d.START_TIME_,'%Y-%m') = #{time}
                </if>

        UNION ALL
            SELECT
                count(*) num
            FROM
                t_order a
                INNER JOIN ACT_HI_TASKINST d ON a.process_ins_id=d.PROC_INST_ID_
            WHERE
                a.type = 1
                AND a.process_ins_id IS NOT NULL
                AND ( a.node_flag != 199 AND a.STATUS = 64 )= FALSE
                AND a.know_after_sales_channel = 0
                AND a.node_flag &gt; 112
                AND left(d.NAME_,3) = '113'
                <if test="time != null and time != ''">
                AND DATE_FORMAT(d.START_TIME_,'%Y-%m') = #{time}
                </if>
        ) c

    </select>

    <!--安装审核通过数量-->
    <select id="getInstallationAuditPassedOrderCount" resultType="java.lang.Integer">
        SELECT
           count(*)
        FROM
            t_order a
            INNER JOIN ACT_HI_TASKINST d ON a.process_ins_id=d.PROC_INST_ID_
        WHERE
            a.type = 1
            AND a.process_ins_id IS NOT NULL
            AND ( a.node_flag != 199 AND a.STATUS = 64 )= FALSE
            AND a.node_flag > 112
            AND left(d.NAME_,3) = '113'
            <if test="time != null and time != ''">
                AND DATE_FORMAT(d.START_TIME_,'%Y-%m') = #{time}
            </if>
    </select>






</mapper>