/*
 * 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_Device;
import com.ifish.bean.Tbl_Device_Statistics;
import com.ifish.bean.Tbl_Device_User;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

/**
 *
 * @author Administrator
 */
public class Tbl_Device_MapperSql {

    /**
     * 修改设备和用户的表
     *
     * @param device_User
     * @return
     */
    public String updateTblDeviceUser(@Param("deviceUser") Tbl_Device_User device_User) {
        StringBuilder sb = new StringBuilder();

        sb.append("UPDATE tbl_device_user SET ");

        if (device_User.getUserId() != null && device_User.getUserId() > 0) {
            sb.append("user_id = #{deviceUser.userId}, ");
        }
        if (device_User.getDeviceId() != null && device_User.getDeviceId() > 0) {
            sb.append("device_id = #{deviceUser.deviceId}, ");
        }
        if (StringUtils.isNotBlank(device_User.getIsMaster())) {
            sb.append("is_master = #{deviceUser.isMaster}, ");
        }
        if (StringUtils.isNotBlank(device_User.getShowName())) {
            sb.append("show_name = #{deviceUser.showName}, ");
        }
        if (device_User.getCreateTime() != null && StringUtils.isNotBlank(device_User.getCreateTime().toString())) {
            sb.append("create_time = #{deviceUser.createTime}, ");
        }
        if (StringUtils.isNotBlank(device_User.getCustomIconName())) {
            sb.append("custom_icon_name = #{deviceUser.customIconName}, ");
        }
        if (StringUtils.isNotBlank(device_User.getCustomShowName())) {
            sb.append("custom_show_name = #{deviceUser.customShowName}, ");
        }
        if (StringUtils.isNotBlank(device_User.getIsLook())) {
            sb.append("is_look = #{deviceUser.isLook}, ");
        }
        if (StringUtils.isNotBlank(device_User.getIsLive())) {
            sb.append("is_live = #{deviceUser.isLive}, ");
        }
        sb.append("update_time = now() ");
        sb.append(" WHERE id = #{deviceUser.id}");
        return sb.toString();
    }

    /**
     * 插入一条设备统计信息表
     *
     * @param device_Statistics
     * @return
     */
    public String insertDeviceStatistics(@Param("deviceStatistics") Tbl_Device_Statistics device_Statistics) {
        StringBuilder sb = new StringBuilder();

        sb.append("INSERT tbl_device_statistics(device_id,mac_address,login_count,login_time,first_activate,create_time,is_charge,sdk_version,sdk_time,create_date,authorize_time,create_code,hardware_type,factory_code,brand_code)");
        sb.append("values(#{deviceStatistics.deviceId},#{deviceStatistics.macAddress},#{deviceStatistics.loginCount},#{deviceStatistics.loginTime},#{deviceStatistics.firstActivate},#{deviceStatistics.createTime},");
        sb.append("#{deviceStatistics.isCharge},#{deviceStatistics.sdkVersion},#{deviceStatistics.sdkTime},#{deviceStatistics.createDate},#{deviceStatistics.authorizeTime},#{deviceStatistics.createCode},");
        sb.append("#{deviceStatistics.hardwareType},#{deviceStatistics.factoryCode},#{deviceStatistics.brandCode})");
        return sb.toString();
    }

