123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- package v1
- import (
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/tasker/httptasker"
- "git.getensh.com/common/gopkgs/util"
- "github.com/gin-gonic/gin"
- "go.uber.org/zap"
- "net/http"
- "property-system-gateway/errors"
- param_v1 "property-system-gateway/param/v1"
- "property-system-gateway/pb"
- "property-system-gateway/pb/v1"
- "property-system-gateway/utils"
- )
- //
- // @Summary 添加投票
- // @Description 添加投票
- // @Tags 投票
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param body body v1.VoteAddBody true "信息"
- // @Success 200 {object} v1.VoteAddResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/service/vote [post]
- func (c *Controller) VoteAdd(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.VoteAddRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.VoteAddBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.VoteAddResponse{}
- rpcReq := &v1.VoteAddRequest{
- GardenId: tokenInfo.GardenId,
- Title: req.Title,
- Pics: req.Pics,
- Start: req.Start,
- End: req.End,
- }
- topics := make([]*v1.VoteTopic, len(req.Topics))
- for i, v := range req.Topics {
- topics[i] = &v1.VoteTopic{
- TopicType: v.TopicType,
- TopicName: v.TopicName,
- // 总星数,当为评分题时有效
- Star: v.Star,
- // 是否必选
- Must: v.Must,
- // 题目编号
- Number: v.Number,
- }
- choiceItems := make([]*v1.VoteTopicChoiceItem, len(v.ChoiceItems))
- for j, w := range v.ChoiceItems {
- choiceItems[j] = &v1.VoteTopicChoiceItem{
- Flag: w.Flag,
- Text: w.Text,
- }
- }
- topics[i].ChoiceItems = choiceItems
- }
- rpcReq.Topics = topics
- _, err = pb.Garden.VoteAdd(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Garden.VoteAdd"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 修改投票
- // @Description 修改投票
- // @Tags 投票
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param body body v1.VoteUpdateBody true "信息"
- // @Success 200 {object} v1.VoteUpdateResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/service/vote [put]
- func (c *Controller) VoteUpdate(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.VoteUpdateRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.VoteUpdateBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.VoteUpdateResponse{}
- rpcReq := &v1.VoteUpdateRequest{
- GardenId: tokenInfo.GardenId,
- Title: req.Title,
- Pics: req.Pics,
- Start: req.Start,
- End: req.End,
- Id: req.Id,
- }
- topics := make([]*v1.VoteTopic, len(req.Topics))
- for i, v := range req.Topics {
- topics[i] = &v1.VoteTopic{
- TopicType: v.TopicType,
- TopicName: v.TopicName,
- // 总星数,当为评分题时有效
- Star: v.Star,
- // 是否必选
- Must: v.Must,
- // 题目编号
- Number: v.Number,
- }
- choiceItems := make([]*v1.VoteTopicChoiceItem, len(v.ChoiceItems))
- for j, w := range v.ChoiceItems {
- choiceItems[j] = &v1.VoteTopicChoiceItem{
- Flag: w.Flag,
- Text: w.Text,
- }
- }
- topics[i].ChoiceItems = choiceItems
- }
- rpcReq.Topics = topics
- _, err = pb.Garden.VoteUpdate(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Garden.VoteUpdate"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 删除投票
- // @Description 删除投票
- // @Tags 投票
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param id query int true " "
- // @Success 200 {object} v1.VoteDelResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/service/vote [delete]
- func (c *Controller) VoteDel(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.VoteDelRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteDelQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.VoteDelResponse{}
- rpcReq := &v1.VoteDelRequest{
- GardenId: tokenInfo.GardenId,
- Id: req.Id,
- }
- _, err = pb.Garden.VoteDel(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Garden.VoteDel"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 投票列表
- // @Description 投票列表
- // @Tags 投票
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param page query int false " "
- // @Param page_size query int false " "
- // @Param title query string false "投票标题"
- // @Success 200 {object} v1.VoteListResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/service/vote [get]
- func (c *Controller) VoteList(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.VoteListRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteListQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.VoteListResponse{}
- rpcReq := &v1.VoteListRequest{
- GardenId: tokenInfo.GardenId,
- Title: req.Title,
- PageSize: req.PageSize,
- Page: req.Page,
- }
- rpcRsp, err := pb.Garden.VoteList(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Garden.VoteList"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- if rpcRsp.List == nil {
- rpcRsp.List = make([]*v1.VoteItem, 0)
- }
- for i, v := range rpcRsp.List {
- if v.Topics == nil {
- rpcRsp.List[i].Topics = make([]*v1.VoteTopic, 0)
- }
- if v.Pics == nil {
- rpcRsp.List[i].Pics = make([]string, 0)
- }
- for j, w := range v.Topics {
- if w.ChoiceItems == nil {
- rpcRsp.List[i].Topics[j].ChoiceItems = make([]*v1.VoteTopicChoiceItem, 0)
- }
- }
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 一道题的回答详情
- // @Description 一道题的回答详情
- // @Tags 投票
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param page query int false " "
- // @Param page_size query int false " "
- // @Param id query int true " "
- // @Param number query int true "题的编号"
- // @Success 200 {object} v1.VoteResultListResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/service/vote/result [get]
- func (c *Controller) VoteResultList(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.VoteResultListRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteResultListQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.VoteResultListResponse{}
- rpcReq := &v1.VoteResultListRequest{
- GardenId: tokenInfo.GardenId,
- Id: req.Id,
- Page: req.Page,
- PageSize: req.PageSize,
- Number: req.Number,
- }
- rpcRsp, err := pb.Garden.VoteResultList(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Garden.VoteResultList"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- if rpcRsp.List == nil {
- rpcRsp.List = make([]*v1.VoteResultUserAnswerItem, 0)
- }
- for i, v := range rpcRsp.List {
- if v.Answer.CompletionAnswers == nil {
- rpcRsp.List[i].Answer.CompletionAnswers = make([]string, 0)
- }
- if v.Answer.ChoiceAnswers == nil {
- rpcRsp.List[i].Answer.ChoiceAnswers = make([]string, 0)
- }
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- //
- // @Summary 投票结果统计
- // @Description 投票结果统计
- // @Tags 投票
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param id query int true " "
- // @Success 200 {object} v1.VoteResultStatisticResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/service/vote/result/statistic [get]
- func (c *Controller) VoteResultStatistic(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.VoteResultStatisticRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteResultStatisticQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- tokenInfo, err := utils.GetSubjectValue(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.VoteResultStatisticResponse{}
- rpcReq := &v1.VoteResultStatisticRequest{
- GardenId: tokenInfo.GardenId,
- Id: req.Id,
- }
- rpcRsp, err := pb.Garden.VoteResultStatistic(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Garden.VoteResultStatistic"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- if rpcRsp.List == nil {
- rpcRsp.List = make([]*v1.VoteResultStatisticItem, 0)
- }
- for i, v := range rpcRsp.List {
- if v.ChoiceStatistic == nil {
- rpcRsp.List[i].ChoiceStatistic = make([]*v1.VoteResultStatisticChoice, 0)
- }
- if v.ChoiceItems == nil {
- rpcRsp.List[i].ChoiceItems = make([]*v1.VoteTopicChoiceItem, 0)
- }
- if v.StarStatistic == nil {
- rpcRsp.List[i].StarStatistic = make([]*v1.VoteResultStatisticStar, 0)
- }
- }
- resp.Data = *rpcRsp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
|