package base_api import ( "context" "gd_management/apis" "gd_management/errors" "fmt" "strings" "gd_management/common.in/utils" "github.com/astaxie/beego/orm" "go.uber.org/zap" ) var PAGESIZE = 10 func getBaseApiType(typeInt int) string { if typeInt == 0 { return "数据" } if typeInt == 1 { return "h5" } return "辅助" } func getMerchantApiIds(o orm.Ormer, req *apis.ManagementGetBaseApiListReq) ([]int64, error) { ids := []int64{} sql := fmt.Sprintf("select api_id from t_gd_merchant_child_data_api as t1 left join t_gd_merchant_data_api as t2 on t1.merchant_data_api_id=t2.id where t2.merchant_id=%d", req.MerchantId) _, err := o.Raw(sql).QueryRows(&ids) if err != nil && err != orm.ErrNoRows { return nil, errors.DataBaseError } return ids, nil } func ManagementGetBaseApiList(ctx context.Context, req *apis.ManagementGetBaseApiListReq, reply *apis.ManagementGetBaseApiListReply) (err error) { o := orm.NewOrm() list := []apis.TGdApi{} apiIds := []interface{}{} if req.MerchantId != 0 && req.IsAll { ids, err := getMerchantApiIds(o, req) if err != nil { return errors.DataBaseError } if len(ids) == 0 { return nil } apiIds = make([]interface{}, len(ids)) for i, _ := range ids { apiIds[i] = ids[i] } } if req.IsAll == true { if len(apiIds) == 0 { _, err = o.QueryTable("t_gd_api").Filter("name__contains", req.Search).OrderBy("-update_time").All(&list) } else { _, err = o.QueryTable("t_gd_api").Filter("name__contains", req.Search).Filter("id__in", apiIds...).OrderBy("-update_time").All(&list) } if err != nil && err != orm.ErrNoRows { l.Error("func", zap.String("call", "ManagementGetBaseApiList"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } for _, v := range list { iterm := apis.ManagementBaseApiListItem{} iterm.ApiId = v.Id iterm.Enable = v.Enable iterm.IsShow = v.IsShow iterm.Name = v.Name iterm.Type = getBaseApiType(v.ApiType) iterm.Router = v.Router iterm.OperationTime = v.UpdateTime iterm.Method = v.Method iterm.RequestParam = v.RequestParam iterm.ResponseParam = v.ResponseParam iterm.ThresholdTimeout = v.ThresholdTimeout iterm.ThresholdFailRate = v.ThresholdFailRate iterm.ThresholdNorecordRate = v.ThresholdNorecordRate iterm.WarningEnable = v.WarningEnable iterm.RateLimit = v.RateLimit reply.BaseApiItemList = append(reply.BaseApiItemList, iterm) } return nil } if req.PageNumber <= 0 { req.PageNumber = 1 } if req.Search != "" { req.Search = strings.TrimSpace(req.Search) } sql := `select count(id) from t_gd_api where name like binary "%+%" or router like binary "%+%" and api_type=?` sql = strings.Replace(sql, "+", req.Search, -1) err = o.Raw(sql, req.ApiType).QueryRow(&reply.Total) if err != nil && err != orm.ErrNoRows { l.Error("mysql", zap.String("sql", sql), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } if err == orm.ErrNoRows { return nil } querySet := o.QueryTable("t_gd_api").Filter("api_type", req.ApiType) cond := orm.NewCondition().AndCond(orm.NewCondition().And("name__contains", req.Search).Or("router__contains", req.Search)) querySet = querySet.SetCond(cond) _, err = querySet.OrderBy("-update_time").Limit(PAGESIZE, (req.PageNumber-1)*PAGESIZE).All(&list) //_, err = o.QueryTable("t_gd_api").Filter("name__contains", req.Search).Filter("api_type", req.ApiType).OrderBy("-update_time").Limit(PAGESIZE, (req.PageNumber-1)*PAGESIZE).All(&list) if err != nil && err != orm.ErrNoRows { l.Error("func", zap.String("call", "ManagementGetBaseApiList"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } if err != nil && err == orm.ErrNoRows { return nil } for _, v := range list { iterm := apis.ManagementBaseApiListItem{} iterm.ApiId = v.Id iterm.Enable = v.Enable iterm.IsShow = v.IsShow iterm.Name = v.Name iterm.Type = getBaseApiType(v.ApiType) iterm.Router = v.Router iterm.OperationTime = v.UpdateTime iterm.ThresholdTimeout = v.ThresholdTimeout iterm.ThresholdFailRate = v.ThresholdFailRate iterm.ThresholdNorecordRate = v.ThresholdNorecordRate iterm.WarningEnable = v.WarningEnable iterm.RateLimit = v.RateLimit reply.BaseApiItemList = append(reply.BaseApiItemList, iterm) } reply.PageSize = 10 reply.PageNumber = req.PageNumber l.Debug(utils.MarshalJsonString(req, reply)) return }