123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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("[%%﹪!!]")
- var YMD_Reg1 = regexp.MustCompile("(\\d{4}年\\d{1,2}月\\d{1,2}日)")
- var YMD_Reg2 = regexp.MustCompile("(\\d{4}[-]\\d{1,2}[-]\\d{1,2})")
- var HMS_Reg1 = regexp.MustCompile("(\\d{1,2}时\\d{1,2}分\\d{1,2}秒)")
- var HMS_Reg2 = regexp.MustCompile("(\\d{1,2}[:]\\d{1,2}[:]\\d{1,2})")
- func convertYMD(ymd string) string {
- if ymd1 := YMD_Reg1.FindString(ymd); ymd1 != "" {
- }
- return ""
- }
- func convertHMS(hms string) string {
- if hms1 := YMD_Reg1.FindString(hms); hms1 != "" {
- }
- return ""
- }
- // 清洗时间
- func CleanTime(st string) int64 {
- if st == "" || st == "无" {
- return 0
- }
- //YYYY-MM-DD HH:MM:SS
- /*
- 2024-02-28 09:00:00
- 2024年3月4日 08:00:00
- 2024年03月01日08时00分00秒
- 2024年3月8日 17:00:00
- 2024年03月08日09时30分00秒
- */
- st = strings.ReplaceAll(st, ":", ":")
- ymd, hms := convertYMD(st), convertHMS(st)
- st = ymd + " " + hms
- 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
- }
|