merchant_price_list.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package accounting
  4. import (
  5. "context"
  6. "gd_management/apis"
  7. "gd_management/errors"
  8. "fmt"
  9. "strings"
  10. "gd_management/common.in/utils"
  11. "github.com/astaxie/beego/orm"
  12. "go.uber.org/zap"
  13. )
  14. func MerchantPriceList(ctx context.Context, req *apis.MerchantPriceListReq, reply *apis.MerchantPriceListReply) error {
  15. //select id,merchant_name,balance,merchant_type from t_gd_merchants where auth_status=1
  16. if req.PageNumber > 0 {
  17. req.PageNumber -= 1
  18. } else {
  19. req.PageNumber = 0
  20. }
  21. o := orm.NewOrm()
  22. 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"
  23. where := ""
  24. if req.MerchantId != 0 {
  25. where = fmt.Sprintf("%s and id=%d", where, req.MerchantId)
  26. }
  27. if req.MerchantTypeList != "" {
  28. if !strings.Contains(req.MerchantTypeList ,"0"){
  29. if strings.Contains(req.MerchantTypeList,"4"){
  30. req.MerchantTypeList = strings.Replace(req.MerchantTypeList,"4","2,3",-1)
  31. }
  32. where = fmt.Sprintf("%s and merchant_type in (%s)", where, req.MerchantTypeList)
  33. }
  34. }
  35. sql = sql + where
  36. if req.ArrearageOrder != 0 {
  37. if req.ArrearageOrder == 1{
  38. sql = fmt.Sprintf("%s order by balance asc", sql)
  39. }else if req.ArrearageOrder == 2{
  40. sql = fmt.Sprintf("%s order by balance desc", sql)
  41. }
  42. }else{
  43. sql = fmt.Sprintf("%s order by id desc", sql)
  44. }
  45. sql = sql + " LIMIT ? OFFSET ?"
  46. countSql := `select count(1) from t_gd_merchants where auth_status=1` + where
  47. err := o.Raw(countSql).QueryRow(&reply.Total)
  48. if err != nil {
  49. l.Error("func",
  50. zap.String("call", "MerchantPriceList"),
  51. zap.String("args", utils.MarshalJsonString(req)),
  52. zap.String("error", err.Error()))
  53. return errors.DataBaseError
  54. }
  55. if reply.Total == 0 {
  56. reply.PageSize = PAGESIZE
  57. return nil
  58. }
  59. _, err = o.Raw(sql, PAGESIZE, req.PageNumber*PAGESIZE).QueryRows(&reply.List)
  60. if err != nil {
  61. l.Error("func",
  62. zap.String("call", "MerchantPriceList"),
  63. zap.String("args", utils.MarshalJsonString(req)),
  64. zap.String("error", err.Error()))
  65. return errors.DataBaseError
  66. }
  67. var merchantIdList string
  68. for _,v := range reply.List{
  69. if merchantIdList == "" {
  70. merchantIdList = fmt.Sprintf("%d",v.MerchantId)
  71. }else{
  72. merchantIdList = fmt.Sprintf("%s,%d",merchantIdList,v.MerchantId)
  73. }
  74. }
  75. merchantDataApiPriceList := []apis.MerchantDataApiPrice{}
  76. merchantDataApiPriceMap := make(map[int64][]apis.MerchantDataApiPrice)
  77. 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)
  78. _, err = o.Raw(sql).QueryRows(&merchantDataApiPriceList)
  79. if err != nil {
  80. l.Error("func",
  81. zap.String("call", "MerchantPriceList"),
  82. zap.String("args", utils.MarshalJsonString(req)),
  83. zap.String("error", err.Error()))
  84. return errors.DataBaseError
  85. }
  86. for index, _ := range merchantDataApiPriceList {
  87. if merchantDataApiPriceList[index].Alias != "" {
  88. merchantDataApiPriceList[index].MerchantDataApiName = merchantDataApiPriceList[index].Alias
  89. }
  90. if value,ok := merchantDataApiPriceMap[merchantDataApiPriceList[index].MerchantId];ok{
  91. value = append(value,merchantDataApiPriceList[index])
  92. merchantDataApiPriceMap[merchantDataApiPriceList[index].MerchantId] = value
  93. }else{
  94. tmp := []apis.MerchantDataApiPrice{}
  95. tmp = append(tmp,merchantDataApiPriceList[index])
  96. merchantDataApiPriceMap[merchantDataApiPriceList[index].MerchantId] = tmp
  97. }
  98. }
  99. for index,_ := range reply.List{
  100. if v, ok := merchantDataApiPriceMap[reply.List[index].MerchantId];ok{
  101. reply.List[index].ApiPriceList = v
  102. }
  103. }
  104. reply.PageSize = PAGESIZE
  105. reply.PageNumber = req.PageNumber + 1
  106. return nil
  107. }