123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- 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-household-gateway/errors"
- param_v1 "property-household-gateway/param/v1"
- "property-household-gateway/pb"
- "property-household-gateway/pb/v1"
- "property-household-gateway/utils"
- )
- //
- // @Summary 提交投票回答
- // @Description 提交投票回答
- // @Tags 投票
- // @Accept json
- // @Produce json
- // @Param token header string true "token"
- // @Param body body v1.VoteAddAnswerBody true "信息"
- // @Success 200 {object} v1.VoteAddAnswerResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/service/vote/answer [post]
- func (c *Controller) VoteAddAnswer(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.VoteAddAnswerRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.VoteAddAnswerBody)
- 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.GetJwtTokenInfo(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.VoteAddAnswerResponse{}
- rpcReq := &v1.VoteAddAnswerRequest{
- GardenId:req.GardenId,
- Uid:tokenInfo.Uid,
- Id:req.Id,
- }
- answers := make([]*v1.VoteTopicAnswer, len(req.Answers))
- for i, v := range req.Answers {
- answers[i] = &v1.VoteTopicAnswer{
- Number:v.Number,
- TopicType:v.TopicType,
- ChoiceAnswers:v.ChoiceAnswers,
- CompletionAnswers:v.CompletionAnswers,
- StarAnswers:v.StarAnswers,
- }
- }
- rpcReq.Answers = answers
- _, err = pb.Garden.VoteAddAnswer(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Garden.VoteAddAnswer"),
- 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 "投票标题"
- // @Param garden_id query int true "小区id"
- // @Success 200 {object} v1.VoteListForHouseholdResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/service/vote [get]
- func (c *Controller) VoteListForHousehold(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.VoteListForHouseholdRequest{}
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.VoteListForHouseholdQuery, 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.GetJwtTokenInfo(ctx)
- if err != nil {
- return err
- }
- // 响应数据
- resp := param_v1.VoteListForHouseholdResponse{}
- rpcReq := &v1.VoteListForHouseholdRequest{
- GardenId:req.GardenId,
- Title:req.Title,
- PageSize:req.PageSize,
- Page:req.Page,
- Uid:tokenInfo.Uid,
- }
- rpcRsp, err := pb.Garden.VoteListForHousehold(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "pb.Garden.VoteListForHousehold"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- if rpcRsp.List == nil {
- rpcRsp.List = make([]*v1.VoteListForHouseholdItem, 0)
- }
- for i, v := range rpcRsp.List {
- if v.Topics == nil {
- rpcRsp.List[i].Topics = make([]*v1.VoteTopicForHouseholdItem, 0)
- }
- for j, w := range v.Topics {
- if w.ChoiceItems == nil {
- rpcRsp.List[i].Topics[j].ChoiceItems = make([]*v1.VoteTopicChoiceItem, 0)
- }
- if w.ChoiceAnswers == nil {
- rpcRsp.List[i].Topics[j].ChoiceAnswers = make([]string, 0)
- }
- if w.CompletionAnswers == nil {
- rpcRsp.List[i].Topics[j].CompletionAnswers = 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 " "
- // @Param garden_id query int true "小区id"
- // @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 {
- // 响应数据
- resp := param_v1.VoteResultStatisticResponse{}
- rpcReq := &v1.VoteResultStatisticRequest{
- GardenId:req.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)
- }
|