|
@@ -0,0 +1,372 @@
|
|
|
+package survey_result
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "os"
|
|
|
+ "strings"
|
|
|
+ "surveyService/model"
|
|
|
+ "surveyService/response"
|
|
|
+ "surveyService/sdk/survey_disease"
|
|
|
+ "surveyService/service/survey"
|
|
|
+ "surveyService/service/survey_mechanism"
|
|
|
+ "surveyService/service/survey_token"
|
|
|
+ "surveyService/util"
|
|
|
+ "surveyService/util/rabbitmq"
|
|
|
+ "surveyService/validators"
|
|
|
+
|
|
|
+ "github.com/golang-module/carbon"
|
|
|
+ jsoniter "github.com/json-iterator/go"
|
|
|
+ "gogs.uu.mdfitnesscao.com/hys/sdk"
|
|
|
+ "gogs.uu.mdfitnesscao.com/hys/sdk/mechanism"
|
|
|
+)
|
|
|
+
|
|
|
+var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
+
|
|
|
+type Mechanism struct {
|
|
|
+ *sdk.AuthMechanism
|
|
|
+}
|
|
|
+
|
|
|
+func InitMechanism(authMechanism *sdk.AuthMechanism) *Mechanism {
|
|
|
+ return &Mechanism{authMechanism}
|
|
|
+}
|
|
|
+
|
|
|
+// 获取问卷结果
|
|
|
+func (m *Mechanism) Detail(sn string) (*model.SurveyResult, *response.ErrCode) {
|
|
|
+ var surveyResult *model.SurveyResult
|
|
|
+ err := model.DB.Where("sn = ?", sn).Preload("SurveyMechanism").First(&surveyResult).Error
|
|
|
+ if err != nil {
|
|
|
+ return nil, &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "获取问卷结果失败",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if m.AuthMechanism != nil && m.Mechanism.ID != surveyResult.SurveyMechanism.MechanismId {
|
|
|
+ return nil, &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "没有找到有效的问卷结果数据",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return surveyResult, nil
|
|
|
+}
|
|
|
+
|
|
|
+// // 获取某个问卷最后一份的结果
|
|
|
+// func (m *Mechanism) LastResult(surveyMechanismId string) (*model.SurveyResult, *response.ErrCode) {
|
|
|
+
|
|
|
+// }
|
|
|
+
|
|
|
+// 获取问卷结果
|
|
|
+func (m *Mechanism) DetailByExtra(extra string) (*model.SurveyResult, *response.ErrCode) {
|
|
|
+ tx := model.DB.Model(&model.SurveyResult{})
|
|
|
+ var surveyResult *model.SurveyResult
|
|
|
+ err := tx.Where("extra = ?", extra).Preload("SurveyMechanism").First(&surveyResult).Error
|
|
|
+ if err != nil {
|
|
|
+ return nil, &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "获取问卷结果失败",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if m.AuthMechanism != nil && m.Mechanism.ID != surveyResult.SurveyMechanism.MechanismId {
|
|
|
+ return nil, &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "没有找到有效的问卷结果数据",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return surveyResult, nil
|
|
|
+}
|
|
|
+
|
|
|
+// 获取问卷结果列表
|
|
|
+func (m *Mechanism) Paginate(queryData validators.SurveyResultPaginate) ([]*model.SurveyResult, int64, *response.ErrCode) {
|
|
|
+ var surveyResults []*model.SurveyResult = make([]*model.SurveyResult, 0)
|
|
|
+ var total int64
|
|
|
+
|
|
|
+ query := model.DB.Model(model.SurveyResult{})
|
|
|
+ if queryData.Method != 0 {
|
|
|
+ query = query.Where("method = ?", queryData.Method)
|
|
|
+ }
|
|
|
+ if queryData.SurveyMechanismId != "" {
|
|
|
+ surveyMechanismRawId := (&model.SurveyMechanism{}).GetRawId(queryData.SurveyMechanismId)
|
|
|
+ if surveyMechanismRawId == 0 {
|
|
|
+ return surveyResults, total, &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "无效的问卷编号",
|
|
|
+ }
|
|
|
+ }
|
|
|
+ query = query.Where("survey_mechanism_id = ?", surveyMechanismRawId)
|
|
|
+ }
|
|
|
+ if queryData.ArchivesId != "" {
|
|
|
+ query = query.Where("archives_id = ?", queryData.ArchivesId)
|
|
|
+ }
|
|
|
+ if m.AuthMechanism == nil {
|
|
|
+ if queryData.SurveyId != "" {
|
|
|
+ surveyRawId := (&model.Survey{}).GetRawId(queryData.SurveyId)
|
|
|
+ if surveyRawId == 0 {
|
|
|
+ return surveyResults, total, &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "无效的问卷编号",
|
|
|
+ }
|
|
|
+ }
|
|
|
+ query = query.Where("survey_mechanism_id in ?", model.DB.Where("survey_id = ?", surveyRawId).Select("id").Model(&model.SurveyMechanism{}))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ mechanismId := ""
|
|
|
+ if m.AuthMechanism != nil {
|
|
|
+ mechanismId = m.Mechanism.ID
|
|
|
+ }
|
|
|
+ query.Scopes(model.MechanismQuery(mechanismId)).Count(&total)
|
|
|
+ if total == 0 {
|
|
|
+ return surveyResults, total, nil
|
|
|
+ }
|
|
|
+ query.Scopes(model.Paginate(queryData.Page, queryData.PageSize)).Scopes(model.MechanismQuery(mechanismId)).Preload("SurveyMechanism").Order("start_time desc").Find(&surveyResults)
|
|
|
+
|
|
|
+ return surveyResults, total, nil
|
|
|
+}
|
|
|
+
|
|
|
+// 修改答题人
|
|
|
+func (m *Mechanism) UpdateArchivesId(sn string, archivesId string) *response.ErrCode {
|
|
|
+ // 获取问卷结果数据
|
|
|
+ surveyResult, findErr := m.Detail(sn)
|
|
|
+ if findErr != nil {
|
|
|
+ return findErr
|
|
|
+ }
|
|
|
+
|
|
|
+ // Todo 检查档案是否有效
|
|
|
+
|
|
|
+ // 只有后台新增的才可以修改
|
|
|
+ if surveyResult.Method == model.SurveyResultMethodArchives {
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "当前问卷不允许修改答题人",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存问卷结果
|
|
|
+ updateErr := model.DB.Model(model.SurveyResult{}).Where("id = ?", surveyResult.ID).Update("archives_id", archivesId).Error
|
|
|
+ if updateErr != nil {
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "保存问卷结果失败",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// 重新提交到转译系统
|
|
|
+func (m *Mechanism) Run(sn string) *response.ErrCode {
|
|
|
+ // 获取问卷结果
|
|
|
+ surveyResult, findErr := m.Detail(sn)
|
|
|
+ if findErr != nil {
|
|
|
+ return findErr
|
|
|
+ }
|
|
|
+
|
|
|
+ var answers map[string]any
|
|
|
+ fmt.Println(surveyResult.AnswerRaw)
|
|
|
+ unmarshalErr := json.UnmarshalFromString(surveyResult.AnswerRaw, &answers)
|
|
|
+ if unmarshalErr != nil {
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "无效的答案",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ _, findErr = survey.Find(surveyResult.SurveyMechanism.SurveyId)
|
|
|
+ if findErr != nil {
|
|
|
+ return findErr
|
|
|
+ }
|
|
|
+
|
|
|
+ var formatedAnswers map[string]*sdk.SurveyAnswer
|
|
|
+ jsoniter.UnmarshalFromString(util.JsonEncode(answers), &formatedAnswers)
|
|
|
+ surveyDiseaseCalcResult := survey_disease.DiseaseScreeningCal(
|
|
|
+ formatedAnswers,
|
|
|
+ )
|
|
|
+ ProcessResult(sn, surveyDiseaseCalcResult)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// 格式化答题结果
|
|
|
+func Format(rawSurveyResult *model.SurveyResult, needResult bool) *validators.SurveyResult {
|
|
|
+ var answerResult map[string]any
|
|
|
+ var resultRaw string
|
|
|
+ if needResult {
|
|
|
+ if rawSurveyResult.AnswerRaw != "" {
|
|
|
+ json.UnmarshalFromString(rawSurveyResult.AnswerRaw, &answerResult)
|
|
|
+ }
|
|
|
+ resultRaw = rawSurveyResult.ResultRaw
|
|
|
+ }
|
|
|
+ return &validators.SurveyResult{
|
|
|
+ ID: rawSurveyResult.SN,
|
|
|
+ Method: rawSurveyResult.Method,
|
|
|
+ StartTime: carbon.Time2Carbon(rawSurveyResult.StartTime).Format("Y/m/d H:i:s"),
|
|
|
+ EndTime: carbon.Time2Carbon(rawSurveyResult.EndTime).Format("Y/m/d H:i:s"),
|
|
|
+ Status: rawSurveyResult.Status,
|
|
|
+ ArchivesId: rawSurveyResult.ArchivesId,
|
|
|
+ AnswerResult: answerResult,
|
|
|
+ ResultRaw: resultRaw,
|
|
|
+ CanAnalyze: rawSurveyResult.AnswerRaw != "",
|
|
|
+ SurveyMechanismId: (&model.SurveyMechanism{}).GetHashId(rawSurveyResult.SurveyMechanismId),
|
|
|
+ SurveyMechanism: survey_mechanism.ListFormat([]*model.SurveyMechanism{rawSurveyResult.SurveyMechanism})[0],
|
|
|
+ CreatedAt: carbon.Time2Carbon(rawSurveyResult.CreatedAt).Format("Y/m/d H:i:s"),
|
|
|
+ UpdatedAt: carbon.Time2Carbon(rawSurveyResult.UpdatedAt).Format("Y/m/d H:i:s"),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 接收问卷结果回调
|
|
|
+func ReceiveResult(modelSn, extra string, modelType int, data map[string]any) *response.ErrCode {
|
|
|
+ if modelType != 1 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ // 获取问卷结果数据
|
|
|
+ surveyResult, findErr := InitMechanism(nil).Detail(extra)
|
|
|
+ if findErr != nil {
|
|
|
+ return findErr
|
|
|
+ }
|
|
|
+
|
|
|
+ // 只有在填写中的问卷才能保存
|
|
|
+ if surveyResult.Status != model.SurveyResultStatusFulled {
|
|
|
+ fmt.Println("当前问卷状态不支持保存结果")
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "当前问卷状态不支持保存结果",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存问卷结果
|
|
|
+ updateErr := model.DB.Model(model.SurveyResult{}).Where("id = ?", surveyResult.ID).Select([]string{"ResultRaw", "Status"}).Updates(&model.SurveyResult{
|
|
|
+ ResultRaw: util.JsonEncode(data),
|
|
|
+ Status: model.SurveyResultStatusDone,
|
|
|
+ }).Error
|
|
|
+ if updateErr != nil {
|
|
|
+ fmt.Println("保存问卷结果失败")
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "保存问卷结果失败",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 开始回调机构配置的回调地址
|
|
|
+ go CallbackSurveyResultToMechanism(surveyResult.SN)
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// 接收问卷结果回调
|
|
|
+func ProcessResult(sn string, data *survey_disease.SurveyResult) *response.ErrCode {
|
|
|
+ // 获取问卷结果数据
|
|
|
+ surveyResult, findErr := InitMechanism(nil).Detail(sn)
|
|
|
+ if findErr != nil {
|
|
|
+ return findErr
|
|
|
+ }
|
|
|
+
|
|
|
+ // 只有在填写中的问卷才能保存
|
|
|
+ if surveyResult.Status != model.SurveyResultStatusFulled {
|
|
|
+ fmt.Println("当前问卷状态不支持保存结果")
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "当前问卷状态不支持保存结果",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存问卷结果
|
|
|
+ updateErr := model.DB.Model(model.SurveyResult{}).Where("id = ?", surveyResult.ID).Select([]string{"ResultRaw", "Status"}).Updates(&model.SurveyResult{
|
|
|
+ ResultRaw: util.JsonEncode(map[string]any{
|
|
|
+ "result": data,
|
|
|
+ }),
|
|
|
+ Status: model.SurveyResultStatusDone,
|
|
|
+ }).Error
|
|
|
+ if updateErr != nil {
|
|
|
+ fmt.Println("保存问卷结果失败")
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "保存问卷结果失败",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 开始回调机构配置的回调地址
|
|
|
+ go CallbackSurveyResultToMechanism(surveyResult.SN)
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// 将PDF报告推送至机构端
|
|
|
+func CallbackSurveyResultToMechanism(surveyResultId string) *response.ErrCode {
|
|
|
+ surveyResult, findSurveyResultErr := InitMechanism(nil).Detail(surveyResultId)
|
|
|
+ if findSurveyResultErr != nil {
|
|
|
+ return findSurveyResultErr
|
|
|
+ }
|
|
|
+ if surveyResult.Extra == "" {
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "无自定义参数,不回调",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var callbackData map[string]any = map[string]any{
|
|
|
+ "dataType": "Survey",
|
|
|
+ "errMsg": "",
|
|
|
+ "extra": surveyResult.Extra,
|
|
|
+ "surveyId": surveyResult.SurveyMechanism.SN,
|
|
|
+ "status": surveyResult.Status,
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查机构是否配置了回调地址
|
|
|
+ mechanismListResponse, findErr := mechanism.ListMechanism([]string{surveyResult.MechanismId})
|
|
|
+ if findErr != nil {
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "获取机构信息失败: " + findErr.Msg,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(mechanismListResponse.Data.List) == 0 {
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "没有找到机构信息",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 开始推送
|
|
|
+ var mechanismInfo = mechanismListResponse.Data.List[0]
|
|
|
+ if mechanismInfo.CallbackUrlList == "" {
|
|
|
+ return &response.ErrCode{
|
|
|
+ Code: response.ERROR,
|
|
|
+ Msg: "机构没有配置回调地址",
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 开始推送
|
|
|
+ var callbackUrlList []string = strings.Split(mechanismInfo.CallbackUrlList, "\n")
|
|
|
+ for _, callbackUrl := range callbackUrlList {
|
|
|
+ if callbackUrl == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ var webhookMessage rabbitmq.WebhookMessage
|
|
|
+ webhookMessage.Body = callbackData
|
|
|
+ webhookMessage.Config.Url = callbackUrl
|
|
|
+ rabbitmq.Webhook(webhookMessage)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// 创建一个可以临时访问结果的链接
|
|
|
+func (m *Mechanism) GetSurveyResultVisitLink(extra string) (string, int64, *model.SurveyResult, *response.ErrCode) {
|
|
|
+ var timeout int64 = 7200
|
|
|
+ // 检查问卷是否已被完成
|
|
|
+ surveyResult, findErr := m.DetailByExtra(extra)
|
|
|
+ if findErr != nil {
|
|
|
+ return "", timeout, nil, findErr
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查问卷结果是否存在
|
|
|
+ token := survey_token.InitMechanism(m.AuthMechanism).GenerateSurveyToken(surveyResult.SurveyMechanism.SN, surveyResult.SN, extra, timeout)
|
|
|
+ if token == "" {
|
|
|
+ return "", timeout, surveyResult, &response.ErrCode{
|
|
|
+ Msg: "生成临时访问链接失败",
|
|
|
+ Code: response.ERROR,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return fmt.Sprintf("%s/#/%s/survey/result?id=%s&surveyMechanismId=%s&token=%s", os.Getenv("H5_DOMAIN"), m.Mechanism.ID, surveyResult.SN, surveyResult.SurveyMechanism.SN, token), timeout, surveyResult, nil
|
|
|
+}
|