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

    <resultMap type="com.yeejoin.equipmanage.common.entity.MaintenanceResourceData" id="MaintenanceResourceDataResult">
        <result property="id" column="id"/>
        <result property="maintenanceCompanyId" column="maintenance_company_id"/>
        <result property="maintenanceCompanyCode" column="maintenance_company_code"/>
        <result property="maintenanceCompanyName" column="maintenance_company_name"/>
        <result property="ownerUnitId" column="owner_unit_id"/>
        <result property="ownerUnitCode" column="owner_unit_code"/>
        <result property="ownerUnitName" column="owner_unit_name"/>
        <result property="fireFightSysId" column="fire_fight_sys_id"/>
        <result property="fireFightSysCode" column="fire_fight_sys_code"/>
        <result property="fireFightSysName" column="fire_fight_sys_name"/>
        <result property="classifyId" column="classify_id"/>
        <result property="classifyCode" column="classify_code"/>
        <result property="classifyName" column="classify_name"/>
        <result property="classifyType" column="classify_type"/>
        <result property="fireFacilityId" column="fire_facility_id"/>
        <result property="fireFacilityCode" column="fire_facility_code"/>
        <result property="fireFacilityName" column="fire_facility_name"/>
        <result property="fireFacilityType" column="fire_facility_type"/>
        <result property="maintenanceExpirationTime" column="maintenance_expiration_time"/>
        <result property="location" column="location"/>
        <result property="createDate" column="create_date"/>
    </resultMap>

    <sql id="selectMaintenanceResourceDataVo">
        select id, maintenance_company_id, maintenance_company_code, maintenance_company_name, owner_unit_id, owner_unit_code, owner_unit_name, fire_fight_sys_id, fire_fight_sys_code, fire_fight_sys_name, classify_id, classify_code, classify_name, classify_type, fire_facility_id, fire_facility_code, fire_facility_name, fire_facility_type, maintenance_expiration_time, maintenance_time, maintenance_cycle, create_date from mt_maintenance_resource_data
    </sql>

    <select id="selectMaintenanceResourceDataListByclassifyId" parameterType="com.yeejoin.equipmanage.common.entity.MaintenanceResourceData"
            resultMap="MaintenanceResourceDataResult">
        SELECT
            id,
            maintenance_company_id,
            maintenance_company_code,
            maintenance_company_name,
            owner_unit_id,
            owner_unit_code,
            owner_unit_name,
            fire_fight_sys_id,
            fire_fight_sys_code,
            fire_fight_sys_name,
            classify_id,
            classify_code,
            classify_name,
            classify_type,
            fire_facility_id,
            fire_facility_code,
            fire_facility_name,
            fire_facility_type,
            maintenance_expiration_time,
            maintenance_time,
            maintenance_cycle,
            create_date
        FROM
            mt_maintenance_resource_data
        GROUP BY maintenance_company_id,classify_id,owner_unit_id,fire_fight_sys_id
    </select>

    <select id="selectMaintenanceResourceDataList"
            parameterType="com.yeejoin.equipmanage.common.entity.MaintenanceResourceData"
            resultMap="MaintenanceResourceDataResult">
        <include refid="selectMaintenanceResourceDataVo"/>
        <where>
            <if test="maintenanceCompanyId != null ">and maintenance_company_id = #{maintenanceCompanyId}</if>
            <if test="maintenanceCompanyCode != null  and maintenanceCompanyCode != ''">and maintenance_company_code =
                #{maintenanceCompanyCode}
            </if>
            <if test="maintenanceCompanyName != null  and maintenanceCompanyName != ''">and maintenance_company_name
                like concat('%', #{maintenanceCompanyName}, '%')
            </if>
            <if test="ownerUnitId != null  and ownerUnitId != ''">and owner_unit_id = #{ownerUnitId}</if>
            <if test="ownerUnitCode != null  and ownerUnitCode != ''">and owner_unit_code = #{ownerUnitCode}</if>
            <if test="ownerUnitName != null  and ownerUnitName != ''">and owner_unit_name like concat('%',
                #{ownerUnitName}, '%')
            </if>
            <if test="fireFightSysId != null ">and fire_fight_sys_id = #{fireFightSysId}</if>
            <if test="fireFightSysCode != null  and fireFightSysCode != ''">and fire_fight_sys_code =
                #{fireFightSysCode}
            </if>
            <if test="fireFightSysName != null  and fireFightSysName != ''">and fire_fight_sys_name like concat('%',
                #{fireFightSysName}, '%')
            </if>
            <if test="classifyId != null ">and classify_id = #{classifyId}</if>
            <if test="classifyCode != null  and classifyCode != ''">and classify_code = #{classifyCode}</if>
            <if test="classifyName != null  and classifyName != ''">and classify_name like concat('%', #{classifyName},
                '%')
            </if>
            <if test="classifyType != null  and classifyType != ''">and classify_type = #{classifyType}</if>
            <if test="fireFacilityId != null ">and fire_facility_id = #{fireFacilityId}</if>
            <if test="fireFacilityCode != null  and fireFacilityCode != ''">and fire_facility_code =
                #{fireFacilityCode}
            </if>
            <if test="fireFacilityName != null  and fireFacilityName != ''">and fire_facility_name like concat('%',
                #{fireFacilityName}, '%')
            </if>
            <if test="fireFacilityType != null  and fireFacilityType != ''">and fire_facility_type =
                #{fireFacilityType}
            </if>
            <if test="maintenanceExpirationTime != null ">and maintenance_expiration_time =
                #{maintenanceExpirationTime}
            </if>
            <if test="location != null  and location != ''">and location = #{location}</if>
            <if test="createDate != null ">and create_date = #{createDate}</if>
        </where>
    </select>

    <select id="selectMaintenanceResourceDataById" parameterType="Long" resultMap="MaintenanceResourceDataResult">
        <include refid="selectMaintenanceResourceDataVo"/>
        where id = #{id}
    </select>

    <insert id="insertMaintenanceResourceData"
            parameterType="com.yeejoin.equipmanage.common.entity.MaintenanceResourceData">
        insert into mt_maintenance_resource_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="maintenanceCompanyId != null">maintenance_company_id,</if>
            <if test="maintenanceCompanyCode != null">maintenance_company_code,</if>
            <if test="maintenanceCompanyName != null">maintenance_company_name,</if>
            <if test="ownerUnitId != null and ownerUnitId != ''">owner_unit_id,</if>
            <if test="ownerUnitCode != null">owner_unit_code,</if>
            <if test="ownerUnitName != null">owner_unit_name,</if>
            <if test="fireFightSysId != null">fire_fight_sys_id,</if>
            <if test="fireFightSysCode != null">fire_fight_sys_code,</if>
            <if test="fireFightSysName != null">fire_fight_sys_name,</if>
            <if test="classifyId != null">classify_id,</if>
            <if test="classifyCode != null">classify_code,</if>
            <if test="classifyName != null">classify_name,</if>
            <if test="classifyType != null">classify_type,</if>
            <if test="fireFacilityId != null">fire_facility_id,</if>
            <if test="fireFacilityCode != null">fire_facility_code,</if>
            <if test="fireFacilityName != null">fire_facility_name,</if>
            <if test="fireFacilityType != null">fire_facility_type,</if>
            <if test="maintenanceExpirationTime != null">maintenance_expiration_time,</if>
            <if test="maintenanceTime != null">maintenance_time = #{maintenanceTime},</if>
            <if test="maintenanceCycle != null">maintenance_cycle = #{maintenanceCycle},</if>
            <if test="location != null">location,</if>
            <if test="createDate != null">create_date,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="maintenanceCompanyId != null">#{maintenanceCompanyId},</if>
            <if test="maintenanceCompanyCode != null">#{maintenanceCompanyCode},</if>
            <if test="maintenanceCompanyName != null">#{maintenanceCompanyName},</if>
            <if test="ownerUnitId != null and ownerUnitId != ''">#{ownerUnitId},</if>
            <if test="ownerUnitCode != null">#{ownerUnitCode},</if>
            <if test="ownerUnitName != null">#{ownerUnitName},</if>
            <if test="fireFightSysId != null">#{fireFightSysId},</if>
            <if test="fireFightSysCode != null">#{fireFightSysCode},</if>
            <if test="fireFightSysName != null">#{fireFightSysName},</if>
            <if test="classifyId != null">#{classifyId},</if>
            <if test="classifyCode != null">#{classifyCode},</if>
            <if test="classifyName != null">#{classifyName},</if>
            <if test="classifyType != null">#{classifyType},</if>
            <if test="fireFacilityId != null">#{fireFacilityId},</if>
            <if test="fireFacilityCode != null">#{fireFacilityCode},</if>
            <if test="fireFacilityName != null">#{fireFacilityName},</if>
            <if test="fireFacilityType != null">#{fireFacilityType},</if>
            <if test="maintenanceExpirationTime != null">#{maintenanceExpirationTime},</if>
            <if test="maintenanceTime != null">#{maintenanceTime},</if>
            <if test="maintenanceCycle != null">#{maintenanceCycle},</if>
            <if test="location != null">#{location},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createDate != null">#{createDate},</if>
        </trim>
    </insert>

    <update id="updateMaintenanceResourceData"
            parameterType="com.yeejoin.equipmanage.common.entity.MaintenanceResourceData">
        update mt_maintenance_resource_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="maintenanceCompanyId != null">maintenance_company_id = #{maintenanceCompanyId},</if>
            <if test="maintenanceCompanyCode != null">maintenance_company_code = #{maintenanceCompanyCode},</if>
            <if test="maintenanceCompanyName != null">maintenance_company_name = #{maintenanceCompanyName},</if>
            <if test="ownerUnitId != null and ownerUnitId != ''">owner_unit_id = #{ownerUnitId},</if>
            <if test="ownerUnitCode != null">owner_unit_code = #{ownerUnitCode},</if>
            <if test="ownerUnitName != null">owner_unit_name = #{ownerUnitName},</if>
            <if test="fireFightSysId != null">fire_fight_sys_id = #{fireFightSysId},</if>
            <if test="fireFightSysCode != null">fire_fight_sys_code = #{fireFightSysCode},</if>
            <if test="fireFightSysName != null">fire_fight_sys_name = #{fireFightSysName},</if>
            <if test="classifyId != null">classify_id = #{classifyId},</if>
            <if test="classifyCode != null">classify_code = #{classifyCode},</if>
            <if test="classifyName != null">classify_name = #{classifyName},</if>
            <if test="classifyType != null">classify_type = #{classifyType},</if>
            <if test="fireFacilityId != null">fire_facility_id = #{fireFacilityId},</if>
            <if test="fireFacilityCode != null">fire_facility_code = #{fireFacilityCode},</if>
            <if test="fireFacilityName != null">fire_facility_name = #{fireFacilityName},</if>
            <if test="fireFacilityType != null">fire_facility_type = #{fireFacilityType},</if>
            <if test="maintenanceExpirationTime != null">maintenance_expiration_time = #{maintenanceExpirationTime},
            </if>
            <if test="maintenanceTime != null">maintenance_time = #{maintenanceTime},</if>
            <if test="maintenanceCycle != null">maintenance_cycle = #{maintenanceCycle},</if>
            <if test="location != null">location = #{location},</if>
            <if test="createDate != null">create_date = #{createDate},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMaintenanceResourceDataById" parameterType="Long">
        delete from mt_maintenance_resource_data where id = #{id}
    </delete>

    <delete id="deleteMaintenanceResourceDataByIds" parameterType="String">
        delete from mt_maintenance_resource_data where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="getMaintenanceResourceDataList"
            resultType="com.yeejoin.equipmanage.common.entity.MaintenanceResourceData">

    </select>
    <select id="getRelationKey" resultType="java.lang.String">
        SELECT
        CONCAT(m.fire_facility_id,'-',m.fire_facility_type)
        FROM
        mt_maintenance_resource_data m
        <where>
            <choose>
                <when test="list != null and list.size() >0">
                    CONCAT(m.fire_facility_id,'-',m.fire_facility_type) IN
                    <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    m.id = -1
                </otherwise>
            </choose>
        </where>
    </select>
    <select id="getBandWbCompany" resultType="java.lang.String">
        SELECT
       m.fire_facility_id
        FROM
        mt_maintenance_resource_data m
        <where>
          <if test="maintenanceCompanyId != null and maintenanceCompanyId != '' ">
              m.maintenance_company_id != #{maintenanceCompanyId}
          </if>
        </where>
    </select>
    <select id="getClassifyPage" resultType="com.yeejoin.equipmanage.common.dto.MaintenanceResourceDataDto">
        SELECT
        m.maintenance_company_id,
        m.maintenance_company_name,
        m.owner_unit_id,
        m.owner_unit_name,

        <choose>
            <when  test="fireFightSysId != null and fireFightSysId != '' ">
                m.fire_fight_sys_id,
            </when>
            <otherwise>
                '' as fire_fight_sys_id,
            </otherwise>
        </choose>


        m.fire_fight_sys_name,
        m.classify_id,
        m.classify_name,
        m.classify_type,
        m.classify_code,
        COUNT( 1 ) AS amount,
        SUM(IF(m.maintenance_expiration_time &lt;= CURRENT_DATE,1,0)) AS expirationAmount,
        we.img,
        wes.code
        FROM
        `mt_maintenance_resource_data` m
        LEFT JOIN  wl_equipment_specific wes on m.fire_facility_id = wes.id
        LEFT JOIN  wl_equipment_detail wed on wes.equipment_detail_id = wed.id
        LEFT JOIN  wl_equipment we on we.id = wed.equipment_id
        <where>
            <if test="maintenanceCompanyId != null">
                m.maintenance_company_id = #{maintenanceCompanyId}
            </if>
            <if test="ownerUnitId != null">
                m.owner_unit_id = #{ownerUnitId}
            </if>
            <if test="fireFightSysId != null">
                m.fire_fight_sys_id = #{fireFightSysId}
            </if>
        </where>
        GROUP BY
--             m.classify_code
        m.classify_id
    </select>
    <select id="getFireFacilityPage" resultType="com.yeejoin.equipmanage.common.dto.MaintenanceResourceDataDto">
        SELECT
        m.maintenance_company_id,
        m.maintenance_company_name,
        m.owner_unit_id,
        m.owner_unit_name,
        m.fire_fight_sys_id,
        m.fire_fight_sys_name,
        m.classify_id,
        m.classify_name,
        m.classify_type,
        m.classify_code,
        m.fire_facility_id,
        m.fire_facility_code,
        m.fire_facility_name,
        m.fire_facility_type,
        m.maintenance_time,
        m.maintenance_cycle,
        m.`status`,
        we.img,
        wes.code,
        IF(m.maintenance_expiration_time IS NOT NULL , IF(m.maintenance_expiration_time &lt;= CURRENT_DATE,'已到期','正常'), '') AS `statusText`,
        m.location
        FROM
        `mt_maintenance_resource_data` m
        LEFT JOIN  wl_equipment_specific wes on m.fire_facility_id = wes.id
        LEFT JOIN  wl_equipment_detail wed on wes.equipment_detail_id = wed.id
        LEFT JOIN  wl_equipment we on we.id = wed.equipment_id
        <where>
            <if test="maintenanceCompanyId != null">
                m.maintenance_company_id = #{maintenanceCompanyId}
            </if>
            <if test="ownerUnitId != null">
                AND m.owner_unit_id = #{ownerUnitId}
            </if>
            <if test="fireFightSysId != null and fireFightSysId != '' ">
                AND m.fire_fight_sys_id = #{fireFightSysId}
            </if>
            <if test="classifyId != null">
                AND m.classify_id = #{classifyId}
            </if>
            <if test="buildingId != null and buildingId.size() > 0">
                AND m.building_id IN
                <foreach collection="buildingId" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="status != null and status != ''">
                AND m.`status` = #{status}
            </if>
        </where>
    </select>
    <select id="getFireFacilityList" resultType="com.yeejoin.equipmanage.common.entity.MaintenanceResourceData">
        SELECT
            m.maintenance_company_id,
            m.maintenance_company_name,
            m.owner_unit_id,
            m.owner_unit_name,
            m.fire_fight_sys_id,
            m.fire_fight_sys_name,
            m.classify_id,
            m.classify_name,
            m.classify_type,
            m.classify_code,
            m.fire_facility_id,
            m.fire_facility_code,
            m.fire_facility_name,
            m.fire_facility_type,
            m.`status`
        FROM
            `mt_maintenance_resource_data` m
        <where>
            m.maintenance_expiration_time &lt;= CURRENT_DATE
            <if test="type != null">
                <if test="id != null and type == 1">
                    AND m.maintenance_company_id = #{id}
                </if>
                <if test="id != null and type == 2">
                    AND m.owner_unit_id = #{id}
                </if>
                <if test="id != null and type == 3">
                    AND m.fire_fight_sys_id = #{id}
                </if>
                <if test="id != null and type == 4">
                    AND m.fire_facility_id = #{id}
                </if>
            </if>
        </where>
    </select>
    <select id="deleteBeforeMainResCount" resultType="com.yeejoin.equipmanage.common.dto.MaintenanceResourceDataDto">
        SELECT
            m.maintenance_company_id,
            m.owner_unit_id,
            m.fire_fight_sys_id,
            m.classify_id,
            COUNT( m.fire_facility_id ) AS amount
        FROM
            `mt_maintenance_resource_data` m
        <where>
            <if test="list != null and list.length >0">
                m.id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY
            m.maintenance_company_id,
            m.owner_unit_id,
            m.fire_fight_sys_id,
            m.classify_id;
    </select>
    <delete id="deleteClassifyList" parameterType="java.util.List">
        delete from mt_maintenance_resource where
        <foreach collection="list"  item="item" separator=" or " index="index">
            (id= #{item.classifyId} and parent_id= #{item.fireFightSysId})
        </foreach>
    </delete>
    <delete id="deleteFireFightSysList" parameterType="java.util.List">
        delete from mt_maintenance_resource where
        <foreach collection="list"  item="item" separator=" or " index="index">
            (id= #{item.fireFightSysId} and parent_id= #{item.ownerUnitId})
        </foreach>
    </delete>
</mapper>