<?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.gpsboot.mapper.maintain.RepairorderMapper">

    <resultMap id="repairorderMap" type="com.tofly.pojo.maintain.Repairorder">
        <id property="id" column="ID"/>
        <result property="orderCode" column="ORDER_CODE"/>
        <result property="faultType" column="FAULT_TYPE"/>
        <result property="yearId" column="YEAR_ID"/>
        <result property="orderSource" column="ORDER_SOURCE"/>
        <result property="phone" column="PHONE"/>
        <result property="typeId" column="TYPE_ID"/>
        <result property="month" column="MONTH"/>
        <result property="startTime" column="START_TIME"/>
        <result property="endTime" column="END_TIME"/>
        <result property="address" column="ADDRESS"/>
        <result property="lgtd" column="LGTD"/>
        <result property="lttd" column="LTTD"/>
        <result property="num" column="NUM"/>
        <result property="unit" column="UNIT"/>
        <result property="notes" column="NOTES"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="state" column="STATE"/>
        <result property="percent" column="PERCENT"/>
        <result property="overNum" column="OVER_NUM"/>
        <result property="fileIds" column="FILE_IDS"/>
        <result property="overTime" column="OVER_TIME"/>
        <result property="isDispatching" column="IS_DISPATCHING"/>
        <result property="dispatchingId" column="DISPATCHING_ID"/>
        <result property="artificialEndFlag" column="ARTIFICIAL_END_FLAG"/>
        <result property="maintenanceFlag" column="MAINTENANCE_FLAG"/>
        <result property="timeoutRemindFlag" column="TIMEOUT_REMIND_FLAG"/>
    </resultMap>


    <resultMap id="repairOrderBaseInfoVoMap" type="com.tofly.pojo.maintain.vo.datail.RepairOrderBaseInfoVo">
        <id property="id" column="ID"/>
        <result property="orderCode" column="ORDER_CODE"/>
        <result property="orderSource" column="ORDER_SOURCE"/>
        <result property="typeId" column="TYPE_ID"/>
        <result property="startTime" column="START_TIME"/>
        <result property="endTime" column="END_TIME"/>
        <result property="address" column="ADDRESS"/>
        <result property="num" column="NUM"/>
        <result property="unit" column="UNIT"/>
        <result property="notes" column="NOTES"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="state" column="STATE"/>
        <result property="percent" column="PERCENT"/>
        <result property="overNum" column="OVER_NUM"/>
        <result property="fileIds" column="FILE_IDS"/>
        <result property="overTime" column="OVER_TIME"/>
        <result property="dispatchingId" column="DISPATCHING_ID"/>
        <result property="x" column="X"/>
        <result property="y" column="Y"/>
        <association property="orderSourceName" column="ORDER_SOURCE" select="getOrderSourceName"/>
        <association property="typeName" column="TYPE_ID" select="getTypeName"/>
        <association property="dispatchingIdName" column="DISPATCHING_ID" select="getRealName"/>
        <association property="stateName" column="STATE" select="getStateName"/>
        <collection property="fileIdList" column="FILE_IDS" select="getFileIdsList"/>
    </resultMap>

    <select id="getOrderSourceName" resultType="string">
        select decode(#{orderSource},'0','计划','1','自建','2','热线') as orderSourceName from dual
    </select>
    <select id="getTypeName" resultType="string">
        select name as typeName
        from TF_YWPN_REPAIRTYPE_D
        where id = #{typeId}
    </select>
    <select id="getRealName" resultType="string" databaseId="oracle">
        select REAL_NAME dispatchingIdName
        from TF_SMPN_USER_X
        where id = #{DISPATCHING_ID}
    </select>
    <select id="getFileIdsList" resultType="string" databaseId="oracle">
        select FILE_PATH as filePath
        from TF_YWPN_FILE_W
        <if test="fileIds != null and fileIds != ''  ">where id in
            <foreach item="nodeCodestr" collection="fileIds.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>
        </if>
        <if test="fileIdsOne == null ">
            where id = ''
        </if>

    </select>
    <select id="getStateName" resultType="string">
        select decode(#{state}, '0', '未派工', '1', '未审核', '2', '未开始', '3', '正在执行', '4', '处理完成', '5', '已开始未执行', '6', '驳回',
                      '7', '转移申请', '8', '已转移', '9', '超时', '10', '终止') as stateName
        from dual
    </select>
    <select id="getRepairOrderBaseInfo" resultMap="repairOrderBaseInfoVoMap">
        SELECT ID,
               ORDER_CODE,
               TYPE_ID,
               NUM,
               UNIT,
               ORDER_SOURCE,
               ADDRESS,
               NOTES,
               DISPATCHING_ID,
               to_char(CREATE_TIME, 'yyyy-MM-dd hh24:mi:ss') as CREATE_TIME,
               to_char(OVER_TIME, 'yyyy-MM-dd hh24:mi:ss') as OVER_TIME,
               TRUNC((NVL(OVER_NUM,0)/NUM),4) * 100 as PERCENT,
               OVER_NUM,
               to_char(START_TIME, 'yyyy-MM-dd hh24:mi:ss') as START_TIME,
               to_char(END_TIME, 'yyyy-MM-dd hh24:mi:ss') as END_TIME,
               STATE,
               FILE_IDS,
               LGTD as X,
               LTTD as Y
        FROM TF_YWPN_REPAIRORDER_W
        WHERE ID = #{id}
    </select>
    <resultMap id="repairOrderHotlineInfoVoMap" type="com.tofly.pojo.maintain.vo.datail.RepairOrderHotlineInfoVo">
        <result property="gdbh" column="GDBH"/>
        <result property="kubh" column="KUBH"/>
        <result property="kuxm" column="KUXM"/>
        <result property="lxdh" column="LXDH"/>
        <result property="ysxzMc" column="YSXZ_MC"/>
        <result property="sbbh" column="SBBH"/>
        <result property="sbkj" column="SBKJ"/>
        <result property="khdz" column="KHDZ"/>
        <result property="typeName" column="TYPENAME"/>
        <result property="typeXFName" column="TYPEXFNAME"/>
        <result property="jjcd" column="JJCD"/>
        <result property="gdms" column="GDMS"/>
        <result property="djry" column="DJRY"/>
        <result property="djsj" column="DJSJ"/>
        <result property="lcjd" column="LCJD"/>
        <result property="gddjFileId" column="GDDJ_FILEID"/>
        <association property="jjcdName" column="JJCD" select="getJjcdName"/>
        <association property="djbmName" column="DJRY" select="getDjryDeptName"/>
        <association property="djryName" column="DJRY" select="getDjryName"/>
        <association property="lcjdName" column="LCJD" select="getLcjdName"/>
        <collection property="gddjFileIdList" column="GDDJ_FILEID" select="getGddjFileIdList"/>
    </resultMap>

    <!-- 热线信息 -->
    <select id="getRepairOrderHotlineInfoVo" resultMap="repairOrderHotlineInfoVoMap">
        SELECT A.GDBH,
               A.KHBH,
               A.KHXM,
               A.LXDH,
               c.YSXZ_MC,
               B.SBBH,
               B.SBKJ,
               A.KHDZ,
               (
                   SELECT GDLX_MC
                   FROM TF_YWPN_GDGLTYPEBM_W
                   WHERE ID = A.GDLX
               )        typeName,
               (
                   CASE
                       WHEN A.GDXF2 IS NOT NULL THEN
                           (
                               SELECT GDLX_MC
                               FROM TF_YWPN_GDGLTYPEBM_W
                               WHERE ID = A.GDXF2
                           )
                       WHEN A.GDXF1 IS NOT NULL THEN
                           (
                               SELECT GDLX_MC
                               FROM TF_YWPN_GDGLTYPEBM_W
                               WHERE ID = A.GDXF1
                           )
                       WHEN A.GDLX IS NOT NULL THEN
                           (
                               SELECT GDLX_MC
                               FROM TF_YWPN_GDGLTYPEBM_W
                               WHERE ID = A.GDLX
                           )
                       END
                   ) AS typeXFName,
               A.JJCD,
               A.GDMS,
               A.DJRY,
               to_char(A.DJSJ, 'yyyy-MM-dd hh24:mi:ss') as DJSJ,
               A.LCJD,
               A.GDDJ_FILEID
        FROM TF_YWPN_GDINFO_W A
                 LEFT JOIN TF_YWPN_QFKHJBXX_W B ON A.KHBH = B.YHBH
                 LEFT JOIN TF_YWPN_QFMIDYSXZBM_E C ON B.YSXZ = C.YSXZ_BM
        WHERE A.GDBH IN (
            (
                SELECT t1.GDBH
                FROM TF_YWPN_GDCIRCULATION_W t1
                         LEFT JOIN TF_YWPN_REPAIRORDER_W t2 ON t1.OTHER_BH = t2.ID
                WHERE t2.ORDER_SOURCE = '2'
                  AND t2.ID = #{id}
            )
        )
    </select>


    <select id="getJjcdName" resultType="string">
        select decode(#{jjcd},'0','一般','1','重要','2','非常急') as jjcdName from dual
    </select>
    <select id="getLcjdName" resultType="string">
        select decode(#{lcjd},'1','工单登记','2','认证处置','3','认证审核','4','认证质检','5','结果处置','6','结果审核','7','工单归档') as lcjdName from dual
    </select>

    <select id="getDjryName" resultType="string" databaseId="oracle">
        select REAL_NAME djryName
        from TF_SMPN_USER_X
        where id = #{djry}
    </select>
    <select id="getDjryDeptName" resultType="string" databaseId="oracle">
        select NAME as djbmName from TF_SMPN_DEPARTMENT_X where id =
        (select DEPARTMENT_ID
        from TF_SMPN_USER_X
        where id = #{djry})
    </select>

    <select id="getGddjFileIdList" resultType="string" databaseId="oracle">
        select FILE_PATH as filePath
        from TF_YWPN_FILE_W
        <if test="gddjFileId != null and gddjFileId != ''  ">where id in
            <foreach item="nodeCodestr" collection="gddjFileId.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>
        </if>
        <if test="fileIdsOne == null ">
            where id = ''
        </if>

    </select>

    <select id="getPlanTempMaxId" parameterType="java.lang.String" resultType="java.lang.Long">
        select nvl(max(id), 0) + 1
        from ${tableName}
    </select>


    <insert id="addMessageList" parameterType="java.util.List" >
        INSERT ALL
        <foreach item="item" index="index" collection="list">
            INTO TF_YWPN_MESSAGEPUSH_W
            (
            ID,
            TOPIC,
            MESSAGE,
            TYPE,
            TABLE_NAME,
            ROUTE_IP,
            TABLE_ID,
            CREATE_TIME,
            CREATE_USER,
            ROUTE_IP_APP
            ) VALUES
            (
            #{item.id},
            #{item.topic},
            #{item.message},
            #{item.type},
            #{item.tableName},
            #{item.routeIp},
            #{item.tableId},
            #{item.createTime},
            #{item.createUser},
            #{item.routeIpApp}
            )
        </foreach>
        SELECT 1 FROM DUAL
    </insert>

    <insert id="addMessageRecipientList" parameterType="java.util.List" >
        INSERT ALL
        <foreach item="item" index="index" collection="list">
            INTO TF_YWPN_MESSAGERECIPIENT_W
            (
            ID,
            MESSAGE_ID,
            RECIPIENT,
            FLAG
            ) VALUES
            (
            #{item.id},
            #{item.messageId},
            #{item.recipient},
            #{item.flag}
            )
        </foreach>
        SELECT 1 FROM DUAL
    </insert>
    <insert id="addUserTaskList" parameterType="java.util.List">
        INSERT ALL
        <foreach item="item" index="index" collection="list">
            INTO TF_YWPN_USERTASK_W
            (
            ID,
            TKNM,
            TKTY,
            TABLE_NAME,
            TABLE_ID,
            ROUTE_IP,
            ROUTE_APP_IP,
            ISHANDLE,
            HANDLE_USER,
            START_TIME,
            END_TIME,
            CREATE_USER,
            CREATE_TIME
            ) VALUES
            (
            #{item.id},
            #{item.tknm},
            #{item.tkty},
            #{item.tableName},
            #{item.tableId},
            #{item.routeIp},
            #{item.routeAppIp},
            #{item.ishandle},
            #{item.handleUser},
            #{item.startTime},
            #{item.endTime},
            #{item.createUser},
            #{item.createTime}
            )
        </foreach>
        SELECT 1 FROM DUAL
    </insert>

</mapper>