    /**
     * 修改设备统计信息表
     *
     * @param device_Statistics
     * @return
     */
    public String updateDeviceStatistics(@Param("deviceStatistics") Tbl_Device_Statistics device_Statistics) {
        StringBuilder sb = new StringBuilder();

        sb.append("UPDATE tbl_device_statistics SET ");

        if (device_Statistics.getDeviceId() != null && device_Statistics.getDeviceId() > 0) {
            sb.append("device_id = #{deviceStatistics.deviceId}, ");
        }
        if (StringUtils.isNotBlank(device_Statistics.getMacAddress())) {
            sb.append("mac_address = #{deviceStatistics.macAddress}, ");
        }
        if (device_Statistics.getLoginCount() != null && device_Statistics.getLoginCount() > 0) {
            sb.append("login_count = #{deviceStatistics.loginCount}, ");
        }
        if (device_Statistics.getLoginTime() != null && StringUtils.isNotBlank(device_Statistics.getLoginTime().toString())) {
            sb.append("login_time= #{deviceStatistics.loginTime}, ");
        }
        if (device_Statistics.getFirstActivate() != null && StringUtils.isNotBlank(device_Statistics.getFirstActivate().toString())) {
            sb.append("first_activate= #{deviceStatistics.firstActivate}, ");
        }
        if (device_Statistics.getCreateTime() != null && StringUtils.isNotBlank(device_Statistics.getCreateTime().toString())) {
            sb.append("create_time= #{deviceStatistics.createTime}, ");
        }
        if (StringUtils.isNotBlank(device_Statistics.getIsCharge())) {
            sb.append("is_charge = #{deviceStatistics.isCharge}, ");
        }
        if (StringUtils.isNotBlank(device_Statistics.getSdkVersion())) {
            sb.append("sdk_version = #{deviceStatistics.sdkVersion}, ");
        }
        if (device_Statistics.getSdkTime() != null && StringUtils.isNotBlank(device_Statistics.getSdkTime().toString())) {
            sb.append("sdk_time= #{deviceStatistics.sdkTime}, ");
        }
        if (device_Statistics.getCreateDate() != null && StringUtils.isNotBlank(device_Statistics.getCreateDate().toString())) {
            sb.append("create_date= #{deviceStatistics.createDate}, ");
        }
        if (StringUtils.isNotBlank(device_Statistics.getIsUpgrade())) {
            sb.append("is_upgrade = #{deviceStatistics.isUpgrade}, ");
        }
        if (StringUtils.isNotBlank(device_Statistics.getUpgradeVersion())) {
            sb.append("upgrade_version = #{deviceStatistics.upgradeVersion}, ");
        }
        if (device_Statistics.getUpgradeTime() != null && StringUtils.isNotBlank(device_Statistics.getUpgradeTime().toString())) {
            sb.append("upgrade_time= #{deviceStatistics.upgradeTime}, ");
        }
        if (device_Statistics.getAuthorizeTime() != null && StringUtils.isNotBlank(device_Statistics.getAuthorizeTime().toString())) {
            sb.append("authorize_time= #{deviceStatistics.authorizeTime}, ");
        }
        if (StringUtils.isNotBlank(device_Statistics.getCreateCode())) {
            sb.append("create_code = #{deviceStatistics.createCode}, ");
        }
        if (device_Statistics.getMcuCount() != null && device_Statistics.getMcuCount() > 0) {
            sb.append("mcu_count = #{deviceStatistics.mcuCount}, ");
        }
        if (device_Statistics.getModuleCount() != null && device_Statistics.getModuleCount() > 0) {
            sb.append("module_count = #{deviceStatistics.moduleCount}, ");
        }
        if (device_Statistics.getRouterCount() != null && device_Statistics.getRouterCount() > 0) {
            sb.append("router_count = #{deviceStatistics.routerCount}, ");
        }
        if (device_Statistics.getServerCount() != null && device_Statistics.getServerCount() > 0) {
            sb.append("server_count = #{deviceStatistics.serverCount}, ");
        }
        if (device_Statistics.getServerTryCount() != null && device_Statistics.getServerTryCount() > 0) {
            sb.append("server_try_count = #{deviceStatistics.serverTryCount}, ");
        }
        if (StringUtils.isNotBlank(device_Statistics.getSoftwareVersion())) {
            sb.append("software_version = #{deviceStatistics.softwareVersion}, ");
        }
        if (StringUtils.isNotBlank(device_Statistics.getHardwareType())) {
            sb.append("hardware_type = #{deviceStatistics.hardwareType}, ");
        }
        if (StringUtils.isNotBlank(device_Statistics.getFactoryCode())) {
            sb.append("factory_code = #{deviceStatistics.factoryCode}, ");
        }
        if (StringUtils.isNotBlank(device_Statistics.getBrandCode())) {
            sb.append("brand_code = #{deviceStatistics.brandCode}, ");
        }
        if (sb.lastIndexOf(",") == sb.length() - 2) {
            sb.deleteCharAt(sb.length() - 2);
        }
        sb.append(" WHERE id = #{deviceStatistics.id}");
        return sb.toString();
    }

