config.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package service
  2. import (
  3. "context"
  4. "math"
  5. "time"
  6. "go-common/app/admin/main/reply/model"
  7. "go-common/library/log"
  8. )
  9. // AddReplyConfig create a new administrator configuration for reply business
  10. func (s *Service) AddReplyConfig(c context.Context, m *model.Config) (id int64, err error) {
  11. sub, err := s.subject(c, m.Oid, m.Type)
  12. if err != nil {
  13. return
  14. }
  15. now := time.Now()
  16. if _, err = s.dao.AddConfig(c, m.Type, m.Category, m.Oid, m.AdminID, m.Operator, m.Config, now); err != nil {
  17. return
  18. }
  19. if m.ShowEntry == 1 && m.ShowAdmin == 1 {
  20. sub.AttrSet(model.AttrNo, model.SubAttrConfig)
  21. } else {
  22. sub.AttrSet(model.AttrYes, model.SubAttrConfig)
  23. }
  24. if _, err = s.dao.UpSubjectAttr(c, m.Oid, m.Type, sub.Attr, now); err != nil {
  25. log.Error("s.dao.UpSubjectAttr(%d,%d,%d,%d) error(%v)", m.Type, m.Oid, model.SubAttrConfig, m.ShowEntry, err)
  26. return
  27. }
  28. if err = s.dao.DelSubjectCache(c, m.Oid, m.Type); err != nil {
  29. log.Error("ReplyConfig del subject cache error(%v)", err)
  30. }
  31. if err = s.dao.DelConfigCache(c, m.Oid, m.Type, m.Category); err != nil {
  32. log.Error("ReplyConfig del config cache error(%v)", err)
  33. }
  34. return
  35. }
  36. // LoadReplyConfig load a configuration record of reply business.
  37. func (s *Service) LoadReplyConfig(c context.Context, typ, category int32, oid int64) (m *model.Config, err error) {
  38. m, err = s.dao.LoadConfig(c, typ, category, oid)
  39. return
  40. }
  41. //PaginateReplyConfig paginate configuration list of records indexing from start to end, and a total count of records
  42. func (s *Service) PaginateReplyConfig(c context.Context, typ, category int32, oid int64, operator string, offset, count int) (configs []*model.Config, totalCount, pages int64, err error) {
  43. configs, _ = s.dao.PaginateConfig(c, typ, category, oid, operator, offset, count)
  44. totalCount, _ = s.dao.PaginateConfigCount(c, typ, category, oid, operator)
  45. pages = int64(math.Ceil(float64(totalCount) / float64(count)))
  46. return
  47. }
  48. //RenewReplyConfig reset reply configuration by default, with deleting the detail configurations from db
  49. func (s *Service) RenewReplyConfig(c context.Context, id int64) (result bool, err error) {
  50. now := time.Now()
  51. config, err := s.dao.LoadConfigByID(c, id)
  52. if err != nil {
  53. log.Error("s.dao.LoadConfigByID(%d) error(%v)", id, err)
  54. }
  55. if config == nil {
  56. return false, nil
  57. }
  58. sub, err := s.dao.Subject(c, config.Oid, config.Type)
  59. if err != nil {
  60. return
  61. }
  62. sub.AttrSet(model.AttrNo, model.SubAttrConfig)
  63. _, err = s.dao.UpSubjectAttr(c, config.Oid, config.Type, sub.Attr, now)
  64. if err != nil {
  65. log.Error("s.dao.UpSubjectAttr(%d,%d,%d,%d) error(%v)", config.Type, config.Oid, model.SubAttrConfig, config.ShowEntry, err)
  66. return
  67. }
  68. if _, err = s.dao.DeleteConfig(c, id); err != nil {
  69. log.Error("s.dao.DeleteConfig(%d) error(%v)", id, err)
  70. return
  71. }
  72. if err = s.dao.DelSubjectCache(c, config.Oid, config.Type); err != nil {
  73. log.Error("ReplyConfig del subject cache error(%v)", err)
  74. }
  75. if err = s.dao.DelConfigCache(c, config.Oid, config.Type, config.Category); err != nil {
  76. log.Error("ReplyConfig del config cache error(%v)", err)
  77. }
  78. result = true
  79. return
  80. }