label.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/ep/melloi/model"
  5. )
  6. // AddLabel add new label
  7. func (d *Dao) AddLabel(label *model.Label) error {
  8. return d.DB.Table(model.Label{}.LabelName()).Create(label).Error
  9. }
  10. // QueryLabels query all labels
  11. func (d *Dao) QueryLabels(c context.Context) (labels []*model.Label, err error) {
  12. err = d.DB.Table(model.Label{}.LabelName()).Where("active = ?", 1).Find(&labels).Error
  13. return
  14. }
  15. // QueryLabel query label by label name and id
  16. func (d *Dao) QueryLabel(lb *model.Label) (label *model.Label, err error) {
  17. label = &model.Label{}
  18. err = d.DB.Table(model.Label{}.LabelName()).Where("active = ?", 1).
  19. Where(model.Label{Name: lb.Name, ID: lb.ID}).First(label).Error
  20. return
  21. }
  22. // DeleteLabel delete label
  23. func (d *Dao) DeleteLabel(id int64) error {
  24. return d.DB.Table(model.Label{}.LabelName()).Where("id = ?", id).Update("active", 0).Error
  25. }
  26. // AddLabelRelation add label relation of target
  27. func (d *Dao) AddLabelRelation(relation *model.LabelRelation) error {
  28. return d.DB.Table(model.LabelRelation{}.LabelRelationName()).Create(relation).Error
  29. }
  30. // QueryLabelRelation query label relation
  31. func (d *Dao) QueryLabelRelation(lre *model.LabelRelation) (lr []*model.LabelRelation, err error) {
  32. err = d.DB.Table(model.LabelRelation{}.LabelRelationName()).
  33. Where("active = ?", 1).
  34. Where(model.LabelRelation{Type: lre.Type, LabelID: lre.LabelID, TargetID: lre.TargetID}).Find(&lr).Error
  35. return
  36. }
  37. // QueryLabelRelationByIDs Query label relation by ids
  38. func (d *Dao) QueryLabelRelationByIDs(ids []int64) (lr []*model.LabelRelation, err error) {
  39. err = d.DB.Table(model.LabelRelation{}.LabelRelationName()).Where(" active = ? ", 1).
  40. Where("id in (?)", ids).Find(lr).Error
  41. return
  42. }
  43. // CheckLabelRelationExist check label relation exist
  44. func (d *Dao) CheckLabelRelationExist(id int64) (result bool, err error) {
  45. result = false
  46. lr := &model.LabelRelation{}
  47. err = d.DB.Table(model.LabelRelation{}.LabelRelationName()).Where("active = ?", 1).Where(" id = ?", id).First(lr).Error
  48. if lr.ID > 0 {
  49. result = true
  50. }
  51. return
  52. }
  53. // QueryLabelExist check label exist
  54. func (d *Dao) QueryLabelExist(lre *model.LabelRelation) (lr *model.LabelRelation, err error) {
  55. lr = &model.LabelRelation{}
  56. err = d.DB.Table(model.LabelRelation{}.LabelRelationName()).
  57. Where("active = ?", 1).
  58. Where(model.LabelRelation{Type: lre.Type, LabelID: lre.LabelID, TargetID: lre.TargetID}).First(lr).Error
  59. return
  60. }
  61. // DeleteLabelRelation delete relation of label
  62. func (d *Dao) DeleteLabelRelation(id int64) (err error) {
  63. return d.DB.Table(model.LabelRelation{}.LabelRelationName()).Where(" id = ?", id).Update("active", 0).Error
  64. }