瀏覽代碼

feature:培训考试学习记录管理

liuyaqi 3 年之前
父節點
當前提交
a8d7db2997

+ 90 - 0
handler/learning/learning_record.go

@@ -0,0 +1,90 @@
+package learning
+
+import (
+	"context"
+	"fmt"
+	"lims_adapter/model/learning"
+	learningSrv "lims_adapter/service/learning"
+
+	"dashoo.cn/common_definition/comm_def"
+	"dashoo.cn/micro_libary/micro_srv"
+	"dashoo.cn/micro_libary/myerrors"
+	"github.com/gogf/gf/frame/g"
+)
+
+type LearningLearningRecord struct{}
+
+func (c *LearningLearningRecord) List(ctx context.Context, req *learning.LearningLearningRecordListReq, rsp *comm_def.CommonMsg) error {
+	g.Log().Infof("LearningLearningRecord.List request %v ", &req)
+	s, err := learningSrv.NewLearningLearningRecordService(ctx)
+	if err != nil {
+		return err
+	}
+	total, ent, err := s.List(ctx, req)
+	_, err, code, msg := myerrors.CheckError(err)
+	if err != nil {
+		return err
+	}
+	if ent == nil {
+		ent = []*learning.LearningLearningRecordGetRsp{}
+	}
+	rsp.Code = code
+	rsp.Msg = msg
+	rsp.Data = map[string]interface{}{
+		"total": total,
+		"list":  ent,
+	}
+	return nil
+}
+
+func (c *LearningLearningRecord) ListMy(ctx context.Context, req *learning.LearningLearningRecordListReq, rsp *comm_def.CommonMsg) error {
+	g.Log().Infof("LearningLearningRecord.List request %v ", &req)
+	s, err := learningSrv.NewLearningLearningRecordService(ctx)
+	if err != nil {
+		return err
+	}
+	userInfo, err := micro_srv.GetUserInfo(ctx)
+	if err != nil {
+		return fmt.Errorf("获取用户信息异常:%s", err.Error())
+	}
+
+	req.UserId = int(userInfo.Id)
+	total, ent, err := s.List(ctx, req)
+	_, err, code, msg := myerrors.CheckError(err)
+	if err != nil {
+		return err
+	}
+	if ent == nil {
+		ent = []*learning.LearningLearningRecordGetRsp{}
+	}
+	rsp.Code = code
+	rsp.Msg = msg
+	rsp.Data = map[string]interface{}{
+		"total": total,
+		"list":  ent,
+	}
+	return nil
+}
+
+func (c *LearningLearningRecord) AddToMy(ctx context.Context, req *learning.LearningLearningRecordAddReq, rsp *comm_def.CommonMsg) error {
+	g.Log().Infof("LearningLearningRecord.AddToMy request %v ", &req)
+	s, err := learningSrv.NewLearningLearningRecordService(ctx)
+	if err != nil {
+		return err
+	}
+	userInfo, err := micro_srv.GetUserInfo(ctx)
+	if err != nil {
+		return fmt.Errorf("获取用户信息异常:%s", err.Error())
+	}
+
+	req.UserId = int(userInfo.Id)
+	id, err := s.Add(ctx, req)
+	_, err, code, msg := myerrors.CheckError(err)
+	if err != nil {
+		return err
+	}
+	rsp.Code = code
+	rsp.Msg = msg
+	rsp.Data = id
+	return nil
+}

+ 21 - 0
model/learning/learning_learning_record.go

@@ -5,6 +5,7 @@
 package learning
 
 import (
+	"lims_adapter/model"
 	"lims_adapter/model/learning/internal"
 )
 
