WarningSand.go 3.3 KB

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