c_time.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package clean
  2. import (
  3. "data_ai/ul"
  4. "github.com/shopspring/decimal"
  5. qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  6. "regexp"
  7. "strings"
  8. "time"
  9. )
  10. var numReg = regexp.MustCompile("[0-9.]+")
  11. var symbolReg = regexp.MustCompile("[%%﹪!!]")
  12. // 清洗时间
  13. func CleanTime(st string) int64 {
  14. if st == "" || st == "无" {
  15. return 0
  16. }
  17. //YYYY-MM-DD HH:MM:SS
  18. t, _ := time.ParseInLocation(ul.TimeLayout, st, time.Local)
  19. return t.Unix()
  20. }
  21. // 清洗折扣率
  22. func CleanDiscount(str string) float64 {
  23. str = fieldReg1.ReplaceAllString(str, "")
  24. if str == "" || str == "无" {
  25. return 0.0
  26. }
  27. if biddiscount := RateToFloat(str); biddiscount > 0.0 {
  28. baseCount := 1.0
  29. num1 := decimal.NewFromFloat(baseCount)
  30. num2 := decimal.NewFromFloat(biddiscount)
  31. if strings.Contains(str, "上浮") {
  32. decimalValue := num1.Add(num2)
  33. res, _ := decimalValue.Float64()
  34. return res
  35. } else if strings.Contains(str, "下浮") {
  36. decimalValue := num1.Sub(num2)
  37. res, _ := decimalValue.Float64()
  38. return res
  39. } else {
  40. return biddiscount
  41. }
  42. }
  43. return 0.0
  44. }
  45. // 转换系数
  46. func RateToFloat(str string) float64 {
  47. if num0 := qu.Float64All(numReg.FindString(str)); num0 > 0.0 {
  48. num1 := decimal.NewFromFloat(100.0)
  49. num2 := decimal.NewFromFloat(num0)
  50. if symbolReg.MatchString(str) {
  51. decimalValue := num2.Div(num1)
  52. res, _ := decimalValue.Float64()
  53. if res < 1.0 {
  54. return res
  55. } else {
  56. return 0.0
  57. }
  58. } else {
  59. if num0 < 1.0 {
  60. return num0
  61. } else if num0 == 1 {
  62. return 0.0
  63. } else {
  64. decimalValue := num2.Div(num1)
  65. res, _ := decimalValue.Float64()
  66. if res < 1.0 {
  67. return res
  68. } else {
  69. return 0.0
  70. }
  71. }
  72. }
  73. }
  74. return 0.0
  75. }