mechanism.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package survey_mechanism
  2. import (
  3. "surveyService/model"
  4. "surveyService/response"
  5. "surveyService/service/questionnaire_survey"
  6. "surveyService/validators"
  7. "gogs.uu.mdfitnesscao.com/cuiguohai/sdk"
  8. )
  9. type Mechanism struct {
  10. *sdk.AuthMechanism
  11. }
  12. func InitMechanism(authMechanism *sdk.AuthMechanism) *Mechanism {
  13. return &Mechanism{authMechanism}
  14. }
  15. // 获取问卷详情
  16. func (m *Mechanism) Detail(sn string) (*model.SurveyMechanism, *response.ErrCode) {
  17. var surveyMechanismModel model.SurveyMechanism
  18. findErr := model.DB.Where("sn = ?", sn).Preload("Survey").Where("mechanism_id = ?", m.Mechanism.ID).First(&surveyMechanismModel).Error
  19. if findErr != nil {
  20. return nil, &response.ErrCode{
  21. Code: response.ERROR,
  22. Msg: "没有找到有效的问卷信息",
  23. }
  24. }
  25. // 检查当前问卷是否已被授权
  26. if surveyMechanismModel.AuthorizeStatus != model.SURVEY_MECHANISM_AUTHORIZE_STATUS_ENABLE {
  27. return nil, &response.ErrCode{
  28. Code: response.ERROR,
  29. Msg: "当前问卷已被停止授权,无法操作该问卷",
  30. }
  31. }
  32. return &surveyMechanismModel, nil
  33. }
  34. // 设置问卷信息
  35. func (m *Mechanism) Update(surveyMechanism *validators.SurveyMechanism) *response.ErrCode {
  36. existsSurveyMechanism, findErr := m.Detail(surveyMechanism.ID)
  37. if findErr != nil {
  38. return findErr
  39. }
  40. // 开始修改
  41. updateErr := model.DB.Where("id = ?", existsSurveyMechanism.ID).Select("name", "cover", "description").Updates(&model.SurveyMechanism{
  42. Name: surveyMechanism.Name,
  43. Cover: surveyMechanism.Cover,
  44. Description: surveyMechanism.Description,
  45. }).Error
  46. if updateErr != nil {
  47. return &response.ErrCode{
  48. Code: response.ERROR,
  49. Msg: "修改问卷信息失败",
  50. }
  51. }
  52. return nil
  53. }
  54. // 修改问卷状态
  55. func (m *Mechanism) UpdateStatus(sn string, status int) *response.ErrCode {
  56. if status != model.SURVEY_MECHANISM_STATUS_ENABLE && status != model.SURVEY_MECHANISM_STATUS_DISABLE {
  57. return &response.ErrCode{
  58. Code: response.ERROR,
  59. Msg: "不支持的状态",
  60. }
  61. }
  62. existsSurveyMechanism, findErr := m.Detail(sn)
  63. if findErr != nil {
  64. return findErr
  65. }
  66. // 修改状态
  67. updateErr := model.DB.Model(model.SurveyMechanism{}).Where("id = ?", existsSurveyMechanism.ID).Update("status", status).Error
  68. if updateErr != nil {
  69. return &response.ErrCode{
  70. Code: response.ERROR,
  71. Msg: "修改状态失败",
  72. }
  73. }
  74. return nil
  75. }
  76. // 获取问卷列表
  77. func (m *Mechanism) Paginate(page, pageSize int, status int, key string) ([]*model.SurveyMechanism, int64, *response.ErrCode) {
  78. var surveyMechanismList []*model.SurveyMechanism
  79. var count int64
  80. // 开始查询
  81. query := model.DB.Model(model.SurveyMechanism{}).Where("mechanism_id = ?", m.Mechanism.ID)
  82. if status != 0 {
  83. query = query.Where("status = ?", status)
  84. }
  85. if key != "" {
  86. query = query.Where("name like @key or description like @key", map[string]any{
  87. "key": "%" + key + "%",
  88. })
  89. }
  90. query.Count(&count)
  91. // 开始分页
  92. query.Scopes(model.Paginate(page, pageSize)).Preload("Survey").Order("id desc").Find(&surveyMechanismList)
  93. return surveyMechanismList, count, nil
  94. }
  95. // 获取问卷列表
  96. func (m *Mechanism) List(status int, key string) []*model.SurveyMechanism {
  97. var surveyMechanismList []*model.SurveyMechanism
  98. // 开始查询
  99. query := model.DB.Model(model.SurveyMechanism{}).Where("mechanism_id = ?", m.Mechanism.ID)
  100. if status != 0 {
  101. query = query.Where("status = ?", status)
  102. }
  103. if key != "" {
  104. query = query.Where("name like @key or description like @key", map[string]any{
  105. "key": "%" + key + "%",
  106. })
  107. }
  108. // 开始分页
  109. query.Preload("Survey").Order("id desc").Find(&surveyMechanismList)
  110. return surveyMechanismList
  111. }
  112. // 获取问卷详情
  113. func (m *Mechanism) DetailSurveyConfig(sn string) (*validators.QuestionnaireSurvey, *response.ErrCode) {
  114. surveyMechanism, findErr := m.Detail(sn)
  115. if findErr != nil {
  116. return nil, findErr
  117. }
  118. // if surveyMechanism.Survey.SurveyCode == "" {
  119. // return nil, &response.ErrCode{
  120. // Code: response.ERROR,
  121. // Msg: "当前问卷未配置关联模型",
  122. // }
  123. // }
  124. surveyCode := "DZTJV1"
  125. if surveyMechanism.Survey.SurveyCode != "" {
  126. surveyCode = surveyMechanism.Survey.SurveyCode
  127. }
  128. // 获取问卷配置信息
  129. surveyConfig, detailErr := questionnaire_survey.FindBySN(surveyCode)
  130. if detailErr != nil {
  131. return nil, &response.ErrCode{
  132. Code: response.ERROR,
  133. Msg: "获取问卷配置失败",
  134. }
  135. }
  136. return questionnaire_survey.Format(surveyConfig), nil
  137. }