123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- package survey_mechanism
- import (
- "surveyService/model"
- "surveyService/response"
- "surveyService/service/survey"
- "surveyService/validators"
- "github.com/golang-module/carbon"
- "github.com/samber/lo"
- "gogs.uu.mdfitnesscao.com/hys/sdk"
- "gogs.uu.mdfitnesscao.com/hys/sdk/mechanism"
- )
- // 获取某个问卷已被授权的机构列表
- func ListMechanismIdsForSurvey(surveyId string) ([]string, *response.ErrCode) {
- var mechanismIds []string
- // 检查问卷是否真实存在
- surveyModel, findErr := survey.FindBySn(surveyId)
- if findErr != nil {
- return nil, findErr
- }
- model.DB.Where("survey_id = ?", surveyModel.ID).Pluck("mechanism_id", &mechanismIds)
- return mechanismIds, nil
- }
- // 获取问卷授权信息
- func FindBySn(sn string) (*model.SurveyMechanism, *response.ErrCode) {
- var surveyMechanismModel model.SurveyMechanism
- findErr := model.DB.Where("sn = ?", sn).First(&surveyMechanismModel).Error
- if findErr != nil {
- return nil, &response.ErrCode{
- Code: response.ERROR,
- Msg: "问卷授权不存在",
- }
- }
- return &surveyMechanismModel, nil
- }
- // 开始创建问卷授权
- func Create(surveyId string, mechanismIds []string) *response.ErrCode {
- // 检查问卷是否真实存在
- surveyModel, findErr := survey.FindBySn(surveyId)
- if findErr != nil {
- return findErr
- }
- // 过滤掉所有跟问卷已经授权的机构
- var existMechanismIds []string
- model.DB.Model(&model.SurveyMechanism{}).Where("survey_id = ?", surveyModel.ID).Pluck("mechanism_id", &existMechanismIds)
- mechanismIds, _ = lo.Difference(mechanismIds, existMechanismIds)
- if len(mechanismIds) == 0 {
- return &response.ErrCode{
- Code: response.ERROR,
- Msg: "没有需要授权的机构",
- }
- }
- // 开始创建授权
- var surveyMechanisms []*model.SurveyMechanism
- for _, mechanismId := range mechanismIds {
- surveyMechanisms = append(surveyMechanisms, &model.SurveyMechanism{
- SurveyId: surveyModel.ID,
- MechanismId: mechanismId,
- })
- }
- createErr := model.DB.CreateInBatches(surveyMechanisms, len(surveyMechanisms)).Error
- if createErr != nil {
- return &response.ErrCode{
- Code: response.ERROR,
- Msg: "创建问卷授权失败",
- }
- }
- return nil
- }
- // 获取授权列表
- func Paginate(page, pageSize int, key string, mechanismId string, surveyId string) ([]*model.SurveyMechanism, int64, *response.ErrCode) {
- var surveyMechanisms []*model.SurveyMechanism
- var total int64
- // 开始查询
- query := model.DB.Model(&model.SurveyMechanism{})
- // 模糊搜索
- if key != "" {
- query = query.Where("name LIKE @key or description like @key", map[string]any{
- "key": "%" + key + "%",
- })
- }
- // 机构ID
- if mechanismId != "" {
- query = query.Where("mechanism_id = ?", mechanismId)
- }
- // 问卷ID
- if surveyId != "" {
- // 检查问卷是否真实存在
- surveyModel, findErr := survey.FindBySn(surveyId)
- if findErr != nil {
- return surveyMechanisms, total, findErr
- }
- query = query.Where("survey_id = ?", surveyModel.ID)
- }
- // 开始分页
- paginateErr := query.Count(&total).Error
- if paginateErr != nil {
- return nil, 0, &response.ErrCode{
- Code: response.ERROR,
- Msg: "获取授权列表失败",
- }
- }
- query.Scopes(model.Paginate(page, pageSize)).Order("id desc").Preload("Survey").Find(&surveyMechanisms)
- return surveyMechanisms, total, nil
- }
- // 修改授权状态
- func UpdateAuthorizeStatus(sn string, status int) *response.ErrCode {
- if status != model.SURVEY_MECHANISM_AUTHORIZE_STATUS_ENABLE && status != model.SURVEY_MECHANISM_AUTHORIZE_STATUS_DISABLE {
- return &response.ErrCode{
- Code: response.ERROR,
- Msg: "不支持的状态",
- }
- }
- existsSurveyMechanism, findErr := FindBySn(sn)
- if findErr != nil {
- return findErr
- }
- // 修改状态
- updateErr := model.DB.Model(model.SurveyMechanism{}).Where("id = ?", existsSurveyMechanism.ID).Update("authorize_status", status).Error
- if updateErr != nil {
- return &response.ErrCode{
- Code: response.ERROR,
- Msg: "修改授权状态失败",
- }
- }
- return nil
- }
- // 批量格式化
- func ListFormat(surveyMechanisms []*model.SurveyMechanism) []*validators.SurveyMechanism {
- formatedSurveyMechanisms := make([]*validators.SurveyMechanism, 0)
- var mechanismIds []string
- for _, surveyMechanism := range surveyMechanisms {
- if surveyMechanism != nil {
- mechanismIds = append(mechanismIds, surveyMechanism.MechanismId)
- }
- }
- mechanismIds = lo.Uniq(mechanismIds)
- // 获取机构列表
- mechanisms, _ := mechanism.ListMechanismByIds(mechanismIds)
- for _, surveyMechanism := range surveyMechanisms {
- var mechanism *sdk.Mechanism
- // 找到机构
- _, index, _ := lo.FindIndexOf(mechanisms, func(mechanismItem *sdk.Mechanism) bool {
- return mechanismItem.ID == surveyMechanism.MechanismId
- })
- if index >= 0 {
- mechanism = mechanisms[index]
- formatedSurveyMechanisms = append(formatedSurveyMechanisms, Format(surveyMechanism, mechanism, true))
- }
- }
- return formatedSurveyMechanisms
- }
- // 格式化
- func Format(surveyMechanism *model.SurveyMechanism, mechanism *sdk.Mechanism, visibleSurvey bool) *validators.SurveyMechanism {
- if surveyMechanism == nil {
- return nil
- }
- formatedSurvey := survey.Format(&surveyMechanism.Survey)
- surveyId := formatedSurvey.ID
- name := surveyMechanism.Name
- cover := surveyMechanism.Cover
- if name == "" {
- name = formatedSurvey.Name
- }
- if cover == "" {
- cover = formatedSurvey.Cover
- }
- if !visibleSurvey {
- formatedSurvey = nil
- surveyId = ""
- }
- return &validators.SurveyMechanism{
- ID: surveyMechanism.SN,
- AuthorizeStatus: surveyMechanism.AuthorizeStatus,
- Status: surveyMechanism.Status,
- MechanismId: surveyMechanism.MechanismId,
- Mechanism: mechanism,
- SurveyId: surveyId,
- Survey: formatedSurvey,
- CreatedAt: carbon.Time2Carbon(surveyMechanism.CreatedAt).Format("Y/m/d H:i:s"),
- UpdatedAt: carbon.Time2Carbon(surveyMechanism.UpdatedAt).Format("Y/m/d H:i:s"),
- Permissions: surveyMechanism.Permissions,
- Name: name,
- Cover: cover,
- Description: surveyMechanism.Description,
- }
- }
|