bulletin.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package Announcement
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. "github.com/beego/beego/v2/adapter/orm"
  9. orm2 "github.com/beego/beego/v2/client/orm"
  10. "github.com/beego/beego/v2/core/logs"
  11. "strconv"
  12. "sync"
  13. "time"
  14. )
  15. type Bulletin struct {
  16. Id int `orm:"column(ID);size(11);auto;pk" json:"id"`
  17. T_uuid string `orm:"size(256);null"`
  18. BulletinId string `orm:"size(256);null" json:"bulletinId"` // 公告id
  19. Title string `orm:"size(256);null" json:"title"` // 标题
  20. Content string `orm:"size(256);null" json:"content"` // 内容
  21. State int `orm:"size(2);default(0)" json:"state"` // 状态 0 可查看 1 不可查看
  22. IsShow int `orm:"size(2);default(0)" json:"IsShow"` // 是否查看 0 未查看 1 已查看
  23. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` // auto_now 每次 model 保存时都会对时间自动更新
  24. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` // auto_now_add 第一次保存时才设置时间
  25. }
  26. func (ab *Bulletin) MarshalJSON() ([]byte, error) {
  27. loc, _ := time.LoadLocation("Asia/Shanghai")
  28. type Alias Bulletin
  29. return json.Marshal(&struct {
  30. CreateTime string `json:"CreateTime,omitempty"`
  31. UpdateTime string `json:"UpdateTime,omitempty"`
  32. *Alias
  33. }{
  34. CreateTime: ab.CreateTime.In(loc).Format("2006-01-02 15:04:05"),
  35. UpdateTime: ab.UpdateTime.In(loc).Format("2006-01-02 15:04:05"),
  36. Alias: (*Alias)(ab),
  37. })
  38. }
  39. func (n *Bulletin) TableName() string {
  40. return "bulletin"
  41. }
  42. var redisCache_bulletin cache.Cache
  43. var Notice_map *sync.Map
  44. func init() {
  45. //注册模型
  46. Notice_map = new(sync.Map)
  47. //注册模型
  48. orm.RegisterModel(new(Bulletin))
  49. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  50. "redis_Notice", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  51. fmt.Println(config)
  52. var err error
  53. redisCache_bulletin, err = cache.NewCache("redis", config)
  54. if err != nil || redisCache_bulletin == nil {
  55. errMsg := "failed to init redis"
  56. logs.Error(errMsg, err)
  57. panic(errMsg)
  58. }
  59. }
  60. // ---------------- Redis -------------------
  61. func Redis_Notice_Set(r Bulletin) (err error) {
  62. //json序列化
  63. str, err := json.Marshal(r)
  64. if err != nil {
  65. logs.Error(lib.FuncName(), err)
  66. return
  67. }
  68. uptime := r.UpdateTime.Format("2006-01-02 15:04:05")
  69. key := uptime + r.Title
  70. err = redisCache_bulletin.Put(key, str, 24*time.Hour)
  71. if err != nil {
  72. logs.Error("set key:", key, ",value:", str, err)
  73. }
  74. return
  75. }
  76. func Redis_Notice_Get(b Bulletin) (r Bulletin, is bool) {
  77. uptime := b.UpdateTime.Format("2006-01-02 15:04:05")
  78. key := uptime + b.Title
  79. if redisCache_bulletin.IsExist(key) {
  80. //println("找到key:",key)
  81. v := redisCache_bulletin.Get(key)
  82. err := json.Unmarshal(v.([]byte), &r)
  83. if err != nil {
  84. logs.Error(lib.FuncName(), err)
  85. return Bulletin{}, false
  86. }
  87. return r, true
  88. }
  89. //println("没有 找到key:",key)
  90. return Bulletin{}, false
  91. }
  92. func Redis_Company_DelK(b Bulletin) (err error) {
  93. uptime := b.UpdateTime.Format("2006-01-02 15:04:05")
  94. key := uptime + b.Title
  95. err = redisCache_bulletin.Delete(key)
  96. if err != nil {
  97. logs.Error(lib.FuncName(), err)
  98. }
  99. return
  100. }
  101. // ---------------- 特殊方法 -------------------
  102. // 添加公告
  103. func AddBulletin(n Bulletin) (id int64, err error) {
  104. o := orm.NewOrm()
  105. id, err = o.Insert(&n)
  106. if err != nil {
  107. logs.Error(lib.FuncName(), err)
  108. return
  109. }
  110. n.Id = int(id)
  111. Redis_Notice_Set(n)
  112. return id, err
  113. }
  114. // GetBulletinById 根据id查询公告
  115. func GetBulletinById(id, t_uuid string) (bulletin Bulletin, err error) {
  116. o := orm.NewOrm()
  117. query := o.QueryTable(&bulletin)
  118. err = query.Filter("ID", id).Filter("State", 0).Filter("t_uuid", t_uuid).One(&bulletin)
  119. if err != nil {
  120. logs.Error(lib.FuncName(), err)
  121. return
  122. }
  123. return bulletin, err
  124. }
  125. // DeleteBulletinById 删除公告
  126. func DeleteBulletinById(bulletin_id string) (err error) {
  127. o := orm.NewOrm()
  128. query := o.QueryTable(new(Bulletin))
  129. _, err = query.Filter("bulletin_id", bulletin_id).Delete()
  130. if err != nil {
  131. logs.Error(lib.FuncName(), err)
  132. return
  133. }
  134. //Redis_Company_DelK(bulletin)
  135. return err
  136. }
  137. // UpdateBulletin 修改公告
  138. func UpdateBulletin(b Bulletin) (err error) {
  139. o := orm.NewOrm()
  140. query := o.QueryTable(new(Bulletin))
  141. params := orm.Params{
  142. "UpdateTime": b.UpdateTime,
  143. "state": b.State,
  144. }
  145. if b.Title != "" {
  146. params["Title"] = b.Title
  147. }
  148. if b.Content != "" {
  149. params["Content"] = b.Content
  150. }
  151. if b.IsShow != 0 {
  152. params["IsShow"] = b.IsShow
  153. }
  154. _, err = query.Filter("bulletin_id", b.BulletinId).Update(orm2.Params(params))
  155. if err != nil {
  156. logs.Error(lib.FuncName(), err)
  157. return
  158. }
  159. return err
  160. }
  161. // Read_Bulletin_List 获取公告列表
  162. func Read_Bulletin_List(t_uuid string, page int, page_z int, is_show string) (BulletinList []Bulletin, cnt int64) {
  163. o := orm.NewOrm()
  164. // 也可以直接使用 Model 结构体作为表名
  165. qs := o.QueryTable(new(Bulletin))
  166. var maps []Bulletin
  167. var offset int64
  168. if page <= 1 {
  169. offset = 0
  170. } else {
  171. offset = int64((page - 1) * page_z)
  172. }
  173. cond := orm.NewCondition()
  174. cond1 := cond.And("State", 0).And("T_uuid", t_uuid)
  175. if len(is_show) > 0 {
  176. is_show_int, _ := strconv.Atoi(is_show)
  177. cond1 = cond1.And("is_show", is_show_int)
  178. }
  179. //if len(T_name) > 0 {
  180. // cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
  181. //}
  182. var err error
  183. if page_z == 9999 {
  184. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps)
  185. } else {
  186. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps)
  187. }
  188. if err != nil {
  189. logs.Error(lib.FuncName(), err)
  190. }
  191. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  192. if err != nil {
  193. logs.Error(lib.FuncName(), err)
  194. }
  195. for _, v := range maps {
  196. BulletinList = append(BulletinList, v)
  197. }
  198. return BulletinList, cnt
  199. }
  200. // IsReadBulletin 已读状态
  201. func IsReadBulletin(id, t_uuid, bulletin_id string) (err error) {
  202. o := orm.NewOrm()
  203. query := o.QueryTable(new(Bulletin))
  204. params := orm.Params{
  205. "is_show": bulletin_id,
  206. }
  207. _, err = query.Filter("id", id).Filter("t_uuid", t_uuid).Update(orm2.Params(params))
  208. if err != nil {
  209. logs.Error(lib.FuncName(), err)
  210. return
  211. }
  212. return err
  213. }
  214. func Read_Bulletin_List_Admin(t_uuid string, page int, page_z int) (BulletinList []Bulletin, cnt int64) {
  215. o := orm.NewOrm()
  216. // 也可以直接使用 Model 结构体作为表名
  217. qs := o.QueryTable(new(Bulletin))
  218. var maps []Bulletin
  219. var offset int64
  220. if page <= 1 {
  221. offset = 0
  222. } else {
  223. offset = int64((page - 1) * page_z)
  224. }
  225. cond := orm.NewCondition()
  226. cond1 := cond.And("T_uuid", t_uuid)
  227. //if len(T_name) > 0 {
  228. // cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
  229. //}
  230. var err error
  231. if page_z == 9999 {
  232. _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps)
  233. } else {
  234. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps)
  235. }
  236. if err != nil {
  237. logs.Error(lib.FuncName(), err)
  238. }
  239. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  240. if err != nil {
  241. logs.Error(lib.FuncName(), err)
  242. }
  243. for i, _ := range maps {
  244. BulletinList = append(BulletinList, maps[i])
  245. }
  246. return BulletinList, cnt
  247. }