/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.mapper;

import com.ifish.bean.Tbl_Activa_Code;
import com.ifish.bean.Tbl_Device;
import com.ifish.bean.Tbl_Device_Statistics;
import com.ifish.bean.Tbl_Device_User;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

/**
 *
 * @author Administrator
 */
public interface Tbl_Device_Mapper {

    /**
     * 根据设备ID获取设备详情
     *
     * @param deviceid
     * @return
     */
    @Select("SELECT device_id,server_ip,device_ip,is_camera,mac_address,login_time,is_blacklist,hardware_type,on_off,"
            + "today_remind,water_remind,remind_cycle,remind_date,factory_code,brand_code,create_time,active_code,active_time,update_time,camera_id from tbl_device WHERE device_id= #{deviceid}")
    Tbl_Device getDeviceById(@Param("deviceid") Integer deviceid);

    /**
     * 根据摄像头ID获取设备详情
     *
     * @param deviceid
     * @return
     */
    @Select("SELECT device_id,server_ip,device_ip,is_camera,mac_address,login_time,is_blacklist,hardware_type,on_off,"
            + "today_remind,water_remind,remind_cycle,remind_date,factory_code,brand_code,create_time,active_code,active_time,update_time,camera_id from tbl_device WHERE camera_id= #{cameraID}")
    Tbl_Device getDeviceByCameraId(@Param("cameraID") String cameraID);

    /**
     * 根据设备ID获取设备详情
     *
     * @param deviceid
     * @return
     */
    @Select("SELECT device_id,server_ip,device_ip,is_camera,mac_address,login_time,is_blacklist,hardware_type,on_off,"
            + "today_remind,water_remind,remind_cycle,remind_date,factory_code,brand_code,create_time,active_code,active_time,update_time,camera_id from tbl_device WHERE mac_address= #{mac_address}")
    Tbl_Device getDeviceByMacAddress(@Param("mac_address") String macAddress);

    /**
     * 根据用户ID获取用户绑定设备集合
     *
     * @param userid
     * @param deviceId
     * @return
     */
    @Select("SELECT id,user_id,device_id,is_master,show_name,create_time,update_time,custom_icon_name,custom_show_name,is_look,is_live from tbl_device_user WHERE user_id = #{userid}")
    List<Tbl_Device_User> getDeviceUsersByUserId(@Param("userid") Integer userid);

    /**
     * 根据设备ID和用户ID获取设备与其他用户的关系
     *
     * @param userid
     * @param deviceId
     * @return
     */
    @Select("SELECT id,user_id,device_id,is_master,show_name,create_time,update_time,custom_icon_name,custom_show_name,is_look,is_live from tbl_device_user WHERE user_id <> ${userid} AND device_id = #{deviceid}")
    List<Tbl_Device_User> getOtherDeviceUsersByOtherUserIdAndDeviceId(@Param("userid") Integer userid, @Param("deviceid") Integer deviceId);

    /**
     * 根据设备ID和用户ID获取设备与用户的关系
     *
     * @param userid
     * @param deviceId
     * @return
     */
    @Select("SELECT id,user_id,device_id,is_master,show_name,create_time,update_time,custom_icon_name,custom_show_name,is_look,is_live from tbl_device_user WHERE user_id = #{userid} AND device_id = #{deviceid}")
    Tbl_Device_User getDeviceUsersByUserIdAndDeviceId(@Param("userid") Integer userid, @Param("deviceid") Integer deviceId);

    /**
     * 根据设备ID查询此设备关联的所有用户
     *
     * @param deviceId
     * @return
     */
    @Select("SELECT id,user_id,device_id,is_master,show_name,create_time,update_time,custom_icon_name,custom_show_name,is_look,is_live from tbl_device_user WHERE device_id = #{deviceid}")
    List<Tbl_Device_User> getDeviceUsersByDeviceId(@Param("deviceid") Integer deviceId);

    /**
     * 修改用户和设备关系表数据
     *
     * @param device_User
     * @return
     */
    @UpdateProvider(method = "updateTblDeviceUser", type = Tbl_Device_MapperSql.class)
    int updateTblDeviceUser(@Param("deviceUser") Tbl_Device_User device_User);

