123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- package Company
- import (
- "ColdP_server/conf"
- "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/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_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 {
- 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_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|
- T_Notice_mechanism string `orm:"type(text);null"` // 报警机制
- }
- 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"
- fmt.Println(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 {
- fmt.Print(err)
- return
- }
- err = redisCache_CompanyNotice.Put(key, str, 2*time.Hour)
- if err != nil {
- fmt.Println("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
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- return CompanyNotice{}, false
- }
- func Redis_CompanyNotice_DelK(key string) (err error) {
- err = redisCache_CompanyNotice.Delete(key)
- return
- }
- // ---------------- 特殊方法 -------------------
- func CompanyNoticeToCompanyNotice_R(t CompanyNotice) (r CompanyNotice_R) {
- 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_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 {
- fmt.Println(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 {
- fmt.Println(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
- if num, err = o.Update(&v, "T_State"); err == nil {
- fmt.Println("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) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- fmt.Println("Number of records updated in database:", num)
- Redis_CompanyNotice_Set(m)
- return true
- }
- return false
- }
- // 删除
- func Delete_CompanyNotice_ByT_pid_All(T_pid int) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(CompanyNotice))
- var r []CompanyNotice
- qs.Filter("T_pid", T_pid).Filter("T_State", 1).All(&r)
- 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_All_1() (r []CompanyNotice) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(CompanyNotice))
- qs.Filter("T_State", 1).All(&r)
- return r
- }
- // 获取列表
- 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_z == 0 {
- page_z = conf.Page_size
- }
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- 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)
- cnt, _ = qs.Filter("T_pid", T_pid).Filter("T_name__icontains", T_name).Filter("T_State", 1).Count()
- for _, v := range maps {
- r = append(r, CompanyNoticeToCompanyNotice_R(v))
- }
- return r, cnt
- }
- // 获取列表
- func Read_CompanyNotice_ALL_T_pid(T_pid int) (r []CompanyNotice) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(CompanyNotice))
- qs.Filter("T_pid", T_pid).OrderBy("-Id").Filter("T_State", 1).All(&r)
- return r
- }
- // 获取全部列表
- 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)
- }
- qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
- 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 := T_sn + strconv.Itoa(T_id) + "|"
- if err = o.Read(&v, "Id"); err == nil {
- 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
- o.Update(&v, "T_Notice_bind")
- }
- return err
- }
- 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 := T_sn + strconv.Itoa(T_id) + "|"
- if err = o.Read(&v, "Id"); err == nil {
- v.T_Notice_bind = strings.Replace(v.T_Notice_bind, T_Notice_bind, "", -1)
- o.Update(&v, "T_Notice_bind")
- }
- return err
- }
|