<?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="java.lang.String">
        select (case #{orderSource}
                    when '0' then '计划'
                    when '1' then '自建'
                    when '2' then '热线'
                    else '' end)
                   as orderSourceName
        from dual
    </select>
    <select id="getTypeName" resultType="java.lang.String">
        select name as typeName
        from TF_YWPN_REPAIRTYPE_D
        where id = #{typeId}
    </select>
    <select id="getRealName" resultType="java.lang.String">
        select REAL_NAME dispatchingIdName
        from TF_SMPN_USER_X
        where id = #{DISPATCHING_ID}
    </select>
    <select id="getFileIdsList" resultType="java.lang.String">
        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="java.lang.String">
        select (case #{state}
                    when '0' then '未派工'
                    when '1' then '未审核'
                    when '2' then '未开始'
                    when '3' then '正在执行'
                    when '4' then '处理完成'
                    when '5' then '已开始未执行'
                    when '6' then '驳回'
                    when '7' then '转移申请'
                    when '8' then '已转移'
                    when '9' then '超时'
                    when '10' then '终止'
                    else '' end)
                   as stateName
        from dual
    </select>
    <select id="getRepairOrderBaseInfo" resultMap="repairOrderBaseInfoVoMap">
        SELECT ID,
               ORDER_CODE,
               TYPE_ID,
               NUM,
               UNIT,
               ORDER_SOURCE,
               ADDRESS,
               NOTES,
               DISPATCHING_ID,
               date_format(CREATE_TIME, '%Y-%m-%d %H:%i:%s') as CREATE_TIME,
               date_format(OVER_TIME, '%Y-%m-%d %H:%i:%s')   as OVER_TIME,
               round((isnull(OVER_NUM, 0) / NUM), 4) * 100   as PERCENT,
               OVER_NUM,
               date_format(START_TIME, '%Y-%m-%d %H:%i:%s')  as START_TIME,
               date_format(END_TIME, '%Y-%m-%d %H:%i:%s')    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="sjdh" column="SJDH"/>
        <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,
               A.SJDH,
               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,
               date_format(A.DJSJ, '%Y-%m-%d %H:%i:%s') 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="java.lang.String">
        select CNAME
        FROM TF_YWPN_SYSCHILDCODE_D
        WHERE ID = #{jjcd}
    </select>
    <select id="getLcjdName" resultType="java.lang.String">
        select (case #{state}
                    when '1' then '工单登记'
                    when '2' then '认证处置'
                    when '3' then '认证审核'
                    when '4' then '认证质检'
                    when '5' then '结果处置'
                    when '6' then '结果审核'
                    when '7' then '工单归档'
                    else '' end)
                   as lcjdName
        from dual
    </select>

    <select id="getDjryName" resultType="java.lang.String">
        select REAL_NAME djryName
        from TF_SMPN_USER_X
        where id = #{djry}
    </select>
    <select id="getDjryDeptName" resultType="java.lang.String">
        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="java.lang.String">
        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 into TF_YWPN_MESSAGEPUSH_W
        <trim prefix="(" suffix=")" suffixOverrides=",">
            ID,
            TOPIC,
            MESSAGE,
            TYPE,
            TABLE_NAME,
            ROUTE_IP,
            CREATE_TIME,
            CREATE_USER,
            ROUTE_IP_APP
        </trim>
        select
        <foreach item="item" collection="list" index="index">
            <if test="index != 0">
                union select
            </if>
            #{item.id},
            #{item.topic},
            #{item.message},
            #{item.type},
            #{item.tableName},
            #{item.routeIp},
            #{item.createTime},
            #{item.createUser},
            #{item.routeIpApp}
        </foreach>
    </insert>

    <insert id="addMessageRecipientList" parameterType="java.util.List">
        insert into TF_YWPN_MESSAGERECIPIENT_W
        <trim prefix="(" suffix=")" suffixOverrides=",">
            ID,
            MESSAGE_ID,
            RECIPIENT,
            FLAG
        </trim>
        select
        <foreach item="item" collection="list" index="index">
            <if test="index != 0">
                union select
            </if>
            #{item.id},
            #{item.messageId},
            #{item.recipient},
            #{item.flag}
            )
        </foreach>
    </insert>
</mapper>
