questionnaire.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_cygx/models"
  5. "hongze/hongze_cygx/utils"
  6. "strconv"
  7. "time"
  8. )
  9. // 问卷调查
  10. type QuestionnaireController struct {
  11. BaseAuthController
  12. }
  13. // @Title 详情
  14. // @Description 详情接口
  15. // @Param IsBestNew query bool false "是否获取最新的一条问卷消息"
  16. // @Param QuestionnaireId query int true "问卷ID"
  17. // @Success Ret=200 {object} cygx.GetCygxTacticsTimeLineResp
  18. // @router /detail [get]
  19. func (this *QuestionnaireController) Detail() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. user := this.User
  26. if user == nil {
  27. br.Msg = "请登录"
  28. br.ErrMsg = "请登录,用户信息为空"
  29. br.Ret = 408
  30. return
  31. }
  32. resp := new(models.GetCygxQuestionnaireDetailResp)
  33. questionnaireId, _ := this.GetInt("QuestionnaireId")
  34. isBestNew, _ := this.GetBool("IsBestNew", false)
  35. var err error
  36. detail := new(models.CygxQuestionnaireResp)
  37. //如果是最新的就获取最新的一套问卷信息
  38. if isBestNew {
  39. detail, err = models.GetCygxQuestionnaireDetailBestNew()
  40. if err != nil {
  41. br.Msg = "详情不存在"
  42. br.ErrMsg = "获取失败,GetCygxQuestionnaireDetailBestNew Err:" + err.Error()
  43. return
  44. }
  45. questionnaireId = detail.QuestionnaireId
  46. } else {
  47. detail, err = models.GetCygxQuestionnaireDetail(questionnaireId)
  48. if err != nil {
  49. br.Msg = "详情不存在"
  50. br.ErrMsg = "获取失败,GetCygxQuestionnaireDetail Err:" + err.Error()
  51. return
  52. }
  53. }
  54. ListTheme, err := models.GetCygxQuestionnaireThemeListByQuestionnaireId(questionnaireId)
  55. if err != nil {
  56. br.Msg = "详情不存在"
  57. br.ErrMsg = "获取失败,GetCygxQuestionnaireThemeListByQuestionnaireId Err:" + err.Error()
  58. return
  59. }
  60. detail.ListTheme = ListTheme
  61. resp.Detail = detail
  62. br.Ret = 200
  63. br.Success = true
  64. br.Msg = "获取成功"
  65. br.Data = resp
  66. }
  67. // @Title 问卷投票
  68. // @Description 问卷投票接口
  69. // @Param QuestionnaireId query int true "问卷ID"
  70. // @Success Ret=200 {object} cygx.GetCygxTacticsTimeLineResp
  71. // @router /vote_add [post]
  72. func (this *QuestionnaireController) VoteAdd() {
  73. br := new(models.BaseResponse).Init()
  74. defer func() {
  75. this.Data["json"] = br
  76. this.ServeJSON()
  77. }()
  78. user := this.User
  79. if user == nil {
  80. br.Msg = "请登录"
  81. br.ErrMsg = "请登录,用户信息为空"
  82. br.Ret = 408
  83. return
  84. }
  85. uid := user.UserId
  86. var req models.CygxQuestionnaireVoteReq
  87. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  88. if err != nil {
  89. br.Msg = "参数解析异常!"
  90. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  91. return
  92. }
  93. if uid == 0 {
  94. br.Msg = "请登录"
  95. br.ErrMsg = "请登录,用户信息为空"
  96. br.Ret = 408
  97. }
  98. questionnaireId := req.QuestionnaireId
  99. questionnaireThemeIds := req.QuestionnaireThemeIds
  100. content := req.Content
  101. if len(questionnaireThemeIds) == 0 && content == "" {
  102. br.Msg = "提交信息不能为空"
  103. return
  104. }
  105. detail, err := models.GetCygxQuestionnaireDetail(questionnaireId)
  106. if err != nil {
  107. br.Msg = "提交失败"
  108. br.ErrMsg = "获取失败,GetCygxQuestionnaireDetail Err:" + err.Error()
  109. return
  110. }
  111. endTime := utils.StrTimeToTime(detail.EndTime + " 23:59:59") //时间字符串格式转时间格式
  112. if endTime.Before(time.Now()) {
  113. br.Msg = "此次投票已截止"
  114. return
  115. }
  116. lenCheck := len(questionnaireThemeIds)
  117. if lenCheck > detail.MaxChooseTotal {
  118. br.Msg = "最多可选择" + strconv.Itoa(detail.MaxChooseTotal) + "项"
  119. return
  120. }
  121. sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  122. if err != nil && err.Error() != utils.ErrNoRow() {
  123. return
  124. }
  125. var voteItems []*models.CygxQuestionnaireVote
  126. //主题ID提交问卷需要写入的信息
  127. if lenCheck > 0 {
  128. var condition string
  129. var pars []interface{}
  130. condition += " AND user_id = ? AND questionnaire_id = ? "
  131. pars = append(pars, user.UserId, questionnaireId)
  132. totalhistory, err := models.GetCygxQuestionnaireVoteCount(condition, pars)
  133. if err != nil {
  134. br.Msg = "提交失败"
  135. br.ErrMsg = "获取失败,GetCygxQuestionnaireDetail Err:" + err.Error()
  136. return
  137. }
  138. if totalhistory > 0 {
  139. br.Msg = "您已投过票"
  140. return
  141. }
  142. for _, v := range questionnaireThemeIds {
  143. item := new(models.CygxQuestionnaireVote)
  144. item.QuestionnaireId = questionnaireId
  145. item.QuestionnaireThemeId = v
  146. item.UserId = user.UserId
  147. item.CreateTime = time.Now()
  148. item.ModifyTime = time.Now()
  149. item.Mobile = user.Mobile
  150. item.Email = user.Email
  151. item.CompanyId = user.CompanyId
  152. item.CompanyName = user.CompanyName
  153. item.RealName = user.RealName
  154. if sellerItem != nil {
  155. item.SellerName = sellerItem.RealName
  156. }
  157. item.RegisterPlatform = utils.REGISTER_PLATFORM
  158. voteItems = append(voteItems, item)
  159. }
  160. }
  161. //其它主题类型提交需要写入的信息
  162. itemOther := new(models.CygxQuestionnaireVoteOtherTheme)
  163. if content != "" {
  164. itemOther.QuestionnaireId = questionnaireId
  165. itemOther.UserId = user.UserId
  166. itemOther.CreateTime = time.Now()
  167. itemOther.ModifyTime = time.Now()
  168. itemOther.Mobile = user.Mobile
  169. itemOther.Email = user.Email
  170. itemOther.CompanyId = user.CompanyId
  171. itemOther.CompanyName = user.CompanyName
  172. itemOther.RealName = user.RealName
  173. itemOther.Content = content
  174. if sellerItem != nil {
  175. itemOther.SellerName = sellerItem.RealName
  176. }
  177. itemOther.RegisterPlatform = utils.REGISTER_PLATFORM
  178. }
  179. err = models.AddCygxQuestionnaireVote(voteItems, itemOther)
  180. if err != nil {
  181. br.Msg = "提交失败"
  182. br.ErrMsg = "获取失败,AddCygxQuestionnaireVote Err:" + err.Error()
  183. return
  184. }
  185. br.Ret = 200
  186. br.Success = true
  187. br.Msg = "提交成功"
  188. }