<?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.zcloud.mapper.datasource.risk.IdeRepositoryMapper">

    <!--表名 -->
    <sql id="tableName">
        BUS_IDEREPOSITORY
    </sql>

    <!--数据字典表名 -->
    <sql id="dicTableName">
        SYS_DICTIONARIES
    </sql>

    <!-- 字段 -->
    <sql id="Field">
        f.CORP_TYPE,
		f.CORP_TYPE2,
		f.CORP_TYPE3,
		f.CORP_TYPE4,
		f.CORP_TYPE_NAME,
		f.RISKTYPE,
		f.RISKUNIT,
		f.IDENTIFICATION,
		f.RISK_DESCR,
		f.LEVELID,
		f.MEASURES,
		f.ACCIDENTS,
		f.EME_MEASURES,
		f.ISDELETE,
		f.RESOURCETYPE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ACCIDENTS_NAME,
		f.LIKELIHOOD,
		f.EXPOSURE,
		f.CONSEQUENCE,
		f.DANGER,
		f.IDEREPOSITORY_ID
    </sql>

    <!-- 字段用于新增 -->
    <sql id="Field2">
        CORP_TYPE,
		CORP_TYPE2,
		CORP_TYPE3,
		CORP_TYPE4,
		CORP_TYPE_NAME,
		RISKTYPE,
		RISKUNIT,
		IDENTIFICATION,
		RISK_DESCR,
		LEVELID,
		MEASURES,
		ACCIDENTS,
		EME_MEASURES,
		ISDELETE,
		RESOURCETYPE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ACCIDENTS_NAME,
		LIKELIHOOD,
		EXPOSURE,
		CONSEQUENCE,
		DANGER,
		IDEREPOSITORY_ID
    </sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
        #{CORP_TYPE},
        #{CORP_TYPE2},
        #{CORP_TYPE3},
        #{CORP_TYPE4},
        #{CORP_TYPE_NAME},
        #{RISKTYPE},
        #{RISKUNIT},
        #{IDENTIFICATION},
        #{RISK_DESCR},
        #{LEVELID},
        #{MEASURES},
        #{ACCIDENTS},
        #{EME_MEASURES},
        #{ISDELETE},
        #{RESOURCETYPE},
        #{CREATOR},
        #{CREATTIME},
        #{OPERATOR},
        #{OPERATTIME},
        #{ACCIDENTS_NAME},
        #{LIKELIHOOD},
        #{EXPOSURE},
        #{CONSEQUENCE},
        #{DANGER},
        #{IDEREPOSITORY_ID}
    </sql>

    <!-- 新增-->
    <insert id="save" parameterType="pd">
        insert into
        <include refid="tableName"></include>
        (
        <include refid="Field2"></include>
        ) values (
        <include refid="FieldValue"></include>
        )
    </insert>

    <!-- 删除-->
    <delete id="delete" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        ISDELETE = '1'
        where
        IDEREPOSITORY_ID = #{IDEREPOSITORY_ID}
    </delete>

    <!-- 修改 -->
    <update id="edit" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        CORP_TYPE = #{CORP_TYPE},
        CORP_TYPE2 = #{CORP_TYPE2},
        CORP_TYPE3 = #{CORP_TYPE3},
        CORP_TYPE4 = #{CORP_TYPE4},
        CORP_TYPE_NAME = #{CORP_TYPE_NAME},
        RISKTYPE = #{RISKTYPE},
        RISKUNIT = #{RISKUNIT},
        IDENTIFICATION = #{IDENTIFICATION},
        RISK_DESCR = #{RISK_DESCR},
        LEVELID = #{LEVELID},
        MEASURES = #{MEASURES},
        ACCIDENTS = #{ACCIDENTS},
        EME_MEASURES = #{EME_MEASURES},
        ACCIDENTS_NAME = #{ACCIDENTS_NAME},
        LIKELIHOOD = #{LIKELIHOOD},
        EXPOSURE = #{EXPOSURE},
        CONSEQUENCE = #{CONSEQUENCE},
        DANGER = #{DANGER},
        IDEREPOSITORY_ID = IDEREPOSITORY_ID
        where
        IDEREPOSITORY_ID = #{IDEREPOSITORY_ID}
    </update>

    <!-- 通过ID获取数据 -->
    <select id="findById" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>,
        r.NAME RISKTYPENAME,
        l.NAME LEVELIDNAME
        from
        <include refid="tableName"></include>
        f
        LEFT JOIN sys_dictionaries r ON r.BIANMA = f.RISKTYPE
        AND r.YNDEL = 'NO'
        LEFT JOIN sys_dictionaries l ON l.BIANMA = f.LEVELID
        AND l.YNDEL = 'NO'
        where
        f.IDEREPOSITORY_ID = #{IDEREPOSITORY_ID}
    </select>

    <!-- 列表 -->
    <select id="datalistPage" parameterType="page" resultType="pd">
        SELECT
        *
        FROM
        (
        SELECT
        f.IDEREPOSITORY_ID ID,
        f.CORP_TYPE_NAME,
        r.NAME RISKTYPENAME,
        f.RISKUNIT,
        f.IDENTIFICATION,
        f.RISK_DESCR,
        l.NAME LEVELIDNAME,
        f.MEASURES,
        f.ACCIDENTS,
        f.EME_MEASURES,
        f.ISDELETE,
        f.RESOURCETYPE,
        f.CREATOR,
        f.CREATTIME,
        f.OPERATOR,
        f.OPERATTIME,
        f.ACCIDENTS_NAME
        FROM
        BUS_IDEREPOSITORY f
        LEFT JOIN sys_dictionaries r ON r.BIANMA = f.RISKTYPE
        LEFT JOIN sys_dictionaries l ON l.BIANMA = f.LEVELID
        WHERE
        f.ISDELETE = 0
        <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
            and
            (
            f.RISKUNIT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            or
            f.IDENTIFICATION LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            )
        </if>
        UNION ALL
        SELECT
        f.RISKPOINT_ID ID,
        '' INDUSTRYNAME,
        '' RISKTYPENAME,
        un.RISKUNITNAME RISKUNIT,
        id.PARTSNAME IDENTIFICATION,
        f.RISK_DESCR,
        d5.NAME LEVELIDNAME,
        f.MEASURES,
        f.ACCIDENTS,
        f.EME_MEASURES,
        f.ISDELETE,
        '2' RESOURCETYPE,
        f.CREATOR,
        f.CREATTIME,
        f.OPERATOR,
        f.OPERATTIME,
        f.ACCIDENTS_NAME
        FROM
        bus_riskpoint f
        LEFT JOIN sys_dictionaries d5 ON f.LEVELID = d5.BIANMA
        LEFT JOIN bus_riskunit un ON un.RISKUNIT_ID = f.RISK_UNIT_ID
        AND un.ISDELETE = 0
        LEFT JOIN bus_identificationparts id ON id.IDENTIFICATIONPARTS_ID = f.IDENTIFICATION_ID
        AND id.ISDELETE = 0
        WHERE
        f.ISDELETE = 0
        and f.RES_ISDELETE = 0
        <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
            and
            (
            un.RISKUNITNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            or
            id.PARTSNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            )
        </if>
        <if test="pd.ArrayDATA_IDS != null and pd.ArrayDATA_IDS != ''">
            AND f.CORPINFO_ID IN
            <foreach item="item" index="index" collection="pd.ArrayDATA_IDS" open="(" separator="," close=")">
                '${item}'
            </foreach>
        </if>
        ) w
        ORDER BY
        w.RESOURCETYPE,
        w.RISKUNIT,
        w.IDENTIFICATION
    </select>

    <!-- 列表 -->
    <select id="resdatalistPage" parameterType="page" resultType="pd">
        SELECT
        f.IDEREPOSITORY_ID ID,
        f.CORP_TYPE_NAME,
        r.NAME RISKTYPENAME,
        f.RISKUNIT,
        f.IDENTIFICATION,
        f.RISK_DESCR,
        l.NAME LEVELIDNAME,
        f.MEASURES,
        f.ACCIDENTS,
        f.EME_MEASURES,
        f.ISDELETE,
        f.RESOURCETYPE,
        f.LEVELID,
        f.CREATOR,
        f.CREATTIME,
        f.OPERATOR,
        f.OPERATTIME,
        f.ACCIDENTS_NAME
        FROM
        BUS_IDEREPOSITORY f
        LEFT JOIN sys_dictionaries r ON r.BIANMA = f.RISKTYPE
        LEFT JOIN sys_dictionaries l ON l.BIANMA = f.LEVELID
        WHERE
        f.ISDELETE = 0
        <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
            and
            (
            f.RISKUNIT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            or
            f.IDENTIFICATION LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            )
        </if>
        <if test="pd.LEVELID != null and pd.LEVELID != ''">
            and f.LEVELID=#{pd.LEVELID}
        </if>
        <if test="pd.CORP_TYPE != null and pd.CORP_TYPE != ''">
            and f.CORP_TYPE=#{pd.CORP_TYPE}
        </if>
        <if test="pd.CORP_TYPE2 != null and pd.CORP_TYPE2 != ''">
            and f.CORP_TYPE2=#{pd.CORP_TYPE2}
        </if>
        <if test="pd.CORP_TYPE3 != null and pd.CORP_TYPE3 != ''">
            and f.CORP_TYPE3=#{pd.CORP_TYPE3}
        </if>
        <if test="pd.CORP_TYPE4 != null and pd.CORP_TYPE4 != ''">
            and f.CORP_TYPE4=#{pd.CORP_TYPE4}
        </if>
        <if test="pd.BIANMA != null and pd.BIANMA != ''">
            and f.ACCIDENTS LIKE CONCAT(CONCAT('%', #{pd.BIANMA}),'%')
        </if>
        <if test="pd.RISKTYPE != null and pd.RISKTYPE != ''">
            and f.RISKTYPE=#{pd.RISKTYPE}
        </if>
        ORDER BY
        f.RESOURCETYPE,
        f.RISKUNIT,
        f.IDENTIFICATION
    </select>

    <!-- 列表 -->
    <select id="riskdatalistPage" parameterType="page" resultType="pd">
        SELECT
        f.RISKPOINT_ID ID,
        '' INDUSTRYNAME,
        '' RISKTYPENAME,
        un.RISKUNITNAME RISKUNIT,
        id.PARTSNAME IDENTIFICATION,
        f.RISK_DESCR,
        d5.NAME LEVELIDNAME,
        f.MEASURES,
        f.ACCIDENTS,
        f.EME_MEASURES,
        f.LEVELID,
        f.ISDELETE,
        '2' RESOURCETYPE,
        f.CREATOR,
        f.CREATTIME,
        f.OPERATOR,
        f.OPERATTIME,
        f.ACCIDENTS_NAME
        FROM
        bus_riskpoint f
        LEFT JOIN sys_dictionaries d5 ON f.LEVELID = d5.BIANMA
        LEFT JOIN bus_riskunit un ON un.RISKUNIT_ID = f.RISK_UNIT_ID
        AND un.ISDELETE = 0
        LEFT JOIN bus_identificationparts id ON id.IDENTIFICATIONPARTS_ID = f.IDENTIFICATION_ID
        AND id.ISDELETE = 0
        WHERE
        f.ISDELETE = 0
        and f.RES_ISDELETE = 0
        <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
            and
            (
            un.RISKUNITNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            or
            id.PARTSNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            )
        </if>
        <if test="pd.ArrayDATA_IDS != null and pd.ArrayDATA_IDS != ''">
            AND f.CORPINFO_ID IN
            <foreach item="item" index="index" collection="pd.ArrayDATA_IDS" open="(" separator="," close=")">
                '${item}'
            </foreach>
        </if>
        <if test="pd.LEVELID != null and pd.LEVELID != ''">
            and f.LEVELID=#{pd.LEVELID}
        </if>
        <if test="pd.BIANMA != null and pd.BIANMA != ''">
            and f.ACCIDENTS LIKE CONCAT(CONCAT('%', #{pd.BIANMA}),'%')
        </if>
        ORDER BY
        f.RISK_UNIT_ID,
        f.IDENTIFICATION_ID
    </select>


    <!-- 列表(全部) -->
    <select id="listAll" parameterType="pd" resultType="pd">
        SELECT
        f.IDEREPOSITORY_ID ID,
        r.NAME RISKTYPENAME,
        f.RISKUNIT,
        f.IDENTIFICATION,
        f.RISK_DESCR,
        l.NAME LEVELIDNAME,
        f.MEASURES,
        f.ACCIDENTS,
        f.EME_MEASURES,
        f.ISDELETE,
        f.RESOURCETYPE,
        f.CREATOR,
        f.CREATTIME,
        f.OPERATOR,
        f.OPERATTIME,
        f.ACCIDENTS_NAME
        FROM
        BUS_IDEREPOSITORY f
        LEFT JOIN sys_dictionaries r ON r.BIANMA = f.RISKTYPE
        LEFT JOIN sys_dictionaries l ON l.BIANMA = f.LEVELID
        WHERE
        f.ISDELETE = 0 and f.RESOURCETYPE = 2
        <if test="CORP_TYPE != null and CORP_TYPE != ''">
            and f.CORP_TYPE=#{CORP_TYPE}
        </if>
        <if test="CORP_TYPE2 != null and CORP_TYPE2 != ''">
            and f.CORP_TYPE2=#{CORP_TYPE2}
        </if>
        <if test="CORP_TYPE3 != null and CORP_TYPE3 != ''">
            and f.CORP_TYPE3=#{CORP_TYPE3}
        </if>
        <if test="CORP_TYPE4 != null and CORP_TYPE4 != ''">
            and f.CORP_TYPE4=#{CORP_TYPE4}
        </if>
        <if test="IDENTIFICATION != null and IDENTIFICATION != ''">
            and f.IDENTIFICATION=#{IDENTIFICATION}
        </if>
        <if test="RISKUNIT != null and RISKUNIT != ''">
            and f.RISKUNIT=#{RISKUNIT}
        </if>
        <if test="rptResIds != null and rptResIds != ''">
            and f.IDEREPOSITORY_ID not in
            <foreach item="item" index="index" collection="rptResIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ORDER BY
        f.RESOURCETYPE,
        f.RISKUNIT,
        f.IDENTIFICATION
    </select>

    <!-- 验证是否重复 -->
    <select id="checkHas" parameterType="pd" resultType="int">
        select
            count(f.IDEREPOSITORY_ID)
        from
            <include refid="tableName"></include> f
        WHERE
            f.ISDELETE = 0
            <if test="CORP_TYPE != null and CORP_TYPE != ''">
                and f.CORP_TYPE=#{CORP_TYPE}
            </if>
            <if test="CORP_TYPE2 != null and CORP_TYPE2 != ''">
                and f.CORP_TYPE2=#{CORP_TYPE2}
            </if>
            <if test="CORP_TYPE3 != null and CORP_TYPE3 != ''">
                and f.CORP_TYPE3=#{CORP_TYPE3}
            </if>
            <if test="CORP_TYPE4 != null and CORP_TYPE4 != ''">
                and f.CORP_TYPE4=#{CORP_TYPE4}
            </if>
            <if test="IDENTIFICATION != null and IDENTIFICATION != ''">
                and f.IDENTIFICATION=#{IDENTIFICATION}
            </if>
            <if test="RISKUNIT != null and RISKUNIT != ''">
                and f.RISKUNIT=#{RISKUNIT}
            </if>
            <if test="MEASURES != null and MEASURES != ''">
                and f.MEASURES=#{MEASURES}
            </if>
            <if test="RISK_DESCR != null and RISK_DESCR != ''">
                and f.RISK_DESCR=#{RISK_DESCR}
            </if>
            <if test="EME_MEASURES != null and EME_MEASURES != ''">
                and f.EME_MEASURES=#{EME_MEASURES}
            </if>
    </select>


    <!-- 批量删除 -->
    <delete id="deleteAll" parameterType="String">
        update
        <include refid="tableName"></include>
        set
        ISDELETE = '1'
        where
        IDEREPOSITORY_ID in
        <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

    <!-- 通过ID获取数据 -->
    <select id="listAllByIds" parameterType="String" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include> f
        where
        f.IDEREPOSITORY_ID in
        <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

</mapper>