    /**
     * 删除设备和用户的关系
     *
     * @param id
     * @return
     */
    @Delete("DELETE from tbl_device_user where id = #{id}")
    int deleteDeviceUserById(@Param("id") Integer id);

    /**
     * 根据设备ID查询设备统计信息
     *
     * @param deviceid
     * @return
     */
    @Select("SELECT id,device_id,mac_address,login_count,login_time,first_activate,create_time,is_charge,sdk_version,sdk_time,create_date,is_upgrade,upgrade_version,"
            + "upgrade_time,authorize_time,create_code,MCU_count,module_count,router_count,server_count,server_try_count,software_version,hardware_type,factory_code,brand_code FROM tbl_device_statistics WHERE device_id=#{deviceid}")
    Tbl_Device_Statistics getDevStatisticsByDeviceId(@Param("deviceid") Integer deviceid);

    /**
     * 根据设备ID查询设备统计信息
     *
     * @param deviceid
     * @return
     */
    @Select("SELECT id,device_id,mac_address,login_count,login_time,first_activate,create_time,is_charge,sdk_version,sdk_time,create_date,is_upgrade,upgrade_version,"
            + "upgrade_time,authorize_time,create_code,MCU_count,module_count,router_count,server_count,server_try_count,software_version,hardware_type,factory_code,brand_code FROM tbl_device_statistics WHERE mac_address=#{mac}")
    Tbl_Device_Statistics getDevStatisticsByMacAddress(@Param("mac") String macAddress);

    /**
     * 插入一条设备统计信息
     *
     * @param device_Statistics
     * @return
     */
    @InsertProvider(type = Tbl_Device_MapperSql.class, method = "insertDeviceStatistics")
    @SelectKey(statement = "select @@IDENTITY as id", keyProperty = "deviceStatistics.id", keyColumn = "id", before = false, resultType = int.class)
    Integer insertDeviceStatistics(@Param("deviceStatistics") Tbl_Device_Statistics device_Statistics);

    /**
     * 修改设备统计信息表
     *
     * @param device_Statistics
     * @return
     */
    @UpdateProvider(type = Tbl_Device_MapperSql.class, method = "updateDeviceStatistics")
    Integer updateDeviceStatistics(@Param("deviceStatistics") Tbl_Device_Statistics device_Statistics);

    /**
     * 插入一条设备信息
     *
     * @param device
     * @return
     */
    @InsertProvider(type = Tbl_Device_MapperSql.class, method = "insertDevice")
    @SelectKey(statement = "select @@IDENTITY as device_id", keyProperty = "device.deviceId", keyColumn = "device_id", before = false, resultType = int.class)
    Integer insertDevice(@Param("device") Tbl_Device device);

    /**
     * 插入一套设备与用户关系数据
     *
     * @param deviceUser
     * @return
     */
    @InsertProvider(type = Tbl_Device_MapperSql.class, method = "insertDeviceUser")
    @SelectKey(statement = "select @@IDENTITY as id", keyProperty = "deviceUser.id", keyColumn = "id", before = false, resultType = int.class)
    Integer insertDeviceUser(@Param("deviceUser") Tbl_Device_User deviceUser);

    /**
     * 根据ID删除设备信息数据(摄像头)
     *
     * @param id
     * @return
     */
    @Delete("DELETE from tbl_device where id = #{id}")
    int deleteDeviceById(@Param("id") Integer id);

    /**
     * 根据激活码查询激活码对象
     *
     * @param activeCode
     * @return
     */
    @Select("SELECT active_code,batch_code,is_used,create_time FROM tbl_activa_code WHERE active_code = #{code}")
    Tbl_Activa_Code getActiva_CodeByCode(@Param("code") String activeCode);

    /**
     * 修改设备信息
     *
     * @param device
     * @return
     */
    @UpdateProvider(type = Tbl_Device_MapperSql.class, method = "updateTbl_Device")
    Integer updateTbl_Device(@Param("device") Tbl_Device device);

    /**
     * 修改摄像头激活码信息为已使用
     *
     * @param activa_Code
     * @return
     */
    @Update("UPDATE tbl_activa_code set is_used='1' WHERE active_code = #{code}")
    Integer updateTblActivaCodeIsUse(@Param("code") String activa_Code);

}
