Warning.go 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. db "Cold_Api/initialize"
  7. "Cold_Api/models/Account"
  8. "Cold_Api/models/Device"
  9. "Cold_Api/models/System"
  10. "Cold_Api/models/Warning"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/beego/beego/v2/core/logs"
  14. "github.com/robfig/cron/v3"
  15. "github.com/xuri/excelize/v2"
  16. "math"
  17. "net/http"
  18. "os"
  19. "strconv"
  20. "time"
  21. )
  22. // 设备告警 ------------------------------------------
  23. // 告警列表
  24. func (c *DeviceController) DeviceWarning_List() {
  25. type R_JSONS struct {
  26. //必须的大写开头
  27. Data []Warning.Warning_R
  28. Num int64
  29. Page int
  30. Page_size int
  31. }
  32. var r_jsons R_JSONS
  33. page, _ := c.GetInt("page")
  34. if page < 1 {
  35. page = 1
  36. }
  37. page_z, _ := c.GetInt("page_z")
  38. if page_z < 1 {
  39. page_z = conf.Page_size
  40. }
  41. T_name := c.GetString("T_name")
  42. T_tp := c.GetString("T_tp")
  43. Time_start := c.GetString("Time_start")
  44. Time_end := c.GetString("Time_end")
  45. T_handle, _ := c.GetInt("T_handle")
  46. T_admin, _ := c.GetInt("T_admin")
  47. T_history, _ := c.GetInt("T_history")
  48. tpCount := Warning.Read_WarningType_Count()
  49. var tpList []string
  50. if len(T_tp) > 0 {
  51. tpList = lib.SplitStringIds(T_tp, "T")
  52. if len(tpList) == int(tpCount) {
  53. // 类型全选,替换为空,不进行查询筛选(查询全部)
  54. tpList = []string{}
  55. }
  56. } else {
  57. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  58. if power.T_warning != "*" {
  59. tpList = lib.SplitStringIds(power.T_warning, "W")
  60. }
  61. }
  62. countRedisKey := fmt.Sprintf("Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, T_admin, T_history)
  63. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  64. if err != nil {
  65. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  66. c.ServeJSON()
  67. return
  68. }
  69. var T_year, T_month string
  70. if T_history == 1 {
  71. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  72. if err != nil {
  73. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  74. c.ServeJSON()
  75. return
  76. }
  77. T_year, T_month = date.Format("2006"), date.Format("01")
  78. }
  79. // 管理员界面
  80. if T_admin == 1 {
  81. if T_history == 1 {
  82. // 获取备份
  83. r_jsons.Data, r_jsons.Num = Warning.Read_Admin_Warning_Backups(*c.Admin_r, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, countRedisKey)
  84. } else {
  85. // 获取最新
  86. r_jsons.Data, r_jsons.Num = Warning.Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, countRedisKey)
  87. }
  88. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  89. c.ServeJSON()
  90. return
  91. }
  92. // 公司报警界面
  93. if T_history == 1 {
  94. r_jsons.Data, r_jsons.Num = Warning.Read_Warning_Backups(c.T_pid, bindSN, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, countRedisKey)
  95. } else {
  96. // 获取最新
  97. r_jsons.Data, r_jsons.Num = Warning.Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, countRedisKey)
  98. }
  99. r_jsons.Page = page
  100. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  101. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  102. c.ServeJSON()
  103. return
  104. }
  105. func (c *DeviceController) DeviceWarning_List2() {
  106. type R_JSONS struct {
  107. //必须的大写开头
  108. Data []Warning.Warning_R
  109. Num int64
  110. Page int
  111. Page_size int
  112. }
  113. var r_jsons R_JSONS
  114. page, _ := c.GetInt("page")
  115. if page < 1 {
  116. page = 1
  117. }
  118. page_z, _ := c.GetInt("page_z")
  119. if page_z < 1 {
  120. page_z = conf.Page_size
  121. }
  122. T_name := c.GetString("T_name")
  123. T_tp := c.GetString("T_tp")
  124. Time_start := c.GetString("Time_start")
  125. Time_end := c.GetString("Time_end")
  126. T_handle, _ := c.GetInt("T_handle")
  127. T_admin, _ := c.GetInt("T_admin")
  128. T_history, _ := c.GetInt("T_history")
  129. T_abandon, _ := c.GetInt("T_abandon") // 1 弃用设备
  130. if len(Time_start) == 0 {
  131. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"}
  132. c.ServeJSON()
  133. return
  134. }
  135. if len(Time_end) == 0 {
  136. c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"}
  137. c.ServeJSON()
  138. return
  139. }
  140. tpCount := Warning.Read_WarningType_Count()
  141. var tpList []string
  142. if len(T_tp) > 0 {
  143. tpList = lib.SplitStringIds(T_tp, "T")
  144. if len(tpList) == int(tpCount) {
  145. // 类型全选,替换为空,不进行查询筛选(查询全部)
  146. tpList = []string{}
  147. }
  148. } else {
  149. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  150. if power.T_warning != "*" {
  151. tpList = lib.SplitStringIds(power.T_warning, "W")
  152. }
  153. }
  154. countRedisKey := fmt.Sprintf("Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, T_admin, T_history, T_abandon)
  155. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  156. if err != nil {
  157. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  158. c.ServeJSON()
  159. return
  160. }
  161. // 查询弃用设备
  162. abandonDeviceList, _ := Device.Read_Device_List_ByT_State(bindSN, c.T_pid, 0)
  163. abandonSN := make([]string, 0)
  164. for _, r := range abandonDeviceList {
  165. abandonSN = append(abandonSN, r.T_sn)
  166. }
  167. // 管理员界面
  168. if T_admin == 1 {
  169. // 获取最新 获取绑定的sn 如果未绑定则获取全部
  170. r_jsons.Data, r_jsons.Num = Warning.Gorm_Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN, page, page_z, countRedisKey)
  171. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  172. c.ServeJSON()
  173. return
  174. }
  175. r_jsons.Data, r_jsons.Num = Warning.Gorm_Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN, page, page_z, countRedisKey)
  176. r_jsons.Page = page
  177. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  178. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  179. c.ServeJSON()
  180. return
  181. }
  182. func (c *DeviceController) DeviceWarning_List_Sse() {
  183. page, _ := c.GetInt("page")
  184. if page < 1 {
  185. page = 1
  186. }
  187. page_z, _ := c.GetInt("page_z")
  188. if page_z < 1 {
  189. page_z = conf.Page_size
  190. }
  191. T_name := c.GetString("T_name")
  192. T_tp := c.GetString("T_tp")
  193. Time_start := c.GetString("Time_start")
  194. Time_end := c.GetString("Time_end")
  195. T_handle, _ := c.GetInt("T_handle")
  196. T_admin, _ := c.GetInt("T_admin")
  197. T_abandon, _ := c.GetInt("T_abandon") // 1 弃用设备
  198. tpCount := Warning.Read_WarningType_Count()
  199. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  200. if err != nil {
  201. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  202. c.ServeJSON()
  203. return
  204. }
  205. // 查询弃用设备
  206. abandonDeviceList, _ := Device.Read_Device_List_ByT_State(bindSN, c.T_pid, 0)
  207. abandonSN := make([]string, 0)
  208. for _, r := range abandonDeviceList {
  209. abandonSN = append(abandonSN, r.T_sn)
  210. }
  211. var tpList []string
  212. if len(T_tp) > 0 {
  213. tpList = lib.SplitStringIds(T_tp, "T")
  214. if len(tpList) == int(tpCount) {
  215. // 类型全选,替换为空,不进行查询筛选(查询全部)
  216. tpList = []string{}
  217. }
  218. } else {
  219. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  220. if power.T_warning != "*" {
  221. tpList = lib.SplitStringIds(power.T_warning, "W")
  222. }
  223. }
  224. countRedisKey := fmt.Sprintf("Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, T_admin, 0, T_abandon)
  225. tableNames := Warning.GenerateWarningTableNames(Time_start, Time_end)
  226. // 设置响应头
  227. c.Ctx.ResponseWriter.Header().Set("Content-Type", "text/event-stream")
  228. c.Ctx.ResponseWriter.Header().Set("Cache-Control", "no-cache")
  229. c.Ctx.ResponseWriter.Header().Set("Connection", "keep-alive")
  230. c.Ctx.ResponseWriter.WriteHeader(http.StatusOK)
  231. var cnt int64
  232. tableCount := make(map[string]int64)
  233. // 管理员界面
  234. if T_admin == 1 {
  235. gormReadWarningListScopes := Warning.GormReadAdminWarningListScopes(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN)
  236. cnt, tableCount, err = Warning.Redis_WarningCount_GetOne(c.Admin_r.Id, countRedisKey)
  237. offset := (page - 1) * page_z
  238. remaining := offset
  239. var warningsCount int
  240. for _, tableName := range tableNames {
  241. tableDB := db.DB.Table(tableName)
  242. var count int64
  243. if tableName == fmt.Sprintf("warning_%d_%02d", time.Now().Year(), time.Now().Month()) {
  244. if saveTime, ok := tableCount["SaveTime"]; ok {
  245. t1 := time.Unix(saveTime, 0)
  246. if time.Now().Sub(t1) < 5*time.Minute {
  247. if count, ok = tableCount[tableName]; !ok {
  248. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  249. tableCount[tableName] = count
  250. }
  251. } else {
  252. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  253. tableCount[tableName] = count
  254. }
  255. }
  256. } else {
  257. var ok bool
  258. if count, ok = tableCount[tableName]; !ok {
  259. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  260. tableCount[tableName] = count
  261. }
  262. }
  263. cnt += count
  264. if remaining >= int(count) {
  265. remaining -= int(count)
  266. continue
  267. }
  268. var tableWarning []Warning.Warning
  269. if warningsCount < page_z {
  270. tableDB.Scopes(gormReadWarningListScopes).
  271. Order("t__ut desc").Offset(remaining).Limit(page_z - warningsCount).Find(&tableWarning)
  272. }
  273. warningsCount += len(tableWarning)
  274. remaining = 0
  275. var r_jsons lib.R_JSONS
  276. r_jsons.Num = cnt
  277. r_jsons.Data = tableWarning
  278. json_b, _ := json.Marshal(r_jsons)
  279. c.Ctx.ResponseWriter.Write(json_b)
  280. c.Ctx.ResponseWriter.Flush()
  281. // 可以根据需要调整间隔时间
  282. time.Sleep(1 * time.Second)
  283. }
  284. Warning.Redis_WarningCount_Set(c.Admin_r.Id, countRedisKey, cnt, tableCount)
  285. return
  286. }
  287. //从redis获取报警统计数量
  288. gormReadWarningListScopes := Warning.GormReadWarningListScopes(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN)
  289. cnt, tableCount, err = Warning.Redis_WarningCount_GetOne(c.T_pid, countRedisKey)
  290. offset := (page - 1) * page_z
  291. remaining := offset
  292. var warningsCount int
  293. for _, tableName := range tableNames {
  294. tableDB := db.DB.Table(tableName)
  295. var count int64
  296. if tableName == fmt.Sprintf("warning_%d_%02d", time.Now().Year(), time.Now().Month()) {
  297. if saveTime, ok := tableCount["SaveTime"]; ok {
  298. t1 := time.Unix(saveTime, 0)
  299. if time.Now().Sub(t1) < 5*time.Minute {
  300. if count, ok = tableCount[tableName]; !ok {
  301. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  302. tableCount[tableName] = count
  303. }
  304. } else {
  305. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  306. tableCount[tableName] = count
  307. }
  308. }
  309. } else {
  310. var ok bool
  311. if count, ok = tableCount[tableName]; !ok {
  312. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  313. tableCount[tableName] = count
  314. }
  315. }
  316. cnt += count
  317. if remaining >= int(count) {
  318. remaining -= int(count)
  319. continue
  320. }
  321. var tableWarning []Warning.Warning
  322. if warningsCount < page_z {
  323. tableDB.Scopes(gormReadWarningListScopes).
  324. Order("t__ut desc").Offset(remaining).Limit(page_z - warningsCount).Find(&tableWarning)
  325. }
  326. warningsCount += len(tableWarning)
  327. remaining = 0
  328. var r_jsons lib.R_JSONS
  329. r_jsons.Num = cnt
  330. r_jsons.Data = tableWarning
  331. json_b, _ := json.Marshal(r_jsons)
  332. c.Ctx.ResponseWriter.Write(json_b)
  333. c.Ctx.ResponseWriter.Flush()
  334. // 可以根据需要调整间隔时间
  335. time.Sleep(1 * time.Second)
  336. }
  337. Warning.Redis_WarningCount_Set(c.T_pid, countRedisKey, cnt, tableCount)
  338. }
  339. func (c *DeviceController) CompanyWarning_List() {
  340. var r_jsons lib.R_JSONS
  341. page, _ := c.GetInt("page")
  342. if page < 1 {
  343. page = 1
  344. }
  345. page_z, _ := c.GetInt("page_z")
  346. if page_z < 1 {
  347. page_z = conf.Page_size
  348. }
  349. T_name := c.GetString("T_name")
  350. T_tp := c.GetString("T_tp")
  351. Time_start := c.GetString("Time_start")
  352. Time_end := c.GetString("Time_end")
  353. T_handle, _ := c.GetInt("T_handle")
  354. T_history, _ := c.GetInt("T_history")
  355. tpCount := Warning.Read_WarningType_Count()
  356. var tpList []string
  357. if len(T_tp) > 0 {
  358. tpList = lib.SplitStringIds(T_tp, "T")
  359. if len(tpList) == int(tpCount) {
  360. // 类型全选,替换为空,不进行查询筛选(查询全部)
  361. tpList = []string{}
  362. }
  363. } else {
  364. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  365. if power.T_warning != "*" {
  366. tpList = lib.SplitStringIds(power.T_warning, "W")
  367. }
  368. }
  369. countRedisKey := fmt.Sprintf("Company_Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0, T_history)
  370. Company_r, err := Account.Read_Company_ById(c.T_pid)
  371. if err != nil {
  372. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  373. c.ServeJSON()
  374. return
  375. }
  376. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  377. var T_year, T_month string
  378. if T_history == 1 {
  379. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  380. if err != nil {
  381. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  382. c.ServeJSON()
  383. return
  384. }
  385. T_year, T_month = date.Format("2006"), date.Format("01")
  386. }
  387. Account.Read_Company_All_Maps()
  388. if T_history == 1 {
  389. r_jsons.Data, r_jsons.Num = Warning.Read_Company_Warning_Backups(T_pids, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, c.T_pid, countRedisKey)
  390. } else {
  391. // 获取最新
  392. r_jsons.Data, r_jsons.Num = Warning.Read_Company_Warning_List(T_pids, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, c.T_pid, countRedisKey)
  393. }
  394. r_jsons.Page = page
  395. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  396. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  397. c.ServeJSON()
  398. return
  399. }
  400. func (c *DeviceController) CompanyWarning_List2() {
  401. var r_jsons lib.R_JSONS
  402. page, _ := c.GetInt("page")
  403. if page < 1 {
  404. page = 1
  405. }
  406. page_z, _ := c.GetInt("page_z")
  407. if page_z < 1 {
  408. page_z = conf.Page_size
  409. }
  410. T_name := c.GetString("T_name")
  411. T_tp := c.GetString("T_tp")
  412. Time_start := c.GetString("Time_start")
  413. Time_end := c.GetString("Time_end")
  414. T_handle, _ := c.GetInt("T_handle")
  415. if len(Time_start) == 0 {
  416. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"}
  417. c.ServeJSON()
  418. return
  419. }
  420. if len(Time_end) == 0 {
  421. c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"}
  422. c.ServeJSON()
  423. return
  424. }
  425. tpCount := Warning.Read_WarningType_Count()
  426. var tpList []string
  427. if len(T_tp) > 0 {
  428. tpList = lib.SplitStringIds(T_tp, "T")
  429. if len(tpList) == int(tpCount) {
  430. // 类型全选,替换为空,不进行查询筛选(查询全部)
  431. tpList = []string{}
  432. }
  433. } else {
  434. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  435. if power.T_warning != "*" {
  436. tpList = lib.SplitStringIds(power.T_warning, "W")
  437. }
  438. }
  439. countRedisKey := fmt.Sprintf("Company_Warning_Count_%d_%s_%s_%s_%s_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0)
  440. Company_r, err := Account.Read_Company_ById(c.T_pid)
  441. if err != nil {
  442. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  443. c.ServeJSON()
  444. return
  445. }
  446. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  447. Account.Read_Company_All_Maps()
  448. // 获取最新
  449. r_jsons.Data, r_jsons.Num = Warning.Gorm_Read_Company_Warning_List(T_pids, tpList, T_name, T_handle, Time_start, Time_end, page, page_z, c.T_pid, countRedisKey)
  450. r_jsons.Page = page
  451. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  452. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  453. c.ServeJSON()
  454. return
  455. }
  456. // 查询告警 暂时没有用到
  457. func (c *DeviceController) DeviceWarning_Get() {
  458. id, _ := c.GetInt("T_id")
  459. T_time := c.GetString("T_time")
  460. if len(T_time) == 0 {
  461. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  462. c.ServeJSON()
  463. return
  464. }
  465. var T_year, T_month string
  466. date, err := time.Parse("2006-01-02 15:04:05", T_time)
  467. if err != nil {
  468. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  469. c.ServeJSON()
  470. return
  471. }
  472. T_year, T_month = date.Format("2006"), date.Format("01")
  473. Wtab := "warning"
  474. Wtab += "_" + T_year + "_" + T_month
  475. T, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  476. if err != nil {
  477. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  478. c.ServeJSON()
  479. return
  480. }
  481. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.WarningToWarning_R(0, T)}
  482. c.ServeJSON()
  483. return
  484. }
  485. // 编辑告警(处理告警)
  486. func (c *DeviceController) DeviceWarning_Post() {
  487. id, _ := c.GetInt("T_id")
  488. T_Text := c.GetString("T_Text")
  489. T_time := c.GetString("T_time")
  490. T_history, _ := c.GetInt("T_history") // TODO 后期废弃
  491. if len(T_time) == 0 && conf.UseNewWarningQuery {
  492. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  493. c.ServeJSON()
  494. return
  495. }
  496. var T_year, T_month string
  497. var warning Warning.Warning
  498. Wtab := "warning"
  499. if T_history == 1 {
  500. date, err := time.Parse("2006-01-02 15:04:05", T_time)
  501. if err != nil {
  502. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  503. c.ServeJSON()
  504. return
  505. }
  506. T_year, T_month = date.Format("2006"), date.Format("01")
  507. Wtab += "_" + T_year + "_" + T_month
  508. warning, err = Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  509. if err != nil && err.Error() != "<QuerySeter> no row found" {
  510. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  511. c.ServeJSON()
  512. return
  513. }
  514. warning.T_Text = T_Text
  515. warning.T_State = 2
  516. warning.UpdateTime = time.Now()
  517. if is := Warning.Update_Warning_Backups(warning, T_year, T_month); !is {
  518. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  519. c.ServeJSON()
  520. return
  521. }
  522. if !conf.UseNewWarningQuery {
  523. warning = Warning.Read_Warning_ById(int64(id))
  524. if warning.Id > 0 {
  525. warning.T_Text = T_Text
  526. warning.T_State = 2
  527. warning.UpdateTime = time.Now()
  528. if is := Warning.Update_Warning(warning, "T_Text", "T_State", "UpdateTime"); !is {
  529. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  530. c.ServeJSON()
  531. return
  532. }
  533. }
  534. }
  535. } else {
  536. // 使用新的查询方式后 warning表不做更新
  537. if !conf.UseNewWarningQuery {
  538. warning = Warning.Read_Warning_ById(int64(id))
  539. if warning.Id == 0 {
  540. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  541. c.ServeJSON()
  542. return
  543. }
  544. warning.T_Text = T_Text
  545. warning.T_State = 2
  546. warning.UpdateTime = time.Now()
  547. if is := Warning.Update_Warning(warning, "T_Text", "T_State", "UpdateTime"); !is {
  548. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  549. c.ServeJSON()
  550. return
  551. }
  552. }
  553. // 同步处理历史数据
  554. date := warning.T_Ut
  555. T_year, T_month = date.Format("2006"), date.Format("01")
  556. Wtab += "_" + T_year + "_" + T_month
  557. warningBackups, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  558. if err != nil && err.Error() != "<QuerySeter> no row found" {
  559. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  560. c.ServeJSON()
  561. return
  562. }
  563. if warningBackups.Id > 0 {
  564. warningBackups.T_Text = T_Text
  565. warningBackups.T_State = 2
  566. warningBackups.UpdateTime = time.Now()
  567. if is := Warning.Update_Warning_Backups(warningBackups, T_year, T_month); !is {
  568. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  569. c.ServeJSON()
  570. return
  571. }
  572. }
  573. }
  574. // 删除报警列表统计的缓存
  575. Warning.Redis_WarningCount_DelK(c.T_pid)
  576. Warning.Redis_WarningCount_DelK(c.Admin_r.Id)
  577. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警处理操作", Wtab+":"+strconv.Itoa(id)+"->"+T_Text)
  578. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  579. c.ServeJSON()
  580. return
  581. }
  582. // 删除告警
  583. func (c *DeviceController) DeviceWarning_Del() {
  584. id, _ := c.GetInt("T_id")
  585. T_time := c.GetString("T_time")
  586. T_history, _ := c.GetInt("T_history") // TODO 后期废弃
  587. if len(T_time) == 0 && conf.UseNewWarningQuery {
  588. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  589. c.ServeJSON()
  590. return
  591. }
  592. var T_year, T_month string
  593. var warning Warning.Warning
  594. Wtab := "warning"
  595. if T_history == 1 {
  596. date, err := time.Parse("2006-01-02 15:04:05", T_time)
  597. if err != nil {
  598. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  599. c.ServeJSON()
  600. return
  601. }
  602. T_year, T_month = date.Format("2006"), date.Format("01")
  603. Wtab += "_" + T_year + "_" + T_month
  604. warning, err = Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  605. if err != nil {
  606. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  607. c.ServeJSON()
  608. return
  609. }
  610. warning.T_State = 0
  611. warning.UpdateTime = time.Now()
  612. if is := Warning.Update_Warning_Backups(warning, T_year, T_month); !is {
  613. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  614. c.ServeJSON()
  615. return
  616. }
  617. warning = Warning.Read_Warning_ById(int64(id))
  618. if warning.Id > 0 {
  619. warning.T_State = 0
  620. if is := Warning.Update_Warning(warning, "T_State"); !is {
  621. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  622. c.ServeJSON()
  623. return
  624. }
  625. }
  626. } else {
  627. warning = Warning.Read_Warning_ById(int64(id))
  628. if warning.Id > 0 {
  629. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  630. c.ServeJSON()
  631. return
  632. }
  633. warning.T_State = 0
  634. if is := Warning.Update_Warning(warning, "T_State"); !is {
  635. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  636. c.ServeJSON()
  637. return
  638. }
  639. date := warning.T_Ut
  640. T_year, T_month = date.Format("2006"), date.Format("01")
  641. Wtab += "_" + T_year + "_" + T_month
  642. warningBackups, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  643. if err != nil && err.Error() != "<QuerySeter> no row found" {
  644. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  645. c.ServeJSON()
  646. return
  647. }
  648. if warningBackups.Id > 0 {
  649. warningBackups.T_State = 0
  650. warningBackups.UpdateTime = time.Now()
  651. if is := Warning.Update_Warning_Backups(warningBackups, T_year, T_month); !is {
  652. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  653. c.ServeJSON()
  654. return
  655. }
  656. }
  657. }
  658. // 删除报警列表统计的缓存
  659. Warning.Redis_WarningCount_DelK(c.T_pid)
  660. Warning.Redis_WarningCount_DelK(c.Admin_r.Id)
  661. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警删除操作", Wtab+":"+strconv.Itoa(id))
  662. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  663. c.ServeJSON()
  664. return
  665. }
  666. // 导出告警
  667. func (c *DeviceController) DeviceWarning_Data_Excel() {
  668. T_name := c.GetString("T_name")
  669. T_tp := c.GetString("T_tp")
  670. Time_start := c.GetString("Time_start")
  671. Time_end := c.GetString("Time_end")
  672. T_handle, _ := c.GetInt("T_handle")
  673. T_admin, _ := c.GetInt("T_admin")
  674. T_history, _ := c.GetInt("T_history")
  675. tpCount := Warning.Read_WarningType_Count()
  676. var tpList []string
  677. if len(T_tp) > 0 {
  678. tpList = lib.SplitStringIds(T_tp, "T")
  679. if len(tpList) == int(tpCount) {
  680. // 类型全选,替换为空,不进行查询筛选(查询全部)
  681. tpList = []string{}
  682. }
  683. } else {
  684. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  685. if power.T_warning != "*" {
  686. tpList = lib.SplitStringIds(power.T_warning, "W")
  687. }
  688. }
  689. countRedisKey := fmt.Sprintf("Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0, T_history)
  690. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  691. if err != nil {
  692. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  693. c.ServeJSON()
  694. return
  695. }
  696. var T_year, T_month string
  697. if T_history == 1 {
  698. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  699. if err != nil {
  700. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  701. c.ServeJSON()
  702. return
  703. }
  704. T_year, T_month = date.Format("2006"), date.Format("01")
  705. }
  706. var Device_data []Warning.Warning_R
  707. // 管理员界面
  708. if T_admin == 1 {
  709. if T_history == 1 {
  710. // 获取备份
  711. Device_data, _ = Warning.Read_Admin_Warning_Backups(*c.Admin_r, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  712. } else {
  713. // 获取最新
  714. Device_data, _ = Warning.Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  715. }
  716. }
  717. if T_history == 1 {
  718. // 获取备份
  719. Device_data, _ = Warning.Read_Warning_Backups(c.T_pid, bindSN, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  720. } else {
  721. // 获取最新
  722. Device_data, _ = Warning.Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  723. }
  724. f := excelize.NewFile() // 设置单元格的值
  725. // 这里设置表头ÒÒ
  726. f.SetCellValue("Sheet1", "A1", "报警类型")
  727. f.SetCellValue("Sheet1", "B1", "Sn")
  728. f.SetCellValue("Sheet1", "C1", "设备名称")
  729. f.SetCellValue("Sheet1", "D1", "传感器")
  730. f.SetCellValue("Sheet1", "E1", "报警内容")
  731. f.SetCellValue("Sheet1", "F1", "记录时间")
  732. f.SetCellValue("Sheet1", "G1", "处理")
  733. f.SetCellValue("Sheet1", "H1", "处理时间")
  734. // 设置列宽
  735. f.SetColWidth("Sheet1", "A", "A", 20)
  736. f.SetColWidth("Sheet1", "B", "B", 25)
  737. f.SetColWidth("Sheet1", "C", "C", 30)
  738. f.SetColWidth("Sheet1", "D", "D", 30)
  739. f.SetColWidth("Sheet1", "G", "E", 30)
  740. f.SetColWidth("Sheet1", "H", "F", 15)
  741. f.SetColWidth("Sheet1", "I", "G", 30)
  742. f.SetColWidth("Sheet1", "J", "H", 15)
  743. line := 1
  744. // 循环写入数据
  745. for _, v := range Device_data {
  746. line++
  747. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  748. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  749. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_D_name)
  750. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_DS_name)
  751. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_Remark)
  752. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Ut)
  753. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Text)
  754. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.UpdateTime)
  755. }
  756. timeStr := time.Now().Format("20060102150405")
  757. // 保存文件
  758. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  759. fmt.Println(err)
  760. }
  761. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  762. if !is {
  763. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  764. c.ServeJSON()
  765. return
  766. }
  767. // 删除文件
  768. err = os.Remove("ofile/" + timeStr + ".xlsx")
  769. if err != nil {
  770. fmt.Println(err)
  771. }
  772. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  773. c.ServeJSON()
  774. return
  775. }
  776. func (c *DeviceController) DeviceWarning_Data_Excel2() {
  777. T_name := c.GetString("T_name")
  778. T_tp := c.GetString("T_tp")
  779. Time_start := c.GetString("Time_start")
  780. Time_end := c.GetString("Time_end")
  781. T_handle, _ := c.GetInt("T_handle")
  782. if len(Time_start) == 0 {
  783. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"}
  784. c.ServeJSON()
  785. return
  786. }
  787. if len(Time_end) == 0 {
  788. c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"}
  789. c.ServeJSON()
  790. return
  791. }
  792. T_admin, _ := c.GetInt("T_admin")
  793. T_abandon, _ := c.GetInt("T_abandon") // 1 弃用设备
  794. tpCount := Warning.Read_WarningType_Count()
  795. var tpList []string
  796. if len(T_tp) > 0 {
  797. tpList = lib.SplitStringIds(T_tp, "T")
  798. if len(tpList) == int(tpCount) {
  799. // 类型全选,替换为空,不进行查询筛选(查询全部)
  800. tpList = []string{}
  801. }
  802. } else {
  803. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  804. if power.T_warning != "*" {
  805. tpList = lib.SplitStringIds(power.T_warning, "W")
  806. }
  807. }
  808. countRedisKey := fmt.Sprintf("Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0, T_abandon)
  809. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  810. if err != nil {
  811. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  812. c.ServeJSON()
  813. return
  814. }
  815. // 查询弃用设备
  816. abandonDeviceList, _ := Device.Read_Device_List_ByT_State(bindSN, c.T_pid, 0)
  817. abandonSN := make([]string, 0)
  818. for _, r := range abandonDeviceList {
  819. abandonSN = append(abandonSN, r.T_sn)
  820. }
  821. var Device_data []Warning.Warning_R
  822. // 管理员界面
  823. if T_admin == 1 {
  824. // 获取最新
  825. //Device_data, _ = Warning.Gorm_Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  826. Device_data, _ = Warning.Gorm_Read_Admin_Warning_List_Excel(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN, 0, 9999, countRedisKey)
  827. } else {
  828. // 获取最新
  829. //Device_data, _ = Warning.Gorm_Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  830. Device_data, _ = Warning.Gorm_Read_Warning_List_Excel(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, T_abandon, abandonSN, 0, 9999, countRedisKey)
  831. }
  832. f := excelize.NewFile() // 设置单元格的值
  833. // 这里设置表头ÒÒ
  834. f.SetCellValue("Sheet1", "A1", "报警类型")
  835. f.SetCellValue("Sheet1", "B1", "Sn")
  836. f.SetCellValue("Sheet1", "C1", "设备名称")
  837. f.SetCellValue("Sheet1", "D1", "传感器")
  838. f.SetCellValue("Sheet1", "E1", "报警内容")
  839. f.SetCellValue("Sheet1", "F1", "记录时间")
  840. f.SetCellValue("Sheet1", "G1", "处理")
  841. f.SetCellValue("Sheet1", "H1", "处理时间")
  842. // 设置列宽
  843. f.SetColWidth("Sheet1", "A", "A", 20)
  844. f.SetColWidth("Sheet1", "B", "B", 25)
  845. f.SetColWidth("Sheet1", "C", "C", 30)
  846. f.SetColWidth("Sheet1", "D", "D", 30)
  847. f.SetColWidth("Sheet1", "G", "E", 30)
  848. f.SetColWidth("Sheet1", "H", "F", 15)
  849. f.SetColWidth("Sheet1", "I", "G", 30)
  850. f.SetColWidth("Sheet1", "J", "H", 15)
  851. line := 1
  852. // 循环写入数据
  853. for _, v := range Device_data {
  854. line++
  855. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  856. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  857. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_D_name)
  858. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_DS_name)
  859. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_Remark)
  860. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Ut)
  861. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Text)
  862. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.UpdateTime)
  863. }
  864. timeStr := time.Now().Format("20060102150405")
  865. // 保存文件
  866. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  867. fmt.Println(err)
  868. }
  869. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  870. if !is {
  871. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  872. c.ServeJSON()
  873. return
  874. }
  875. // 删除文件
  876. err = os.Remove("ofile/" + timeStr + ".xlsx")
  877. if err != nil {
  878. fmt.Println(err)
  879. }
  880. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  881. c.ServeJSON()
  882. return
  883. }
  884. // 公司管理 - 导出告警
  885. func (c *DeviceController) CompanyWarning_Data_Excel() {
  886. T_name := c.GetString("T_name")
  887. T_tp := c.GetString("T_tp")
  888. Time_start := c.GetString("Time_start")
  889. Time_end := c.GetString("Time_end")
  890. T_handle, _ := c.GetInt("T_handle")
  891. T_history, _ := c.GetInt("T_history")
  892. tpCount := Warning.Read_WarningType_Count()
  893. var tpList []string
  894. if len(T_tp) > 0 {
  895. tpList = lib.SplitStringIds(T_tp, "T")
  896. if len(tpList) == int(tpCount) {
  897. // 类型全选,替换为空,不进行查询筛选(查询全部)
  898. tpList = []string{}
  899. }
  900. } else {
  901. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  902. if power.T_warning != "*" {
  903. tpList = lib.SplitStringIds(power.T_warning, "W")
  904. }
  905. }
  906. countRedisKey := fmt.Sprintf("Company_Warning_Count_%d_%s_%s_%s_%s_%d_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0, T_history)
  907. Account.Read_Company_All_Maps()
  908. Company_r, err := Account.Read_Company_ById(c.T_pid)
  909. if err != nil {
  910. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  911. c.ServeJSON()
  912. return
  913. }
  914. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  915. var T_year, T_month string
  916. if T_history == 1 {
  917. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  918. if err != nil {
  919. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  920. c.ServeJSON()
  921. return
  922. }
  923. T_year, T_month = date.Format("2006"), date.Format("01")
  924. }
  925. var Device_data []Warning.CompanyWarning_R
  926. if T_history == 1 {
  927. // 获取备份
  928. Device_data, _ = Warning.Read_Company_Warning_Backups(T_pids, T_year, T_month, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, c.T_pid, countRedisKey)
  929. } else {
  930. // 获取最新
  931. Device_data, _ = Warning.Read_Company_Warning_List(T_pids, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, c.T_pid, countRedisKey)
  932. }
  933. f := excelize.NewFile() // 设置单元格的值
  934. // 这里设置表头
  935. f.SetCellValue("Sheet1", "A1", "报警类型")
  936. f.SetCellValue("Sheet1", "A1", "公司名称")
  937. f.SetCellValue("Sheet1", "B1", "Sn")
  938. f.SetCellValue("Sheet1", "C1", "设备名称")
  939. f.SetCellValue("Sheet1", "D1", "传感器")
  940. f.SetCellValue("Sheet1", "E1", "报警内容")
  941. f.SetCellValue("Sheet1", "F1", "记录时间")
  942. f.SetCellValue("Sheet1", "G1", "处理")
  943. f.SetCellValue("Sheet1", "H1", "处理时间")
  944. // 设置列宽
  945. f.SetColWidth("Sheet1", "A", "A", 20)
  946. f.SetColWidth("Sheet1", "B", "B", 40)
  947. f.SetColWidth("Sheet1", "C", "C", 25)
  948. f.SetColWidth("Sheet1", "D", "D", 30)
  949. f.SetColWidth("Sheet1", "E", "E", 30)
  950. f.SetColWidth("Sheet1", "F", "F", 30)
  951. f.SetColWidth("Sheet1", "J", "J", 15)
  952. f.SetColWidth("Sheet1", "H", "H", 30)
  953. f.SetColWidth("Sheet1", "I", "I", 15)
  954. line := 1
  955. // 循环写入数据
  956. for _, v := range Device_data {
  957. line++
  958. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  959. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s", v.T_pid_name))
  960. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  961. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_D_name)
  962. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_DS_name)
  963. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Remark)
  964. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.T_Ut)
  965. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_Text)
  966. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.UpdateTime)
  967. }
  968. timeStr := time.Now().Format("20060102150405")
  969. // 保存文件
  970. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  971. fmt.Println(err)
  972. }
  973. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  974. if !is {
  975. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  976. c.ServeJSON()
  977. return
  978. }
  979. // 删除文件
  980. err = os.Remove("ofile/" + timeStr + ".xlsx")
  981. if err != nil {
  982. fmt.Println(err)
  983. }
  984. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  985. c.ServeJSON()
  986. return
  987. }
  988. func (c *DeviceController) CompanyWarning_Data_Excel2() {
  989. T_name := c.GetString("T_name")
  990. T_tp := c.GetString("T_tp")
  991. Time_start := c.GetString("Time_start")
  992. Time_end := c.GetString("Time_end")
  993. T_handle, _ := c.GetInt("T_handle")
  994. if len(Time_start) == 0 {
  995. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"}
  996. c.ServeJSON()
  997. return
  998. }
  999. if len(Time_end) == 0 {
  1000. c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"}
  1001. c.ServeJSON()
  1002. return
  1003. }
  1004. tpCount := Warning.Read_WarningType_Count()
  1005. var tpList []string
  1006. if len(T_tp) > 0 {
  1007. tpList = lib.SplitStringIds(T_tp, "T")
  1008. if len(tpList) == int(tpCount) {
  1009. // 类型全选,替换为空,不进行查询筛选(查询全部)
  1010. tpList = []string{}
  1011. }
  1012. } else {
  1013. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  1014. if power.T_warning != "*" {
  1015. tpList = lib.SplitStringIds(power.T_warning, "W")
  1016. }
  1017. }
  1018. countRedisKey := fmt.Sprintf("Company_Warning_Count_%d_%s_%s_%s_%s_%d_%d", c.T_pid, T_name, T_tp, Time_start, Time_end, T_handle, 0)
  1019. Account.Read_Company_All_Maps()
  1020. Company_r, err := Account.Read_Company_ById(c.T_pid)
  1021. if err != nil {
  1022. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1023. c.ServeJSON()
  1024. return
  1025. }
  1026. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1027. var Device_data []Warning.CompanyWarning_R
  1028. // 获取最新
  1029. Device_data, _ = Warning.Gorm_Read_Company_Warning_List_Excel(T_pids, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, c.T_pid, countRedisKey)
  1030. f := excelize.NewFile() // 设置单元格的值
  1031. // 这里设置表头
  1032. f.SetCellValue("Sheet1", "A1", "报警类型")
  1033. f.SetCellValue("Sheet1", "B1", "公司名称")
  1034. f.SetCellValue("Sheet1", "C1", "Sn")
  1035. f.SetCellValue("Sheet1", "D1", "设备名称")
  1036. f.SetCellValue("Sheet1", "E1", "传感器")
  1037. f.SetCellValue("Sheet1", "F1", "报警内容")
  1038. f.SetCellValue("Sheet1", "G1", "记录时间")
  1039. f.SetCellValue("Sheet1", "H1", "处理")
  1040. f.SetCellValue("Sheet1", "I1", "处理时间")
  1041. // 设置列宽
  1042. f.SetColWidth("Sheet1", "A", "A", 20)
  1043. f.SetColWidth("Sheet1", "B", "B", 40)
  1044. f.SetColWidth("Sheet1", "C", "C", 25)
  1045. f.SetColWidth("Sheet1", "D", "D", 30)
  1046. f.SetColWidth("Sheet1", "E", "E", 30)
  1047. f.SetColWidth("Sheet1", "F", "F", 30)
  1048. f.SetColWidth("Sheet1", "J", "J", 15)
  1049. f.SetColWidth("Sheet1", "H", "H", 30)
  1050. f.SetColWidth("Sheet1", "I", "I", 15)
  1051. line := 1
  1052. // 循环写入数据
  1053. for _, v := range Device_data {
  1054. line++
  1055. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  1056. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s", v.T_pid_name))
  1057. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  1058. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_D_name)
  1059. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_DS_name)
  1060. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Remark)
  1061. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Ut)
  1062. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_Text)
  1063. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.UpdateTime)
  1064. }
  1065. timeStr := time.Now().Format("20060102150405")
  1066. // 保存文件
  1067. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1068. fmt.Println(err)
  1069. }
  1070. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1071. if !is {
  1072. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1073. c.ServeJSON()
  1074. return
  1075. }
  1076. // 删除文件
  1077. err = os.Remove("ofile/" + timeStr + ".xlsx")
  1078. if err != nil {
  1079. fmt.Println(err)
  1080. }
  1081. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1082. c.ServeJSON()
  1083. return
  1084. }
  1085. // 告警提示列表
  1086. func (c *DeviceController) DeviceWarningList_T_Tips() {
  1087. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All_T_Notice_mechanism()}
  1088. c.ServeJSON()
  1089. return
  1090. }
  1091. // 告警类型列表
  1092. func (c *DeviceController) WarningType_List_All() {
  1093. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All()}
  1094. c.ServeJSON()
  1095. return
  1096. }
  1097. // 告警类型列表 - 权限关联列表
  1098. func (c *DeviceController) WarningType_Power_List_All() {
  1099. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1100. if err != nil {
  1101. c.Data["json"] = lib.JSONS{Code: 200, Msg: "获取菜单失败"}
  1102. c.ServeJSON()
  1103. return
  1104. }
  1105. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_Power_All(power.T_warning)}
  1106. c.ServeJSON()
  1107. return
  1108. }
  1109. // 通过传感器类型获取报警列表
  1110. func (c *DeviceController) Read_Warning_List_By_DS_T_type() {
  1111. var r_jsons lib.R_JSONS
  1112. page, _ := c.GetInt("page")
  1113. if page < 1 {
  1114. page = 1
  1115. }
  1116. page_z, _ := c.GetInt("page_z")
  1117. if page_z < 1 {
  1118. page_z = conf.Page_size
  1119. }
  1120. T_type, _ := c.GetInt("T_type")
  1121. T_name := c.GetString("T_name")
  1122. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  1123. if err != nil {
  1124. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1125. c.ServeJSON()
  1126. return
  1127. }
  1128. var T_pids []int
  1129. if c.Admin_r.T_pid > 0 {
  1130. // 查询公司
  1131. Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
  1132. if err != nil {
  1133. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1134. c.ServeJSON()
  1135. return
  1136. }
  1137. // 查询公司下面所有子公司id
  1138. T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
  1139. }
  1140. var tpList []string
  1141. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  1142. if power.T_warning != "*" {
  1143. tpList = lib.SplitStringIds(power.T_warning, "W")
  1144. }
  1145. countRedisKey := fmt.Sprintf("Company_Warning_Count_By_DS_T_type_%d_%s_%d_%d", c.T_pid, T_name, T_type, c.Admin_r.T_pid)
  1146. r_jsons.Data, r_jsons.Num = Warning.Read_Warning_List_By_DS_T_type(c.Admin_r, c.T_pid, bindSN, T_pids, T_type, T_name, tpList, page, page_z, countRedisKey)
  1147. r_jsons.Page = page
  1148. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1149. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1150. c.ServeJSON()
  1151. return
  1152. }
  1153. func (c *DeviceController) Read_WarningSend_List() {
  1154. var r_jsons lib.R_JSONS
  1155. page, _ := c.GetInt("page")
  1156. if page < 1 {
  1157. page = 1
  1158. }
  1159. page_z, _ := c.GetInt("page_z")
  1160. if page_z < 1 {
  1161. page_z = conf.Page_size
  1162. }
  1163. T_ntype, _ := c.GetInt("T_ntype")
  1164. r_jsons.Data, r_jsons.Num = Warning.Read_WarningSand_List(c.T_pid, T_ntype, page, page_z)
  1165. r_jsons.Page = page
  1166. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1167. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1168. c.ServeJSON()
  1169. return
  1170. }
  1171. func (c *DeviceController) Read_WarningRate_List() {
  1172. page, _ := c.GetInt("page")
  1173. if page < 1 {
  1174. page = 1
  1175. }
  1176. page_z, _ := c.GetInt("page_z")
  1177. if page_z < 1 {
  1178. page_z = conf.Page_size
  1179. }
  1180. T_uuid := c.GetString("T_uuid")
  1181. T_year, _ := c.GetInt("T_year")
  1182. T_month, _ := c.GetInt("T_month")
  1183. var startTime, endTime string
  1184. now := time.Now()
  1185. var month time.Month
  1186. // 默认本月
  1187. if T_year == 0 {
  1188. T_year = now.Year()
  1189. }
  1190. if T_month == 0 {
  1191. month = now.Month()
  1192. } else {
  1193. month = time.Month(T_month)
  1194. }
  1195. T_type := Warning.WarningRateDay
  1196. if T_month == 0 {
  1197. T_type = Warning.WarningRateMonth
  1198. }
  1199. if T_type == Warning.WarningRateDay {
  1200. startTime = time.Date(T_year, month, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  1201. endTime = time.Date(T_year, month+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  1202. }
  1203. // 默认本年
  1204. if T_type == Warning.WarningRateMonth {
  1205. startTime = time.Date(T_year, 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  1206. endTime = time.Date(T_year, 12, 31, 0, 0, 0, 0, time.Local).Format("2006-01")
  1207. }
  1208. if len(T_uuid) == 0 {
  1209. T_uuid = c.Admin_r.T_uuid
  1210. }
  1211. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningRate_List(T_uuid, T_type, startTime, endTime)}
  1212. c.ServeJSON()
  1213. return
  1214. }
  1215. func (c *DeviceController) Read_WarningHandle_List() {
  1216. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningHandle_List()}
  1217. c.ServeJSON()
  1218. return
  1219. }
  1220. func Cron_WarningRate() {
  1221. // 创建一个定时任务对象
  1222. c := cron.New(cron.WithSeconds())
  1223. // 给对象增加定时任务
  1224. // @daily 每日运行一次
  1225. // c.AddFunc("0 */1 * * * ?", Cron_WarningRateDay_Add)
  1226. // c.AddFunc("0 */1 * * * ?", Cron_WarningRateMonth_Add)
  1227. c.AddFunc("@daily", Cron_WarningRateDay_Add)
  1228. c.AddFunc("@monthly", Cron_WarningRateMonth_Add)
  1229. // 启动定时任务
  1230. c.Start()
  1231. defer c.Stop()
  1232. // 查询语句,阻塞,让main函数不退出,保持程序运行
  1233. select {}
  1234. }
  1235. // 保存每天的设备报警率
  1236. func Cron_WarningRateDay_Add() {
  1237. T_date := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  1238. logs.Info("开始统计" + T_date + "设备报警率")
  1239. // 获取管理员用户列表
  1240. adminList := Account.Read_Admin_List_All()
  1241. // 获取内部用户关联的pid
  1242. for _, admin := range adminList {
  1243. // 内部用户已绑定公司,* 绑定所有公司
  1244. if len(admin.T_pids) == 0 || admin.T_pids == "*" {
  1245. continue
  1246. }
  1247. // 获取排除后的pid
  1248. T_pids := lib.SplitStringToIntIds(admin.T_pids, "P")
  1249. if len(T_pids) == 0 {
  1250. continue
  1251. }
  1252. pids := Account.ReadCompanyWarningIds_T_pids(T_pids)
  1253. if len(pids) == 0 {
  1254. continue
  1255. }
  1256. // 获取探头数量
  1257. dsCnt := Device.Read_DeviceSensorCount_ByT_pids(pids)
  1258. // 获取当天报警数量
  1259. warningCount := Warning.Read_WarningCount_byDay(pids)
  1260. T_rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(warningCount)/float64(dsCnt)), 64) // 保留2位小数
  1261. // 保存设备报警率
  1262. warningRate := Warning.WarningRate{
  1263. T_uuid: admin.T_uuid,
  1264. T_date: T_date,
  1265. T_type: Warning.WarningRateDay,
  1266. T_device_num: dsCnt,
  1267. T_warning_num: warningCount,
  1268. T_rate: float32(T_rate),
  1269. }
  1270. Warning.Add_WarningRate(warningRate)
  1271. }
  1272. }
  1273. // 保存每月的设备报警率
  1274. func Cron_WarningRateMonth_Add() {
  1275. now := time.Now()
  1276. // fixme 测试
  1277. //now := time.Now().AddDate(0, 1, 0)
  1278. T_date := now.AddDate(0, -1, 0).Format("2006-01")
  1279. firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)
  1280. startTime := firstOfMonth.AddDate(0, -1, 0).Format("2006-01-02") // 上月第一天
  1281. endTime := firstOfMonth.AddDate(0, 0, -1).Format("2006-01-02") // 上月最后一天
  1282. logs.Info("开始统计" + T_date + "设备报警率")
  1283. logs.Info("开始时间" + startTime)
  1284. logs.Info("结束时间" + endTime)
  1285. rateAvgList := Warning.Read_WarningRateAvg_Month(startTime, endTime)
  1286. // 获取内部用户关联的pid
  1287. for _, rate := range rateAvgList {
  1288. T_rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(rate.T_warning_num)/float64(rate.T_device_num)), 64) // 保留2位小数
  1289. // 保存设备报警率
  1290. warningRate := Warning.WarningRate{
  1291. T_uuid: rate.T_uuid,
  1292. T_date: T_date,
  1293. T_type: Warning.WarningRateMonth,
  1294. T_device_num: rate.T_device_num,
  1295. T_warning_num: rate.T_warning_num,
  1296. T_rate: float32(T_rate),
  1297. }
  1298. Warning.Add_WarningRate(warningRate)
  1299. }
  1300. }