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