@@ -12,3 +13,23 @@ import (
 type LearningLearningRecord internal.LearningLearningRecord
 
 // Fill with you ideas below.
+type LearningLearningRecordListReq struct {
+	Page       *model.Page    `json:"page"`
+	OrderBy    *model.OrderBy `json:"orderBy"`
+	UserId     int            `json:"userId"`     // 用户 Id
+	MaterialId int            `json:"materialId"` // 资料 Id
+	Status     int            `json:"status"`     // 状态 1 已学习
+}
+
+type LearningLearningRecordGetRsp struct {
+	LearningLearningRecord
+	UserName     string `json:"userName"`     // 用户名称
+	SkillName    string `json:"skillName"`    // 技能名称
+	MaterialName string `json:"materialName"` // 资料名称
+}
+
+type LearningLearningRecordAddReq struct {
+	UserId     int `json:"userId" v:"required#请输入用户Id"`               // 用户 Id
+	MaterialId int `json:"materialId" v:"required#请输入资料Id"`           // 资料 Id
+	Status     int `json:"status" v:"required#请输入状态 in:1#请输入正确的状态类型"` // 状态 1 已学习
+}

+ 7 - 7
service/learning/exam_record.go

@@ -41,19 +41,18 @@ func (s LearningExamRecordService) List(ctx context.Context, req *learning.Learn
 	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")
+		LeftJoin("base_user c", "a.SkillId=b.Id")
 	if req.UserId != 0 {
-		m = m.Where("UserId = ?", req.UserId)
+		m = m.Where("a.UserId = ?", req.UserId)
 	}
 	if req.SkillId != 0 {
-		m = m.Where("SkillId = ?", req.SkillId)
+		m = m.Where("a.SkillId = ?", req.SkillId)
 	}
 	if req.TestpaperId != 0 {
-		m = m.Where("TestpaperId = ?", req.TestpaperId)
+		m = m.Where("a.TestpaperId = ?", req.TestpaperId)
 	}
 	if req.Status != 0 {
-		m = m.Where("Status = ?", req.Status)
+		m = m.Where("a.Status = ?", req.Status)
 	}
 	total, err := m.Count()
 	if err != nil {
@@ -78,7 +77,8 @@ func (s LearningExamRecordService) List(ctx context.Context, req *learning.Learn
 	}
 
 	records := []*learning.LearningExamRecordGetRsp{}
-	err = m.Structs(&records)
+	err = m.Fields("a.*", "b.Name as SkillName", "c.Realname as UserName").
+		Structs(&records)
 	if err != nil && err != sql.ErrNoRows {
 		return 0, nil, err
 	}

+ 122 - 0
service/learning/learning_record.go

@@ -0,0 +1,122 @@
+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 LearningLearningRecordService struct {
+	Dao      *dao.LearningLearningRecordDao
+	Tenant   string
+	userInfo request.UserInfo
+}
+
+func NewLearningLearningRecordService(ctx context.Context) (*LearningLearningRecordService, 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 &LearningLearningRecordService{
+		Dao:      dao.NewLearningLearningRecordDao(tenant),
+		Tenant:   tenant,
+		userInfo: userInfo,
+	}, nil
+}
+
+func (s LearningLearningRecordService) List(ctx context.Context, req *learning.LearningLearningRecordListReq) (int, []*learning.LearningLearningRecordGetRsp, error) {
+	m := s.Dao.DB.
+		Table("learning_learning_record a").
+		LeftJoin("learning_material b", "a.MaterialId=b.Id").
+		LeftJoin("learning_skill c", "b.SkillId=c.Id").
+		LeftJoin("base_user d", "a.UserId=d.Id")
+	if req.UserId != 0 {
+		m = m.Where("a.UserId = ?", req.UserId)
+	}
+	if req.MaterialId != 0 {
+		m = m.Where("a.MaterialId = ?", req.MaterialId)
+	}
+	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.Value != "" {
+		order := "asc"
+		if req.OrderBy.Type == "desc" {
+			order = "desc"
+		}
+		m = m.Order(req.OrderBy.Value, order)
+	}
+
+	records := []*learning.LearningLearningRecordGetRsp{}
+	err = m.Fields(
+		"a.*",
+		"b.Name as MaterialName",
+		"c.Name as SkillName",
+		"d.Realname as UserName").
+		Structs(&records)
+	if err != nil && err != sql.ErrNoRows {
+		return 0, nil, err
+	}
+	return total, records, err
+}
+
+func (s LearningLearningRecordService) Add(ctx context.Context, req *learning.LearningLearningRecordAddReq) (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.MaterialId != 0 {
+		r, err := s.Dao.DB.Table("learning_material").Where("Id = ?", req.MaterialId).One()
+		if err != nil {
+			return 0, err
+		}
+		if r.IsEmpty() {
+			return 0, myerrors.NewMsgError(nil, fmt.Sprintf("资料不存在: %d", req.MaterialId))
+		}
+	}
+
+	id, err := s.Dao.InsertAndGetId(learning.LearningLearningRecord{
+		UserId:     req.UserId,
+		MaterialId: req.MaterialId,
+		Status:     req.Status,
+		CreatedAt:  gtime.Now(),
+		UpdatedAt:  gtime.Now(),
+	})
+	return int(id), err
+}

+ 171 - 6
swaggerui/swagger.yml

@@ -480,6 +480,82 @@ paths:
                   success:
                     $ref: "#/components/examples/success"
 
+    /LearningLearningRecord.AddToMy:
+      post:
+        tags:
+          - 考试培训-学习记录
+        operationId: LearningLearningRecordAddToMy
+        summary: 添加学习记录
+        requestBody:
+          required: true
+          content:
+            application/json:
+              schema:
+                oneOf:
+                  - $ref: '#/components/schemas/LearningLearningRecordAddToMy'
+              examples:
+                LearningLearningRecordAddToMy:
+                  $ref: '#/components/examples/LearningLearningRecordAddToMy'
+        responses:
+          200:
+            description: 请求成功
+            content:
+              application/json:
+                examples:
+                  success:
+                    $ref: "#/components/examples/success"
+
+    /LearningLearningRecord.ListMy:
+      post:
+        tags:
+          - 考试培训-学习记录
+        operationId: LearningLearningRecordListMy
+        summary: 查询我的学习记录
+        requestBody:
+          required: true
+          content:
+            application/json:
+              schema:
+                oneOf:
+                  - $ref: '#/components/schemas/LearningLearningRecordListMy'
+              examples:
+                LearningLearningRecordListMy:
+                  $ref: '#/components/examples/LearningLearningRecordListMy'
+        responses:
+          200:
+            description: 请求成功
+            content:
+              application/json:
+                examples:
+                  success:
+                    $ref: "#/components/examples/success"
+
+    /LearningLearningRecord.List:
+      post:
+        tags:
+          - 考试培训-学习记录
+        operationId: LearningLearningRecordList
+        summary: 查询学习记录
+        requestBody:
+          required: true
+          content:
+            application/json:
+              schema:
+                oneOf:
+                  - $ref: '#/components/schemas/LearningLearningRecordList'
+              examples:
+                LearningLearningRecordList:
+                  $ref: '#/components/examples/LearningLearningRecordList'
+        responses:
+          200:
+            description: 请求成功
+            content:
+              application/json:
+                examples:
+                  success:
+                    $ref: "#/components/examples/success"
+
+
 # 添加这个 swagger ui 会显示授权按钮
 security:
   - bearerAuth: []
@@ -815,10 +891,10 @@ components:
       properties:
         skillId:
           type: integer
-          description: 技
+          description: 技能 Id
         testpaperId:
           type: integer
-          description: 试
+          description: 试卷 Id
         status:
           type: integer
           description: 状态 1 通过 2 未通过
@@ -850,10 +926,10 @@ components:
               description: 字段名
         skillId:
           type: integer
-          description: 技
+          description: 技能 Id
         testpaperId:
           type: integer
-          description: 试
+          description: 试卷 Id
         status:
           type: integer
           description: 状态 1 通过 2 未通过
@@ -885,16 +961,95 @@ components:
               description: 字段名
         skillId:
           type: integer
-          description: 技
+          description: 技能 Id
         testpaperId:
           type: integer
-          description: 试
+          description: 试卷 Id
         status:
           type: integer
           description: 状态 1 通过 2 未通过
         userId:
           type: integer
           description: 用户 Id
+    LearningLearningRecordAddToMy:
+      type: object
+      required:
+        - materialId
+        - status
+      properties:
+        materialId:
+          type: integer
+          description: 资料 Id
+        status:
+          type: integer
+          description: 状态 1 已学习
+    LearningLearningRecordListMy:
+      type: object
+      properties:
+        page:
+          type: object
+          description: 分页信息,不传默认不分页,返回所有数据
+          properties:
+            current:
+              type: integer
+              description: 当前页面
+            size:
+              type: integer
+              description: 每页条数
+        orderBy:
+          type: object
+          description: 排序
+          properties:
+            type:
+              type: string
+              description: 排序方式
+              enum:
+                - asc
+                - desc
+            value:
+              type: string
+              description: 字段名
+        materialId:
+          type: integer
+          description: 资料 Id
+        status:
+          type: integer
+          description: 状态 1 已学习
+    LearningLearningRecordList:
+      type: object
+      properties:
+        page:
+          type: object
+          description: 分页信息,不传默认不分页,返回所有数据
+          properties:
+            current:
+              type: integer
+              description: 当前页面
+            size:
+              type: integer
+              description: 每页条数
+        orderBy:
+          type: object
+          description: 排序
+          properties:
+            type:
+              type: string
+              description: 排序方式
+              enum:
+                - asc
+                - desc
+            value:
+              type: string
+              description: 字段名
+        materialId:
+          type: integer
+          description: 资料 Id
+        status:
+          type: integer
+          description: 状态 1 已学习
+        userId:
+          type: integer
+          description: 用户 Id
 
   examples:
     success:
@@ -1049,3 +1204,13 @@ components:
     LearningExamRecordList:
       value:
         skillId: 2
+    LearningLearningRecordAddToMy:
+      value:
+        materialId: 2
+        status: 2
+    LearningLearningRecordListMy:
+      value:
+        materialId: 2
+    LearningLearningRecordList:
+      value:
+        materialId: 2