| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- 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
- }
|