vote.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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.VoteAddBody true "信息"
  23. // @Success 200 {object} v1.VoteAddResponse
  24. // @Failure 500 {object} base.HTTPError
  25. // @Router /api/v1/service/vote [post]
  26. func (c *Controller) VoteAdd(ctx *gin.Context) {
  27. // 解析参数
  28. req := &param_v1.VoteAddRequest{}
  29. parseParamTask := func() error {
  30. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.VoteAddBody)
  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.VoteAddResponse{}
  47. rpcReq := &v1.VoteAddRequest{
  48. GardenId: tokenInfo.GardenId,
  49. Title: req.Title,
  50. Pics: req.Pics,
  51. Start: req.Start,
  52. End: req.End,
  53. }
  54. topics := make([]*v1.VoteTopic, len(req.Topics))
  55. for i, v := range req.Topics {
  56. topics[i] = &v1.VoteTopic{
  57. TopicType: v.TopicType,
  58. TopicName: v.TopicName,
  59. // 总星数,当为评分题时有效
  60. Star: v.Star,
  61. // 是否必选
  62. Must: v.Must,
  63. // 题目编号
  64. Number: v.Number,
  65. }
  66. choiceItems := make([]*v1.VoteTopicChoiceItem, len(v.ChoiceItems))
  67. for j, w := range v.ChoiceItems {
  68. choiceItems[j] = &v1.VoteTopicChoiceItem{
  69. Flag: w.Flag,
  70. Text: w.Text,
  71. }
  72. }
  73. topics[i].ChoiceItems = choiceItems
  74. }
  75. rpcReq.Topics = topics
  76. _, err = pb.Garden.VoteAdd(ctx, rpcReq)
  77. if err != nil {
  78. s, _ := json.MarshalToString(req)
  79. logger.Error("func",
  80. zap.String("call", "pb.Garden.VoteAdd"),
  81. zap.String("params", s),
  82. zap.String("error", err.Error()))
  83. return errors.ErrorTransForm(err)
  84. }
  85. ctx.JSON(http.StatusOK, resp)
  86. return nil
  87. }
  88. // 执行任务
  89. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  90. }
  91. //
  92. // @Summary 修改投票
  93. // @Description 修改投票
  94. // @Tags 投票
  95. // @Accept json
  96. // @Produce json
  97. // @Param token header string true "token"
  98. // @Param body body v1.VoteUpdateBody true "信息"
  99. // @Success 200 {object} v1.VoteUpdateResponse
  100. // @Failure 500 {object} base.HTTPError
  101. // @Router /api/v1/service/vote [put]
  102. func (c *Controller) VoteUpdate(ctx *gin.Context) {
  103. // 解析参数
  104. req := &param_v1.VoteUpdateRequest{}
  105. parseParamTask := func() error {
  106. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.VoteUpdateBody)
  107. if err != nil {
  108. logger.Error("func",
  109. zap.String("call", "util.ShouldBind"),
  110. zap.String("error", err.Error()))
  111. return errors.ParamsError
  112. }
  113. return nil
  114. }
  115. // 业务处理
  116. handleServiceTask := func() error {
  117. tokenInfo, err := utils.GetSubjectValue(ctx)
  118. if err != nil {
  119. return err
  120. }
  121. // 响应数据
  122. resp := param_v1.VoteUpdateResponse{}
  123. rpcReq := &v1.VoteUpdateRequest{
  124. GardenId: tokenInfo.GardenId,
  125. Title: req.Title,
  126. Pics: req.Pics,
  127. Start: req.Start,
  128. End: req.End,
  129. Id: req.Id,
  130. }
  131. topics := make([]*v1.VoteTopic, len(req.Topics))
  132. for i, v := range req.Topics {
  133. topics[i] = &v1.VoteTopic{
  134. TopicType: v.TopicType,
  135. TopicName: v.TopicName,
  136. // 总星数,当为评分题时有效
  137. Star: v.Star,
  138. // 是否必选
  139. Must: v.Must,
  140. // 题目编号
  141. Number: v.Number,
  142. }
  143. choiceItems := make([]*v1.VoteTopicChoiceItem, len(v.ChoiceItems))
  144. for j, w := range v.ChoiceItems {
  145. choiceItems[j] = &v1.VoteTopicChoiceItem{
  146. Flag: w.Flag,
  147. Text: w.Text,
  148. }
  149. }
  150. topics[i].ChoiceItems = choiceItems
  151. }
  152. rpcReq.Topics = topics
  153. _, err = pb.Garden.VoteUpdate(ctx, rpcReq)
  154. if err != nil {
  155. s, _ := json.MarshalToString(req)
  156. logger.Error("func",
  157. zap.String("call", "pb.Garden.VoteUpdate"),
  158. zap.String("params", s),
  159. zap.String("error", err.Error()))
  160. return errors.ErrorTransForm(err)
  161. }
  162. ctx.JSON(http.StatusOK, resp)
  163. return nil
  164. }
  165. // 执行任务
  166. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  167. }
  168. //
  169. // @Summary 删除投票
  170. // @Description 删除投票
  171. // @Tags 投票
  172. // @Accept json
  173. // @Produce json
  174. // @Param token header string true "token"
  175. // @Param id query int true " "
  176. // @Success 200 {object} v1.VoteDelResponse
  177. // @Failure 500 {object} base.HTTPError
  178. // @Router /api/v1/service/vote [delete]
  179. func (c *Controller) VoteDel(ctx *gin.Context) {
  180. // 解析参数
  181. req := &param_v1.VoteDelRequest{}
  182. parseParamTask := func() error {
  183. err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteDelQuery, nil)
  184. if err != nil {
  185. logger.Error("func",
  186. zap.String("call", "util.ShouldBind"),
  187. zap.String("error", err.Error()))
  188. return errors.ParamsError
  189. }
  190. return nil
  191. }
  192. // 业务处理
  193. handleServiceTask := func() error {
  194. tokenInfo, err := utils.GetSubjectValue(ctx)
  195. if err != nil {
  196. return err
  197. }
  198. // 响应数据
  199. resp := param_v1.VoteDelResponse{}
  200. rpcReq := &v1.VoteDelRequest{
  201. GardenId: tokenInfo.GardenId,
  202. Id: req.Id,
  203. }
  204. _, err = pb.Garden.VoteDel(ctx, rpcReq)
  205. if err != nil {
  206. s, _ := json.MarshalToString(req)
  207. logger.Error("func",
  208. zap.String("call", "pb.Garden.VoteDel"),
  209. zap.String("params", s),
  210. zap.String("error", err.Error()))
  211. return errors.ErrorTransForm(err)
  212. }
  213. ctx.JSON(http.StatusOK, resp)
  214. return nil
  215. }
  216. // 执行任务
  217. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  218. }
  219. //
  220. // @Summary 投票列表
  221. // @Description 投票列表
  222. // @Tags 投票
  223. // @Accept json
  224. // @Produce json
  225. // @Param token header string true "token"
  226. // @Param page query int false " "
  227. // @Param page_size query int false " "
  228. // @Param title query string false "投票标题"
  229. // @Success 200 {object} v1.VoteListResponse
  230. // @Failure 500 {object} base.HTTPError
  231. // @Router /api/v1/service/vote [get]
  232. func (c *Controller) VoteList(ctx *gin.Context) {
  233. // 解析参数
  234. req := &param_v1.VoteListRequest{}
  235. parseParamTask := func() error {
  236. err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteListQuery, nil)
  237. if err != nil {
  238. logger.Error("func",
  239. zap.String("call", "util.ShouldBind"),
  240. zap.String("error", err.Error()))
  241. return errors.ParamsError
  242. }
  243. return nil
  244. }
  245. // 业务处理
  246. handleServiceTask := func() error {
  247. tokenInfo, err := utils.GetSubjectValue(ctx)
  248. if err != nil {
  249. return err
  250. }
  251. // 响应数据
  252. resp := param_v1.VoteListResponse{}
  253. rpcReq := &v1.VoteListRequest{
  254. GardenId: tokenInfo.GardenId,
  255. Title: req.Title,
  256. PageSize: req.PageSize,
  257. Page: req.Page,
  258. }
  259. rpcRsp, err := pb.Garden.VoteList(ctx, rpcReq)
  260. if err != nil {
  261. s, _ := json.MarshalToString(req)
  262. logger.Error("func",
  263. zap.String("call", "pb.Garden.VoteList"),
  264. zap.String("params", s),
  265. zap.String("error", err.Error()))
  266. return errors.ErrorTransForm(err)
  267. }
  268. if rpcRsp.List == nil {
  269. rpcRsp.List = make([]*v1.VoteItem, 0)
  270. }
  271. for i, v := range rpcRsp.List {
  272. if v.Topics == nil {
  273. rpcRsp.List[i].Topics = make([]*v1.VoteTopic, 0)
  274. }
  275. if v.Pics == nil {
  276. rpcRsp.List[i].Pics = make([]string, 0)
  277. }
  278. for j, w := range v.Topics {
  279. if w.ChoiceItems == nil {
  280. rpcRsp.List[i].Topics[j].ChoiceItems = make([]*v1.VoteTopicChoiceItem, 0)
  281. }
  282. }
  283. }
  284. resp.Data = *rpcRsp
  285. ctx.JSON(http.StatusOK, resp)
  286. return nil
  287. }
  288. // 执行任务
  289. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  290. }
  291. //
  292. // @Summary 一道题的回答详情
  293. // @Description 一道题的回答详情
  294. // @Tags 投票
  295. // @Accept json
  296. // @Produce json
  297. // @Param token header string true "token"
  298. // @Param page query int false " "
  299. // @Param page_size query int false " "
  300. // @Param id query int true " "
  301. // @Param number query int true "题的编号"
  302. // @Success 200 {object} v1.VoteResultListResponse
  303. // @Failure 500 {object} base.HTTPError
  304. // @Router /api/v1/service/vote/result [get]
  305. func (c *Controller) VoteResultList(ctx *gin.Context) {
  306. // 解析参数
  307. req := &param_v1.VoteResultListRequest{}
  308. parseParamTask := func() error {
  309. err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteResultListQuery, nil)
  310. if err != nil {
  311. logger.Error("func",
  312. zap.String("call", "util.ShouldBind"),
  313. zap.String("error", err.Error()))
  314. return errors.ParamsError
  315. }
  316. return nil
  317. }
  318. // 业务处理
  319. handleServiceTask := func() error {
  320. tokenInfo, err := utils.GetSubjectValue(ctx)
  321. if err != nil {
  322. return err
  323. }
  324. // 响应数据
  325. resp := param_v1.VoteResultListResponse{}
  326. rpcReq := &v1.VoteResultListRequest{
  327. GardenId: tokenInfo.GardenId,
  328. Id: req.Id,
  329. Page: req.Page,
  330. PageSize: req.PageSize,
  331. Number: req.Number,
  332. }
  333. rpcRsp, err := pb.Garden.VoteResultList(ctx, rpcReq)
  334. if err != nil {
  335. s, _ := json.MarshalToString(req)
  336. logger.Error("func",
  337. zap.String("call", "pb.Garden.VoteResultList"),
  338. zap.String("params", s),
  339. zap.String("error", err.Error()))
  340. return errors.ErrorTransForm(err)
  341. }
  342. if rpcRsp.List == nil {
  343. rpcRsp.List = make([]*v1.VoteResultUserAnswerItem, 0)
  344. }
  345. for i, v := range rpcRsp.List {
  346. if v.Answer.CompletionAnswers == nil {
  347. rpcRsp.List[i].Answer.CompletionAnswers = make([]string, 0)
  348. }
  349. if v.Answer.ChoiceAnswers == nil {
  350. rpcRsp.List[i].Answer.ChoiceAnswers = make([]string, 0)
  351. }
  352. }
  353. resp.Data = *rpcRsp
  354. ctx.JSON(http.StatusOK, resp)
  355. return nil
  356. }
  357. // 执行任务
  358. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  359. }
  360. //
  361. // @Summary 投票结果统计
  362. // @Description 投票结果统计
  363. // @Tags 投票
  364. // @Accept json
  365. // @Produce json
  366. // @Param token header string true "token"
  367. // @Param id query int true " "
  368. // @Success 200 {object} v1.VoteResultStatisticResponse
  369. // @Failure 500 {object} base.HTTPError
  370. // @Router /api/v1/service/vote/result/statistic [get]
  371. func (c *Controller) VoteResultStatistic(ctx *gin.Context) {
  372. // 解析参数
  373. req := &param_v1.VoteResultStatisticRequest{}
  374. parseParamTask := func() error {
  375. err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteResultStatisticQuery, nil)
  376. if err != nil {
  377. logger.Error("func",
  378. zap.String("call", "util.ShouldBind"),
  379. zap.String("error", err.Error()))
  380. return errors.ParamsError
  381. }
  382. return nil
  383. }
  384. // 业务处理
  385. handleServiceTask := func() error {
  386. tokenInfo, err := utils.GetSubjectValue(ctx)
  387. if err != nil {
  388. return err
  389. }
  390. // 响应数据
  391. resp := param_v1.VoteResultStatisticResponse{}
  392. rpcReq := &v1.VoteResultStatisticRequest{
  393. GardenId: tokenInfo.GardenId,
  394. Id: req.Id,
  395. }
  396. rpcRsp, err := pb.Garden.VoteResultStatistic(ctx, rpcReq)
  397. if err != nil {
  398. s, _ := json.MarshalToString(req)
  399. logger.Error("func",
  400. zap.String("call", "pb.Garden.VoteResultStatistic"),
  401. zap.String("params", s),
  402. zap.String("error", err.Error()))
  403. return errors.ErrorTransForm(err)
  404. }
  405. if rpcRsp.List == nil {
  406. rpcRsp.List = make([]*v1.VoteResultStatisticItem, 0)
  407. }
  408. for i, v := range rpcRsp.List {
  409. if v.ChoiceStatistic == nil {
  410. rpcRsp.List[i].ChoiceStatistic = make([]*v1.VoteResultStatisticChoice, 0)
  411. }
  412. if v.ChoiceItems == nil {
  413. rpcRsp.List[i].ChoiceItems = make([]*v1.VoteTopicChoiceItem, 0)
  414. }
  415. if v.StarStatistic == nil {
  416. rpcRsp.List[i].StarStatistic = make([]*v1.VoteResultStatisticStar, 0)
  417. }
  418. }
  419. resp.Data = *rpcRsp
  420. ctx.JSON(http.StatusOK, resp)
  421. return nil
  422. }
  423. // 执行任务
  424. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  425. }