<?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.psdc.mapper.PsdcDeviceMapper">
    <resultMap type="com.psdc.entity.PsdcDevice" id="PsdcDeviceMap">
        <result property="deviceId" column="device_id" />
        <result property="userId" column="user_id" />
        <result property="deviceType" column="device_type" />
        <result property="deviceSn" column="device_sn" />
        <result property="deviceName" column="device_name" />
        <result property="hardVersion" column="hard_version" />
        <result property="softVersion" column="soft_version" />
        <result property="deviceAddress" column="device_address" />
        <result property="startTime" column="start_time" />
        <result property="deviceStatus" column="device_status" />
        <result property="photoUrl" column="photo_url" />
        <result property="deviceRunstatus" column="device_runstatus" />
        <result property="photoUrl" column="photo_url" />
        <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>

    <resultMap id="PsdcDeviceMap2" type="com.psdc.entity.vo.DeviceStatusVo">
        <result property="deviceId" column="device_id" />
        <result property="deviceType" column="device_type" />
        <result property="deviceSn" column="device_sn" />
        <result property="deviceName" column="device_name" />
        <result property="photoUrl" column="photo_url" />
        <result property="deviceRunstatus" column="device_runstatus" />
    </resultMap>

    <select id="queryDeviceStatusById" resultMap="PsdcDeviceMap2">
        Select device_id, device_type, device_sn, device_name, device_runstatus, photo_url From psdc_device
        Where device_id = #{deviceId}
    </select>

    <!-- 通过ID查询单条数据 -->
    <select id="queryById" resultMap="PsdcDeviceMap">
        select device_id,
               user_id,
               device_type,
               device_sn,
               device_name,
               hard_version,
               soft_version,
               device_address,
               start_time,
               device_status,
               device_runstatus,
               photo_url,
               create_by,
               create_time,
               update_by,
               update_time
        from psdc_device
        where device_id = #{deviceId}
    </select>

    <select id="selMyWorkDevices" resultType="java.lang.Integer">
        Select device_id From psdc_device
        <where>
            <if test="devType != null and devType != ''">
                device_type = #{devType}
            </if>
            <if test="userId != null and userId != ''">
                And user_id = #{userId}
            </if>
        </where>
    </select>

    <select id="queryByUserId" resultMap="PsdcDeviceMap">
        select device_id,
               user_id,
               device_type,
               device_sn,
               device_name,
               hard_version,
               soft_version,
               device_address,
               start_time,
               device_status,
               device_runstatus,
               photo_url,
               create_by,
               create_time,
               update_by,
               update_time
        from psdc_device
        where user_id = #{userId}
    </select>

    <!--分页查询指定行数据-->
    <select id="queryAllByLimit" resultMap="PsdcDeviceMap">
        select
        device_id,user_id,device_type,device_sn,device_name,hard_version,soft_version,device_address,start_time,device_status,device_runstatus,photo_url,create_by,create_time,update_by,update_time
        from psdc_device
        <where>
            <if test="deviceId != null and deviceId != ''">
                and device_id = #{deviceId}
            </if>
            <if test="userId != null and userId != ''">
                and user_id = #{userId}
            </if>
            <if test="deviceType != null and deviceType != ''">
                and device_type = #{deviceType}
            </if>
            <if test="deviceSn != null and deviceSn != ''">
                and device_sn = #{deviceSn}
            </if>
            <if test="deviceName != null and deviceName != ''">
                and device_name = #{deviceName}
            </if>
            <if test="hardVersion != null and hardVersion != ''">
                and hard_version = #{hardVersion}
            </if>
            <if test="softVersion != null and softVersion != ''">
                and soft_version = #{softVersion}
            </if>
            <if test="deviceAddress != null and deviceAddress != ''">
                and device_address = #{deviceAddress}
            </if>
            <if test="startTime != null and startTime != ''">
                and start_time = #{startTime}
            </if>
            <if test="deviceStatus != null and deviceStatus != ''">
                and device_status = #{deviceStatus}
            </if>
            <if test="deviceRunstatus != null and deviceRunstatus != ''">
                and device_runstatus = #{deviceRunstatus}
            </if>
            <if test="photoUrl != null and photoUrl != ''">
                and photo_url = #{photoUrl}
            </if>
            <if test="createBy != null and createBy != ''">
                and create_by = #{createBy}
            </if>
            <if test="createTime != null and createTime != ''">
                and create_time = #{createTime}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and update_by = #{updateBy}
            </if>
            <if test="updateTime != null and updateTime != ''">
                and update_time = #{updateTime}
            </if>
        </where>
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from psdc_device
        <where>
            <if test="deviceId != null and deviceId != ''">
                and device_id = #{deviceId}
            </if>
            <if test="userId != null and userId != ''">
                and user_id = #{userId}
            </if>
            <if test="deviceType != null and deviceType != ''">
                and device_type = #{deviceType}
            </if>
            <if test="deviceSn != null and deviceSn != ''">
                and device_sn = #{deviceSn}
            </if>
            <if test="deviceName != null and deviceName != ''">
                and device_name = #{deviceName}
            </if>
            <if test="hardVersion != null and hardVersion != ''">
                and hard_version = #{hardVersion}
            </if>
            <if test="softVersion != null and softVersion != ''">
                and soft_version = #{softVersion}
            </if>
            <if test="deviceAddress != null and deviceAddress != ''">
                and device_address = #{deviceAddress}
            </if>
            <if test="startTime != null and startTime != ''">
                and start_time = #{startTime}
            </if>
            <if test="deviceStatus != null and deviceStatus != ''">
                and device_status = #{deviceStatus}
            </if>
            <if test="deviceRunstatus != null and deviceRunstatus != ''">
                and device_runstatus = #{deviceRunstatus}
            </if>
            <if test="photoUrl != null and photoUrl != ''">
                and photo_url = #{photoUrl}
            </if>
            <if test="createBy != null and createBy != ''">
                and create_by = #{createBy}
            </if>
            <if test="createTime != null and createTime != ''">
                and create_time = #{createTime}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and update_by = #{updateBy}
            </if>
            <if test="updateTime != null and updateTime != ''">
                and update_time = #{updateTime}
            </if>
        </where>
    </select>

    <!--新增数据-->
    <insert id="insert">
        insert into psdc_device(user_id, device_type, device_sn, device_name, hard_version, soft_version,
                                device_address, start_time, device_status, device_runstatus, photo_url, create_by,
                                create_time, update_by, update_time)
        values (#{userId}, #{deviceType}, #{deviceSn}, #{deviceName}, #{hardVersion}, #{softVersion}, #{deviceAddress},
                #{startTime}, #{deviceStatus}, #{deviceRunstatus}, #{photoUrl}, #{createBy}, #{createTime}, #{updateBy},
                #{updateTime})
    </insert>

    <insert id="saveDevicesData" parameterType="com.psdc.entity.PsdcDevice" useGeneratedKeys="true">
        Insert into psdc_device(user_id, device_type, device_sn, device_name, hard_version, soft_version,
        device_address, start_time, device_status, device_runstatus, photo_url, create_by,
        create_time, update_by, update_time)
        Values
        <foreach collection="devRecord" item="entity" separator=",">
            (NULL, #{entity.deviceType}, #{entity.deviceSn}, #{entity.deviceName}, #{entity.hardVersion}, #{entity.softVersion}, #{entity.deviceAddress},
            #{entity.startTime}, #{entity.deviceStatus}, #{entity.deviceRunstatus}, #{entity.photoUrl}, #{entity.createBy}, #{entity.createTime}, NULL, NULL)
        </foreach>
    </insert>

    <!-- 更新数据 -->
    <update id="update">
        update psdc_device
        <set>
            <if test="deviceId != null and deviceId != ''">
                device_id = #{deviceId},
            </if>
            <if test="userId != null and userId != ''">
                user_id = #{userId},
            </if>
            <if test="deviceType != null and deviceType != ''">
                device_type = #{deviceType},
            </if>
            <if test="deviceSn != null and deviceSn != ''">
                device_sn = #{deviceSn},
            </if>
            <if test="deviceName != null and deviceName != ''">
                device_name = #{deviceName},
            </if>
            <if test="hardVersion != null and hardVersion != ''">
                hard_version = #{hardVersion},
            </if>
            <if test="softVersion != null and softVersion != ''">
                soft_version = #{softVersion},
            </if>
            <if test="deviceAddress != null and deviceAddress != ''">
                device_address = #{deviceAddress},
            </if>
            <if test="startTime != null and startTime != ''">
                start_time = #{startTime},
            </if>
            <if test="deviceStatus != null and deviceStatus != ''">
                device_status = #{deviceStatus},
            </if>
            <if test="deviceRunstatus != null and deviceRunstatus != ''">
                device_runstatus = #{deviceRunstatus},
            </if>
            <if test="photoUrl != null and photoUrl != ''">
                photo_url = #{photoUrl},
            </if>
            <if test="createBy != null and createBy != ''">
                create_by = #{createBy},
            </if>
            <if test="createTime != null and createTime != ''">
                create_time = #{createTime},
            </if>
            <if test="updateBy != null and updateBy != ''">
                update_by = #{updateBy},
            </if>
            <if test="updateTime != null ">
                update_time = #{updateTime},
            </if>
        </set>
        where device_id = #{deviceId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from psdc_device where device_id in
        <foreach collection="deviceIds" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </delete>


    <update id="updateDevRunStatusByDevId">
        update psdc_device set device_runstatus = #{runStatus} where device_id = #{deviceId}
    </update>

    <!--    根据设备id查询设备名称-->
    <select id="queryDeviceNameByDeviceId" resultType="string">
        select device_name from psdc_device where device_id = #{deviceId}
    </select>

</mapper>