survey_mechanism.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package survey_mechanism
  2. import (
  3. "surveyService/model"
  4. "surveyService/response"
  5. "surveyService/service/survey"
  6. "surveyService/validators"
  7. "github.com/golang-module/carbon"
  8. "github.com/samber/lo"
  9. "gogs.uu.mdfitnesscao.com/cuiguohai/sdk"
  10. "gogs.uu.mdfitnesscao.com/cuiguohai/sdk/mechanism"
  11. )
  12. // 获取某个问卷已被授权的机构列表
  13. func ListMechanismIdsForSurvey(surveyId string) ([]string, *response.ErrCode) {
  14. var mechanismIds []string
  15. // 检查问卷是否真实存在
  16. surveyModel, findErr := survey.FindBySn(surveyId)
  17. if findErr != nil {
  18. return nil, findErr
  19. }
  20. model.DB.Where("survey_id = ?", surveyModel.ID).Pluck("mechanism_id", &mechanismIds)
  21. return mechanismIds, nil
  22. }
  23. // 获取问卷授权信息
  24. func FindBySn(sn string) (*model.SurveyMechanism, *response.ErrCode) {
  25. var surveyMechanismModel model.SurveyMechanism
  26. findErr := model.DB.Where("sn = ?", sn).First(&surveyMechanismModel).Error
  27. if findErr != nil {
  28. return nil, &response.ErrCode{
  29. Code: response.ERROR,
  30. Msg: "问卷授权不存在",
  31. }
  32. }
  33. return &surveyMechanismModel, nil
  34. }
  35. // 开始创建问卷授权
  36. func Create(surveyId string, mechanismIds []string) *response.ErrCode {
  37. // 检查问卷是否真实存在
  38. surveyModel, findErr := survey.FindBySn(surveyId)
  39. if findErr != nil {
  40. return findErr
  41. }
  42. // 过滤掉所有跟问卷已经授权的机构
  43. var existMechanismIds []string
  44. model.DB.Model(&model.SurveyMechanism{}).Where("survey_id = ?", surveyModel.ID).Pluck("mechanism_id", &existMechanismIds)
  45. mechanismIds, _ = lo.Difference(mechanismIds, existMechanismIds)
  46. if len(mechanismIds) == 0 {
  47. return &response.ErrCode{
  48. Code: response.ERROR,
  49. Msg: "没有需要授权的机构",
  50. }
  51. }
  52. // 开始创建授权
  53. var surveyMechanisms []*model.SurveyMechanism
  54. for _, mechanismId := range mechanismIds {
  55. surveyMechanisms = append(surveyMechanisms, &model.SurveyMechanism{
  56. SurveyId: surveyModel.ID,
  57. MechanismId: mechanismId,
  58. })
  59. }
  60. createErr := model.DB.CreateInBatches(surveyMechanisms, len(surveyMechanisms)).Error
  61. if createErr != nil {
  62. return &response.ErrCode{
  63. Code: response.ERROR,
  64. Msg: "创建问卷授权失败",
  65. }
  66. }
  67. return nil
  68. }
  69. // 获取授权列表
  70. func Paginate(page, pageSize int, key string, mechanismId string, surveyId string) ([]*model.SurveyMechanism, int64, *response.ErrCode) {
  71. var surveyMechanisms []*model.SurveyMechanism
  72. var total int64
  73. // 开始查询
  74. query := model.DB.Model(&model.SurveyMechanism{})
  75. // 模糊搜索
  76. if key != "" {
  77. query = query.Where("name LIKE @key or description like @key", map[string]any{
  78. "key": "%" + key + "%",
  79. })
  80. }
  81. // 机构ID
  82. if mechanismId != "" {
  83. query = query.Where("mechanism_id = ?", mechanismId)
  84. }
  85. // 问卷ID
  86. if surveyId != "" {
  87. // 检查问卷是否真实存在
  88. surveyModel, findErr := survey.FindBySn(surveyId)
  89. if findErr != nil {
  90. return surveyMechanisms, total, findErr
  91. }
  92. query = query.Where("survey_id = ?", surveyModel.ID)
  93. }
  94. // 开始分页
  95. paginateErr := query.Count(&total).Error
  96. if paginateErr != nil {
  97. return nil, 0, &response.ErrCode{
  98. Code: response.ERROR,
  99. Msg: "获取授权列表失败",
  100. }
  101. }
  102. query.Scopes(model.Paginate(page, pageSize)).Order("id desc").Preload("Survey").Find(&surveyMechanisms)
  103. return surveyMechanisms, total, nil
  104. }
  105. // 修改授权状态
  106. func UpdateAuthorizeStatus(sn string, status int) *response.ErrCode {
  107. if status != model.SURVEY_MECHANISM_AUTHORIZE_STATUS_ENABLE && status != model.SURVEY_MECHANISM_AUTHORIZE_STATUS_DISABLE {
  108. return &response.ErrCode{
  109. Code: response.ERROR,
  110. Msg: "不支持的状态",
  111. }
  112. }
  113. existsSurveyMechanism, findErr := FindBySn(sn)
  114. if findErr != nil {
  115. return findErr
  116. }
  117. // 修改状态
  118. updateErr := model.DB.Model(model.SurveyMechanism{}).Where("id = ?", existsSurveyMechanism.ID).Update("authorize_status", status).Error
  119. if updateErr != nil {
  120. return &response.ErrCode{
  121. Code: response.ERROR,
  122. Msg: "修改授权状态失败",
  123. }
  124. }
  125. return nil
  126. }
  127. // 批量格式化
  128. func ListFormat(surveyMechanisms []*model.SurveyMechanism) []*validators.SurveyMechanism {
  129. formatedSurveyMechanisms := make([]*validators.SurveyMechanism, 0)
  130. var mechanismIds []string
  131. for _, surveyMechanism := range surveyMechanisms {
  132. if surveyMechanism != nil {
  133. mechanismIds = append(mechanismIds, surveyMechanism.MechanismId)
  134. }
  135. }
  136. mechanismIds = lo.Uniq(mechanismIds)
  137. // 获取机构列表
  138. mechanisms, _ := mechanism.ListMechanismByIds(mechanismIds)
  139. for _, surveyMechanism := range surveyMechanisms {
  140. var mechanism *sdk.Mechanism
  141. // 找到机构
  142. _, index, _ := lo.FindIndexOf(mechanisms, func(mechanismItem *sdk.Mechanism) bool {
  143. return mechanismItem.ID == surveyMechanism.MechanismId
  144. })
  145. if index >= 0 {
  146. mechanism = mechanisms[index]
  147. formatedSurveyMechanisms = append(formatedSurveyMechanisms, Format(surveyMechanism, mechanism, true))
  148. }
  149. }
  150. return formatedSurveyMechanisms
  151. }
  152. // 格式化
  153. func Format(surveyMechanism *model.SurveyMechanism, mechanism *sdk.Mechanism, visibleSurvey bool) *validators.SurveyMechanism {
  154. if surveyMechanism == nil {
  155. return nil
  156. }
  157. formatedSurvey := survey.Format(&surveyMechanism.Survey)
  158. surveyId := formatedSurvey.ID
  159. name := surveyMechanism.Name
  160. cover := surveyMechanism.Cover
  161. if name == "" {
  162. name = formatedSurvey.Name
  163. }
  164. if cover == "" {
  165. cover = formatedSurvey.Cover
  166. }
  167. if !visibleSurvey {
  168. formatedSurvey = nil
  169. surveyId = ""
  170. }
  171. return &validators.SurveyMechanism{
  172. ID: surveyMechanism.SN,
  173. AuthorizeStatus: surveyMechanism.AuthorizeStatus,
  174. Status: surveyMechanism.Status,
  175. MechanismId: surveyMechanism.MechanismId,
  176. Mechanism: mechanism,
  177. SurveyId: surveyId,
  178. Survey: formatedSurvey,
  179. CreatedAt: carbon.Time2Carbon(surveyMechanism.CreatedAt).Format("Y/m/d H:i:s"),
  180. UpdatedAt: carbon.Time2Carbon(surveyMechanism.UpdatedAt).Format("Y/m/d H:i:s"),
  181. Permissions: surveyMechanism.Permissions,
  182. Name: name,
  183. Cover: cover,
  184. Description: surveyMechanism.Description,
  185. }
  186. }