<?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.repair.mapper.RepairRecordMapper">

    <resultMap type="com.repair.model.vo.RepairRecordCountVo" id="repairRecordCountMap">
        <result property="Incomplete" column="Incomplete"/>
        <result property="Confirmed" column="Confirmed"/>
        <result property="Finished" column="Finished"/>
    </resultMap>

    <select id="queryRepairRecordCount" resultType="com.repair.model.vo.RepairRecordCountVo"
            resultMap="repairRecordCountMap">
        SELECT COUNT(IF(maintenance_state &lt; 7 or maintenance_state = 11,1, NULL))
        Incomplete,COUNT(IF(maintenance_state=4,1, NULL))
        Confirmed,COUNT(IF(maintenance_state &gt;=7 and maintenance_state &lt;= 10 ,1, NULL)) Finished FROM
        `repair_record`
        <where>
            and deleted = 0
            <if test="userId != null and userId != ''">
                and user_id = #{userId}
            </if>
        </where>
    </select>

    <resultMap type="com.repair.model.vo.MyRepairRecordVo" id="repairRecordOwnMap">
        <result property="id" column="id"/>
        <result property="recordNo" column="record_no"/>
        <result property="reportTime" column="report_time"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="areaName" column="area_name"/>
        <result property="address" column="address"/>
        <result property="articleName" column="article_name"/>
        <result property="descript" column="description"/>
        <result property="image" column="image"/>
        <result property="stateId" column="state_id"/>
        <result property="price" column="price"/>
        <result property="maintenancerName" column="maintenancer_name"/>
        <result property="maintenancerPhone" column="maintenancer_phone"/>
        <result property="timeoutDispStr" column="timeout_disp_str"/>
        <result property="timeoutReceStr" column="timeout_rece_str"/>
        <result property="voice" column="voice"/>
        <result property="voiceLength" column="voice_length"/>
        <result property="finishVoice" column="finish_voice"/>
        <result property="finishVoiceLength" column="finish_voice_length"/>
    </resultMap>

    <select id="queryMyRepairRecordList" resultType="com.repair.model.vo.MyRepairRecordVo"
            resultMap="repairRecordOwnMap">
        select
        case ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and
        rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;=
        rdr.receiving_time) and rr.maintenance_state &lt;= 7 and rr.is_duty = 0 when true then '超时未接'
        else null end as timeout_disp_str,
        case ((date_add(rdv.receiving_time, interval rdv.maintenance_assess_time minute) &lt;= NOW() and
        rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time
        minute) &lt;= rdv.finish_time) and rr.maintenance_state &lt;= 7 and rr.is_duty = 0 when true then '维修超时'
        else null end as timeout_rece_str,rr.user_class,
        rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.price,
        rr.maintenancer_name,rr.maintenancer_phone,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id
        as area_name,rr.address,rr.description,rat.name as article_name,rr.image,rr.maintenance_state as state_id from
        repair_record rr
        left join repair_article_type rat on rat.id = rr.article_id
        left join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.record_id = rr.id and rdr.is_lose_efficacy = 0
        left join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0
        <where>
            and rr.deleted = 0
            <if test="stateStr != null and stateStr != ''">
                and (rr.maintenance_state ${stateStr}
            </if>
            <if test="userId != null and userId != ''">
                and rr.user_id = #{userId}
            </if>
        </where>
        GROUP BY rr.id
        order by rr.report_time DESC
    </select>

    <resultMap type="com.repair.model.vo.MaintenanceTechnicianVo" id="repairRecordTechnicianMap">
        <result property="receivingCount" column="receiving_count"/>
        <result property="maintenanceCount" column="maintenance_count"/>
        <result property="timeoutCount" column="timeout_count"/>
        <result property="examineCount" column="examine_count"/>
    </resultMap>

    <select id="queryRepairTechnicianCount" resultType="com.repair.model.vo.MaintenanceTechnicianVo"
            resultMap="repairRecordTechnicianMap">
        select (select Count(*) from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id and rdr.receiving_time is null and rdr.users_id = #{userId}
        where rr.deleted = 0 and rr.school_id = #{schoolId} and rr.maintenance_state = 1) as receiving_count ,
        (select Count(*) from repair_record rr2
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.user_id = #{userId} and rdv.record_id = rr2.id and rdv.is_lose_efficacy = 0
        where rr2.maintenance_state = 2 and rr2.school_id = #{schoolId} and rr2.deleted = 0) as maintenance_count,
        (select Count(*) as cs from (select rr3.*
        from repair_record rr3
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.users_id = #{userId} and rdr.record_id = rr3.id and
        rdr.is_lose_efficacy = 0
        and ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= #{dateNow} and rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= rdr.receiving_time)
        where rr3.is_duty = 0 and rr3.school_id = #{schoolId} and rr3.maintenance_state &lt;= 7 and rr3.deleted = 0
        UNION
        select rr4.* from repair_record rr4
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.user_id = #{userId} and rdv.record_id = rr4.id and
        rdv.is_lose_efficacy = 0
        and ((date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= #{dateNow} and rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= rdv.finish_time)
        where rr4.is_duty = 0 and rr4.school_id = #{schoolId} and rr4.maintenance_state &lt;= 7 and rr4.deleted = 0) c) as timeout_count,
        (select Count(*) as sh from (
        select rr5.* from repair_record rr5
        inner join repair_collaborate_record rcr on rcr.deleted = 0 and rcr.user_id = #{userId} and rcr.record_id = rr5.id and
        rcr.approval_statu = 2
        where rr5.maintenance_state = 6 and rr5.school_id = #{schoolId} and rr5.deleted = 0
        UNION ALL
        select rr.* from repair_record rr
        inner join repair_transfer_record rtr on rtr.deleted = 0 and rtr.user_id = #{userId} and rtr.record_id = rr.id and
        rtr.approval_statu = 2
        where rr.maintenance_state = 5 and rr.school_id = #{schoolId} and rr.deleted = 0
        UNION ALL
        select rr.* from repair_record rr
        inner join repair_offline_record ror on ror.deleted = 0 and ror.user_id = #{userId} and ror.record_id = rr.id and
        ror.approval_statu = 2
        where rr.maintenance_state = 11 and rr.school_id = #{schoolId} and rr.deleted = 0
        ) a) as examine_count
    </select>

    <resultMap type="com.repair.model.vo.RepairTechnicianVo" id="repairRecordTechnicianDataMap">
        <result property="id" column="id"/>
        <result property="recordNo" column="record_no"/>
        <result property="reportTime" column="report_time"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="areaName" column="area_name"/>
        <result property="address" column="address"/>
        <result property="articleName" column="article_name"/>
        <result property="description" column="description"/>
        <result property="image" column="image"/>
        <result property="state" column="state"/>
        <result property="maintenanceState" column="maintenance_state"/>
        <result property="timeoutStr" column="timeout_str"/>
        <result property="maintenanceTimeout" column="maintenance_timeout"/>
        <result property="maintenancerName" column="maintenancer_name"/>
        <result property="maintenancerPhone" column="maintenancer_phone"/>
        <result property="voice" column="voice"/>
        <result property="voiceLength" column="voice_length"/>
        <result property="finishVoice" column="finish_voice"/>
        <result property="finishVoiceLength" column="finish_voice_length"/>
        <result property="price" column="price"/>
    </resultMap>

    <!--待接单-->
    <select id="queryPendingOrderList" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        select t.*,ru.user_name as maintenancer_name,ru.user_phone as maintenancer_phone from (
        select case ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= #{dateNow} and rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= rdr.receiving_time) and rr.is_duty = 0 when true then '超时未接' else null end as timeout_str,
        rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name,rr.description,rr.image,'待接单' as state,rr.price,
        rr.user_class
        from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.users_id = #{userId} and rdr.record_id = rr.id and rdr.is_lose_efficacy = 0 and rdr.receiving_time is null
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.maintenance_state = 1 and rr.deleted = 0 and rr.school_id = #{schoolId}
        ) t
		left join repair_user ru on ru.id = #{userId}
		order by t.report_time DESC
    </select>

    <!--维修中-->
    <select id="queryRepairOrderList" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        select t.*,ru.user_name as maintenancer_name,ru.user_phone as maintenancer_phone from (
        select case ((date_add(rdv.receiving_time, interval rdv.maintenance_assess_time minute) &lt;= #{dateNow} and rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= rdv.finish_time) and rr.is_duty = 0 when true then '维修超时' else null end as timeout_str,
        rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'维修中' as state,rr.price,
        rr.user_class
        from repair_record rr
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.user_id = #{userId} and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.maintenance_state = 2 and rr.deleted = 0 and rr.school_id = #{schoolId}
        ) t
		left join repair_user ru on ru.id = #{userId}
		order by t.report_time DESC
    </select>

    <!--已超时-->
    <select id="queryTimeoutOrderList" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        SELECT case ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= rdr.receiving_time) when true then '超时未接' else null end as timeout_str,case ((date_add(rdv.receiving_time, interval rdv.maintenance_assess_time minute) &lt;= NOW() and rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= rdv.finish_time) when true then '维修超时' else null end as maintenance_timeout,rat.name as article_name,t.* FROM (
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.users_id = #{userId} and rdr.record_id = rr.id and rdr.is_lose_efficacy = 0
        and ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= rdr.receiving_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.school_id = #{schoolId} and rr.deleted = 0
        UNION
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.user_id = #{userId} and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0
        and ((date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= NOW() and rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= rdv.finish_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.school_id = #{schoolId} and rr.deleted = 0) t
        left join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.users_id = #{userId} and rdr.record_id = t.id and rdr.is_lose_efficacy = 0
        left join repair_receiving rdv on rdv.deleted = 0 and rdv.user_id = #{userId} and rdv.record_id = t.id and rdv.is_lose_efficacy = 0
        left join repair_article_type rat on rat.deleted = 0 and rat.id = t.article_id
        order by t.report_time desc
    </select>


    <!--待审核-->
    <select id="queryExamineOrderList" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'协作待审核' as state,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        inner join repair_collaborate_record rcr on rcr.deleted = 0 and rcr.user_id = #{userId} and rcr.record_id = rr.id and rcr.approval_statu = 2
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.maintenance_state = 6 and rr.deleted = 0 and rr.school_id = #{schoolId}
        UNION ALL
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'转单待审核' as state,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        inner join repair_transfer_record rtr on rtr.deleted = 0 and rtr.user_id = #{userId} and rtr.record_id = rr.id and rtr.approval_statu = 2
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.maintenance_state = 5 and rr.deleted = 0 and rr.school_id = #{schoolId}
        UNION ALL
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'转线下待审核' as state,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        inner join repair_offline_record ror on ror.deleted = 0 and ror.user_id = #{userId} and ror.record_id = rr.id and ror.approval_statu = 2
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.maintenance_state = 11 and rr.deleted = 0 and rr.school_id = #{schoolId}
    </select>

    <resultMap type="com.repair.model.vo.RepairManageCountVo" id="repairManageCountMap">
        <result property="dispatchCount" column="dispatch_count"/>
        <result property="maintenanceCount" column="maintenance_count"/>
        <result property="logisticsCount" column="logistics_count"/>
        <result property="examineCount" column="examine_count"/>
        <result property="finishedCount" column="finished_count"/>
        <result property="canceledCount" column="canceled_count"/>
        <result property="closedCount" column="closed_count"/>
        <result property="timeoutCount" column="timeout_count"/>
        <result property="offlineCount" column="offline_count"/>
    </resultMap>

    <select id="queryManageCount" resultType="com.repair.model.vo.RepairManageCountVo" resultMap="repairManageCountMap">
        select
        (select Count(*) from (select rdr.users_id from repair_record rr
        left join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id
        and rr.maintenance_state = 1
        where rr.deleted = 0 and rr.maintenance_state = 1
        <if test="keyWord != null and keyWord != ''">
            and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
            #{keyWord} '%' or rr.user_phone like '%'
            #{keyWord} '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        <if test="schoolId != null and schoolId != ''">
            and rr.school_id = #{schoolId}
        </if>
        ) t
        <if test="maintenanceName != null and maintenanceName != ''">
            where t.users_id in (select id from repair_user where user_name like '%' #{maintenanceName} '%' and deleted
            = 0)
        </if>) as dispatch_count,
        (select Count(*) from (select rdv.user_id from repair_record rr
        left join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0 and
        rr.maintenance_state = 2
        where rr.deleted = 0 and rr.maintenance_state = 2
        <if test="keyWord != null and keyWord != ''">
            and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
            #{keyWord} '%' or rr.user_phone like '%'
            #{keyWord} '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        <if test="schoolId != null and schoolId != ''">
            and rr.school_id = #{schoolId}
        </if>
        ) t
        <if test="maintenanceName != null and maintenanceName != ''">
            where t.user_id in (select id from repair_user where user_name like '%' #{maintenanceName} '%' and deleted =
            0)
        </if>) as maintenance_count,
        (select Count(*) from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id
        <if test="maintenanceName != null and maintenanceName != ''">
            and rdr.users_id in (select id from repair_user where user_name like '%' #{maintenanceName} '%' and deleted
            = 0)
        </if>
        where rr.deleted = 0 and rr.maintenance_state = 3
        <if test="keyWord != null and keyWord != ''">
            and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
            #{keyWord} '%' or rr.user_phone like '%' #{keyWord}
            '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        <if test="schoolId != null and schoolId != ''">
            and rr.school_id = #{schoolId}
        </if>
        ) as logistics_count,
        (select Count(*) from repair_record where deleted = 0 and (maintenance_state = 5 or maintenance_state = 6 or
        maintenance_state = 11)
        <if test="keyWord != null and keyWord != ''">
            and (area_id like '%' #{keyWord} '%' or user_name like '%' #{keyWord} '%' or user_phone like '%' #{keyWord}
            '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and report_time between #{startTime} and #{endTime}
        </if>
        <if test="schoolId != null and schoolId != ''">
            and school_id = #{schoolId}
        </if>
        <if test="maintenanceName != null and maintenanceName != ''">
            and maintenancer_name like '%' #{maintenanceName} '%'
        </if>) as examine_count,
        (select Count(*) from repair_record rr
        where rr.deleted = 0 and rr.maintenance_state = 7
        <if test="keyWord != null and keyWord != ''">
            and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
            #{keyWord} '%' or rr.user_phone like '%' #{keyWord}
            '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        <if test="schoolId != null and schoolId != ''">
            and rr.school_id = #{schoolId}
        </if>
        <if test="maintenanceName != null and maintenanceName != ''">
            and rr.maintenancer_name like '%' #{maintenanceName} '%'
        </if>
        ) as finished_count,
        (select Count(*) from repair_record rr
        where rr.deleted = 0 and rr.maintenance_state = 8
        <if test="keyWord != null and keyWord != ''">
            and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
            #{keyWord} '%' or rr.user_phone like '%' #{keyWord}
            '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        <if test="schoolId != null and schoolId != ''">
            and rr.school_id = #{schoolId}
        </if>
        <if test="maintenanceName != null and maintenanceName != ''">
            and rr.maintenancer_name like '%' #{maintenanceName} '%'
        </if>
        ) as canceled_count,
        (select Count(*) from repair_record rr
        where rr.deleted = 0 and rr.maintenance_state = 9
        <if test="keyWord != null and keyWord != ''">
            and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
            #{keyWord} '%' or rr.user_phone like '%' #{keyWord}
            '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        <if test="schoolId != null and schoolId != ''">
            and rr.school_id = #{schoolId}
        </if>
        <if test="maintenanceName != null and maintenanceName != ''">
            and rr.maintenancer_name like '%' #{maintenanceName} '%'
        </if>) as closed_count,
        (SELECT count(*) as cs FROM (
        select rdr.users_id as
        user_id,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id
        as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price from
        repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.record_id = rr.id and rdr.is_lose_efficacy = 0
        and ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and
        rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;=
        rdr.receiving_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.deleted = 0
        <if test="keyWord != null and keyWord != ''">
            and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
            #{keyWord} '%' or rr.user_phone like '%'
            #{keyWord} '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        <if test="schoolId != null and schoolId != ''">
            and rr.school_id = #{schoolId}
        </if>
        UNION
        select
        rdv.user_id,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id
        as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price from
        repair_record rr
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0
        and ((date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= NOW() and
        rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time
        minute) &lt;= rdv.finish_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.deleted = 0
        <if test="keyWord != null and keyWord != ''">
            and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
            #{keyWord} '%' or rr.user_phone like '%'
            #{keyWord} '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        <if test="schoolId != null and schoolId != ''">
            and rr.school_id = #{schoolId}
        </if>
        ) t
        <if test="maintenanceName != null and maintenanceName != ''">
            where t.user_id in (select id from repair_user where user_name like '%' #{maintenanceName} '%' and deleted =
            0)
        </if>
        ) as timeout_count,
        (select Count(*) from repair_record rr
        where rr.deleted = 0 and rr.maintenance_state = 10
        <if test="keyWord != null and keyWord != ''">
            and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
            #{keyWord} '%' or rr.user_phone like '%' #{keyWord}
            '%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        <if test="schoolId != null and schoolId != ''">
            and rr.school_id = #{schoolId}
        </if>
        <if test="maintenanceName != null and maintenanceName != ''">
            and rr.maintenancer_name like '%' #{maintenanceName} '%'
        </if>
        ) as offline_count
    </select>

    <!--管理端工单分页数据-->
    <resultMap type="com.repair.model.vo.RepairRecordPageVo" id="repairRecordPageMap">
        <result property="id" column="id"/>
        <result property="recordNo" column="record_no"/>
        <result property="schoolId" column="school_id"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="articleName" column="article_name"/>
        <result property="areaName" column="area_name"/>
        <result property="description" column="description"/>
        <result property="reportTime" column="report_time"/>
        <result property="maintenancerName" column="maintenancer_name"/>
        <result property="details" column="details"/>
        <result property="rdvName" column="rdvName"/>
        <result property="rdrName" column="rdrName"/>
        <result property="rorName" column="rorName"/>
        <result property="userId" column="user_id"/>
    </resultMap>

    <!--除已超时外的其他数据获取方式-->
    <select id="queryNotTimeout" resultType="com.repair.model.vo.RepairRecordPageVo" resultMap="repairRecordPageMap">
        select * from (select rr.school_id,rr.user_id,1 as details,rr.id,rr.record_no,rr.area_id as
        area_name,rr.user_name,rr.user_phone,rat.name as
        article_name,
        rr.description,rr.report_time,rr.maintenancer_name,rr.maintenance_state,(select user_name from repair_user where
        id = rdv.user_id and deleted = 0) as rdvName,(select user_name from repair_user where id = rdr.users_id and
        deleted = 0) as rdrName,(select user_name from repair_user where id = ror.user_id and
        deleted = 0) as rorName
        from repair_record rr
        left join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0 and
        rdv.finish_time is null and rr.maintenance_state = 2
        left join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id
        and rdr.receiving_time is null and rr.maintenance_state = 1
        left join repair_offline_record ror on ror.deleted = 0 and ror.record_id = rr.id and ror.approval_statu != 0
        <if test="stateStr != null and stateStr != ''">
            and (rr.maintenance_state ${stateStr}
        </if>
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        <where>
            and rr.deleted = 0
            <if test="schoolId != null and schoolId != ''">
                and rr.school_id = #{schoolId}
            </if>
            <if test="stateStr != null and stateStr != ''">
                and (rr.maintenance_state ${stateStr}
            </if>
            <if test="stateStr == null or stateStr == ''">
                and rr.maintenance_state != 4
            </if>
            <if test="keyWord != null and keyWord != ''">
                and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
                #{keyWord} '%' or rr.user_phone like
                '%' #{keyWord} '%')
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr.report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) t
        <if test="maintenanceName != null and maintenanceName != ''">
            where t.maintenancer_name like '%' #{maintenanceName} '%' or t.rdvName like '%' #{maintenanceName} '%' or
            t.rdrName like '%' #{maintenanceName} '%'
        </if>
        order by t.report_time desc
    </select>

    <resultMap type="com.repair.model.vo.RepairRecordTimeoutPageVo" id="repairRecordTimeoutPageMap">
        <result property="id" column="id"/>
        <result property="recordNo" column="record_no"/>
        <result property="schoolId" column="school_id"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="articleName" column="article_name"/>
        <result property="areaName" column="area_name"/>
        <result property="description" column="description"/>
        <result property="reportTime" column="report_time"/>
        <result property="maintenancerName" column="maintenancer_name"/>
        <result property="details" column="details"/>
        <result property="maintenanceState" column="maintenance_state"/>
        <result property="timeoutStr" column="timeout_str"/>
        <result property="maintenanceTimeout" column="maintenance_timeout"/>
        <result property="userId" column="user_id"/>
    </resultMap>

    <!--#已超时获取方式-->
    <select id="queryTimeout" resultType="com.repair.model.vo.RepairRecordTimeoutPageVo"
            resultMap="repairRecordTimeoutPageMap">
        select * from (select
        t.school_id,t.user_id,t.details,t.id,t.record_no,t.area_name,t.user_name,t.user_phone,rat.name as
        article_name,t.description,t.report_time,t.maintenance_state,ru.user_name as maintenancer_name,ru.user_phone as
        maintenancer_phone,
        case ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and
        rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;=
        rdr.receiving_time) when true then '超时未接' else null end as timeout_str,
        case ((date_add(rdv.receiving_time, interval rdv.maintenance_assess_time minute) &lt;= NOW() and
        rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time
        minute) &lt;= rdv.finish_time) when true then '维修超时' else null end as maintenance_timeout
        FROM (
        select rr.school_id,rdr.users_id as user_id,1 as details,rr.id,rr.record_no,rr.area_id as
        area_name,rr.user_name,rr.user_phone,rr.price,rr.maintenance_state,rr.article_id,rr.report_time,rr.description
        from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.record_id = rr.id and rdr.is_lose_efficacy = 0
        and ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and
        rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;=
        rdr.receiving_time)
        <where>
            and rr.deleted = 0 and rr.is_duty = 0 and rr.maintenance_state &lt;= 7
            <if test="keyWord != null and keyWord != ''">
                and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
                #{keyWord} '%' or rr.user_phone like
                '%' #{keyWord} '%')
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr.report_time between #{startTime} and #{endTime}
            </if>
            <if test="schoolId != null and schoolId != ''">
                and rr.school_id = #{schoolId}
            </if>
        </where>
        UNION
        select rr.school_id,rdv.user_id,1 as details,rr.id,rr.record_no,rr.area_id as
        area_name,rr.user_name,rr.user_phone,rr.price,rr.maintenance_state,rr.article_id,rr.report_time,rr.description
        from repair_record rr
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0
        and ((date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= NOW() and
        rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time
        minute) &lt;= rdv.finish_time)
        <where>
            and rr.deleted = 0 and rr.is_duty = 0 and rr.maintenance_state &lt;= 7
            <if test="keyWord != null and keyWord != ''">
                and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
                #{keyWord} '%' or rr.user_phone like
                '%' #{keyWord} '%')
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr.report_time between #{startTime} and #{endTime}
            </if>
            <if test="schoolId != null and schoolId != ''">
                and rr.school_id = #{schoolId}
            </if>
        </where>
        ) t
        left join repair_dispatch_record rdr on rdr.users_id =t.user_id and rdr.deleted = 0 and rdr.record_id = t.id and
        rdr.is_lose_efficacy = 0
        left join repair_receiving rdv on rdv.user_id =t.user_id and rdv.deleted = 0 and rdv.record_id = t.id and
        rdv.is_lose_efficacy = 0
        left join repair_article_type rat on rat.deleted = 0 and rat.id = t.article_id
        left join repair_user ru on ru.deleted = 0 and ru.id = t.user_id) d
        <if test="maintenanceName != null and maintenanceName != ''">
            where d.maintenancer_name like '%' #{maintenanceName} '%'
        </if>
        order by d.report_time desc
    </select>


    <!--#获取待处理池数据-->
    <resultMap type="com.repair.model.vo.pendingOrderVo" id="repairPendingOrderMap">
        <result property="id" column="id"/>
        <result property="recordNo" column="record_no"/>
        <result property="reportTime" column="report_time"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="areaName" column="area_name"/>
        <result property="address" column="address"/>
        <result property="articleName" column="article_name"/>
        <result property="description" column="description"/>
        <result property="image" column="image"/>
        <result property="state" column="state"/>
        <result property="voice" column="voice"/>
        <result property="voiceLength" column="voice_length"/>
        <result property="finishVoice" column="finish_voice"/>
        <result property="finishVoiceLength" column="finish_voice_length"/>
    </resultMap>

    <select id="queryPendingOrder" resultType="com.repair.model.vo.pendingOrderVo" resultMap="repairPendingOrderMap">
        select * from (select (select Count(*) from repair_dispatch_record where is_lose_efficacy = 0 and record_id = rr.id and receiving_time is null) as disp_count,rr.id,rr.record_no,
        rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name,rr.description,rr.image,'待接单' as state
        from repair_record rr
        left join repair_article_type rat on rat.id = rr.article_id
        where rr.maintenance_state = 1 and rr.deleted = 0 and rr.school_id = #{schoolId}) t where t.disp_count &lt;= 0
        order by t.report_time desc
    </select>


    <!--郭师傅维修 团队工单 头部数量-->
    <resultMap type="com.repair.model.vo.TeamWorkCountVo" id="teamWorkOrderMap">
        <result property="examineCount" column="examine_count"/>
        <result property="receivingCount" column="receiving_count"/>
        <result property="maintenanceCount" column="maintenance_count"/>
        <result property="finishedCount" column="finished_count"/>
        <result property="logisticsCount" column="logistics_count"/>
        <result property="timeoutCount" column="timeout_count"/>
        <result property="closedCount" column="closed_count"/>
        <result property="offlineCount" column="offline_count"/>
    </resultMap>

    <select id="queryTeamWorkCount" resultType="com.repair.model.vo.TeamWorkCountVo" resultMap="teamWorkOrderMap">
        select
        (select Count(*) as sh from (
        select rr5.* from repair_record rr5
        inner join repair_collaborate_record rcr on rcr.deleted = 0 and rcr.record_id = rr5.id and
        rcr.approval_statu = 2
        where rr5.maintenance_state = 6 and rr5.school_id = #{schoolId} and rr5.deleted = 0
        UNION ALL
        select rr.* from repair_record rr
        inner join repair_transfer_record rtr on rtr.deleted = 0 and rtr.record_id = rr.id and
        rtr.approval_statu = 2
        where rr.maintenance_state = 5 and rr.school_id = #{schoolId} and rr.deleted = 0
        UNION ALL
        select rr.* from repair_record rr
        inner join repair_offline_record ror on ror.deleted = 0 and ror.record_id = rr.id and ror.approval_statu = 2
        where rr.maintenance_state = 11 and rr.school_id = #{schoolId} and rr.deleted = 0
        ) a) as examine_count,
        (select Count(*) from repair_record rr1
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.record_id = rr1.id and
        rdr.is_lose_efficacy = 0 and rdr.receiving_time is null
        where rr1.maintenance_state = 1 and rr1.school_id = #{schoolId} and rr1.deleted = 0) as receiving_count,
        (select Count(*) from repair_record rr
        where rr.deleted = 0 and rr.school_id = #{schoolId}  and rr.maintenance_state = 10) as offline_count,
        (select Count(*) from repair_record rr2
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr2.id and
        rdv.is_lose_efficacy = 0
        where rr2.maintenance_state = 2 and rr2.school_id = #{schoolId} and rr2.deleted = 0) as maintenance_count,
        (select Count(*) from repair_record rr7
        where rr7.deleted = 0 and rr7.school_id = #{schoolId} and rr7.maintenance_state = 7) as finished_count,
        (select Count(*) from repair_record rr9
        where rr9.deleted = 0 and rr9.school_id = #{schoolId} and rr9.maintenance_state = 3) as logistics_count,
        (SELECT count(*) as cs FROM (
        select rdr.users_id as user_id,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price  from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.record_id = rr.id and rdr.is_lose_efficacy = 0
        and ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= rdr.receiving_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.deleted = 0 and rr.school_id = #{schoolId}
        UNION
        select rdv.user_id,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price  from repair_record rr
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0
        and ((date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= NOW() and rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= rdv.finish_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.deleted = 0 and rr.school_id = #{schoolId}) t) as timeout_count,
        (select Count(*) from repair_record rr8
        where rr8.deleted = 0 and rr8.school_id = #{schoolId} and rr8.maintenance_state = 9) as closed_count
    </select>

    <!--待审核-->
    <select id="queryTeamWorkExamine" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        select rr5.id,rr5.record_no,rr5.report_time,rr5.user_name,rr5.user_phone,rr5.area_id as area_name,rr5.address,rat.name as article_name ,rr5.description,rr5.image,'协作待审核' as state,rr5.maintenancer_name,rr5.maintenancer_phone,rr5.price from repair_record rr5
        inner join repair_collaborate_record rcr on rcr.deleted = 0 and rcr.record_id = rr5.id and rcr.approval_statu = 2
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr5.article_id
        where rr5.maintenance_state = 6 and rr5.school_id = #{schoolId} and rr5.deleted = 0
        UNION ALL
        select rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'转单待审核' as state,rr.maintenancer_name,rr.maintenancer_phone,rr.price from repair_record rr
        inner join repair_transfer_record rtr on rtr.deleted = 0 and rtr.record_id = rr.id and rtr.approval_statu = 2
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.maintenance_state = 5 and rr.school_id = #{schoolId} and rr.deleted = 0
        UNION ALL
		select rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'转线下待审核' as state,rr.maintenancer_name,rr.maintenancer_phone,rr.price from repair_record rr
        inner join repair_offline_record ror on ror.deleted = 0 and ror.record_id = rr.id and ror.approval_statu = 2
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.maintenance_state = 11 and rr.school_id = #{schoolId} and rr.deleted = 0
    </select>

    <!--待接单-->
    <select id="queryTeamWorkPending" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        select t.*,ru.user_name as maintenancer_name,ru.user_phone as maintenancer_phone from
        (select case ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= #{dateNow} and rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= rdr.receiving_time) when true then '超时未接' else null end as timeout_str,
        rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name,rr.description,rr.image,'待接单' as state,rdr.users_id as rdrUId,rr.price,rr.user_class
        from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.record_id = rr.id and rdr.is_lose_efficacy = 0
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.maintenance_state = 1 and rr.school_id = #{schoolId} and rr.deleted = 0
        ) t
		left join repair_user ru on ru.id = t.rdrUId
		order by t.report_time DESC
    </select>

    <!--维修中-->
    <select id="queryTeamWorkMaintenance" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        select t.*,ru.user_name as maintenancer_name,ru.user_phone as maintenancer_phone from
        (select case ((date_add(rdv.receiving_time, interval rdv.maintenance_assess_time minute) &lt;= #{dateNow} and rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= rdv.finish_time) when true then '维修超时' else null end as timeout_str,
        rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'维修中' as state,rdv.user_id as rdrUId,rr.price,rr.user_class
        from repair_record rr
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.maintenance_state = 2 and rr.school_id = #{schoolId} and rr.deleted = 0
        ) t
		left join repair_user ru on ru.id = t.rdrUId
		order by t.report_time DESC
    </select>

    <!--已完成-->
    <select id="queryTeamWorkFinished" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'已完成' as state,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.deleted = 0 and rr.school_id = #{schoolId} and rr.maintenance_state = 7
        order by rr.report_time DESC
    </select>

    <!--待处理-->
    <select id="queryTeamWorkLogistics" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'待处理' as state,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.deleted = 0 and rr.school_id = #{schoolId} and rr.maintenance_state = 3
        order by rr.report_time DESC
    </select>

    <!--已超时-->
    <select id="queryTeamWorkTimeout" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        SELECT t.voice,t.voice_length,t.finish_voice,t.finish_voice_length,ru.user_name as maintenancer_name,ru.user_phone as maintenancer_phone,case ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= rdr.receiving_time) when true then '超时未接' else null end as timeout_str,case ((date_add(rdv.receiving_time, interval rdv.maintenance_assess_time minute) &lt;= NOW() and rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= rdv.finish_time) when true then '维修超时' else null end as maintenance_timeout,rat.name as article_name,
        t.id,t.maintenance_state,t.record_no,t.report_time,t.user_name,t.user_phone,t.area_name,t.address,t.description,t.image,t.price FROM (
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rdr.users_id as user_id,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.record_id = rr.id and rdr.is_lose_efficacy = 0
        and ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= rdr.receiving_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.deleted = 0 and rr.school_id = #{schoolId}
        UNION
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rdv.user_id,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0
        and ((date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= NOW() and rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= rdv.finish_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.deleted = 0 and rr.school_id = #{schoolId}) t
        left join repair_dispatch_record rdr on rdr.users_id =t.user_id and rdr.deleted = 0 and rdr.record_id = t.id and rdr.is_lose_efficacy = 0
        left join repair_receiving rdv on rdv.user_id =t.user_id and rdv.deleted = 0 and rdv.record_id = t.id and rdv.is_lose_efficacy = 0
        left join repair_article_type rat on rat.deleted = 0 and rat.id = t.article_id
        left join repair_user ru on ru.deleted = 0 and ru.id = t.user_id
        order by t.report_time desc
    </select>

    <!--已关单-->
    <select id="queryTeamWorkClosed" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'已关单' as state,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.deleted = 0 and rr.school_id = #{schoolId} and rr.maintenance_state = 9
        order by rr.report_time DESC
    </select>

    <!--转线下-->
    <select id="queryTeamWorkOffline" resultType="com.repair.model.vo.RepairTechnicianVo"
            resultMap="repairRecordTechnicianDataMap">
        select rr.voice,rr.voice_length,rr.finish_voice,rr.finish_voice_length,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rat.name as article_name ,rr.description,rr.image,'转线下' as state,rr.maintenancer_name,rr.maintenancer_phone,rr.price,rr.user_class
        from repair_record rr
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.deleted = 0 and rr.school_id = #{schoolId} and rr.maintenance_state = 10
        order by rr.report_time DESC
    </select>

    <!-- 报表导出获取除已超时外的其他数据获取方式-->
    <select id="queryNotTimeoutReportExport" resultType="com.repair.model.vo.ReportExportVo">
        select rr.user_id,1 as details,rr.id,rr.record_no,rr.area_id as area_name,rr.user_name,rr.user_phone,rat.name as
        article_name,
        rr.description,rr.report_time,rr.maintenancer_name,rr.maintenance_state
        from repair_record rr
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        <where>
            and rr.deleted = 0
            <if test="schoolId != null and schoolId != ''">
                and rr.school_id = #{schoolId}
            </if>
            <if test="stateStr != null and stateStr != ''">
                and (rr.maintenance_state ${stateStr}
            </if>
            <if test="keyWord != null and keyWord != ''">
                and (rr.record_no like '%' #{keyWord} '%' or rr.area_id like '%' #{keyWord} '%' or rr.user_name like '%'
                #{keyWord} '%' or rr.user_phone like
                '%' #{keyWord} '%')
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr.report_time between #{startTime} and #{endTime}
            </if>
        </where>
        order by rr.report_time desc
    </select>

    <!-- 报表导出获取除已超时数据获取方式-->
    <select id="queryTimeoutReportExport" resultType="com.repair.model.vo.ReportExportVo">
        SELECT * FROM (
        select rr3.user_id,1 as details,rr3.id,rr3.record_no,rr3.area_id as
        area_name,rr3.user_name,rr3.user_phone,rat.name as article_name,
        rr3.description,rr3.report_time,(select user_name from repair_user where id = rdr.users_id) as
        maintenancer_name,rr3.maintenance_state
        from repair_record rr3
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr3.article_id
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.record_id = rr3.id and
        rdr.is_lose_efficacy = 0
        and ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and
        rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;=
        rdr.receiving_time)
        <where>
            and rr3.deleted = 0 and rr3.is_duty = 0 and rr3.maintenance_state &lt;= 7
            <if test="keyWord != null and keyWord != ''">
                and (rr3.record_no like '%' #{keyWord} '%' or rr3.area_id like '%' #{keyWord} '%' or rr3.user_name like
                '%' #{keyWord} '%' or rr3.user_phone like
                '%' #{keyWord} '%')
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr3.report_time between #{startTime} and #{endTime}
            </if>
            <if test="schoolId != null and schoolId != ''">
                and rr3.school_id = #{schoolId}
            </if>
        </where>
        UNION ALL
        select rr4.user_id,1 as details,rr4.id,rr4.record_no,rr4.area_id as
        area_name,rr4.user_name,rr4.user_phone,rat.name as
        article_name,
        rr4.description,rr4.report_time,(select user_name from repair_user where id = rdv.user_id) as
        maintenancer_name,rr4.maintenance_state
        from repair_record rr4
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr4.article_id
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr4.id and
        rdv.is_lose_efficacy = 0
        and ((date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= NOW() and
        rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time
        minute) &lt;= rdv.finish_time)
        <where>
            and rr4.deleted = 0 and rr4.is_duty = 0 and rr4.maintenance_state &lt;= 7
            <if test="keyWord != null and keyWord != ''">
                and (rr4.record_no like '%' #{keyWord} '%' or rr4.area_id like '%' #{keyWord} '%' or rr4.user_name like
                '%' #{keyWord} '%' or rr4.user_phone like
                '%' #{keyWord} '%')
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr4.report_time between #{startTime} and #{endTime}
            </if>
            <if test="schoolId != null and schoolId != ''">
                and rr4.school_id = #{schoolId}
            </if>
        </where>
        ) t
        order by t.report_time desc
    </select>

    <!--维修师傅首页头部数量-->
    <resultMap type="com.repair.model.vo.MasterHomepageVo" id="masterHomepageMap">
        <result property="workCount" column="work_count"/>
        <result property="processCount" column="process_count"/>
        <result property="finishCount" column="finish_count"/>
        <result property="timeoutCount" column="timeout_count"/>
        <result property="offlineCount" column="offline_count"/>
    </resultMap>
    <!--说明：-->
    <!--#work_count 工单数：维修师傅的工单总数，只计算维修师傅的接单数，待接单不用计算进去-->
    <!--#process_count 处理中：维修中没有完成的工单，包含维修中，待接单，待审核-->
    <!--#finish_count 已完成：已取消 已完成 已关单-->
    <!--#timeout_count 已超时：包含接单超时 维修超时-->
    <select id="queryMasterHomeCount" resultType="com.repair.model.vo.MasterHomepageVo" resultMap="masterHomepageMap">
        select
        (select count(*) from repair_receiving rc
        inner join repair_record rr on rr.deleted = 0 and rr.school_id = #{schoolId} and rr.id = rc.record_id
        where rc.deleted = 0 and rc.user_id = #{userId}) as work_count,
        (select
        (select Count(*) as rc from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id and rdr.users_id = #{userId}
        where rr.deleted = 0 and rr.school_id = #{schoolId} and rr.maintenance_state = 1) +
        (select Count(*) as wx from repair_record rr2
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.user_id = #{userId} and rdv.record_id = rr2.id and
        rdv.is_lose_efficacy = 0
        where rr2.maintenance_state = 2 and rr2.school_id = #{schoolId} and rr2.deleted = 0) +
        (select Count(*) as sh from (
        select rr5.* from repair_record rr5
        inner join repair_collaborate_record rcr on rcr.deleted = 0 and rcr.user_id = #{userId} and rcr.record_id = rr5.id and
        rcr.approval_statu = 2
        where rr5.maintenance_state = 6 and rr5.deleted = 0 and rr5.school_id = #{schoolId}
        UNION ALL
        select rr.* from repair_record rr
        inner join repair_transfer_record rtr on rtr.deleted = 0 and rtr.user_id = #{userId} and rtr.record_id = rr.id and
        rtr.approval_statu = 2
        where rr.maintenance_state = 5 and rr.deleted = 0 and rr.school_id = #{schoolId}) a) as process_count) as process_count,
        (select Count(*) from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id and rdr.users_id = #{userId}
        where rr.deleted = 0 and rr.school_id = #{schoolId} and rr.maintenance_state >= 7) as finish_count,
        (select Count(*) as cs from (select rr3.*
        from repair_record rr3
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.users_id = #{userId} and rdr.record_id = rr3.id and
        rdr.is_lose_efficacy = 0
        and ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= rdr.receiving_time)
        where rr3.maintenance_state &lt;= 7 and rr3.is_duty = 0 and rr3.school_id = #{schoolId} and rr3.deleted = 0
        UNION
        select rr4.* from repair_record rr4
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.user_id = #{userId} and rdv.record_id = rr4.id and
        rdv.is_lose_efficacy = 0
        and ((date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= NOW() and rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= rdv.finish_time)
        where rr4.maintenance_state &lt;= 7 and rr4.is_duty = 0 and rr4.school_id = #{schoolId} and rr4.deleted = 0) c) as timeout_count
    </select>

    <!--后勤人员和郭师傅首页头部数量-->
    <!--说明：-->
    <!--#work_count 工单数：对应校区的工单总数，只计算对应人员校区的接单数，待接单不用计算进去-->
    <!--#process_count 处理中：维修中没有完成的工单，包含维修中，待接单，待审核-->
    <!--#finish_count 已完成：已取消 已完成 已关单-->
    <!--#timeout_count 已超时：包含接单超时 维修超时-->
    <select id="queryLogisticHomeCount" resultType="com.repair.model.vo.MasterHomepageVo" resultMap="masterHomepageMap">
        select
        (select Count(*) from repair_record rr
        where rr.deleted = 0 and rr.maintenance_state = 10) as offline_count,
        (select Count(*) from repair_receiving rc
        inner join repair_record rr on rr.deleted = 0 and rr.school_id = #{schoolId} and rr.id = rc.record_id
        where rc.deleted = 0) as work_count,
        (select
        (select Count(*) as rc from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id
        where rr.deleted = 0 and rr.maintenance_state = 1 and rr.school_id = #{schoolId}) +
        (select Count(*) as wx from repair_record rr2
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr2.id and
        rdv.is_lose_efficacy = 0
        where rr2.maintenance_state = 2 and rr2.deleted = 0 and rr2.school_id = #{schoolId}) +
        (select Count(*) as sh from (
        select rr5.* from repair_record rr5
        inner join repair_collaborate_record rcr on rcr.deleted = 0 and rcr.record_id = rr5.id and
        rcr.approval_statu = 2
        where rr5.maintenance_state = 6 and rr5.deleted = 0 and rr5.school_id = #{schoolId}
        UNION
        select rr.* from repair_record rr
        inner join repair_transfer_record rtr on rtr.deleted = 0 and rtr.record_id = rr.id and
        rtr.approval_statu = 2
        where rr.maintenance_state = 5 and rr.deleted = 0 and rr.school_id = #{schoolId}) a) as process_count) as process_count,
        (select Count(*) from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id
        where rr.deleted = 0 and rr.maintenance_state >= 7 and rr.school_id = #{schoolId}) as finish_count,
        (SELECT count(*) as cs FROM (
        select rdr.users_id as user_id,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price  from repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.record_id = rr.id and rdr.is_lose_efficacy = 0
        and ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= rdr.receiving_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.deleted = 0 and rr.school_id = #{schoolId}
        UNION
        select rdv.user_id,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price  from repair_record rr
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0
        and ((date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= NOW() and rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= rdv.finish_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.deleted = 0 and rr.school_id = #{schoolId}) t) as timeout_count
    </select>

    <resultMap type="com.repair.model.vo.RepairCampusRecordVo" id="repairCampusMap">
        <result property="id" column="id"/>
        <result property="price" column="price"/>
        <result property="schoolId" column="school_id"/>
        <result property="maintenanceState" column="maintenance_state"/>
    </resultMap>

    <select id="repairCampusTotal" resultType="com.repair.model.vo.RepairCampusRecordVo" resultMap="repairCampusMap">
        SELECT rr.id,rr.price,rr.school_id,rr.maintenance_state
        FROM `repair_record` rr
        left join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.receiving_time is null and rdr.record_id = rr.id and rr.maintenance_state = 1
        left join repair_receiving rre on rre.deleted = 0 and rre.is_lose_efficacy = 0 and rre.finish_time is null and rre.record_id = rr.id and rr.maintenance_state = 2
        where rr.deleted = 0
    </select>
    <select id="repairCampus" resultType="com.repair.model.vo.RepairCampusRecordVo" resultMap="repairCampusMap">
        SELECT rr.id,rr.price,rr.school_id,rr.maintenance_state
        FROM `repair_record` rr
        left join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.receiving_time is
        null and rdr.record_id = rr.id and rr.maintenance_state = 1
        left join repair_receiving rre on rre.deleted = 0 and rre.is_lose_efficacy = 0 and rre.finish_time is null and
        rre.record_id = rr.id and rr.maintenance_state = 2
        <where>
            and rr.deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr.report_time between #{startTime} and #{endTime}
            </if>
        </where>
    </select>

    <select id="repairAreaTotal" resultType="com.repair.model.vo.RepairAreaVo">
        SELECT row_number() over ( ORDER BY ra.area_name ) AS id, ra.*
        FROM (SELECT count(rr.area_id)   AS orders_total,
                     rs.`name`           AS school_name,
                     rr.area_id          AS area_name,
                     (SELECT COUNT(area_id)
                      FROM `repair_record`
                      WHERE price > 0
                        AND rr.area_id = area_id
                        AND deleted = 0) AS orders_compensation,
                     (SELECT COUNT(area_id)
                      FROM `repair_record`
                      WHERE (price IS NULL OR price = 0)
                        AND rr.area_id = area_id
                        AND deleted = 0) AS orders_gratis,
                     SUM(rr.price)       AS total_price
              FROM `repair_record` rr
                       LEFT JOIN repair_school rs ON rs.deleted = 0
                  AND rr.school_id = rs.id
              where rr.deleted = 0
              GROUP BY rr.area_id) ra
    </select>

    <select id="repairArea" resultType="com.repair.model.vo.RepairAreaVo">
        SELECT row_number() over ( ORDER BY ra.area_name ) AS id, ra.*
        FROM (SELECT count(rr.area_id) AS orders_total,
        rs.`name` AS school_name,
        rr.area_id AS area_name,
        (SELECT COUNT(area_id)
        FROM `repair_record`
        <where>
            AND price > 0
            AND rr.area_id = area_id
            AND deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) AS orders_compensation,
        (SELECT COUNT(area_id)
        FROM `repair_record`
        <where>
            AND (price IS NULL OR price = 0)
            AND rr.area_id = area_id
            AND deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) AS orders_gratis,
        SUM(rr.price) AS total_price
        FROM `repair_record` rr
        LEFT JOIN repair_school rs ON rs.deleted = 0
        AND rr.school_id = rs.id
        <where>
            AND rr.deleted=0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        GROUP BY rr.area_id) ra
    </select>
    <select id="maintenanceGoodsTotal" resultType="com.repair.model.vo.MaintenanceGoodsVo">
        SELECT row_number() over ( ORDER BY wx.article_name ) AS id, wx.*
        FROM (SELECT rat.`name`           AS article_name,
                     rat.school_id as school_id,
                     count(rr.article_id) AS orders_total,
                     (SELECT COUNT(article_id)
                      FROM `repair_record`
                      WHERE price > 0
                        AND rr.article_id = article_id
                        AND deleted = 0)  AS orders_compensation,
                     (SELECT COUNT(article_id)
                      FROM `repair_record`
                      WHERE (price IS NULL OR price = 0)
                        AND rr.article_id = article_id
                        AND deleted = 0)  AS orders_gratis,
                     SUM(rr.price)        AS total_price
              FROM `repair_record` rr
                       LEFT JOIN repair_article_type rat ON rr.article_id = rat.id
              WHERE rr.deleted = 0
              GROUP BY rr.article_id) wx
    </select>

    <select id="maintenanceGoods" resultType="com.repair.model.vo.MaintenanceGoodsVo">
        SELECT row_number() over ( ORDER BY wx.article_name ) AS id, wx.*
        FROM (SELECT rat.`name` AS article_name,
        rat.school_id as school_id,
        count(rr.article_id) AS orders_total,
        (SELECT COUNT(article_id)
        FROM `repair_record`
        <where>
            and price > 0 AND rr.article_id = article_id AND deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) AS orders_compensation,
        (SELECT COUNT(article_id)
        FROM `repair_record`
        <where>
            and (price IS NULL OR price = 0)
            AND rr.article_id = article_id
            AND deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) AS orders_gratis,
        SUM(rr.price) AS total_price
        FROM `repair_record` rr
        LEFT JOIN repair_article_type rat ON rr.article_id = rat.id
        <where>
            and rr.deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        GROUP BY rr.article_id) wx
    </select>
    <select id="repairAreaExport" resultType="com.repair.model.vo.RepairAreaVo">
        SELECT row_number() over ( ORDER BY ra.area_name ) AS id, ra.*
        FROM (SELECT count(rr.area_id) AS orders_total,
        rs.`name` AS school_name,
        rr.area_id AS area_name,
        (SELECT COUNT(area_id)
        FROM `repair_record`
        <where>
            AND price > 0
            AND rr.area_id = area_id
            AND deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) AS orders_compensation,
        (SELECT COUNT(area_id)
        FROM `repair_record`
        <where>
            AND (price IS NULL OR price = 0)
            AND rr.area_id = area_id
            AND deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) AS orders_gratis,
        SUM(rr.price) AS total_price
        FROM `repair_record` rr
        LEFT JOIN repair_school rs ON rs.deleted = 0
        AND rr.school_id = rs.id
        <where>
            AND rr.deleted=0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        GROUP BY rr.area_id) ra
    </select>

    <select id="maintenanceGoodsExport" resultType="com.repair.model.vo.MaintenanceGoodsVo">
        SELECT row_number() over ( ORDER BY wx.article_name ) AS id, wx.*
        FROM (SELECT rat.`name` AS article_name,
        rat.school_id as school_id,
        count(rr.article_id) AS orders_total,
        (SELECT COUNT(article_id)
        FROM `repair_record`
        <where>
            and price > 0 AND rr.article_id = article_id AND deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) AS orders_compensation,
        (SELECT COUNT(article_id)
        FROM `repair_record`
        <where>
            and (price IS NULL OR price = 0)
            AND rr.article_id = article_id
            AND deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) AS orders_gratis,
        SUM(rr.price) AS total_price
        FROM `repair_record` rr
        LEFT JOIN repair_article_type rat ON rat.deleted = 0
        AND rr.article_id = rat.id
        <where>
            and rr.deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        GROUP BY rr.article_id) wx
    </select>

    <!--报修详情-->
    <resultMap type="com.repair.model.vo.DetailsVo" id="detailDatasMap">
        <result property="id" column="id"/>
        <result property="maintenanceState" column="maintenance_state"/>
        <result property="recordNo" column="record_no"/>
        <result property="reportTime" column="report_time"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="userClass" column="user_class"/>
        <result property="areaName" column="area_name"/>
        <result property="address" column="address"/>
        <result property="articleName" column="article_name"/>
        <result property="description" column="description"/>
        <result property="voice" column="voice"/>
        <result property="voiceLength" column="voice_length"/>
        <result property="finishVoice" column="finish_voice"/>
        <result property="finishVoiceLength" column="finish_voice_length"/>
        <result property="image" column="image"/>
        <result property="maintenancerName" column="maintenancer_name"/>
        <result property="maintenancerPhone" column="maintenancer_phone"/>
        <result property="price" column="price"/>
    </resultMap>
    <select id="queryDetailData" resultType="com.repair.model.vo.DetailsVo" resultMap="detailDatasMap">
        select
        rr.id,rr.maintenance_state,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.user_class,rr.area_id as
        area_name,rr.address,rat.name as article_name,rr.description,rr.voice,rr.voice_length,
        rr.finish_voice,finish_voice_length,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price
        from repair_record rr
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.deleted = 0
        <if test="recordId != null and recordId != ''">
            and rr.id = #{recordId}
        </if>

    </select>

    <select id="repairAnalysis" resultType="com.repair.model.vo.RepairAnalysisVo">
        SELECT
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and (maintenance_state in (5,6,11))
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as check_count ,
        (SELECT COUNT(*) FROM repair_record rr
        left join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id
        and rr.maintenance_state = 1
        <where>
            AND rr.deleted = 0
            and rr.maintenance_state = 1
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr.report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as receiving_count ,
        (SELECT COUNT(*) FROM repair_record rr
        left join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0 and
        rr.maintenance_state = 2
        <where>
            AND rr.deleted = 0
            and rr.maintenance_state = 2
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr.report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as maintain_count ,
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and maintenance_state = 7
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as accomplish_count ,
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and maintenance_state = 9
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as shut_count ,
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and maintenance_state = 8
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as cancel_count ,
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and maintenance_state = 3
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as dispose_count ,
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and maintenance_state = 4
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        )
        as affirm_count
    </select>

    <select id="queryTimeOut" resultType="java.lang.Integer">
        SELECT count(*) FROM (
        select rdr.users_id as
        user_id,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id
        as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price from
        repair_record rr
        inner join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.record_id = rr.id and rdr.is_lose_efficacy = 0
        and ((date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;= NOW() and
        rdr.receiving_time is null) or date_add(rdr.assigned_time , interval rdr.acceptance_assess_time minute) &lt;=
        rdr.receiving_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.deleted = 0
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        UNION
        select
        rdv.user_id,rr.maintenance_state,rr.article_id,rr.id,rr.record_no,rr.report_time,rr.user_name,rr.user_phone,rr.area_id
        as area_name,rr.address,rr.description,rr.image,rr.maintenancer_name,rr.maintenancer_phone,rr.price from
        repair_record rr
        inner join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0
        and ((date_add(rdv.receiving_time , interval rdv.maintenance_assess_time minute) &lt;= NOW() and
        rdv.maintenance_assess_time is null) or date_add(rdv.receiving_time , interval rdv.maintenance_assess_time
        minute) &lt;= rdv.finish_time)
        where rr.is_duty = 0 and rr.maintenance_state &lt;= 7 and rr.deleted = 0
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and rr.report_time between #{startTime} and #{endTime}
        </if>
        ) t
    </select>

    <select id="repairSummarizing" resultType="com.repair.model.vo.RepairSummarizingVo">
        SELECT
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and (maintenance_state in (5,6,11))
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as check_count ,
        (SELECT COUNT(*) FROM repair_record rr
        left join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id
        and rr.maintenance_state = 1
        <where>
            AND rr.deleted = 0
            and rr.maintenance_state = 1
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr.report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as receiving_count ,
        (SELECT COUNT(*) FROM repair_record rr
        left join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0 and
        rr.maintenance_state = 2
        <where>
            AND rr.deleted = 0
            and rr.maintenance_state = 2
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rr.report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as maintain_count ,
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and maintenance_state = 7
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as accomplish_count ,
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and maintenance_state = 9
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as shut_count ,
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and maintenance_state = 8
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as cancel_count ,
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and maintenance_state = 3
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        ) as dispose_count ,
        (SELECT COUNT(*) FROM repair_record
        <where>
            AND deleted = 0
            and maintenance_state = 4
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and report_time between #{startTime} and #{endTime}
            </if>
        </where>
        )
        as affirm_count
    </select>

    <select id="repairTotalData" resultType="com.repair.model.vo.RepairTotalDataVo">
        select (SELECT COUNT(*)
                      FROM repair_record
                      where deleted = 0
                        and (maintenance_state in (5, 6,11))) as check_count,
                     (SELECT COUNT(*)
                      FROM repair_record rr
                      left join repair_dispatch_record rdr on rdr.deleted = 0 and rdr.is_lose_efficacy = 0 and rdr.record_id = rr.id and rr.maintenance_state = 1
                      where rr.deleted = 0
                        and rr.maintenance_state = 1)         as receiving_count,
                     (SELECT COUNT(*)
                      FROM repair_record rr
                      left join repair_receiving rdv on rdv.deleted = 0 and rdv.record_id = rr.id and rdv.is_lose_efficacy = 0 and rr.maintenance_state = 2
                      where rr.deleted = 0
                        and rr.maintenance_state = 2)         as maintain_count,
                     (SELECT COUNT(*)
                      FROM repair_record
                      where deleted = 0
                        and maintenance_state = 7)         as accomplish_count,
                     (SELECT COUNT(*)
                      FROM repair_record
                      where deleted = 0
                        and maintenance_state = 9)         as shut_count,
                     (SELECT COUNT(*)
                      FROM repair_record
                      where deleted = 0
                        and maintenance_state = 8)         as cancel_count,
                     (SELECT COUNT(*)
                      FROM repair_record
                      where deleted = 0
                        and maintenance_state = 3)         as dispose_count,
                     (SELECT COUNT(*)
                      FROM repair_record
                      where deleted = 0
                        and maintenance_state = 4)
                                                           as affirm_count
    </select>

    <!--打印-->
    <resultMap type="com.repair.model.vo.PrintVo" id="PrintDataMap">
        <result property="recordNo" column="record_no"/>
        <result property="userName" column="user_name"/>
        <result property="userClass" column="user_class"/>
        <result property="userPhone" column="user_phone"/>
        <result property="areaId" column="area_id"/>
        <result property="articleName" column="article_name"/>
        <result property="reportTime" column="report_time"/>
        <result property="description" column="description"/>
        <result property="maintenancerName" column="maintenancer_name"/>
        <result property="maintenancerPhone" column="maintenancer_phone"/>
        <result property="maintenanceState" column="maintenance_state"/>
        <result property="schoolId" column="school_id"/>
        <result property="price" column="price"/>
        <result property="refund" column="refund"/>
    </resultMap>
    <select id="queryPrintData" resultType="com.repair.model.vo.PrintVo" resultMap="PrintDataMap">
        select rr.record_no,rr.user_name,rr.user_class,rr.user_phone,CONCAT(rr.area_id,rr.address) as area_id,rat.name
        as
        article_name,rr.report_time,rr.description,rr.maintenancer_name,rr.maintenancer_phone,rr.maintenance_state,rr.price,rr.school_id,
        (select IFNULL(sum(refund_price),0) from repair_refund_record where deleted = 0 and record_id = rr.id and
        approve = 2) as refund
        from repair_record rr
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rr.article_id
        where rr.deleted = 0
        <if test="recordId != null and recordId != ''">
            and rr.id = #{recordId}
        </if>
    </select>


</mapper>
