material.go 6.4 KB

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