1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package user_merchant
- import (
- "context"
- "gd_management/apis"
- "gd_management/common.in/utils"
- "gd_management/errors"
- "fmt"
- "github.com/astaxie/beego/orm"
- "strings"
- )
- func addAccessFailedCount(o orm.Ormer, accessCountId int64) error {
- sql := "update t_gd_api_access_count set fail_count=fail_count+1 where id=?"
- _, err := o.Raw(sql, accessCountId).Exec()
- if err != nil {
- setMysqlError(sql, fmt.Sprintf("%s", accessCountId), err)
- return errors.DataBaseError
- }
- return nil
- }
- func decreaseAccessCount(o orm.Ormer, accessCountId int64) error {
- count := 0
- sql := "select count from t_gd_api_access_count where id=?"
- err := o.Raw(sql, accessCountId).QueryRow(&count)
- if err != nil {
- setMysqlError(sql, fmt.Sprintf("%s", accessCountId), err)
- return errors.DataBaseError
- }
- if count == 0 {
- return nil
- }
- sql = "update t_gd_api_access_count set count=count-1 where id=?"
- _, err = o.Raw(sql, accessCountId).Exec()
- if err != nil {
- setMysqlError(sql, fmt.Sprintf("%s", accessCountId), err)
- return errors.DataBaseError
- }
- return nil
- }
- func ManagementCheckCountCode(ctx context.Context, req *apis.ManagementCheckCountCodeReq, reply *apis.ManagementCheckCountCodeReply) (err error) {
- var merchantDataApiId int64
- o := orm.NewOrm()
- sql := "select merchant_data_api_id from t_gd_api_access_count where id=?"
- err = o.Raw(sql, req.AccessCountId).QueryRow(&merchantDataApiId)
- if err != nil {
- setMysqlError(sql, fmt.Sprintf("%s", req.AccessCountId), err)
- return errors.DataBaseError
- }
- utils.Lock(fmt.Sprintf("%d", merchantDataApiId))
- defer utils.UnLock(fmt.Sprintf("%d", merchantDataApiId))
- if req.Code != "0" {
- err = addAccessFailedCount(o, req.AccessCountId)
- }
- if err != nil {
- return err
- }
- if req.CountType == 0 {
- return nil
- }
- array := strings.Split(req.CountCode, ",")
- for _, v := range array {
- if v == req.Code {
- return nil
- }
- }
- return decreaseAccessCount(o, req.AccessCountId)
- }
|