WarningSand.go 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package Warning
  2. import (
  3. "Cold_mqtt/lib"
  4. "Cold_mqtt/logs"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. _ "github.com/go-sql-driver/mysql"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // 报警发送统计
  13. type WarningSand struct {
  14. Id int `orm:"column(ID);size(11);auto;pk"`
  15. T_pid int `orm:"index;size(100);"` // Account.Company 绑定公司, -1:未知 0:管理员 >0 :绑定公司
  16. T_Spid int `orm:"index;size(100);"` // 触发公司 ID
  17. T_tp int `orm:"size(20);"` // 报警类型 ->WarningList
  18. T_Ntype int `orm:"size(4);"` // 通知方式 0:微信 1:短信 ¥0.1 2:电话 ¥0.2
  19. T_Notice string `orm:"size(256);"` // 18888888888
  20. T_Remark string `orm:"type(text);"` // 备注
  21. //CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  22. }
  23. func (t *WarningSand) TableName() string {
  24. return "warning_sand" // 数据库名称 // ************** 替换 FormulaList **************
  25. }
  26. func init() {
  27. //注册模型
  28. orm.RegisterModel(new(WarningSand))
  29. }
  30. func CREATE(name string) string {
  31. sql := "CREATE TABLE `warning_sand` (\n `ID` int NOT NULL AUTO_INCREMENT,\n `t_pid` int NULL DEFAULT NULL,\n `t_tp` int NULL DEFAULT NULL,\n `t__notice` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n `t_type` int NULL DEFAULT NULL,\n `t__remark` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n `create_time` datetime NULL DEFAULT NULL,\n `t__ntype` int NOT NULL DEFAULT 0,\n `t__spid` int NOT NULL DEFAULT 0,\n PRIMARY KEY (`ID`) USING BTREE,\n INDEX `warning_sand_t_pid`(`t_pid`) USING BTREE,\n INDEX `warning_sand_t_tp`(`t_tp`) USING BTREE,\n INDEX `warning_sand_t_type`(`t_type`) USING BTREE,\n INDEX `warning_sand_t__spid`(`t__spid`) USING BTREE\n) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;"
  32. sql = strings.Replace(sql, "warning_sand", name, -1)
  33. return sql
  34. }
  35. // 添加 数据 返回 真:替换 假:第一条无替换
  36. func Add_WarningSand(v WarningSand) bool {
  37. o := orm.NewOrm()
  38. currentTime := time.Now()
  39. TABLE_name := fmt.Sprintf("warning_sand_%d%02d", currentTime.Year(), currentTime.Month())
  40. breakHere:
  41. // 开始插入数据
  42. sql := "INSERT INTO " + TABLE_name + "(`t_pid`, `t_tp`, `t__ntype`, `t__notice`, `t__remark`, `create_time`) " +
  43. "VALUES (" + strconv.Itoa(v.T_pid) + " , " + strconv.Itoa(v.T_tp) + ", " + strconv.Itoa(v.T_Ntype) + ", '" + v.T_Notice + "', '" + v.T_Remark + "', '" + currentTime.Format("2006-01-02 15:04:05") + "')"
  44. // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
  45. logs.Println(sql)
  46. _, err := o.Raw(sql).Exec()
  47. if err != nil {
  48. //Error 1146: Table 'cold.z_device_data_202335objgv2pzk' doesn't exist
  49. if strings.Contains(err.Error(), "1146") { // 没有创建表
  50. _, err = o.Raw(CREATE(TABLE_name)).Exec()
  51. if err != nil {
  52. logs.PrintlnError("SELECT_Add_WarningSand:" + err.Error())
  53. return false
  54. }
  55. goto breakHere // 跳转到标签
  56. }
  57. }
  58. return true
  59. }
  60. // 获取 发送统计数量
  61. func Get_WarningSandNum(TABLE_name string, Pid, Ntype int, StartTine, EndTime string) int {
  62. o := orm.NewOrm()
  63. var maps []orm.Params
  64. 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)
  65. if err == nil && num > 0 {
  66. intx, _ := strconv.Atoi(lib.To_string(maps[0]["count"]))
  67. logs.Println("Get_WarningSandNum:", TABLE_name, Pid, Ntype, StartTine, EndTime, intx) // slene
  68. return intx
  69. }
  70. logs.PrintlnError("执行错误!!!", "Get_WarningSandNum:", TABLE_name, Pid, Ntype, StartTine, EndTime, maps) // slene
  71. return 0
  72. }