<?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.xrtymis.mapper.GdflowconfigMapper">

    <resultMap id="gdflowconfigMap" type="com.tofly.xrtymis.entity.Gdflowconfig">
        <id property="id" column="ID"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="gdlx" column="GDLX"/>
        <result property="ssbm" column="SSBM"/>
        <result property="gdzsx" column="GDZSX"/>
        <result property="gddjSfqy" column="GDDJ_SFQY"/>
        <result property="gddjJdsx" column="GDDJ_JDSX"/>
        <result property="gddjSfbh" column="GDDJ_SFBH"/>
        <result property="rzczSfqy" column="RZCZ_SFQY"/>
        <result property="rzczCzsx" column="RZCZ_CZSX"/>
        <result property="rzczSfbh" column="RZCZ_SFBH"/>
        <result property="rzshSfqy" column="RZSH_SFQY"/>
        <result property="rzshShsx" column="RZSH_SHSX"/>
        <result property="rzshSfbh" column="RZSH_SFBH"/>
        <result property="rzzjSfqy" column="RZZJ_SFQY"/>
        <result property="rzzjZjsx" column="RZZJ_ZJSX"/>
        <result property="rzzjSfbh" column="RZZJ_SFBH"/>
        <result property="jgczSfqy" column="JGCZ_SFQY"/>
        <result property="jgczCzsx" column="JGCZ_CZSX"/>
        <result property="jgczSfbh" column="JGCZ_SFBH"/>
        <result property="jgshSfqy" column="JGSH_SFQY"/>
        <result property="jgshShsx" column="JGSH_SHSX"/>
        <result property="jgshSfbh" column="JGSH_SFBH"/>
        <result property="jgshCcjb" column="JGSH_CCJB"/>
        <result property="gdxf1" column="GDXF1"/>
        <result property="gdxf2" column="GDXF2"/>
        <result property="sfDelete" column="SF_DELETE"/>
        <result property="allLcjd" column="ALL_LCJD"/>
        <result property="transferOutType" column="TRANSFER_OUT_TYPE"/>
        <result property="tabType" column="TAB_TYPE"/>
        <result property="viewType" column="VIEW_TYPE"/>

        <result property="ssbmName" column="DEPART_NAME"/>
        <association property="gddjSfqyName" column="GDDJ_SFQY" select="gddjSfqyName"/>
        <association property="gddjSfbhName" column="GDDJ_SFBH" select="gddjSfbhName"/>
        <association property="rzczSfqyName" column="RZCZ_SFQY" select="rzczSfqyName"/>
        <association property="rzczSfbhName" column="RZCZ_SFBH" select="rzczSfbhName"/>
        <association property="rzshSfqyName" column="RZSH_SFQY" select="rzshSfqyName"/>
        <association property="rzshSfbhName" column="RZSH_SFBH" select="rzshSfbhName"/>
        <association property="rzzjSfqyName" column="RZZJ_SFQY" select="rzzjSfqyName"/>
        <association property="rzzjSfbhName" column="RZZJ_SFBH" select="rzzjSfbhName"/>
        <association property="jgczSfqyName" column="JGCZ_SFQY" select="jgczSfqyName"/>
        <association property="jgczSfbhName" column="JGCZ_SFBH" select="jgczSfbhName"/>
        <association property="jgshSfqyName" column="JGSH_SFQY" select="jgshSfqyName"/>
        <association property="jgshSfbhName" column="JGSH_SFBH" select="jgshSfbhName"/>
        <association property="jgshCcjbName" column="JGSH_SCJB" select="jgshCcjbName"/>
        <association property="gdlxName" column="GDLX" select="gdlxName"/>
        <association property="gdxf1Name" column="GDXF1" select="gdlxName"/>
        <association property="gdxf2Name" column="GDXF2" select="gdlxName"/>
        <!--        <association property="ssbmName" column="SSBM" select="ssbmName"/>-->
        <association property="transferOutTypeName" column="TRANSFER_OUT_TYPE" select="codeTypeName"/>
        <association property="tabTypeName" column="TAB_TYPE" select="codeTypeName"/>
        <!--        <collection property="ssbmList" column="ID" select="getDepartList"/>-->
    </resultMap>

    <select id="codeTypeName" resultType="string">
        select CNAME
        FROM TF_YWPN_SYSCHILDCODE_D
        WHERE ID = #{id}
    </select>


    <select id="gdlxName" resultType="string">
        SELECT GDLX_MC
        FROM "TF_YWPN_GDGLTYPEBM_W"
        WHERE ID = #{gdlx}
    </select>
    <select id="ssbmName" resultType="string">
        select NAME
        FROM "TF_XRTY_NEW_UDB"."TF_SMPN_DEPARTMENT_X"
        WHERE ID = #{ssbm}
    </select>


    <select id="gddjSfqyName" resultType="string">
        select decode(#{gddjSfqy}, '0', '未启用', '1', '启用')
        from dual
    </select>
    <select id="rzczSfqyName" resultType="string">
        select decode(#{rzczSfqy}, '0', '未启用', '1', '启用')
        from dual
    </select>
    <select id="rzshSfqyName" resultType="string">
        select decode(#{rzshSfqy}, '0', '未启用', '1', '启用')
        from dual
    </select>
    <select id="rzzjSfqyName" resultType="string">
        select decode(#{rzzjSfqy}, '0', '未启用', '1', '启用')
        from dual
    </select>
    <select id="jgczSfqyName" resultType="string">
        select decode(#{jgczSfqy}, '0', '未启用', '1', '启用')
        from dual
    </select>
    <select id="jgshSfqyName" resultType="string">
        select decode(#{jgshSfqy}, '0', '未启用', '1', '启用')
        from dual
    </select>
    <select id="jgshCcjbName" resultType="string">
        select decode(#{jgshCcjb}, '0', '未启用', '1', '启用')
        from dual
    </select>


    <select id="gddjSfbhName" resultType="string">
        select decode(#{gddjSfbh}, '0', '没有驳回', '1', '有驳回')
        from dual
    </select>
    <select id="rzczSfbhName" resultType="string">
        select decode(#{rzczSfbh}, '0', '没有驳回', '1', '有驳回')
        from dual
    </select>
    <select id="rzshSfbhName" resultType="string">
        select decode(#{rzshSfbh}, '0', '没有驳回', '1', '有驳回')
        from dual
    </select>
    <select id="rzzjSfbhName" resultType="string">
        select decode(#{rzzjSfbh}, '0', '没有驳回', '1', '有驳回')
        from dual
    </select>
    <select id="jgczSfbhName" resultType="string">
        select decode(#{jgczSfbh}, '0', '没有驳回', '1', '有驳回')
        from dual
    </select>
    <select id="jgshSfbhName" resultType="string">
        select decode(#{jgshSfbh}, '0', '没有驳回', '1', '有驳回')
        from dual
    </select>

    <select id="getMaxId" resultType="java.lang.Long">
        select nvl(max(id), 0) + 1
        from TF_YWPN_GDYWCONFIG_W
    </select>

    <select id="selectExist" resultMap="gdflowconfigMap">
        SELECT GDLX , GDXF1 , GDXF2 FROM TF_YWPN_GDYWCONFIG_W
        WHERE SF_DELETE = '1'
        AND ('工单类型' || GDLX || '细分一' || GDXF1 || '细分二' || GDXF2) IN
        <foreach collection="list" open="(" separator="," item="gdlxKey" close=")">
            #{gdlxKey}
        </foreach>
    </select>

    <insert id="insertBatchConfig">
        insert into TF_YWPN_GDYWCONFIG_W (ID , GDLX , GDXF1 , GDXF2 ,
        GDZSX , GDDJ_JDSX , GDDJ_SFQY , RZCZ_SFQY , RZCZ_CZSX , RZSH_SFQY , RZSH_SHSX , RZZJ_SFQY , RZZJ_ZJSX ,
        JGCZ_SFQY ,
        JGCZ_CZSX , JGSH_SFQY , JGSH_SHSX , JGSH_CCJB , ALL_LCJD , TRANSFER_OUT_TYPE , TAB_TYPE , CREATE_TIME ,
        UPDATE_TIME ,CREATE_USER,VIEW_TYPE)
        <foreach collection="list" item="info" separator="union all">
            SELECT
            #{info.id,jdbcType=NUMERIC},
            #{info.gdlx,jdbcType=NUMERIC},
            #{info.gdxf1,jdbcType=NUMERIC},
            #{info.gdxf2,jdbcType=NUMERIC},
            #{flowConfig.gdzsx,jdbcType=NUMERIC},
            #{flowConfig.gddjJdsx,jdbcType=NUMERIC},
            #{flowConfig.gddjSfqy,jdbcType=VARCHAR},
            #{flowConfig.rzczSfqy,jdbcType=VARCHAR},
            #{flowConfig.rzczCzsx,jdbcType=NUMERIC},
            #{flowConfig.rzshSfqy,jdbcType=VARCHAR},
            #{flowConfig.rzshShsx,jdbcType=NUMERIC},
            #{flowConfig.rzzjSfqy,jdbcType=VARCHAR},
            #{flowConfig.rzzjZjsx,jdbcType=NUMERIC},
            #{flowConfig.jgczSfqy,jdbcType=VARCHAR},
            #{flowConfig.jgczCzsx,jdbcType=NUMERIC},
            #{flowConfig.jgshSfqy,jdbcType=VARCHAR},
            #{flowConfig.jgshShsx,jdbcType=NUMERIC},
            #{flowConfig.jgshCcjb,jdbcType=NUMERIC},
            #{flowConfig.allLcjd,jdbcType=VARCHAR},
            #{flowConfig.transferOutType,jdbcType=NUMERIC},
            #{flowConfig.tabType,jdbcType=NUMERIC},
            #{time,jdbcType=TIMESTAMP},
            #{time,jdbcType=TIMESTAMP},
            #{userId,jdbcType=NUMERIC},
            #{info.viewType,jdbcType=VARCHAR}
            FROM DUAL
        </foreach>
    </insert>

    <!--查询多部门-->
    <select id="getDepartList" resultType="com.tofly.xrtymis.entity.Gddepartment">
        SELECT DEPART_ID as departId, DEPART_NAME as departName , CONFIG_ID as configId
        FROM TF_YWPN_GDDEPARTMENT_W
        WHERE CONFIG_ID = #{configId}
    </select>
    <!--查询多人员-->
    <select id="getPersonList" resultType="com.tofly.xrtymis.entity.Gdperson">
        SELECT DEPART_ID   as departId,
               DEPART_NAME as departName,
               PERSON_ID   as personId,
               PERSON_NAME as personName,
               NODE        as node,
               CHILD_NODE  as childNode,
               CONFIG_ID as configId
        FROM TF_YWPN_GDPERSON_W
        WHERE CONFIG_ID = #{configId}
    </select>
    <resultMap id="configPersonDepartMap" type="com.tofly.xrtymis.custom.entity.OrderTypeByIdResult"
               extends="gdflowconfigMap">
        <collection property="gddepartmentList" column="ID" select="getDepartList"/>
        <collection property="gdpersonList" column="ID" select="getPersonList"/>
    </resultMap>
    <select id="getConfig" resultMap="configPersonDepartMap">
        SELECT a.* , b.DEPART_NAME
        FROM TF_YWPN_GDYWCONFIG_W a
        JOIN
        (SELECT CONFIG_ID , wm_concat(DEPART_NAME) DEPART_NAME
        FROM TF_YWPN_GDDEPARTMENT_W
        <if test="departList != null">
            WHERE DEPART_ID IN
            <foreach collection="departList" open="(" item="depart" separator="," close=")">
                #{depart}
            </foreach>
        </if>
        GROUP BY CONFIG_ID) b
        ON a.ID = b.CONFIG_ID
        <where>
            <if test="sfDelete != '' and sfDelete != null">
                a.SF_DELETE = #{sfDelete}
            </if>
            <if test="typeList != null">
                AND (
                a.GDLX in
                <foreach collection="typeList" open="(" item="type" separator="," close=")">
                    #{type}
                </foreach>
                OR a.GDXF1 in
                <foreach collection="typeList" open="(" item="type" separator="," close=")">
                    #{type}
                </foreach>
                OR a.GDXF2 in
                <foreach collection="typeList" open="(" item="type" separator="," close=")">
                    #{type}
                </foreach>
                )
            </if>
            <if test="gdlx != null">
                AND a.GDLX = #{gdlx}
            </if>
            <if test="viewType != null and viewType != ''">
                AND a.VIEW_TYPE = #{viewType}
            </if>
        </where>
    </select>

    <resultMap id="OrderTypeMap" type="com.tofly.xrtymis.custom.vo.ConfingTypeVo">
        <association property="name" column="TYPE_ID" select="com.tofly.xrtymis.mapper.GdgltypebmMapper.getTypeName"/>
    </resultMap>
    <select id="getOrderType" resultMap="OrderTypeMap">
        SELECT id, DECODE(GDXF2,NULL,DECODE(GDXF1,NULL,GDLX,GDXF1),GDXF2) TYPE_ID FROM TF_YWPN_GDYWCONFIG_W where VIEW_TYPE like '%989%' AND  SF_DELETE = 1
    </select>

</mapper>
