12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package Warning
- import (
- "Cold_mqtt/lib"
- "Cold_mqtt/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);"` // Account.Company 绑定公司, -1:未知 0:管理员 >0 :绑定公司
- T_Spid int `orm:"index;size(100);"` // 触发公司 ID
- T_tp int `orm:"size(20);"` // 报警类型 ->WarningList
- T_Ntype int `orm:"size(4);"` // 通知方式 0:微信 1:短信 ¥0.1 2:电话 ¥0.2
- T_Notice string `orm:"size(256);"` // 18888888888
- T_Remark string `orm:"type(text);"` // 备注
- //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 `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;"
- 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
- }
|