6
0

material.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package learning
  2. import (
  3. "context"
  4. "fmt"
  5. "lims_adapter/dao/learning"
  6. "lims_adapter/model/learning"
  7. "dashoo.cn/micro_libary/micro_srv"
  8. "dashoo.cn/micro_libary/myerrors"
  9. "dashoo.cn/micro_libary/request"
  10. "github.com/gogf/gf/os/gtime"
  11. "github.com/gogf/gf/util/gvalid"
  12. )
  13. type LearningMaterialService struct {
  14. Dao *dao.LearningMaterialDao
  15. FileDao *dao.LearningMaterialFileDao
  16. LearningRecordDao *dao.LearningLearningRecordDao
  17. LearningRecordSrv *LearningLearningRecordService
  18. Tenant string
  19. userInfo request.UserInfo
  20. }
  21. func NewLearningMaterialService(ctx context.Context) (*LearningMaterialService, error) {
  22. tenant, err := micro_srv.GetTenant(ctx)
  23. if err != nil {
  24. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  25. }
  26. // 获取用户信息
  27. userInfo, err := micro_srv.GetUserInfo(ctx)
  28. if err != nil {
  29. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  30. }
  31. lrSrv, err := NewLearningLearningRecordService(ctx)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return &LearningMaterialService{
  36. Dao: dao.NewLearningMaterialDao(tenant),
  37. FileDao: dao.NewLearningMaterialFileDao(tenant),
  38. LearningRecordDao: dao.NewLearningLearningRecordDao(tenant),
  39. LearningRecordSrv: lrSrv,
  40. Tenant: tenant,
  41. userInfo: userInfo,
  42. }, nil
  43. }
  44. func (s LearningMaterialService) Get(ctx context.Context, req *learning.LearningMaterialGetReq) (ent *learning.LearningMaterialGetRsp, err error) {
  45. var m *learning.LearningMaterial
  46. if req.Id != 0 {
  47. m, err = s.Dao.Where("Id = ?", req.Id).One()
  48. if err != nil {
  49. return
  50. }
  51. }
  52. if req.Name != "" {
  53. m, err = s.Dao.Where("Name = ?", req.Name).One()
  54. if err != nil {
  55. return
  56. }
  57. }
  58. if m == nil {
  59. return nil, myerrors.NewMsgError(nil, "培训材料不存在")
  60. }
  61. file, err := s.FileDao.Where("MaterialId = ?", m.Id).All()
  62. if err != nil {
  63. return nil, err
  64. }
  65. _, record, err := s.LearningRecordSrv.List(ctx, &learning.LearningLearningRecordListReq{MaterialId: req.Id})
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &learning.LearningMaterialGetRsp{
  70. LearningMaterial: *m,
  71. File: file,
  72. LearningRecord: record,
  73. }, nil
  74. }
  75. func (s LearningMaterialService) List(ctx context.Context, req *learning.LearningMaterialListReq) (int, []*learning.LearningMaterial, error) {
  76. dao := &s.Dao.LearningMaterialDao
  77. if req.Name != "" {
  78. dao = dao.Where("Name LIKE ?", fmt.Sprintf("%%%s%%", req.Name))
  79. }
  80. if req.SkillId != 0 {
  81. dao = dao.Where("SkillId = ?", req.SkillId)
  82. }
  83. total, err := dao.Count()
  84. if err != nil {
  85. return 0, nil, err
  86. }
  87. if req.Page != nil {
  88. if req.Page.Current == 0 {
  89. req.Page.Current = 1
  90. }
  91. if req.Page.Size == 0 {
  92. req.Page.Size = 10
  93. }
  94. dao = dao.Page(req.Page.Current, req.Page.Size)
  95. }
  96. if req.OrderBy != nil && req.OrderBy.Value != "" {
  97. order := "asc"
  98. if req.OrderBy.Type == "desc" {
  99. order = "desc"
  100. }
  101. dao = dao.Order(req.OrderBy.Value, order)
  102. }
  103. ent, err := dao.All()
  104. return total, ent, err
  105. }
  106. func (s LearningMaterialService) Add(ctx context.Context, req *learning.LearningMaterialAddReq) (int, error) {
  107. validErr := gvalid.CheckStruct(ctx, req, nil)
  108. if validErr != nil {
  109. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  110. }
  111. r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
  112. if err != nil {
  113. return 0, err
  114. }
  115. if r.IsEmpty() {
  116. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  117. }
  118. m, err := s.Dao.Where("Name = ?", req.Name).One()
  119. if err != nil {
  120. return 0, err
  121. }
  122. if m != nil {
  123. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("资料已存在: %s", req.Name))
  124. }
  125. id, err := s.Dao.InsertAndGetId(learning.LearningMaterial{
  126. SkillId: req.SkillId,
  127. Name: req.Name,
  128. Type: req.Type,
  129. SortNo: req.SortNo,
  130. Enable: req.Enable,
  131. Content: req.Content,
  132. OperateBy: s.userInfo.RealName,
  133. CreatedAt: gtime.Now(),
  134. UpdatedAt: gtime.Now(),
  135. })
  136. if err != nil {
  137. return 0, err
  138. }
  139. if len(req.File) == 0 {
  140. return int(id), err
  141. }
  142. files := []learning.LearningMaterialFile{}
  143. for _, f := range req.File {
  144. files = append(files, learning.LearningMaterialFile{
  145. MaterialId: int(id),
  146. Name: f.Name,
  147. Url: f.Url,
  148. Size: f.Size,
  149. Extend: f.Extend,
  150. OperateBy: s.userInfo.RealName,
  151. CreatedAt: gtime.Now(),
  152. UpdatedAt: gtime.Now(),
  153. })
  154. }
  155. _, err = s.FileDao.Insert(files)
  156. return int(id), err
  157. }
  158. func (s LearningMaterialService) Update(ctx context.Context, req *learning.LearningMaterialUpdateReq) error {
  159. validErr := gvalid.CheckStruct(ctx, req, nil)
  160. if validErr != nil {
  161. return myerrors.NewMsgError(nil, validErr.Current().Error())
  162. }
  163. m, err := s.Dao.Where("Id = ?", req.Id).One()
  164. if err != nil {
  165. return err
  166. }
  167. if m == nil {
  168. return myerrors.NewMsgError(nil, fmt.Sprintf("资料不存在: %d", req.Id))
  169. }
  170. if req.SkillId != 0 {
  171. r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
  172. if err != nil {
  173. return err
  174. }
  175. if r.IsEmpty() {
  176. return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  177. }
  178. }
  179. if req.Name != "" {
  180. existM, err := s.Dao.Where("Name = ?", req.Name).One()
  181. if err != nil {
  182. return err
  183. }
  184. if existM != nil && existM.Id != m.Id {
  185. return myerrors.NewMsgError(nil, fmt.Sprintf("资料已存在: %s", req.Name))
  186. }
  187. }
  188. dao := &s.Dao.LearningMaterialDao
  189. toupdate := map[string]interface{}{}
  190. if req.SkillId != 0 {
  191. toupdate["SkillId"] = req.SkillId
  192. }
  193. if req.Name != "" {
  194. toupdate["Name"] = req.Name
  195. }
  196. if req.Type != nil {
  197. toupdate["Type"] = req.Type
  198. }
  199. if req.SortNo != nil {
  200. toupdate["SortNo"] = req.SortNo
  201. }
  202. if req.Enable != nil {
  203. toupdate["Enable"] = req.Enable
  204. }
  205. if req.Content != "" {
  206. toupdate["Content"] = req.Content
  207. }
  208. if len(toupdate) != 0 {
  209. toupdate["OperateBy"] = s.userInfo.RealName
  210. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  211. if err != nil {
  212. return err
  213. }
  214. }
  215. if req.File == nil {
  216. return nil
  217. }
  218. _, err = s.FileDao.Where("MaterialId = ?", req.Id).Delete()
  219. if err != nil {
  220. return err
  221. }
  222. if len(req.File) == 0 {
  223. return nil
  224. }
  225. files := []learning.LearningMaterialFile{}
  226. for _, f := range req.File {
  227. files = append(files, learning.LearningMaterialFile{
  228. MaterialId: m.Id,
  229. Name: f.Name,
  230. Url: f.Url,
  231. Size: f.Size,
  232. Extend: f.Extend,
  233. OperateBy: s.userInfo.RealName,
  234. CreatedAt: gtime.Now(),
  235. UpdatedAt: gtime.Now(),
  236. })
  237. }
  238. _, err = s.FileDao.Insert(files)
  239. return err
  240. }
  241. func (s LearningMaterialService) Delete(ctx context.Context, id []int) error {
  242. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  243. if err != nil {
  244. return err
  245. }
  246. _, err = s.FileDao.Where("MaterialId IN (?)", id).Delete()
  247. return err
  248. }