eta_trial_questionnaire.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package eta_trial
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type EtaTrialQuestionnaire struct {
  7. QuestionnaireId int `orm:"column(questionnaire_id);pk" description:"问卷题目id"`
  8. Question string `description:"题目"`
  9. Options string `description:"选项"`
  10. Type int `description:"1单选 2多选 3简答题"`
  11. Sort int `description:"排序"`
  12. IsMust int `description:"是否必填"`
  13. CreateTime time.Time
  14. }
  15. // Update
  16. func (item *EtaTrialQuestionnaire) Update(cols []string) (err error) {
  17. o := orm.NewOrm()
  18. _, err = o.Update(item, cols...)
  19. return
  20. }
  21. // AddEtaTrialQuestionnaire
  22. func AddEtaTrialQuestionnaire(questionnaire *EtaTrialQuestionnaire) (err error) {
  23. o := orm.NewOrm()
  24. id, err := o.Insert(questionnaire)
  25. if err != nil {
  26. return
  27. }
  28. questionnaire.QuestionnaireId = int(id)
  29. return
  30. }
  31. func GetETATrialQuestionnaireList(cond string) (items []*EtaTrialQuestionnaire, err error) {
  32. sql := `SELECT * FROM eta_trial_questionnaire AS a WHERE 1=1 `
  33. o := orm.NewOrm()
  34. if cond != "" {
  35. sql += cond
  36. }
  37. _, err = o.Raw(sql).QueryRows(&items)
  38. return
  39. }
  40. type EtaTrialQuestionnaireRespItem struct {
  41. QuestionnaireId int `orm:"column(questionnaire_id);pk" description:"问卷题目id"`
  42. Question string `description:"题目"`
  43. Type int `description:"1单选 2多选 3简答题"`
  44. Sort int `description:"排序"`
  45. Options []string `description:"选项"`
  46. IsMust int `description:"是否必填"`
  47. CreateTime string
  48. }
  49. type EtaTrialQuestionnaireResp struct {
  50. List []EtaTrialQuestionnaireRespItem
  51. }
  52. type EtaTrialQuestionnaireSaveReq struct {
  53. List []EtaTrialQuestionnaireSaveItem
  54. }
  55. type EtaTrialQuestionnaireSaveItem struct {
  56. QuestionnaireId int ` description:"问卷题目id"`
  57. Question string `description:"题目"`
  58. Options []string `description:"选项"`
  59. Sort int `description:"排序"`
  60. IsMust int `description:"是否必填 0是 1不是"`
  61. Type int `description:"1单选 2多选 3简答题"`
  62. }
  63. type EtaTrialQuestionnaireDelReq struct {
  64. QuestionnaireId int `description:"问卷题目id"`
  65. }
  66. func GetETATrialQuestionnaireById(qId int) (count int, err error) {
  67. sql := `SELECT count(1) AS count FROM eta_trial_questionnaire WHERE questionnaire_id = ? `
  68. o := orm.NewOrm()
  69. err = o.Raw(sql, qId).QueryRow(&count)
  70. return
  71. }
  72. func DelETATrialQuestionnaireById(qId int) (err error) {
  73. o := orm.NewOrm()
  74. to, err := o.Begin()
  75. if err != nil {
  76. return
  77. }
  78. defer func() {
  79. if err != nil {
  80. _ = to.Rollback()
  81. } else {
  82. _ = to.Commit()
  83. }
  84. }()
  85. //删问题
  86. sql := `DELETE FROM eta_trial_questionnaire WHERE questionnaire_id = ? `
  87. _, err = to.Raw(sql, qId).Exec()
  88. if err != nil {
  89. return
  90. }
  91. //删选项
  92. sql = `DELETE FROM eta_trial_questionnaire_options WHERE questionnaire_id = ? `
  93. _, err = to.Raw(sql, qId).Exec()
  94. if err != nil {
  95. return
  96. }
  97. //删记录
  98. sql = `DELETE FROM eta_trial_questionnaire_record WHERE questionnaire_id = ? `
  99. _, err = to.Raw(sql, qId).Exec()
  100. if err != nil {
  101. return
  102. }
  103. return
  104. }
  105. func AddETATrialQuestionnaire(questions []*EtaTrialQuestionnaire) (err error) {
  106. o := orm.NewOrm()
  107. to, err := o.Begin()
  108. if err != nil {
  109. return
  110. }
  111. defer func() {
  112. if err != nil {
  113. _ = to.Rollback()
  114. } else {
  115. _ = to.Commit()
  116. }
  117. }()
  118. //新增问题
  119. _, tmpErr := to.InsertMulti(len(questions), questions)
  120. if tmpErr != nil {
  121. err = tmpErr
  122. return
  123. }
  124. return
  125. }
  126. func (m EtaTrialQuestionnaireResp) Len() int {
  127. return len(m.List)
  128. }
  129. func (m EtaTrialQuestionnaireResp) Less(i, j int) bool {
  130. return m.List[i].Sort < m.List[j].Sort
  131. }
  132. func (m EtaTrialQuestionnaireResp) Swap(i, j int) {
  133. m.List[i], m.List[j] = m.List[j], m.List[i]
  134. }