package clean import ( "data_ai/ul" "github.com/shopspring/decimal" qu "jygit.jydev.jianyu360.cn/data_processing/common_utils" "regexp" "strings" "time" ) var numReg = regexp.MustCompile("[0-9.]+") // 清洗时间 func CleanTime(st string) int64 { if st == "" { return 0 } //YYYY-MM-DD HH:MM:SS t, _ := time.ParseInLocation(ul.TimeLayout, st, time.Local) return t.Unix() } // 清洗折扣率 func CleanDiscount(str string) float64 { /* 上浮率:20% 下浮率:20% 折扣率:20% */ if biddiscount := RateToFloat(str); biddiscount > 0.0 { baseCount := 1.0 num1 := decimal.NewFromFloat(baseCount) num2 := decimal.NewFromFloat(biddiscount) if strings.Contains(str, "上浮") { decimalValue := num1.Add(num2) res, _ := decimalValue.Float64() return res } else if strings.Contains(str, "下浮") { decimalValue := num1.Sub(num2) res, _ := decimalValue.Float64() return res } else { return biddiscount } } return 0.0 } // 转换系数 func RateToFloat(str string) float64 { if num0 := qu.Float64All(numReg.FindString(str)); num0 > 0.0 { num1 := decimal.NewFromFloat(100.0) num2 := decimal.NewFromFloat(num0) if strings.Contains(str, "%") || strings.Contains(str, "%") { decimalValue := num2.Div(num1) res, _ := decimalValue.Float64() if res < 1.0 { return res } else { return 0.0 } } else { if num0 < 1.0 { return num0 } else if num0 == 1 { return 0.0 } else { decimalValue := num2.Div(num1) res, _ := decimalValue.Float64() if res < 1.0 { return res } else { return 0.0 } } } } return 0.0 }