database.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package action
  2. import (
  3. "crm-api/internal/app/ami"
  4. "crm-api/internal/app/redis"
  5. "crm-api/pkg/utils"
  6. "errors"
  7. "fmt"
  8. )
  9. func DBPut(family, key, value string) (err error) {
  10. action := map[string]string{
  11. "Action": "DBPut",
  12. "Family": family,
  13. "Key": key,
  14. "Val": value,
  15. }
  16. res, _, err := ami.AminInstance.Send(action)
  17. if err != nil {
  18. return err
  19. }
  20. if res["Response"] != "Success" {
  21. return errors.New(res["Message"])
  22. }
  23. if family == "DND" {
  24. redis.ExtensionDNDSet(key, value)
  25. _, _, _ = utils.ExecCmdAsync("asterisk", "-rx", fmt.Sprintf("presencestate change CustomPresence:%s dnd", key))
  26. _, _, _ = utils.ExecCmdAsync("asterisk", "-rx", fmt.Sprintf("pjsip send notify dndon endpoint %s", key))
  27. }
  28. return nil
  29. }
  30. func DBDel(family, key string) error {
  31. action := map[string]string{
  32. "Action": "DBDel",
  33. "Family": family,
  34. "Key": key,
  35. }
  36. res, _, err := ami.AminInstance.Send(action)
  37. if err != nil {
  38. return err
  39. }
  40. if res["Response"] != "Success" {
  41. return errors.New(res["Message"])
  42. }
  43. if family == "DND" {
  44. redis.ExtensionDNDDel(key)
  45. _, _, _ = utils.ExecCmdAsync("asterisk", "-rx", fmt.Sprintf("presencestate change CustomPresence:%s XA", key))
  46. _, _, _ = utils.ExecCmdAsync("asterisk", "-rx", fmt.Sprintf("pjsip send notify dndoff endpoint %s", key))
  47. }
  48. return nil
  49. }
  50. func DBGet(family, key string) (value string, err error) {
  51. action := map[string]string{
  52. "Action": "DBGet",
  53. "Family": family,
  54. "Key": key,
  55. }
  56. res, events, err := ami.AminInstance.Send(action)
  57. if err != nil {
  58. return "", err
  59. }
  60. if res["Response"] != "Success" {
  61. return "", errors.New(res["Message"])
  62. }
  63. for _, v := range events {
  64. if v.Data["Event"] == "DBGetResponse" {
  65. return v.Data["Val"], nil
  66. }
  67. }
  68. return "", nil
  69. }