callLog.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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) ([]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("create_at 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. dataMap := map[string]interface{}{
  42. "app_id": v.AppID,
  43. "invok_time": v.CreateAt.Local().Format("2006-01-02 15:04:05"),
  44. "ip": v.Ip,
  45. "name": productname,
  46. }
  47. datas = append(datas, dataMap)
  48. }
  49. }
  50. return datas, nil
  51. }