<?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.ygtx.gxt.mapper.GxtEquipmentMapper">
    
    <resultMap type="GxtEquipment" id="GxtEquipmentResult">
        <result property="equipmentId"    column="equipment_id"    />
        <result property="equipmentCode"    column="equipment_code"    />
        <result property="maintenanceCenter"    column="maintenance_center"    />
        <result property="station"    column="station"    />
        <result property="brand"    column="brand"    />
        <result property="model"    column="model"    />
        <result property="equipmentType"    column="equipment_type"    />
        <result property="status"    column="status"    />
        <result property="responsiblePersonName"    column="responsible_person_name"    />
        <result property="responsiblePersonId"    column="responsible_person_id"    />
        <result property="maintenanceCenterId"    column="maintenance_center_id"    />
        <result property="stationId"    column="station_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectGxtEquipmentVo">
        select equipment_id, equipment_code, maintenance_center, station, brand, model, equipment_type, status, responsible_person_name, responsible_person_id, maintenance_center_id, station_id, create_by, create_time, update_by, update_time from gxt_equipment t
    </sql>

    <select id="selectGxtEquipmentList" parameterType="GxtEquipment" resultMap="GxtEquipmentResult">
        <include refid="selectGxtEquipmentVo"/>
        <where>  
            <if test="equipmentCode != null  and equipmentCode != ''"> and equipment_code like concat('%', #{equipmentCode}, '%')</if>
            <if test="maintenanceCenter != null  and maintenanceCenter != ''"> and maintenance_center like concat('%', #{maintenanceCenter}, '%')</if>
            <if test="station != null  and station != ''"> and station like concat('%', #{station}, '%')</if>
            <if test="brand != null  and brand != ''"> and brand = #{brand}</if>
            <if test="model != null  and model != ''"> and model = #{model}</if>
            <if test="equipmentType != null  and equipmentType != ''"> and equipment_type = #{equipmentType}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            
            <!-- 业务特定数据权限过滤条件 -->
            <if test="params.businessDataScope != null and params.businessDataScope != ''">
                ${params.businessDataScope}
            </if>
        </where>
    </select>
    
    <select id="selectGxtEquipmentByEquipmentId" parameterType="Long" resultMap="GxtEquipmentResult">
        <include refid="selectGxtEquipmentVo"/>
        where equipment_id = #{equipmentId}
    </select>
    
    <select id="selectGxtEquipmentByEquipmentCode" parameterType="String" resultMap="GxtEquipmentResult">
        <include refid="selectGxtEquipmentVo"/>
        where equipment_code = #{equipmentCode}
    </select>
    
    <select id="selectGxtEquipmentByCodeAndLocation" parameterType="GxtEquipment" resultMap="GxtEquipmentResult">
        <include refid="selectGxtEquipmentVo"/>
        where equipment_code = #{equipmentCode} 
          and maintenance_center = #{maintenanceCenter} 
          and station = #{station}
    </select>
    
    <select id="selectBrandList" resultType="String">
        select distinct brand from gxt_equipment where brand is not null and brand != '' order by brand
    </select>
    
    <select id="selectModelListByBrand" parameterType="String" resultType="String">
        select distinct model from gxt_equipment where brand = #{brand} and model is not null and model != '' order by model
    </select>

    <insert id="insertGxtEquipment" parameterType="GxtEquipment" useGeneratedKeys="true" keyProperty="equipmentId">
        insert into gxt_equipment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="equipmentCode != null and equipmentCode != ''">equipment_code,</if>
            <if test="maintenanceCenter != null">maintenance_center,</if>
            <if test="station != null">station,</if>
            <if test="brand != null">brand,</if>
            <if test="model != null">model,</if>
            <if test="equipmentType != null">equipment_type,</if>
            <if test="status != null">status,</if>
            <if test="responsiblePersonName != null">responsible_person_name,</if>
            <if test="responsiblePersonId != null">responsible_person_id,</if>
            <if test="maintenanceCenterId != null">maintenance_center_id,</if>
            <if test="stationId != null">station_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="equipmentCode != null and equipmentCode != ''">#{equipmentCode},</if>
            <if test="maintenanceCenter != null">#{maintenanceCenter},</if>
            <if test="station != null">#{station},</if>
            <if test="brand != null">#{brand},</if>
            <if test="model != null">#{model},</if>
            <if test="equipmentType != null">#{equipmentType},</if>
            <if test="status != null">#{status},</if>
            <if test="responsiblePersonName != null">#{responsiblePersonName},</if>
            <if test="responsiblePersonId != null">#{responsiblePersonId},</if>
            <if test="maintenanceCenterId != null">#{maintenanceCenterId},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateGxtEquipment" parameterType="GxtEquipment">
        update gxt_equipment
        <trim prefix="SET" suffixOverrides=",">
            <if test="equipmentCode != null and equipmentCode != ''">equipment_code = #{equipmentCode},</if>
            <if test="maintenanceCenter != null">maintenance_center = #{maintenanceCenter},</if>
            <if test="station != null">station = #{station},</if>
            <if test="brand != null">brand = #{brand},</if>
            <if test="model != null">model = #{model},</if>
            <if test="equipmentType != null">equipment_type = #{equipmentType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="responsiblePersonName != null">responsible_person_name = #{responsiblePersonName},</if>
            <if test="responsiblePersonId != null">responsible_person_id = #{responsiblePersonId},</if>
            <if test="maintenanceCenterId != null">maintenance_center_id = #{maintenanceCenterId},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where equipment_id = #{equipmentId}
    </update>

    <delete id="deleteGxtEquipmentByEquipmentId" parameterType="Long">
        delete from gxt_equipment where equipment_id = #{equipmentId}
    </delete>

    <delete id="deleteGxtEquipmentByEquipmentIds" parameterType="String">
        delete from gxt_equipment where equipment_id in 
        <foreach item="equipmentId" collection="array" open="(" separator="," close=")">
            #{equipmentId}
        </foreach>
    </delete>
    
    <select id="selectMaintenanceCenterList" resultType="String">
        select distinct maintenance_center from gxt_equipment where maintenance_center is not null and maintenance_center != '' order by maintenance_center
    </select>
    
    <select id="selectStationListByMaintenanceCenter" parameterType="String" resultType="String">
        select distinct station from gxt_equipment where maintenance_center = #{maintenanceCenter} and station is not null and station != '' order by station
    </select>
    
    <select id="selectAllModels" resultType="String">
        select distinct model from gxt_equipment where model is not null and model != '' order by model
    </select>
    
    <select id="selectModelListByStationId" parameterType="Long" resultType="String">
        select distinct model from gxt_equipment where station_id = #{stationId} and model is not null and model != '' order by model
    </select>
</mapper>