    /**
     * 插入一条设备信息
     *
     * @param device
     * @return
     */
    public String insertDevice(@Param("device") Tbl_Device device) {
        StringBuilder sb = new StringBuilder();

        sb.append("INSERT INTO tbl_device(server_ip,device_ip,is_camera,mac_address,login_time,is_blacklist,hardware_type,on_off,today_remind,water_remind,remind_date,remind_cycle,factory_code,brand_code,");
        sb.append("create_time,active_code,active_time,update_time,camera_id) VALUES(#{device.serverIp},#{device.deviceIp},#{device.isCamera},#{device.macAddress},#{device.loginTime},#{device.isBlacklist},#{device.hardwareType},#{device.onOff},");
        sb.append("#{device.todayRemind},#{device.waterRemind},#{device.remindDate},#{device.remindCycle},#{device.factoryCode},#{device.brandCode},#{device.createTime},#{device.activeCode},#{device.activeTime},#{device.updateTime},#{device.cameraId})");

        return sb.toString();
    }

    /**
     * 插入设备和用户信息
     *
     * @param deviceUser
     * @return
     */
    public String insertDeviceUser(@Param("deviceUser") Tbl_Device_User deviceUser) {
        StringBuilder sb = new StringBuilder();

        sb.append("INSERT INTO tbl_device_user (user_id,device_id,is_master,show_name,create_time,update_time,custom_icon_name,custom_show_name,is_look,is_live) VALUES(");
        sb.append("#{deviceUser.userId},#{deviceUser.deviceId},#{deviceUser.isMaster},#{deviceUser.showName},#{deviceUser.createTime},#{deviceUser.updateTime},#{deviceUser.customIconName},#{deviceUser.customShowName},#{deviceUser.isLook},#{deviceUser.isLive})");

        return sb.toString();
    }

    /**
     * 修改设备信息
     *
     * @param device
     * @return
     */
    public String updateTbl_Device(@Param("device") Tbl_Device device) {
        StringBuilder sb = new StringBuilder();

        sb.append("UPDATE tbl_device SET ");
        if (StringUtils.isNotBlank(device.getServerIp())) {
            sb.append("server_ip = #{device.serverIp}, ");
        }
        if (StringUtils.isNotBlank(device.getDeviceIp())) {
            sb.append("device_ip = #{device.deviceIp}, ");
        }
        if (StringUtils.isNotBlank(device.getIsCamera())) {
            sb.append("is_camera = #{device.isCamera}, ");
        }
        if (StringUtils.isNotBlank(device.getMacAddress())) {
            sb.append("mac_address = #{device.macAddress}, ");
        }
        if (device.getLoginTime() != null && StringUtils.isNotBlank(device.getLoginTime().toString())) {
            sb.append("login_time = #{device.loginTime}, ");
        }
        if (device.getRemindDate() != null && StringUtils.isNotBlank(device.getRemindDate().toString())) {
            sb.append("remind_date = #{device.loginTime}, ");
        }
        if (StringUtils.isNotBlank(device.getIsBlacklist())) {
            sb.append("is_blacklist = #{device.isBlacklist}, ");
        }
        if (StringUtils.isNotBlank(device.getHardwareType())) {
            sb.append("hardware_type = #{device.hardwareType}, ");
        }
        if (StringUtils.isNotBlank(device.getOnOff())) {
            sb.append("on_off = #{device.onOff}, ");
        }
        if (StringUtils.isNotBlank(device.getTodayRemind())) {
            sb.append("today_remind = #{device.todayRemind}, ");
        }
        if (StringUtils.isNotBlank(device.getWaterRemind())) {
            sb.append("water_remind = #{device.waterRemind}, ");
        }
        if (device.getRemindCycle() != null && device.getRemindCycle() > 0) {
            sb.append("remind_cycle = #{device.remindCycle}, ");
        }
        if (StringUtils.isNotBlank(device.getFactoryCode())) {
            sb.append("factory_code = #{device.factoryCode}, ");
        }
        if (StringUtils.isNotBlank(device.getBrandCode())) {
            sb.append("brand_code = #{device.brandCode}, ");
        }
        if (StringUtils.isNotBlank(device.getCameraId())) {
            sb.append("camera_id = #{device.cameraId}, ");
        }
        if (device.getCreateTime() != null && StringUtils.isNotBlank(device.getCreateTime().toString())) {
            sb.append("create_time = #{device.createTime}, ");
        }
        if (device.getActiveTime() != null && StringUtils.isNotBlank(device.getActiveTime().toString())) {
            sb.append("active_time = #{device.activeTime}, ");
        }

        if (StringUtils.isNotBlank(device.getActiveCode())) {
            sb.append("active_code = #{device.activeCode}, ");
        }
        sb.append("update_time = now() ");
        sb.append(" WHERE device_id = #{device.deviceId}");
        return sb.toString();
    }
}
