<?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.yijia.station.mapper.StationDeviceManageMapper">
    
    <resultMap type="StationDeviceManage" id="StationDeviceManageResult">
        <result property="deviceId"    column="device_id"    />
        <result property="deviceNo"    column="device_no"    />
        <result property="deviceName"    column="device_name"    />
        <result property="stationId"    column="station_id"    />
        <result property="stationName"    column="station_name"    />
        <result property="deviceType"    column="device_type"    />
        <result property="deviceSerialNum"    column="device_serial_num"    />
        <result property="gunNo"    column="gun_no"    />
        <result property="deviceStatus"    column="device_status"    />
        <result property="deviceFactory"    column="device_factory"    />
    </resultMap>

    <sql id="selectStationDeviceManageVo">
        select device_id, device_no, device_name, station_id, station_name, device_type, device_serial_num, gun_no, device_status, device_factory from station_device_manage
    </sql>

    <select id="selectStationDeviceManageList" parameterType="StationDeviceManage" resultMap="StationDeviceManageResult">
        <include refid="selectStationDeviceManageVo"/>
        <where>  
            <if test="deviceNo != null  and deviceNo != ''"> and device_no = #{deviceNo}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="stationName != null  and stationName != ''"> and station_name like concat('%', #{stationName}, '%')</if>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="deviceSerialNum != null  and deviceSerialNum != ''"> and device_serial_num = #{deviceSerialNum}</if>
            <if test="gunNo != null  and gunNo != ''"> and gun_no = #{gunNo}</if>
            <if test="deviceStatus != null  and deviceStatus != ''"> and device_status = #{deviceStatus}</if>
            <if test="deviceFactory != null  and deviceFactory != ''"> and device_factory = #{deviceFactory}</if>
            <if test="stationIdList != null ">
                and station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by device_id desc
    </select>
    
    <select id="selectStationDeviceManageById" parameterType="Long" resultMap="StationDeviceManageResult">
        <include refid="selectStationDeviceManageVo"/>
        where device_id = #{deviceId}
    </select>
        
    <insert id="insertStationDeviceManage" parameterType="StationDeviceManage" useGeneratedKeys="true" keyProperty="deviceId">
        insert into station_device_manage
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceNo != null">device_no,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="stationId != null">station_id,</if>
            <if test="stationName != null">station_name,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="deviceSerialNum != null">device_serial_num,</if>
            <if test="gunNo != null">gun_no,</if>
            <if test="deviceStatus != null">device_status,</if>
            <if test="deviceFactory != null">device_factory,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceNo != null">#{deviceNo},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="stationName != null">#{stationName},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="deviceSerialNum != null">#{deviceSerialNum},</if>
            <if test="gunNo != null">#{gunNo},</if>
            <if test="deviceStatus != null">#{deviceStatus},</if>
            <if test="deviceFactory != null">#{deviceFactory},</if>
         </trim>
    </insert>

    <update id="updateStationDeviceManage" parameterType="StationDeviceManage">
        update station_device_manage
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceNo != null">device_no = #{deviceNo},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="deviceSerialNum != null">device_serial_num = #{deviceSerialNum},</if>
            <if test="gunNo != null">gun_no = #{gunNo},</if>
            <if test="deviceStatus != null">device_status = #{deviceStatus},</if>
            <if test="deviceFactory != null">device_factory = #{deviceFactory},</if>
        </trim>
        where device_id = #{deviceId}
    </update>

    <delete id="deleteStationDeviceManageById" parameterType="Long">
        delete from station_device_manage where device_id = #{deviceId}
    </delete>

    <delete id="deleteStationDeviceManageByIds" parameterType="String">
        delete from station_device_manage where device_id in
        <foreach item="deviceId" collection="array" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </delete>

    <!--根据油站Id获取打印机编号 -->
    <select id="getPrinterSnByStationId" parameterType="map" resultType="map">
        select * from station_device_manage where device_id =(
            SELECT max(device_id) from  station_device_manage
            <where>
                <if test="stationId != null and stationId !=''">
                    AND station_id = #{stationId}
                </if>
                <if test="deviceType != null and deviceType !=''">
                    AND device_type = #{deviceType}
                </if>
            </where>
        );
    </select>
</mapper>