<?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.yeejoin.equipmanage.mapper.EquipmentManageMapper">
    <select id="queryEquipmenInfo" resultType="com.yeejoin.equipmanage.common.vo.EquipmentManageVo">
        select
            id as id,
            name as name,
            code as code,
            form_group_id,
            instance_id,
            date_format(install_date,'%Y-%m-%d') AS installDate,
            date_format(first_maintenance_date,'%Y-%m-%d') AS firstMaintenanceDate,
            charge_person as chargePerson,
            (select name from wl_manufacturer_info where id = construction_unit) as constructionUnit,
            (select name from wl_manufacturer_info where id = maintenance_unit) as maintenanceUnit,
            (select name from wl_equipment_category where id = system_type) as systemType,
            system_type as systemTypeCode,
            construction_unit as constructionUnitCode,
            maintenance_unit as maintenanceUnitCode,
            maintenance_frequency as maintenanceFrequency,
            biz_org_code as bizOrgCode,
            biz_org_name as bizOrgName,
            scene_id as sceneId,
            (select count(1) from `wl_equipment_specific` es where find_in_set(sys.id,es.system_id) and es.single = true ) equipCount
            from
            f_fire_fighting_system as sys
            where 1=1
            <if test="equimentName != 'null'  ">
                AND NAME like  CONCAT('%',#{equimentName},'%')
            </if>
            <if test="equimentCode != 'null'  ">
                AND CODE like CONCAT('%',#{equimentCode},'%')
            </if>
            <if test="construction != 'all' ">
                AND CONSTRUCTION_UNIT = #{construction}
            </if>
            <if test="maintenance != 'all' ">
                AND MAINTENANCE_UNIT = #{maintenance}
            </if>
            <if test="bizOrgCode != null and bizOrgCode != ''">
                AND biz_org_code like CONCAT(#{bizOrgCode},'%')
            </if>
            <if test="formGroupId != null and formGroupId != '' and formGroupId != '-1'">
                AND form_group_id = #{formGroupId}
            </if>
            order by id DESC
            LIMIT #{spage},#{pageSize}
    </select>
    <select id="queryEquipmenCount" resultType="long">
        select
            count(1)
            from
             f_fire_fighting_system
            where 1=1
            <if test="equimentName != 'null'  ">
                AND name like  CONCAT('%',#{equimentName},'%')
            </if>
            <if test="equimentCode != 'null'  ">
                AND code like CONCAT('%',#{equimentCode},'%')
            </if>
            <if test="construction != 'all' ">
                AND construction_unit = #{construction}
            </if>
            <if test="maintenance != 'all' ">
                AND maintenance_unit = #{maintenance}
            </if>
            <if test="bizOrgCode != null and bizOrgCode != ''">
                AND biz_org_code like CONCAT(#{bizOrgCode},'%')
            </if>
            <if test="formGroupId != null and formGroupId != '' and formGroupId != '-1'">
                AND form_group_id = #{formGroupId}
            </if>
    </select>
    <select id="getUtils" resultType="com.yeejoin.equipmanage.common.vo.EquipmentManageVo">
         select
             id ,
             name as name
             from
             wl_manufacturer_info
             order by id
    </select>
    <select id="getSystemList" resultType="com.yeejoin.equipmanage.common.vo.EquipmentManageVo">
         SELECT
			id,
			`name`,
			`code`
		FROM
			wl_equipment_category
		WHERE
			parent_id = (
				SELECT
					CONCAT(id, '')
				FROM
					wl_equipment_category
				WHERE
					`code` = '92000000'
			)
		ORDER BY
			`code`
    </select>
    <delete id="deleteByid">
            DELETE  FROM f_fire_fighting_system WHERE id = #{param}
    </delete>
    <select id="checkCode" resultType="int">
         select
            count(1) from f_fire_fighting_system
            where CODE=#{param}
    </select>
    <insert id="insertEquipment">
         INSERT INTO
                f_fire_fighting_system
                (id,
                name,
                code,
                system_type,
                install_date,
                charge_person,
                construction_unit,
                maintenance_unit,
                maintenance_frequency,
                first_maintenance_date,
                rec_user_id,
                rec_date,
                sort)
                VALUES(
                #{id},
                #{name},
                #{code},
                #{systemType},
                STR_TO_DATE(#{installDate},'%Y-%m-%d'),
                #{chargePerson},
                #{constructionUnit},
                #{maintenanceUnit},
                #{maintenanceFrequency},
                STR_TO_DATE(#{firstMaintenanceDate},'%Y-%m-%d'),
                #{recUserId},
                now(),
                #{sort})
    </insert>
    <insert id="insertFile">
        insert into wl_upload_file
             (id,
             url,
             name,
             user_id,
             file_type,
             object_id,
             create_date)
             VALUES(
             #{id},
             #{url},
             #{name},
             #{userId},
             #{fileType},
             #{objectId},
             now())
    </insert>
    <select id="getFiles" resultType="com.yeejoin.equipmanage.common.vo.FileUploadVo">
         select
                 id,
                 url,
                 name,
                 file_type as fileType,
                 object_id as objectId,
                 date_format(create_date,'%Y-%m-%d') AS createDate
                 from
                 wl_upload_file
                 where
                 object_id =#{id}
                 and file_type=#{type}
    </select>
    <update id="updateEquipment">
         UPDATE
              f_fire_fighting_system
              SET
              NAME = #{name},
              system_type =#{systemType} ,
              install_date =STR_TO_DATE(#{installDate},'%Y-%m-%d'),
              charge_person =#{chargePerson},
              construction_unit =#{constructionUnit},
              maintenance_unit =#{maintenanceUnit},
              maintenance_frequency =#{maintenanceFrequency},
              first_maintenance_date =STR_TO_DATE(#{firstMaintenanceDate},'%Y-%m-%d'),
              rec_user_id =#{recUserId},
              rec_date = now()
              where id = #{id}
    </update>
    <update id="updateFile">
         UPDATE
          wl_upload_file
          SET
          url = #{url},
          create_date= now()
          where
           object_id =#{objectId}
           and file_type=#{fileType}
    </update>
    <delete id="deleteFilre">
        delete from wl_upload_file WHERE object_id = #{id}
    </delete>

    <select id="chekcName" resultType="int">
        select count(1)
        from f_fire_fighting_system
        where NAME=#{name}
        <if test="id != null ">
            and id !=#{id}
        </if>
    </select>

    <select id="getNotFireSystemList" resultType="hashmap">
        SELECT
            fs.id AS systemId,
	        fs.`name` AS systemName
        FROM
            f_fire_fighting_system fs
            JOIN wl_equipment_category ec ON ec.id = fs.system_type
            AND ec.`code` != #{alarmSystemCode}
    </select>
</mapper>
