c_money.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. package clean
  2. import (
  3. "fmt"
  4. "github.com/shopspring/decimal"
  5. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  6. "math"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "unicode/utf8"
  11. )
  12. var (
  13. moneyReg1 = regexp.MustCompile("([\\s ,]+)")
  14. moneyReg2 = regexp.MustCompile("^([0-9.]+)E([1-7])$")
  15. numReg1 = regexp.MustCompile("([0-9\\.]+)")
  16. )
  17. var unpkvBidamountReg = regexp.MustCompile("^([Xx]\\+[1-9\\.]+元/每)")
  18. var specBidamountReg = regexp.MustCompile("^([0-9.]+)E([1-7])$")
  19. var regUnitMoneyClean = regexp.MustCompile("^(.*单价[0-9.]+元[/][袋|块])[,,](含税总价[0-9.]+[万元]+)[.。]$")
  20. var blackMoneyClean = regexp.MustCompile("^([0-9.]+以下[万]?|分)$")
  21. var impactMoneyClean = regexp.MustCompile("(分二串口|分站模块)")
  22. // 大写金额补充
  23. var impactMoneyeplenish = regexp.MustCompile("^([壹贰叁肆伍陆柒捌玖]分)")
  24. // 特殊金额-格式-重置
  25. var resetAamountReg = regexp.MustCompile("[.](0|00)[.](0|00)")
  26. var regPercentMoney, _ = regexp.Compile(`[0-9.]+[((]?[%|%][))]?`)
  27. var regQianw, _ = regexp.Compile(`\d{1,2}千万`)
  28. var kxjsReg = regexp.MustCompile("[0-9][E|e]{1}[-—+]{1}[0-9]{1,2}")
  29. var regOperator, _ = regexp.Compile(`[*|+|)*)]`)
  30. var regNumFloat, _ = regexp.Compile(`([1-9]\d*|0)(\.\d+)?`)
  31. var regStrUnit, _ = regexp.Compile(`[元|万|亿]`)
  32. var regStrJe = regexp.MustCompile(`([1-9]\d*|0)(\.\d_+)?[\s|元|万|亿]{0,3}`)
  33. var regStrChar = `[〇|零|点|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾|百|佰|千|仟|万|亿|億|元|圆|角|分|整|正]`
  34. var moneyRegChar, _ = regexp.Compile(regStrChar)
  35. var contentUnit, _ = regexp.Compile(`(万元|单位/万)`)
  36. var numCapitals, _ = regexp.Compile(`([〇|零|点|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾|百|佰|千|仟|万|亿|億|元|圆|角|分|整|正]{4,40})`)
  37. var moneyUnitRegBool = regexp.MustCompile(`(中标金额|成交金额|合同金额|中标价|成交价|成交价格|中标\(成交\)金额|投标报价|中标标价|成交结果)?[::\s]?(0|零|0.0|¥0)+(0|\.)*[\s]?(万|元|){0,2}[\s]?((人民币))?$`)
  38. var cutAllSpace, _ = regexp.Compile(`\s*`)
  39. var spaces = []string{"\u3000", "\u2003", "\u00a0", "\t", "\r", "\n", "\u0001"}
  40. var moneyClearSpidercode map[string]interface{}
  41. var moneyChar = map[string]interface{}{ //"〇": "0", "零": "0",壹贰叁肆伍陆柒捌玖
  42. "一": float64(1), "壹": float64(1), "二": float64(2), "贰": float64(2), "三": float64(3), "叁": float64(3), "四": float64(4), "肆": float64(4), "五": float64(5), "伍": float64(5),
  43. "六": float64(6), "陆": float64(6), "七": float64(7), "柒": float64(7), "八": float64(8), "捌": float64(8), "九": float64(9), "玖": float64(9), "十": float64(10), "拾": float64(10),
  44. "百": float64(100), "佰": float64(100), "千": float64(1000), "仟": float64(1000), "万": float64(10000), "亿": float64(100000000), "億": float64(100000000),
  45. "零": float64(0), "点": ".", "角": float64(0.1), "分": float64(0.01),
  46. }
  47. var NumChar = map[string]interface{}{
  48. "一": 1, "二": 1, "三": 1, "四": 1, "五": 1, "六": 1, "七": 1, "八": 1, "久": 1, "十": 1,
  49. }
  50. var moneyUnit = map[string]float64{
  51. "元": float64(1), "万": float64(10000), "亿": float64(100000000), "億": float64(100000000), //单位
  52. }
  53. func init() {
  54. regOperator, _ = regexp.Compile(`[*|+|)*)]`)
  55. regNumFloat, _ = regexp.Compile(`([1-9]\d*|0)(\.\d+)?`)
  56. regStrUnit, _ = regexp.Compile(`[元|万|亿]`)
  57. regStrJe = regexp.MustCompile(`([1-9]\d*|0)(\.\d_+)?[\s|元|万|亿]{0,3}`)
  58. regStrChar = `[〇|零|点|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾|百|佰|千|仟|万|亿|億|元|圆|角|分|整|正]`
  59. moneyRegChar, _ = regexp.Compile(regStrChar)
  60. contentUnit, _ = regexp.Compile(`(万元|单位/万)`)
  61. numCapitals, _ = regexp.Compile(`([〇|零|点|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾|百|佰|千|仟|万|亿|億|元|圆|角|分|整|正]{4,40})`)
  62. regQianw, _ = regexp.Compile(`\d{1,2}千万`)
  63. kxjsReg = regexp.MustCompile("[0-9][E|e]{1}[-—+]{1}[0-9]{1,2}")
  64. regPercentMoney, _ = regexp.Compile(`[0-9.]+[((]?[%|%][))]?`)
  65. }
  66. // 转换金额
  67. func ConvertMoney(money float64, unit string) float64 {
  68. if strings.Contains(unit, "万") && money > 0.0 {
  69. //倍率
  70. num1 := decimal.NewFromFloat(money)
  71. num2 := decimal.NewFromFloat(10000)
  72. decimalValue := num1.Mul(num2)
  73. res, _ := decimalValue.Float64()
  74. if res < 1000000000.0 {
  75. return res
  76. }
  77. }
  78. if strings.Contains(unit, "亿") && money > 0.0 {
  79. //倍率
  80. num1 := decimal.NewFromFloat(money)
  81. num2 := decimal.NewFromFloat(100000000)
  82. decimalValue := num1.Mul(num2)
  83. res, _ := decimalValue.Float64()
  84. if res < 1000000000.0 {
  85. return res
  86. }
  87. }
  88. return money
  89. }
  90. // 金额转换
  91. func CleanMoney(data []interface{}) (float64, bool) {
  92. isFindUnit := false
  93. tmpstr := (data)[0]
  94. totmpstr := ""
  95. if _, ok := tmpstr.(float64); ok {
  96. totmpstr = fmt.Sprintf("%f", tmpstr)
  97. } else {
  98. totmpstr = util.ObjToString(tmpstr)
  99. }
  100. //去除空格
  101. totmpstr = strings.ReplaceAll(totmpstr, " ", "")
  102. (data)[0] = totmpstr
  103. //特殊转换-科学计数法
  104. if specBidamountReg.MatchString(totmpstr) {
  105. price := util.Float64All(specBidamountReg.ReplaceAllString(totmpstr, "${1}"))
  106. if unit := util.Float64All(specBidamountReg.ReplaceAllString(totmpstr, "${2}")); unit > 0.0 && price > 0.0 {
  107. totmpstr = fmt.Sprintf("%f", math.Pow(10, unit)*price)
  108. (data)[0] = totmpstr
  109. }
  110. }
  111. //异常替换
  112. if unpkvBidamountReg.MatchString(totmpstr) {
  113. totmpstr = unpkvBidamountReg.ReplaceAllString(totmpstr, "")
  114. (data)[0] = totmpstr
  115. }
  116. if resetAamountReg.MatchString(totmpstr) {
  117. totmpstr = resetAamountReg.ReplaceAllString(totmpstr, ".0")
  118. (data)[0] = totmpstr
  119. }
  120. //单位指定
  121. if regUnitMoneyClean.MatchString(totmpstr) {
  122. totmpstr = regUnitMoneyClean.ReplaceAllString(totmpstr, "$2")
  123. (data)[0] = totmpstr
  124. }
  125. //特殊替换
  126. if impactMoneyClean.MatchString(totmpstr) {
  127. totmpstr = impactMoneyClean.ReplaceAllString(totmpstr, "")
  128. (data)[0] = totmpstr
  129. }
  130. //大写金额补充
  131. if impactMoneyeplenish.MatchString(totmpstr) {
  132. totmpstr = "零元" + totmpstr
  133. (data)[0] = totmpstr
  134. }
  135. //黑名单
  136. if blackMoneyClean.MatchString(totmpstr) {
  137. totmpstr = ""
  138. (data)[0] = totmpstr
  139. }
  140. //未含税总价1454400.00元,税率6%,含税总价1541664.00元
  141. Percent := regPercentMoney.FindAllString(totmpstr, -1)
  142. for _, v := range Percent {
  143. totmpstr = strings.ReplaceAll(totmpstr, v, "")
  144. }
  145. totmpstr = strings.ReplaceAll(totmpstr, "_", "")
  146. (data)[0] = totmpstr //过滤到%相关数字
  147. if utf8.RuneCountInString(totmpstr) > 100 { //过长-字符无有效金额
  148. (data)[0] = 0
  149. data = append(data, false)
  150. return 0.0, isFindUnit
  151. }
  152. if utf8.RuneCountInString(totmpstr) > 20 {
  153. if numCapitals.MatchString(totmpstr) {
  154. tmpstr = numCapitals.FindString(totmpstr)
  155. } else if regStrJe.MatchString(totmpstr) {
  156. tmpstr = regStrJe.FindString(totmpstr)
  157. } else {
  158. (data)[0] = 0
  159. data = append(data, false)
  160. return 0.0, isFindUnit
  161. }
  162. }
  163. //是否发现单位
  164. if strings.Contains(fmt.Sprint(data[0]), "万") || strings.Contains(fmt.Sprint(data[0]), "亿") {
  165. isFindUnit = true
  166. }
  167. ret := capitalMoney(data)[0]
  168. if ret.(float64) < float64(10000) || ret.(float64) > float64(50000000000) {
  169. ret2, _ := numMoney(data)
  170. //isfindUnit = b
  171. if ret2[0].(float64) > ret.(float64) {
  172. ret = ret2[0]
  173. }
  174. }
  175. f := util.Float64All(ret)
  176. //f, _ := strconv.ParseFloat(strconv.FormatFloat(ret.(float64), 'f', 4, 64), 64)
  177. //if f < 1 {
  178. // f = 0
  179. //}
  180. //若果金额小于50,全文检索单位:万
  181. // if f < 50 && f > 0 && isfindUnit {
  182. // rep := contentUnit.FindAllStringIndex(fmt.Sprint(data[1]), -1)
  183. // if len(rep) > 0 {
  184. // f = f * 10000
  185. // }
  186. // }
  187. data[0] = util.Float64All(ret)
  188. if f == 0 && !moneyUnitRegBool.MatchString(fmt.Sprint(tmpstr)) {
  189. data = append(data, false)
  190. return 0.0, isFindUnit
  191. }
  192. data = append(data, true)
  193. if len(data) > 0 {
  194. return util.Float64All(data[0]), isFindUnit
  195. } else {
  196. return 0.0, isFindUnit
  197. }
  198. }
  199. // 数字金额转换
  200. func numMoney(data []interface{}) ([]interface{}, bool) {
  201. tmp := fmt.Sprintf("%f", data[0])
  202. tmp = strings.ReplaceAll(tmp, "(不含税)", "")
  203. //费率转换% ‰
  204. flv := float64(1)
  205. if strings.HasSuffix(tmp, "%") {
  206. flv = 0.01
  207. } else if strings.HasSuffix(tmp, "‰") {
  208. flv = 0.001
  209. }
  210. repUnit := float64(1)
  211. if regQianw.MatchString(tmp) {
  212. tmp = strings.Replace(tmp, "千万", "万", -1)
  213. repUnit = float64(1000)
  214. }
  215. tmp = replaceSymbol(tmp, []string{",", ",", "(", ")", "(", ")", ":", "\n"})
  216. tmp = replaceString(tmp, []string{"万元", "亿元", "."}, []string{"万", "亿", "."})
  217. tmp = fmt.Sprint(CutAllSpace([]interface{}{tmp, data[1]})[0])
  218. rets := regNumFloat.FindAllString(tmp, -1)
  219. fnums := []float64{}
  220. unitstrs := []string{}
  221. if len(rets) > 0 {
  222. pindex := 0 //单位前置
  223. for k, v := range rets {
  224. f, err := strconv.ParseFloat(v, 64)
  225. if err == nil {
  226. fnums = append(fnums, f)
  227. index := strings.Index(tmp, v)
  228. //单位后置
  229. start := index + len(v)
  230. end := start + 3
  231. //log.Println("vvv", tmp, v, pindex, index, start)
  232. if k > 0 {
  233. if start >= pindex+3 {
  234. pstart := pindex + 3
  235. if pstart >= index {
  236. pstart = index
  237. }
  238. if len(tmp) > end {
  239. unitstrs = append(unitstrs, tmp[pstart:index]+tmp[start:end])
  240. } else {
  241. unitstrs = append(unitstrs, tmp[pstart:index]+tmp[start:])
  242. }
  243. } else {
  244. if len(tmp) > end {
  245. unitstrs = append(unitstrs, tmp[start:end])
  246. } else {
  247. unitstrs = append(unitstrs, tmp[start:])
  248. }
  249. }
  250. } else {
  251. if len(tmp) > end {
  252. if index-3 >= 0 {
  253. unitstrs = append(unitstrs, tmp[index-3:index]+tmp[start:end])
  254. } else {
  255. unitstrs = append(unitstrs, tmp[start:end])
  256. }
  257. } else {
  258. if index-3 >= 0 {
  259. unitstrs = append(unitstrs, tmp[index-3:index]+tmp[start:])
  260. } else {
  261. unitstrs = append(unitstrs, tmp[start:])
  262. }
  263. }
  264. }
  265. pindex = start
  266. }
  267. }
  268. }
  269. //log.Println("unitstrs", fnums, unitstrs)
  270. unit := float64(0)
  271. fnum := float64(0)
  272. for k, v := range fnums {
  273. fnum = v
  274. units := regStrUnit.FindAllString(unitstrs[k], -1)
  275. for _, v := range units {
  276. if moneyUnit[v] != 0 {
  277. unit = moneyUnit[v]
  278. break
  279. }
  280. }
  281. if unit != float64(0) { //取第一个
  282. break
  283. }
  284. }
  285. fnum = fnum * repUnit
  286. if unit == float64(0) {
  287. data[0] = fnum * flv
  288. } else {
  289. data[0] = fnum * unit * flv
  290. }
  291. if unit == 10000 {
  292. return data, false
  293. } else {
  294. return data, true
  295. }
  296. }
  297. // 大写数子金额转换
  298. func capitalMoney(data []interface{}) []interface{} {
  299. nodes := []float64{}
  300. node := float64(0)
  301. tmp := float64(0)
  302. decimals := 0.0
  303. ishaspoint := false //是否含小数点
  304. fnum := float64(0)
  305. end := false
  306. str := fmt.Sprint(data[0])
  307. //提取第一个大写信息
  308. if strings.Contains(str, "壹") {
  309. str = strings.ReplaceAll(str, "一", "壹")
  310. }
  311. strmatch := numCapitals.FindAllStringSubmatch(str, -1)
  312. if len(strmatch) > 0 {
  313. str = strmatch[0][0]
  314. }
  315. suffixUnit := float64(1)
  316. if strings.HasSuffix(str, "万") || strings.HasSuffix(str, "万元") || strings.HasSuffix(str, "万元整") {
  317. index := strings.LastIndex(str, "万")
  318. str = str[0:index]
  319. suffixUnit = float64(10000)
  320. }
  321. yy := false
  322. moneyRegChar.ReplaceAllStringFunc(str, func(key string) string {
  323. if key == "元" || key == "圆" || key == "点" {
  324. ishaspoint = true
  325. }
  326. if v, ok := moneyChar[key].(float64); ok && !end {
  327. if ishaspoint && v > 10 { //排除后面有其他的单位
  328. return ""
  329. }
  330. //fmt.Println(key, v, fnum)
  331. if v < 10 && v >= 0 {
  332. if ishaspoint { //小数部分
  333. if v >= 1 {
  334. fnum = v
  335. } else if v < 1 && v > 0 {
  336. decimals += fnum * v
  337. }
  338. } else {
  339. if tmp != float64(0) {
  340. node += tmp
  341. }
  342. tmp = float64(v)
  343. }
  344. } else if v == 10000 || v == 100000000 { //单位万、亿
  345. if tmp != float64(0) {
  346. node += tmp
  347. tmp = float64(0)
  348. }
  349. nodes = append(nodes, node*float64(v))
  350. if v == 100000000 {
  351. yy = true
  352. }
  353. node = float64(0)
  354. } else {
  355. if v == 10 && tmp == 0 {
  356. tmp = 1
  357. }
  358. tmp = tmp * float64(v)
  359. node += tmp
  360. tmp = float64(0)
  361. }
  362. }
  363. if key == "整" || key == "正" || key == "分" {
  364. end = true
  365. }
  366. return ""
  367. })
  368. if yy {
  369. nodes = append(nodes, node*suffixUnit, tmp)
  370. } else {
  371. nodes = append(nodes, node, tmp)
  372. }
  373. ret := float64(0)
  374. for _, v := range nodes {
  375. ret += v
  376. }
  377. if yy {
  378. return []interface{}{(ret + decimals), data[1]}
  379. } else {
  380. return []interface{}{(ret + decimals) * suffixUnit, data[1]}
  381. }
  382. }
  383. // 过滤符号
  384. func replaceSymbol(con string, rep []string) string {
  385. for _, v := range rep {
  386. con = strings.Replace(con, v, "", -1)
  387. }
  388. return con
  389. }
  390. // 符号替换
  391. func replaceString(con string, ret, rep []string) string {
  392. for k, v := range ret {
  393. if len(rep) > k {
  394. con = strings.Replace(con, v, rep[k], -1)
  395. }
  396. }
  397. return con
  398. }
  399. // 清理所有空白符
  400. func CutAllSpace(data []interface{}) []interface{} {
  401. tmp := cutAllSpace.ReplaceAllString(fmt.Sprint(data[0]), "")
  402. tmp = replaceSymbol(tmp, spaces)
  403. data[0] = tmp
  404. return data
  405. }