WarningSand.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package Warning
  2. import (
  3. "ColdP_server/controllers/lib"
  4. "ColdP_server/logs"
  5. "ColdP_server/models/Account"
  6. "fmt"
  7. "github.com/beego/beego/v2/client/orm"
  8. orm2 "github.com/beego/beego/v2/client/orm"
  9. _ "github.com/go-sql-driver/mysql"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // 模板
  15. // 报警发送统计
  16. type WarningSand struct {
  17. Id int `orm:"column(ID);size(11);auto;pk"`
  18. T_pid int `orm:"index;size(100);null"` // Account.Company 绑定公司, -1:未知 0:管理员 >0 :绑定公司
  19. T_Spid int `orm:"index;size(100);"` // 触发公司 ID
  20. T_tp int `orm:"index;size(20);null"` // 报警类型 ->WarningList
  21. T_Notice string `orm:"size(256);null"` // 18888888888
  22. T_type int `orm:"index;size(4);null"` // 1 短信 2 电话
  23. T_Remark string `orm:"size(256);null"` // 备注
  24. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  25. }
  26. func (t *WarningSand) TableName() string {
  27. return "warning_sand" // 数据库名称 // ************** 替换 FormulaList **************
  28. }
  29. func init() {
  30. //注册模型
  31. orm.RegisterModel(new(WarningSand))
  32. }
  33. type WarningSand_R struct {
  34. Id int
  35. T_pid int // Account.Company 绑定公司, -1:未知 0:管理员 >0 :绑定公司
  36. T_tp int // 报警类型 ->WarningList
  37. T_tp_name string
  38. T_Notice string // 18888888888
  39. T_type int // 1 短信 2 电话
  40. T_Remark []string // 备注
  41. CreateTime string //auto_now_add 第一次保存时才设置时间
  42. }
  43. func WarningSandToWarningSand_R(t WarningSand) (r WarningSand_R) {
  44. r.Id = t.Id
  45. r.T_pid = t.T_pid
  46. r.T_tp = t.T_tp
  47. r.T_tp_name = Read_WarningType_Get(t.T_tp)
  48. r.T_Notice = t.T_Notice
  49. r.T_type = t.T_type
  50. //r.T_Remark = t.T_Remark
  51. if len(t.T_Remark) > 0 {
  52. r.T_Remark = strings.Split(strings.TrimRight(t.T_Remark, "\n"), "\n")
  53. }
  54. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  55. return r
  56. }
  57. // ---------------- 特殊方法 -------------------
  58. // 获取 ById
  59. func Read_WarningWx_ById(id int) (r WarningSand) {
  60. o := orm.NewOrm()
  61. r = WarningSand{Id: id}
  62. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  63. if err != nil {
  64. fmt.Println(err)
  65. }
  66. return r
  67. }
  68. // 添加
  69. func Add_WarningHand(m WarningSand) {
  70. warningWxList := Read_DeviceWarning(m)
  71. if len(warningWxList) == 0 {
  72. Add_WarningHand(m)
  73. }
  74. }
  75. // 添加
  76. func Add_WarningSand(m WarningSand) (id int64, err error) {
  77. o := orm.NewOrm()
  78. id, err = o.Insert(&m)
  79. //if err != nil {
  80. // fmt.Println(err)
  81. //}
  82. return id, err
  83. }
  84. // 获取列表
  85. func Read_DeviceWarning(Warning WarningSand) (r []WarningSand) {
  86. o := orm.NewOrm()
  87. // 也可以直接使用 Model 结构体作为表名
  88. qs := o.QueryTable(new(WarningSand))
  89. now := time.Now()
  90. // N 个小时前
  91. dd, _ := time.ParseDuration("-1m") //"s", "m", "h".
  92. currentTime := now.Add(dd)
  93. qs.Filter("T_pid", Warning.T_pid).Filter("T_tp", Warning.T_tp).Filter("T_Notice", Warning.T_Notice).Filter("T_type", Warning.T_type).Filter("CreateTime__gte", currentTime.Format("2006-01-02 15:04:05")).OrderBy("-Id").All(&r)
  94. // 记录 ++
  95. if len(r) > 0 {
  96. ColAdd_DeviceWarningBy_ID(r[0].Id)
  97. }
  98. return r
  99. }
  100. // 获取时 累加 1
  101. func ColAdd_DeviceWarningBy_ID(id int) {
  102. o := orm.NewOrm()
  103. // 也可以直接使用 Model 结构体作为表名
  104. design := new(WarningSand)
  105. qs := o.QueryTable(design)
  106. // 获取时 累加 1
  107. qs.Filter("Id", id).Update(orm2.Params{"T_Filter": orm2.ColValue(orm.ColAdd, 1)})
  108. return
  109. }
  110. func Read_WarningSand_ALL_T_sn_TIME_Count(T_sn string) (cnt int) {
  111. o := orm.NewOrm()
  112. var maps_z []orm2.ParamsList
  113. now := time.Now()
  114. timeStr := now.Format("2006-01-02") + " 00:00:00"
  115. sql := "SELECT COUNT(ID) FROM warning_sand WHERE t__title NOT LIKE '%恢复%' AND t__ut > '" + timeStr + "' AND t_sn LIKE '%" + T_sn + "%' "
  116. fmt.Println(sql)
  117. _, err := o.Raw(sql).ValuesList(&maps_z)
  118. if err != nil {
  119. return 0
  120. }
  121. if len(maps_z) == 0 {
  122. return 0
  123. }
  124. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  125. key, _ := strconv.Atoi(maps_z[0][0].(string))
  126. return key
  127. }
  128. func Read_WarningSand_ALL_T_Bind_TIME_Count(user_ Account.Admin, T_sn string) (cnt int) {
  129. o := orm.NewOrm()
  130. var maps_z []orm2.ParamsList
  131. now := time.Now()
  132. timeStr := now.Format("2006-01-02") + " 00:00:00"
  133. T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
  134. sql := "SELECT COUNT(ID) FROM warning_sand WHERE t__title NOT LIKE '%恢复%' AND t__ut > '" + timeStr + "' AND t__bind LIKE '%" + T_Bind + "%' AND t_sn LIKE '%" + T_sn + "%'"
  135. fmt.Println(sql)
  136. _, err := o.Raw(sql).ValuesList(&maps_z)
  137. if err != nil {
  138. return 0
  139. }
  140. if len(maps_z) == 0 {
  141. return 0
  142. }
  143. //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
  144. key, _ := strconv.Atoi(maps_z[0][0].(string))
  145. return key
  146. }
  147. // 获取 发送统计数量
  148. func Get_WarningSandNum(TABLE_name string, Pid, Ntype int, StartTine, EndTime string) int {
  149. o := orm.NewOrm()
  150. var maps []orm.Params
  151. num, err := o.Raw("SELECT count(ID) AS count FROM " + TABLE_name + " WHERE `t_pid` = " + strconv.Itoa(Pid) + " AND `t__ntype` = " + strconv.Itoa(Ntype) + " AND `create_time` >= '" + StartTine + "' AND `create_time` <= '" + EndTime + "'").Values(&maps)
  152. if err == nil && num > 0 {
  153. intx, _ := strconv.Atoi(lib.To_string(maps[0]["count"]))
  154. logs.Println("Get_WarningSandNum:", TABLE_name, Pid, Ntype, StartTine, EndTime, intx) // slene
  155. return intx
  156. }
  157. logs.Error("执行错误!!!", "Get_WarningSandNum:", TABLE_name, Pid, Ntype, StartTine, EndTime, maps) // slene
  158. return 0
  159. }
  160. // 获取本月通知数量
  161. func Get_WarningSandNum_CurrentMonth(T_pid, Ntype int) int {
  162. o := orm.NewOrm()
  163. var maps []orm.Params
  164. tableName := fmt.Sprintf("warning_sand_%s", time.Now().Format("200601"))
  165. _, err := o.Raw("SELECT count(ID) AS count FROM " + tableName + " WHERE `t_pid` = " + strconv.Itoa(T_pid) + " AND `t__ntype` = " + strconv.Itoa(Ntype)).Values(&maps)
  166. if err != nil {
  167. logs.Error(lib.FuncName(), err)
  168. return 0
  169. }
  170. intx, _ := strconv.Atoi(lib.To_string(maps[0]["count"]))
  171. return intx
  172. }
  173. func Read_WarningSand_List(T_pid, Ntype int, page int, page_z int) (r []WarningSand_R, cnt int64) {
  174. o := orm.NewOrm()
  175. var offset int
  176. if page <= 1 {
  177. offset = 0
  178. } else {
  179. offset = (page - 1) * page_z
  180. }
  181. var maps []WarningSand
  182. var maps_z []orm2.ParamsList
  183. tableName := fmt.Sprintf("warning_sand_%s", time.Now().Format("200601"))
  184. sql := "SELECT count(ID) FROM " + tableName + " WHERE `t_pid` = " + strconv.Itoa(T_pid) + " AND `t__ntype` = " + strconv.Itoa(Ntype)
  185. _, err := o.Raw(sql).ValuesList(&maps_z)
  186. if err != nil {
  187. logs.Error(lib.FuncName(), err)
  188. return r, 0
  189. }
  190. if len(maps_z) == 0 {
  191. return r, 0
  192. }
  193. sql = "SELECT * FROM " + tableName + " WHERE `t_pid` = " + strconv.Itoa(T_pid) + " AND `t__ntype` = " + strconv.Itoa(Ntype)
  194. if page_z != 9999 {
  195. sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  196. }
  197. _, err = o.Raw(sql).QueryRows(&maps)
  198. if err != nil {
  199. logs.Error(lib.FuncName(), err)
  200. return
  201. }
  202. key, _ := strconv.ParseInt(maps_z[0][0].(string), 10, 64)
  203. for _, v := range maps {
  204. r = append(r, WarningSandToWarningSand_R(v))
  205. }
  206. return r, key
  207. }