CompanyNotice.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package Company
  2. import (
  3. "ColdP_server/conf"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/astaxie/beego/cache"
  7. _ "github.com/astaxie/beego/cache/redis"
  8. "github.com/beego/beego/v2/adapter/orm"
  9. orm2 "github.com/beego/beego/v2/client/orm"
  10. _ "github.com/go-sql-driver/mysql"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type CompanyNotice struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  18. T_name string `orm:"size(256);null"` // 分类
  19. T_Notice_wx string `orm:"type(text);null"` //w微信公众号 appid/名字|
  20. T_Notice_wx2 string `orm:"type(text);null"` //w微信公众号 appid/名字|
  21. T_Notice_phone string `orm:"type(text);null"` //p手机 1111111|
  22. T_Notice_message string `orm:"type(text);null"` //m短信 1111111|
  23. T_Notice_mailbox string `orm:"type(text);null"` //e邮箱 1111111|
  24. T_Notice_bind string `orm:"type(text);null"` // 绑定T_sn,Tid| 862289056463538,1|8622546456433,1|
  25. T_Notice_mechanism string `orm:"type(text);null"` // 报警机制
  26. // W报警编号,处理,w启用,数量,上限,~|
  27. // W15,0,0,0,0,0,0,0,0,0,0,0,0|
  28. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  29. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  30. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  31. }
  32. type CompanyNotice_R struct {
  33. T_name string `orm:"size(256);null"` // 分类
  34. T_Notice_wx string `orm:"type(text);null"` //w微信公众号 appid/名字|
  35. T_Notice_wx2 string `orm:"type(text);null"` //w微信公众号 appid/名字|
  36. T_Notice_phone string `orm:"type(text);null"` //p手机 1111111|
  37. T_Notice_message string `orm:"type(text);null"` //m短信 1111111|
  38. T_Notice_mailbox string `orm:"type(text);null"` //e邮箱 1111111|
  39. T_Notice_bind string `orm:"type(text);null"` // 绑定T_sn,Tid|
  40. T_Notice_mechanism string `orm:"type(text);null"` // 报警机制
  41. }
  42. func (t *CompanyNotice) TableName() string {
  43. return "company_notice" // 数据库名称 // ************** 替换 DesignClass **************
  44. }
  45. var redisCache_CompanyNotice cache.Cache
  46. func init() {
  47. //注册模型
  48. orm.RegisterModel(new(CompanyNotice))
  49. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  50. "redis_CompanyNotice", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  51. fmt.Println(config)
  52. var err error
  53. redisCache_CompanyNotice, err = cache.NewCache("redis", config)
  54. if err != nil || redisCache_CompanyNotice == nil {
  55. errMsg := "failed to init redis"
  56. fmt.Println(errMsg, err)
  57. panic(errMsg)
  58. }
  59. }
  60. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  61. func Redis_CompanyNotice_Set(r CompanyNotice) (err error) {
  62. key := strconv.Itoa(r.Id)
  63. //json序列化
  64. str, err := json.Marshal(r)
  65. if err != nil {
  66. fmt.Print(err)
  67. return
  68. }
  69. err = redisCache_CompanyNotice.Put(key, str, 2*time.Hour)
  70. if err != nil {
  71. fmt.Println("set key:", key, ",value:", str, err)
  72. }
  73. return
  74. }
  75. // if r,is :=Redis_Get(T_sn);is{
  76. // return r,nil
  77. // }
  78. func Redis_CompanyNotice_Get(key string) (CompanyNotice, bool) {
  79. println("找到key:", key)
  80. if redisCache_CompanyNotice.IsExist(key) {
  81. //println("找到key:",key)
  82. v := redisCache_CompanyNotice.Get(key)
  83. var r CompanyNotice
  84. json.Unmarshal(v.([]byte), &r)
  85. return r, true
  86. }
  87. return CompanyNotice{}, false
  88. }
  89. func Redis_CompanyNotice_DelK(key string) (err error) {
  90. err = redisCache_CompanyNotice.Delete(key)
  91. return
  92. }
  93. // ---------------- 特殊方法 -------------------
  94. func CompanyNoticeToCompanyNotice_R(t CompanyNotice) (r CompanyNotice_R) {
  95. r.T_name = t.T_name
  96. r.T_Notice_wx = t.T_Notice_wx
  97. r.T_Notice_wx2 = t.T_Notice_wx2
  98. r.T_Notice_phone = t.T_Notice_phone
  99. r.T_Notice_message = t.T_Notice_message
  100. r.T_Notice_mailbox = t.T_Notice_mailbox
  101. r.T_Notice_bind = t.T_Notice_bind
  102. r.T_Notice_mechanism = t.T_Notice_mechanism
  103. return r
  104. }
  105. // 获取 ById
  106. func Read_CompanyNotice_ById(id int) (r CompanyNotice, err error) {
  107. key := strconv.Itoa(id)
  108. if r, is := Redis_CompanyNotice_Get(key); is {
  109. //println("Redis_Get OK")
  110. return r, nil
  111. }
  112. o := orm.NewOrm()
  113. r = CompanyNotice{Id: id}
  114. err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  115. if err != nil {
  116. fmt.Println(err)
  117. return r, err
  118. }
  119. Redis_CompanyNotice_Set(r)
  120. return r, err
  121. }
  122. // 添加
  123. func Add_CompanyNotice(m CompanyNotice) (id int64, err error) {
  124. o := orm.NewOrm()
  125. id, err = o.Insert(&m)
  126. if err != nil {
  127. fmt.Println(err)
  128. }
  129. Redis_CompanyNotice_Set(m)
  130. return
  131. }
  132. // 删除
  133. func Delete_CompanyNotice_ById(id int) bool {
  134. o := orm.NewOrm()
  135. v := CompanyNotice{Id: id}
  136. // ascertain id exists in the database
  137. if err := o.Read(&v); err == nil {
  138. var num int64
  139. v.T_State = 0
  140. if num, err = o.Update(&v, "T_State"); err == nil {
  141. fmt.Println("Number of records updated in database:", num)
  142. key := strconv.Itoa(v.Id)
  143. Redis_CompanyNotice_DelK(key)
  144. return true
  145. }
  146. }
  147. return false
  148. }
  149. // 修改
  150. func Update_CompanyNotice(m CompanyNotice, cols ...string) bool {
  151. o := orm.NewOrm()
  152. if num, err := o.Update(&m, cols...); err == nil {
  153. fmt.Println("Number of records updated in database:", num)
  154. Redis_CompanyNotice_Set(m)
  155. return true
  156. }
  157. return false
  158. }
  159. // 删除
  160. func Delete_CompanyNotice_ByT_pid_All(T_pid int) {
  161. o := orm.NewOrm()
  162. qs := o.QueryTable(new(CompanyNotice))
  163. var r []CompanyNotice
  164. qs.Filter("T_pid", T_pid).Filter("T_State", 1).All(&r)
  165. for _, v := range r {
  166. v.T_State = 0
  167. if _, err := o.Update(&v, "T_State"); err == nil {
  168. Redis_CompanyNotice_DelK(strconv.Itoa(v.Id))
  169. }
  170. }
  171. }
  172. // 获取全部
  173. func Read_CompanyNotice_All_1() (r []CompanyNotice) {
  174. o := orm.NewOrm()
  175. qs := o.QueryTable(new(CompanyNotice))
  176. qs.Filter("T_State", 1).All(&r)
  177. return r
  178. }
  179. // 获取列表
  180. func Read_CompanyNotice_List(T_pid int, T_name string, page int, page_z int) (r []CompanyNotice_R, cnt int64) {
  181. o := orm.NewOrm()
  182. // 也可以直接使用 Model 结构体作为表名
  183. qs := o.QueryTable(new(CompanyNotice))
  184. var maps []CompanyNotice
  185. var offset int64
  186. if page_z == 0 {
  187. page_z = conf.Page_size
  188. }
  189. if page <= 1 {
  190. offset = 0
  191. } else {
  192. offset = int64((page - 1) * page_z)
  193. }
  194. 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)
  195. cnt, _ = qs.Filter("T_pid", T_pid).Filter("T_name__icontains", T_name).Filter("T_State", 1).Count()
  196. for _, v := range maps {
  197. r = append(r, CompanyNoticeToCompanyNotice_R(v))
  198. }
  199. return r, cnt
  200. }
  201. // 获取列表
  202. func Read_CompanyNotice_ALL_T_pid(T_pid int) (r []CompanyNotice) {
  203. o := orm.NewOrm()
  204. // 也可以直接使用 Model 结构体作为表名
  205. qs := o.QueryTable(new(CompanyNotice))
  206. qs.Filter("T_pid", T_pid).OrderBy("-Id").Filter("T_State", 1).All(&r)
  207. return r
  208. }
  209. // 获取全部列表
  210. func Read_CompanyNotice_All(T_pid int, T_name string) (r []CompanyNotice_R) {
  211. o := orm.NewOrm()
  212. // 也可以直接使用 Model 结构体作为表名
  213. var map_r []CompanyNotice
  214. qs := o.QueryTable(new(CompanyNotice))
  215. cond := orm.NewCondition()
  216. cond1 := cond.And("T_State", 1).And("T_pid", T_pid)
  217. if len(T_name) > 0 {
  218. cond1 = cond1.And("T_name", T_name)
  219. }
  220. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  221. for _, v := range map_r {
  222. r = append(r, CompanyNoticeToCompanyNotice_R(v))
  223. }
  224. return r
  225. }
  226. func Add_T_Notice_bind(T_sn string, T_id int, T_Notice_id int) (err error) {
  227. o := orm.NewOrm()
  228. v := CompanyNotice{Id: T_Notice_id}
  229. T_Notice_bind := T_sn + strconv.Itoa(T_id) + "|"
  230. if err = o.Read(&v, "Id"); err == nil {
  231. v.T_Notice_bind = strings.Replace(v.T_Notice_bind, T_Notice_bind, "", -1)
  232. v.T_Notice_bind = v.T_Notice_bind + T_Notice_bind
  233. o.Update(&v, "T_Notice_bind")
  234. }
  235. return err
  236. }
  237. func Delete_T_Notice_bind(T_sn string, T_id int, T_Notice_id int) (err error) {
  238. o := orm.NewOrm()
  239. v := CompanyNotice{Id: T_Notice_id}
  240. T_Notice_bind := T_sn + strconv.Itoa(T_id) + "|"
  241. if err = o.Read(&v, "Id"); err == nil {
  242. v.T_Notice_bind = strings.Replace(v.T_Notice_bind, T_Notice_bind, "", -1)
  243. o.Update(&v, "T_Notice_bind")
  244. }
  245. return err
  246. }