<?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.IdentificationPartsMapper">

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

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

    <!-- 字段 -->
    <sql id="Field">
        f
        .
        PARTSNAME
        ,
		f.CORPINFO_ID,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.LONGITUDE,
		f.LATITUDE,
        f.ELECTRONIC_FENCE_ID,
		f.ELECTRONIC_FENCE_NAME,
		f.ELECTRONIC_FENCE_AREA_ID,
		f.IDENTIFICATIONPARTS_ID,
		f.RISK_UNIT_ID,
		f.ISMATCHING
    </sql>

    <!-- 字段用于新增 -->
    <sql id="Field2">
        PARTSNAME
        ,
		CORPINFO_ID,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		LONGITUDE,
		LATITUDE,
        ELECTRONIC_FENCE_ID,
		ELECTRONIC_FENCE_NAME,
		ELECTRONIC_FENCE_AREA_ID,
		IDENTIFICATIONPARTS_ID,
		RISK_UNIT_ID,
		ISMATCHING
    </sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
        #{PARTSNAME},
        #{CORPINFO_ID},
        #{CREATOR},
        #{CREATTIME},
        #{OPERATOR},
        #{OPERATTIME},
        #{ISDELETE},
        #{LONGITUDE},
        #{LATITUDE},
		#{ELECTRONIC_FENCE_ID},
		#{ELECTRONIC_FENCE_NAME},
		#{ELECTRONIC_FENCE_AREA_ID},
        #{IDENTIFICATIONPARTS_ID},
        #{RISK_UNIT_ID},
        #{ISMATCHING}
    </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',
        OPERATOR = #{OPERATOR},
        OPERATTIME = #{OPERATTIME}
        where
        IDENTIFICATIONPARTS_ID = #{IDENTIFICATIONPARTS_ID}
    </delete>

    <!-- 修改 -->
    <update id="edit" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        PARTSNAME = #{PARTSNAME},
        OPERATOR = #{OPERATOR},
        OPERATTIME = #{OPERATTIME},
        LONGITUDE = #{LONGITUDE},
        LATITUDE = #{LATITUDE},
        ELECTRONIC_FENCE_ID = #{ELECTRONIC_FENCE_ID},
        ELECTRONIC_FENCE_NAME = #{ELECTRONIC_FENCE_NAME},
        ELECTRONIC_FENCE_AREA_ID = #{ELECTRONIC_FENCE_AREA_ID},
        RISK_UNIT_ID=#{RISK_UNIT_ID},
        IDENTIFICATIONPARTS_ID = IDENTIFICATIONPARTS_ID
        where
        IDENTIFICATIONPARTS_ID = #{IDENTIFICATIONPARTS_ID}
    </update>
    <!-- 修改匹配资源库状态 -->
    <update id="editMatching" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        ISMATCHING = #{ISMATCHING}
        where
        IDENTIFICATIONPARTS_ID = #{IDENTIFICATIONPARTS_ID}
    </update>

    <!-- 通过ID获取数据 -->
    <select id="findById" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>,
        un.RISKUNITNAME,
        d.NAME DEPTNAME,
        d.DEPARTMENT_ID
        from
        <include refid="tableName"></include>
        f
        left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID
        left join oa_department d on d.DEPARTMENT_ID = un.DEPARTMENT_ID
        where
        f.IDENTIFICATIONPARTS_ID = #{IDENTIFICATIONPARTS_ID}
    </select>
    <!-- 通过ID获取数据 -->
    <select id="findByName" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where 1=1 and f.ISDELETE = '0'

        <if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
            and f.CORPINFO_ID = #{CORPINFO_ID}
        </if>
        <if test="PARTSNAME != null and PARTSNAME != ''"><!-- 重名验证专用 -->
            AND
            f.PARTSNAME = #{PARTSNAME}
        </if>
        <if test="RISK_UNIT_ID != null and RISK_UNIT_ID != ''"><!-- 重名验证专用 -->
            AND
            f.RISK_UNIT_ID = #{RISK_UNIT_ID}
        </if>
    </select>

    <!-- 列表 -->
    <select id="datalistPage" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>,
        un.RISKUNITNAME,
        de.NAME as DEPT_NAME,
        min(r.LEVELID) LEVELID
        from
        <include refid="tableName"></include>
        f
        left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
        left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID
        left join OA_DEPARTMENT de on de.DEPARTMENT_ID = un.DEPARTMENT_ID
        LEFT JOIN bus_riskpoint r ON r.IDENTIFICATION_ID = f.IDENTIFICATIONPARTS_ID
        where f.ISDELETE = '0'
        <if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
            and f.CORPINFO_ID = #{pd.CORPINFO_ID}
        </if>
        <if test="pd.RISK_UNIT_ID != null and pd.RISK_UNIT_ID != ''"><!-- 关键词检索 -->
            and f.RISK_UNIT_ID = #{pd.RISK_UNIT_ID}
        </if>
        <!--		<if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">&lt;!&ndash; 部门 &ndash;&gt;-->
        <!--			and de.DEPARTMENT_ID = #{pd.DEPARTMENT_ID}-->
        <!--		</if>-->
        <choose>
            <when test='pd.ISMAIN != null and pd.ISMAIN != "" and pd.ISMAIN == "1"'>
                <if test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
                    and
                    un.DEPARTMENT_ID in
                    <foreach item="item" index="index"
                             collection="pd.DEPT_IDS" open="(" separator="," close=")">
                        '${item}'
                    </foreach>
                </if>
            </when>
            <otherwise>
                <choose>
                    <when test='pd.ISSUPERVISE != null and pd.ISSUPERVISE != "" and pd.ISSUPERVISE == "1"'>
                        <choose>
                            <when test='pd.ISLEADER != null and pd.ISLEADER != "" and pd.ISLEADER == "1"'><!-- 监管部门的部门领导需要查看下级部门和监管部门的 -->
                                <choose>
                                    <when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
                                        and
                                        un.DEPARTMENT_ID in
                                        <foreach item="item" index="index"
                                                 collection="pd.DEPT_IDS" open="(" separator="," close=")">
                                            '${item}'
                                        </foreach>
                                    </when>
                                    <otherwise>
                                        and
                                        ((un.DEPARTMENT_ID in (
                                        select
                                        SUB_DEPARTMENT_ID
                                        from
                                        oa_supervision_department osd
                                        where
                                        osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
                                        and osd.ISDELETE = '0'
                                        and osd.CORPINFO_ID = #{pd.CORPINFO_ID}
                                        )) or (
                                        un.DEPARTMENT_ID in
                                        <foreach item="item" index="index"
                                                 collection="pd.DEPARTMENT_IDS" open="(" separator="," close=")">
                                            '${item}'
                                        </foreach>
                                        ))
                                    </otherwise>
                                </choose>
                            </when>
                            <when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
                                and
                                un.DEPARTMENT_ID in
                                <foreach item="item" index="index"
                                         collection="pd.DEPT_IDS" open="(" separator="," close=")">
                                    '${item}'
                                </foreach>
                            </when>
                            <otherwise>
                                and
                                (un.DEPARTMENT_ID in (
                                select
                                SUB_DEPARTMENT_ID
                                from
                                oa_supervision_department osd
                                where
                                osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
                                and osd.ISDELETE = '0'
                                and osd.CORPINFO_ID = #{pd.CORPINFO_ID})
                                or un.DEPARTMENT_ID = #{pd.DEPARTMENT_ID})<!-- 监管部门的普通用户需要查看自己部门的 -->
                            </otherwise>
                        </choose>
                    </when>

                    <when test='pd.ISLEADER != null and pd.ISLEADER != "" and pd.ISLEADER == "1"'>
                        <choose>
                            <when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
                                and
                                un.DEPARTMENT_ID in
                                <foreach item="item" index="index"
                                         collection="pd.DEPT_IDS" open="(" separator="," close=")">
                                    '${item}'
                                </foreach>
                            </when>
                            <otherwise>
                                and
                                un.DEPARTMENT_ID in
                                <foreach item="item" index="index"
                                         collection="pd.DEPARTMENT_IDS" open="(" separator="," close=")">
                                    '${item}'
                                </foreach>
                            </otherwise>
                        </choose>
                    </when>
                    <when test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
                        and un.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
                    </when>
                    <otherwise>
                    </otherwise>
                </choose>
            </otherwise>
        </choose>
        <if test="pd.LEVELID != null and pd.LEVELID != ''"><!-- 部门 -->
            and r.LEVELID = #{pd.LEVELID}
        </if>
        <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
            and
            (
            f.PARTSNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            or
            f.RISK_UNIT_ID in ( select RISKUNIT_ID from BUS_RISKUNIT where RISKUNITNAME LIKE CONCAT(CONCAT('%',
            #{pd.KEYWORDS}),'%') )
            )
        </if>
        <if test="pd.IS_APP != null and pd.IS_APP != ''"><!-- app独占 -->
            and r.LEVELID IS NOT NULL
        </if>
        GROUP BY f.IDENTIFICATIONPARTS_ID
        order by f.OPERATTIME desc
    </select>

    <!-- 列表(全部) -->
    <select id="listAll" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>,
        c.CORP_NAME,
        un.RISKUNITNAME
        from
        <include refid="tableName"></include>
        f
        left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
        left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID
        where f.ISDELETE = '0'
        <if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
            and f.CORPINFO_ID = #{CORPINFO_ID}
        </if>
        <if test="RISK_UNIT_ID != null and RISK_UNIT_ID != ''"><!-- 关键词检索 -->
            and f.RISK_UNIT_ID = #{RISK_UNIT_ID}
        </if>
        <if test="RISKUNIT_ID != null and RISKUNIT_ID != ''"><!-- 关键词检索 -->
            and f.RISK_UNIT_ID = #{RISKUNIT_ID}
        </if>
        <if test="CHECK_NAME != null and CHECK_NAME != ''"><!-- 关键词检索 -->
            and f.PARTSNAME = #{CHECK_NAME}
        </if>
        <if test="unRISKUNITNAME !=null and unRISKUNITNAME !=''">
            and un.RISKUNITNAME = #{unRISKUNITNAME}
        </if>
        order by f.OPERATTIME desc
    </select>
    <select id="listOut" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>,
        c.CORP_NAME,
        un.RISKUNITNAME
        from
        <include refid="tableName"></include>
        f
        left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
        left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID
        where f.ISDELETE = '0' and f.IDENTIFICATIONPARTS_ID in
        <foreach item="item" index="index" open="(" separator="," close=")" collection="ArrayDATA_IDS">
            #{item}
        </foreach>
        <if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
            and f.CORPINFO_ID = #{CORPINFO_ID}
        </if>
        <if test="RISK_UNIT_ID != null and RISK_UNIT_ID != ''"><!-- 关键词检索 -->
            and f.RISK_UNIT_ID = #{RISK_UNIT_ID}
        </if>
        <if test="RISKUNIT_ID != null and RISKUNIT_ID != ''"><!-- 关键词检索 -->
            and f.RISK_UNIT_ID = #{RISKUNIT_ID}
        </if>
        <if test="CHECK_NAME != null and CHECK_NAME != ''"><!-- 关键词检索 -->
            and f.PARTSNAME = #{CHECK_NAME}
        </if>
        <if test="unRISKUNITNAME !=null and unRISKUNITNAME !=''">
            and un.RISKUNITNAME = #{unRISKUNITNAME}
        </if>
        order by f.OPERATTIME desc

    </select>

    <!-- 列表(全部) -->
    <select id="listAllByIdens" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>,
        c.CORP_NAME,
        un.RISKUNITNAME
        from
        <include refid="tableName"></include>
        f
        left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
        left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID
        where f.ISDELETE = '0'
        <if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
            and f.CORPINFO_ID = #{CORPINFO_ID}
        </if>
        <if test="RISK_UNIT_ID != null and RISK_UNIT_ID != ''"><!-- 关键词检索 -->
            and f.RISK_UNIT_ID = #{RISK_UNIT_ID}
        </if>
        <if test="RISKUNIT_ID != null and RISKUNIT_ID != ''"><!-- 关键词检索 -->
            and f.RISK_UNIT_ID = #{RISKUNIT_ID}
        </if>
        <if test="CHECK_NAME != null and CHECK_NAME != ''"><!-- 关键词检索 -->
            and f.PARTSNAME = #{CHECK_NAME}
        </if>
        <if test="ArrayDATA_IDS != null and ArrayDATA_IDS != ''"><!-- 关键词检索 -->
            and
            (
            f.IDENTIFICATIONPARTS_ID in
            <foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                #{item}
            </foreach>
            )
        </if>
        order by f.OPERATTIME desc
    </select>

    <!-- 列表(全部) -->
    <select id="listIndexAll" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>,
        c.CORP_NAME,
        un.RISKUNITNAME
        from
        <include refid="tableName"></include>
        f
        left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
        left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID
        where f.ISDELETE = '0'
        <if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
            and f.CORPINFO_ID = #{CORPINFO_ID}
        </if>
        <!-- 		group by un.RISKUNITNAME,f.PARTSNAME -->
        order by f.OPERATTIME desc
    </select>

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

    <!-- 清单所有部位(全部) -->
    <select id="listAllByList" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>,
        un.RISKUNITNAME
        from
        <include refid="tableName"></include>
        f
        left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID
        left join bus_riskpoint r on r.IDENTIFICATION_ID = f.IDENTIFICATIONPARTS_ID
        left join bus_list_checkitem lck on lck.RISKPOINT_ID = r.RISKPOINT_ID
        where f.ISDELETE = '0'
        <if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
            and f.CORPINFO_ID = #{CORPINFO_ID}
        </if>
        <if test="LISTMANAGER_ID != null and LISTMANAGER_ID != ''"><!-- 关键词检索 -->
            and lck.LISTMANAGER_ID = #{LISTMANAGER_ID}
        </if>
        AND lck.ISDELETE = '0'
        GROUP BY f.IDENTIFICATIONPARTS_ID
        order by (select o.ORDERBY from BUS_CHECKROUTE o where o.LISTMANAGER_ID = lck.LISTMANAGER_ID and
        o.IDENTIFICATIONPARTS_ID = f.IDENTIFICATIONPARTS_ID)
    </select>

    <select id="getBiCount" parameterType="pd" resultType="pd">
        SELECT l.LEVELID,
               count(l.LEVELID) COUNT
        FROM
            (
            SELECT
            min ( r.LEVELID ) LEVELID
            FROM
            bus_identificationparts f
            LEFT JOIN bus_riskpoint r ON f.IDENTIFICATIONPARTS_ID = r.IDENTIFICATION_ID
            WHERE
            f.ISDELETE = '0'
            AND r.ISDELETE = '0'
            AND f.CORPINFO_ID = #{CORPINFO_ID}
            GROUP BY
            f.IDENTIFICATIONPARTS_ID
            ) l
        GROUP BY
            l.LEVELID
        ORDER BY
            l.LEVELID
    </select>

</mapper>
