<?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.usky.demo.mapper.SysUserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.demo.domain.SysUser">
        <id column="user_id" property="userId" />
        <result column="dept_id" property="deptId" />
        <result column="user_name" property="userName" />
        <result column="nick_name" property="nickName" />
        <result column="user_type" property="userType" />
        <result column="email" property="email" />
        <result column="phonenumber" property="phonenumber" />
        <result column="sex" property="sex" />
        <result column="avatar" property="avatar" />
        <result column="password" property="password" />
        <result column="status" property="status" />
        <result column="del_flag" property="delFlag" />
        <result column="login_ip" property="loginIp" />
        <result column="login_date" property="loginDate" />
        <result column="create_by" property="createBy" />
        <result column="create_time" property="createTime" />
        <result column="update_by" property="updateBy" />
        <result column="update_time" property="updateTime" />
        <result column="remark" property="remark" />
    </resultMap>
    <insert id="DeviceRealDataToDB" parameterType="java.util.List">
    insert into data_real_time(
    device_id,
    product_code,
    device_type,
    attribute_name,
    attribute_data,
    data_time,
    insert_time)
    values
    <foreach collection="list" item="item" separator=",">
        (#{item.deviceId},
        #{item.productCode},
        #{item.deviceType},
        #{item.attributeName},
        #{item.attributeData},
        #{item.dataTime},
        #{item.insertTime})
    </foreach>
        ON DUPLICATE KEY UPDATE  attribute_data = values(attribute_data),data_time = values(data_time),insert_time = values(insert_time);
    </insert>
    <insert id="DeviceDataToDB" parameterType="java.util.List">
        insert into sp_d${deviceId}(
            device_id,
            product_code,
            device_type,
            attribute_name,
            attribute_data,
            data_time,
            insert_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.deviceId},
            #{item.productCode},
            #{item.deviceType},
            #{item.attributeName},
            #{item.attributeData},
            #{item.dataTime},
            #{item.insertTime})
        </foreach>
        ON DUPLICATE KEY UPDATE  attribute_data = values(attribute_data),insert_time = values(insert_time);
    </insert>
    <!-- 判断表是否存在 -->
    <select id="existTable" parameterType="string" resultType="java.lang.Integer">
        SELECT COUNT(*) as count FROM information_schema.TABLES WHERE table_name = #{tableName}
    </select>
    <!-- 创建对应设备历史数据表 -->
    <update id="createTable" parameterType="string">
        CREATE TABLE IF NOT EXISTS ${tableName} (
        `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
        `device_id` VARCHAR(50) DEFAULT NULL COMMENT '设备ID',
        `product_code` VARCHAR(50) DEFAULT NULL COMMENT '产品编码',
        `device_type` INT(5) DEFAULT NULL COMMENT '设备类型',
        `attribute_name` VARCHAR(50) DEFAULT NULL COMMENT '属性名称',
        `attribute_data` VARCHAR(20) DEFAULT NULL COMMENT '属性值',
        `data_time` DATETIME DEFAULT NULL COMMENT '数据时间',
        `insert_time` DATETIME DEFAULT NULL COMMENT '插入时间',
        PRIMARY KEY (`id`) USING BTREE,
        UNIQUE KEY `uid_dad` (`device_id`,`attribute_name`,`data_time`)
        ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
    </update>

</mapper>
