|
|
@@ -0,0 +1,130 @@
|
|
|
+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 LearningExamRecordService struct {
|
|
|
+ Dao *dao.LearningExamRecordDao
|
|
|
+ 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())
|
|
|
+ }
|
|
|
+ return &LearningExamRecordService{
|
|
|
+ Dao: dao.NewLearningExamRecordDao(tenant),
|
|
|
+ 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.SkillId=b.Id").
|
|
|
+ Fields("a.*", "b.Name as SkillName", "c.Realname as UserName")
|
|
|
+ if req.UserId != 0 {
|
|
|
+ m = m.Where("UserId = ?", req.UserId)
|
|
|
+ }
|
|
|
+ if req.SkillId != 0 {
|
|
|
+ m = m.Where("SkillId = ?", req.SkillId)
|
|
|
+ }
|
|
|
+ if req.TestpaperId != 0 {
|
|
|
+ m = m.Where("TestpaperId = ?", req.TestpaperId)
|
|
|
+ }
|
|
|
+ if req.Status != 0 {
|
|
|
+ m = m.Where("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.Value != "" {
|
|
|
+ order := "asc"
|
|
|
+ if req.OrderBy.Type == "desc" {
|
|
|
+ order = "desc"
|
|
|
+ }
|
|
|
+ m = m.Order(req.OrderBy.Value, order)
|
|
|
+ }
|
|
|
+
|
|
|
+ records := []*learning.LearningExamRecordGetRsp{}
|
|
|
+ err = m.Structs(&records)
|
|
|
+ if err != nil && err != sql.ErrNoRows {
|
|
|
+ return 0, nil, err
|
|
|
+ }
|
|
|
+ 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, validErr.Current()
|
|
|
+ }
|
|
|
+ if req.UserId != 0 {
|
|
|
+ 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))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if req.SkillId != 0 {
|
|
|
+ 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))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if req.TestpaperId != 0 {
|
|
|
+ r, err := s.Dao.DB.Table("learning_testpaper").Where("Id = ?", req.TestpaperId).One()
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ if r.IsEmpty() {
|
|
|
+ return 0, myerrors.NewMsgError(nil, fmt.Sprintf("试卷不存在: %d", req.TestpaperId))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ id, err := s.Dao.InsertAndGetId(learning.LearningExamRecord{
|
|
|
+ UserId: req.UserId,
|
|
|
+ SkillId: req.SkillId,
|
|
|
+ TestpaperId: req.TestpaperId,
|
|
|
+ Status: req.Status,
|
|
|
+ CreatedAt: gtime.Now(),
|
|
|
+ UpdatedAt: gtime.Now(),
|
|
|
+ })
|
|
|
+ return int(id), err
|
|
|
+}
|