questionnaire.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package cygx
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. type CygxQuestionnaire struct {
  10. QuestionnaireId int `orm:"column(questionnaire_id);pk"`
  11. ActivityTypeId int `description:"活动类型id"`
  12. ActivityTypeName string `description:"活动名称"`
  13. Content string `description:"内容"`
  14. EndTime time.Time `description:"截止日期"`
  15. CreateTime time.Time `description:"创建时间"`
  16. ModifyTime time.Time `description:"更新时间"`
  17. PublishTime time.Time `description:"发布时间"`
  18. AdminId int `description:"管理员ID"`
  19. MaxChooseTotal int `description:"最多可选几项"`
  20. OtherThemeTotal int `description:"其余主题投票数量"`
  21. }
  22. type CygxQuestionnaireTheme struct {
  23. QuestionnaireThemeId int `orm:"column(questionnaire_theme_id);pk"`
  24. QuestionnaireId int `description:"问卷ID"`
  25. ActivityTheme string `description:"活动主题"`
  26. CreateTime time.Time `description:"创建时间"`
  27. ModifyTime time.Time `description:"更新时间"`
  28. }
  29. type AddCygxQuestionnaireReq struct {
  30. QuestionnaireId int `description:"ID"`
  31. ActivityTypeId int `description:"活动类型id"`
  32. Content string `description:"内容"`
  33. EndTime string `description:"截止日期"`
  34. MaxChooseTotal int `description:"最多可选几项"`
  35. ListTheme []*AddCygxQuestionnaireThemeReq
  36. }
  37. type AddCygxQuestionnaireThemeReq struct {
  38. QuestionnaireThemeId int `description:"问卷主题ID"`
  39. ActivityTheme string `description:"活动主题"`
  40. VoteTotal int `description:"票数"`
  41. }
  42. type CygxQuestionnaireResp struct {
  43. QuestionnaireId int `orm:"column(questionnaire_id);pk"`
  44. ActivityTypeId int `description:"活动类型id"`
  45. ActivityTypeName string `description:"活动类型名称"`
  46. Content string `description:"内容"`
  47. EndTime string `description:"截止日期"`
  48. CreateTime string `description:"创建时间"`
  49. ModifyTime string `description:"更新时间"`
  50. PublishTime string `description:"发布时间"`
  51. AdminId int `description:"管理员ID"`
  52. MaxChooseTotal int `description:"最多可选几项"`
  53. State string `description:"状态"`
  54. OtherThemeTotal int `description:"其余主题投票数量"`
  55. ListTheme []*AddCygxQuestionnaireThemeReq
  56. }
  57. type GetCygxQuestionnaireResp struct {
  58. Paging *paging.PagingItem `description:"分页数据"`
  59. List []*CygxQuestionnaireResp
  60. }
  61. // 添加
  62. func AddCygxQuestionnaire(item *CygxQuestionnaire, listTheme []*AddCygxQuestionnaireThemeReq) (newId int64, err error) {
  63. o := orm.NewOrmUsingDB("hz_cygx")
  64. to, err := o.Begin()
  65. if err != nil {
  66. return
  67. }
  68. defer func() {
  69. if err != nil {
  70. _ = to.Rollback()
  71. } else {
  72. _ = to.Commit()
  73. }
  74. }()
  75. newId, err = to.Insert(item)
  76. if err != nil {
  77. return
  78. }
  79. //添加主题与问卷ID的关联
  80. for _, v := range listTheme {
  81. itemTheme := new(CygxQuestionnaireTheme)
  82. itemTheme.QuestionnaireId = int(newId)
  83. itemTheme.ActivityTheme = v.ActivityTheme
  84. itemTheme.CreateTime = time.Now()
  85. itemTheme.ModifyTime = time.Now()
  86. _, err = to.Insert(itemTheme)
  87. if err != nil {
  88. return
  89. }
  90. }
  91. return
  92. }
  93. // 编辑
  94. func EditCygxQuestionnaire(item *CygxQuestionnaire, listTheme []*AddCygxQuestionnaireThemeReq) (err error) {
  95. o := orm.NewOrmUsingDB("hz_cygx")
  96. to, err := o.Begin()
  97. if err != nil {
  98. return
  99. }
  100. defer func() {
  101. if err != nil {
  102. _ = to.Rollback()
  103. } else {
  104. _ = to.Commit()
  105. }
  106. }()
  107. updateParams := make(map[string]interface{})
  108. updateParams["ActivityTypeId"] = item.ActivityTypeId
  109. updateParams["ActivityTypeName"] = item.ActivityTypeName
  110. updateParams["EndTime"] = item.EndTime
  111. updateParams["ModifyTime"] = time.Now()
  112. updateParams["MaxChooseTotal"] = item.MaxChooseTotal
  113. updateParams["Content"] = item.Content
  114. ptrStructOrTableName := "cygx_questionnaire"
  115. whereParam := map[string]interface{}{"questionnaire_id": item.QuestionnaireId}
  116. qs := to.QueryTable(ptrStructOrTableName)
  117. for expr, exprV := range whereParam {
  118. qs = qs.Filter(expr, exprV)
  119. }
  120. _, err = qs.Update(updateParams)
  121. if err != nil {
  122. return
  123. }
  124. //如果有减少的就进行删除
  125. var questionnaireThemeIds []string
  126. for _, v := range listTheme {
  127. questionnaireThemeIds = append(questionnaireThemeIds, strconv.Itoa(v.QuestionnaireThemeId))
  128. }
  129. sql := ` DELETE FROM cygx_questionnaire_theme WHERE questionnaire_id = ? AND questionnaire_theme_id NOT IN (` + strings.Join(questionnaireThemeIds, ",") + `)`
  130. _, err = to.Raw(sql, item.QuestionnaireId).Exec()
  131. if err != nil {
  132. return
  133. }
  134. //添加主题与问卷ID的关联
  135. for _, v := range listTheme {
  136. if v.QuestionnaireThemeId == 0 {
  137. itemTheme := new(CygxQuestionnaireTheme)
  138. itemTheme.QuestionnaireId = item.QuestionnaireId
  139. itemTheme.ActivityTheme = v.ActivityTheme
  140. itemTheme.CreateTime = time.Now()
  141. itemTheme.ModifyTime = time.Now()
  142. _, err = to.Insert(itemTheme)
  143. if err != nil {
  144. return
  145. }
  146. } else {
  147. updateParams := make(map[string]interface{})
  148. updateParams["ActivityTheme"] = v.ActivityTheme
  149. updateParams["ModifyTime"] = time.Now()
  150. ptrStructOrTableName := "cygx_questionnaire_theme"
  151. whereParam := map[string]interface{}{"questionnaire_theme_id": v.QuestionnaireThemeId}
  152. qs := to.QueryTable(ptrStructOrTableName)
  153. for expr, exprV := range whereParam {
  154. qs = qs.Filter(expr, exprV)
  155. }
  156. _, err = qs.Update(updateParams)
  157. if err != nil {
  158. return
  159. }
  160. }
  161. }
  162. return
  163. }
  164. // 获取数量
  165. func GetCygxQuestionnaireCount(condition string, pars []interface{}) (count int, err error) {
  166. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_questionnaire as art WHERE 1= 1 `
  167. if condition != "" {
  168. sqlCount += condition
  169. }
  170. o := orm.NewOrmUsingDB("hz_cygx")
  171. err = o.Raw(sqlCount, pars).QueryRow(&count)
  172. return
  173. }
  174. // 列表
  175. func GetCygxQuestionnaireList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxQuestionnaireResp, err error) {
  176. o := orm.NewOrmUsingDB("hz_cygx")
  177. sql := `SELECT * FROM cygx_questionnaire as art WHERE 1= 1 `
  178. if condition != "" {
  179. sql += condition
  180. }
  181. sql += ` LIMIT ?,? `
  182. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  183. return
  184. }
  185. type GetCygxQuestionnaireDetailResp struct {
  186. Detail *CygxQuestionnaireResp
  187. }
  188. // 通过ID获取详情
  189. func GetCygxQuestionnaireDetail(questionnaireId int) (item *CygxQuestionnaireResp, err error) {
  190. o := orm.NewOrmUsingDB("hz_cygx")
  191. sql := `SELECT * FROM cygx_questionnaire WHERE questionnaire_id=? `
  192. err = o.Raw(sql, questionnaireId).QueryRow(&item)
  193. return
  194. }
  195. // 列表
  196. func GetCygxQuestionnaireThemeListByQuestionnaireId(questionnaireId int) (items []*AddCygxQuestionnaireThemeReq, err error) {
  197. o := orm.NewOrmUsingDB("hz_cygx")
  198. sql := `SELECT * FROM cygx_questionnaire_theme as art WHERE questionnaire_id = ? `
  199. _, err = o.Raw(sql, questionnaireId).QueryRows(&items)
  200. return
  201. }