123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- package Company
- import (
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- "github.com/beego/beego/v2/core/logs"
- _ "github.com/go-sql-driver/mysql"
- "strconv"
- "strings"
- "time"
- )
- type CompanyNotice struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
- T_name string `orm:"size(256);null"` // 分类
- T_Notice_wx string `orm:"type(text);null"` //w微信公众号 appid/名字|
- T_Notice_wx2 string `orm:"type(text);null"` //w微信公众号 appid/名字|
- T_Notice_app string `orm:"type(text);null"` //app
- T_Notice_phone string `orm:"type(text);null"` //p手机 1111111|
- T_Notice_message string `orm:"type(text);null"` //m短信 1111111|
- T_Notice_mailbox string `orm:"type(text);null"` //e邮箱 1111111|
- T_Notice_bind string `orm:"type(text);null"` // 绑定T_sn,Tid| 862289056463538,1|8622546456433,1|
- T_Notice_mechanism string `orm:"type(text);null"` // 报警机制
- // W报警编号,处理,w启用,数量,上限,~|
- // W15,0,0,0,0,0,0,0,0,0,0,0,0|
- T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
- }
- type CompanyNotice_R struct {
- Id int
- T_name string // 分类
- T_Notice_wx string //w微信公众号 appid/名字|
- T_Notice_wx2 string //w微信公众号 appid/名字|
- T_Notice_app string //app
- T_Notice_phone string //p手机 1111111|
- T_Notice_message string //m短信 1111111|
- T_Notice_mailbox string //e邮箱 1111111|
- T_Notice_bind string // 绑定T_sn,Tid|
- T_Notice_mechanism string // 报警机制
- }
- func (t *CompanyNotice) TableName() string {
- return "company_notice" // 数据库名称 // ************** 替换 DesignClass **************
- }
- var redisCache_CompanyNotice cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(CompanyNotice))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_CompanyNotice", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_CompanyNotice, err = cache.NewCache("redis", config)
- if err != nil || redisCache_CompanyNotice == nil {
- errMsg := "failed to init redis"
- logs.Error(errMsg, err)
- panic(errMsg)
- }
- }
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_CompanyNotice_Set(r CompanyNotice) (err error) {
- key := strconv.Itoa(r.Id)
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_CompanyNotice.Put(key, str, 2*time.Hour)
- if err != nil {
- logs.Error("set key:", key, ",value:", str, err)
- }
- return
- }
- // if r,is :=Redis_Get(T_sn);is{
- // return r,nil
- // }
- func Redis_CompanyNotice_Get(key string) (CompanyNotice, bool) {
- println("找到key:", key)
- if redisCache_CompanyNotice.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_CompanyNotice.Get(key)
- var r CompanyNotice
- err := json.Unmarshal(v.([]byte), &r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, false
- }
- return r, true
- }
- return CompanyNotice{}, false
- }
- func Redis_CompanyNotice_DelK(key string) (err error) {
- err = redisCache_CompanyNotice.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // ---------------- 特殊方法 -------------------
- func CompanyNoticeToCompanyNotice_R(t CompanyNotice) (r CompanyNotice_R) {
- r.Id = t.Id
- r.T_name = t.T_name
- r.T_Notice_wx = t.T_Notice_wx
- r.T_Notice_wx2 = t.T_Notice_wx2
- r.T_Notice_app = t.T_Notice_app
- r.T_Notice_phone = t.T_Notice_phone
- r.T_Notice_message = t.T_Notice_message
- r.T_Notice_mailbox = t.T_Notice_mailbox
- r.T_Notice_bind = t.T_Notice_bind
- r.T_Notice_mechanism = t.T_Notice_mechanism
- return r
- }
- // 获取 ById
- func Read_CompanyNotice_ById(id int) (r CompanyNotice, err error) {
- key := strconv.Itoa(id)
- if r, is := Redis_CompanyNotice_Get(key); is {
- //println("Redis_Get OK")
- return r, nil
- }
- o := orm.NewOrm()
- r = CompanyNotice{Id: id}
- err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, err
- }
- Redis_CompanyNotice_Set(r)
- return r, err
- }
- // 添加
- func Add_CompanyNotice(m CompanyNotice) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return id, err
- }
- Redis_CompanyNotice_Set(m)
- return
- }
- // 删除
- func Delete_CompanyNotice_ById(id int) bool {
- o := orm.NewOrm()
- v := CompanyNotice{Id: id}
- // ascertain id exists in the database
- if err := o.Read(&v); err == nil {
- var num int64
- v.T_State = 0
- num, err = o.Update(&v, "T_State")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false
- }
- logs.Info("Number of records updated in database:", num)
- key := strconv.Itoa(v.Id)
- Redis_CompanyNotice_DelK(key)
- return true
- }
- return false
- }
- // 修改
- func Update_CompanyNotice(m CompanyNotice, cols ...string) error {
- o := orm.NewOrm()
- num, err := o.Update(&m, cols...)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- logs.Info("Number of records updated in database:", num)
- Redis_CompanyNotice_Set(m)
- return nil
- }
- // 删除
- func Delete_CompanyNotice_ByT_pid_All(T_pid int) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(CompanyNotice))
- var r []CompanyNotice
- _, err := qs.Filter("T_pid", T_pid).Filter("T_State", 1).All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range r {
- v.T_State = 0
- if _, err := o.Update(&v, "T_State"); err == nil {
- Redis_CompanyNotice_DelK(strconv.Itoa(v.Id))
- }
- }
- }
- // 获取列表
- func Read_CompanyNotice_List(T_pid int, T_name string, page int, page_z int) (r []CompanyNotice_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(CompanyNotice))
- var maps []CompanyNotice
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- _, err := qs.Limit(conf.Page_size, offset).Filter("T_pid", T_pid).Filter("T_name__icontains", T_name).Filter("T_State", 1).OrderBy("-Id").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- cnt, err = qs.Filter("T_pid", T_pid).Filter("T_name__icontains", T_name).Filter("T_State", 1).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range maps {
- r = append(r, CompanyNoticeToCompanyNotice_R(v))
- }
- return r, cnt
- }
- // 获取全部列表
- func Read_CompanyNotice_All(T_pid int, T_name string) (r []CompanyNotice_R) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- var map_r []CompanyNotice
- qs := o.QueryTable(new(CompanyNotice))
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_pid", T_pid)
- if len(T_name) > 0 {
- cond1 = cond1.And("T_name", T_name)
- }
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range map_r {
- r = append(r, CompanyNoticeToCompanyNotice_R(v))
- }
- return r
- }
- func Add_T_Notice_bind(T_sn string, T_id int, T_Notice_id int) (err error) {
- o := orm.NewOrm()
- v := CompanyNotice{Id: T_Notice_id}
- T_Notice_bind := fmt.Sprintf("%s,%d|", T_sn, T_id)
- err = o.Read(&v, "Id")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- v.T_Notice_bind = strings.Replace(v.T_Notice_bind, T_Notice_bind, "", -1)
- v.T_Notice_bind = v.T_Notice_bind + T_Notice_bind
- _, err = o.Update(&v, "T_Notice_bind")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- Redis_CompanyNotice_Set(v)
- return nil
- }
- func Delete_T_Notice_bind(T_sn string, T_id int, T_Notice_id int) (err error) {
- o := orm.NewOrm()
- v := CompanyNotice{Id: T_Notice_id}
- T_Notice_bind := fmt.Sprintf("%s,%d|", T_sn, T_id)
- err = o.Read(&v, "Id")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- v.T_Notice_bind = strings.Replace(v.T_Notice_bind, T_Notice_bind, "", -1)
- _, err = o.Update(&v, "T_Notice_bind")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- Redis_CompanyNotice_Set(v)
- return nil
- }
- // 修改用户同时修改报警策略绑定的用户信息
- func Update_CompanyNotice_Bind_By_T_uuid(T_uuid, T_name string) bool {
- o := orm.NewOrm()
- o.Begin()
- // 也可以直接使用 Model 结构体作为表名
- var map_r []CompanyNotice
- qs := o.QueryTable(new(CompanyNotice))
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).AndCond(
- cond.Or("T_Notice_wx__icontains", T_uuid).
- Or("T_Notice_wx2__icontains", T_uuid).
- Or("T_Notice_app__icontains", T_uuid).
- Or("T_Notice_phone__icontains", T_uuid).
- Or("T_Notice_message__icontains", T_uuid).
- Or("T_Notice_mailbox__icontains", T_uuid))
- _, err := qs.SetCond((*orm2.Condition)(cond1)).All(&map_r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- o.Rollback()
- return false
- }
- T_uuid_name := fmt.Sprintf("%s/%s|", T_uuid, T_name)
- for _, v := range map_r {
- v.T_Notice_wx = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx, T_uuid_name)
- v.T_Notice_wx2 = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx2, T_uuid_name)
- v.T_Notice_app = Replice_T_Notice_Bind(T_uuid, v.T_Notice_app, T_uuid_name)
- v.T_Notice_phone = Replice_T_Notice_Bind(T_uuid, v.T_Notice_phone, T_uuid_name)
- v.T_Notice_message = Replice_T_Notice_Bind(T_uuid, v.T_Notice_message, T_uuid_name)
- v.T_Notice_mailbox = Replice_T_Notice_Bind(T_uuid, v.T_Notice_mailbox, T_uuid_name)
- _, err = o.Update(&v, "T_Notice_wx", "T_Notice_wx2", "T_Notice_app", "T_Notice_phone", "T_Notice_message", "T_Notice_mailbox", "T_Notice_mechanism")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- o.Rollback()
- return false
- }
- Redis_CompanyNotice_DelK(strconv.Itoa(v.Id))
- }
- o.Commit()
- return true
- }
- // 删除用户同时删除报警策略绑定的用户信息
- func Delete_CompanyNotice_Bind_By_T_uuid(T_uuid string) bool {
- o := orm.NewOrm()
- o.Begin()
- // 也可以直接使用 Model 结构体作为表名
- var map_r []CompanyNotice
- qs := o.QueryTable(new(CompanyNotice))
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).AndCond(
- cond.Or("T_Notice_wx__icontains", T_uuid).
- Or("T_Notice_wx2__icontains", T_uuid).
- Or("T_Notice_app__icontains", T_uuid).
- Or("T_Notice_phone__icontains", T_uuid).
- Or("T_Notice_message__icontains", T_uuid).
- Or("T_Notice_mailbox__icontains", T_uuid))
- _, err := qs.SetCond((*orm2.Condition)(cond1)).All(&map_r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- o.Rollback()
- return false
- }
- for _, v := range map_r {
- v.T_Notice_wx = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx, "")
- v.T_Notice_wx2 = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx2, "")
- v.T_Notice_app = Replice_T_Notice_Bind(T_uuid, v.T_Notice_app, "")
- v.T_Notice_phone = Replice_T_Notice_Bind(T_uuid, v.T_Notice_phone, "")
- v.T_Notice_message = Replice_T_Notice_Bind(T_uuid, v.T_Notice_message, "")
- v.T_Notice_mailbox = Replice_T_Notice_Bind(T_uuid, v.T_Notice_mailbox, "")
- _, err := o.Update(&v, "T_Notice_wx", "T_Notice_wx2", "T_Notice_app", "T_Notice_phone", "T_Notice_message", "T_Notice_mailbox", "T_Notice_mechanism")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- o.Rollback()
- return false
- }
- Redis_CompanyNotice_DelK(strconv.Itoa(v.Id))
- }
- o.Commit()
- return true
- }
- // 删除报警策略
- func Delete_CompanyNotice_Bind_By_T_uuid_T_pid(T_uuid string, T_pids []int) bool {
- o := orm.NewOrm()
- o.Begin()
- // 也可以直接使用 Model 结构体作为表名
- var map_r []CompanyNotice
- qs := o.QueryTable(new(CompanyNotice))
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_pid__in", T_pids).AndCond(
- cond.Or("T_Notice_wx__icontains", T_uuid).
- Or("T_Notice_wx2__icontains", T_uuid).
- Or("T_Notice_app__icontains", T_uuid).
- Or("T_Notice_phone__icontains", T_uuid).
- Or("T_Notice_message__icontains", T_uuid).
- Or("T_Notice_mailbox__icontains", T_uuid))
- _, err := qs.SetCond((*orm2.Condition)(cond1)).All(&map_r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- o.Rollback()
- return false
- }
- for _, v := range map_r {
- v.T_Notice_wx = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx, "")
- v.T_Notice_wx2 = Replice_T_Notice_Bind(T_uuid, v.T_Notice_wx2, "")
- v.T_Notice_app = Replice_T_Notice_Bind(T_uuid, v.T_Notice_app, "")
- v.T_Notice_phone = Replice_T_Notice_Bind(T_uuid, v.T_Notice_phone, "")
- v.T_Notice_message = Replice_T_Notice_Bind(T_uuid, v.T_Notice_message, "")
- v.T_Notice_mailbox = Replice_T_Notice_Bind(T_uuid, v.T_Notice_mailbox, "")
- _, err := o.Update(&v, "T_Notice_wx", "T_Notice_wx2", "T_Notice_app", "T_Notice_phone", "T_Notice_message", "T_Notice_mailbox", "T_Notice_mechanism")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- o.Rollback()
- return false
- }
- Redis_CompanyNotice_DelK(strconv.Itoa(v.Id))
- }
- o.Commit()
- return true
- }
- func Replice_T_Notice_Bind(T_uuid, T_Notice_Bind string, new string) string {
- if len(T_Notice_Bind) == 0 {
- return ""
- }
- list := strings.Split(T_Notice_Bind, "|")
- for _, v := range list {
- if strings.Contains(v, T_uuid) {
- T_Notice_Bind = strings.Replace(T_Notice_Bind, v+"|", new, -1)
- break
- }
- }
- return T_Notice_Bind
- }
|