questionnaire.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package cygx
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hz_crm_api/controllers"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/cygx"
  8. cygxService "hongze/hz_crm_api/services/cygx"
  9. "hongze/hz_crm_api/utils"
  10. "time"
  11. )
  12. // 问卷调查
  13. type QuestionnaireController struct {
  14. controllers.BaseAuthController
  15. }
  16. // @Title 新增、修改问卷内容
  17. // @Description 新增、修改问卷内容接口
  18. // @Param request body cygx.AddCygxQuestionnaireReq true "type json string"
  19. // @Success 200 {object} "保存成功"
  20. // @router /questionnaire/preserveAndEdit [post]
  21. func (this *QuestionnaireController) PreserveAndPublish() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. sysUser := this.SysUser
  28. if sysUser == nil {
  29. br.Msg = "请登录"
  30. br.ErrMsg = "请登录,SysUser Is Empty"
  31. br.Ret = 408
  32. return
  33. }
  34. var req cygx.AddCygxQuestionnaireReq
  35. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  36. if err != nil {
  37. br.Msg = "参数解析异常!"
  38. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  39. return
  40. }
  41. endTime := utils.StrDateToDate(req.EndTime) //时间字符串格式转时间格式
  42. questionnaireId := req.QuestionnaireId
  43. activityTypeId := req.ActivityTypeId
  44. content := req.Content
  45. maxChooseTotal := req.MaxChooseTotal
  46. listTheme := req.ListTheme
  47. activityTypeInfo, errType := cygx.GetActivityTypeDetailById(activityTypeId)
  48. if errType != nil {
  49. br.Msg = "请选择活动类型"
  50. br.ErrMsg = "获取活动类型信息失败,Err:" + errType.Error()
  51. return
  52. }
  53. item := new(cygx.CygxQuestionnaire)
  54. item.QuestionnaireId = req.QuestionnaireId
  55. item.ActivityTypeId = activityTypeId
  56. item.ActivityTypeName = activityTypeInfo.ActivityTypeName
  57. item.Content = content
  58. item.EndTime = endTime
  59. item.PublishTime = time.Now()
  60. item.CreateTime = time.Now()
  61. item.ModifyTime = time.Now()
  62. item.AdminId = sysUser.AdminId
  63. item.MaxChooseTotal = maxChooseTotal
  64. if questionnaireId == 0 {
  65. newId, err := cygx.AddCygxQuestionnaire(item, listTheme)
  66. if err != nil {
  67. br.Msg = "保存失败"
  68. br.ErrMsg = "保存失败,Err:" + err.Error()
  69. return
  70. }
  71. //投票发布时,给所有开通买方研选正式试用的客户,推送模板消息
  72. go cygxService.SendWxMsgCygxQuestionnaire(int(newId))
  73. } else {
  74. err = cygx.EditCygxQuestionnaire(item, listTheme)
  75. if err != nil {
  76. br.Msg = "保存失败"
  77. br.ErrMsg = "保存失败,Err:" + err.Error()
  78. return
  79. }
  80. }
  81. br.Ret = 200
  82. br.Success = true
  83. br.IsAddLog = true
  84. br.Msg = "操作成功"
  85. }
  86. // @Title 列表
  87. // @Description 列表接口
  88. // @Param PageSize query int true "每页数据条数"
  89. // @Param CurrentIndex query int true "当前页页码,从1开始"
  90. // @Success Ret=200 {object} cygx.GetCygxQuestionnaireResp
  91. // @router /questionnaire/list [get]
  92. func (this *QuestionnaireController) List() {
  93. br := new(models.BaseResponse).Init()
  94. defer func() {
  95. this.Data["json"] = br
  96. this.ServeJSON()
  97. }()
  98. sysUser := this.SysUser
  99. if sysUser == nil {
  100. br.Msg = "请登录"
  101. br.ErrMsg = "请登录,SysUser Is Empty"
  102. br.Ret = 408
  103. return
  104. }
  105. resp := new(cygx.GetCygxQuestionnaireResp)
  106. pageSize, _ := this.GetInt("PageSize")
  107. currentIndex, _ := this.GetInt("CurrentIndex")
  108. var startSize int
  109. if pageSize <= 0 {
  110. pageSize = utils.PageSize20
  111. }
  112. if currentIndex <= 0 {
  113. currentIndex = 1
  114. }
  115. startSize = utils.StartIndex(currentIndex, pageSize)
  116. var condition string
  117. var pars []interface{}
  118. total, err := cygx.GetCygxQuestionnaireCount(condition, pars)
  119. if err != nil {
  120. br.Msg = "获取失败"
  121. br.ErrMsg = "获取失败,Err:" + err.Error()
  122. return
  123. }
  124. condition += " ORDER BY art.publish_time DESC "
  125. list, err := cygx.GetCygxQuestionnaireList(condition, pars, startSize, pageSize)
  126. if err != nil {
  127. br.Msg = "获取失败"
  128. br.ErrMsg = "获取失败,Err:" + err.Error()
  129. return
  130. }
  131. for _, v := range list {
  132. endTime := utils.StrTimeToTime(v.EndTime + " 23:59:59") //时间字符串格式转时间格式
  133. if endTime.Before(time.Now()) {
  134. v.State = "已截止"
  135. } else {
  136. v.State = "发布中"
  137. }
  138. }
  139. page := paging.GetPaging(currentIndex, pageSize, total)
  140. resp.List = list
  141. resp.Paging = page
  142. br.Ret = 200
  143. br.Success = true
  144. br.Msg = "获取成功"
  145. br.Data = resp
  146. }
  147. // @Title 详情
  148. // @Description 详情接口
  149. // @Param QuestionnaireId query int true "问卷ID"
  150. // @Success Ret=200 {object} cygx.GetCygxTacticsTimeLineResp
  151. // @router /questionnaire/detail [get]
  152. func (this *QuestionnaireController) Detail() {
  153. br := new(models.BaseResponse).Init()
  154. defer func() {
  155. this.Data["json"] = br
  156. this.ServeJSON()
  157. }()
  158. sysUser := this.SysUser
  159. if sysUser == nil {
  160. br.Msg = "请登录"
  161. br.ErrMsg = "请登录,SysUser Is Empty"
  162. br.Ret = 408
  163. return
  164. }
  165. resp := new(cygx.GetCygxQuestionnaireDetailResp)
  166. questionnaireId, _ := this.GetInt("QuestionnaireId")
  167. detail, err := cygx.GetCygxQuestionnaireDetail(questionnaireId)
  168. if err != nil {
  169. br.Msg = "详情不存在"
  170. br.ErrMsg = "获取失败,GetCygxQuestionnaireDetail Err:" + err.Error()
  171. return
  172. }
  173. ListTheme, err := cygx.GetCygxQuestionnaireThemeListByQuestionnaireId(questionnaireId)
  174. if err != nil {
  175. br.Msg = "详情不存在"
  176. br.ErrMsg = "获取失败,GetCygxQuestionnaireThemeListByQuestionnaireId Err:" + err.Error()
  177. return
  178. }
  179. detail.ListTheme = ListTheme
  180. resp.Detail = detail
  181. br.Ret = 200
  182. br.Success = true
  183. br.Msg = "获取成功"
  184. br.Data = resp
  185. }
  186. // @Title 投票结果
  187. // @Description 投票结果接口
  188. // @Param QuestionnaireId query int true "问卷ID"
  189. // @Success Ret=200 {object} cygx.CygxQuestionnaireVoteListResp
  190. // @router /questionnaire/vote_detail [get]
  191. func (this *QuestionnaireController) VoteDetail() {
  192. br := new(models.BaseResponse).Init()
  193. defer func() {
  194. this.Data["json"] = br
  195. this.ServeJSON()
  196. }()
  197. sysUser := this.SysUser
  198. if sysUser == nil {
  199. br.Msg = "请登录"
  200. br.ErrMsg = "请登录,SysUser Is Empty"
  201. br.Ret = 408
  202. return
  203. }
  204. resp := new(cygx.CygxQuestionnaireVoteListResp)
  205. questionnaireId, _ := this.GetInt("QuestionnaireId")
  206. listTheme, err := cygx.GetCygxQuestionnaireThemeListByQuestionnaireId(questionnaireId)
  207. if err != nil {
  208. br.Msg = "详情不存在"
  209. br.ErrMsg = "获取失败,GetCygxQuestionnaireThemeListByQuestionnaireId Err:" + err.Error()
  210. return
  211. }
  212. listVote, err := cygx.GetCygxCygxQuestionnaireVoteListByQuestionnaireId(questionnaireId)
  213. if err != nil {
  214. br.Msg = "详情不存在"
  215. br.ErrMsg = "获取失败,GetCygxCygxQuestionnaireVoteListByQuestionnaireId Err:" + err.Error()
  216. return
  217. }
  218. //计算每个主题的投票数量
  219. mapVoteTotal := make(map[int]int)
  220. for _, v := range listVote {
  221. mapVoteTotal[v.QuestionnaireThemeId] += 1
  222. }
  223. for _, v := range listTheme {
  224. item := new(cygx.AddCygxQuestionnaireThemeReq)
  225. item.QuestionnaireThemeId = v.QuestionnaireThemeId
  226. item.ActivityTheme = v.ActivityTheme
  227. item.VoteTotal = mapVoteTotal[v.QuestionnaireThemeId]
  228. resp.List = append(resp.List, item)
  229. }
  230. if len(resp.List) == 0 {
  231. resp.List = make([]*cygx.AddCygxQuestionnaireThemeReq, 0)
  232. }
  233. br.Ret = 200
  234. br.Success = true
  235. br.Msg = "获取成功"
  236. br.Data = resp
  237. }
  238. // @Title 票数明细
  239. // @Description 票数明细接口
  240. // @Param QuestionnaireThemeId query int true "主题问卷ID"
  241. // @Success Ret=200 {object} cygx.GetCygxTacticsTimeLineResp
  242. // @router /questionnaire/vote_list [get]
  243. func (this *QuestionnaireController) VoteList() {
  244. br := new(models.BaseResponse).Init()
  245. defer func() {
  246. this.Data["json"] = br
  247. this.ServeJSON()
  248. }()
  249. sysUser := this.SysUser
  250. if sysUser == nil {
  251. br.Msg = "请登录"
  252. br.ErrMsg = "请登录,SysUser Is Empty"
  253. br.Ret = 408
  254. return
  255. }
  256. resp := new(cygx.CygxCygxQuestionnaireVoteListResp)
  257. questionnaireThemeId, _ := this.GetInt("QuestionnaireThemeId")
  258. listVote, err := cygx.GetCygxQuestionnaireVoteListByQuestionnaireThemeId(questionnaireThemeId)
  259. if err != nil {
  260. br.Msg = "详情不存在"
  261. br.ErrMsg = "获取失败,GetCygxQuestionnaireVoteListByQuestionnaireThemeId Err:" + err.Error()
  262. return
  263. }
  264. resp.List = listVote
  265. if len(resp.List) == 0 {
  266. resp.List = make([]*cygx.CygxQuestionnaireVoteResp, 0)
  267. }
  268. br.Ret = 200
  269. br.Success = true
  270. br.Msg = "获取成功"
  271. br.Data = resp
  272. }
  273. // @Title 其余主题投票结果
  274. // @Description 其余主题投票结果接口
  275. // @Param QuestionnaireId query int true "问卷ID"
  276. // @Success Ret=200 {object} cygx.CygxQuestionnaireVoteListResp
  277. // @router /questionnaire/vote_other [get]
  278. func (this *QuestionnaireController) VoteOther() {
  279. br := new(models.BaseResponse).Init()
  280. defer func() {
  281. this.Data["json"] = br
  282. this.ServeJSON()
  283. }()
  284. sysUser := this.SysUser
  285. if sysUser == nil {
  286. br.Msg = "请登录"
  287. br.ErrMsg = "请登录,SysUser Is Empty"
  288. br.Ret = 408
  289. return
  290. }
  291. resp := new(cygx.CygxQuestionnaireVoteOtherThemeListResp)
  292. questionnaireId, _ := this.GetInt("QuestionnaireId")
  293. listOther, err := cygx.GetCygxQuestionnaireVoteOtherThemeListByQuestionnaireId(questionnaireId)
  294. if err != nil {
  295. br.Msg = "详情不存在"
  296. br.ErrMsg = "获取失败,GetCygxCygxQuestionnaireVoteListByQuestionnaireId Err:" + err.Error()
  297. return
  298. }
  299. resp.List = listOther
  300. if len(resp.List) == 0 {
  301. resp.List = make([]*cygx.CygxQuestionnaireVoteOtherThemeResp, 0)
  302. }
  303. br.Ret = 200
  304. br.Success = true
  305. br.Msg = "获取成功"
  306. br.Data = resp
  307. }