123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package data_api
- import (
- "context"
- "gd_management/apis"
- "gd_management/errors"
- "fmt"
- "strconv"
- "strings"
- "gd_management/common.in/utils"
- "github.com/astaxie/beego/orm"
- "go.uber.org/zap"
- )
- const (
- DATAAPIPAGESIZE = 10
- )
- func getDataApiSql(req *apis.ManagementDataApiGetAllReq) string {
- if req.Search != "" {
- req.Search = strings.TrimSpace(req.Search)
- }
- sql := "select t1.* from t_gd_data_api as t1"
- where := false
- if req.MerchantId > 0 {
- sql = fmt.Sprintf("select t1.* from t_gd_data_api as t1 left join t_gd_data_api_query_type as t2 on t1.id = t2.data_api_id left join t_gd_merchant_data_api as t3 on t3.query_type_id = t2.id where t3.merchant_id=%d", req.MerchantId)
- where = true
- }
- if req.Search != "" {
- search := `%search%`
- search = strings.Replace(search, "search", req.Search, -1)
- if where {
- sql = fmt.Sprintf("%s and t1.data_api_name like '%s'", sql, search)
- } else {
- sql = fmt.Sprintf("%s where t1.data_api_name like '%s'", sql, search)
- }
- where = true
- }
- if req.DataApiType != "" {
- dtype, err := strconv.Atoi(req.DataApiType)
- if err != nil {
- return sql
- }
- if dtype <= 0 {
- return sql
- }
- if where {
- sql = fmt.Sprintf("%s and t1.data_api_type=%d", sql, dtype)
- } else {
- sql = fmt.Sprintf("%s where t1.data_api_type=%d", sql, dtype)
- }
- }
- return sql
- }
- func DataApiGetAll(ctx context.Context, req *apis.ManagementDataApiGetAllReq, reply *apis.ManagementDataApiGetAllReply) error {
- /*defer func() {
- newApis := []apis.DataApi{}
- if req.DataApiType == "" {
- return
- }
- dtype, err := strconv.Atoi(req.DataApiType)
- if err != nil {
- return
- }
- for _, v := range reply.DataApis {
- if v.DataApiType == dtype {
- newApis = append(newApis, v)
- }
- }
- reply.DataApis = newApis
- }()*/
- o := orm.NewOrm()
- sql := getDataApiSql(req)
- if req.IsAll == true {
- sql = fmt.Sprintf("%s order by update_time desc", sql)
- _, err := o.Raw(sql).QueryRows(&reply.DataApis)
- if err != nil && err != orm.ErrNoRows {
- l.Error("mysql",
- zap.String("sql", "select t_gd_data_api"),
- zap.String("fields", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- return nil
- }
- /*sql := ""
- if req.DataApiType != "" {
- sql = fmt.Sprintf(`select count(id) from t_gd_data_api where data_api_name like "%?%" and data_api_type = %s`, req.DataApiType)
- } else {
- sql = `select count(id) from t_gd_data_api where data_api_name like "%?%"`
- }
- sql = strings.Replace(sql, "?", req.Search, -1)
- err := o.Raw(sql).QueryRow(&reply.Total)
- if err != nil && err != orm.ErrNoRows {
- l.Error("mysql",
- zap.String("sql", sql),
- zap.String("fields", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }*/
- if req.PageSize == 0 {
- req.PageSize = DATAAPIPAGESIZE
- }
- if req.PageNumber == 0 {
- req.PageNumber = 1
- }
- var querySeter orm.QuerySeter
- querySeter = o.QueryTable("t_gd_data_api").Filter("data_api_name__contains", req.Search)
- if req.DataApiType != "" {
- dtype, err := strconv.Atoi(req.DataApiType)
- if err != nil {
- return err
- }
- if dtype > 0 {
- querySeter = querySeter.Filter("data_api_type", dtype)
- }
- }
- reply.Total, _ = querySeter.Count()
- _, err := querySeter.OrderBy("-update_time").Limit(req.PageSize, (req.PageNumber-1)*req.PageSize).All(&reply.DataApis)
- if err != nil && err != orm.ErrNoRows {
- l.Error("mysql",
- zap.String("sql", "select t_gd_data_api"),
- zap.String("fields", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- reply.PageNumber = req.PageNumber
- reply.PageSize = req.PageSize
- return nil
- }
|