| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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, 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_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
- }
- func (s LearningLearningRecordService) LearntMaterialIds(ctx context.Context, userId int) ([]int, error) {
- _, records, err := s.List(ctx, &learning.LearningLearningRecordListReq{
- UserId: userId,
- Status: 1,
- })
- if err != nil {
- return nil, err
- }
- ids := []int{}
- for _, r := range records {
- ids = append(ids, r.MaterialId)
- }
- return ids, err
- }
|