// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package accounting import ( "context" "gd_management/apis" "gd_management/errors" "fmt" "strings" "gd_management/common.in/utils" "github.com/astaxie/beego/orm" "go.uber.org/zap" ) func MerchantPriceList(ctx context.Context, req *apis.MerchantPriceListReq, reply *apis.MerchantPriceListReply) error { //select id,merchant_name,balance,merchant_type from t_gd_merchants where auth_status=1 if req.PageNumber > 0 { req.PageNumber -= 1 } else { req.PageNumber = 0 } o := orm.NewOrm() sql := "select id as merchant_id,merchant_name,balance,merchant_type,arrearage,is_send_notify as is_notify from t_gd_merchants where auth_status=1" where := "" if req.MerchantId != 0 { where = fmt.Sprintf("%s and id=%d", where, req.MerchantId) } if req.MerchantTypeList != "" { if !strings.Contains(req.MerchantTypeList ,"0"){ if strings.Contains(req.MerchantTypeList,"4"){ req.MerchantTypeList = strings.Replace(req.MerchantTypeList,"4","2,3",-1) } where = fmt.Sprintf("%s and merchant_type in (%s)", where, req.MerchantTypeList) } } sql = sql + where if req.ArrearageOrder != 0 { if req.ArrearageOrder == 1{ sql = fmt.Sprintf("%s order by balance asc", sql) }else if req.ArrearageOrder == 2{ sql = fmt.Sprintf("%s order by balance desc", sql) } }else{ sql = fmt.Sprintf("%s order by id desc", sql) } sql = sql + " LIMIT ? OFFSET ?" countSql := `select count(1) from t_gd_merchants where auth_status=1` + where err := o.Raw(countSql).QueryRow(&reply.Total) if err != nil { l.Error("func", zap.String("call", "MerchantPriceList"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } if reply.Total == 0 { reply.PageSize = PAGESIZE return nil } _, err = o.Raw(sql, PAGESIZE, req.PageNumber*PAGESIZE).QueryRows(&reply.List) if err != nil { l.Error("func", zap.String("call", "MerchantPriceList"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } var merchantIdList string for _,v := range reply.List{ if merchantIdList == "" { merchantIdList = fmt.Sprintf("%d",v.MerchantId) }else{ merchantIdList = fmt.Sprintf("%s,%d",merchantIdList,v.MerchantId) } } merchantDataApiPriceList := []apis.MerchantDataApiPrice{} merchantDataApiPriceMap := make(map[int64][]apis.MerchantDataApiPrice) sql = fmt.Sprintf("select t1.id as merchant_data_api_id,t1.unit_price,t1.merchant_id,t1.alias,t3.data_api_name as merchant_data_api_name from t_gd_merchant_data_api as t1 left join t_gd_data_api_query_type as t2 on t1.query_type_id=t2.id left join t_gd_data_api as t3 on t3.id=t2.data_api_id where t1.merchant_id in (%s)",merchantIdList) _, err = o.Raw(sql).QueryRows(&merchantDataApiPriceList) if err != nil { l.Error("func", zap.String("call", "MerchantPriceList"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } for index, _ := range merchantDataApiPriceList { if merchantDataApiPriceList[index].Alias != "" { merchantDataApiPriceList[index].MerchantDataApiName = merchantDataApiPriceList[index].Alias } if value,ok := merchantDataApiPriceMap[merchantDataApiPriceList[index].MerchantId];ok{ value = append(value,merchantDataApiPriceList[index]) merchantDataApiPriceMap[merchantDataApiPriceList[index].MerchantId] = value }else{ tmp := []apis.MerchantDataApiPrice{} tmp = append(tmp,merchantDataApiPriceList[index]) merchantDataApiPriceMap[merchantDataApiPriceList[index].MerchantId] = tmp } } for index,_ := range reply.List{ if v, ok := merchantDataApiPriceMap[reply.List[index].MerchantId];ok{ reply.List[index].ApiPriceList = v } } reply.PageSize = PAGESIZE reply.PageNumber = req.PageNumber + 1 return nil }