bulletin.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package controllers
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/models/Account"
  6. "Cold_Api/models/Announcement"
  7. "encoding/json"
  8. beego "github.com/beego/beego/v2/server/web"
  9. "github.com/google/uuid"
  10. "math"
  11. "time"
  12. )
  13. type BulletinController struct {
  14. beego.Controller
  15. Admin_r Account.Admin // 登陆的用户
  16. T_pid int // 公司id
  17. }
  18. func (c *BulletinController) Prepare() {
  19. GetCookie := c.Ctx.GetCookie("User_tokey")
  20. GetString := c.GetString("User_tokey")
  21. User_tokey := GetCookie
  22. if len(User_tokey) == 0 {
  23. User_tokey = GetString
  24. }
  25. if Account.Admin_r == nil {
  26. return
  27. }
  28. c.Admin_r = *Account.Admin_r
  29. T_pid := c.Admin_r.T_pid
  30. EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
  31. if EntryPid > 0 {
  32. T_pid = EntryPid
  33. }
  34. c.T_pid = T_pid
  35. }
  36. // AddBulletin 添加公告
  37. func (b *BulletinController) AddBulletin() {
  38. title := b.GetString("title")
  39. content := b.GetString("content")
  40. if len(title) == 0 {
  41. b.Data["json"] = lib.JSONS{Code: 310, Msg: "标题不能为空"}
  42. b.ServeJSON()
  43. return
  44. }
  45. if len(content) == 0 {
  46. b.Data["json"] = lib.JSONS{Code: 310, Msg: "内容不能为空"}
  47. b.ServeJSON()
  48. return
  49. }
  50. list := Account.Read_User_List_All()
  51. var err error
  52. uid, _ := uuid.NewRandom()
  53. for _, v := range list {
  54. notice := Announcement.Bulletin{
  55. BulletinId: uid.String(),
  56. T_uuid: v.T_uuid,
  57. Title: title,
  58. Content: content,
  59. State: 1,
  60. CreateTime: time.Time{},
  61. UpdateTime: time.Time{},
  62. }
  63. _, err = Announcement.AddBulletin(notice)
  64. }
  65. if err != nil {
  66. b.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  67. b.ServeJSON()
  68. return
  69. }
  70. b.Data["json"] = lib.JSONS{Code: 200, Msg: "添加成功!"}
  71. b.ServeJSON()
  72. return
  73. }
  74. // GetBulletinById 根据id查询公告详情
  75. func (b *BulletinController) GetBulletinById() {
  76. id := b.GetString("id")
  77. if len(id) == 0 {
  78. b.Data["json"] = lib.JSONS{Code: 310, Msg: "id不能为空"}
  79. b.ServeJSON()
  80. return
  81. }
  82. bulletin, err := Announcement.GetBulletinById(id, b.Admin_r.T_uuid)
  83. if err != nil {
  84. b.Data["json"] = lib.JSONS{Code: 301, Msg: "查询失败"}
  85. b.ServeJSON()
  86. return
  87. }
  88. b.Data["json"] = lib.JSONS{Code: 200, Msg: "查询成功!", Data: bulletin}
  89. b.ServeJSON()
  90. return
  91. }
  92. // DeleteBulletin 删除公告
  93. func (b *BulletinController) DeleteBulletin() {
  94. id := b.GetString("bulletin_id")
  95. if len(id) == 0 {
  96. b.Data["json"] = lib.JSONS{Code: 310, Msg: "id不能为空"}
  97. b.ServeJSON()
  98. return
  99. }
  100. err := Announcement.DeleteBulletinById(id)
  101. if err != nil {
  102. b.Data["json"] = lib.JSONS{Code: 301, Msg: "删除失败"}
  103. b.ServeJSON()
  104. return
  105. }
  106. b.Data["json"] = lib.JSONS{Code: 200}
  107. b.ServeJSON()
  108. return
  109. }
  110. // UpdateBulletin 修改公告
  111. func (b *BulletinController) UpdateBulletin() {
  112. var bulletin Announcement.Bulletin
  113. err := json.Unmarshal(b.Ctx.Input.RequestBody, &bulletin)
  114. if err != nil {
  115. b.Data["json"] = lib.JSONS{Code: 302, Msg: "json序列化失败"}
  116. b.ServeJSON()
  117. return
  118. }
  119. bulletin.T_uuid = b.Admin_r.T_uuid
  120. bulletin.UpdateTime = time.Now()
  121. err = Announcement.UpdateBulletin(bulletin)
  122. if err != nil {
  123. b.Data["json"] = lib.JSONS{Code: 301, Msg: "修改失败"}
  124. b.ServeJSON()
  125. return
  126. }
  127. b.Data["json"] = lib.JSONS{Code: 200, Msg: "修改成功"}
  128. b.ServeJSON()
  129. return
  130. }
  131. // GetBulletinsList 返回用户公告列表
  132. func (b *BulletinController) GetBulletinsList() {
  133. type R_JSONS struct {
  134. //必须的大写开头
  135. Data []Announcement.Bulletin
  136. Num int64
  137. Page int
  138. Page_size int
  139. }
  140. var r_jsons R_JSONS
  141. page, _ := b.GetInt("page")
  142. if page < 1 {
  143. page = 1
  144. }
  145. page_z, _ := b.GetInt("page_z")
  146. if page_z < 1 {
  147. page_z = conf.Page_size
  148. }
  149. r_jsons.Data, r_jsons.Num = Announcement.Read_Bulletin_List(b.Admin_r.T_uuid, page, page_z)
  150. r_jsons.Page = page
  151. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  152. b.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  153. b.ServeJSON()
  154. return
  155. }
  156. // IsReadBulletin 公告是否已读
  157. func (b *BulletinController) IsReadBulletin() {
  158. is_show := b.GetString("is_show")
  159. id := b.GetString("id")
  160. if len(is_show) == 0 {
  161. b.Data["json"] = lib.JSONS{Code: 310, Msg: "缺少必要参数"}
  162. b.ServeJSON()
  163. return
  164. }
  165. err := Announcement.IsReadBulletin(id, b.Admin_r.T_uuid, is_show)
  166. if err != nil {
  167. b.Data["json"] = lib.JSONS{Code: 301, Msg: "修改失败"}
  168. b.ServeJSON()
  169. return
  170. }
  171. b.Data["json"] = lib.JSONS{Code: 200, Msg: "修改成功"}
  172. b.ServeJSON()
  173. return
  174. }