package learning import ( "context" "database/sql" "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 LearningTestpaperService struct { Dao *dao.LearningTestpaperDao QuestionDao *dao.LearningQuestionDao QuestionTestpaperDao *dao.LearningQuestionTestpaperDao SkillDao *dao.LearningSkillDao LearningMaterialSrv *LearningMaterialService LearningRecordSrv *LearningLearningRecordService ExamRecordService *LearningExamRecordService Tenant string userInfo request.UserInfo } func NewLearningTestpaperService(ctx context.Context) (*LearningTestpaperService, 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()) } examRecordService, err := NewLearningExamRecordService(ctx) if err != nil { return nil, err } lrSrv, err := NewLearningLearningRecordService(ctx) if err != nil { return nil, err } lmSrv, err := NewLearningMaterialService(ctx) if err != nil { return nil, err } return &LearningTestpaperService{ Dao: dao.NewLearningTestpaperDao(tenant), QuestionDao: dao.NewLearningQuestionDao(tenant), QuestionTestpaperDao: dao.NewLearningQuestionTestpaperDao(tenant), SkillDao: dao.NewLearningSkillDao(tenant), LearningMaterialSrv: lmSrv, LearningRecordSrv: lrSrv, ExamRecordService: examRecordService, Tenant: tenant, userInfo: userInfo, }, nil } func (s LearningTestpaperService) Get(ctx context.Context, req *learning.LearningTestpaperGetReq) (*learning.LearningTestpaperGetRsp, error) { validErr := gvalid.CheckStruct(ctx, req, nil) if validErr != nil { return nil, myerrors.NewMsgError(nil, validErr.Current().Error()) } tp, err := s.Dao.Where("Id = ?", req.Id).One() if err != nil { return nil, err } if tp == nil { return nil, myerrors.NewMsgError(nil, "试卷不存在") } _, record, err := s.ExamRecordService.List(ctx, &learning.LearningExamRecordListReq{ TestpaperId: tp.Id, }) if err != nil { return nil, err } questionEnt := []*learning.LearningQuestion{} err = s.QuestionTestpaperDao. LeftJoin("learning_question", "learning_question_testpaper.QuestionId=learning_question.Id"). Where("learning_question_testpaper.TestpaperId", req.Id). Fields("learning_question.*").Structs(&questionEnt) if err != nil && err != sql.ErrNoRows { return nil, err } quesitons, err := ConvLearningQuestionGetRsp(questionEnt) if err != nil { return nil, err } return &learning.LearningTestpaperGetRsp{ LearningTestpaper: *tp, ExamRecord: record, Question: quesitons, }, nil } func (s LearningTestpaperService) List(ctx context.Context, req *learning.LearningTestpaperListReq) (int, []*learning.LearningTestpaper, error) { dao := &s.Dao.LearningTestpaperDao if req.SkillId != 0 { dao = dao.Where("SkillId = ?", req.SkillId) } if req.Enable != nil { dao = dao.Where("Enable = ?", req.Enable) } 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() return total, ent, err } func (s LearningTestpaperService) ListMy(ctx context.Context) ([]*learning.LearningTestpaperMy, error) { enable := 1 _, testpaper, err := s.List(ctx, &learning.LearningTestpaperListReq{Enable: &enable}) if err != nil { return nil, err } list := []*learning.LearningTestpaperMy{} for _, tp := range testpaper { skill, err := s.SkillDao.Where("Id = ?", tp.SkillId).One() if err != nil { return nil, err } if skill == nil { continue } materialIds, err := s.LearningMaterialSrv.MaterialIds(ctx, skill.Id) if err != nil { return nil, err } recordIds, err := s.LearningRecordSrv.LearntMaterialIds(ctx, int(s.userInfo.Id)) if err != nil { return nil, err } // 如果没有关联的资料,直接算学完了 learntAll := true for _, mid := range materialIds { found := false for _, rid := range recordIds { if mid == rid { found = true } } if !found { learntAll = false break } } list = append(list, &learning.LearningTestpaperMy{ LearningTestpaper: *tp, SkillName: skill.Name, LearntAll: learntAll, }) } return list, nil } func (s LearningTestpaperService) Add(ctx context.Context, req *learning.LearningTestpaperAddReq) (int, error) { validErr := gvalid.CheckStruct(ctx, req, nil) if validErr != nil { return 0, myerrors.NewMsgError(nil, validErr.Current().Error()) } if req.PassLimit > len(req.Question) { return 0, myerrors.NewMsgError(nil, "合格标准大于题目总数量") } r, err := s.Dao.DB.Table("learning_skill").Where("Id = ?", req.SkillId).One() if err != nil { return 0, err } if r.IsEmpty() { return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId)) } for _, qid := range req.Question { q, err := s.QuestionDao.Where("Id = ?", qid).One() if err != nil { return 0, err } if q == nil { return 0, myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在 %d", qid)) } } id, err := s.Dao.InsertAndGetId(learning.LearningTestpaper{ SkillId: req.SkillId, Name: req.Name, Enable: 0, TimeLimit: req.TimeLimit, PassLimit: req.PassLimit, OperateBy: s.userInfo.RealName, CreatedAt: gtime.Now(), UpdatedAt: gtime.Now(), }) if err != nil { return 0, err } if len(req.Question) == 0 { return int(id), err } bind := []learning.LearningQuestionTestpaper{} for _, qid := range req.Question { bind = append(bind, learning.LearningQuestionTestpaper{ QuestionId: qid, TestpaperId: int(id), }) } _, err = s.QuestionTestpaperDao.Insert(bind) return int(id), err } func (s LearningTestpaperService) Update(ctx context.Context, req *learning.LearningTestpaperUpdateReq) error { validErr := gvalid.CheckStruct(ctx, req, nil) if validErr != nil { return myerrors.NewMsgError(nil, validErr.Current().Error()) } if req.PassLimit > len(req.Question) { return myerrors.NewMsgError(nil, "合格标准大于题目总数量") } tp, err := s.Dao.Where("Id = ?", req.Id).One() if err != nil { return err } if tp == 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.LearningTestpaperDao toupdate := map[string]interface{}{} if req.SkillId != 0 { toupdate["SkillId"] = req.SkillId } if req.Name != "" { toupdate["Name"] = req.Name } if req.Enable != nil { toupdate["Enable"] = req.Enable // 禁用已启用的试卷 if *req.Enable == 1 { _, err = dao.Where("SkillId", tp.SkillId).Data("Enable", 0).Update() if err != nil { return err } } } if req.TimeLimit != 0 { toupdate["TimeLimit"] = req.TimeLimit } if req.PassLimit != 0 { toupdate["PassLimit"] = req.PassLimit } if len(toupdate) != 0 { toupdate["OperateBy"] = s.userInfo.RealName _, err = dao.Where("Id", req.Id).Data(toupdate).Update() if err != nil { return err } } if req.Question == nil { return nil } _, err = s.QuestionTestpaperDao.Where("TestpaperId = ?", req.Id).Delete() if err != nil { return err } if len(req.Question) == 0 { return nil } bind := []learning.LearningQuestionTestpaper{} for _, qid := range req.Question { q, err := s.QuestionDao.Where("Id = ?", qid).One() if err != nil { return err } if q == nil { return myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在 %d", qid)) } bind = append(bind, learning.LearningQuestionTestpaper{ QuestionId: qid, TestpaperId: tp.Id, }) } _, err = s.QuestionTestpaperDao.Insert(bind) return err } func (s LearningTestpaperService) Delete(ctx context.Context, id []int) error { _, err := s.Dao.Where("Id IN (?)", id).Delete() if err != nil { return err } _, err = s.QuestionTestpaperDao.Where("TestpaperId IN (?)", id).Delete() return err }