Warning.go 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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. warning = Warning.Read_Warning_ById(int64(id))
  523. if warning.Id > 0 {
  524. warning.T_Text = T_Text
  525. warning.T_State = 2
  526. warning.UpdateTime = time.Now()
  527. if is := Warning.Update_Warning(warning, "T_Text", "T_State", "UpdateTime"); !is {
  528. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  529. c.ServeJSON()
  530. return
  531. }
  532. }
  533. } else {
  534. // 使用新的查询方式后 warning表不做更新
  535. warning = Warning.Read_Warning_ById(int64(id))
  536. if warning.Id > 0 {
  537. warning.T_Text = T_Text
  538. warning.T_State = 2
  539. warning.UpdateTime = time.Now()
  540. if is := Warning.Update_Warning(warning, "T_Text", "T_State", "UpdateTime"); !is {
  541. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  542. c.ServeJSON()
  543. return
  544. }
  545. }
  546. // 同步处理历史数据
  547. date, err := time.Parse("2006-01-02 15:04:05", T_time)
  548. T_year, T_month = date.Format("2006"), date.Format("01")
  549. Wtab += "_" + T_year + "_" + T_month
  550. warningBackups, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  551. if err != nil && err.Error() != "<QuerySeter> no row found" {
  552. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  553. c.ServeJSON()
  554. return
  555. }
  556. if warningBackups.Id > 0 {
  557. warningBackups.T_Text = T_Text
  558. warningBackups.T_State = 2
  559. warningBackups.UpdateTime = time.Now()
  560. if is := Warning.Update_Warning_Backups(warningBackups, T_year, T_month); !is {
  561. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  562. c.ServeJSON()
  563. return
  564. }
  565. }
  566. }
  567. // 删除报警列表统计的缓存
  568. Warning.Redis_WarningCount_DelK(c.T_pid)
  569. Warning.Redis_WarningCount_DelK(c.Admin_r.Id)
  570. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警处理操作", Wtab+":"+strconv.Itoa(id)+"->"+T_Text)
  571. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  572. c.ServeJSON()
  573. return
  574. }
  575. // 删除告警
  576. func (c *DeviceController) DeviceWarning_Del() {
  577. id, _ := c.GetInt("T_id")
  578. T_time := c.GetString("T_time")
  579. T_history, _ := c.GetInt("T_history") // TODO 后期废弃
  580. if len(T_time) == 0 && conf.UseNewWarningQuery {
  581. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  582. c.ServeJSON()
  583. return
  584. }
  585. var T_year, T_month string
  586. var warning Warning.Warning
  587. Wtab := "warning"
  588. if T_history == 1 {
  589. date, err := time.Parse("2006-01-02 15:04:05", T_time)
  590. if err != nil {
  591. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  592. c.ServeJSON()
  593. return
  594. }
  595. T_year, T_month = date.Format("2006"), date.Format("01")
  596. Wtab += "_" + T_year + "_" + T_month
  597. warning, err = Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  598. if err != nil {
  599. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  600. c.ServeJSON()
  601. return
  602. }
  603. warning.T_State = 0
  604. warning.UpdateTime = time.Now()
  605. if is := Warning.Update_Warning_Backups(warning, T_year, T_month); !is {
  606. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  607. c.ServeJSON()
  608. return
  609. }
  610. warning = Warning.Read_Warning_ById(int64(id))
  611. if warning.Id > 0 {
  612. warning.T_State = 0
  613. if is := Warning.Update_Warning(warning, "T_State"); !is {
  614. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  615. c.ServeJSON()
  616. return
  617. }
  618. }
  619. } else {
  620. warning = Warning.Read_Warning_ById(int64(id))
  621. if warning.Id > 0 {
  622. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  623. c.ServeJSON()
  624. return
  625. }
  626. warning.T_State = 0
  627. if is := Warning.Update_Warning(warning, "T_State"); !is {
  628. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  629. c.ServeJSON()
  630. return
  631. }
  632. date := warning.T_Ut
  633. T_year, T_month = date.Format("2006"), date.Format("01")
  634. Wtab += "_" + T_year + "_" + T_month
  635. warningBackups, err := Warning.Read_Warning_ById_Backups(id, T_year, T_month)
  636. if err != nil && err.Error() != "<QuerySeter> no row found" {
  637. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  638. c.ServeJSON()
  639. return
  640. }
  641. if warningBackups.Id > 0 {
  642. warningBackups.T_State = 0
  643. warningBackups.UpdateTime = time.Now()
  644. if is := Warning.Update_Warning_Backups(warningBackups, T_year, T_month); !is {
  645. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  646. c.ServeJSON()
  647. return
  648. }
  649. }
  650. }
  651. // 删除报警列表统计的缓存
  652. Warning.Redis_WarningCount_DelK(c.T_pid)
  653. Warning.Redis_WarningCount_DelK(c.Admin_r.Id)
  654. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警删除操作", Wtab+":"+strconv.Itoa(id))
  655. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  656. c.ServeJSON()
  657. return
  658. }
  659. // 导出告警
  660. func (c *DeviceController) DeviceWarning_Data_Excel() {
  661. T_name := c.GetString("T_name")
  662. T_tp := c.GetString("T_tp")
  663. Time_start := c.GetString("Time_start")
  664. Time_end := c.GetString("Time_end")
  665. T_handle, _ := c.GetInt("T_handle")
  666. T_admin, _ := c.GetInt("T_admin")
  667. T_history, _ := c.GetInt("T_history")
  668. tpCount := Warning.Read_WarningType_Count()
  669. var tpList []string
  670. if len(T_tp) > 0 {
  671. tpList = lib.SplitStringIds(T_tp, "T")
  672. if len(tpList) == int(tpCount) {
  673. // 类型全选,替换为空,不进行查询筛选(查询全部)
  674. tpList = []string{}
  675. }
  676. } else {
  677. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  678. if power.T_warning != "*" {
  679. tpList = lib.SplitStringIds(power.T_warning, "W")
  680. }
  681. }
  682. 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)
  683. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  684. if err != nil {
  685. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  686. c.ServeJSON()
  687. return
  688. }
  689. var T_year, T_month string
  690. if T_history == 1 {
  691. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  692. if err != nil {
  693. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  694. c.ServeJSON()
  695. return
  696. }
  697. T_year, T_month = date.Format("2006"), date.Format("01")
  698. }
  699. var Device_data []Warning.Warning_R
  700. // 管理员界面
  701. if T_admin == 1 {
  702. if T_history == 1 {
  703. // 获取备份
  704. 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)
  705. } else {
  706. // 获取最新
  707. Device_data, _ = Warning.Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  708. }
  709. }
  710. if T_history == 1 {
  711. // 获取备份
  712. 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)
  713. } else {
  714. // 获取最新
  715. Device_data, _ = Warning.Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  716. }
  717. f := excelize.NewFile() // 设置单元格的值
  718. // 这里设置表头ÒÒ
  719. f.SetCellValue("Sheet1", "A1", "报警类型")
  720. f.SetCellValue("Sheet1", "B1", "Sn")
  721. f.SetCellValue("Sheet1", "C1", "设备名称")
  722. f.SetCellValue("Sheet1", "D1", "传感器")
  723. f.SetCellValue("Sheet1", "E1", "报警内容")
  724. f.SetCellValue("Sheet1", "F1", "记录时间")
  725. f.SetCellValue("Sheet1", "G1", "处理")
  726. f.SetCellValue("Sheet1", "H1", "处理时间")
  727. // 设置列宽
  728. f.SetColWidth("Sheet1", "A", "A", 20)
  729. f.SetColWidth("Sheet1", "B", "B", 25)
  730. f.SetColWidth("Sheet1", "C", "C", 30)
  731. f.SetColWidth("Sheet1", "D", "D", 30)
  732. f.SetColWidth("Sheet1", "G", "E", 30)
  733. f.SetColWidth("Sheet1", "H", "F", 15)
  734. f.SetColWidth("Sheet1", "I", "G", 30)
  735. f.SetColWidth("Sheet1", "J", "H", 15)
  736. line := 1
  737. // 循环写入数据
  738. for _, v := range Device_data {
  739. line++
  740. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  741. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  742. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_D_name)
  743. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_DS_name)
  744. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_Remark)
  745. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Ut)
  746. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Text)
  747. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.UpdateTime)
  748. }
  749. timeStr := time.Now().Format("20060102150405")
  750. // 保存文件
  751. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  752. fmt.Println(err)
  753. }
  754. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  755. if !is {
  756. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  757. c.ServeJSON()
  758. return
  759. }
  760. // 删除文件
  761. err = os.Remove("ofile/" + timeStr + ".xlsx")
  762. if err != nil {
  763. fmt.Println(err)
  764. }
  765. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  766. c.ServeJSON()
  767. return
  768. }
  769. func (c *DeviceController) DeviceWarning_Data_Excel2() {
  770. T_name := c.GetString("T_name")
  771. T_tp := c.GetString("T_tp")
  772. Time_start := c.GetString("Time_start")
  773. Time_end := c.GetString("Time_end")
  774. T_handle, _ := c.GetInt("T_handle")
  775. if len(Time_start) == 0 {
  776. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"}
  777. c.ServeJSON()
  778. return
  779. }
  780. if len(Time_end) == 0 {
  781. c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"}
  782. c.ServeJSON()
  783. return
  784. }
  785. T_admin, _ := c.GetInt("T_admin")
  786. T_abandon, _ := c.GetInt("T_abandon") // 1 弃用设备
  787. tpCount := Warning.Read_WarningType_Count()
  788. var tpList []string
  789. if len(T_tp) > 0 {
  790. tpList = lib.SplitStringIds(T_tp, "T")
  791. if len(tpList) == int(tpCount) {
  792. // 类型全选,替换为空,不进行查询筛选(查询全部)
  793. tpList = []string{}
  794. }
  795. } else {
  796. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  797. if power.T_warning != "*" {
  798. tpList = lib.SplitStringIds(power.T_warning, "W")
  799. }
  800. }
  801. 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)
  802. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  803. if err != nil {
  804. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  805. c.ServeJSON()
  806. return
  807. }
  808. // 查询弃用设备
  809. abandonDeviceList, _ := Device.Read_Device_List_ByT_State(bindSN, c.T_pid, 0)
  810. abandonSN := make([]string, 0)
  811. for _, r := range abandonDeviceList {
  812. abandonSN = append(abandonSN, r.T_sn)
  813. }
  814. var Device_data []Warning.Warning_R
  815. // 管理员界面
  816. if T_admin == 1 {
  817. // 获取最新
  818. //Device_data, _ = Warning.Gorm_Read_Admin_Warning_List(*c.Admin_r, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  819. 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)
  820. } else {
  821. // 获取最新
  822. //Device_data, _ = Warning.Gorm_Read_Warning_List(c.T_pid, bindSN, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, countRedisKey)
  823. 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)
  824. }
  825. f := excelize.NewFile() // 设置单元格的值
  826. // 这里设置表头ÒÒ
  827. f.SetCellValue("Sheet1", "A1", "报警类型")
  828. f.SetCellValue("Sheet1", "B1", "Sn")
  829. f.SetCellValue("Sheet1", "C1", "设备名称")
  830. f.SetCellValue("Sheet1", "D1", "传感器")
  831. f.SetCellValue("Sheet1", "E1", "报警内容")
  832. f.SetCellValue("Sheet1", "F1", "记录时间")
  833. f.SetCellValue("Sheet1", "G1", "处理")
  834. f.SetCellValue("Sheet1", "H1", "处理时间")
  835. // 设置列宽
  836. f.SetColWidth("Sheet1", "A", "A", 20)
  837. f.SetColWidth("Sheet1", "B", "B", 25)
  838. f.SetColWidth("Sheet1", "C", "C", 30)
  839. f.SetColWidth("Sheet1", "D", "D", 30)
  840. f.SetColWidth("Sheet1", "G", "E", 30)
  841. f.SetColWidth("Sheet1", "H", "F", 15)
  842. f.SetColWidth("Sheet1", "I", "G", 30)
  843. f.SetColWidth("Sheet1", "J", "H", 15)
  844. line := 1
  845. // 循环写入数据
  846. for _, v := range Device_data {
  847. line++
  848. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  849. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  850. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_D_name)
  851. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_DS_name)
  852. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_Remark)
  853. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Ut)
  854. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Text)
  855. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.UpdateTime)
  856. }
  857. timeStr := time.Now().Format("20060102150405")
  858. // 保存文件
  859. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  860. fmt.Println(err)
  861. }
  862. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  863. if !is {
  864. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  865. c.ServeJSON()
  866. return
  867. }
  868. // 删除文件
  869. err = os.Remove("ofile/" + timeStr + ".xlsx")
  870. if err != nil {
  871. fmt.Println(err)
  872. }
  873. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  874. c.ServeJSON()
  875. return
  876. }
  877. // 公司管理 - 导出告警
  878. func (c *DeviceController) CompanyWarning_Data_Excel() {
  879. T_name := c.GetString("T_name")
  880. T_tp := c.GetString("T_tp")
  881. Time_start := c.GetString("Time_start")
  882. Time_end := c.GetString("Time_end")
  883. T_handle, _ := c.GetInt("T_handle")
  884. T_history, _ := c.GetInt("T_history")
  885. tpCount := Warning.Read_WarningType_Count()
  886. var tpList []string
  887. if len(T_tp) > 0 {
  888. tpList = lib.SplitStringIds(T_tp, "T")
  889. if len(tpList) == int(tpCount) {
  890. // 类型全选,替换为空,不进行查询筛选(查询全部)
  891. tpList = []string{}
  892. }
  893. } else {
  894. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  895. if power.T_warning != "*" {
  896. tpList = lib.SplitStringIds(power.T_warning, "W")
  897. }
  898. }
  899. 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)
  900. Account.Read_Company_All_Maps()
  901. Company_r, err := Account.Read_Company_ById(c.T_pid)
  902. if err != nil {
  903. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  904. c.ServeJSON()
  905. return
  906. }
  907. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  908. var T_year, T_month string
  909. if T_history == 1 {
  910. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  911. if err != nil {
  912. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误"}
  913. c.ServeJSON()
  914. return
  915. }
  916. T_year, T_month = date.Format("2006"), date.Format("01")
  917. }
  918. var Device_data []Warning.CompanyWarning_R
  919. if T_history == 1 {
  920. // 获取备份
  921. 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)
  922. } else {
  923. // 获取最新
  924. Device_data, _ = Warning.Read_Company_Warning_List(T_pids, tpList, T_name, T_handle, Time_start, Time_end, 0, 9999, c.T_pid, countRedisKey)
  925. }
  926. f := excelize.NewFile() // 设置单元格的值
  927. // 这里设置表头
  928. f.SetCellValue("Sheet1", "A1", "报警类型")
  929. f.SetCellValue("Sheet1", "A1", "公司名称")
  930. f.SetCellValue("Sheet1", "B1", "Sn")
  931. f.SetCellValue("Sheet1", "C1", "设备名称")
  932. f.SetCellValue("Sheet1", "D1", "传感器")
  933. f.SetCellValue("Sheet1", "E1", "报警内容")
  934. f.SetCellValue("Sheet1", "F1", "记录时间")
  935. f.SetCellValue("Sheet1", "G1", "处理")
  936. f.SetCellValue("Sheet1", "H1", "处理时间")
  937. // 设置列宽
  938. f.SetColWidth("Sheet1", "A", "A", 20)
  939. f.SetColWidth("Sheet1", "B", "B", 40)
  940. f.SetColWidth("Sheet1", "C", "C", 25)
  941. f.SetColWidth("Sheet1", "D", "D", 30)
  942. f.SetColWidth("Sheet1", "E", "E", 30)
  943. f.SetColWidth("Sheet1", "F", "F", 30)
  944. f.SetColWidth("Sheet1", "J", "J", 15)
  945. f.SetColWidth("Sheet1", "H", "H", 30)
  946. f.SetColWidth("Sheet1", "I", "I", 15)
  947. line := 1
  948. // 循环写入数据
  949. for _, v := range Device_data {
  950. line++
  951. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  952. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s", v.T_pid_name))
  953. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  954. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_D_name)
  955. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_DS_name)
  956. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Remark)
  957. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.T_Ut)
  958. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_Text)
  959. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.UpdateTime)
  960. }
  961. timeStr := time.Now().Format("20060102150405")
  962. // 保存文件
  963. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  964. fmt.Println(err)
  965. }
  966. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  967. if !is {
  968. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  969. c.ServeJSON()
  970. return
  971. }
  972. // 删除文件
  973. err = os.Remove("ofile/" + timeStr + ".xlsx")
  974. if err != nil {
  975. fmt.Println(err)
  976. }
  977. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  978. c.ServeJSON()
  979. return
  980. }
  981. func (c *DeviceController) CompanyWarning_Data_Excel2() {
  982. T_name := c.GetString("T_name")
  983. T_tp := c.GetString("T_tp")
  984. Time_start := c.GetString("Time_start")
  985. Time_end := c.GetString("Time_end")
  986. T_handle, _ := c.GetInt("T_handle")
  987. if len(Time_start) == 0 {
  988. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能为空!"}
  989. c.ServeJSON()
  990. return
  991. }
  992. if len(Time_end) == 0 {
  993. c.Data["json"] = lib.JSONS{Code: 202, Msg: "结束时间不能为空!"}
  994. c.ServeJSON()
  995. return
  996. }
  997. tpCount := Warning.Read_WarningType_Count()
  998. var tpList []string
  999. if len(T_tp) > 0 {
  1000. tpList = lib.SplitStringIds(T_tp, "T")
  1001. if len(tpList) == int(tpCount) {
  1002. // 类型全选,替换为空,不进行查询筛选(查询全部)
  1003. tpList = []string{}
  1004. }
  1005. } else {
  1006. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  1007. if power.T_warning != "*" {
  1008. tpList = lib.SplitStringIds(power.T_warning, "W")
  1009. }
  1010. }
  1011. 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)
  1012. Account.Read_Company_All_Maps()
  1013. Company_r, err := Account.Read_Company_ById(c.T_pid)
  1014. if err != nil {
  1015. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1016. c.ServeJSON()
  1017. return
  1018. }
  1019. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1020. var Device_data []Warning.CompanyWarning_R
  1021. // 获取最新
  1022. 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)
  1023. f := excelize.NewFile() // 设置单元格的值
  1024. // 这里设置表头
  1025. f.SetCellValue("Sheet1", "A1", "报警类型")
  1026. f.SetCellValue("Sheet1", "B1", "公司名称")
  1027. f.SetCellValue("Sheet1", "C1", "Sn")
  1028. f.SetCellValue("Sheet1", "D1", "设备名称")
  1029. f.SetCellValue("Sheet1", "E1", "传感器")
  1030. f.SetCellValue("Sheet1", "F1", "报警内容")
  1031. f.SetCellValue("Sheet1", "G1", "记录时间")
  1032. f.SetCellValue("Sheet1", "H1", "处理")
  1033. f.SetCellValue("Sheet1", "I1", "处理时间")
  1034. // 设置列宽
  1035. f.SetColWidth("Sheet1", "A", "A", 20)
  1036. f.SetColWidth("Sheet1", "B", "B", 40)
  1037. f.SetColWidth("Sheet1", "C", "C", 25)
  1038. f.SetColWidth("Sheet1", "D", "D", 30)
  1039. f.SetColWidth("Sheet1", "E", "E", 30)
  1040. f.SetColWidth("Sheet1", "F", "F", 30)
  1041. f.SetColWidth("Sheet1", "J", "J", 15)
  1042. f.SetColWidth("Sheet1", "H", "H", 30)
  1043. f.SetColWidth("Sheet1", "I", "I", 15)
  1044. line := 1
  1045. // 循环写入数据
  1046. for _, v := range Device_data {
  1047. line++
  1048. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), Warning.Read_WarningType_Get(v.T_tp))
  1049. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("%s", v.T_pid_name))
  1050. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), fmt.Sprintf("%s[%d]", v.T_sn, v.T_id))
  1051. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_D_name)
  1052. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_DS_name)
  1053. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Remark)
  1054. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Ut)
  1055. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_Text)
  1056. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.UpdateTime)
  1057. }
  1058. timeStr := time.Now().Format("20060102150405")
  1059. // 保存文件
  1060. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1061. fmt.Println(err)
  1062. }
  1063. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1064. if !is {
  1065. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1066. c.ServeJSON()
  1067. return
  1068. }
  1069. // 删除文件
  1070. err = os.Remove("ofile/" + timeStr + ".xlsx")
  1071. if err != nil {
  1072. fmt.Println(err)
  1073. }
  1074. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1075. c.ServeJSON()
  1076. return
  1077. }
  1078. // 告警提示列表
  1079. func (c *DeviceController) DeviceWarningList_T_Tips() {
  1080. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All_T_Notice_mechanism()}
  1081. c.ServeJSON()
  1082. return
  1083. }
  1084. // 告警类型列表
  1085. func (c *DeviceController) WarningType_List_All() {
  1086. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All()}
  1087. c.ServeJSON()
  1088. return
  1089. }
  1090. // 告警类型列表 - 权限关联列表
  1091. func (c *DeviceController) WarningType_Power_List_All() {
  1092. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1093. if err != nil {
  1094. c.Data["json"] = lib.JSONS{Code: 200, Msg: "获取菜单失败"}
  1095. c.ServeJSON()
  1096. return
  1097. }
  1098. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_Power_All(power.T_warning)}
  1099. c.ServeJSON()
  1100. return
  1101. }
  1102. // 通过传感器类型获取报警列表
  1103. func (c *DeviceController) Read_Warning_List_By_DS_T_type() {
  1104. var r_jsons lib.R_JSONS
  1105. page, _ := c.GetInt("page")
  1106. if page < 1 {
  1107. page = 1
  1108. }
  1109. page_z, _ := c.GetInt("page_z")
  1110. if page_z < 1 {
  1111. page_z = conf.Page_size
  1112. }
  1113. T_type, _ := c.GetInt("T_type")
  1114. T_name := c.GetString("T_name")
  1115. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  1116. if err != nil {
  1117. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1118. c.ServeJSON()
  1119. return
  1120. }
  1121. var T_pids []int
  1122. if c.Admin_r.T_pid > 0 {
  1123. // 查询公司
  1124. Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
  1125. if err != nil {
  1126. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1127. c.ServeJSON()
  1128. return
  1129. }
  1130. // 查询公司下面所有子公司id
  1131. T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
  1132. }
  1133. var tpList []string
  1134. power, _ := Account.Read_Power_ById(c.Admin_r.T_power)
  1135. if power.T_warning != "*" {
  1136. tpList = lib.SplitStringIds(power.T_warning, "W")
  1137. }
  1138. 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)
  1139. 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)
  1140. r_jsons.Page = page
  1141. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1142. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1143. c.ServeJSON()
  1144. return
  1145. }
  1146. func (c *DeviceController) Read_WarningSend_List() {
  1147. var r_jsons lib.R_JSONS
  1148. page, _ := c.GetInt("page")
  1149. if page < 1 {
  1150. page = 1
  1151. }
  1152. page_z, _ := c.GetInt("page_z")
  1153. if page_z < 1 {
  1154. page_z = conf.Page_size
  1155. }
  1156. T_ntype, _ := c.GetInt("T_ntype")
  1157. r_jsons.Data, r_jsons.Num = Warning.Read_WarningSand_List(c.T_pid, T_ntype, page, page_z)
  1158. r_jsons.Page = page
  1159. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1160. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1161. c.ServeJSON()
  1162. return
  1163. }
  1164. func (c *DeviceController) Read_WarningRate_List() {
  1165. page, _ := c.GetInt("page")
  1166. if page < 1 {
  1167. page = 1
  1168. }
  1169. page_z, _ := c.GetInt("page_z")
  1170. if page_z < 1 {
  1171. page_z = conf.Page_size
  1172. }
  1173. T_uuid := c.GetString("T_uuid")
  1174. T_year, _ := c.GetInt("T_year")
  1175. T_month, _ := c.GetInt("T_month")
  1176. var startTime, endTime string
  1177. now := time.Now()
  1178. var month time.Month
  1179. // 默认本月
  1180. if T_year == 0 {
  1181. T_year = now.Year()
  1182. }
  1183. if T_month == 0 {
  1184. month = now.Month()
  1185. } else {
  1186. month = time.Month(T_month)
  1187. }
  1188. T_type := Warning.WarningRateDay
  1189. if T_month == 0 {
  1190. T_type = Warning.WarningRateMonth
  1191. }
  1192. if T_type == Warning.WarningRateDay {
  1193. startTime = time.Date(T_year, month, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  1194. endTime = time.Date(T_year, month+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  1195. }
  1196. // 默认本年
  1197. if T_type == Warning.WarningRateMonth {
  1198. startTime = time.Date(T_year, 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  1199. endTime = time.Date(T_year, 12, 31, 0, 0, 0, 0, time.Local).Format("2006-01")
  1200. }
  1201. if len(T_uuid) == 0 {
  1202. T_uuid = c.Admin_r.T_uuid
  1203. }
  1204. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningRate_List(T_uuid, T_type, startTime, endTime)}
  1205. c.ServeJSON()
  1206. return
  1207. }
  1208. func (c *DeviceController) Read_WarningHandle_List() {
  1209. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningHandle_List()}
  1210. c.ServeJSON()
  1211. return
  1212. }
  1213. func Cron_WarningRate() {
  1214. // 创建一个定时任务对象
  1215. c := cron.New(cron.WithSeconds())
  1216. // 给对象增加定时任务
  1217. // @daily 每日运行一次
  1218. // c.AddFunc("0 */1 * * * ?", Cron_WarningRateDay_Add)
  1219. // c.AddFunc("0 */1 * * * ?", Cron_WarningRateMonth_Add)
  1220. c.AddFunc("@daily", Cron_WarningRateDay_Add)
  1221. c.AddFunc("@monthly", Cron_WarningRateMonth_Add)
  1222. // 启动定时任务
  1223. c.Start()
  1224. defer c.Stop()
  1225. // 查询语句,阻塞,让main函数不退出,保持程序运行
  1226. select {}
  1227. }
  1228. // 保存每天的设备报警率
  1229. func Cron_WarningRateDay_Add() {
  1230. T_date := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  1231. logs.Info("开始统计" + T_date + "设备报警率")
  1232. // 获取管理员用户列表
  1233. adminList := Account.Read_Admin_List_All()
  1234. // 获取内部用户关联的pid
  1235. for _, admin := range adminList {
  1236. // 内部用户已绑定公司,* 绑定所有公司
  1237. if len(admin.T_pids) == 0 || admin.T_pids == "*" {
  1238. continue
  1239. }
  1240. // 获取排除后的pid
  1241. T_pids := lib.SplitStringToIntIds(admin.T_pids, "P")
  1242. if len(T_pids) == 0 {
  1243. continue
  1244. }
  1245. pids := Account.ReadCompanyWarningIds_T_pids(T_pids)
  1246. if len(pids) == 0 {
  1247. continue
  1248. }
  1249. // 获取探头数量
  1250. dsCnt := Device.Read_DeviceSensorCount_ByT_pids(pids)
  1251. // 获取当天报警数量
  1252. warningCount := Warning.Read_WarningCount_byDay(pids)
  1253. T_rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(warningCount)/float64(dsCnt)), 64) // 保留2位小数
  1254. // 保存设备报警率
  1255. warningRate := Warning.WarningRate{
  1256. T_uuid: admin.T_uuid,
  1257. T_date: T_date,
  1258. T_type: Warning.WarningRateDay,
  1259. T_device_num: dsCnt,
  1260. T_warning_num: warningCount,
  1261. T_rate: float32(T_rate),
  1262. }
  1263. Warning.Add_WarningRate(warningRate)
  1264. }
  1265. }
  1266. // 保存每月的设备报警率
  1267. func Cron_WarningRateMonth_Add() {
  1268. now := time.Now()
  1269. // fixme 测试
  1270. //now := time.Now().AddDate(0, 1, 0)
  1271. T_date := now.AddDate(0, -1, 0).Format("2006-01")
  1272. firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)
  1273. startTime := firstOfMonth.AddDate(0, -1, 0).Format("2006-01-02") // 上月第一天
  1274. endTime := firstOfMonth.AddDate(0, 0, -1).Format("2006-01-02") // 上月最后一天
  1275. logs.Info("开始统计" + T_date + "设备报警率")
  1276. logs.Info("开始时间" + startTime)
  1277. logs.Info("结束时间" + endTime)
  1278. rateAvgList := Warning.Read_WarningRateAvg_Month(startTime, endTime)
  1279. // 获取内部用户关联的pid
  1280. for _, rate := range rateAvgList {
  1281. T_rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(rate.T_warning_num)/float64(rate.T_device_num)), 64) // 保留2位小数
  1282. // 保存设备报警率
  1283. warningRate := Warning.WarningRate{
  1284. T_uuid: rate.T_uuid,
  1285. T_date: T_date,
  1286. T_type: Warning.WarningRateMonth,
  1287. T_device_num: rate.T_device_num,
  1288. T_warning_num: rate.T_warning_num,
  1289. T_rate: float32(T_rate),
  1290. }
  1291. Warning.Add_WarningRate(warningRate)
  1292. }
  1293. }