Warning.go 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  1. package Warning
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. db "Cold_Api/initialize"
  6. "Cold_Api/models/Account"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/astaxie/beego/cache"
  10. _ "github.com/astaxie/beego/cache/redis"
  11. "github.com/beego/beego/v2/adapter/orm"
  12. orm2 "github.com/beego/beego/v2/client/orm"
  13. "github.com/beego/beego/v2/core/logs"
  14. _ "github.com/go-sql-driver/mysql"
  15. "github.com/gomodule/redigo/redis"
  16. "gorm.io/gorm"
  17. "sort"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "time"
  22. )
  23. var (
  24. CompanyCallthepolice = "companyCallthepolice" // 公司管理-设备报警
  25. Callthepolice = "callthepolice" // 公司管理-设备报警
  26. )
  27. // 模板
  28. type Warning struct {
  29. Id int64 `orm:"column(ID);size(11);auto;pk" gorm:"column:ID"`
  30. T_pid int `orm:"size(256);null" gorm:"column:t_pid"` // Account.Company 绑定公司
  31. T_tp int `orm:"size(200);null" gorm:"column:t_tp"` // 报警类型 ->WarningList
  32. T_sn string `orm:"index;size(256);null" gorm:"column:t_sn"` // 设备序列号
  33. T_D_name string `orm:"size(256);null" gorm:"column:t__d_name"` // 设备名称
  34. T_id int `orm:"size(200);null" gorm:"column:t_id"` // 传感器 ID
  35. T_DS_name string `orm:"size(256);null" gorm:"column:t__d_s_name"` // 传感器名称
  36. T_Remark string `orm:"type(text);null" gorm:"column:t__remark"` // 采集内容
  37. T_Ut time.Time `orm:"type(timestamp);null;" gorm:"column:t__ut"` // 采集时间
  38. T_fUt time.Time `orm:"type(timestamp);null;" gorm:"column:t_f_ut"` // 首次采集时间
  39. T_Text string `orm:"type(text);null" gorm:"column:t__text"` // 处理备注
  40. T_Log string `orm:"type(text);null" gorm:"column:t__log"` // 通知日志
  41. T_Msid int64 `orm:"size(256);null" gorm:"column:t__msid"` // 消息ID
  42. T_State int `orm:"size(2);default(2)" gorm:"column:t__state"` // 0 删除 1 不处理 2 已处理 3 未处理
  43. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  44. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  45. }
  46. // 多字段索引
  47. func (u *Warning) TableIndex() [][]string {
  48. return [][]string{
  49. []string{"T_pid", "T_tp", "T_State"},
  50. }
  51. }
  52. /*
  53. 分表机制
  54. |--------30天--------|
  55. |-----8月---||--------7月---------||--------6月---------||--------5月---------|
  56. 每天备份 到对应 月份
  57. */
  58. // 模板
  59. type Warning_R struct {
  60. Id int64
  61. T_pid int // Account.Company 绑定公司
  62. T_tp int // 报警类型 ->WarningList
  63. T_tp_name string // 报警类型名称
  64. T_sn string // 设备序列号
  65. T_D_name string // 设备名称
  66. T_id int // 传感器 ID
  67. T_DS_name string // 传感器名称
  68. T_Remark string // 采集内容
  69. T_Ut string // 采集时间
  70. T_fUt string // 首次采集时间
  71. T_Text string // 处理备注
  72. T_Log []string // 处理日志
  73. T_Msid int64 // 消息ID
  74. T_State int // 0 删除 1 未处理 2 已处理
  75. T_history int // 0 40天 1 历史数据
  76. CreateTime string // 创建时间
  77. UpdateTime string // 更新时间
  78. }
  79. type CompanyWarning_R struct {
  80. Id int64
  81. T_pid int // Account.Company 绑定公司
  82. T_pid_name string // Account.Company 公司名称
  83. T_tp int // 报警类型 ->WarningList
  84. T_tp_name string // 报警类型名称
  85. T_sn string // 设备序列号
  86. T_D_name string // 设备名称
  87. T_id int // 传感器 ID
  88. T_DS_name string // 传感器名称
  89. T_Remark string // 采集内容
  90. T_Ut string // 采集时间
  91. T_fUt string // 首次采集时间
  92. T_Text string // 处理备注
  93. T_Log []string // 处理日志
  94. T_Msid int64 // 消息ID
  95. T_State int // 0 删除 1 未处理 2 已处理
  96. T_history int // 0 40天 1 历史数据
  97. CreateTime string // 创建时间
  98. UpdateTime string // 更新时间
  99. }
  100. type Warning_Applet struct {
  101. Id int64
  102. T_pid int // Account.Company 绑定公司
  103. T_company_name string // 公司名称
  104. T_tp int // 报警类型 ->WarningList
  105. T_tp_name string // 报警类型名称
  106. T_sn string // 设备序列号
  107. T_D_name string // 设备名称
  108. T_id int // 传感器 ID
  109. T_DS_name string // 传感器名称
  110. T_Remark string // 采集内容
  111. T_Ut string // 采集时间
  112. T_fUt string // 首次采集时间
  113. T_Text string // 处理备注
  114. T_Log []string // 处理日志
  115. T_Msid int64 // 消息ID
  116. T_State int // 0 删除 1 未处理 2 已处理
  117. CreateTime string // 创建时间
  118. }
  119. type Warning_Count struct {
  120. Key string
  121. Count int64
  122. TableCount map[string]int64
  123. }
  124. func (t *Warning) TableName() string {
  125. return "warning" // 数据库名称 // ************** 替换 FormulaList **************
  126. }
  127. var redis_Warning_Count cache.Cache
  128. func init() {
  129. //注册模型
  130. orm.RegisterModel(new(Warning))
  131. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  132. "redis_Warning_Count", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  133. fmt.Println(config)
  134. var err error
  135. redis_Warning_Count, err = cache.NewCache("redis", config)
  136. if err != nil || redis_Warning_Count == nil {
  137. errMsg := "failed to init redis"
  138. logs.Error(errMsg, err)
  139. panic(errMsg)
  140. }
  141. }
  142. func Redis_WarningCount_Set(id int, key string, count int64, TableCount map[string]int64) (err error) {
  143. //json序列化
  144. idKey := strconv.Itoa(id)
  145. TableCount["SaveTime"] = time.Now().Unix()
  146. warningCount, err := Redis_WarningCount_Get(idKey)
  147. warningCount = append(warningCount, Warning_Count{
  148. Key: key,
  149. Count: count,
  150. TableCount: TableCount,
  151. })
  152. str, err := json.Marshal(warningCount)
  153. if err != nil {
  154. logs.Error(lib.FuncName(), err)
  155. return
  156. }
  157. err = redis_Warning_Count.Put(idKey, str, 30*time.Minute)
  158. if err != nil {
  159. logs.Error("Redis_Admin_Set", "set key:", idKey, ",value:", str, err)
  160. }
  161. return
  162. }
  163. // if r,is :=Redis_Get(T_sn);is{
  164. // return r,nil
  165. // }
  166. func Redis_WarningCount_Get(userId string) (count []Warning_Count, err error) {
  167. if redis_Warning_Count.IsExist(userId) {
  168. //println("找到key:",key)
  169. v := redis_Warning_Count.Get(userId)
  170. err = json.Unmarshal(v.([]byte), &count)
  171. if err != nil {
  172. logs.Error(lib.FuncName(), err)
  173. return count, err
  174. }
  175. return count, nil
  176. }
  177. return count, redis.ErrNil
  178. }
  179. func Redis_WarningCount_GetOne(id int, key string) (count int64, tableCount map[string]int64, err error) {
  180. countList := []Warning_Count{}
  181. tableCount = make(map[string]int64)
  182. idKey := strconv.Itoa(id)
  183. if !redis_Warning_Count.IsExist(idKey) {
  184. return count, tableCount, redis.ErrNil
  185. }
  186. //println("找到key:",key)
  187. v := redis_Warning_Count.Get(idKey)
  188. err = json.Unmarshal(v.([]byte), &countList)
  189. if err != nil {
  190. logs.Error(lib.FuncName(), err)
  191. return count, tableCount, err
  192. }
  193. for _, c := range countList {
  194. if c.Key == key {
  195. return c.Count, c.TableCount, nil
  196. }
  197. }
  198. return count, tableCount, redis.ErrNil
  199. }
  200. func Redis_WarningCount_DelK(id int) (err error) {
  201. idKey := strconv.Itoa(id)
  202. err = redis_Warning_Count.Delete(idKey)
  203. if err != nil {
  204. logs.Error(lib.FuncName(), err)
  205. }
  206. return
  207. }
  208. // ---------------- 特殊方法 -------------------
  209. // T_history 0 40天 1 历史数据
  210. func WarningToWarning_R(T_history int, t Warning) (r Warning_R) {
  211. r.Id = t.Id
  212. r.T_pid = t.T_pid
  213. r.T_tp = t.T_tp
  214. r.T_tp_name = Read_WarningType_Get(t.T_tp)
  215. r.T_sn = t.T_sn
  216. r.T_D_name = t.T_D_name
  217. r.T_id = t.T_id
  218. r.T_DS_name = t.T_DS_name
  219. r.T_Remark = t.T_Remark
  220. r.T_Ut = t.T_Ut.Format("2006-01-02 15:04:05")
  221. if !t.T_fUt.IsZero() {
  222. r.T_fUt = t.T_fUt.Format("2006-01-02 15:04:05")
  223. }
  224. r.T_Text = t.T_Text
  225. if len(t.T_Log) > 0 {
  226. r.T_Log = strings.Split(strings.TrimRight(t.T_Log, "\n"), "\n")
  227. }
  228. r.T_Msid = t.T_Msid
  229. r.T_State = t.T_State
  230. r.T_history = T_history
  231. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  232. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  233. return r
  234. }
  235. func WarningToCompanyWarning_R(T_history int, t Warning) (r CompanyWarning_R) {
  236. r.Id = t.Id
  237. r.T_pid = t.T_pid
  238. r.T_pid_name = Account.Read_Company_Get(t.T_pid)
  239. r.T_tp = t.T_tp
  240. r.T_tp_name = Read_WarningType_Get(t.T_tp)
  241. r.T_sn = t.T_sn
  242. r.T_D_name = t.T_D_name
  243. r.T_id = t.T_id
  244. r.T_DS_name = t.T_DS_name
  245. r.T_Remark = t.T_Remark
  246. r.T_Ut = t.T_Ut.Format("2006-01-02 15:04:05")
  247. if !t.T_fUt.IsZero() {
  248. r.T_fUt = t.T_fUt.Format("2006-01-02 15:04:05")
  249. }
  250. r.T_Text = t.T_Text
  251. if len(t.T_Log) > 0 {
  252. r.T_Log = strings.Split(strings.TrimRight(t.T_Log, "\n"), "\n")
  253. }
  254. r.T_Msid = t.T_Msid
  255. r.T_State = t.T_State
  256. r.T_history = T_history
  257. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  258. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  259. return r
  260. }
  261. func WarningToWarning_Applet(t Warning) (r Warning_Applet) {
  262. r.Id = t.Id
  263. r.T_pid = t.T_pid
  264. company, _ := Account.Read_Company_ById(t.T_pid)
  265. r.T_company_name = company.T_name
  266. r.T_tp = t.T_tp
  267. r.T_tp_name = Read_WarningType_Get(t.T_tp)
  268. r.T_sn = t.T_sn
  269. r.T_D_name = t.T_D_name
  270. r.T_id = t.T_id
  271. r.T_DS_name = t.T_DS_name
  272. r.T_Remark = t.T_Remark
  273. r.T_Ut = t.T_Ut.Format("2006-01-02 15:04:05")
  274. if !t.T_fUt.IsZero() {
  275. r.T_fUt = t.T_fUt.Format("2006-01-02 15:04:05")
  276. }
  277. r.T_Text = t.T_Text
  278. if len(t.T_Log) > 0 {
  279. r.T_Log = strings.Split(strings.TrimRight(t.T_Log, "\n"), "\n")
  280. }
  281. r.T_Msid = t.T_Msid
  282. r.T_State = t.T_State
  283. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  284. return r
  285. }
  286. // 获取 ById
  287. func Read_Warning_ById(id int64) (r Warning) {
  288. o := orm.NewOrm()
  289. r = Warning{Id: id}
  290. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  291. if err != nil {
  292. logs.Error(lib.FuncName(), err)
  293. }
  294. return r
  295. }
  296. // 获取 ById
  297. func Read_Warning_ById_Backups(id int, T_year string, T_month string) (r Warning, err error) {
  298. o := orm.NewOrm()
  299. var maps []Warning
  300. // Warning_2022_07
  301. Wtab := "warning_" + T_year + "_" + T_month
  302. sql_WHERE := fmt.Sprintf(" t__state > 0 AND `ID`= %d", id)
  303. sql := "SELECT ID,t_pid,t_tp,t_sn,t__d_name,t_id,t__d_s_name,t__remark,t__ut,t__text,t__log,t__msid,t__state,create_time,update_time " +
  304. "FROM " + Wtab + " WHERE" + sql_WHERE
  305. logs.Debug(sql)
  306. num, err := o.Raw(sql).QueryRows(&maps)
  307. if err != nil {
  308. logs.Error(lib.FuncName(), err)
  309. return
  310. }
  311. if num == 0 {
  312. return r, orm.ErrNoRows
  313. }
  314. return maps[0], nil
  315. }
  316. // 添加
  317. func Add_Warning(m Warning) (id int64, err error) {
  318. o := orm.NewOrm()
  319. id, err = o.Insert(&m)
  320. if err != nil {
  321. logs.Error(lib.FuncName(), err)
  322. }
  323. return id, err
  324. }
  325. // 修改
  326. func Update_Warning(r Warning, cols ...string) bool {
  327. o := orm.NewOrm()
  328. num, err := o.Update(&r, cols...)
  329. if err != nil {
  330. logs.Error(lib.FuncName(), err)
  331. return false
  332. }
  333. logs.Info("Number of records updated in database:", num)
  334. return true
  335. }
  336. // 修改
  337. func Update_Warning_Backups(r Warning, T_year string, T_month string) bool {
  338. o := orm.NewOrm()
  339. Wtab := "warning_" + T_year + "_" + T_month
  340. num, err := o.Raw("UPDATE "+Wtab+" SET `t__text` = ?,`t__state` = ?, `update_time` = ? WHERE `ID` = ?", r.T_Text, r.T_State, r.UpdateTime, r.Id).Exec()
  341. if err != nil {
  342. logs.Error(lib.FuncName(), err)
  343. return false
  344. }
  345. logs.Info("Number of records updated in database:", num)
  346. return true
  347. }
  348. // 获取列表
  349. func Read_Warning_List(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
  350. countRedisKey string) (r []Warning_R, cnt int64) {
  351. o := orm.NewOrm()
  352. // 也可以直接使用 Model 结构体作为表名
  353. var map_r []Warning
  354. qs := o.QueryTable(new(Warning))
  355. var offset int64
  356. if page <= 1 {
  357. offset = 0
  358. } else {
  359. offset = int64((page - 1) * page_z)
  360. }
  361. cond := orm.NewCondition()
  362. cond1 := orm.NewCondition()
  363. if len(bindSN) > 0 {
  364. cond1 = cond1.And("T_sn__in", bindSN)
  365. }
  366. if T_pid > 0 {
  367. cond1 = cond1.And("T_pid", T_pid)
  368. }
  369. if len(tpList) > 0 {
  370. cond1 = cond1.And("T_tp__in", tpList)
  371. }
  372. if len(T_name) > 0 {
  373. if len(T_name) == 16 {
  374. cond1 = cond1.And("T_sn", T_name)
  375. } else {
  376. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).
  377. Or("T_D_name__icontains", T_name).
  378. Or("T_id__icontains", T_name).
  379. Or("T_DS_name__icontains", T_name))
  380. }
  381. }
  382. if len(Time_start_) > 0 {
  383. cond1 = cond1.And("T_Ut__gte", Time_start_)
  384. }
  385. if len(Time_end_) > 0 {
  386. cond1 = cond1.And("T_Ut__lte", Time_end_)
  387. }
  388. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  389. // T_handle 1:默认 2:全部记录 3 未处理
  390. if T_handle == 1 {
  391. cond1 = cond1.And("T_State__gt", 1)
  392. } else if T_handle == 3 {
  393. cond1 = cond1.And("T_State", 3)
  394. } else {
  395. cond1 = cond1.And("T_State__gt", 0)
  396. }
  397. var err error
  398. // 从redis获取报警统计数量
  399. cnt, _, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
  400. if err != nil {
  401. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  402. if err != nil {
  403. logs.Error(lib.FuncName(), err)
  404. return
  405. }
  406. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
  407. }
  408. if cnt == 0 {
  409. return r, 0
  410. }
  411. if page_z == 9999 {
  412. // 获取全部
  413. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-T_Ut").All(&map_r)
  414. } else {
  415. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_Ut").All(&map_r)
  416. }
  417. if err != nil {
  418. logs.Error(lib.FuncName(), err)
  419. return
  420. }
  421. for _, v := range map_r {
  422. r = append(r, WarningToWarning_R(0, v))
  423. }
  424. return r, cnt
  425. }
  426. // 管理员报警列表
  427. func Read_Admin_Warning_List(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
  428. countRedisKey string) (r []Warning_R, cnt int64) {
  429. if len(admin.T_pids) == 0 {
  430. return r, cnt
  431. }
  432. o := orm.NewOrm()
  433. // 也可以直接使用 Model 结构体作为表名
  434. var map_r []Warning
  435. qs := o.QueryTable(new(Warning))
  436. var offset int64
  437. if page_z == 0 {
  438. page_z = conf.Page_size
  439. }
  440. if page <= 1 {
  441. offset = 0
  442. } else {
  443. offset = int64((page - 1) * page_z)
  444. }
  445. cond := orm.NewCondition()
  446. cond1 := orm.NewCondition()
  447. if admin.T_pids != "*" && len(admin.T_pids) > 0 {
  448. list := lib.SplitStringIds(admin.T_pids, "P")
  449. cond1 = cond1.And("T_pid__in", list)
  450. }
  451. if len(T_tp) > 0 {
  452. cond1 = cond1.And("T_tp__in", T_tp)
  453. }
  454. if len(T_name) > 0 {
  455. if len(T_name) == 16 {
  456. cond1 = cond1.And("T_sn", T_name)
  457. } else {
  458. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).
  459. Or("T_D_name__icontains", T_name).
  460. Or("T_id__icontains", T_name).
  461. Or("T_DS_name__icontains", T_name))
  462. }
  463. }
  464. if len(Time_start_) > 0 {
  465. cond1 = cond1.And("T_Ut__gte", Time_start_)
  466. }
  467. if len(Time_end_) > 0 {
  468. cond1 = cond1.And("T_Ut__lte", Time_end_)
  469. }
  470. //1:默认 2:全部记录 3 未处理
  471. if T_handle == 1 {
  472. cond1 = cond1.And("T_State__gt", 1)
  473. } else if T_handle == 3 {
  474. cond1 = cond1.And("T_State", 3)
  475. } else {
  476. cond1 = cond1.And("T_State__gt", 0)
  477. }
  478. var err error
  479. // 从redis获取报警统计数量
  480. cnt, _, err = Redis_WarningCount_GetOne(admin.Id, countRedisKey)
  481. if err != nil {
  482. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  483. if err != nil {
  484. logs.Error(lib.FuncName(), err)
  485. return
  486. }
  487. Redis_WarningCount_Set(admin.Id, countRedisKey, cnt, map[string]int64{})
  488. }
  489. if cnt == 0 {
  490. return r, 0
  491. }
  492. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_Ut").All(&map_r)
  493. if err != nil {
  494. logs.Error(lib.FuncName(), err)
  495. }
  496. for _, v := range map_r {
  497. r = append(r, WarningToWarning_R(0, v))
  498. }
  499. return r, cnt
  500. }
  501. // 获取列表备份
  502. func Read_Warning_Backups(T_pid int, bindSN []string, T_year string, T_month string, tpList []string, T_name string, T_handle int, Time_start_ string,
  503. Time_end_ string, page int, page_z int, countRedisKey string) (r []Warning_R, cnt int64) {
  504. o := orm.NewOrm()
  505. var maps []Warning
  506. var maps_z []orm2.ParamsList
  507. // 也可以直接使用 Model 结构体作为表名
  508. // Warning_2022_07
  509. Wtab := "warning_" + T_year + "_" + T_month
  510. var offset int
  511. if page <= 1 {
  512. offset = 0
  513. } else {
  514. offset = (page - 1) * page_z
  515. }
  516. sql_WHERE := ""
  517. if len(bindSN) > 0 {
  518. sql_WHERE += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
  519. }
  520. if T_pid > 0 {
  521. sql_WHERE += " AND t_pid = " + strconv.Itoa(T_pid)
  522. }
  523. if len(tpList) > 0 {
  524. tp := lib.StringListToDotStr(tpList)
  525. sql_WHERE += fmt.Sprintf(" AND t_tp in (%s)", tp)
  526. }
  527. if len(T_name) > 0 {
  528. sql_WHERE += " AND (t_sn like '%" + T_name + "%' OR t__d_name like '%" + T_name + "%' OR t_id like '%" + T_name + "%' OR t__d_s_name like '%" + T_name + "%')"
  529. }
  530. if len(Time_start_) > 0 {
  531. //cond1 = cond1.And("T_Ut__gte", Time_start_)
  532. Time_start_ = lib.ReplaceSQL(Time_start_)
  533. sql_WHERE += fmt.Sprintf(" AND t__ut >= '%s'", Time_start_)
  534. }
  535. if len(Time_end_) > 0 {
  536. //cond1 = cond1.And("T_Ut__lte", Time_end_)
  537. Time_end_ = lib.ReplaceSQL(Time_end_)
  538. sql_WHERE += fmt.Sprintf(" AND t__ut <= '%s'", Time_end_)
  539. }
  540. //1:默认 2:全部记录 3 未处理
  541. if T_handle == 1 {
  542. sql_WHERE += " AND t__state > 1"
  543. } else if T_handle == 3 {
  544. sql_WHERE += " AND t__state = 3"
  545. } else {
  546. sql_WHERE += " AND t__state > 0"
  547. }
  548. // -------------
  549. sql_WHERE = strings.TrimLeft(sql_WHERE, " AND")
  550. // 从redis获取报警统计数量
  551. cnt, _, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
  552. if err != nil {
  553. sql := "SELECT COUNT(*) FROM " + Wtab + " WHERE " + sql_WHERE
  554. logs.Debug(sql)
  555. _, err = o.Raw(sql).ValuesList(&maps_z)
  556. if err != nil {
  557. logs.Error(lib.FuncName(), err)
  558. return r, 0
  559. }
  560. if len(maps_z) == 0 {
  561. return r, 0
  562. }
  563. cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  564. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
  565. }
  566. if cnt == 0 {
  567. return r, 0
  568. }
  569. sql := "SELECT ID,t_pid,t_tp,t_sn,t__d_name,t_id,t__d_s_name,t__remark,t__ut,t__text,t__log,t__msid,t__state,create_time,update_time " +
  570. "FROM " + Wtab + " WHERE " + sql_WHERE + " ORDER BY t__ut DESC"
  571. if page_z != 9999 {
  572. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  573. }
  574. logs.Debug(sql)
  575. _, err = o.Raw(sql).QueryRows(&maps)
  576. if err != nil {
  577. logs.Error(lib.FuncName(), err)
  578. return
  579. }
  580. for _, v := range maps {
  581. r = append(r, WarningToWarning_R(1, v))
  582. }
  583. return r, cnt
  584. }
  585. // 获取管理员列表备份
  586. func Read_Admin_Warning_Backups(admin Account.Admin, T_year string, T_month string, T_tp []string, T_name string, T_handle int,
  587. Time_start_ string, Time_end_ string, page int, page_z int, countRedisKey string) (r []Warning_R, cnt int64) {
  588. if len(admin.T_pids) == 0 {
  589. return r, cnt
  590. }
  591. o := orm.NewOrm()
  592. var maps []Warning
  593. var maps_z []orm2.ParamsList
  594. // 也可以直接使用 Model 结构体作为表名
  595. // Warning_2022_07
  596. Wtab := "warning_" + T_year + "_" + T_month
  597. var offset int
  598. if page_z == 0 {
  599. page_z = conf.Page_size
  600. }
  601. if page <= 1 {
  602. offset = 0
  603. } else {
  604. offset = (page - 1) * page_z
  605. }
  606. sql_WHERE := ""
  607. if admin.T_pids != "*" {
  608. list := lib.SplitStringToDotStr(admin.T_pids, "P")
  609. sql_WHERE += fmt.Sprintf(" AND t_pid in (%s)", list)
  610. }
  611. if len(T_tp) > 0 {
  612. tp := lib.StringListToDotStr(T_tp)
  613. sql_WHERE += fmt.Sprintf(" AND t_tp in (%s)", tp)
  614. }
  615. if len(T_name) > 0 {
  616. sql_WHERE += " AND (t_sn like '%" + T_name + "%' OR t__d_name like '%" + T_name + "%' OR t_id like '%" + T_name + "%' OR t__d_s_name like '%" + T_name + "%')"
  617. }
  618. if len(Time_start_) > 0 {
  619. //cond1 = cond1.And("T_Ut__gte", Time_start_)
  620. Time_start_ = lib.ReplaceSQL(Time_start_)
  621. sql_WHERE += fmt.Sprintf(" AND t__ut >= '%s'", Time_start_)
  622. }
  623. if len(Time_end_) > 0 {
  624. //cond1 = cond1.And("T_Ut__lte", Time_end_)
  625. Time_end_ = lib.ReplaceSQL(Time_end_)
  626. sql_WHERE += fmt.Sprintf(" AND t__ut <= '%s'", Time_end_)
  627. }
  628. //1:默认 2:全部记录 3 未处理
  629. if T_handle == 1 {
  630. sql_WHERE += " AND t__state > 1"
  631. } else if T_handle == 3 {
  632. sql_WHERE += " AND t__state = 3"
  633. } else {
  634. sql_WHERE += " AND t__state > 0"
  635. }
  636. sql_WHERE = strings.TrimLeft(sql_WHERE, " AND")
  637. // 从redis获取报警统计数量
  638. cnt, _, err := Redis_WarningCount_GetOne(admin.Id, countRedisKey)
  639. if err != nil {
  640. sql := "SELECT COUNT(*) FROM " + Wtab + " WHERE " + sql_WHERE
  641. logs.Debug(sql)
  642. _, err = o.Raw(sql).ValuesList(&maps_z)
  643. if err != nil {
  644. logs.Error(lib.FuncName(), err)
  645. return r, 0
  646. }
  647. if len(maps_z) == 0 {
  648. return r, 0
  649. }
  650. cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  651. Redis_WarningCount_Set(admin.Id, countRedisKey, cnt, map[string]int64{})
  652. }
  653. if cnt == 0 {
  654. return r, 0
  655. }
  656. sql := "SELECT ID,t_pid,t_tp,t_sn,t__d_name,t_id,t__d_s_name,t__remark,t__ut,t__text,t__log,t__msid,t__state,create_time,update_time " +
  657. "FROM " + Wtab + " WHERE " + sql_WHERE + " ORDER BY t__ut DESC"
  658. if page_z != 9999 {
  659. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  660. }
  661. logs.Debug(sql)
  662. _, err = o.Raw(sql).QueryRows(&maps)
  663. if err != nil {
  664. logs.Error(lib.FuncName(), err)
  665. return
  666. }
  667. key, _ := strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  668. for _, v := range maps {
  669. r = append(r, WarningToWarning_R(1, v))
  670. }
  671. return r, key
  672. }
  673. // 用户首页统计
  674. func Read_Warning_ALL_T_State_Count2(T_pid int, T_pids []int, T_handle int, bindSN []string, T_Warning string, Is_Today bool) (cnt int64) {
  675. o := orm.NewOrm()
  676. qs := o.QueryTable(new(Warning))
  677. cond := orm.NewCondition()
  678. cond1 := cond.And("T_State__gt", 0)
  679. if T_pid > 0 {
  680. cond1 = cond1.And("T_pid", T_pid)
  681. }
  682. if T_pid == 0 {
  683. cond1 = cond1.And("T_pid__in", T_pids)
  684. }
  685. if len(bindSN) > 0 {
  686. cond1 = cond1.And("T_sn__in", bindSN)
  687. }
  688. if Is_Today {
  689. startTime := time.Now().Format("2006-01-02") + " 00:00:00"
  690. endTime := time.Now().Format("2006-01-02") + " 23:59:59"
  691. cond1 = cond1.And("T_Ut__gte", startTime).And("T_Ut__lte", endTime)
  692. }
  693. if len(T_Warning) > 0 && T_Warning != "*" {
  694. list := lib.SplitStringIds(T_Warning, "W")
  695. cond1 = cond1.And("T_tp__in", list)
  696. }
  697. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  698. // T_handle 1:默认 2:全部记录 3 未处理
  699. if T_handle == 1 {
  700. cond1 = cond1.And("T_State__gt", 1)
  701. }
  702. if T_handle == 3 {
  703. cond1 = cond1.And("T_State", 3)
  704. }
  705. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
  706. if err != nil {
  707. logs.Error(lib.FuncName(), err)
  708. }
  709. return cnt
  710. }
  711. // 用户首页统计(查询最近40天),使用gorm查询
  712. func Read_Warning_ALL_T_State_Count(T_pid int, T_pids []int, T_handle int, bindSN []string, T_Warning string, Is_Today bool) (cnt int64) {
  713. tableName := "warning"
  714. gormDB := db.DB.Table(tableName)
  715. daysAgo40 := time.Now().AddDate(0, 0, -40).Format("2006-01-02")
  716. gormDB.Where("t__state > 0 ").Where("t__ut > ?", daysAgo40+" 00:00:00")
  717. if T_pid > 0 {
  718. gormDB.Where("t_pid = ?", T_pid)
  719. }
  720. if T_pid == 0 {
  721. gormDB.Where("t_pid in (?)", T_pids)
  722. }
  723. if len(bindSN) > 0 {
  724. gormDB.Where("t_sn in (?)", bindSN)
  725. }
  726. if Is_Today {
  727. startTime := time.Now().Format("2006-01-02") + " 00:00:00"
  728. endTime := time.Now().Format("2006-01-02") + " 23:59:59"
  729. gormDB.Where("t__ut between ? and ?", startTime, endTime)
  730. }
  731. if len(T_Warning) > 0 && T_Warning != "*" {
  732. list := lib.SplitStringIds(T_Warning, "W")
  733. gormDB.Where("t_tp in (?)", list)
  734. }
  735. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  736. // T_handle 1:默认 2:全部记录 3 未处理
  737. if T_handle == 1 {
  738. gormDB.Where("t__state > ?", 1)
  739. }
  740. if T_handle == 3 {
  741. gormDB.Where("t__state = ?", 3)
  742. }
  743. err := gormDB.Count(&cnt).Error
  744. if err != nil {
  745. logs.Error(lib.FuncName(), err)
  746. }
  747. return cnt
  748. }
  749. // 设备日志
  750. type DeviceLogs struct {
  751. Id int64
  752. T_sn string //
  753. Logs_Txt string // 详情
  754. CreateTime string
  755. }
  756. func WarningToDeviceLogs(t Warning) (r DeviceLogs) {
  757. r.Id = t.Id
  758. r.T_sn = t.T_sn
  759. r.Logs_Txt = t.T_Remark
  760. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  761. return r
  762. }
  763. // 获取列表
  764. func Read_DeviceLogs_List(T_sn string, page, page_z int) (r []DeviceLogs, cnt int64) {
  765. o := orm.NewOrm()
  766. // 也可以直接使用 Model 结构体作为表名
  767. qs := o.QueryTable(new(Warning))
  768. var maps []Warning
  769. var offset int64
  770. if page <= 1 {
  771. offset = 0
  772. } else {
  773. offset = int64((page - 1) * page_z)
  774. }
  775. _, err := qs.Limit(page_z, offset).Filter("T_sn", T_sn).Filter("T_tp__in", DeviceLogType).OrderBy("-Id").All(&maps)
  776. if err != nil {
  777. logs.Error(lib.FuncName(), err)
  778. return
  779. }
  780. cnt, err = qs.Filter("T_sn", T_sn).Filter("T_tp__in", DeviceLogType).Count()
  781. if err != nil {
  782. logs.Error(lib.FuncName(), err)
  783. return
  784. }
  785. for _, v := range maps {
  786. r = append(r, WarningToDeviceLogs(v))
  787. }
  788. return r, cnt
  789. }
  790. // 通过传感器类型获取报警列表
  791. func Read_Warning_List_By_DS_T_type(admin *Account.Admin, T_pid int, bindSN []string, T_pids []int, DS_T_type int, T_name string, T_tp []string,
  792. page int, page_z int, countRedisKey string) (r []Warning_Applet, cnt int64) {
  793. o := orm.NewOrm()
  794. // 也可以直接使用 Model 结构体作为表名
  795. var maps []Warning
  796. var maps_z []orm2.ParamsList
  797. var offset int
  798. if page <= 1 {
  799. offset = 0
  800. } else {
  801. offset = (page - 1) * page_z
  802. }
  803. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  804. sql_WHERE := "1 < t__state"
  805. if admin.T_pid > 0 {
  806. if len(T_pids) > 0 {
  807. sql_WHERE += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids))
  808. }
  809. } else {
  810. sql_WHERE += " AND t_pid = " + strconv.Itoa(T_pid)
  811. }
  812. if len(T_tp) > 0 {
  813. tp := lib.StringListToDotStr(T_tp)
  814. sql_WHERE += fmt.Sprintf(" AND w.t_tp in (%s)", tp)
  815. }
  816. if len(bindSN) > 0 {
  817. sql_WHERE += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
  818. }
  819. if DS_T_type > 0 {
  820. sql_WHERE += " AND ds.t_type = " + strconv.Itoa(DS_T_type)
  821. }
  822. if len(T_name) > 0 {
  823. sql_WHERE += " AND (t_sn like '%" + T_name + "%' OR t__d_name like '%" + T_name + "%' OR t_id like '%" + T_name + "%' OR t__d_s_name like '%" + T_name + "%')"
  824. }
  825. sql := "SELECT COUNT(w.ID) FROM warning w " +
  826. "LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_type FROM device_sensor) AS ds " +
  827. "ON w.t_sn = ds.tsn AND w.t_id = ds.tid " +
  828. "WHERE " + sql_WHERE
  829. cnt, _, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
  830. if err != nil {
  831. logs.Debug(sql)
  832. _, err = o.Raw(sql).ValuesList(&maps_z)
  833. if err != nil {
  834. logs.Error(lib.FuncName(), err)
  835. return r, 0
  836. }
  837. if len(maps_z) == 0 {
  838. return r, 0
  839. }
  840. cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  841. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
  842. }
  843. if cnt == 0 {
  844. return r, 0
  845. }
  846. sql = "SELECT w.*, ds.tsn, ds.tid, ds.t_type FROM warning w " +
  847. "LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_type FROM device_sensor) AS ds " +
  848. "ON w.t_sn = ds.tsn AND w.t_id = ds.tid " +
  849. "WHERE " + sql_WHERE + " ORDER BY t__ut DESC"
  850. if page_z != 9999 {
  851. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  852. }
  853. logs.Debug(sql)
  854. _, err = o.Raw(sql).QueryRows(&maps)
  855. if err != nil {
  856. logs.Error(lib.FuncName(), err)
  857. return
  858. }
  859. for _, v := range maps {
  860. r = append(r, WarningToWarning_Applet(v))
  861. }
  862. return r, cnt
  863. }
  864. // 获取当天设备报警总数
  865. func Read_WarningCount_byDay(T_pids []int) (cnt int64) {
  866. o := orm.NewOrm()
  867. qs := o.QueryTable(new(Warning))
  868. cond := orm.NewCondition()
  869. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  870. cond = cond.And("T_State__gt", 0).And("T_tp__in", RateType).And("CreateTime__gte", yesterday+" 00:00:00").And("CreateTime__lte", yesterday+" 23:59:59")
  871. if len(T_pids) == 1 {
  872. cond = cond.And("T_pid", T_pids[0])
  873. } else if len(T_pids) > 1 {
  874. cond = cond.And("T_pid__in", T_pids)
  875. }
  876. cnt, err := qs.SetCond((*orm2.Condition)(cond)).Count()
  877. if err != nil {
  878. logs.Error(lib.FuncName(), err)
  879. }
  880. return cnt
  881. }
  882. // 公司管理报警列表
  883. func Read_Company_Warning_List(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
  884. T_pid int, countRedisKey string) (r []CompanyWarning_R, cnt int64) {
  885. o := orm.NewOrm()
  886. // 也可以直接使用 Model 结构体作为表名
  887. var map_r []Warning
  888. qs := o.QueryTable(new(Warning))
  889. var offset int64
  890. if page_z == 0 {
  891. page_z = conf.Page_size
  892. }
  893. if page <= 1 {
  894. offset = 0
  895. } else {
  896. offset = int64((page - 1) * page_z)
  897. }
  898. cond := orm.NewCondition()
  899. cond1 := orm.NewCondition()
  900. if len(T_pids) == 1 {
  901. cond1 = cond1.And("T_pid", T_pids[0])
  902. } else if len(T_pids) > 1 {
  903. cond1 = cond1.And("T_pid__in", T_pids)
  904. }
  905. if len(T_tp) > 0 {
  906. cond1 = cond1.And("T_tp__in", T_tp)
  907. }
  908. if len(T_name) > 0 {
  909. if len(T_name) == 16 {
  910. cond1 = cond1.And("T_sn", T_name)
  911. } else {
  912. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", T_name).
  913. Or("T_D_name__icontains", T_name).
  914. Or("T_id__icontains", T_name).
  915. Or("T_DS_name__icontains", T_name))
  916. }
  917. }
  918. if len(Time_start_) > 0 {
  919. cond1 = cond1.And("T_Ut__gte", Time_start_)
  920. }
  921. if len(Time_end_) > 0 {
  922. cond1 = cond1.And("T_Ut__lte", Time_end_)
  923. }
  924. //1:默认 2:全部记录 3 未处理
  925. if T_handle == 1 {
  926. cond1 = cond1.And("T_State__gt", 1)
  927. } else if T_handle == 3 {
  928. cond1 = cond1.And("T_State", 3)
  929. } else {
  930. cond1 = cond1.And("T_State__gt", 0)
  931. }
  932. var err error
  933. // 从redis获取报警统计数量
  934. cnt, _, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
  935. if err != nil {
  936. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  937. if err != nil {
  938. logs.Error(lib.FuncName(), err)
  939. return
  940. }
  941. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
  942. }
  943. if cnt == 0 {
  944. return r, 0
  945. }
  946. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_Ut").All(&map_r)
  947. if err != nil {
  948. logs.Error(lib.FuncName(), err)
  949. }
  950. for _, v := range map_r {
  951. r = append(r, WarningToCompanyWarning_R(0, v))
  952. }
  953. return r, cnt
  954. }
  955. // 获取公司管理列表备份
  956. func Read_Company_Warning_Backups(T_pids []int, T_year string, T_month string, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string,
  957. page int, page_z int, T_pid int, countRedisKey string) (r []CompanyWarning_R, cnt int64) {
  958. if len(T_pids) == 0 {
  959. return r, cnt
  960. }
  961. o := orm.NewOrm()
  962. var maps []Warning
  963. var maps_z []orm2.ParamsList
  964. // 也可以直接使用 Model 结构体作为表名
  965. // Warning_2022_07
  966. Wtab := "warning_" + T_year + "_" + T_month
  967. var offset int
  968. if page_z == 0 {
  969. page_z = conf.Page_size
  970. }
  971. if page <= 1 {
  972. offset = 0
  973. } else {
  974. offset = (page - 1) * page_z
  975. }
  976. sql_WHERE := ""
  977. if len(T_pids) == 1 {
  978. sql_WHERE += fmt.Sprintf(" AND t_pid = %d", T_pids[0])
  979. } else if len(T_pids) > 1 {
  980. sql_WHERE += fmt.Sprintf(" AND t_pid in (%s)", lib.IntListToDotStr(T_pids))
  981. }
  982. if len(T_tp) > 0 {
  983. tp := lib.StringListToDotStr(T_tp)
  984. sql_WHERE += fmt.Sprintf(" AND t_tp in (%s)", tp)
  985. }
  986. if len(T_name) > 0 {
  987. sql_WHERE += " AND (t_sn like '%" + T_name + "%' OR t__d_name like '%" + T_name + "%' OR t_id like '%" + T_name + "%' OR t__d_s_name like '%" + T_name + "%')"
  988. }
  989. if len(Time_start_) > 0 {
  990. //cond1 = cond1.And("T_Ut__gte", Time_start_)
  991. Time_start_ = lib.ReplaceSQL(Time_start_)
  992. sql_WHERE += fmt.Sprintf(" AND t__ut >= '%s'", Time_start_)
  993. }
  994. if len(Time_end_) > 0 {
  995. //cond1 = cond1.And("T_Ut__lte", Time_end_)
  996. Time_end_ = lib.ReplaceSQL(Time_end_)
  997. sql_WHERE += fmt.Sprintf(" AND t__ut <= '%s'", Time_end_)
  998. }
  999. //1:默认 2:全部记录 3 未处理
  1000. if T_handle == 1 {
  1001. sql_WHERE += " AND t__state > 1"
  1002. } else if T_handle == 3 {
  1003. sql_WHERE += " AND t__state = 3"
  1004. } else {
  1005. sql_WHERE += " AND t__state > 0"
  1006. }
  1007. sql_WHERE = strings.TrimLeft(sql_WHERE, " AND")
  1008. // 从redis获取报警统计数量
  1009. cnt, _, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
  1010. if err != nil {
  1011. sql := "SELECT COUNT(*) FROM " + Wtab + " WHERE " + sql_WHERE
  1012. logs.Debug(sql)
  1013. _, err = o.Raw(sql).ValuesList(&maps_z)
  1014. if err != nil {
  1015. logs.Error(lib.FuncName(), err)
  1016. return r, 0
  1017. }
  1018. if len(maps_z) == 0 {
  1019. return r, 0
  1020. }
  1021. cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  1022. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, map[string]int64{})
  1023. }
  1024. if cnt == 0 {
  1025. return r, 0
  1026. }
  1027. sql := "SELECT ID,t_pid,t_tp,t_sn,t__d_name,t_id,t__d_s_name,t__remark,t__ut,t__text,t__log,t__msid,t__state,create_time,update_time " +
  1028. "FROM " + Wtab + " WHERE " + sql_WHERE + " ORDER BY t__ut DESC"
  1029. if page_z != 9999 {
  1030. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  1031. }
  1032. logs.Debug(sql)
  1033. _, err = o.Raw(sql).QueryRows(&maps)
  1034. if err != nil {
  1035. logs.Error(lib.FuncName(), err)
  1036. return
  1037. }
  1038. for _, v := range maps {
  1039. r = append(r, WarningToCompanyWarning_R(1, v))
  1040. }
  1041. return r, cnt
  1042. }
  1043. func GormReadWarningListScopes(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string) func(db *gorm.DB) *gorm.DB {
  1044. return func(db *gorm.DB) *gorm.DB {
  1045. if T_pid > 0 {
  1046. db.Where("t_pid = ?", T_pid)
  1047. }
  1048. if T_abandon == 1 {
  1049. // 弃用
  1050. if len(abandonSN) > 0 {
  1051. db.Where("t_sn in (?)", abandonSN)
  1052. } else {
  1053. db.Where("1=0")
  1054. return db
  1055. }
  1056. } else if conf.UseNewWarningQuery {
  1057. if len(bindSN) > 0 {
  1058. // 将绑定sn内非弃用sn筛选出来
  1059. bindSNWithOutAbandon := lib.FindUniqueElements(bindSN, abandonSN)
  1060. if len(bindSNWithOutAbandon) > 0 {
  1061. db.Where("t_sn in (?)", bindSNWithOutAbandon)
  1062. } else {
  1063. db.Where("1=0")
  1064. return db
  1065. }
  1066. } else {
  1067. if len(abandonSN) > 0 {
  1068. db.Where("t_sn not in (?)", abandonSN)
  1069. }
  1070. }
  1071. }
  1072. if len(tpList) > 0 {
  1073. min, max, is := checkStringListContinuous(tpList)
  1074. if is {
  1075. db.Where("t_tp between ? and ?", min, max)
  1076. } else {
  1077. db.Where("t_tp in (?)", tpList)
  1078. }
  1079. }
  1080. if len(T_name) > 0 {
  1081. if len(T_name) == 16 {
  1082. db.Where("t_sn = ?", T_name)
  1083. } else {
  1084. likeName := "%" + T_name + "%"
  1085. db.Where("t_sn like ? or t__d_name like ? or t_id like ? or t__d_s_name like ? ", likeName, likeName, likeName, likeName)
  1086. }
  1087. }
  1088. if len(Time_start_) > 0 {
  1089. db.Where("t__ut >= ?", Time_start_)
  1090. }
  1091. if len(Time_end_) > 0 {
  1092. db.Where("t__ut <= ?", Time_end_)
  1093. }
  1094. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  1095. // T_handle 1:默认 2:全部记录 3 未处理
  1096. if T_handle == 1 {
  1097. db.Where("t__state > ?", 1)
  1098. } else if T_handle == 3 {
  1099. db.Where("t__state = ?", 3)
  1100. } else {
  1101. db.Where("t__state > ?", 0)
  1102. }
  1103. return db
  1104. }
  1105. }
  1106. // 获取列表
  1107. func Gorm_Read_Warning_List(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string, page int, page_z int,
  1108. countRedisKey string) (r []Warning_R, cnt int64) {
  1109. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1110. var err error
  1111. var warnings []Warning
  1112. tableCount := make(map[string]int64)
  1113. gormReadWarningListScopes := GormReadWarningListScopes(T_pid, bindSN, tpList, T_name, T_handle, Time_start_, Time_end_, T_abandon, abandonSN)
  1114. // 从redis获取报警统计数量
  1115. cnt, tableCount, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
  1116. if err != nil {
  1117. for _, tableName := range tableNames {
  1118. tableDB := db.DB.Table(tableName)
  1119. var count int64
  1120. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  1121. tableCount[tableName] = count
  1122. cnt += count
  1123. }
  1124. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, tableCount)
  1125. }
  1126. if cnt == 0 {
  1127. return r, 0
  1128. }
  1129. if page_z == 9999 {
  1130. for _, tableName := range tableNames {
  1131. var tableWarning []Warning
  1132. tableDB := db.DB.Table(tableName)
  1133. tableDB.Scopes(gormReadWarningListScopes).
  1134. Order("t__ut desc").Offset(-1).Limit(-1).Find(&tableWarning)
  1135. warnings = append(warnings, tableWarning...)
  1136. }
  1137. for _, v := range warnings {
  1138. r = append(r, WarningToWarning_R(0, v))
  1139. }
  1140. return r, cnt
  1141. }
  1142. offset := (page - 1) * page_z
  1143. remaining := offset
  1144. for _, tableName := range tableNames {
  1145. var tableWarning []Warning
  1146. tableDB := db.DB.Table(tableName)
  1147. var count int64
  1148. if tableName == fmt.Sprintf("warning_%d_%02d", time.Now().Year(), time.Now().Month()) {
  1149. if saveTime, ok := tableCount["SaveTime"]; ok {
  1150. t1 := time.Unix(saveTime, 0)
  1151. if time.Now().Sub(t1) < 5*time.Minute {
  1152. if count, ok = tableCount[tableName]; !ok {
  1153. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  1154. tableCount[tableName] = count
  1155. }
  1156. } else {
  1157. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  1158. tableCount[tableName] = count
  1159. }
  1160. }
  1161. } else {
  1162. var ok bool
  1163. if count, ok = tableCount[tableName]; !ok {
  1164. tableDB.Scopes(gormReadWarningListScopes).Count(&count)
  1165. tableCount[tableName] = count
  1166. }
  1167. }
  1168. if remaining >= int(count) {
  1169. remaining -= int(count)
  1170. continue
  1171. }
  1172. tableDB2 := db.DB.Table(tableName)
  1173. tableDB2.Scopes(gormReadWarningListScopes).Order("t__ut desc").Offset(remaining).Limit(page_z - len(warnings)).Find(&tableWarning)
  1174. warnings = append(warnings, tableWarning...)
  1175. if len(warnings) >= page_z {
  1176. break
  1177. }
  1178. remaining = 0
  1179. }
  1180. for _, v := range warnings {
  1181. r = append(r, WarningToWarning_R(0, v))
  1182. }
  1183. return r, cnt
  1184. }
  1185. func Gorm_Read_Warning_List_Excel(T_pid int, bindSN, tpList []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string, page int, page_z int,
  1186. countRedisKey string) (r []Warning_R, cnt int64) {
  1187. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1188. gormReadWarningListScopes := GormReadWarningListScopes(T_pid, bindSN, tpList, T_name, T_handle, Time_start_, Time_end_, T_abandon, abandonSN)
  1189. var warnings []Warning
  1190. var wg sync.WaitGroup
  1191. for _, tableName := range tableNames {
  1192. wg.Add(1)
  1193. go func(table string) {
  1194. defer wg.Done()
  1195. offset := 0
  1196. limit := 10000
  1197. for {
  1198. var tableWarning []Warning
  1199. tableDB := db.DB.Table(table)
  1200. tableDB.Scopes(gormReadWarningListScopes).
  1201. Order("t__ut desc").Offset(offset).Limit(limit).Find(&tableWarning)
  1202. if len(tableWarning) == 0 {
  1203. break // 如果没有数据则结束循环
  1204. }
  1205. offset += limit // 更新偏移量
  1206. warnings = append(warnings, tableWarning...)
  1207. }
  1208. }(tableName)
  1209. }
  1210. // 等待所有协程完成
  1211. wg.Wait()
  1212. // 倒序
  1213. sort.Slice(warnings, func(i, j int) bool {
  1214. if warnings[i].T_Ut.After(warnings[j].T_Ut) {
  1215. return true
  1216. }
  1217. return false
  1218. })
  1219. for _, v := range warnings {
  1220. r = append(r, WarningToWarning_R(0, v))
  1221. }
  1222. return r, cnt
  1223. }
  1224. func GormReadAdminWarningListScopes(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string) func(db *gorm.DB) *gorm.DB {
  1225. return func(db *gorm.DB) *gorm.DB {
  1226. if admin.T_pids != "*" && len(admin.T_pids) > 0 {
  1227. list := lib.SplitStringIds(admin.T_pids, "P")
  1228. db.Where("t_pid in (?)", list)
  1229. }
  1230. if T_abandon == 1 {
  1231. // 查询弃用设备
  1232. if len(abandonSN) > 0 {
  1233. db.Where("t_sn in (?)", abandonSN)
  1234. } else {
  1235. db.Where("1=0")
  1236. return db
  1237. }
  1238. } else if conf.UseNewWarningQuery {
  1239. if len(abandonSN) > 0 {
  1240. db.Where("t_sn not in (?)", abandonSN)
  1241. }
  1242. }
  1243. if len(T_tp) > 0 {
  1244. min, max, is := checkStringListContinuous(T_tp)
  1245. if is {
  1246. db.Where("t_tp between ? and ?", min, max)
  1247. } else {
  1248. db.Where("t_tp in (?)", T_tp)
  1249. }
  1250. }
  1251. if len(T_name) > 0 {
  1252. if len(T_name) == 16 {
  1253. db.Where("t_sn = ?", T_name)
  1254. } else {
  1255. likeName := "%" + T_name + "%"
  1256. db.Where("t_sn like ? or t__d_name like ? or t_id like ? or t__d_s_name like ? ", likeName, likeName, likeName, likeName)
  1257. }
  1258. }
  1259. if len(Time_start_) > 0 {
  1260. db.Where("t__ut >= ?", Time_start_)
  1261. }
  1262. if len(Time_end_) > 0 {
  1263. db.Where("t__ut <= ?", Time_end_)
  1264. }
  1265. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  1266. // T_handle 1:默认 2:全部记录 3 未处理
  1267. if T_handle == 1 {
  1268. db.Where("t__state > ?", 1)
  1269. } else if T_handle == 3 {
  1270. db.Where("t__state = ?", 3)
  1271. } else {
  1272. db.Where("t__state > ?", 0)
  1273. }
  1274. return db
  1275. }
  1276. }
  1277. // 管理员报警列表
  1278. func Gorm_Read_Admin_Warning_List(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string, page int, page_z int,
  1279. countRedisKey string) (r []Warning_R, cnt int64) {
  1280. if len(admin.T_pids) == 0 {
  1281. return r, cnt
  1282. }
  1283. gormReadAdminWarningListScopes := GormReadAdminWarningListScopes(admin, T_tp, T_name, T_handle, Time_start_, Time_end_, T_abandon, abandonSN)
  1284. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1285. var err error
  1286. var warnings []Warning
  1287. tableCount := make(map[string]int64)
  1288. // 从redis获取报警统计数量
  1289. cnt, tableCount, err = Redis_WarningCount_GetOne(admin.Id, countRedisKey)
  1290. if err != nil {
  1291. for _, tableName := range tableNames {
  1292. tableDB := db.DB.Table(tableName)
  1293. var count int64
  1294. tableDB.Scopes(gormReadAdminWarningListScopes).Count(&count)
  1295. tableCount[tableName] = count
  1296. cnt += count
  1297. }
  1298. Redis_WarningCount_Set(admin.Id, countRedisKey, cnt, tableCount)
  1299. }
  1300. if cnt == 0 {
  1301. return r, 0
  1302. }
  1303. // page_z == 9999 获取全部
  1304. if page_z == 9999 {
  1305. for _, tableName := range tableNames {
  1306. var tableWarning []Warning
  1307. tableDB := db.DB.Table(tableName)
  1308. tableDB.Scopes(gormReadAdminWarningListScopes).
  1309. Order("t__ut desc").Offset(-1).Limit(-1).Find(&tableWarning)
  1310. warnings = append(warnings, tableWarning...)
  1311. }
  1312. for _, v := range warnings {
  1313. r = append(r, WarningToWarning_R(0, v))
  1314. }
  1315. return r, cnt
  1316. }
  1317. offset := (page - 1) * page_z
  1318. remaining := offset
  1319. for _, tableName := range tableNames {
  1320. var tableWarning []Warning
  1321. tableDB := db.DB.Table(tableName)
  1322. count := tableCount[tableName]
  1323. if remaining >= int(count) {
  1324. remaining -= int(count)
  1325. continue
  1326. }
  1327. tableDB.Scopes(gormReadAdminWarningListScopes).
  1328. Order("t__ut desc").Offset(remaining).Limit(page_z - len(warnings)).Find(&tableWarning)
  1329. warnings = append(warnings, tableWarning...)
  1330. if len(warnings) >= page_z {
  1331. break
  1332. }
  1333. remaining = 0
  1334. }
  1335. for _, v := range warnings {
  1336. r = append(r, WarningToWarning_R(0, v))
  1337. }
  1338. return r, cnt
  1339. }
  1340. func Gorm_Read_Admin_Warning_List_Excel(admin Account.Admin, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, T_abandon int, abandonSN []string, page int, page_z int,
  1341. countRedisKey string) (r []Warning_R, cnt int64) {
  1342. if len(admin.T_pids) == 0 {
  1343. return r, cnt
  1344. }
  1345. gormReadAdminWarningListScopes := GormReadAdminWarningListScopes(admin, T_tp, T_name, T_handle, Time_start_, Time_end_, T_abandon, abandonSN)
  1346. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1347. var warnings []Warning
  1348. var wg sync.WaitGroup
  1349. for _, tableName := range tableNames {
  1350. wg.Add(1)
  1351. go func(table string) {
  1352. defer wg.Done()
  1353. offset := 0
  1354. limit := 10000
  1355. for {
  1356. var tableWarning []Warning
  1357. tableDB := db.DB.Table(table)
  1358. tableDB.Scopes(gormReadAdminWarningListScopes).
  1359. Order("t__ut desc").Offset(offset).Limit(limit).Find(&tableWarning)
  1360. if len(tableWarning) == 0 {
  1361. break // 如果没有数据则结束循环
  1362. }
  1363. offset += limit // 更新偏移量
  1364. warnings = append(warnings, tableWarning...)
  1365. }
  1366. }(tableName)
  1367. }
  1368. // 等待所有协程完成
  1369. wg.Wait()
  1370. // 倒序
  1371. sort.Slice(warnings, func(i, j int) bool {
  1372. if warnings[i].T_Ut.After(warnings[j].T_Ut) {
  1373. return true
  1374. }
  1375. return false
  1376. })
  1377. for _, v := range warnings {
  1378. r = append(r, WarningToWarning_R(0, v))
  1379. }
  1380. return r, cnt
  1381. }
  1382. func GormReadCompanyWarningListScopes(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string) func(db *gorm.DB) *gorm.DB {
  1383. return func(db *gorm.DB) *gorm.DB {
  1384. if len(T_pids) > 0 {
  1385. db.Where("t_pid in (?)", T_pids)
  1386. }
  1387. if len(T_tp) > 0 {
  1388. min, max, is := checkStringListContinuous(T_tp)
  1389. if is {
  1390. db.Where("t_tp between ? and ?", min, max)
  1391. } else {
  1392. db.Where("t_tp in (?)", T_tp)
  1393. }
  1394. }
  1395. if len(T_name) > 0 {
  1396. if len(T_name) == 16 {
  1397. db.Where("t_sn = ?", T_name)
  1398. } else {
  1399. likeName := "%" + T_name + "%"
  1400. db.Where("t_sn like ? or t__d_name like ? or t_id like ? or t__d_s_name like ?", likeName, likeName, likeName, likeName)
  1401. }
  1402. }
  1403. if len(Time_start_) > 0 {
  1404. db.Where("t__ut >= ?", Time_start_)
  1405. }
  1406. if len(Time_end_) > 0 {
  1407. db.Where("t__ut <= ?", Time_end_)
  1408. }
  1409. // T_State 0 删除 1 不处理 2 已处理 3 未处理
  1410. // T_handle 1:默认 2:全部记录 3 未处理
  1411. if T_handle == 1 {
  1412. db.Where("t__state > ?", 1)
  1413. } else if T_handle == 3 {
  1414. db.Where("t__state = ?", 3)
  1415. } else {
  1416. db.Where("t__state > ?", 0)
  1417. }
  1418. return db
  1419. }
  1420. }
  1421. // 公司管理报警列表
  1422. func Gorm_Read_Company_Warning_List(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
  1423. T_pid int, countRedisKey string) (r []CompanyWarning_R, cnt int64) {
  1424. gormReadCompanyWarningListScopes := GormReadCompanyWarningListScopes(T_pids, T_tp, T_name, T_handle, Time_start_, Time_end_)
  1425. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1426. var err error
  1427. var warnings []Warning
  1428. tableCount := make(map[string]int64)
  1429. // 从redis获取报警统计数量
  1430. cnt, tableCount, err = Redis_WarningCount_GetOne(T_pid, countRedisKey)
  1431. if err != nil {
  1432. for _, tableName := range tableNames {
  1433. tableDB := db.DB.Table(tableName)
  1434. var count int64
  1435. tableDB.Scopes(gormReadCompanyWarningListScopes).Count(&count)
  1436. tableCount[tableName] = count
  1437. cnt += count
  1438. }
  1439. Redis_WarningCount_Set(T_pid, countRedisKey, cnt, tableCount)
  1440. }
  1441. if cnt == 0 {
  1442. return r, 0
  1443. }
  1444. offset := (page - 1) * page_z
  1445. remaining := offset
  1446. for _, tableName := range tableNames {
  1447. var tableWarning []Warning
  1448. tableDB := db.DB.Table(tableName)
  1449. count := tableCount[tableName]
  1450. if remaining >= int(count) {
  1451. remaining -= int(count)
  1452. continue
  1453. }
  1454. tableDB.Scopes(gormReadCompanyWarningListScopes).
  1455. Order("t__ut desc").Offset(remaining).Limit(page_z - len(warnings)).Find(&tableWarning)
  1456. warnings = append(warnings, tableWarning...)
  1457. if len(warnings) >= page_z {
  1458. break
  1459. }
  1460. remaining = 0
  1461. }
  1462. for _, v := range warnings {
  1463. r = append(r, WarningToCompanyWarning_R(0, v))
  1464. }
  1465. return r, cnt
  1466. }
  1467. func Gorm_Read_Company_Warning_List_Excel(T_pids []int, T_tp []string, T_name string, T_handle int, Time_start_ string, Time_end_ string, page int, page_z int,
  1468. T_pid int, countRedisKey string) (r []CompanyWarning_R, cnt int64) {
  1469. gormReadCompanyWarningListScopes := GormReadCompanyWarningListScopes(T_pids, T_tp, T_name, T_handle, Time_start_, Time_end_)
  1470. tableNames := GenerateWarningTableNames(Time_start_, Time_end_)
  1471. var warnings []Warning
  1472. var wg sync.WaitGroup
  1473. for _, tableName := range tableNames {
  1474. wg.Add(1)
  1475. go func(table string) {
  1476. defer wg.Done()
  1477. offset := 0
  1478. limit := 10000
  1479. for {
  1480. var tableWarning []Warning
  1481. tableDB := db.DB.Table(table)
  1482. tableDB.Scopes(gormReadCompanyWarningListScopes).
  1483. Order("t__ut desc").Offset(offset).Limit(limit).Find(&tableWarning)
  1484. if len(tableWarning) == 0 {
  1485. break // 如果没有数据则结束循环
  1486. }
  1487. offset += limit // 更新偏移量
  1488. warnings = append(warnings, tableWarning...)
  1489. }
  1490. }(tableName)
  1491. }
  1492. // 等待所有协程完成
  1493. wg.Wait()
  1494. // 倒序
  1495. sort.Slice(warnings, func(i, j int) bool {
  1496. if warnings[i].T_Ut.After(warnings[j].T_Ut) {
  1497. return true
  1498. }
  1499. return false
  1500. })
  1501. for _, v := range warnings {
  1502. r = append(r, WarningToCompanyWarning_R(0, v))
  1503. }
  1504. return r, cnt
  1505. }
  1506. func GenerateWarningTableNames(startDateStr string, endDateStr string) []string {
  1507. if len(startDateStr) == 0 || len(endDateStr) == 0 {
  1508. return []string{}
  1509. }
  1510. startDate, _ := lib.TimeStrToTime(startDateStr)
  1511. endDate, _ := lib.TimeStrToTime(endDateStr)
  1512. monthsArray := []string{}
  1513. for startDate.Before(endDate) || startDate.Equal(endDate) {
  1514. monthsArray = append(monthsArray, fmt.Sprintf("warning_%d_%02d", startDate.Year(), startDate.Month()))
  1515. startDate = time.Date(startDate.Year(), startDate.Month()+1, 1, 0, 0, 0, 0, time.Local)
  1516. }
  1517. sort.Stable(sort.Reverse(sort.StringSlice(monthsArray)))
  1518. return monthsArray
  1519. }
  1520. // 检查数据列表是否连续 返回最小值 最大值
  1521. func checkStringListContinuous(nums []string) (int, int, bool) {
  1522. intNums := make([]int, len(nums))
  1523. for i, num := range nums {
  1524. parsedNum, err := strconv.Atoi(num)
  1525. if err != nil {
  1526. logs.Error("Error converting string to int:", err)
  1527. return 0, 0, false
  1528. }
  1529. intNums[i] = parsedNum
  1530. }
  1531. sort.Ints(intNums)
  1532. for i := 1; i < len(intNums); i++ {
  1533. if intNums[i] != intNums[i-1]+1 {
  1534. return 0, 0, false
  1535. }
  1536. }
  1537. return intNums[0], intNums[len(intNums)-1], true
  1538. }