1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- 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.]+")
- var symbolReg = regexp.MustCompile("[%%﹪!!]")
- // 清洗时间
- func CleanTime(st string) int64 {
- if st == "" || 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 {
- str = fieldReg1.ReplaceAllString(str, "")
- if str == "" || str == "无" {
- return 0.0
- }
- 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 symbolReg.MatchString(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
- }
|