label.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/ep/melloi/model"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. )
  8. // AddLabel create new label
  9. func (s *Service) AddLabel(label *model.Label) (err error) {
  10. if _, err = s.dao.QueryLabel(label); err == nil {
  11. err = ecode.MelloiLabelExistErr
  12. return
  13. }
  14. label.Active = 1
  15. return s.dao.AddLabel(label)
  16. }
  17. // QueryLabel query all labels
  18. func (s *Service) QueryLabel(c context.Context) ([]*model.Label, error) {
  19. return s.dao.QueryLabels(c)
  20. }
  21. // DeleteLabel delete label by id
  22. func (s *Service) DeleteLabel(id int64) error {
  23. if id <= 0 {
  24. return ecode.RequestErr
  25. }
  26. return s.dao.DeleteLabel(id)
  27. }
  28. // AddLabelRelation create new label relation
  29. func (s *Service) AddLabelRelation(lr *model.LabelRelation) (err error) {
  30. label := &model.Label{ID: lr.LabelID}
  31. if label, err = s.dao.QueryLabel(label); err != nil {
  32. return ecode.RequestErr
  33. }
  34. // 存在相同的记录
  35. if _, err = s.dao.QueryLabelExist(lr); err != nil {
  36. return ecode.MelloiLabelExistErr
  37. }
  38. // 每个脚本|任务,最多有2个label
  39. var lre []*model.LabelRelation
  40. relation := model.LabelRelation{Type: lr.Type, TargetID: lr.TargetID}
  41. if lre, err = s.dao.QueryLabelRelation(&relation); err != nil {
  42. return err
  43. }
  44. if len(lre) >= 2 {
  45. return ecode.MelloiLabelCountErr
  46. }
  47. lr.Description = label.Description
  48. lr.Color = label.Color
  49. lr.LabelName = label.Name
  50. lr.Active = 1
  51. if err = s.dao.AddLabelRelation(lr); err != nil {
  52. log.Error("s.dao.AddLabelRelation err :(%v)", err)
  53. return
  54. }
  55. return
  56. }
  57. // DeleteLabelRelation delete label relation by id
  58. func (s *Service) DeleteLabelRelation(id int64) (err error) {
  59. if id <= 0 {
  60. return ecode.RequestErr
  61. }
  62. // 标签不存在
  63. var exist bool
  64. if exist, err = s.dao.CheckLabelRelationExist(id); err != nil {
  65. return
  66. }
  67. if !exist {
  68. return ecode.MelloiLabelRelationNotExist
  69. }
  70. return s.dao.DeleteLabelRelation(id)
  71. }
  72. // QueryLabelRelation query label relation by id , type, targetid
  73. func (s *Service) QueryLabelRelation(lre *model.LabelRelation) (lr []*model.LabelRelation, err error) {
  74. return s.dao.QueryLabelRelation(lre)
  75. }
  76. // QueryLabelRelationByIDs query label relation by ids
  77. func (s *Service) QueryLabelRelationByIDs(ids []int64) (lr []*model.LabelRelation, err error) {
  78. return s.dao.QueryLabelRelationByIDs(ids)
  79. }