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

    <resultMap type="com.yeejoin.equipmanage.common.entity.MaintenanceResource" id="MaintenanceResourceResult">
        <result property="id" column="id"/>
        <result property="code" column="code"/>
        <result property="name" column="name"/>
        <result property="type" column="type"/>
        <result property="parentId" column="parent_id"/>
        <result property="createDate" column="create_date"/>
    </resultMap>

    <sql id="selectMaintenanceResourceVo">
        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, parent_id, create_date from mt_maintenance_resource
    </sql>

    <select id="selectMaintenanceResourceList" parameterType="com.yeejoin.equipmanage.common.entity.MaintenanceResource"
            resultMap="MaintenanceResourceResult">
        <include refid="selectMaintenanceResourceVo"/>
        <where>
        </where>
    </select>

    <select id="selectMaintenanceResourceById" parameterType="Long" resultMap="MaintenanceResourceResult">
        <include refid="selectMaintenanceResourceVo"/>
        where id = #{id}
    </select>

    <insert id="insertMaintenanceResource" parameterType="com.yeejoin.equipmanage.common.entity.MaintenanceResource">
        insert into mt_maintenance_resource
        <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">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="parentId != null">parent_id,</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">#{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="parentId != null">#{parentId},</if>
            <if test="createDate != null">#{createDate},</if>
        </trim>
    </insert>

    <update id="updateMaintenanceResource" parameterType="com.yeejoin.equipmanage.common.entity.MaintenanceResource">
        update mt_maintenance_resource
        <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">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="parentId != null">parent_id = #{parentId},</if>
            <if test="createDate != null">create_date = #{createDate},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMaintenanceResourceById" parameterType="Long">
        delete from mt_maintenance_resource where id = #{id}
    </delete>

    <delete id="deleteMaintenanceResourceByIds" parameterType="String">
        delete from mt_maintenance_resource where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="findTierKeyByParentId" resultType="java.util.Map">
        SELECT
        CONCAT( m.id, '-', m.parent_id ) AS `key`,
        m.id AS `value`
        FROM
        `mt_maintenance_resource` m
        <where>
            <if test="parentId != null">
                m.parent_id <![CDATA[ <> ]]> #{parentId}
            </if>
        </where>
        GROUP BY
        m.id,
        m.parent_id
    </select>
    <select id="selectAll" resultType="com.yeejoin.equipmanage.common.dto.MaintenanceResourceDto">
        <![CDATA[
        SELECT
            m.*
        FROM
        `mt_maintenance_resource` m
        ]]>
    </select>

    <select id="selectAllType" resultType="com.yeejoin.equipmanage.common.dto.MaintenanceResourceDto">
        <![CDATA[
        SELECT
            maintenance_company_id as companyId,
            fire_fight_sys_id AS id,
            fire_fight_sys_name name ,
            owner_unit_id as parentId,
            owner_unit_id as ownerUnitId,
            IFNULL(classify_type,3) as type
        FROM
            `mt_maintenance_resource_data`
        GROUP BY
            owner_unit_id,
            maintenance_company_id,
            fire_fight_sys_id
        ]]>
    </select>
    <select id="findTreeById" resultType="com.yeejoin.equipmanage.common.dto.MaintenanceResourceDto">
        <![CDATA[
        SELECT
            m.*
        FROM
        `mt_maintenance_resource` m
        ]]>
        <where>
            <if test="id != null">
                FIND_IN_SET(m.parent_id, getMaintenanceResourceTreeById(#{id}))
            </if>
        </where>
        UNION (SELECT f.* from mt_maintenance_resource f where f.id =#{id})
    </select>
    <select id="findByIds" resultType="com.yeejoin.equipmanage.common.vo.MaintenanceResourceDataVo">
        <![CDATA[
        SELECT
            m.fire_facility_name,
            m.location,
            m.classify_name,
            m.fire_fight_sys_name,
            m.maintenance_expiration_time,
            m.fire_facility_code,
            m.owner_unit_name,
            m.maintenance_company_name,
            m.maintenance_cycle,
            m.create_date
        FROM
            mt_maintenance_resource_data m
        ]]>
        <where>
            <if test="list != null and list.size() >0">
                AND m.id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="getFireFightSysPage" resultType="com.yeejoin.equipmanage.common.vo.MaintenanceResourceVo">
        <![CDATA[
        SELECT
            m.*
        FROM
        `mt_maintenance_resource` m
        ]]>
        <where>
            <if test="maintenanceCompanyId != null">
                FIND_IN_SET(m.id, getMaintenanceResourceTreeById(#{maintenanceCompanyId}))
            </if>
            <if test="ownerUnitId != null">
                FIND_IN_SET(m.id, getMaintenanceResourceTreeById(#{ownerUnitId}))
            </if>
            <if test="fireFightSysId != null">
                FIND_IN_SET(m.id, getMaintenanceResourceTreeById(#{fireFightSysId}))
            </if>
            <if test="type != null">
                AND m.type = #{type}
            </if>
        </where>
        GROUP BY
            m.id
    </select>
    <select id="selectOwnerUnitList" resultType="com.yeejoin.equipmanage.common.dto.MaintenanceResourceDto">
        <![CDATA[
        SELECT
            m.*
        FROM
        `mt_maintenance_resource` m
        ]]>
        <where>
            <if test="type != null">
                m.type = #{type}
                <if test="id != null">
                    <choose>
                        <when test="type == 1">
                            AND FIND_IN_SET(m.id, getMaintenanceResourceTreeById(#{id}))
                        </when>
                        <otherwise>
                            AND m.id = #{id}
                        </otherwise>
                    </choose>
                </if>
            </if>
        </where>
        GROUP BY
            m.id
    </select>
    <select id="findByTypeIn" resultType="com.yeejoin.equipmanage.common.dto.MaintenanceResourceDto">
        <![CDATA[
        SELECT
            m.*
        FROM
        `mt_maintenance_resource` m
        ]]>
        <where>
            <if test="ids != null and ids.length >0">
                m.type IN
                <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <update id="updateBatch" parameterType="list">
        <if test="list != null and list.size() >0">
            REPLACE INTO  mt_maintenance_resource ( id, `name`, `code`, type, parent_id, create_date ) VALUES
            <foreach collection="list" item="item" index="index" open="" close=";" separator=",">
                (#{item.id},#{item.name},#{item.code},#{item.type},#{item.parentId}, NOW())
            </foreach>
        </if>
    </update>
</mapper>