CompanyNotice.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package Company
  2. import (
  3. "Cold_Api/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. Id int
  34. T_name string // 分类
  35. T_Notice_wx string //w微信公众号 appid/名字|
  36. T_Notice_wx2 string //w微信公众号 appid/名字|
  37. T_Notice_phone string //p手机 1111111|
  38. T_Notice_message string //m短信 1111111|
  39. T_Notice_mailbox string //e邮箱 1111111|
  40. T_Notice_bind string // 绑定T_sn,Tid|
  41. T_Notice_mechanism string // 报警机制
  42. }
  43. func (t *CompanyNotice) TableName() string {
  44. return "company_notice" // 数据库名称 // ************** 替换 DesignClass **************
  45. }
  46. var redisCache_CompanyNotice cache.Cache
  47. func init() {
  48. //注册模型
  49. orm.RegisterModel(new(CompanyNotice))
  50. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  51. "redis_CompanyNotice", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  52. fmt.Println(config)
  53. var err error
  54. redisCache_CompanyNotice, err = cache.NewCache("redis", config)
  55. if err != nil || redisCache_CompanyNotice == nil {
  56. errMsg := "failed to init redis"
  57. fmt.Println(errMsg, err)
  58. panic(errMsg)
  59. }
  60. }
  61. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  62. func Redis_CompanyNotice_Set(r CompanyNotice) (err error) {
  63. key := strconv.Itoa(r.Id)
  64. //json序列化
  65. str, err := json.Marshal(r)
  66. if err != nil {
  67. fmt.Print(err)
  68. return
  69. }
  70. err = redisCache_CompanyNotice.Put(key, str, 2*time.Hour)
  71. if err != nil {
  72. fmt.Println("set key:", key, ",value:", str, err)
  73. }
  74. return
  75. }
  76. // if r,is :=Redis_Get(T_sn);is{
  77. // return r,nil
  78. // }
  79. func Redis_CompanyNotice_Get(key string) (CompanyNotice, bool) {
  80. println("找到key:", key)
  81. if redisCache_CompanyNotice.IsExist(key) {
  82. //println("找到key:",key)
  83. v := redisCache_CompanyNotice.Get(key)
  84. var r CompanyNotice
  85. json.Unmarshal(v.([]byte), &r)
  86. return r, true
  87. }
  88. return CompanyNotice{}, false
  89. }
  90. func Redis_CompanyNotice_DelK(key string) (err error) {
  91. err = redisCache_CompanyNotice.Delete(key)
  92. return
  93. }
  94. // ---------------- 特殊方法 -------------------
  95. func CompanyNoticeToCompanyNotice_R(t CompanyNotice) (r CompanyNotice_R) {
  96. r.Id = t.Id
  97. r.T_name = t.T_name
  98. r.T_Notice_wx = t.T_Notice_wx
  99. r.T_Notice_wx2 = t.T_Notice_wx2
  100. r.T_Notice_phone = t.T_Notice_phone
  101. r.T_Notice_message = t.T_Notice_message
  102. r.T_Notice_mailbox = t.T_Notice_mailbox
  103. r.T_Notice_bind = t.T_Notice_bind
  104. r.T_Notice_mechanism = t.T_Notice_mechanism
  105. return r
  106. }
  107. // 获取 ById
  108. func Read_CompanyNotice_ById(id int) (r CompanyNotice, err error) {
  109. key := strconv.Itoa(id)
  110. if r, is := Redis_CompanyNotice_Get(key); is {
  111. //println("Redis_Get OK")
  112. return r, nil
  113. }
  114. o := orm.NewOrm()
  115. r = CompanyNotice{Id: id}
  116. err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  117. if err != nil {
  118. fmt.Println(err)
  119. return r, err
  120. }
  121. Redis_CompanyNotice_Set(r)
  122. return r, err
  123. }
  124. // 添加
  125. func Add_CompanyNotice(m CompanyNotice) (id int64, err error) {
  126. o := orm.NewOrm()
  127. id, err = o.Insert(&m)
  128. if err != nil {
  129. fmt.Println(err)
  130. }
  131. Redis_CompanyNotice_Set(m)
  132. return
  133. }
  134. // 删除
  135. func Delete_CompanyNotice_ById(id int) bool {
  136. o := orm.NewOrm()
  137. v := CompanyNotice{Id: id}
  138. // ascertain id exists in the database
  139. if err := o.Read(&v); err == nil {
  140. var num int64
  141. v.T_State = 0
  142. if num, err = o.Update(&v, "T_State"); err == nil {
  143. fmt.Println("Number of records updated in database:", num)
  144. key := strconv.Itoa(v.Id)
  145. Redis_CompanyNotice_DelK(key)
  146. return true
  147. }
  148. }
  149. return false
  150. }
  151. // 修改
  152. func Update_CompanyNotice(m CompanyNotice, cols ...string) bool {
  153. o := orm.NewOrm()
  154. if num, err := o.Update(&m, cols...); err == nil {
  155. fmt.Println("Number of records updated in database:", num)
  156. Redis_CompanyNotice_Set(m)
  157. return true
  158. }
  159. return false
  160. }
  161. // 删除
  162. func Delete_CompanyNotice_ByT_pid_All(T_pid int) {
  163. o := orm.NewOrm()
  164. qs := o.QueryTable(new(CompanyNotice))
  165. var r []CompanyNotice
  166. qs.Filter("T_pid", T_pid).Filter("T_State", 1).All(&r)
  167. for _, v := range r {
  168. v.T_State = 0
  169. if _, err := o.Update(&v, "T_State"); err == nil {
  170. Redis_CompanyNotice_DelK(strconv.Itoa(v.Id))
  171. }
  172. }
  173. }
  174. // 获取全部
  175. func Read_CompanyNotice_All_1() (r []CompanyNotice) {
  176. o := orm.NewOrm()
  177. qs := o.QueryTable(new(CompanyNotice))
  178. qs.Filter("T_State", 1).All(&r)
  179. return r
  180. }
  181. // 获取列表
  182. func Read_CompanyNotice_List(T_pid int, T_name string, page int, page_z int) (r []CompanyNotice_R, cnt int64) {
  183. o := orm.NewOrm()
  184. // 也可以直接使用 Model 结构体作为表名
  185. qs := o.QueryTable(new(CompanyNotice))
  186. var maps []CompanyNotice
  187. var offset int64
  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((*orm2.Condition)(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. }