announcement.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package v1
  2. import (
  3. "git.getensh.com/common/gopkgs/logger"
  4. "git.getensh.com/common/gopkgs/tasker/httptasker"
  5. "git.getensh.com/common/gopkgs/util"
  6. "github.com/gin-gonic/gin"
  7. "go.uber.org/zap"
  8. "net/http"
  9. "property-system-gateway/errors"
  10. param_v1 "property-system-gateway/param/v1"
  11. "property-system-gateway/pb"
  12. "property-system-gateway/pb/v1"
  13. "property-system-gateway/utils"
  14. )
  15. //
  16. // @Summary 添加公告
  17. // @Description 添加公告
  18. // @Tags 公告
  19. // @Accept json
  20. // @Produce json
  21. // @Param token header string true "token"
  22. // @Param body body v1.AnnouncementAddBody true "信息"
  23. // @Success 200 {object} v1.AnnouncementAddResponse
  24. // @Failure 500 {object} base.HTTPError
  25. // @Router /api/v1/service/announcement [post]
  26. func (c *Controller) AnnouncementAdd(ctx *gin.Context) {
  27. // 解析参数
  28. req := &param_v1.AnnouncementAddRequest{}
  29. parseParamTask := func() error {
  30. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.AnnouncementAddBody)
  31. if err != nil {
  32. logger.Error("func",
  33. zap.String("call", "util.ShouldBind"),
  34. zap.String("error", err.Error()))
  35. return errors.ParamsError
  36. }
  37. return nil
  38. }
  39. // 业务处理
  40. handleServiceTask := func() error {
  41. tokenInfo, err := utils.GetSubjectValue(ctx)
  42. if err != nil {
  43. return err
  44. }
  45. // 响应数据
  46. resp := param_v1.AnnouncementAddResponse{}
  47. rpcReq := &v1.AnnouncementAddRequest{
  48. GardenId:tokenInfo.GardenId,
  49. Content:req.Content,
  50. Title:req.Title,
  51. }
  52. rpcRsp, err := pb.Garden.AnnouncementAdd(ctx, rpcReq)
  53. if err != nil {
  54. s, _ := json.MarshalToString(req)
  55. logger.Error("func",
  56. zap.String("call", "pb.Garden.AnnouncementAdd"),
  57. zap.String("params", s),
  58. zap.String("error", err.Error()))
  59. return errors.ErrorTransForm(err)
  60. }
  61. resp.Data = *rpcRsp
  62. ctx.JSON(http.StatusOK, resp)
  63. logReq := OperationLogRequest{
  64. Module:ModuleAnnouncement,
  65. Action:ActionAnnouncementAdd,
  66. Origin:nil,
  67. Target:req.AnnouncementAddBody,
  68. UserName:tokenInfo.UserName,
  69. Uid:tokenInfo.Uid,
  70. Cid:tokenInfo.Cid,
  71. GardenId:tokenInfo.GardenId,
  72. }
  73. go OperationLogAdd(&logReq)
  74. return nil
  75. }
  76. // 执行任务
  77. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  78. }
  79. //
  80. // @Summary 修改公告
  81. // @Description 修改公告
  82. // @Tags 公告
  83. // @Accept json
  84. // @Produce json
  85. // @Param token header string true "token"
  86. // @Param body body v1.AnnouncementUpdateBody true "信息"
  87. // @Success 200 {object} v1.AnnouncementUpdateResponse
  88. // @Failure 500 {object} base.HTTPError
  89. // @Router /api/v1/service/announcement [put]
  90. func (c *Controller) AnnouncementUpdate(ctx *gin.Context) {
  91. // 解析参数
  92. req := &param_v1.AnnouncementUpdateRequest{}
  93. parseParamTask := func() error {
  94. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.AnnouncementUpdateBody)
  95. if err != nil {
  96. logger.Error("func",
  97. zap.String("call", "util.ShouldBind"),
  98. zap.String("error", err.Error()))
  99. return errors.ParamsError
  100. }
  101. return nil
  102. }
  103. // 业务处理
  104. handleServiceTask := func() error {
  105. tokenInfo, err := utils.GetSubjectValue(ctx)
  106. if err != nil {
  107. return err
  108. }
  109. // 响应数据
  110. resp := param_v1.AnnouncementUpdateResponse{}
  111. rpcReq := &v1.AnnouncementUpdateRequest{
  112. GardenId:tokenInfo.GardenId,
  113. Content:req.Content,
  114. Title:req.Title,
  115. Id:req.Id,
  116. }
  117. rpcRsp, err := pb.Garden.AnnouncementUpdate(ctx, rpcReq)
  118. if err != nil {
  119. s, _ := json.MarshalToString(req)
  120. logger.Error("func",
  121. zap.String("call", "pb.Garden.AnnouncementUpdate"),
  122. zap.String("params", s),
  123. zap.String("error", err.Error()))
  124. return errors.ErrorTransForm(err)
  125. }
  126. ctx.JSON(http.StatusOK, resp)
  127. logReq := OperationLogRequest{
  128. Module:ModuleAnnouncement,
  129. Action:ActionAnnouncementUpdate,
  130. Origin:rpcRsp.Origin,
  131. Target:req.AnnouncementUpdateBody,
  132. UserName:tokenInfo.UserName,
  133. Uid:tokenInfo.Uid,
  134. Cid:tokenInfo.Cid,
  135. GardenId:tokenInfo.GardenId,
  136. }
  137. go OperationLogAdd(&logReq)
  138. return nil
  139. }
  140. // 执行任务
  141. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  142. }
  143. //
  144. // @Summary 删除公告
  145. // @Description 删除公告
  146. // @Tags 公告
  147. // @Accept json
  148. // @Produce json
  149. // @Param token header string true "token"
  150. // @Param id query int true " "
  151. // @Success 200 {object} v1.AnnouncementDelResponse
  152. // @Failure 500 {object} base.HTTPError
  153. // @Router /api/v1/service/announcement [delete]
  154. func (c *Controller) AnnouncementDel(ctx *gin.Context) {
  155. // 解析参数
  156. req := &param_v1.AnnouncementDelRequest{}
  157. parseParamTask := func() error {
  158. err := util.ShouldBind(ctx, &req.Header, nil, &req.AnnouncementDelQuery, nil)
  159. if err != nil {
  160. logger.Error("func",
  161. zap.String("call", "util.ShouldBind"),
  162. zap.String("error", err.Error()))
  163. return errors.ParamsError
  164. }
  165. return nil
  166. }
  167. // 业务处理
  168. handleServiceTask := func() error {
  169. tokenInfo, err := utils.GetSubjectValue(ctx)
  170. if err != nil {
  171. return err
  172. }
  173. // 响应数据
  174. resp := param_v1.AnnouncementDelResponse{}
  175. rpcReq := &v1.AnnouncementDelRequest{
  176. GardenId:tokenInfo.GardenId,
  177. Id:req.Id,
  178. }
  179. rpcRsp, err := pb.Garden.AnnouncementDel(ctx, rpcReq)
  180. if err != nil {
  181. s, _ := json.MarshalToString(req)
  182. logger.Error("func",
  183. zap.String("call", "pb.Garden.AnnouncementDel"),
  184. zap.String("params", s),
  185. zap.String("error", err.Error()))
  186. return errors.ErrorTransForm(err)
  187. }
  188. ctx.JSON(http.StatusOK, resp)
  189. logReq := OperationLogRequest{
  190. Module:ModuleAnnouncement,
  191. Action:ActionAnnouncementDel,
  192. Origin:rpcRsp.Origin,
  193. Target:nil,
  194. UserName:tokenInfo.UserName,
  195. Uid:tokenInfo.Uid,
  196. Cid:tokenInfo.Cid,
  197. GardenId:tokenInfo.GardenId,
  198. }
  199. go OperationLogAdd(&logReq)
  200. return nil
  201. }
  202. // 执行任务
  203. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  204. }
  205. //
  206. // @Summary 公告列表
  207. // @Description 公告列表
  208. // @Tags 公告
  209. // @Accept json
  210. // @Produce json
  211. // @Param token header string true "token"
  212. // @Param page query int false " "
  213. // @Param page_size query int false " "
  214. // @Success 200 {object} v1.AnnouncementListResponse
  215. // @Failure 500 {object} base.HTTPError
  216. // @Router /api/v1/service/announcement [get]
  217. func (c *Controller) AnnouncementList(ctx *gin.Context) {
  218. // 解析参数
  219. req := &param_v1.AnnouncementListRequest{}
  220. parseParamTask := func() error {
  221. err := util.ShouldBind(ctx, &req.Header, nil, &req.AnnouncementListQuery, nil)
  222. if err != nil {
  223. logger.Error("func",
  224. zap.String("call", "util.ShouldBind"),
  225. zap.String("error", err.Error()))
  226. return errors.ParamsError
  227. }
  228. return nil
  229. }
  230. // 业务处理
  231. handleServiceTask := func() error {
  232. tokenInfo, err := utils.GetSubjectValue(ctx)
  233. if err != nil {
  234. return err
  235. }
  236. // 响应数据
  237. resp := param_v1.AnnouncementListResponse{}
  238. rpcReq := &v1.AnnouncementListRequest{
  239. GardenId:tokenInfo.GardenId,
  240. PageSize:req.PageSize,
  241. Page:req.Page,
  242. }
  243. rpcRsp, err := pb.Garden.AnnouncementList(ctx, rpcReq)
  244. if err != nil {
  245. s, _ := json.MarshalToString(req)
  246. logger.Error("func",
  247. zap.String("call", "pb.Garden.AnnouncementList"),
  248. zap.String("params", s),
  249. zap.String("error", err.Error()))
  250. return errors.ErrorTransForm(err)
  251. }
  252. if rpcRsp.List == nil {
  253. rpcRsp.List = make([]*v1.AnnouncementItem, 0)
  254. }
  255. resp.Data = *rpcRsp
  256. ctx.JSON(http.StatusOK, resp)
  257. return nil
  258. }
  259. // 执行任务
  260. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  261. }