db.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package appApi
  2. import (
  3. "MeterService/core/config"
  4. "MeterService/core/db"
  5. "strconv"
  6. )
  7. type DbAppApi struct {
  8. Db *db.MyDB
  9. }
  10. func (m *DbAppApi) GetList() ([]interface{}, error) {
  11. array := make([]interface{}, 0)
  12. arr, ok := m.Db.Query("SELECT api.app_id as AppId, api.type as Type, api.url as Url,app.host as Host FROM t_app_info AS app LEFT JOIN t_app_api_info AS api ON app.app_id = api.app_id WHERE app.is_delete = 0 AND api.app_id IS NOT NULL")
  13. if ok && arr != nil && len(arr) > 0 {
  14. for _, v := range arr {
  15. appId, _ := strconv.Atoi(v["AppId"])
  16. value := &AppApi{
  17. AppId: appId,
  18. Type: v["Type"],
  19. Host: v["Host"],
  20. Url: v["Url"],
  21. }
  22. array = append(array, value)
  23. }
  24. }
  25. return array, nil
  26. }
  27. func (m *DbAppApi) AddOrUpdate(v interface{}) error {
  28. d := v.(*App)
  29. if d.AppId == 0 {
  30. if _, err := m.Db.Exec("INSERT INTO t_app_info(app_secret,app_name, host,isDelete) VALUES(?,?,?,?)", d.AppSecret, d.AppName, d.Host, 0); err != nil {
  31. return err
  32. }
  33. } else {
  34. if _, err := m.Db.Exec("UPDATE t_app_info SET app_secret=?,app_name=?,host=?,isDelete=? WHERE app_id=?", d.AppSecret, d.AppName, d.Host, d.GetDelete(), d.AppId); err != nil {
  35. return err
  36. }
  37. if _, err := m.Db.Exec("DELETE FROM t_app_api_info WHERE app_id=?", d.AppId); err != nil {
  38. return err
  39. }
  40. }
  41. if len(d.Apis) > 0 {
  42. for _, v := range d.Apis {
  43. _, err := m.Db.Exec("INSERT INTO t_app_api_info(app_id, url, type) VALUES(?,?,?)", d.AppId, v.Url, v.Type)
  44. if err != nil {
  45. return err
  46. }
  47. }
  48. }
  49. return nil
  50. }
  51. func (m *DbAppApi) Delete(v interface{}) error {
  52. d := v.(*App)
  53. _, err := m.Db.Exec("UPDATE t_app_info SET is_delete=? WHERE app_id=?", 1, d.AppId)
  54. return err
  55. }
  56. func (m *DbAppApi) CheckSecret(d *App) bool {
  57. if config.C.Vber.Mode == "debug" && d.AppSecret == "" {
  58. return true
  59. }
  60. appId := d.AppId
  61. if arr, ok := m.Db.Query("SELECT app_id, app_secret as AppSecret FROM t_app_info WHERE app_id=? AND is_delete = 0", appId); !ok {
  62. return false
  63. } else {
  64. if len(arr) != 1 {
  65. return false
  66. }
  67. for _, v := range arr {
  68. if v["AppSecret"] == d.AppSecret {
  69. return true
  70. }
  71. }
  72. return false
  73. }
  74. }
  75. func NewAppApiDb() *DbAppApi {
  76. return &DbAppApi{
  77. Db: db.GetDb(),
  78. }
  79. }