1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package Warning
- import (
- "Cold_DeductionNotice/lib"
- "Cold_DeductionNotice/logs"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- "strconv"
- "strings"
- "time"
- )
- // 报警发送统计
- type WarningSand struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_pid int `orm:"index;size(100);null"` // Account.Company 绑定公司, -1:未知 0:管理员 >0 :绑定公司
- T_tp int `orm:"size(20);null"` // 报警类型 ->WarningList
- T_Ntype int `orm:"size(4);null"` // 通知方式 0:微信 1:短信 ¥0.1 2:电话 ¥0.2
- T_Notice string `orm:"size(256);null"` // 18888888888
- T_Remark string `orm:"type(text);null"` // 备注
- //CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
- }
- func (t *WarningSand) TableName() string {
- return "warning_sand" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(WarningSand))
- }
- func CREATE(name string) string {
- 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;"
- sql = strings.Replace(sql, "warning_sand", name, -1)
- return sql
- }
- // 添加 数据 返回 真:替换 假:第一条无替换
- func Add_WarningSand(v WarningSand) bool {
- o := orm.NewOrm()
- currentTime := time.Now()
- TABLE_name := fmt.Sprintf("warning_sand_%d%02d", currentTime.Year(), currentTime.Month())
- breakHere:
- // 开始插入数据
- sql := "INSERT INTO " + TABLE_name + "(`t_pid`, `t_tp`, `t__ntype`, `t__notice`, `t__remark`, `create_time`) " +
- "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") + "')"
- // 这里有时间优化 用于一次 prepare 多次 exec,以提高批量执行的速度
- logs.Println(sql)
- _, err := o.Raw(sql).Exec()
- if err != nil {
- //Error 1146: Table 'cold.z_device_data_202335objgv2pzk' doesn't exist
- if strings.Contains(err.Error(), "1146") { // 没有创建表
- _, err = o.Raw(CREATE(TABLE_name)).Exec()
- if err != nil {
- logs.PrintlnError("SELECT_Add_WarningSand:" + err.Error())
- return false
- }
- goto breakHere // 跳转到标签
- }
- }
- return true
- }
- // 获取 发送统计数量
- func Get_WarningSandNum(TABLE_name string,Pid,Ntype int,StartTine,EndTime string ) int {
- o := orm.NewOrm()
- var maps []orm.Params
- 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)
- if err == nil && num > 0 {
- intx,_:=strconv.Atoi(lib.To_string(maps[0]["count"]))
- logs.Println("Get_WarningSandNum:",TABLE_name ,Pid,Ntype ,StartTine,EndTime,intx) // slene
- return intx
- }
- logs.PrintlnError("执行错误!!!","Get_WarningSandNum:",TABLE_name ,Pid,Ntype ,StartTine,EndTime,maps) // slene
- return 0
- }
|