<?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.huatek.busi.mapper.SysFileMapper">

    <sql id="Base_Column_List">
        id,
        business_id AS businessId,
        equ_type as equType,
        file_name AS fileName,
        suffix_file_path as suffixFilePath,
        file_path AS filePath,
        suffix_name AS suffixName,
        create_date AS createDate,
        file_size AS fileSize,
        file_type AS fileType,
        busi_file_type AS busiFileType,
        count,
        date,
        deleted
    </sql>

    <sql id="Base_Column_List2">
        DISTINCT A.id,
        A.business_id AS businessId,
        A.equ_type AS equType,
        A.file_name AS fileName,
        A.suffix_file_path AS suffixFilePath,
        A.file_path AS filePath,
        A.suffix_name AS suffixName,
        A.create_date AS createDate,
        A.file_size AS fileSize,
        A.file_type AS fileType,
        A.busi_file_type AS busiFileType,
        A.count,
        A.date,
        A.deleted,
        E.user_name AS userName,
        F.test_date AS testDate,
        C.`name` as equName
    </sql>

    <select id="findByBusId" parameterType="java.lang.String" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
            <include refid="Base_Column_List"/>
        FROM
        sys_file
        WHERE
        business_id = #{busId}
    </select>

    <select id="findById" parameterType="java.lang.String" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        sys_file
        WHERE
        id = #{id}
    </select>

    <select id="findModelById" parameterType="java.lang.String" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
            id,
            equ_type as equType,
            file_name AS fileName,
            suffix_file_path as suffixFilePath,
            file_path AS filePath,
            suffix_name AS suffixName,
            create_user AS userName,
            create_date AS createDate,
            file_size AS fileSize,
            file_type AS fileType,
            busi_file_type AS busiFileType,
            date,
            deleted
        FROM
        x_r_model
        WHERE
        id = #{id}
    </select>

    <select id="modelList" parameterType="com.huatek.busi.where.SysFileSelectVo" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
            A.id,
            A.equ_type AS equType,
            A.file_name AS fileName,
            A.suffix_file_path AS suffixFilePath,
            A.file_path AS filePath,
            A.suffix_name AS suffixName,
            B.user_name AS userName,
            A.create_date AS createDate,
            A.file_size AS fileSize,
            A.file_type AS fileType,
            A.busi_file_type AS busiFileType,
            A.date,
            A.deleted
        FROM
            x_r_model A
        LEFT JOIN sys_user B ON A.create_user = B.id
        WHERE
            A.equ_type = #{equType}
            AND A.file_type = #{fileType}
            AND A.deleted = #{deleted}
        ORDER BY
            A.create_date DESC
    </select>

    <select id="findModelByDeleted" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
            id,
            equ_type as equType,
            file_name AS fileName,
            suffix_file_path as suffixFilePath,
            file_path AS filePath,
            suffix_name AS suffixName,
            create_user AS userName,
            create_date AS createDate,
            file_size AS fileSize,
            file_type AS fileType,
            busi_file_type AS busiFileType,
            date,
            deleted
        FROM
        x_r_model
        WHERE
        deleted = 1
        AND equ_type = #{equType}
        AND id in
        <foreach collection="ids" index="index" item="id" separator="," open="(" close=")">
            #{id, jdbcType=VARCHAR}
        </foreach>
    </select>

    <delete id="delete">
        DELETE
        FROM
            sys_file
        WHERE
            business_id = #{busId}
    </delete>

    <select id="findSysFileByBusinessId" parameterType="java.lang.String" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
            <include refid="Base_Column_List"/>
        FROM
            sys_file
        WHERE
            business_id = #{businessId}
    </select>

    <select id="list1" parameterType="com.huatek.busi.where.SysFileSelectVo" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
            <include refid="Base_Column_List2"/>
        FROM
            sys_file A
        LEFT JOIN x_r_master_data B ON A.business_id = B.ref_id
        LEFT JOIN busi_site_tree C ON B.equ_little_id = C.id
        LEFT JOIN busi_site D ON B.site_id = D.id
        LEFT JOIN sys_user E ON E.id = B.create_user
	    LEFT JOIN x_lab_report_basis_data F ON A.business_id = F.ref_id
        WHERE
            A.deleted = #{deleted}
        AND B.deleted = #{deleted}
        AND C.`status` = 1
        AND D.`status` = 1
        AND A.equ_type = #{equType}
        AND A.file_type = #{fileType}
        GROUP BY
	        A.create_date
        ORDER BY
	        A.create_date DESC
    </select>

    <select id="listBlq" parameterType="com.huatek.busi.where.SysFileSelectVo" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
        <include refid="Base_Column_List2"/>
        FROM
        sys_file A
        LEFT JOIN x_r_arrester_data B ON A.business_id = B.ref_id
        LEFT JOIN busi_site_tree C ON B.equ_little_id = C.id
        LEFT JOIN busi_site D ON B.site_id = D.id
        LEFT JOIN sys_user E ON E.id = B.create_user
        LEFT JOIN x_lab_arrester_report_basis_data F ON A.business_id = F.ref_id
        WHERE
        A.deleted = #{deleted}
        AND B.deleted = #{deleted}
        AND C.`status` = 1
        AND D.`status` = 1
        AND A.equ_type = #{equType}
        AND A.file_type = #{fileType}
        GROUP BY
        A.create_date
        ORDER BY
        A.create_date DESC
    </select>

    <select id="listKgg" parameterType="com.huatek.busi.where.SysFileSelectVo" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
        <include refid="Base_Column_List2"/>
        FROM
        sys_file A
        LEFT JOIN x_r_switchgear_data B ON A.business_id = B.ref_id
        LEFT JOIN busi_site_tree C ON B.equ_little_id = C.id
        LEFT JOIN busi_site D ON B.site_id = D.id
        LEFT JOIN sys_user E ON E.id = B.create_user
        LEFT JOIN x_lab_switchgear_report_basis_data F ON A.business_id = F.ref_id
        WHERE
        A.deleted = #{deleted}
        AND B.deleted = #{deleted}
        AND C.`status` = 1
        AND D.`status` = 1
        AND A.equ_type = #{equType}
        AND A.file_type = #{fileType}
        GROUP BY
        A.create_date
        ORDER BY
        A.create_date DESC
    </select>

    <select id="listDl" parameterType="com.huatek.busi.where.SysFileSelectVo" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
        <include refid="Base_Column_List2"/>
        FROM
        sys_file A
        LEFT JOIN x_r_cable_data B ON A.business_id = B.ref_id
        LEFT JOIN busi_site_tree C ON B.equ_little_id = C.id
        LEFT JOIN busi_site D ON B.site_id = D.id
        LEFT JOIN sys_user E ON E.id = B.create_user
        LEFT JOIN x_lab_cable_report_basis_data F ON A.business_id = F.ref_id
        WHERE
        A.deleted = #{deleted}
        AND B.deleted = #{deleted}
        AND C.`status` = 1
        AND D.`status` = 1
        AND A.equ_type = #{equType}
        AND A.file_type = #{fileType}
        GROUP BY
        A.create_date
        ORDER BY
        A.create_date DESC
    </select>

    <select id="listGen" parameterType="com.huatek.busi.where.SysFileSelectVo" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
        <include refid="Base_Column_List2"/>
        FROM
        sys_file A
        LEFT JOIN x_r_generator_data B ON A.business_id = B.ref_id
        LEFT JOIN busi_site_tree C ON B.equ_little_id = C.id
        LEFT JOIN busi_site D ON B.site_id = D.id
        LEFT JOIN sys_user E ON E.id = B.create_user
        LEFT JOIN x_lab_generator_report_basis_data F ON A.business_id = F.ref_id
        WHERE
        A.deleted = #{deleted}
        AND B.deleted = #{deleted}
        AND C.`status` = 1
        AND D.`status` = 1
        AND A.equ_type = #{equType}
        AND A.file_type = #{fileType}
        GROUP BY
        A.create_date
        ORDER BY
        A.create_date DESC
    </select>

    <update id="update">
        UPDATE sys_file
        SET deleted = 1
        WHERE
            equ_type = #{equType}
        AND business_id = #{businessId}
    </update>

    <select id="findByDeleted" parameterType="java.lang.String" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
            <include refid="Base_Column_List"/>
        FROM
            sys_file
        WHERE
            deleted = 1
        AND equ_type = #{equType}
        AND business_id = #{businessId}
    </select>

    <select id="findListAnnex" parameterType="com.huatek.busi.where.SysFileSelectVo" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        sys_file
        WHERE
        deleted = 0
        AND equ_type = #{equType}
        AND business_id = #{businessId}
    </select>

    <update id="updateByRefIdsAndEquType">
        UPDATE sys_file
        SET deleted = 1
        WHERE
            equ_type = #{equType}
        AND business_id in
        <foreach collection="refIds" index="index" item="refId" separator="," open="(" close=")">
            #{refId, jdbcType=VARCHAR}
        </foreach>
    </update>

    <update id="updateByRefIdsAndEquTypes" parameterType="java.lang.String">
        UPDATE sys_file
        SET deleted = 1
        WHERE
            equ_type = #{eqType}
        AND business_id = #{refId}
    </update>

    <select id="findAllByDeleted" resultType="com.huatek.busi.dto.SysFileDto">
        SELECT
            <include refid="Base_Column_List"/>
        FROM
            sys_file
        WHERE
            deleted = 1
        AND equ_type = #{equType}
        AND business_id in
        <foreach collection="refIds" index="index" item="refId" separator="," open="(" close=")">
            #{refId, jdbcType=VARCHAR}
        </foreach>
    </select>

    <select id="findMaxCount" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT
            MAX(count) AS count
        FROM
            sys_file
        WHERE
            deleted = 0
        AND equ_type = #{eqType}
        AND date = #{date}
        AND busi_file_type = #{fileType}
    </select>

    <update id="updateFilePathById" parameterType="java.lang.String">
        UPDATE sys_file
        SET file_path = #{filePath}
        WHERE
            id = #{id}
    </update>

    <update id="updateModelPathById" parameterType="java.lang.String">
        UPDATE x_r_model
        SET file_path = #{filePath}
        WHERE
            id = #{id}
    </update>

    <update id="deleteModels" parameterType="java.lang.String">
        UPDATE x_r_model
        SET deleted = 1
        WHERE
            equ_type = #{equType}
        AND id in
        <foreach collection="ids" index="index" item="id" separator="," open="(" close=")">
            #{id, jdbcType=VARCHAR}
        </foreach>
    </update>
</mapper>
