skill.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Tenant string
  20. userInfo request.UserInfo
  21. }
  22. func NewLearningSkillService(ctx context.Context) (*LearningSkillService, error) {
  23. tenant, err := micro_srv.GetTenant(ctx)
  24. if err != nil {
  25. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  26. }
  27. // 获取用户信息
  28. userInfo, err := micro_srv.GetUserInfo(ctx)
  29. if err != nil {
  30. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  31. }
  32. return &LearningSkillService{
  33. LearningSkillDao: dao.NewLearningSkillDao(tenant),
  34. LearningSkillInstrumentDao: dao.NewLearningSkillInstrumentDao(tenant),
  35. Tenant: tenant,
  36. userInfo: userInfo,
  37. }, nil
  38. }
  39. func (s LearningSkillService) Get(ctx context.Context, req *learning.LearningSkillGetReq) (ent *learning.LearningSkillGetRsp, err error) {
  40. var skill *learning.LearningSkill
  41. if req.Id != 0 {
  42. skill, err = s.LearningSkillDao.Where("Id = ?", req.Id).One()
  43. if err != nil {
  44. return
  45. }
  46. }
  47. if req.Name != "" {
  48. skill, err = s.LearningSkillDao.Where("Name = ?", req.Name).One()
  49. if err != nil {
  50. return
  51. }
  52. }
  53. if skill == nil {
  54. return nil, myerrors.NewMsgError(nil, "技能不存在")
  55. }
  56. instr := []equipment.Instrument{}
  57. err = s.LearningSkillDao.DB.
  58. Table("learning_skill_instrument a").
  59. LeftJoin("instrument b", "a.InstrumentId=b.Id").
  60. Where("a.SkillId = ?", skill.Id).
  61. Fields("b.*").
  62. Structs(&instr)
  63. if err != nil && err != sql.ErrNoRows {
  64. return nil, err
  65. }
  66. return &learning.LearningSkillGetRsp{
  67. Id: skill.Id,
  68. Name: skill.Name,
  69. Instrument: instr,
  70. }, nil
  71. }
  72. func (s LearningSkillService) List(ctx context.Context) ([]*learning.LearningSkill, error) {
  73. return s.LearningSkillDao.All()
  74. }
  75. func (s LearningSkillService) Add(ctx context.Context, req *learning.LearningSkillAddReq) (int, error) {
  76. validErr := gvalid.CheckStruct(ctx, req, nil)
  77. if validErr != nil {
  78. return 0, validErr.Current()
  79. }
  80. skill, err := s.LearningSkillDao.Where("Name = ?", req.Name).One()
  81. if err != nil {
  82. return 0, err
  83. }
  84. if skill != nil {
  85. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能已存在: %s", req.Name))
  86. }
  87. skillId, err := s.LearningSkillDao.InsertAndGetId(learning.LearningSkill{
  88. Name: req.Name,
  89. CreatedAt: gtime.Now(),
  90. UpdatedAt: gtime.Now(),
  91. })
  92. if err != nil {
  93. return 0, err
  94. }
  95. relation := []learning.LearningSkillInstrument{}
  96. for _, instrumentId := range req.InstrumentId {
  97. r, err := g.DB(s.Tenant).Table("instrument").Where("Id = ?", instrumentId).One()
  98. if err != nil {
  99. return 0, err
  100. }
  101. if r.IsEmpty() {
  102. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("设备不存在: %d", instrumentId))
  103. }
  104. relation = append(relation, learning.LearningSkillInstrument{
  105. SkillId: int(skillId),
  106. InstrumentId: instrumentId,
  107. })
  108. }
  109. _, err = s.LearningSkillInstrumentDao.Insert(relation)
  110. return int(skillId), err
  111. }
  112. func (s LearningSkillService) Update(ctx context.Context, req *learning.LearningSkillUpdateReq) error {
  113. validErr := gvalid.CheckStruct(ctx, req, nil)
  114. if validErr != nil {
  115. return validErr.Current()
  116. }
  117. if req.Name != "" {
  118. skill, err := s.LearningSkillDao.Where("Name = ?", req.Name).One()
  119. if err != nil {
  120. return err
  121. }
  122. if skill != nil {
  123. return myerrors.NewMsgError(nil, fmt.Sprintf("技能已存在: %s", req.Name))
  124. }
  125. _, err = s.LearningSkillDao.
  126. Where("Id = ?", req.Id).
  127. Data("Name = ?", req.Name).
  128. Update()
  129. if err != nil {
  130. return err
  131. }
  132. }
  133. if req.InstrumentId != nil {
  134. _, err := s.LearningSkillInstrumentDao.
  135. Where("SkillId = ?", req.Id).Delete()
  136. if err != nil {
  137. return err
  138. }
  139. relation := []learning.LearningSkillInstrument{}
  140. for _, instrumentId := range *req.InstrumentId {
  141. r, err := g.DB(s.Tenant).Table("instrument").Where("Id = ?", instrumentId).One()
  142. if err != nil {
  143. return err
  144. }
  145. if r.IsEmpty() {
  146. return myerrors.NewMsgError(nil, fmt.Sprintf("设备不存在: %d", instrumentId))
  147. }
  148. relation = append(relation, learning.LearningSkillInstrument{
  149. SkillId: int(req.Id),
  150. InstrumentId: instrumentId,
  151. })
  152. }
  153. _, err = s.LearningSkillInstrumentDao.Insert(relation)
  154. return err
  155. }
  156. return nil
  157. }
  158. func (s LearningSkillService) Delete(ctx context.Context, id int) error {
  159. _, err := s.LearningSkillInstrumentDao.Where("SkillId = ?", id).Delete()
  160. if err != nil {
  161. return err
  162. }
  163. _, err = s.LearningSkillDao.Where("Id = ?", id).Delete()
  164. if err != nil {
  165. return err
  166. }
  167. return nil
  168. }