user_merchant_countcode.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package user_merchant
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/common.in/utils"
  6. "gd_management/errors"
  7. "fmt"
  8. "github.com/astaxie/beego/orm"
  9. "strings"
  10. )
  11. func addAccessFailedCount(o orm.Ormer, accessCountId int64) error {
  12. sql := "update t_gd_api_access_count set fail_count=fail_count+1 where id=?"
  13. _, err := o.Raw(sql, accessCountId).Exec()
  14. if err != nil {
  15. setMysqlError(sql, fmt.Sprintf("%s", accessCountId), err)
  16. return errors.DataBaseError
  17. }
  18. return nil
  19. }
  20. func decreaseAccessCount(o orm.Ormer, accessCountId int64) error {
  21. count := 0
  22. sql := "select count from t_gd_api_access_count where id=?"
  23. err := o.Raw(sql, accessCountId).QueryRow(&count)
  24. if err != nil {
  25. setMysqlError(sql, fmt.Sprintf("%s", accessCountId), err)
  26. return errors.DataBaseError
  27. }
  28. if count == 0 {
  29. return nil
  30. }
  31. sql = "update t_gd_api_access_count set count=count-1 where id=?"
  32. _, err = o.Raw(sql, accessCountId).Exec()
  33. if err != nil {
  34. setMysqlError(sql, fmt.Sprintf("%s", accessCountId), err)
  35. return errors.DataBaseError
  36. }
  37. return nil
  38. }
  39. func ManagementCheckCountCode(ctx context.Context, req *apis.ManagementCheckCountCodeReq, reply *apis.ManagementCheckCountCodeReply) (err error) {
  40. var merchantDataApiId int64
  41. o := orm.NewOrm()
  42. sql := "select merchant_data_api_id from t_gd_api_access_count where id=?"
  43. err = o.Raw(sql, req.AccessCountId).QueryRow(&merchantDataApiId)
  44. if err != nil {
  45. setMysqlError(sql, fmt.Sprintf("%s", req.AccessCountId), err)
  46. return errors.DataBaseError
  47. }
  48. utils.Lock(fmt.Sprintf("%d", merchantDataApiId))
  49. defer utils.UnLock(fmt.Sprintf("%d", merchantDataApiId))
  50. if req.Code != "0" {
  51. err = addAccessFailedCount(o, req.AccessCountId)
  52. }
  53. if err != nil {
  54. return err
  55. }
  56. if req.CountType == 0 {
  57. return nil
  58. }
  59. array := strings.Split(req.CountCode, ",")
  60. for _, v := range array {
  61. if v == req.Code {
  62. return nil
  63. }
  64. }
  65. return decreaseAccessCount(o, req.AccessCountId)
  66. }