init.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package models
  2. import (
  3. "net/url"
  4. "strconv"
  5. "time"
  6. "github.com/jinzhu/gorm"
  7. _ "github.com/jinzhu/gorm/dialects/mysql"
  8. "lc/common/util"
  9. )
  10. var G_db *gorm.DB
  11. type LcModel struct {
  12. CreatedAt time.Time
  13. UpdatedAt time.Time
  14. DeletedAt *time.Time `sql:"index"`
  15. }
  16. func CreateTable() {
  17. //if !G_db.HasTable(&Gateway{}) {
  18. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&Gateway{})
  19. //}
  20. if !G_db.HasTable(&GatewayDevice{}) {
  21. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&GatewayDevice{})
  22. }
  23. if !G_db.HasTable(&GatewaySerial{}) {
  24. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&GatewaySerial{})
  25. }
  26. if !G_db.HasTable(&GatewaySysInfo{}) {
  27. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&GatewaySysInfo{})
  28. }
  29. if !G_db.HasTable(&DeviceHourData{}) {
  30. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceHourData{})
  31. }
  32. if !G_db.HasTable(&DeviceDayData{}) {
  33. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceDayData{})
  34. }
  35. if !G_db.HasTable(&DeviceCmdRecord{}) {
  36. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceCmdRecord{})
  37. }
  38. //if !G_db.HasTable(&DeviceConcentrator{}) {
  39. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceConcentrator{})
  40. //}
  41. //if !G_db.HasTable(&DeviceEnvironment{}) {
  42. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceEnvironment{})
  43. //}
  44. //if !G_db.HasTable(&DeviceLampController{}) {
  45. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceLampController{})
  46. //}
  47. //if !G_db.HasTable(&Lampstrategy{}) {
  48. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&Lampstrategy{})
  49. //}
  50. if !G_db.HasTable(&DeviceEvents{}) {
  51. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceEvents{})
  52. }
  53. if !G_db.HasTable(&DeviceLampEvents{}) {
  54. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceLampEvents{})
  55. }
  56. if !G_db.HasTable(&DeviceAlarm{}) {
  57. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceAlarm{})
  58. }
  59. if !G_db.HasTable(&DeviceEnergyDayData{}) {
  60. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceEnergyDayData{})
  61. }
  62. if !G_db.HasTable(&DeviceEnergyMonthData{}) {
  63. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceEnergyMonthData{})
  64. }
  65. if !G_db.HasTable(&CameraDevice{}) {
  66. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&CameraDevice{})
  67. }
  68. if !G_db.HasTable(&IPCAlarm{}) {
  69. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&IPCAlarm{})
  70. }
  71. if !G_db.HasTable(&SosAlarm{}) {
  72. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&SosAlarm{})
  73. }
  74. if !G_db.HasTable(&DeviceLampEventsSplit{}) {
  75. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceLampEventsSplit{})
  76. }
  77. if !G_db.HasTable(&LightingData{}) {
  78. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LightingData{})
  79. }
  80. if !G_db.HasTable(&LightingRateData{}) {
  81. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LightingRateData{})
  82. }
  83. if !G_db.HasTable(&IpcPresets{}) {
  84. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&IpcPresets{})
  85. }
  86. if !G_db.HasTable(&LightingRateMiniteData{}) {
  87. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LightingRateMiniteData{})
  88. }
  89. //if !G_db.HasTable(&LedDevice{}) {
  90. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedDevice{})
  91. //}
  92. //if !G_db.HasTable(&LedCommandRecord{}) {
  93. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedCommandRecord{})
  94. //}
  95. //if !G_db.HasTable(&LedStatusRaw{}) {
  96. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedStatusRaw{})
  97. //}
  98. //if !G_db.HasTable(&LedPowerStatusRecord{}) {
  99. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedPowerStatusRecord{})
  100. //}
  101. //if !G_db.HasTable(&LedVsnStatusRecord{}) {
  102. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedVsnStatusRecord{})
  103. //}
  104. //if !G_db.HasTable(&LedPlayingRecord{}) {
  105. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedPlayingRecord{})
  106. //}
  107. //if !G_db.HasTable(&LedDimension{}) {
  108. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedDimension{})
  109. //}
  110. //if !G_db.HasTable(&LedProgramLibrary{}) {
  111. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedProgramLibrary{})
  112. //}
  113. //if !G_db.HasTable(&LedLanProgramLibrary{}) {
  114. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedLanProgramLibrary{})
  115. //}
  116. //if !G_db.HasTable(&LedPrograms{}) {
  117. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedPrograms{})
  118. //}
  119. //if !G_db.HasTable(&LedProgramMedia{}) {
  120. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedProgramMedia{})
  121. //}
  122. //if !G_db.HasTable(&LedDownlaodInfo{}) {
  123. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedDownlaodInfo{})
  124. //}
  125. //if !G_db.HasTable(&LedProgramsSchedules{}) {
  126. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedProgramsSchedules{})
  127. //}
  128. //if !G_db.HasTable(&LedCommandSchedules{}) {
  129. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LedCommandSchedules{})
  130. //}
  131. if !G_db.HasTable(&IotModel{}) {
  132. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&IotModel{})
  133. }
  134. if !G_db.HasTable(&IotModelDataUp{}) {
  135. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&IotModelDataUp{})
  136. }
  137. if !G_db.HasTable(&IotModelCmdDown{}) {
  138. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&IotModelCmdDown{})
  139. }
  140. if !G_db.HasTable(&LampSwitchRecord{}) {
  141. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&LampSwitchRecord{})
  142. }
  143. //if !G_db.HasTable(&ItsDevice{}) {
  144. // G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsDevice{})
  145. //}
  146. if !G_db.HasTable(&ItsVehicleSpeed{}) {
  147. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsVehicleSpeed{})
  148. }
  149. if !G_db.HasTable(&ItsVehicleDirection{}) {
  150. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsVehicleDirection{})
  151. }
  152. if !G_db.HasTable(&ItsVehicleProvince{}) {
  153. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsVehicleProvince{})
  154. }
  155. if !G_db.HasTable(&ItsVehicleProvinceCity{}) {
  156. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsVehicleProvinceCity{})
  157. }
  158. if !G_db.HasTable(&ItsVehicleType{}) {
  159. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&ItsVehicleType{})
  160. }
  161. if !G_db.HasTable(&AlarmStrategy{}) {
  162. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&AlarmStrategy{})
  163. }
  164. if !G_db.HasTable(&AlarmAssociate{}) {
  165. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&AlarmAssociate{})
  166. }
  167. if !G_db.HasTable(&DeviceSensor{}) {
  168. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DeviceSensor{})
  169. }
  170. if !G_db.HasTable(&RadarData{}) {
  171. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&RadarData{})
  172. }
  173. if !G_db.HasTable(&RadarHourData{}) {
  174. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&RadarHourData{})
  175. }
  176. if !G_db.HasTable(&RadarDayData{}) {
  177. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&RadarDayData{})
  178. }
  179. if !G_db.HasTable(&RadarDevice{}) {
  180. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&RadarDevice{})
  181. }
  182. if !G_db.HasTable(&CableGuardianStatus{}) {
  183. G_db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&CableGuardianStatus{})
  184. }
  185. }
  186. func InitDB(conf *util.MySQLConfig) {
  187. 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)
  188. db0, err := gorm.Open("mysql", dsn)
  189. if err != nil {
  190. panic(err)
  191. } else {
  192. G_db = db0
  193. G_db.DB().SetMaxOpenConns(32)
  194. G_db.DB().SetMaxIdleConns(5)
  195. G_db.LogMode(false)
  196. CreateTable()
  197. }
  198. }
  199. func DestroyDB() {
  200. if G_db != nil {
  201. G_db.Close()
  202. }
  203. }