F01.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. package query
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "gd_adm_data/apis"
  7. "gd_adm_data/errors"
  8. "gd_adm_data/utils"
  9. "strconv"
  10. "strings"
  11. "time"
  12. jsoniter "github.com/json-iterator/go"
  13. )
  14. // 计算最后投保日期
  15. func calcLatestInsuredate(reqMap map[string]interface{}) {
  16. retLen := utils.MonthLen
  17. firstInsuranceDate := ""
  18. lastCompulsoryInsuranceDate := ""
  19. if v, ok := reqMap["first_insurance_date"]; ok {
  20. firstInsuranceDate = v.(string)
  21. }
  22. if v, ok := reqMap["last_compulsory_insurance_date"]; ok {
  23. lastCompulsoryInsuranceDate = v.(string)
  24. }
  25. if firstInsuranceDate == "" && lastCompulsoryInsuranceDate == "" {
  26. return
  27. }
  28. latestInsuranceDateStart := ""
  29. lastCompulsoryInsuranceDate = strings.TrimSpace(lastCompulsoryInsuranceDate)
  30. firstInsuranceDate = strings.TrimSpace(firstInsuranceDate)
  31. if lastCompulsoryInsuranceDate == "" {
  32. return
  33. } else if firstInsuranceDate == "" {
  34. latestInsuranceDateStart = lastCompulsoryInsuranceDate
  35. } else if len(lastCompulsoryInsuranceDate) < utils.MonthLen ||
  36. len(firstInsuranceDate) < utils.MonthLen {
  37. return
  38. } else if lastCompulsoryInsuranceDate[utils.MonthLen-2:utils.MonthLen] == firstInsuranceDate[utils.MonthLen-2:utils.MonthLen] {
  39. if len(firstInsuranceDate) == utils.DataLen {
  40. latestInsuranceDateStart = lastCompulsoryInsuranceDate + firstInsuranceDate[7:]
  41. retLen = utils.DataLen
  42. } else {
  43. latestInsuranceDateStart = lastCompulsoryInsuranceDate
  44. }
  45. } else {
  46. latestInsuranceDateStart = lastCompulsoryInsuranceDate
  47. }
  48. latestInsuranceDateStart = strings.TrimSpace(latestInsuranceDateStart)
  49. if len(latestInsuranceDateStart) > utils.DataLen {
  50. latestInsuranceDateStart = latestInsuranceDateStart[:utils.DataLen]
  51. } else if len(latestInsuranceDateStart) == utils.MonthLen {
  52. latestInsuranceDateStart = latestInsuranceDateStart + "-01"
  53. }
  54. latestInsuranceDateStartTime, _ := time.Parse(utils.DayLayout, latestInsuranceDateStart)
  55. latestInsuranceDateEnd := latestInsuranceDateStartTime.AddDate(1, 0, 0).String()[:retLen]
  56. reqMap["latest_insurance_date_end"] = latestInsuranceDateEnd
  57. reqMap["latest_nsurance_date_start"] = latestInsuranceDateStart
  58. }
  59. func FormatNormal(in string) string {
  60. in = strings.TrimSpace(in)
  61. in = strings.TrimLeft(in, "-")
  62. in = strings.TrimLeft(in, ".")
  63. in = strings.TrimLeft(in, "/")
  64. in = strings.TrimLeft(in, "&")
  65. in = strings.TrimLeft(in, ",")
  66. in = strings.Replace(in, `"`, "", -1)
  67. if strings.Contains(in, "月") {
  68. return ""
  69. }
  70. switch in {
  71. case "―":
  72. return ""
  73. /*case "99999":
  74. return ""
  75. case "999999":
  76. return ""
  77. case "9999999":
  78. return ""
  79. case "99999999":
  80. return ""*/
  81. case "00976867":
  82. return ""
  83. case "02":
  84. return "2"
  85. case "04":
  86. return "4"
  87. case "06":
  88. return "6"
  89. case "07":
  90. return "7"
  91. case "08":
  92. return "8"
  93. case "09":
  94. return "9"
  95. case "未":
  96. return ""
  97. case "无":
  98. return ""
  99. case "未知":
  100. return ""
  101. case "N/A":
  102. return ""
  103. case "*ANQ":
  104. return "ANQ"
  105. case "/AYJ":
  106. return "AYJ"
  107. case "----":
  108. return ""
  109. case "---":
  110. return ""
  111. case "######":
  112. return ""
  113. case "***":
  114. return ""
  115. case "****":
  116. return ""
  117. case "**/":
  118. return ""
  119. case "....":
  120. return ""
  121. case ".00":
  122. return ""
  123. case "0.1*":
  124. return ""
  125. case "00":
  126. return ""
  127. case "000":
  128. return ""
  129. case "0000":
  130. return ""
  131. case "00000":
  132. return ""
  133. case "000000":
  134. return ""
  135. case "0000000":
  136. return ""
  137. case "+":
  138. return ""
  139. case "+G4E":
  140. return "G4E"
  141. case "0.0":
  142. return ""
  143. case "0":
  144. return ""
  145. case "-1":
  146. return ""
  147. case "?":
  148. return ""
  149. case "-":
  150. return ""
  151. case "--":
  152. return ""
  153. case ".":
  154. return ""
  155. case "..":
  156. return ""
  157. case "...":
  158. return ""
  159. case "/":
  160. return ""
  161. case "#":
  162. return ""
  163. case "##":
  164. return ""
  165. case "*":
  166. return ""
  167. case "**":
  168. return ""
  169. case "__":
  170. return ""
  171. case "null":
  172. return ""
  173. case "/8":
  174. return "8"
  175. case "#NAME?":
  176. return ""
  177. case "1.4/44KW 5G":
  178. return ""
  179. case "1.6":
  180. return ""
  181. case "1.6AT":
  182. return ""
  183. case "1.8":
  184. return ""
  185. case "1.8AT":
  186. return ""
  187. case "1.8T":
  188. return ""
  189. case "1.8TAT":
  190. return ""
  191. case "100":
  192. return ""
  193. case "100-2.2E":
  194. return ""
  195. case "100-2.6E":
  196. return ""
  197. case "1598CC":
  198. return ""
  199. case "1984CC":
  200. return ""
  201. case "1984CC小轿车":
  202. return ""
  203. case "1997CC":
  204. return ""
  205. }
  206. in = strings.Replace(in, ` `, "", -1)
  207. return in
  208. }
  209. // 发动机型号
  210. func FormatEngineType(in string) string {
  211. if in == "" {
  212. return in
  213. }
  214. in = strings.TrimSpace(in)
  215. in = strings.Replace(in, `,`, "", -1)
  216. in = strings.Replace(in, `+`, "", -1)
  217. in = strings.Replace(in, `*`, "", -1)
  218. //in = strings.Replace(in,`.`,"",-1)
  219. //in = strings.TrimLeft(in,"-")
  220. //in = strings.TrimLeft(in,"-")
  221. //in = strings.TrimLeft(in,"/")
  222. return FormatNormal(in)
  223. }
  224. // 轴荷
  225. func FormatAxleWeight(in string) string {
  226. if in == "" {
  227. return ""
  228. }
  229. in = strings.Replace(in, `"`, "", -1)
  230. in = strings.Replace(in, `-/`, "", -1)
  231. return FormatNormal(in)
  232. }
  233. //轮胎数
  234. func FormatTyreNum(tyreNum string) string {
  235. if tyreNum == "" {
  236. return ""
  237. }
  238. if strings.Contains(tyreNum, "AM") || strings.Contains(tyreNum, "月") || strings.Contains(tyreNum, "R") {
  239. return ""
  240. }
  241. tyreNum = strings.Replace(tyreNum, "]", "", -1)
  242. return FormatNormal(tyreNum)
  243. }
  244. // 轴数
  245. func FormatAxleNumber(axleNumber string) string {
  246. if axleNumber == "" {
  247. return axleNumber
  248. }
  249. switch axleNumber {
  250. case "2月4日":
  251. return "2线4轴"
  252. case "2轴":
  253. return "2"
  254. case "3轴":
  255. return "3"
  256. case "4轴":
  257. return "4"
  258. case "5轴":
  259. return "5"
  260. case "三线六轴":
  261. return "3线6轴"
  262. case "三轴":
  263. return "3"
  264. case "两线四轴":
  265. return "2线4轴"
  266. case "二线四轴":
  267. return "2线4轴"
  268. case "-":
  269. return ""
  270. case "-1":
  271. return ""
  272. }
  273. axleNumber = strings.Replace(axleNumber, `"`, "", -1)
  274. axleNumber = strings.Replace(axleNumber, `,`, "", -1)
  275. return axleNumber
  276. }
  277. // 轮胎规格
  278. func FormatTyreSize(tyreSize string) string {
  279. if tyreSize == "" {
  280. return ""
  281. }
  282. tyreSize = strings.Replace(tyreSize, `"`, "", -1)
  283. tyreSize = strings.Replace(tyreSize, `第1轴:`, "第1轴", -1)
  284. tyreSize = strings.Replace(tyreSize, `第2轴:`, "第2轴", -1)
  285. tyreSize = strings.Replace(tyreSize, `;第2轴`, "第2轴", -1)
  286. tyreSize = strings.Replace(tyreSize, `第一轴`, "第1轴", -1)
  287. tyreSize = strings.Replace(tyreSize, `第二轴`, "第2轴", -1)
  288. tyreSize = strings.Replace(tyreSize, `前`, "第1轴", -1)
  289. tyreSize = strings.Replace(tyreSize, `后`, "第2轴", -1)
  290. tyreSize = strings.Replace(tyreSize, ` `, "", -1)
  291. return tyreSize
  292. }
  293. func FormatOilWear(oilWear string) string {
  294. if oilWear == "" {
  295. return ""
  296. }
  297. // 截断
  298. oilWear = strings.Split(oilWear, "L")[0]
  299. oilWear = strings.Split(oilWear, "(")[0]
  300. oilWear = strings.Split(oilWear, "&")[0]
  301. oilWear = strings.Split(oilWear, "R")[0]
  302. oilWear = strings.Split(oilWear, "(")[0]
  303. // 去掉空格
  304. oilWear = strings.Replace(oilWear, "小于等于", "", -1)
  305. oilWear = strings.Replace(oilWear, " ", "", -1)
  306. if oilWear == "" {
  307. return ""
  308. }
  309. num, err := strconv.ParseFloat(oilWear, 10)
  310. if err != nil {
  311. return ""
  312. }
  313. if num == 0 {
  314. return ""
  315. }
  316. if num > 200 {
  317. return ""
  318. }
  319. return fmt.Sprintf("%.2f", num)
  320. }
  321. func FormatRatedPower(in string) string {
  322. num, err := strconv.ParseFloat(in, 10)
  323. if err != nil {
  324. return in
  325. }
  326. if num == 0 {
  327. return ""
  328. }
  329. return fmt.Sprintf("%.1f", num)
  330. }
  331. func FormatLongWideHigh(in string) string {
  332. if in == "" {
  333. return in
  334. }
  335. inList := strings.Split(in, ",")
  336. for _, v := range inList {
  337. if v == "0" {
  338. continue
  339. } else {
  340. return v
  341. }
  342. }
  343. return in
  344. }
  345. func FormatField(reqMap map[string]interface{}, key string) {
  346. if v, ok := reqMap[key]; ok {
  347. if _, ok = v.(string); ok && v.(string) != "" {
  348. switch key {
  349. case "engine_type":
  350. reqMap[key] = FormatEngineType(v.(string))
  351. case "axle_weight":
  352. reqMap[key] = FormatAxleWeight(v.(string))
  353. case "tyre_number":
  354. reqMap[key] = FormatTyreNum(v.(string))
  355. case "axle_number":
  356. reqMap[key] = FormatAxleNumber(v.(string))
  357. case "tyre_size":
  358. reqMap[key] = FormatTyreSize(v.(string))
  359. case "oil_wear":
  360. reqMap[key] = FormatOilWear(v.(string))
  361. case "rated_power":
  362. reqMap[key] = FormatRatedPower(v.(string))
  363. case "traction_mass":
  364. tmp := FormatNormal(v.(string))
  365. reqMap[key] = FormatLongWideHigh(tmp)
  366. case "front_wheel_distance":
  367. tmp := FormatNormal(v.(string))
  368. reqMap[key] = FormatLongWideHigh(tmp)
  369. case "back_wheel_distance":
  370. tmp := FormatNormal(v.(string))
  371. reqMap[key] = FormatLongWideHigh(tmp)
  372. case "gross_mass":
  373. tmp := FormatNormal(v.(string))
  374. reqMap[key] = FormatLongWideHigh(tmp)
  375. case "unladen_mass":
  376. tmp := FormatNormal(v.(string))
  377. reqMap[key] = FormatLongWideHigh(tmp)
  378. case "approved_load":
  379. tmp := FormatNormal(v.(string))
  380. reqMap[key] = FormatLongWideHigh(tmp)
  381. case "wheel_base":
  382. wheelBase := FormatNormal(v.(string))
  383. reqMap[key] = FormatLongWideHigh(wheelBase)
  384. case "long":
  385. reqMap[key] = FormatLongWideHigh(v.(string))
  386. case "wide":
  387. reqMap[key] = FormatLongWideHigh(v.(string))
  388. case "high":
  389. reqMap[key] = FormatLongWideHigh(v.(string))
  390. case "approved_number":
  391. reqMap[key] = FormatNormal(v.(string))
  392. case "displacement":
  393. reqMap[key] = FormatNormal(v.(string))
  394. case "displacement_l":
  395. reqMap[key] = FormatNormal(v.(string))
  396. case "engine_no":
  397. reqMap[key] = FormatNormal(v.(string))
  398. }
  399. }
  400. }
  401. }
  402. func DisplacementTol(displacementml string) string {
  403. tmp, err := strconv.Atoi(displacementml)
  404. if err != nil {
  405. return displacementml
  406. }
  407. if tmp == 0 {
  408. return displacementml
  409. }
  410. left := tmp % 100
  411. if left >= 50 {
  412. tmp = tmp / 100
  413. tmp = tmp * 100
  414. tmp = tmp + 100
  415. } else {
  416. tmp = tmp / 100
  417. tmp = tmp * 100
  418. }
  419. r := float64(tmp) / float64(1000)
  420. return fmt.Sprintf("%.1f", r)
  421. }
  422. // 格式化数据
  423. func F01(ctx context.Context, params string) (reply *apis.QueryResponse, err error) {
  424. reply = &apis.QueryResponse{}
  425. reply.Data = params
  426. reqMap := make(map[string]interface{})
  427. err = json.Unmarshal([]byte(params), &reqMap)
  428. if err != nil {
  429. return reply, errors.ParamsError
  430. }
  431. // 格式化数据
  432. formatData(reqMap)
  433. reply.Data, _ = jsoniter.MarshalToString(reqMap)
  434. return reply, nil
  435. }