util.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package main
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/mongodb"
  5. "fmt"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. "time"
  8. )
  9. func TimeStrcount(start, end string) int64 {
  10. timeFormat := "2006-01-02 15:04:05" // 定义时间格式
  11. // 两个时间字符串
  12. // 解析时间字符串为 Time 对象
  13. startTime, err := time.Parse(timeFormat, start)
  14. if err != nil {
  15. fmt.Println("Error parsing start time:", err)
  16. return 0
  17. }
  18. endTime, err := time.Parse(timeFormat, end)
  19. if err != nil {
  20. fmt.Println("Error parsing end time:", err)
  21. return 0
  22. }
  23. // 计算时间差
  24. diff := endTime.Sub(startTime)
  25. // 获取相差的天数
  26. days := int64(diff.Hours() / 24)
  27. fmt.Printf("相差的天数: %d 天\n", days)
  28. return days
  29. }
  30. // 用户信息转uid查询
  31. func GetUidToUserId(userId string, phone string) (string, string) {
  32. uId := ""
  33. query := map[string]interface{}{}
  34. if userId == "" {
  35. query["phone"] = phone
  36. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  37. if userInfo != nil && len(*userInfo) > 0 {
  38. uId = gconv.String((*userInfo)["uid"])
  39. userId = gconv.String((*userInfo)["userid"])
  40. }
  41. } else {
  42. if !mongodb.IsObjectIdHex(userId) {
  43. userMapping := TiDb.FindOne("data_service.user_system", map[string]interface{}{"position_id": userId}, "", "")
  44. if userMapping != nil && len(*userMapping) > 0 {
  45. userId = common.ObjToString((*userMapping)["userid"])
  46. } else {
  47. return uId, userId
  48. }
  49. }
  50. query["userid"] = userId
  51. userInfo := TiDb.FindOne("dwd_f_userbase_baseinfo", query, "", "")
  52. if userInfo != nil && len(*userInfo) > 0 {
  53. uId = common.ObjToString((*userInfo)["uid"])
  54. } else {
  55. return uId, userId
  56. }
  57. }
  58. return uId, userId
  59. }
  60. func getUserIdToUid(uid string) []string {
  61. personData := TiDb.SelectBySql(`SELECT
  62. IF(type=0,userid,position_id) as positionId
  63. FROM
  64. data_service.user_system
  65. WHERE
  66. uid = ? group by positionId `, uid)
  67. if personData == nil || len(*personData) == 0 {
  68. return []string{}
  69. }
  70. var personArr []string
  71. for _, v := range *personData {
  72. personArr = append(personArr, fmt.Sprintf(`"%s"`,
  73. gconv.String(v["positionId"])))
  74. }
  75. return personArr
  76. }