| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package learning
- import (
- "context"
- "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 LearningMaterialService struct {
- Dao *dao.LearningMaterialDao
- FileDao *dao.LearningMaterialFileDao
- LearningRecordDao *dao.LearningLearningRecordDao
- LearningRecordSrv *LearningLearningRecordService
- Tenant string
- userInfo request.UserInfo
- }
- func NewLearningMaterialService(ctx context.Context) (*LearningMaterialService, 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())
- }
- lrSrv, err := NewLearningLearningRecordService(ctx)
- if err != nil {
- return nil, err
- }
- return &LearningMaterialService{
- Dao: dao.NewLearningMaterialDao(tenant),
- FileDao: dao.NewLearningMaterialFileDao(tenant),
- LearningRecordDao: dao.NewLearningLearningRecordDao(tenant),
- LearningRecordSrv: lrSrv,
- Tenant: tenant,
- userInfo: userInfo,
- }, nil
- }
- func (s LearningMaterialService) Get(ctx context.Context, req *learning.LearningMaterialGetReq) (ent *learning.LearningMaterialGetRsp, err error) {
- var m *learning.LearningMaterial
- if req.Id != 0 {
- m, err = s.Dao.Where("Id = ?", req.Id).One()
- if err != nil {
- return
- }
- }
- if req.Name != "" {
- m, err = s.Dao.Where("Name = ?", req.Name).One()
- if err != nil {
- return
- }
- }
- if m == nil {
- return nil, myerrors.NewMsgError(nil, "培训材料不存在")
- }
- file, err := s.FileDao.Where("MaterialId = ?", m.Id).All()
- if err != nil {
- return nil, err
- }
- _, record, err := s.LearningRecordSrv.List(ctx, &learning.LearningLearningRecordListReq{MaterialId: req.Id})
- if err != nil {
- return nil, err
- }
- return &learning.LearningMaterialGetRsp{
- LearningMaterial: *m,
- File: file,
- LearningRecord: record,
- }, nil
- }
- func (s LearningMaterialService) List(ctx context.Context, req *learning.LearningMaterialListReq) (int, []*learning.LearningMaterial, error) {
- dao := &s.Dao.LearningMaterialDao
- if req.Name != "" {
- dao = dao.Where("Name LIKE ?", fmt.Sprintf("%%%s%%", req.Name))
- }
- if req.SkillId != 0 {
- dao = dao.Where("SkillId = ?", req.SkillId)
- }
- 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 LearningMaterialService) Add(ctx context.Context, req *learning.LearningMaterialAddReq) (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("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))
- }
- m, err := s.Dao.Where("Name = ?", req.Name).One()
- if err != nil {
- return 0, err
- }
- if m != nil {
- return 0, myerrors.NewMsgError(nil, fmt.Sprintf("资料已存在: %s", req.Name))
- }
- id, err := s.Dao.InsertAndGetId(learning.LearningMaterial{
- SkillId: req.SkillId,
- Name: req.Name,
- Type: req.Type,
- SortNo: req.SortNo,
- Enable: req.Enable,
- Content: req.Content,
- OperateBy: s.userInfo.RealName,
- CreatedAt: gtime.Now(),
- UpdatedAt: gtime.Now(),
- })
- if err != nil {
- return 0, err
- }
- if len(req.File) == 0 {
- return int(id), err
- }
- files := []learning.LearningMaterialFile{}
- for _, f := range req.File {
- files = append(files, learning.LearningMaterialFile{
- MaterialId: int(id),
- Name: f.Name,
- Url: f.Url,
- Size: f.Size,
- Extend: f.Extend,
- OperateBy: s.userInfo.RealName,
- CreatedAt: gtime.Now(),
- UpdatedAt: gtime.Now(),
- })
- }
- _, err = s.FileDao.Insert(files)
- return int(id), err
- }
- func (s LearningMaterialService) Update(ctx context.Context, req *learning.LearningMaterialUpdateReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.NewMsgError(nil, validErr.Current().Error())
- }
- m, err := s.Dao.Where("Id = ?", req.Id).One()
- if err != nil {
- return err
- }
- if m == 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))
- }
- }
- if req.Name != "" {
- existM, err := s.Dao.Where("Name = ?", req.Name).One()
- if err != nil {
- return err
- }
- if existM != nil && existM.Id != m.Id {
- return myerrors.NewMsgError(nil, fmt.Sprintf("资料已存在: %s", req.Name))
- }
- }
- dao := &s.Dao.LearningMaterialDao
- toupdate := map[string]interface{}{}
- if req.SkillId != 0 {
- toupdate["SkillId"] = req.SkillId
- }
- if req.Name != "" {
- toupdate["Name"] = req.Name
- }
- if req.Type != nil {
- toupdate["Type"] = req.Type
- }
- if req.SortNo != nil {
- toupdate["SortNo"] = req.SortNo
- }
- if req.Enable != nil {
- toupdate["Enable"] = req.Enable
- }
- if req.Content != "" {
- toupdate["Content"] = req.Content
- }
- 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.File == nil {
- return nil
- }
- _, err = s.FileDao.Where("MaterialId = ?", req.Id).Delete()
- if err != nil {
- return err
- }
- if len(req.File) == 0 {
- return nil
- }
- files := []learning.LearningMaterialFile{}
- for _, f := range req.File {
- files = append(files, learning.LearningMaterialFile{
- MaterialId: m.Id,
- Name: f.Name,
- Url: f.Url,
- Size: f.Size,
- Extend: f.Extend,
- OperateBy: s.userInfo.RealName,
- CreatedAt: gtime.Now(),
- UpdatedAt: gtime.Now(),
- })
- }
- _, err = s.FileDao.Insert(files)
- return err
- }
- func (s LearningMaterialService) Delete(ctx context.Context, id []int) error {
- _, err := s.Dao.Where("Id IN (?)", id).Delete()
- if err != nil {
- return err
- }
- _, err = s.FileDao.Where("MaterialId IN (?)", id).Delete()
- return err
- }
|