<?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.tofly.ghrq.custom.mapper.CustomRushRepairMapper">

    <resultMap id="rushMap" type="com.tofly.ghrq.custom.entity.CustomRushRepair">
        <id property="id" column="ID"/>
        <result property="workOrderId" column="WORK_ORDER_ID"/>
        <result property="workOrderType" column="WORK_ORDER_TYPE"/>
        <result property="workOrderSource" column="WORK_ORDER_SOURCE"/>
        <result property="dispatchUserId" column="DISPATCH_USER_ID"/>
        <result property="dispatchDate" column="DISPATCH_DATE"/>
        <result property="address" column="ADDRESS"/>
        <result property="contact" column="CONTACT"/>
        <result property="contactPhone" column="CONTACT_PHONE"/>
        <result property="rushRepairResponsible" column="RUSH_REPAIR_RESPONSIBLE"/>
        <result property="rushRepairPersons" column="RUSH_REPAIR_PERSONS"/>
        <result property="x" column="X"/>
        <result property="y" column="Y"/>
        <result property="introduction" column="INTRODUCTION"/>
        <result property="description" column="DESCRIPTION"/>
        <result property="note" column="NOTE"/>
        <result property="noteTaker" column="NOTE_TAKER"/>
        <result property="delFlag" column="DEL_FLAG"/>
        <result property="workOrderState" column="WORK_ORDER_STATE"/>
        <association property="workOrderTypeName" column="WORK_ORDER_TYPE" select="getworkOrderTypeName"></association>
        <association property="workOrderStateName" column="WORK_ORDER_STATE"
                     select="getworkOrderStateName"></association>
        <association property="dispatchUserName" column="DISPATCH_USER_ID"
                     select="com.tofly.common.mapper.ToflyCodeCommonMapper.getUserName"></association>
        <association property="responsibleName" column="RUSH_REPAIR_RESPONSIBLE"
                     select="com.tofly.common.mapper.ToflyCodeCommonMapper.getUserName"></association>
        <association property="departmentName" column="DEPARTMENT" select="getdeptName"></association>
    </resultMap>

    <resultMap id="customRushMap" type="com.tofly.ghrq.custom.entity.CustomRushRepair">
        <id property="id" column="ID"/>
        <result property="workOrderId" column="WORK_ORDER_ID"/>
        <result property="workOrderType" column="WORK_ORDER_TYPE"/>
        <result property="workOrderSource" column="WORK_ORDER_SOURCE"/>
        <result property="dispatchUserId" column="DISPATCH_USER_ID"/>
        <result property="dispatchDate" column="DISPATCH_DATE"/>
        <result property="address" column="ADDRESS"/>
        <result property="contact" column="CONTACT"/>
        <result property="contactPhone" column="CONTACT_PHONE"/>
        <result property="rushRepairResponsible" column="RUSH_REPAIR_RESPONSIBLE"/>
        <result property="rushRepairPersons" column="RUSH_REPAIR_PERSONS"/>
        <result property="x" column="X"/>
        <result property="y" column="Y"/>
        <result property="introduction" column="INTRODUCTION"/>
        <result property="description" column="DESCRIPTION"/>
        <result property="note" column="NOTE"/>
        <result property="noteTaker" column="NOTE_TAKER"/>
        <result property="delFlag" column="DEL_FLAG"/>
        <result property="workOrderState" column="WORK_ORDER_STATE"/>
        <result property="creatUser" column="CREAT_USER"/>
        <result property="creatTime" column="CREAT_TIME"/>
        <result property="department" column="DEPARTMENT"/>
        <association property="workOrderTypeName" column="WORK_ORDER_TYPE" select="getworkOrderTypeName"></association>
        <association property="workOrderStateName" column="WORK_ORDER_STATE"
                     select="getworkOrderStateName"></association>
        <association property="dispatchUserName" column="DISPATCH_USER_ID"
                     select="com.tofly.common.mapper.ToflyCodeCommonMapper.getUserName"></association>
        <association property="responsibleName" column="RUSH_REPAIR_RESPONSIBLE"
                     select="com.tofly.common.mapper.ToflyCodeCommonMapper.getUserName"></association>
        <association property="departmentName" column="DEPARTMENT" select="getdeptName"></association>
        <association property="creatUserName" column="CREAT_USER" select="com.tofly.common.mapper.ToflyCodeCommonMapper.getUserName"/>
        <collection property="files" ofType="java.util.List" select="getFilesName" column="ID"/>
        <collection property="voice" ofType="java.util.List" select="getVoice" column="ID"/>
        <collection property="processManages" ofType="java.util.List" select="getProcess" column="ID"/>
        <collection property="auditList" ofType="java.util.List" select="getAuditList" column="ID"/>
        <collection property="endVoice" ofType="java.util.List" select="getEndVoice" column="ID"/>
    </resultMap>

    <resultMap id="customRushRepairAuditEndMap" type="com.tofly.ghrq.custom.entity.CustomRushAudit">
        <id property="id" column="ID"/>
        <result property="auditor" column="AUDITOR"/>
        <result property="auditmind" column="AUDITMIND"/>
        <result property="auditDescription" column="AUDIT_DESCRIPTION"/>
        <result property="workOrderId" column="WORK_ORDER_ID"/>
        <result property="auditTime" column="AUDIT_TIME"/>
        <result property="delFlag" column="DEL_FLAG"/>
        <association property="auditMindName" column="AUDITMIND" select="getauditMindName"></association>
        <association property="auditorName" column="AUDITOR" select="com.tofly.common.mapper.ToflyCodeCommonMapper.getUserName"/>
        <collection property="auditEndFiles" ofType="java.util.List" select="getAuditFiles" column="ID"></collection>
    </resultMap>


    <select id="getauditMindName" resultType="java.lang.Object">
      select decode(#{auditmind},'0','通过','1','不通过') as auditMindName from dual
    </select>


    <select id="getworkOrderTypeName" resultType="java.lang.Object">
      select decode(#{workOrderType},'0','内部工单','1','外部转接') as workOrderTypeName from dual
    </select>
    <select id="getworkOrderStateName" resultType="java.lang.Object">
      select decode(#{workOrderState},'0','待派工','1','派工未通过','2','待处理','3','待审核','4','审核未通过','5','归档') as workOrderStateName from dual
    </select>
    <select id="getdeptName" resultType="java.lang.Object">
        select name as departmentName from tf_smpn_department_x where id=#{department}
    </select>

    <select id="getCustomRushRepairById" resultMap="customRushMap">
        select * from tf_rush_repair where DEL_FLAG = 1 AND id = #{id}
    </select>

    <select id="pageByCondition" resultMap="rushMap">
        select * from tf_rush_repair r where r.DEL_FLAG = 1
        <if test="rushRepairSearch.address != null and rushRepairSearch.address.trim() != ''">
            and (r.ADDRESS like '%'||#{rushRepairSearch.address}||'%')
        </if>
        <if test="rushRepairSearch.workOrderType !=null and rushRepairSearch.workOrderType.trim() != ''">
            and (r.WORK_ORDER_TYPE = #{rushRepairSearch.workOrderType})
        </if>
        <if test="rushRepairSearch.workOrderId !=null and rushRepairSearch.workOrderId.trim() != ''">
            and (r.WORK_ORDER_ID = #{rushRepairSearch.workOrderId})
        </if>
        <if test="rushRepairSearch.workOrderState !=null and rushRepairSearch.workOrderState.trim() != ''">
            and (r.WORK_ORDER_STATE = #{rushRepairSearch.workOrderState})
        </if>
        order by r.CREAT_TIME
    </select>

    <select id="getAll" resultMap="rushMap">
        select * from tf_rush_repair r where DEL_FLAG = 1
        <if test="rushRepairSearch.address != null and rushRepairSearch.address.trim() != ''">
            and (r.ADDRESS like '%'||#{rushRepairSearch.address}||'%')
        </if>
        <if test="rushRepairSearch.workOrderType !=null and rushRepairSearch.workOrderType.trim() != ''">
            and (r.WORK_ORDER_TYPE = #{rushRepairSearch.workOrderType})
        </if>
        <if test="rushRepairSearch.workOrderId !=null and rushRepairSearch.workOrderId.trim() != ''">
            and (r.WORK_ORDER_ID = #{rushRepairSearch.workOrderId})
        </if>
        <if test="rushRepairSearch.workOrderState !=null and rushRepairSearch.workOrderState.trim() != ''">
            and (r.WORK_ORDER_STATE = #{rushRepairSearch.workOrderState})
        </if>
    </select>

    <resultMap id="fileMap" type="java.util.Map">
        <result property="path" column="PATH"/>
        <result property="fileName" column="FILE_NAME"/>
    </resultMap>

    <select id="getFilesName" resultMap="fileMap">
        select f.PATH,f.FILE_NAME from tf_rush_repair_filetorepair fr,tf_rush_repair_file f
        where fr.WORK_ORDER_ID = #{id} AND fr.FILE_ID = f.ID AND f.DEL_FLAG = 1 AND (f.FILE_TYPE = 0 OR f.FILE_TYPE = 2)
    </select>

    <select id="getVoice" resultMap="fileMap">
        select f.PATH,f.FILE_NAME from tf_rush_repair_filetorepair fr,tf_rush_repair_file f
        where fr.WORK_ORDER_ID = #{id} AND fr.FILE_ID = f.ID AND f.DEL_FLAG = 1 AND f.FILE_TYPE = 1
    </select>

    <select id="getEndFilesName" resultMap="fileMap">
        select f.PATH,f.FILE_NAME from tf_rush_repair_filetoaudit fa,tf_rush_repair_audit_end ad,tf_rush_repair_file f
        where ad.WORK_ORDER_ID = #{id}
        AND fa.audit_end_id = ad.id
        AND fa.FILE_ID = f.ID
        AND f.DEL_FLAG = 1
        AND (f.FILE_TYPE = 0 OR f.FILE_TYPE = 2)
    </select>

    <select id="getEndVoice" resultMap="fileMap">
        select f.PATH,f.FILE_NAME from tf_rush_repair_filetoaudit fa,tf_rush_repair_audit_end ad,tf_rush_repair_file f
        where ad.WORK_ORDER_ID = #{id}
        AND fa.audit_end_id = ad.id
        AND fa.FILE_ID = f.ID
        AND f.DEL_FLAG = 1
        AND f.FILE_TYPE = 1
    </select>

    <resultMap id="rushProcessMap" type="com.tofly.ghrq.custom.entity.CustomRushRepairProcessManage">
        <id property="id" column="ID"/>
        <result property="description" column="DESCRIPTION"/>
        <result property="workOrderId" column="WORK_ORDER_ID"/>
        <result property="manageDate" column="MANAGE_DATE"/>
        <result property="x" column="X"/>
        <result property="y" column="Y"/>
        <result property="delFlag" column="DEL_FLAG"/>
        <collection property="processFiles" ofType="java.util.List" select="getProcessFiles" column="ID"></collection>
        <collection property="processVoice" ofType="java.util.List" select="getProcessVoice" column="ID"></collection>
    </resultMap>

    <select id="getProcess" resultMap="rushProcessMap">
        select * from tf_rush_repair_process_manage where work_order_id = #{id} AND DEL_FLAG = 1 order by manage_date
    </select>

    <select id="getProcessFiles" resultMap="fileMap">
        select f.PATH,f.FILE_NAME from tf_rush_repair_filetoprocess fp,tf_rush_repair_file f
        where fp.PROCESS_MANAGE_ID = #{id} AND fp.FILE_ID = f.ID AND f.DEL_FLAG = 1 AND (f.FILE_TYPE = 0 OR f.FILE_TYPE = 2)
    </select>

    <select id="getProcessVoice" resultMap="fileMap">
        select f.PATH,f.FILE_NAME from tf_rush_repair_filetoprocess fp,tf_rush_repair_file f
        where fp.PROCESS_MANAGE_ID = #{id} AND fp.FILE_ID = f.ID AND f.DEL_FLAG = 1 AND f.FILE_TYPE = 1
    </select>

    <select id="getIdByRole" resultType="java.lang.Long">
        select id from tf_rush_repair  where (dispatch_user_id = #{id} or rush_repair_responsible = #{id})  AND DEL_FLAG = 1
    </select>

    <select id="getAuditList" resultMap="customRushRepairAuditEndMap">
        select * from tf_rush_repair_audit_end where work_order_id = #{id} AND DEL_FLAG = 1 order by audit_time
    </select>

    <select id="getAuditFiles" resultMap="fileMap">
        select f.PATH,f.FILE_NAME from tf_rush_repair_filetoaudit fa,tf_rush_repair_file f
        where fa.AUDIT_END_ID = #{id} AND fa.FILE_ID = f.ID AND f.DEL_FLAG = 1 AND (f.FILE_TYPE = 0 OR f.FILE_TYPE = 2)
    </select>


    <select id="getFiles" resultType="map">
        select f.*, t.lx,t.mxid,t.work_order_id
        from tf_rush_repair_file f,
        ((select file_id as fileid, '1' as lx,id as mxid,work_order_id
        from tf_rush_repair_filetorepair
        where 1=1
        <if test="rids !=null and rids.size()>0">
            and WORK_ORDER_ID in
            <foreach collection="rids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        ) union all
        (select fa.file_id as fileid, '2' as lx,ad.id as mxid,ad.work_order_id
        from tf_rush_repair_filetoaudit fa, tf_rush_repair_audit_end ad
        where fa.audit_end_id = ad.id
        <if test="rids !=null and rids.size()>0">
            and ad.WORK_ORDER_ID in
            <foreach collection="rids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>

        ) union all
        (select fp.file_id as fileid, '3' as lx,pm.id as mxid,pm.work_order_id
        from tf_rush_repair_process_manage pm,
        tf_rush_repair_filetoprocess  fp
        where pm.id = fp.PROCESS_MANAGE_ID
        <if test="rids !=null and rids.size()>0">
            and pm.WORK_ORDER_ID in
            <foreach collection="rids" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        )) t
        where f.id = t.fileid and f.DEL_FLAG='1'
    </select>

    <select id="getReportByTYpe" resultType="map">
        select work_order_type,
        decode(work_order_type, '0', '内部工单', '1', '外部转移') as work_order_type_name,
        count(1) as cunt
        from TF_RUSH_REPAIR
        where del_flag='1'
        <if test="deptId!=null">
            and exists (select 1
            from tf_smpn_user_x
            where DEPARTMENT_ID = #{deptId}
            and id = TF_RUSH_REPAIR.Creat_User)
        </if>
        <if test="startDate!=null and startDate.trim() != ''">
            and to_char(creat_time,'yyyy-MM-dd') >= #{startDate}
        </if>
        <if test="endDate!=null and endDate.trim() != ''">
            <![CDATA[  and to_char(creat_time,'yyyy-MM-dd') <= #{endDate}   ]]>
        </if>
        group by work_order_type
    </select>

    <select id="getReportByTime" resultType="map">
        select ${reportType} as sj,
        work_order_type,
        decode(work_order_type, '0', '内部工单', '1', '外部转移') as work_order_type_name,
        count(1) as cunt
        from TF_RUSH_REPAIR
        where del_flag='1'
        <if test="deptId!=null">
            and exists (select 1
            from tf_smpn_user_x
            where DEPARTMENT_ID = #{deptId}
            and id = TF_RUSH_REPAIR.Creat_User)
        </if>
        <if test="startDate!=null and startDate.trim() != ''">
            and ${reportType} >= #{startDate}
        </if>
        <if test="endDate!=null and endDate.trim() != ''">
            <![CDATA[  and ${reportType} <= #{endDate}   ]]>
        </if>
        group by work_order_type, ${reportType}
        order by work_order_type,sj
    </select>


    <select id="querySJByDay" resultType="java.lang.String">
        SELECT TO_CHAR(TO_DATE(#{startDate}, 'YYYY-MM-DD') + ROWNUM - 1,
        'YYYY-MM-DD') sj
        FROM DUAL
      <![CDATA[  CONNECT BY ROWNUM <= nvl(TO_DATE(#{endDate}, 'YYYY-MM-DD') -   ]]>
        TO_DATE(#{endDate}, 'YYYY-MM-DD') + 1
    </select>

    <select id="querySJByMonth" resultType="java.lang.String">
        SELECT TO_CHAR(add_months(TO_DATE(#{startDate}, 'YYYY-MM'), ROWNUM - 1),
        'YYYY-MM') sj
        FROM DUAL
    <![CDATA[    CONNECT BY ROWNUM <=            ]]>
        months_between(TO_DATE(#{endDate}, 'YYYY-MM'),
        TO_DATE(#{startDate}, 'YYYY-MM')) + 1
    </select>

    <select id="querySJByYear" resultType="java.lang.String">
        SELECT TO_CHAR(to_number(#{startDate}) + rownum - 1) sj
        FROM DUAL
  <![CDATA[      CONNECT BY ROWNUM <= to_number(#{endDate}) - to_number(#{startDate}) + 1   ]]>
    </select>
</mapper>