package learning import ( "context" "encoding/json" "fmt" "lims_adapter/dao/learning" "lims_adapter/model/learning" "dashoo.cn/micro_libary/micro_srv" "dashoo.cn/micro_libary/myerrors" "dashoo.cn/micro_libary/request" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/util/gvalid" ) type LearningQuestionService struct { Dao *dao.LearningQuestionDao Tenant string userInfo request.UserInfo } func NewLearningQuestionService(ctx context.Context) (*LearningQuestionService, error) { tenant, err := micro_srv.GetTenant(ctx) if err != nil { return nil, fmt.Errorf("获取组合码异常:%s", err.Error()) } // 获取用户信息 userInfo, err := micro_srv.GetUserInfo(ctx) if err != nil { return nil, fmt.Errorf("获取用户信息异常:%s", err.Error()) } return &LearningQuestionService{ Dao: dao.NewLearningQuestionDao(tenant), Tenant: tenant, userInfo: userInfo, }, nil } func (s LearningQuestionService) Get(ctx context.Context, req *learning.LearningQuestionGetReq) (ent *learning.LearningQuestionGetRsp, err error) { validErr := gvalid.CheckStruct(ctx, req, nil) if validErr != nil { return nil, validErr.Current() } q, err := s.Dao.Where("Id = ?", req.Id).One() if err != nil { return nil, err } if q == nil { return nil, myerrors.NewMsgError(nil, "题目不存在") } content := []learning.LearningQuestionOption{} err = json.Unmarshal([]byte(q.Content), &content) return &learning.LearningQuestionGetRsp{ LearningQuestion: *q, Content: content, }, err } func (s LearningQuestionService) List(ctx context.Context, req *learning.LearningQuestionListReq) (int, []*learning.LearningQuestionGetRsp, error) { dao := &s.Dao.LearningQuestionDao if req.Name != "" { dao = dao.Where("Name LIKE ?", fmt.Sprintf("%%%s%%", req.Name)) } if req.SkillId != 0 { dao = dao.Where("SkillId = ?", req.SkillId) } total, err := dao.Count() if err != nil { return 0, nil, err } if req.Page != nil { if req.Page.Current == 0 { req.Page.Current = 1 } if req.Page.Size == 0 { req.Page.Size = 10 } dao = dao.Page(req.Page.Current, req.Page.Size) } if req.OrderBy != nil && req.OrderBy.Value != "" { order := "asc" if req.OrderBy.Type == "desc" { order = "desc" } dao = dao.Order(req.OrderBy.Value, order) } ent, err := dao.All() if err != nil { return 0, nil, err } var questions []*learning.LearningQuestionGetRsp for _, q := range ent { content := []learning.LearningQuestionOption{} err = json.Unmarshal([]byte(q.Content), &content) if err != nil { return 0, nil, err } questions = append(questions, &learning.LearningQuestionGetRsp{ LearningQuestion: *q, Content: content, }) } return total, questions, err } func (s LearningQuestionService) Add(ctx context.Context, req *learning.LearningQuestionAddReq) (int, error) { validErr := gvalid.CheckStruct(ctx, req, nil) if validErr != nil { return 0, validErr.Current() } if len(req.Content) < 2 { return 0, myerrors.NewMsgError(nil, "至少需要两个选项") } correctCount := 0 for _, o := range req.Content { if o.IsCorrect { correctCount += 1 } } if correctCount < 1 { return 0, myerrors.NewMsgError(nil, "正确答案未设置") } if (req.Type == 1 || req.Type == 3) && correctCount != 1 { return 0, myerrors.NewMsgError(nil, "正确答案只能设置一个") } content, err := json.Marshal(req.Content) if err != nil { return 0, err } id, err := s.Dao.InsertAndGetId(learning.LearningQuestion{ SkillId: req.SkillId, Name: req.Name, Type: req.Type, Enable: req.Enable, Content: string(content), Explanation: req.Explanation, OperateBy: s.userInfo.RealName, CreatedAt: gtime.Now(), UpdatedAt: gtime.Now(), }) if err != nil { return 0, err } return int(id), err } func (s LearningQuestionService) Update(ctx context.Context, req *learning.LearningQuestionUpdateReq) error { validErr := gvalid.CheckStruct(ctx, req, nil) if validErr != nil { return validErr.Current() } if len(req.Content) != 0 { if len(req.Content) < 2 { return myerrors.NewMsgError(nil, "至少需要两个选项") } correctCount := 0 for _, o := range req.Content { if o.IsCorrect { correctCount += 1 } } if correctCount < 1 { return myerrors.NewMsgError(nil, "正确答案未设置") } if (req.Type == 1 || req.Type == 3) && correctCount != 1 { return myerrors.NewMsgError(nil, "正确答案只能设置一个") } } q, err := s.Dao.Where("Id = ?", req.Id).One() if err != nil { return err } if q == nil { return myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在: %d", req.Id)) } if req.SkillId != 0 { r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One() if err != nil { return err } if r.IsEmpty() { return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId)) } } dao := &s.Dao.LearningQuestionDao toupdate := map[string]interface{}{} if req.SkillId != 0 { toupdate["SkillId"] = req.SkillId } if req.Name != "" { toupdate["Name"] = req.Name } if req.Type != 0 { toupdate["Type"] = req.Type } if req.Enable != nil { toupdate["Enable"] = req.Enable } if req.Content != nil { content, err := json.Marshal(req.Content) if err != nil { return err } toupdate["Content"] = content } if req.Explanation != "" { toupdate["Explanation"] = req.Explanation } if len(toupdate) == 0 { return nil } toupdate["OperateBy"] = s.userInfo.RealName _, err = dao.Where("Id", req.Id).Data(toupdate).Update() return err } func (s LearningQuestionService) Delete(ctx context.Context, id int) error { _, err := s.Dao.Where("Id = ?", id).Delete() return err }