| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- package learning
- import (
- "context"
- "database/sql"
- "fmt"
- "lims_adapter/dao/learning"
- "lims_adapter/model"
- "lims_adapter/model/learning"
- "reflect"
- "sort"
- "strings"
- "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 LearningExamRecordService struct {
- Dao *dao.LearningExamRecordDao
- TestpaperDao *dao.LearningTestpaperDao
- QuestionSrv *LearningQuestionService
- Tenant string
- userInfo request.UserInfo
- }
- func NewLearningExamRecordService(ctx context.Context) (*LearningExamRecordService, 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())
- }
- tps, err := NewLearningQuestionService(ctx)
- if err != nil {
- return nil, err
- }
- return &LearningExamRecordService{
- Dao: dao.NewLearningExamRecordDao(tenant),
- TestpaperDao: dao.NewLearningTestpaperDao(tenant),
- QuestionSrv: tps,
- Tenant: tenant,
- userInfo: userInfo,
- }, nil
- }
- func (s LearningExamRecordService) List(ctx context.Context, req *learning.LearningExamRecordListReq) (int, []*learning.LearningExamRecordGetRsp, error) {
- m := s.Dao.DB.
- Table("learning_exam_record a").
- LeftJoin("learning_skill b", "a.SkillId=b.Id").
- LeftJoin("base_user c", "a.UserId=c.Id").
- LeftJoin("learning_testpaper d", "a.TestpaperId=d.Id")
- if req.UserId != 0 {
- m = m.Where("a.UserId = ?", req.UserId)
- }
- if req.SkillId != 0 {
- m = m.Where("a.SkillId = ?", req.SkillId)
- }
- if req.TestpaperId != 0 {
- m = m.Where("a.TestpaperId = ?", req.TestpaperId)
- }
- if req.Status != 0 {
- m = m.Where("a.Status = ?", req.Status)
- }
- total, err := m.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
- }
- m = m.Page(req.Page.Current, req.Page.Size)
- }
- if req.OrderBy == nil {
- req.OrderBy = &model.OrderBy{}
- }
- if req.OrderBy.Value == "" {
- req.OrderBy.Value = "a.CreatedAt"
- req.OrderBy.Type = "desc"
- }
- if req.OrderBy != nil && req.OrderBy.Value != "" {
- order := "asc"
- if req.OrderBy.Type == "desc" {
- order = "desc"
- }
- m = m.Order(req.OrderBy.Value, order)
- }
- records := []*learning.LearningExamRecordGetRsp{}
- err = m.Fields(
- "a.*",
- "b.Name as SkillName",
- "c.Realname as UserName",
- "d.Name as TestpaperName").
- Structs(&records)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- for i := range records {
- if records[i].QuestionCount == 0 || records[i].CorrectCount == 0 {
- records[i].CorrectRate = "0%"
- }
- records[i].CorrectRate = fmt.Sprintf("%.f%%", float64(records[i].CorrectCount)/float64(records[i].QuestionCount)*100)
- }
- return total, records, err
- }
- func (s LearningExamRecordService) Add(ctx context.Context, req *learning.LearningExamRecordAddReq) (int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
- }
- r, err := s.Dao.DB.Table("base_user").Where("Id = ?", req.UserId).One()
- if err != nil {
- return 0, err
- }
- if r.IsEmpty() {
- return 0, myerrors.NewMsgError(nil, fmt.Sprintf("用户不存在: %d", req.UserId))
- }
- 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))
- }
- tp, err := s.TestpaperDao.Where("Id = ?", req.TestpaperId).One()
- if err != nil {
- return 0, err
- }
- if tp == nil {
- return 0, myerrors.NewMsgError(nil, fmt.Sprintf("试卷不存在: %d", req.TestpaperId))
- }
- qustions, err := s.QuestionSrv.ListByTestpaperId(ctx, req.TestpaperId)
- if err != nil {
- return 0, err
- }
- questionCount := len(qustions)
- correctCount := 0
- userAnswerMap := map[int][]string{}
- for _, a := range req.Detail {
- userAnswerMap[a.QuestionId] = a.Answer
- }
- for _, q := range qustions {
- answer := strings.Split(strings.ToUpper(q.Answer), " ")
- userAnswer := userAnswerMap[q.Id]
- if len(answer) != len(userAnswer) {
- continue
- }
- sort.Strings(answer)
- sort.Strings(userAnswer)
- // fmt.Println(q, answer, userAnswer, reflect.DeepEqual(answer, userAnswer))
- if reflect.DeepEqual(answer, userAnswer) {
- correctCount++
- }
- }
- id, err := s.Dao.InsertAndGetId(learning.LearningExamRecord{
- UserId: req.UserId,
- SkillId: req.SkillId,
- TestpaperId: req.TestpaperId,
- QuestionCount: questionCount,
- CorrectCount: correctCount,
- Status: req.Status,
- CreatedAt: gtime.Now(),
- UpdatedAt: gtime.Now(),
- })
- return int(id), err
- }
|