<?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.RepairUserMapper">

    <resultMap type="com.repair.model.vo.RepairUserVo" id="repairUserPageMap">
        <result property="id" column="id"/>
        <result property="cardNumber" column="card_number"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="teamId" column="team_id"/>
        <result property="teamName" column="team_name"/>
        <result property="schoolId" column="school_id"/>
        <result property="schoolName" column="school_name"/>
        <result property="userZzid" column="user_zzid"/>
        <result property="userZzname" column="user_zzname"/>
        <result property="workType" column="work_type"/>
        <result property="workTypeName" column="work_type_name"/>
        <result property="acceptanceTime" column="acceptance_assess_time"/>
        <result property="maintenanceTime" column="maintenance_assess_time"/>
    </resultMap>

    <select id="queryUserPageList" resultType="com.repair.model.vo.RepairUserVo" resultMap="repairUserPageMap">
        select ru.card_number,ru.acceptance_assess_time,ru.maintenance_assess_time,ru.id,ru.user_name, ru.user_phone, ru.team_id, case ru.team_id when 0 then '校方人员' else '第三方人员' end as team_name, ru.school_id,ra.name as school_name,
        ru.user_zzid,ru.work_type,rwt.name as work_type_name from repair_user ru
        left join repair_area ra on ra.id = ru.school_id and ra.deleted = 0
        left join repair_work_type rwt on rwt.id = ru.work_type and ra.deleted = 0
        <where>
            and ru.deleted = 0 and (ru.user_zzid = 1 or ru.user_zzid = 2 or ru.user_zzid = 3 or ru.user_zzid = 4)
            <if test="schoolId != null and schoolId != ''">
                and ru.school_id = ${schoolId}
            </if>
            <if test="teamId != null and teamId != ''">
                and ru.team_id = ${teamId}
            </if>
            <if test="keyWord != null and keyWord != ''">
                and (ru.user_name like '%' #{keyWord} '%' or ru.user_phone like '%' #{keyWord} '%')
            </if>
        </where>
    </select>

    <resultMap type="com.repair.model.vo.ArticleBuildVo" id="repairArticleBuildMap">
        <result property="id" column="id"/>
        <result property="articleId" column="article_id"/>
        <result property="buildId" column="build_id"/>
        <result property="articleName" column="article_name"/>
        <result property="buildName" column="build_name"/>
        <result property="userId" column="user_id"/>
    </resultMap>
    <!--获取报修关联楼栋种的数据-->
    <select id="queryArticleBuilds" resultType="com.repair.model.vo.ArticleBuildVo" resultMap="repairArticleBuildMap">
        SELECT rab.id,rat.name as article_name,rab.article_id,rab.build_id,(select GROUP_CONCAT(name) from repair_area where find_in_set(id,rab.build_id)) as build_name,rab.user_id
        FROM repair_article_build rab
        left join repair_article_type rat on rat.deleted = 0 and rat.id = rab.article_id
        where rab.deleted = 0 and rab.user_id in (${userIds})
    </select>


    <resultMap type="com.repair.model.vo.RepairLogisticsVo" id="repairLogisticsMap">
        <result property="id" column="id"/>
    </resultMap>

    <select id="queryLogisticsList" resultType="com.repair.model.vo.RepairLogisticsVo" resultMap="repairLogisticsMap">
        select id from repair_user ru
        where EXISTS (select id from repair_record where deleted = 0 and id = #{recordId} and school_id = ru.school_id)
        and ru.user_zzid = 3 and ru.deleted = 0
    </select>


    <resultMap type="com.repair.model.vo.UserDataVo" id="userDataMap">
        <result property="id" column="id"/>
        <result property="state" column="state"/>
        <result property="shiftId" column="shift_id"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="startDate" column="start_date"/>
        <result property="articleId" column="article_id"/>
        <result property="acceptanceAssessTime" column="acceptance_assess_time"/>
        <result property="maintenanceAssessTime" column="maintenance_assess_time"/>
    </resultMap>
    <select id="queryUserData" resultType="com.repair.model.vo.UserDataVo" resultMap="userDataMap">
        select ru.user_phone,ru.acceptance_assess_time,ru.maintenance_assess_time,ru.state,ru.id,ru.user_name,rcs.start_date,rcs.shift_id,
        (select GROUP_CONCAT(a.articleId) from (select (select GROUP_CONCAT(id) from repair_article_type where (id =repair_article_build.article_id or parent_id = repair_article_build.article_id) and deleted = 0) as articleId
        from repair_article_build where deleted = 0 and user_id = ru.id) a) as article_id
        from repair_user ru
        left join repair_classes_settings rcs on rcs.deleted = 0 and rcs.user_id = #{userId} and start_date = #{dateNow}
        where ru.id = #{userId} and ru.deleted = 0
    </select>

    <resultMap type="com.repair.model.vo.CollaboratorVo" id="collaboratorMap">
        <result property="id" column="id"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="workTypeName" column="work_type_name"/>
        <result property="state" column="state"/>
    </resultMap>
    <select id="queryCollaborator" resultType="com.repair.model.vo.CollaboratorVo" resultMap="collaboratorMap">
        select ru.id,ru.user_name,ru.user_phone,rwt.name as work_type_name,ru.state
        from repair_user ru
        left join repair_work_type rwt on rwt.deleted = 0 and rwt.id = ru.work_type
        <where>
            and ru.deleted = 0 and (ru.user_zzid = 1 or ru.user_zzid = 2)
            <if test="userId != null and userId != ''">
                and ru.id != #{userId}
            </if>
            <if test="schoolId != null and schoolId != ''">
                and ru.school_id = #{schoolId}
            </if>
            <if test="keyWord != null and keyWord != ''">
                and (ru.user_name like '%' #{keyWord} '%' or ru.user_phone like '%' #{keyWord} '%' or rwt.name like '%' #{keyWord} '%')
            </if>
        </where>
    </select>

    <resultMap type="com.repair.model.vo.AddressBookVo" id="addressBookMap">
        <result property="id" column="id"/>
        <result property="teamId" column="team_id"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="workType" column="work_type"/>
        <result property="workTypeName" column="work_type_name"/>
        <result property="state" column="state"/>
        <result property="acceptanceTime" column="acceptance_assess_time"/>
        <result property="maintenanceTime" column="maintenance_assess_time"/>
    </resultMap>
    <select id="queryAddressBook" resultType="com.repair.model.vo.AddressBookVo" resultMap="addressBookMap">
        select ru.team_id,ru.id,ru.user_name,ru.user_phone,ru.work_type,rwt.name as work_type_name,ru.state,ru.state,ru.acceptance_assess_time,ru.maintenance_assess_time
        from repair_user ru
        left join repair_work_type rwt on rwt.deleted = 0 and rwt.id = ru.work_type
        <where>
            and ru.deleted = 0 and (ru.user_zzid = 1 or ru.user_zzid = 2)
            <if test="schoolId != null and schoolId != ''">
                and ru.school_id = #{schoolId}
            </if>
            <if test="keyWord != null and keyWord != ''">
                and (ru.user_name like '%' #{keyWord} '%' or ru.user_phone like '%' #{keyWord} '%' or rwt.name like '%' #{keyWord} '%')
            </if>
        </where>
    </select>

    <select id="queryUserPageBySchoolId" resultType="com.repair.model.vo.AddressBookVo" resultMap="addressBookMap">
        select ru.team_id,ru.id,ru.user_name,ru.user_phone,ru.work_type,rwt.name as work_type_name,ru.state,ru.state,ru.acceptance_assess_time,ru.maintenance_assess_time
        from repair_user ru
        left join repair_work_type rwt on rwt.deleted = 0 and rwt.id = ru.work_type
        <where>
            and ru.deleted = 0 and (ru.user_zzid = 1 or ru.user_zzid = 2)
            <if test="schoolId != null and schoolId != ''">
                and ru.school_id = #{schoolId}
            </if>
            <if test="userId != null and userId != ''">
                and ru.id = #{userId}
            </if>
        </where>
    </select>

    <resultMap type="com.repair.model.vo.AutoDispatchUserVo" id="AutoDispatchUserMap">
        <result property="id" column="id"/>
        <result property="state" column="state"/>
        <result property="workType" column="work_type"/>
        <result property="rdrCount" column="rdr_count"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="acceptanceTime" column="acceptance_assess_time"/>
        <result property="maintenanceTime" column="maintenance_assess_time"/>
        <result property="startDate" column="start_date"/>
        <result property="shiftId" column="shift_id"/>
    </resultMap>

    <select id="autoDispatchUser" resultType="com.repair.model.vo.AutoDispatchUserVo" resultMap="AutoDispatchUserMap">
        select * from (
        select ru.user_zzid,wt.name as work_type,'正常接单' as state,(select count(*) from repair_dispatch_record where deleted = 0 and is_lose_efficacy = 0 and assigned_time like '%' #{dateNow} '%' and users_id = ru.id) as rdr_count,ru.user_phone,ru.acceptance_assess_time,ru.maintenance_assess_time,ru.id,ru.user_name,rcs.start_date,rcs.shift_id
        from repair_user ru
        inner join repair_classes_settings rcs on rcs.deleted = 0 and rcs.user_id = ru.id and start_date = #{dateNow}
        left join repair_work_type wt on wt.deleted = 0 and wt.id = ru.work_type
        where ru.school_id = #{schoolId} and ru.deleted = 0 and (ru.user_zzid = 1 or ru.user_zzid = 2) and ru.state = 1
        and ru.id in (select user_id from
        (select user_id,(select GROUP_CONCAT(id) from repair_article_type where (id = repair_article_build.article_id or parent_id = repair_article_build.article_id) and deleted = 0) as article_id,build_id
        from repair_article_build
        where deleted = 0) d
        where FIND_IN_SET(${articleId},d.article_id) and FIND_IN_SET(${buildId},d.build_id))
        <if test="keyWord != null and keyWord != ''">
            and (ru.user_name like '%' #{keyWord} '%' or ru.user_phone like '%' #{keyWord} '%')
        </if>
        group by ru.id) t
        order by t.rdr_count
    </select>


    <resultMap type="com.repair.model.vo.AutoOfflineUserVo" id="AutoOfflineUserMap">
        <result property="id" column="id"/>
        <result property="userName" column="user_name"/>
        <result property="teamName" column="team_name"/>
        <result property="userZzName" column="userZz_name"/>
        <result property="workType" column="work_type"/>
    </resultMap>

    <select id="autoOfflineRepairUser" resultType="com.repair.model.vo.AutoOfflineUserVo" resultMap="AutoOfflineUserMap">
        select ru.id,ru.user_name,case team_id when 0 then '校方人员' else '第三方人员' end as team_name,'线下' as userZz_name,wt.name as work_type
        from repair_user ru
        left join repair_work_type wt on wt.deleted = 0 and wt.id = ru.work_type
        where ru.school_id = #{schoolId} and ru.deleted = 0 and ru.user_zzid = 4 and ru.state = 1
        <if test="keyWord != null and keyWord != ''">
            and ru.user_name like '%' #{keyWord} '%'
        </if>
    </select>

    <!--获取协作下拉人员信息-->
    <resultMap type="com.repair.model.vo.RepairWorkTypeVo" id="collaboratorUserMap">
        <result property="id" column="id"/>
        <result property="name" column="user_name"/>
        <result property="workType" column="work_type"/>
        <result property="state" column="state"/>
        <result property="userPhone" column="user_phone"/>
    </resultMap>
    <select id="queryCollaboratorUser" resultType="com.repair.model.vo.RepairWorkTypeVo" resultMap="collaboratorUserMap">
        select ru.id,ru.user_phone,'正常接单' as state,wt.name as work_type,ru.user_name from repair_user ru
        inner join repair_classes_settings rcs on rcs.deleted = 0 and rcs.user_id = ru.id and rcs.shift_id != '' and rcs.start_date = #{dateNow}
        left join repair_work_type wt on wt.deleted = 0 and wt.id = ru.work_type
        where ru.school_id = #{schoolId} and ru.deleted = 0 and (ru.user_zzid = 1 or ru.user_zzid = 2) and ru.state = 1 and ru.id != #{userId}
        <if test="keyWord != null and keyWord != ''">
            and (ru.user_name like '%' #{keyWord} '%' or ru.user_phone like '%' #{keyWord} '%')
        </if>
    </select>


    <resultMap type="com.repair.model.vo.PositiveReviewVo" id="positiveReviewMap">
        <result property="id" column="id"/>
        <result property="userName" column="user_name"/>
        <result property="finishCount" column="finish_count"/>
        <result property="evaluateCount" column="evaluate_count"/>
    </resultMap>
    <select id="queryPositiveReviewPage" resultType="com.repair.model.vo.PositiveReviewVo" resultMap="positiveReviewMap">
        select ru.id,ru.user_name, (select Count(*) from repair_record where deleted = 0 and maintenance_state = 7 and find_in_set(ru.id,maintenancer_id)) as finish_count,
        (select Count(*) from repair_record rr
        inner join repair_evaluate re on re.deleted = 0 and re.star > 3 and re.record_id = rr.id
        where rr.deleted = 0 and rr.maintenance_state = 7 and rr.school_id = #{schoolId} and find_in_set(ru.id,rr.maintenancer_id)) as evaluate_count
        from repair_user ru
        where ru.deleted = 0 and (ru.user_zzid = 1 or ru.user_zzid = 2) and ru.school_id = #{schoolId}
    </select>

    <select id="dropDown" resultType="com.repair.model.vo.AutoDispatchUserVo">
        select * from (
    select wt.name as work_type,'正常接单' as state,(select count(*) from repair_dispatch_record where deleted = 0 and is_lose_efficacy = 0  and users_id = ru.id) as rdr_count,ru.build_id,ru.user_phone,ru.acceptance_assess_time,ru.maintenance_assess_time,ru.id,ru.user_name,rcs.start_date,rcs.shift_id
    from repair_user ru
    inner join repair_classes_settings rcs on rcs.deleted = 0 and rcs.user_id = ru.id and rcs.start_date = CURRENT_DATE()
    left join repair_work_type wt on wt.deleted = 0 and wt.id = ru.work_type
    where
    ru.id in (select user_id from
        (select user_id,(select GROUP_CONCAT(id) from repair_article_type where (id = repair_article_build.article_id or parent_id = repair_article_build.article_id) and deleted = 0) as article_id,build_id
        from repair_article_build
        where deleted = 0) d
        where FIND_IN_SET(${articleId},d.article_id) and FIND_IN_SET(${buildId},d.build_id)) and
    ru.school_id = #{schoolId} and ru.deleted = 0 and (ru.user_zzid = 1 or ru.user_zzid = 2) and ru.state = 1 and ru.id != ${userId}) t
    order by t.rdr_count
    </select>

</mapper>
