123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package survey_mechanism
- import (
- "surveyService/model"
- "surveyService/response"
- "surveyService/service/questionnaire_survey"
- "surveyService/validators"
- "gogs.uu.mdfitnesscao.com/cuiguohai/sdk"
- )
- type Mechanism struct {
- *sdk.AuthMechanism
- }
- func InitMechanism(authMechanism *sdk.AuthMechanism) *Mechanism {
- return &Mechanism{authMechanism}
- }
- // 获取问卷详情
- func (m *Mechanism) Detail(sn string) (*model.SurveyMechanism, *response.ErrCode) {
- var surveyMechanismModel model.SurveyMechanism
- findErr := model.DB.Where("sn = ?", sn).Preload("Survey").Where("mechanism_id = ?", m.Mechanism.ID).First(&surveyMechanismModel).Error
- if findErr != nil {
- return nil, &response.ErrCode{
- Code: response.ERROR,
- Msg: "没有找到有效的问卷信息",
- }
- }
- // 检查当前问卷是否已被授权
- if surveyMechanismModel.AuthorizeStatus != model.SURVEY_MECHANISM_AUTHORIZE_STATUS_ENABLE {
- return nil, &response.ErrCode{
- Code: response.ERROR,
- Msg: "当前问卷已被停止授权,无法操作该问卷",
- }
- }
- return &surveyMechanismModel, nil
- }
- // 设置问卷信息
- func (m *Mechanism) Update(surveyMechanism *validators.SurveyMechanism) *response.ErrCode {
- existsSurveyMechanism, findErr := m.Detail(surveyMechanism.ID)
- if findErr != nil {
- return findErr
- }
- // 开始修改
- updateErr := model.DB.Where("id = ?", existsSurveyMechanism.ID).Select("name", "cover", "description").Updates(&model.SurveyMechanism{
- Name: surveyMechanism.Name,
- Cover: surveyMechanism.Cover,
- Description: surveyMechanism.Description,
- }).Error
- if updateErr != nil {
- return &response.ErrCode{
- Code: response.ERROR,
- Msg: "修改问卷信息失败",
- }
- }
- return nil
- }
- // 修改问卷状态
- func (m *Mechanism) UpdateStatus(sn string, status int) *response.ErrCode {
- if status != model.SURVEY_MECHANISM_STATUS_ENABLE && status != model.SURVEY_MECHANISM_STATUS_DISABLE {
- return &response.ErrCode{
- Code: response.ERROR,
- Msg: "不支持的状态",
- }
- }
- existsSurveyMechanism, findErr := m.Detail(sn)
- if findErr != nil {
- return findErr
- }
- // 修改状态
- updateErr := model.DB.Model(model.SurveyMechanism{}).Where("id = ?", existsSurveyMechanism.ID).Update("status", status).Error
- if updateErr != nil {
- return &response.ErrCode{
- Code: response.ERROR,
- Msg: "修改状态失败",
- }
- }
- return nil
- }
- // 获取问卷列表
- func (m *Mechanism) Paginate(page, pageSize int, status int, key string) ([]*model.SurveyMechanism, int64, *response.ErrCode) {
- var surveyMechanismList []*model.SurveyMechanism
- var count int64
- // 开始查询
- query := model.DB.Model(model.SurveyMechanism{}).Where("mechanism_id = ?", m.Mechanism.ID)
- if status != 0 {
- query = query.Where("status = ?", status)
- }
- if key != "" {
- query = query.Where("name like @key or description like @key", map[string]any{
- "key": "%" + key + "%",
- })
- }
- query.Count(&count)
- // 开始分页
- query.Scopes(model.Paginate(page, pageSize)).Preload("Survey").Order("id desc").Find(&surveyMechanismList)
- return surveyMechanismList, count, nil
- }
- // 获取问卷列表
- func (m *Mechanism) List(status int, key string) []*model.SurveyMechanism {
- var surveyMechanismList []*model.SurveyMechanism
- // 开始查询
- query := model.DB.Model(model.SurveyMechanism{}).Where("mechanism_id = ?", m.Mechanism.ID)
- if status != 0 {
- query = query.Where("status = ?", status)
- }
- if key != "" {
- query = query.Where("name like @key or description like @key", map[string]any{
- "key": "%" + key + "%",
- })
- }
- // 开始分页
- query.Preload("Survey").Order("id desc").Find(&surveyMechanismList)
- return surveyMechanismList
- }
- // 获取问卷详情
- func (m *Mechanism) DetailSurveyConfig(sn string) (*validators.QuestionnaireSurvey, *response.ErrCode) {
- surveyMechanism, findErr := m.Detail(sn)
- if findErr != nil {
- return nil, findErr
- }
- // if surveyMechanism.Survey.SurveyCode == "" {
- // return nil, &response.ErrCode{
- // Code: response.ERROR,
- // Msg: "当前问卷未配置关联模型",
- // }
- // }
- surveyCode := "DZTJV1"
- if surveyMechanism.Survey.SurveyCode != "" {
- surveyCode = surveyMechanism.Survey.SurveyCode
- }
- // 获取问卷配置信息
- surveyConfig, detailErr := questionnaire_survey.FindBySN(surveyCode)
- if detailErr != nil {
- return nil, &response.ErrCode{
- Code: response.ERROR,
- Msg: "获取问卷配置失败",
- }
- }
- return questionnaire_survey.Format(surveyConfig), nil
- }
|