material.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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) ListMy(ctx context.Context, req *learning.LearningMaterialListMyReq) ([]*learning.LearningMaterialListMy, error) {
  107. validErr := gvalid.CheckStruct(ctx, req, nil)
  108. if validErr != nil {
  109. return nil, myerrors.NewMsgError(nil, validErr.Current().Error())
  110. }
  111. ent, err := s.Dao.Where("SkillId = ?", req.SkillId).All()
  112. if err != nil {
  113. return nil, err
  114. }
  115. list := []*learning.LearningMaterialListMy{}
  116. for _, m := range ent {
  117. lr, err := s.LearningRecordDao.Where("MaterialId = ?", m.Id).One()
  118. if err != nil {
  119. return nil, err
  120. }
  121. learnt := true
  122. if lr == nil {
  123. learnt = false
  124. }
  125. list = append(list, &learning.LearningMaterialListMy{
  126. LearningMaterial: *m,
  127. Learnt: learnt,
  128. })
  129. }
  130. return list, nil
  131. }
  132. func (s LearningMaterialService) Add(ctx context.Context, req *learning.LearningMaterialAddReq) (int, error) {
  133. validErr := gvalid.CheckStruct(ctx, req, nil)
  134. if validErr != nil {
  135. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  136. }
  137. r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
  138. if err != nil {
  139. return 0, err
  140. }
  141. if r.IsEmpty() {
  142. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  143. }
  144. m, err := s.Dao.Where("Name = ?", req.Name).One()
  145. if err != nil {
  146. return 0, err
  147. }
  148. if m != nil {
  149. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("资料已存在: %s", req.Name))
  150. }
  151. id, err := s.Dao.InsertAndGetId(learning.LearningMaterial{
  152. SkillId: req.SkillId,
  153. Name: req.Name,
  154. Type: req.Type,
  155. SortNo: req.SortNo,
  156. Enable: req.Enable,
  157. Content: req.Content,
  158. OperateBy: s.userInfo.RealName,
  159. CreatedAt: gtime.Now(),
  160. UpdatedAt: gtime.Now(),
  161. })
  162. if err != nil {
  163. return 0, err
  164. }
  165. if len(req.File) == 0 {
  166. return int(id), err
  167. }
  168. files := []learning.LearningMaterialFile{}
  169. for _, f := range req.File {
  170. files = append(files, learning.LearningMaterialFile{
  171. MaterialId: int(id),
  172. Name: f.Name,
  173. Url: f.Url,
  174. Size: f.Size,
  175. Extend: f.Extend,
  176. OperateBy: s.userInfo.RealName,
  177. CreatedAt: gtime.Now(),
  178. UpdatedAt: gtime.Now(),
  179. })
  180. }
  181. _, err = s.FileDao.Insert(files)
  182. return int(id), err
  183. }
  184. func (s LearningMaterialService) Update(ctx context.Context, req *learning.LearningMaterialUpdateReq) error {
  185. validErr := gvalid.CheckStruct(ctx, req, nil)
  186. if validErr != nil {
  187. return myerrors.NewMsgError(nil, validErr.Current().Error())
  188. }
  189. m, err := s.Dao.Where("Id = ?", req.Id).One()
  190. if err != nil {
  191. return err
  192. }
  193. if m == nil {
  194. return myerrors.NewMsgError(nil, fmt.Sprintf("资料不存在: %d", req.Id))
  195. }
  196. if req.SkillId != 0 {
  197. r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
  198. if err != nil {
  199. return err
  200. }
  201. if r.IsEmpty() {
  202. return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  203. }
  204. }
  205. if req.Name != "" {
  206. existM, err := s.Dao.Where("Name = ?", req.Name).One()
  207. if err != nil {
  208. return err
  209. }
  210. if existM != nil && existM.Id != m.Id {
  211. return myerrors.NewMsgError(nil, fmt.Sprintf("资料已存在: %s", req.Name))
  212. }
  213. }
  214. dao := &s.Dao.LearningMaterialDao
  215. toupdate := map[string]interface{}{}
  216. if req.SkillId != 0 {
  217. toupdate["SkillId"] = req.SkillId
  218. }
  219. if req.Name != "" {
  220. toupdate["Name"] = req.Name
  221. }
  222. if req.Type != nil {
  223. toupdate["Type"] = req.Type
  224. }
  225. if req.SortNo != nil {
  226. toupdate["SortNo"] = req.SortNo
  227. }
  228. if req.Enable != nil {
  229. toupdate["Enable"] = req.Enable
  230. }
  231. if req.Content != "" {
  232. toupdate["Content"] = req.Content
  233. }
  234. if len(toupdate) != 0 {
  235. toupdate["OperateBy"] = s.userInfo.RealName
  236. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  237. if err != nil {
  238. return err
  239. }
  240. }
  241. if req.File == nil {
  242. return nil
  243. }
  244. _, err = s.FileDao.Where("MaterialId = ?", req.Id).Delete()
  245. if err != nil {
  246. return err
  247. }
  248. if len(req.File) == 0 {
  249. return nil
  250. }
  251. files := []learning.LearningMaterialFile{}
  252. for _, f := range req.File {
  253. files = append(files, learning.LearningMaterialFile{
  254. MaterialId: m.Id,
  255. Name: f.Name,
  256. Url: f.Url,
  257. Size: f.Size,
  258. Extend: f.Extend,
  259. OperateBy: s.userInfo.RealName,
  260. CreatedAt: gtime.Now(),
  261. UpdatedAt: gtime.Now(),
  262. })
  263. }
  264. _, err = s.FileDao.Insert(files)
  265. return err
  266. }
  267. func (s LearningMaterialService) Delete(ctx context.Context, id []int) error {
  268. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  269. if err != nil {
  270. return err
  271. }
  272. _, err = s.FileDao.Where("MaterialId IN (?)", id).Delete()
  273. return err
  274. }
  275. func (s LearningMaterialService) MaterialIds(ctx context.Context, skillId int) ([]int, error) {
  276. _, materials, err := s.List(ctx, &learning.LearningMaterialListReq{
  277. SkillId: skillId,
  278. })
  279. if err != nil {
  280. return nil, err
  281. }
  282. materialIds := []int{}
  283. for _, m := range materials {
  284. materialIds = append(materialIds, m.Id)
  285. }
  286. return materialIds, err
  287. }