123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- package Warning
- import (
- "ColdP_server/controllers/lib"
- "ColdP_server/logs"
- "ColdP_server/models/Account"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- orm2 "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_Spid int `orm:"index;size(100);"` // 触发公司 ID
- T_tp int `orm:"index;size(20);null"` // 报警类型 ->WarningList
- T_Notice string `orm:"size(256);null"` // 18888888888
- T_type int `orm:"index;size(4);null"` // 1 短信 2 电话
- T_Remark string `orm:"size(256);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))
- }
- type WarningSand_R struct {
- Id int
- T_pid int // Account.Company 绑定公司, -1:未知 0:管理员 >0 :绑定公司
- T_tp int // 报警类型 ->WarningList
- T_tp_name string
- T_Notice string // 18888888888
- T_type int // 1 短信 2 电话
- T_Remark []string // 备注
- CreateTime string //auto_now_add 第一次保存时才设置时间
- }
- func WarningSandToWarningSand_R(t WarningSand) (r WarningSand_R) {
- r.Id = t.Id
- r.T_pid = t.T_pid
- r.T_tp = t.T_tp
- r.T_tp_name = Read_WarningType_Get(t.T_tp)
- r.T_Notice = t.T_Notice
- r.T_type = t.T_type
- //r.T_Remark = t.T_Remark
- if len(t.T_Remark) > 0 {
- r.T_Remark = strings.Split(strings.TrimRight(t.T_Remark, "\n"), "\n")
- }
- r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
- return r
- }
- // ---------------- 特殊方法 -------------------
- // 获取 ById
- func Read_WarningWx_ById(id int) (r WarningSand) {
- o := orm.NewOrm()
- r = WarningSand{Id: id}
- err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- fmt.Println(err)
- }
- return r
- }
- // 添加
- func Add_WarningHand(m WarningSand) {
- warningWxList := Read_DeviceWarning(m)
- if len(warningWxList) == 0 {
- Add_WarningHand(m)
- }
- }
- // 添加
- func Add_WarningSand(m WarningSand) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- //if err != nil {
- // fmt.Println(err)
- //}
- return id, err
- }
- // 获取列表
- func Read_DeviceWarning(Warning WarningSand) (r []WarningSand) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(WarningSand))
- now := time.Now()
- // N 个小时前
- dd, _ := time.ParseDuration("-1m") //"s", "m", "h".
- currentTime := now.Add(dd)
- 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)
- // 记录 ++
- if len(r) > 0 {
- ColAdd_DeviceWarningBy_ID(r[0].Id)
- }
- return r
- }
- // 获取时 累加 1
- func ColAdd_DeviceWarningBy_ID(id int) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- design := new(WarningSand)
- qs := o.QueryTable(design)
- // 获取时 累加 1
- qs.Filter("Id", id).Update(orm2.Params{"T_Filter": orm2.ColValue(orm.ColAdd, 1)})
- return
- }
- func Read_WarningSand_ALL_T_sn_TIME_Count(T_sn string) (cnt int) {
- o := orm.NewOrm()
- var maps_z []orm2.ParamsList
- now := time.Now()
- timeStr := now.Format("2006-01-02") + " 00:00:00"
- sql := "SELECT COUNT(ID) FROM warning_sand WHERE t__title NOT LIKE '%恢复%' AND t__ut > '" + timeStr + "' AND t_sn LIKE '%" + T_sn + "%' "
- fmt.Println(sql)
- _, err := o.Raw(sql).ValuesList(&maps_z)
- if err != nil {
- return 0
- }
- if len(maps_z) == 0 {
- return 0
- }
- //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
- key, _ := strconv.Atoi(maps_z[0][0].(string))
- return key
- }
- func Read_WarningSand_ALL_T_Bind_TIME_Count(user_ Account.Admin, T_sn string) (cnt int) {
- o := orm.NewOrm()
- var maps_z []orm2.ParamsList
- now := time.Now()
- timeStr := now.Format("2006-01-02") + " 00:00:00"
- T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- 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 + "%'"
- fmt.Println(sql)
- _, err := o.Raw(sql).ValuesList(&maps_z)
- if err != nil {
- return 0
- }
- if len(maps_z) == 0 {
- return 0
- }
- //value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
- key, _ := strconv.Atoi(maps_z[0][0].(string))
- return key
- }
- // 获取 发送统计数量
- 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.Error("执行错误!!!", "Get_WarningSandNum:", TABLE_name, Pid, Ntype, StartTine, EndTime, maps) // slene
- return 0
- }
- // 获取本月通知数量
- func Get_WarningSandNum_CurrentMonth(T_pid, Ntype int) int {
- o := orm.NewOrm()
- var maps []orm.Params
- tableName := fmt.Sprintf("warning_sand_%s", time.Now().Format("200601"))
- _, 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)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return 0
- }
- intx, _ := strconv.Atoi(lib.To_string(maps[0]["count"]))
- return intx
- }
- func Read_WarningSand_List(T_pid, Ntype int, page int, page_z int) (r []WarningSand_R, cnt int64) {
- o := orm.NewOrm()
- var offset int
- if page <= 1 {
- offset = 0
- } else {
- offset = (page - 1) * page_z
- }
- var maps []WarningSand
- var maps_z []orm2.ParamsList
- tableName := fmt.Sprintf("warning_sand_%s", time.Now().Format("200601"))
- sql := "SELECT count(ID) FROM " + tableName + " WHERE `t_pid` = " + strconv.Itoa(T_pid) + " AND `t__ntype` = " + strconv.Itoa(Ntype)
- _, err := o.Raw(sql).ValuesList(&maps_z)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, 0
- }
- if len(maps_z) == 0 {
- return r, 0
- }
- sql = "SELECT * FROM " + tableName + " WHERE `t_pid` = " + strconv.Itoa(T_pid) + " AND `t__ntype` = " + strconv.Itoa(Ntype)
- if page_z != 9999 {
- sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
- }
- _, err = o.Raw(sql).QueryRows(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- key, _ := strconv.ParseInt(maps_z[0][0].(string), 10, 64)
- for _, v := range maps {
- r = append(r, WarningSandToWarningSand_R(v))
- }
- return r, key
- }
|