package models

import (
	"net/url"
	"strconv"
	"time"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"

	"lc/common/util"
)

var G_db *gorm.DB

type LcModel struct {
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`
}

func CreateTable() {
	//if !G_db.HasTable(&Gateway{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&Gateway{})
	//}
	if !G_db.HasTable(&GatewayDevice{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&GatewayDevice{})
	}
	if !G_db.HasTable(&GatewaySerial{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&GatewaySerial{})
	}
	if !G_db.HasTable(&GatewaySysInfo{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&GatewaySysInfo{})
	}
	if !G_db.HasTable(&DeviceHourData{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceHourData{})
	}
	if !G_db.HasTable(&DeviceDayData{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceDayData{})
	}
	if !G_db.HasTable(&DeviceCmdRecord{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceCmdRecord{})
	}
	//if !G_db.HasTable(&DeviceConcentrator{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceConcentrator{})
	//}
	//if !G_db.HasTable(&DeviceEnvironment{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceEnvironment{})
	//}
	//if !G_db.HasTable(&DeviceLampController{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceLampController{})
	//}
	//if !G_db.HasTable(&Lampstrategy{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&Lampstrategy{})
	//}
	if !G_db.HasTable(&DeviceEvents{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceEvents{})
	}
	if !G_db.HasTable(&DeviceLampEvents{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceLampEvents{})
	}
	if !G_db.HasTable(&DeviceAlarm{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceAlarm{})
	}
	if !G_db.HasTable(&DeviceEnergyDayData{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceEnergyDayData{})
	}
	if !G_db.HasTable(&DeviceEnergyMonthData{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceEnergyMonthData{})
	}
	if !G_db.HasTable(&CameraDevice{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&CameraDevice{})
	}
	if !G_db.HasTable(&IPCAlarm{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&IPCAlarm{})
	}
	if !G_db.HasTable(&SosAlarm{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&SosAlarm{})
	}
	if !G_db.HasTable(&DeviceLampEventsSplit{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceLampEventsSplit{})
	}
	if !G_db.HasTable(&LightingData{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LightingData{})
	}
	if !G_db.HasTable(&LightingRateData{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LightingRateData{})
	}
	if !G_db.HasTable(&IpcPresets{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&IpcPresets{})
	}
	if !G_db.HasTable(&LightingRateMiniteData{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LightingRateMiniteData{})
	}
	//if !G_db.HasTable(&LedDevice{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedDevice{})
	//}
	//if !G_db.HasTable(&LedCommandRecord{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedCommandRecord{})
	//}
	//if !G_db.HasTable(&LedStatusRaw{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedStatusRaw{})
	//}
	//if !G_db.HasTable(&LedPowerStatusRecord{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedPowerStatusRecord{})
	//}
	//if !G_db.HasTable(&LedVsnStatusRecord{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedVsnStatusRecord{})
	//}
	//if !G_db.HasTable(&LedPlayingRecord{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedPlayingRecord{})
	//}
	//if !G_db.HasTable(&LedDimension{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedDimension{})
	//}
	//if !G_db.HasTable(&LedProgramLibrary{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedProgramLibrary{})
	//}
	//if !G_db.HasTable(&LedLanProgramLibrary{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedLanProgramLibrary{})
	//}
	//if !G_db.HasTable(&LedPrograms{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedPrograms{})
	//}
	//if !G_db.HasTable(&LedProgramMedia{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedProgramMedia{})
	//}
	//if !G_db.HasTable(&LedDownlaodInfo{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedDownlaodInfo{})
	//}
	//if !G_db.HasTable(&LedProgramsSchedules{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedProgramsSchedules{})
	//}
	//if !G_db.HasTable(&LedCommandSchedules{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedCommandSchedules{})
	//}
	if !G_db.HasTable(&IotModel{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&IotModel{})
	}
	if !G_db.HasTable(&IotModelDataUp{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&IotModelDataUp{})
	}
	if !G_db.HasTable(&IotModelCmdDown{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&IotModelCmdDown{})
	}
	if !G_db.HasTable(&LampSwitchRecord{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LampSwitchRecord{})
	}
	//if !G_db.HasTable(&ItsDevice{}) {
	//	G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsDevice{})
	//}
	if !G_db.HasTable(&ItsVehicleSpeed{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsVehicleSpeed{})
	}
	if !G_db.HasTable(&ItsVehicleDirection{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsVehicleDirection{})
	}
	if !G_db.HasTable(&ItsVehicleProvince{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsVehicleProvince{})
	}
	if !G_db.HasTable(&ItsVehicleProvinceCity{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsVehicleProvinceCity{})
	}
	if !G_db.HasTable(&ItsVehicleType{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsVehicleType{})
	}
	if !G_db.HasTable(&AlarmStrategy{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&AlarmStrategy{})
	}
	if !G_db.HasTable(&AlarmAssociate{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&AlarmAssociate{})
	}
	if !G_db.HasTable(&DeviceSensor{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceSensor{})
	}
	if !G_db.HasTable(&RadarData{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&RadarData{})
	}
	if !G_db.HasTable(&RadarHourData{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&RadarHourData{})
	}
	if !G_db.HasTable(&RadarDayData{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&RadarDayData{})
	}
	if !G_db.HasTable(&RadarDevice{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&RadarDevice{})
	}
	if !G_db.HasTable(&CableGuardianStatus{}) {
		G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&CableGuardianStatus{})
	}
}

func InitDB(conf *util.MySQLConfig) {
	dsn := conf.Mysql_User + ":" + conf.Mysql_Password + "@tcp(" + conf.Mysql_Host + ":" + strconv.Itoa(conf.Mysql_Port) + ")/" + conf.Mysql_Name + "?charset=utf8&parseTime=True" + "&loc=" + url.QueryEscape(conf.Mysql_Timezone)
	db0, err := gorm.Open("mysql", dsn)
	if err != nil {
		panic(err)
	} else {
		G_db = db0
		G_db.DB().SetMaxOpenConns(32)
		G_db.DB().SetMaxIdleConns(5)
		G_db.LogMode(false)
		CreateTable()
	}
}

func DestroyDB() {
	if G_db != nil {
		G_db.Close()
	}
}
