vote.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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-household-gateway/errors"
  10. param_v1 "property-household-gateway/param/v1"
  11. "property-household-gateway/pb"
  12. "property-household-gateway/pb/v1"
  13. "property-household-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.VoteAddAnswerBody true "信息"
  23. // @Success 200 {object} v1.VoteAddAnswerResponse
  24. // @Failure 500 {object} base.HTTPError
  25. // @Router /api/v1/service/vote/answer [post]
  26. func (c *Controller) VoteAddAnswer(ctx *gin.Context) {
  27. // 解析参数
  28. req := &param_v1.VoteAddAnswerRequest{}
  29. parseParamTask := func() error {
  30. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.VoteAddAnswerBody)
  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.GetJwtTokenInfo(ctx)
  42. if err != nil {
  43. return err
  44. }
  45. // 响应数据
  46. resp := param_v1.VoteAddAnswerResponse{}
  47. rpcReq := &v1.VoteAddAnswerRequest{
  48. GardenId:req.GardenId,
  49. Uid:tokenInfo.Uid,
  50. Id:req.Id,
  51. }
  52. answers := make([]*v1.VoteTopicAnswer, len(req.Answers))
  53. for i, v := range req.Answers {
  54. answers[i] = &v1.VoteTopicAnswer{
  55. Number:v.Number,
  56. TopicType:v.TopicType,
  57. ChoiceAnswers:v.ChoiceAnswers,
  58. CompletionAnswers:v.CompletionAnswers,
  59. StarAnswers:v.StarAnswers,
  60. }
  61. }
  62. rpcReq.Answers = answers
  63. _, err = pb.Garden.VoteAddAnswer(ctx, rpcReq)
  64. if err != nil {
  65. s, _ := json.MarshalToString(req)
  66. logger.Error("func",
  67. zap.String("call", "pb.Garden.VoteAddAnswer"),
  68. zap.String("params", s),
  69. zap.String("error", err.Error()))
  70. return errors.ErrorTransForm(err)
  71. }
  72. ctx.JSON(http.StatusOK, resp)
  73. return nil
  74. }
  75. // 执行任务
  76. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  77. }
  78. //
  79. // @Summary 投票列表
  80. // @Description 投票列表
  81. // @Tags 投票
  82. // @Accept json
  83. // @Produce json
  84. // @Param token header string true "token"
  85. // @Param page query int false " "
  86. // @Param page_size query int false " "
  87. // @Param title query string false "投票标题"
  88. // @Param garden_id query int true "小区id"
  89. // @Success 200 {object} v1.VoteListForHouseholdResponse
  90. // @Failure 500 {object} base.HTTPError
  91. // @Router /api/v1/service/vote [get]
  92. func (c *Controller) VoteListForHousehold(ctx *gin.Context) {
  93. // 解析参数
  94. req := &param_v1.VoteListForHouseholdRequest{}
  95. parseParamTask := func() error {
  96. err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteListForHouseholdQuery, nil)
  97. if err != nil {
  98. logger.Error("func",
  99. zap.String("call", "util.ShouldBind"),
  100. zap.String("error", err.Error()))
  101. return errors.ParamsError
  102. }
  103. return nil
  104. }
  105. // 业务处理
  106. handleServiceTask := func() error {
  107. tokenInfo, err := utils.GetJwtTokenInfo(ctx)
  108. if err != nil {
  109. return err
  110. }
  111. // 响应数据
  112. resp := param_v1.VoteListForHouseholdResponse{}
  113. rpcReq := &v1.VoteListForHouseholdRequest{
  114. GardenId:req.GardenId,
  115. Title:req.Title,
  116. PageSize:req.PageSize,
  117. Page:req.Page,
  118. Uid:tokenInfo.Uid,
  119. }
  120. rpcRsp, err := pb.Garden.VoteListForHousehold(ctx, rpcReq)
  121. if err != nil {
  122. s, _ := json.MarshalToString(req)
  123. logger.Error("func",
  124. zap.String("call", "pb.Garden.VoteListForHousehold"),
  125. zap.String("params", s),
  126. zap.String("error", err.Error()))
  127. return errors.ErrorTransForm(err)
  128. }
  129. if rpcRsp.List == nil {
  130. rpcRsp.List = make([]*v1.VoteListForHouseholdItem, 0)
  131. }
  132. for i, v := range rpcRsp.List {
  133. if v.Topics == nil {
  134. rpcRsp.List[i].Topics = make([]*v1.VoteTopicForHouseholdItem, 0)
  135. }
  136. for j, w := range v.Topics {
  137. if w.ChoiceItems == nil {
  138. rpcRsp.List[i].Topics[j].ChoiceItems = make([]*v1.VoteTopicChoiceItem, 0)
  139. }
  140. if w.ChoiceAnswers == nil {
  141. rpcRsp.List[i].Topics[j].ChoiceAnswers = make([]string, 0)
  142. }
  143. if w.CompletionAnswers == nil {
  144. rpcRsp.List[i].Topics[j].CompletionAnswers = make([]string, 0)
  145. }
  146. }
  147. }
  148. resp.Data = *rpcRsp
  149. ctx.JSON(http.StatusOK, resp)
  150. return nil
  151. }
  152. // 执行任务
  153. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  154. }
  155. //
  156. // @Summary 投票结果统计
  157. // @Description 投票结果统计
  158. // @Tags 投票
  159. // @Accept json
  160. // @Produce json
  161. // @Param token header string true "token"
  162. // @Param id query int true " "
  163. // @Param garden_id query int true "小区id"
  164. // @Success 200 {object} v1.VoteResultStatisticResponse
  165. // @Failure 500 {object} base.HTTPError
  166. // @Router /api/v1/service/vote/result/statistic [get]
  167. func (c *Controller) VoteResultStatistic(ctx *gin.Context) {
  168. // 解析参数
  169. req := &param_v1.VoteResultStatisticRequest{}
  170. parseParamTask := func() error {
  171. err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteResultStatisticQuery, nil)
  172. if err != nil {
  173. logger.Error("func",
  174. zap.String("call", "util.ShouldBind"),
  175. zap.String("error", err.Error()))
  176. return errors.ParamsError
  177. }
  178. return nil
  179. }
  180. // 业务处理
  181. handleServiceTask := func() error {
  182. // 响应数据
  183. resp := param_v1.VoteResultStatisticResponse{}
  184. rpcReq := &v1.VoteResultStatisticRequest{
  185. GardenId:req.GardenId,
  186. Id:req.Id,
  187. }
  188. rpcRsp, err := pb.Garden.VoteResultStatistic(ctx, rpcReq)
  189. if err != nil {
  190. s, _ := json.MarshalToString(req)
  191. logger.Error("func",
  192. zap.String("call", "pb.Garden.VoteResultStatistic"),
  193. zap.String("params", s),
  194. zap.String("error", err.Error()))
  195. return errors.ErrorTransForm(err)
  196. }
  197. if rpcRsp.List == nil {
  198. rpcRsp.List = make([]*v1.VoteResultStatisticItem, 0)
  199. }
  200. for i, v := range rpcRsp.List {
  201. if v.ChoiceStatistic == nil {
  202. rpcRsp.List[i].ChoiceStatistic = make([]*v1.VoteResultStatisticChoice, 0)
  203. }
  204. if v.ChoiceItems == nil {
  205. rpcRsp.List[i].ChoiceItems = make([]*v1.VoteTopicChoiceItem, 0)
  206. }
  207. if v.StarStatistic == nil {
  208. rpcRsp.List[i].StarStatistic = make([]*v1.VoteResultStatisticStar, 0)
  209. }
  210. }
  211. resp.Data = *rpcRsp
  212. ctx.JSON(http.StatusOK, resp)
  213. return nil
  214. }
  215. // 执行任务
  216. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  217. }