U01.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package query
  2. import (
  3. "adm-data/errors"
  4. "adm-data/model"
  5. v1 "adm-data/pb/v1"
  6. "adm-data/utils"
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. "strconv"
  11. "strings"
  12. jsoniter "github.com/json-iterator/go"
  13. "git.getensh.com/common/gopkgsv2/database"
  14. "git.getensh.com/common/gopkgsv2/logger"
  15. "go.uber.org/zap"
  16. "google.golang.org/grpc/status"
  17. )
  18. func getString(key string, old map[string]interface{}) string {
  19. if v, ok := old[key]; ok {
  20. switch v.(type) {
  21. case string:
  22. return strings.Split(v.(string), ",")[0]
  23. case int:
  24. return strconv.Itoa(v.(int))
  25. case int16:
  26. return strconv.Itoa(int(v.(int16)))
  27. case int32:
  28. return strconv.Itoa(int(v.(int32)))
  29. case int64:
  30. return strconv.Itoa(int(v.(int64)))
  31. case float64:
  32. return fmt.Sprintf("%1.0f", v.(float64))
  33. case float32:
  34. return fmt.Sprintf("%1.0f", v.(float32))
  35. }
  36. } else {
  37. return ""
  38. }
  39. return ""
  40. }
  41. func calcPlateType(longStr, approvedNumberStr, grossMassStr string) string {
  42. long, _ := strconv.Atoi(longStr)
  43. approvedNumber, _ := strconv.Atoi(approvedNumberStr)
  44. grossMass, _ := strconv.Atoi(grossMassStr)
  45. if long >= 6000 || approvedNumber >= 20 || grossMass >= 4500 {
  46. return "01"
  47. }
  48. if (long != 0) && (approvedNumber != 0 || grossMass != 0) {
  49. if long < 6000 && approvedNumber > 9 {
  50. return "01"
  51. } else {
  52. return "02"
  53. }
  54. }
  55. return ""
  56. }
  57. func supplementPlateTypeByCalc(old map[string]interface{}) {
  58. if v, ok := old["plate_type"]; ok {
  59. switch v.(type) {
  60. case string:
  61. if v.(string) == "" {
  62. longStr := getString("long", old)
  63. approvedNumberStr := getString("approved_number", old)
  64. grossMassStr := getString("gross_mass", old)
  65. old["plate_type"] = calcPlateType(longStr, approvedNumberStr, grossMassStr)
  66. }
  67. }
  68. }
  69. }
  70. func ToInt(newMap map[string]string, k string) (ret int) {
  71. if v, ok := newMap[k]; ok {
  72. if v != "" {
  73. v = strings.Split(v, ",")[0]
  74. }
  75. ret, _ = strconv.Atoi(v)
  76. }
  77. return ret
  78. }
  79. func ToFloat64(newMap map[string]string, k string) (ret float64) {
  80. if v, ok := newMap[k]; ok {
  81. if v != "" {
  82. v = strings.Split(v, ",")[0]
  83. }
  84. ret, _ = strconv.ParseFloat(v, 64)
  85. }
  86. return ret
  87. }
  88. func ToFloat32(newMap map[string]string, k string) (ret float32) {
  89. if v, ok := newMap[k]; ok {
  90. ret1, _ := strconv.ParseFloat(v, 32)
  91. ret = float32(ret1)
  92. }
  93. return ret
  94. }
  95. func supplementData(reqMap map[string]interface{}) {
  96. modelNo := ""
  97. vin := ""
  98. if v, ok := reqMap["model_no"]; ok {
  99. modelNo = v.(string)
  100. }
  101. if v, ok := reqMap["vin"]; ok {
  102. vin = v.(string)
  103. }
  104. // 补充数据
  105. modelNo = supplementByAds19(vin, modelNo, reqMap)
  106. // 补充号牌种类
  107. supplementPlateType(modelNo, reqMap)
  108. supplementPlateTypeByCalc(reqMap)
  109. }
  110. func supplementByAds19(vin string, modelNo string, old map[string]interface{}) (newModelNo string) {
  111. if modelNo == "" && vin == "" {
  112. return
  113. }
  114. db := database.DB()
  115. if modelNo == "" {
  116. vinRule := ""
  117. if len(vin) > 8 {
  118. vinRule = vin[0:8]
  119. }
  120. if vinRule == "" {
  121. return
  122. }
  123. ads21 := &model.Ads21{}
  124. if err := ads21.Query(db, map[string]interface{}{"vin_rule": vinRule}); err != nil {
  125. return
  126. }
  127. old["model_no"] = ads21.ModelNo
  128. modelNo = ads21.ModelNo
  129. }
  130. ads19 := &model.Ads19{}
  131. if err := ads19.Query(db, map[string]interface{}{"model_no": modelNo}); err != nil {
  132. return modelNo
  133. }
  134. newData, _ := json.Marshal(ads19)
  135. newMap := make(map[string]string)
  136. json.Unmarshal(newData, &newMap)
  137. for k, v1 := range old {
  138. switch v1.(type) {
  139. case string:
  140. if v1.(string) == "" {
  141. if newMap[k] != "" {
  142. newMap[k] = strings.Split(newMap[k], ",")[0]
  143. }
  144. old[k] = newMap[k]
  145. }
  146. case int:
  147. if v1.(int) == 0 {
  148. old[k] = ToInt(newMap, k)
  149. }
  150. case int16:
  151. if v1.(int16) == 0 {
  152. old[k] = int16(ToInt(newMap, k))
  153. }
  154. case int32:
  155. if v1.(int32) == 0 {
  156. old[k] = int32(ToInt(newMap, k))
  157. }
  158. case int64:
  159. if v1.(int64) == 0 {
  160. old[k] = int64(ToInt(newMap, k))
  161. }
  162. case float64:
  163. if v1.(float64) == 0 {
  164. old[k] = int64(ToFloat64(newMap, k))
  165. }
  166. case float32:
  167. if v1.(float32) == 0 {
  168. old[k] = int64(ToFloat32(newMap, k))
  169. }
  170. }
  171. }
  172. return modelNo
  173. }
  174. func formatDataCode(m map[string]interface{}, s, t string, f func(string) (string, string)) {
  175. if v, ok := m[s]; ok {
  176. if _, ok = v.(string); ok && v.(string) == "" {
  177. if v, ok = m[t]; ok {
  178. if _, ok = v.(string); ok && v.(string) != "" {
  179. m[s], _ = f(v.(string))
  180. return
  181. }
  182. }
  183. }
  184. }
  185. if v, ok := m[t]; ok {
  186. if _, ok = v.(string); ok && v.(string) == "" {
  187. if v, ok = m[s]; ok {
  188. if _, ok = v.(string); ok && v.(string) != "" {
  189. m[t], _ = f(v.(string))
  190. return
  191. }
  192. }
  193. }
  194. }
  195. }
  196. func formatDate(m map[string]interface{}, s string) {
  197. if v, ok := m[s]; ok {
  198. if _, ok = v.(string); ok && v.(string) != "" {
  199. m[s] = utils.FormatDate(v.(string))
  200. }
  201. }
  202. }
  203. func formatDisplacement(reqMap map[string]interface{}) {
  204. if v, ok := reqMap["displacement"]; ok {
  205. var (
  206. temp int
  207. )
  208. switch v.(type) {
  209. case string:
  210. temp, _ = strconv.Atoi(v.(string))
  211. case float64:
  212. temp = int(v.(float64))
  213. case int64:
  214. temp = int(v.(int64))
  215. }
  216. if temp > 100000 {
  217. temp /= 1000
  218. switch v.(type) {
  219. case string:
  220. reqMap["displacement"] = strconv.Itoa(temp)
  221. case float64:
  222. reqMap["displacement"] = float64(temp)
  223. case int64:
  224. reqMap["displacement"] = int64(temp)
  225. }
  226. }
  227. if temp > 0 {
  228. if l, ok := reqMap["displacement_l"]; ok {
  229. if _, ok = l.(string); ok && l.(string) == "" {
  230. reqMap["displacement_l"] = strconv.FormatFloat(float64(temp)/float64(1000), 'f', 1, 64)
  231. }
  232. }
  233. }
  234. }
  235. if v, ok := reqMap["displacement_l"]; ok {
  236. if _, ok = v.(string); ok && v.(string) != "" {
  237. tmp := strings.Replace(v.(string), "L", "", -1)
  238. tmp = strings.Replace(tmp, "T", "", -1)
  239. displacementL, _ := strconv.ParseFloat(tmp, 64)
  240. if displacementL > 100 {
  241. reqMap["displacement_l"] = strconv.FormatFloat(float64(displacementL)/float64(1000), 'f', 1, 64)
  242. }
  243. if l, ok := reqMap["displacement"]; ok {
  244. var (
  245. temp int
  246. )
  247. switch l.(type) {
  248. case string:
  249. temp, _ = strconv.Atoi(l.(string))
  250. case float64:
  251. temp = int(l.(float64))
  252. case int64:
  253. temp = int(l.(int64))
  254. }
  255. if temp == 0 {
  256. if displacementL > 100 {
  257. temp = int(displacementL)
  258. } else {
  259. temp = int(displacementL * float64(1000))
  260. }
  261. switch l.(type) {
  262. case string:
  263. reqMap["displacement"] = strconv.Itoa(temp)
  264. case float64:
  265. reqMap["displacement"] = float64(temp)
  266. case int64:
  267. reqMap["displacement"] = int64(temp)
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }
  274. func formatBrand(reqMap map[string]interface{}) {
  275. if v, ok := reqMap["brand_name"]; ok {
  276. if _, ok = v.(string); ok && v.(string) != "" {
  277. if ads22, err := model.NewAds22Model().Get(database.DB().Where("source = ? and type = 6", v)); err == nil {
  278. if ads22.Standard != "" {
  279. reqMap["brand_id"] = ads22.Standard
  280. if ads2, err := model.NewAds2Model().Get(database.DB().Where("brand_id = ?", ads22.Standard)); err == nil {
  281. reqMap["brand_name"] = ads2.BrandName
  282. }
  283. } else {
  284. if ads2, err := model.NewAds2Model().Get(database.DB().Where("brand_name = ?", v)); err == nil {
  285. reqMap["brand_name"] = ads2.BrandName
  286. if ads2.BrandId != "" {
  287. reqMap["brand_id"] = ads2.BrandId
  288. }
  289. }
  290. }
  291. } else {
  292. if ads2, err := model.NewAds2Model().Get(database.DB().Where("brand_name = ?", v)); err == nil {
  293. reqMap["brand_name"] = ads2.BrandName
  294. if ads2.BrandId != "" {
  295. reqMap["brand_id"] = ads2.BrandId
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. func formatEmissionStandard(reqMap map[string]interface{}) {
  303. if v, ok := reqMap["emission_standard"]; ok {
  304. if _, ok = v.(string); ok && v.(string) != "" {
  305. reqMap["emission_standard"] = utils.EmissionStandard(v.(string))
  306. }
  307. }
  308. }
  309. func supplementPlateType(modelNo string, reqMap map[string]interface{}) {
  310. if v, ok := reqMap["plate_type"]; ok {
  311. if _, ok = v.(string); ok && v.(string) == "" {
  312. if v1, ok := reqMap["plate_no"]; ok {
  313. if _, ok = v1.(string); ok {
  314. plateNo := v1.(string)
  315. if plateNo != "" {
  316. plateType, _ := utils.VehicleDefaultPlateType(&plateNo)
  317. if plateType != "" {
  318. reqMap["plate_type"] = plateType
  319. return
  320. }
  321. }
  322. }
  323. }
  324. if modelNo == "" {
  325. return
  326. }
  327. if ads18, err := model.NewAds18Model().Get(database.DB().Where("model_no = ?", modelNo)); err == nil {
  328. reqMap["plate_type"] = ads18.PlateType
  329. }
  330. }
  331. }
  332. }
  333. // 判断首保日期和初登日期
  334. func supplementRegisterDate(reqMap map[string]interface{}) {
  335. if insuranceDate, ok := reqMap["insurance_first_date"]; ok {
  336. // 存在首保日期
  337. if initialRegistrationDate, ok := reqMap["initial_registration_date"]; ok && initialRegistrationDate == "" {
  338. reqMap["initial_registration_date"] = insuranceDate
  339. }
  340. }
  341. }
  342. func formatYearMonth(m map[string]interface{},s string){
  343. if v, ok := m[s]; ok {
  344. if _, ok = v.(string); ok && v.(string) != "" {
  345. if len(v.(string)) > utils.MonthLen{
  346. m[s] = v.(string)[0:utils.MonthLen]
  347. }
  348. }
  349. }
  350. }
  351. func formatData(reqMap map[string]interface{}) {
  352. // 补充初等
  353. supplementRegisterDate(reqMap)
  354. // 格式化日期
  355. formatDate(reqMap, "initial_registration_date")
  356. formatDate(reqMap, "inspection_result_effective_to")
  357. formatDate(reqMap, "release_date")
  358. formatDate(reqMap, "insurance_first_date")
  359. formatDate(reqMap, "compulsory_scrap_to")
  360. formatYearMonth(reqMap,"last_compulsory_insurance_date")
  361. // 格式化数据编码
  362. formatDataCode(reqMap, "use_property", "use_property_detail", utils.ParseProperty)
  363. formatDataCode(reqMap, "fuel_type", "fuel_type_detail", utils.ParseFuel)
  364. formatDataCode(reqMap, "vehicle_type", "vehicle_type_detail", utils.VehicleType)
  365. formatDataCode(reqMap, "vehicle_body_color", "vehicle_body_color_detail", utils.ParseColor)
  366. formatDataCode(reqMap, "body_color", "body_color_detail", utils.ParseColor)
  367. // 格式化排放标准
  368. formatEmissionStandard(reqMap)
  369. // 格式化排量
  370. formatDisplacement(reqMap)
  371. // 格式化品牌
  372. formatBrand(reqMap)
  373. // 计算强制报废日期
  374. utils.CalcCompulsoryScrapTo(reqMap)
  375. // 计算年检到期时间
  376. utils.CalcAnnualExamExpireTime(reqMap)
  377. }
  378. func U01(ctx context.Context, params string) (reply *v1.QueryResponse, err error) {
  379. reply = &v1.QueryResponse{}
  380. // 捕获各个task中的异常并返回给调用者
  381. defer func() {
  382. if r := recover(); r != nil {
  383. err = fmt.Errorf("%+v", r)
  384. e := &status.Status{}
  385. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  386. logger.Error("err",
  387. zap.String("system_err", err.Error()),
  388. zap.Stack("stacktrace"))
  389. }
  390. }
  391. }()
  392. reply.Data = params
  393. reqMap := make(map[string]interface{})
  394. err = json.Unmarshal([]byte(params), &reqMap)
  395. if err != nil {
  396. return nil, errors.ParamsError
  397. }
  398. // 补充车辆数据
  399. supplementData(reqMap)
  400. // 格式化数据
  401. formatData(reqMap)
  402. reply.Data, _ = jsoniter.MarshalToString(reqMap)
  403. return reply, nil
  404. }