callLog.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package service
  2. import (
  3. "sfis/db"
  4. "sfis/model"
  5. "sfis/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func CallLog(appid, productId, startTime, endTime string, isSelf bool) ([]map[string]interface{}, error) {
  11. key := []string{}
  12. param := []interface{}{}
  13. datas := []map[string]interface{}{}
  14. if startTime != "" {
  15. key = append(key, "create_at >= ?")
  16. param = append(param, startTime+" 00:00:00")
  17. }
  18. if endTime != "" {
  19. key = append(key, "create_at <= ?")
  20. param = append(param, endTime+" 23:59:59")
  21. } else {
  22. key = append(key, "create_at <= ?")
  23. param = append(param, time.Now().Local().Format("2006-01-02 15:04:05"))
  24. }
  25. if productId != "" {
  26. productIds, _ := strconv.Atoi(productId)
  27. key = append(key, "product_id = ?")
  28. param = append(param, productIds)
  29. }
  30. whereSql := strings.Join(key, " and ")
  31. userCallRecord := []model.UserCallRecord{}
  32. err := db.GetSFISDB().Where(whereSql, param...).Where("app_id = ? ", appid).Order("id desc").Find(&userCallRecord).Error
  33. if err != nil {
  34. return nil, err
  35. } else {
  36. for _, v := range userCallRecord {
  37. productname := ""
  38. if product, ok := utils.ProductCaches.Map.Load(v.ProductId); ok {
  39. productname = product.(*model.Product).Name
  40. }
  41. if isSelf {
  42. interfaceOrder := model.InterfaceOrder{}
  43. err := db.GetSFISDB().First(&interfaceOrder, &model.InterfaceOrder{AppID: v.AppID, OrderCode: v.OrderCode}).Error
  44. if err == nil {
  45. dataMap := map[string]interface{}{
  46. "app_id": v.AppID,
  47. "invok_time": v.CreateAt.Local().Format("2006-01-02 15:04:05"),
  48. "ip": v.Ip,
  49. "name": productname,
  50. "param": v.Param,
  51. "order_code": v.OrderCode,
  52. "before": interfaceOrder.Before,
  53. "after": interfaceOrder.After,
  54. "cost_model": interfaceOrder.CostModel,
  55. "trade_num": interfaceOrder.TradeNum,
  56. }
  57. datas = append(datas, dataMap)
  58. }
  59. } else {
  60. dataMap := map[string]interface{}{
  61. "app_id": v.AppID,
  62. "invok_time": v.CreateAt.Local().Format("2006-01-02 15:04:05"),
  63. "ip": v.Ip,
  64. "name": productname,
  65. "param": v.Param,
  66. }
  67. datas = append(datas, dataMap)
  68. }
  69. }
  70. }
  71. return datas, nil
  72. }