skill.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package learning
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "lims_adapter/dao/learning"
  7. "lims_adapter/model/equipment"
  8. "lims_adapter/model/learning"
  9. "dashoo.cn/micro_libary/micro_srv"
  10. "dashoo.cn/micro_libary/myerrors"
  11. "dashoo.cn/micro_libary/request"
  12. "github.com/gogf/gf/frame/g"
  13. "github.com/gogf/gf/os/gtime"
  14. "github.com/gogf/gf/util/gvalid"
  15. )
  16. type LearningSkillService struct {
  17. LearningSkillDao *dao.LearningSkillDao
  18. LearningSkillInstrumentDao *dao.LearningSkillInstrumentDao
  19. LearningMaterialSrv *LearningMaterialService
  20. LearningRecordSrv *LearningLearningRecordService
  21. Tenant string
  22. userInfo request.UserInfo
  23. }
  24. func NewLearningSkillService(ctx context.Context) (*LearningSkillService, error) {
  25. tenant, err := micro_srv.GetTenant(ctx)
  26. if err != nil {
  27. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  28. }
  29. // 获取用户信息
  30. userInfo, err := micro_srv.GetUserInfo(ctx)
  31. if err != nil {
  32. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  33. }
  34. lrSrv, err := NewLearningLearningRecordService(ctx)
  35. if err != nil {
  36. return nil, err
  37. }
  38. lmSrv, err := NewLearningMaterialService(ctx)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return &LearningSkillService{
  43. LearningSkillDao: dao.NewLearningSkillDao(tenant),
  44. LearningSkillInstrumentDao: dao.NewLearningSkillInstrumentDao(tenant),
  45. LearningMaterialSrv: lmSrv,
  46. LearningRecordSrv: lrSrv,
  47. Tenant: tenant,
  48. userInfo: userInfo,
  49. }, nil
  50. }
  51. func (s LearningSkillService) Get(ctx context.Context, req *learning.LearningSkillGetReq) (ent *learning.LearningSkillGetRsp, err error) {
  52. var skill *learning.LearningSkill
  53. if req.Id != 0 {
  54. skill, err = s.LearningSkillDao.Where("Id = ?", req.Id).One()
  55. if err != nil {
  56. return
  57. }
  58. }
  59. if req.Name != "" {
  60. skill, err = s.LearningSkillDao.Where("Name = ?", req.Name).One()
  61. if err != nil {
  62. return
  63. }
  64. }
  65. if skill == nil {
  66. return nil, myerrors.NewMsgError(nil, "技能不存在")
  67. }
  68. instr := []equipment.Instrument{}
  69. err = s.LearningSkillDao.DB.
  70. Table("learning_skill_instrument a").
  71. LeftJoin("instrument b", "a.InstrumentId=b.Id").
  72. Where("a.SkillId = ?", skill.Id).
  73. Fields("b.*").
  74. Structs(&instr)
  75. if err != nil && err != sql.ErrNoRows {
  76. return nil, err
  77. }
  78. return &learning.LearningSkillGetRsp{
  79. Id: skill.Id,
  80. Name: skill.Name,
  81. Instrument: instr,
  82. }, nil
  83. }
  84. func (s LearningSkillService) List(ctx context.Context) ([]*learning.LearningSkill, error) {
  85. return s.LearningSkillDao.All()
  86. }
  87. func (s LearningSkillService) Add(ctx context.Context, req *learning.LearningSkillAddReq) (int, error) {
  88. validErr := gvalid.CheckStruct(ctx, req, nil)
  89. if validErr != nil {
  90. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  91. }
  92. skill, err := s.LearningSkillDao.Where("Name = ?", req.Name).One()
  93. if err != nil {
  94. return 0, err
  95. }
  96. if skill != nil {
  97. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能已存在: %s", req.Name))
  98. }
  99. skillId, err := s.LearningSkillDao.InsertAndGetId(learning.LearningSkill{
  100. Name: req.Name,
  101. CreatedAt: gtime.Now(),
  102. UpdatedAt: gtime.Now(),
  103. })
  104. if err != nil {
  105. return 0, err
  106. }
  107. relation := []learning.LearningSkillInstrument{}
  108. for _, instrumentId := range req.InstrumentId {
  109. r, err := g.DB(s.Tenant).Table("instrument").Where("Id = ?", instrumentId).One()
  110. if err != nil {
  111. return 0, err
  112. }
  113. if r.IsEmpty() {
  114. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("设备不存在: %d", instrumentId))
  115. }
  116. relation = append(relation, learning.LearningSkillInstrument{
  117. SkillId: int(skillId),
  118. InstrumentId: instrumentId,
  119. })
  120. }
  121. _, err = s.LearningSkillInstrumentDao.Insert(relation)
  122. return int(skillId), err
  123. }
  124. func (s LearningSkillService) Update(ctx context.Context, req *learning.LearningSkillUpdateReq) error {
  125. validErr := gvalid.CheckStruct(ctx, req, nil)
  126. if validErr != nil {
  127. return myerrors.NewMsgError(nil, validErr.Current().Error())
  128. }
  129. skill, err := s.LearningSkillDao.Where("Id = ?", req.Id).One()
  130. if err != nil {
  131. return err
  132. }
  133. if skill == nil {
  134. return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.Id))
  135. }
  136. if req.Name != "" {
  137. existSkill, err := s.LearningSkillDao.Where("Name = ?", req.Name).One()
  138. if err != nil {
  139. return err
  140. }
  141. if existSkill != nil && existSkill.Id != skill.Id {
  142. return myerrors.NewMsgError(nil, fmt.Sprintf("技能已存在: %s", req.Name))
  143. }
  144. _, err = s.LearningSkillDao.
  145. Where("Id = ?", req.Id).
  146. Data("Name = ?", req.Name).
  147. Update()
  148. if err != nil {
  149. return err
  150. }
  151. }
  152. if req.InstrumentId != nil {
  153. _, err := s.LearningSkillInstrumentDao.
  154. Where("SkillId = ?", req.Id).Delete()
  155. if err != nil {
  156. return err
  157. }
  158. relation := []learning.LearningSkillInstrument{}
  159. for _, instrumentId := range *req.InstrumentId {
  160. r, err := g.DB(s.Tenant).Table("instrument").Where("Id = ?", instrumentId).One()
  161. if err != nil {
  162. return err
  163. }
  164. if r.IsEmpty() {
  165. return myerrors.NewMsgError(nil, fmt.Sprintf("设备不存在: %d", instrumentId))
  166. }
  167. relation = append(relation, learning.LearningSkillInstrument{
  168. SkillId: int(req.Id),
  169. InstrumentId: instrumentId,
  170. })
  171. }
  172. _, err = s.LearningSkillInstrumentDao.Insert(relation)
  173. if err != nil {
  174. return err
  175. }
  176. }
  177. return nil
  178. }
  179. func (s LearningSkillService) Delete(ctx context.Context, id []int) error {
  180. _, err := s.LearningSkillInstrumentDao.Where("SkillId IN (?)", id).Delete()
  181. if err != nil {
  182. return err
  183. }
  184. _, err = s.LearningSkillDao.Where("Id IN (?)", id).Delete()
  185. return err
  186. }
  187. func (s LearningSkillService) ListMy(ctx context.Context) ([]*learning.LearningSkillMy, error) {
  188. ent, err := s.LearningSkillDao.All()
  189. if err != nil {
  190. return nil, err
  191. }
  192. list := []*learning.LearningSkillMy{}
  193. for _, skill := range ent {
  194. materialIds, err := s.LearningMaterialSrv.MaterialIds(ctx, skill.Id)
  195. if err != nil {
  196. return nil, err
  197. }
  198. recordIds, err := s.LearningRecordSrv.LearntMaterialIds(ctx, int(s.userInfo.Id))
  199. if err != nil {
  200. return nil, err
  201. }
  202. // 如果没有关联的资料,直接算学完了
  203. learntAll := true
  204. for _, mid := range materialIds {
  205. found := false
  206. for _, rid := range recordIds {
  207. if mid == rid {
  208. found = true
  209. }
  210. }
  211. if !found {
  212. learntAll = false
  213. break
  214. }
  215. }
  216. // fmt.Println(skill.Id, materialIds, recordIds, learntAll)
  217. list = append(list, &learning.LearningSkillMy{
  218. LearningSkill: *skill,
  219. LearntAll: learntAll,
  220. })
  221. }
  222. return list, err
  223. }