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-applete-gateway/errors" param_v1 "property-applete-gateway/param/v1" "property-applete-gateway/pb" "property-applete-gateway/pb/v1" "property-applete-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) } 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 " " // @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, } 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.Answers == nil { rpcRsp.List[i].Answers = make([]*v1.VoteResultData, 0) } for j, w := range rpcRsp.List[i].Answers { if w.CompletionAnswers == nil { rpcRsp.List[i].Answers[j].CompletionAnswers = make([]string, 0) } if w.ChoiceAnswers == nil { rpcRsp.List[i].Answers[j].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) } } ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) }