calc_annual_date.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package utils
  4. import (
  5. "fmt"
  6. "gd_adm_data/errors"
  7. "strings"
  8. "time"
  9. )
  10. const (
  11. //营运载客汽车5年以内每年检验1次;超过5年的,每6个月检验1次
  12. rule1 = iota
  13. //载货汽车和大型、中型非营运载客汽车10年以内每年检验1次;超过10年的,每6个月检验1次
  14. rule2
  15. //小型、微型非营运载客汽车6年以内每2年检验1次;超过6年的,每年检验1次;超过15年的,每6个月检验1次
  16. rule3
  17. //摩托车4年以内每2年检验1次;超过4年的,每年检验1次
  18. rule4
  19. //拖拉机和其他机动车每年检验1次
  20. rule5
  21. //校车每半年检验1次
  22. rule6
  23. //非营运轿车、非营运小型和微型载客汽车6年免检 6年以内每2年免上线检验1次,超过6年的,每年检验1次;超过15年的,每6个月检验1次
  24. rule7
  25. )
  26. /*
  27. rule1:营运载客汽车5年以内每年检验1次;超过5年的,每6个月检验1次
  28. rule2:载货汽车和大型、中型非营运载客汽车10年以内每年检验1次;超过10年的,每6个月检验1次
  29. rule3:小型、微型非营运载客汽车6年以内每2年检验1次;超过6年的,每年检验1次;超过15年的,每6个月检验1次
  30. rule4:摩托车4年以内每2年检验1次;超过4年的,每年检验1次
  31. rule5:拖拉机和其他机动车每年检验1次
  32. rule6:校车每半年检验1次
  33. rule7:非营运轿车、非营运小型和微型载客汽车6年免检 6年以内每2年免上线检验1次,超过6年的,每年检验1次;超过15年的,每6个月检验1次
  34. */
  35. type AnnualExamDetail struct {
  36. Desc string `json:"desc"` //车龄+几年年检+ 1次
  37. NumYear int64 `json:"num"` //第几年
  38. }
  39. type ItemTime struct {
  40. Date string `json:"date"` //时间
  41. Day int64 `json:"day"` //还剩多少天
  42. }
  43. type GetAnnualInspectionTimeInfo struct {
  44. TimeRemain string `json:"time_remain" description:"年检到期剩余时间"`
  45. TimeDate string `json:"time_date" description:"年检到期时间"`
  46. Rule string `json:"rule" description:"年检规则"`
  47. TimeYear string `json:"time_year" description:"第几年"`
  48. CanBeUncheck bool `json:"can_be_uncheck" description:"是否可免检年审"`
  49. CanBeInspection bool `json:"can_be_inspection" description:"是否到达年审时间"`
  50. }
  51. //获取月份的天数
  52. func DayCount(year int, month int) (days int) {
  53. if month != 2 {
  54. if month == 4 || month == 6 || month == 9 || month == 11 {
  55. days = 30
  56. } else {
  57. days = 31
  58. }
  59. } else {
  60. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  61. days = 29
  62. } else {
  63. days = 28
  64. }
  65. }
  66. return days
  67. }
  68. //计算 start 到 end 还有多少天
  69. func DeltaTimeByDay(start time.Time, end time.Time) int64 {
  70. sub := start.Truncate(24 * time.Hour).Sub(end.Truncate(24 * time.Hour))
  71. return int64(sub.Hours() / 24)
  72. }
  73. //营运载客汽车
  74. func CalcAnnualExamTimeOperation(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  75. var item ItemTime
  76. var detail AnnualExamDetail
  77. // 年检时间
  78. annualExamString := ""
  79. var annualExamYear int64 = 0
  80. annualExamDesc := ""
  81. if examTime != 0 {
  82. tempTime := time.Unix(examTime/1000, 0)
  83. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), tempTime.Month(), DayCount(int(tempTime.Year()), int(tempTime.Month())))
  84. } else {
  85. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), cdrqTime.Month(), DayCount(int(cdrqTime.Year()), int(cdrqTime.Month())))
  86. }
  87. annualExamTime, err := time.Parse("2006-01-02", annualExamString)
  88. if err != nil {
  89. return item, detail, errors.TimeCalcFailed
  90. }
  91. for i := 0; i < 5; i++ {
  92. annualExamYear += 1
  93. annualExamTime = annualExamTime.AddDate(1, 0, 0)
  94. annualExamDesc = "5年车龄1年年检1次"
  95. if annualExamTime.After(nowTime) {
  96. if isExpire {
  97. annualExamTime = annualExamTime.AddDate(-1, 0, 0)
  98. }
  99. goto CALCANNUALEXAMTIME
  100. }
  101. }
  102. for i := 0; i < 30; i++ {
  103. if i%2 == 0 {
  104. annualExamYear += 1
  105. }
  106. annualExamTime = annualExamTime.AddDate(0, 6, 0)
  107. annualExamDesc = "5年以上车龄每6个月年检1次"
  108. if annualExamTime.After(nowTime) {
  109. if isExpire {
  110. annualExamTime = annualExamTime.AddDate(0, -6, 0)
  111. }
  112. goto CALCANNUALEXAMTIME
  113. }
  114. }
  115. CALCANNUALEXAMTIME:
  116. item.Date = annualExamTime.Format("2006-01-02 15:04:05")
  117. item.Day = DeltaTimeByDay(annualExamTime, nowTime)
  118. detail.NumYear = annualExamYear
  119. detail.Desc = annualExamDesc
  120. return item, detail, nil
  121. }
  122. //载货汽车和大型、中型非营运载客汽车10年以内每年检验1次;超过10年的每6个月检验1次
  123. func CalcAnnualExamTimeBigVehicle(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  124. var item ItemTime
  125. var detail AnnualExamDetail
  126. // 年检时间
  127. annualExamString := ""
  128. var annualExamYear int64 = 0
  129. annualExamDesc := ""
  130. if examTime != 0 {
  131. tempTime := time.Unix(examTime/1000, 0)
  132. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), tempTime.Month(), DayCount(int(tempTime.Year()), int(tempTime.Month())))
  133. } else {
  134. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), cdrqTime.Month(), DayCount(int(cdrqTime.Year()), int(cdrqTime.Month())))
  135. }
  136. annualExamTime, err := time.Parse("2006-01-02", annualExamString)
  137. if err != nil {
  138. return item, detail, errors.TimeCalcFailed
  139. }
  140. for i := 0; i < 10; i++ {
  141. annualExamYear += 1
  142. annualExamTime = annualExamTime.AddDate(1, 0, 0)
  143. annualExamDesc = "10年车龄1年年检1次"
  144. if annualExamTime.After(nowTime) {
  145. if isExpire {
  146. annualExamTime = annualExamTime.AddDate(-1, 0, 0)
  147. }
  148. goto CALCANNUALEXAMTIME
  149. }
  150. }
  151. for i := 0; i < 30; i++ {
  152. if i%2 == 0 {
  153. annualExamYear += 1
  154. }
  155. annualExamTime = annualExamTime.AddDate(0, 6, 0)
  156. annualExamDesc = "10年以上车龄每6个月年检1次"
  157. if annualExamTime.After(nowTime) {
  158. if isExpire {
  159. annualExamTime = annualExamTime.AddDate(0, -6, 0)
  160. }
  161. goto CALCANNUALEXAMTIME
  162. }
  163. }
  164. CALCANNUALEXAMTIME:
  165. item.Date = annualExamTime.Format("2006-01-02 15:04:05")
  166. item.Day = DeltaTimeByDay(annualExamTime, nowTime)
  167. detail.NumYear = annualExamYear
  168. detail.Desc = annualExamDesc
  169. return item, detail, nil
  170. }
  171. //摩托车
  172. func CalcAnnualExamTimeMotor(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  173. var item ItemTime
  174. var detail AnnualExamDetail
  175. // 年检时间
  176. annualExamString := ""
  177. var annualExamYear int64 = 0
  178. annualExamDesc := ""
  179. if examTime != 0 {
  180. tempTime := time.Unix(examTime/1000, 0)
  181. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), tempTime.Month(), DayCount(int(tempTime.Year()), int(tempTime.Month())))
  182. } else {
  183. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), cdrqTime.Month(), DayCount(int(cdrqTime.Year()), int(cdrqTime.Month())))
  184. }
  185. annualExamTime, err := time.Parse("2006-01-02", annualExamString)
  186. if err != nil {
  187. return item, detail, errors.TimeCalcFailed
  188. }
  189. for i := 0; i < 2; i++ {
  190. annualExamYear += 2
  191. annualExamTime = annualExamTime.AddDate(2, 0, 0)
  192. annualExamDesc = "4年车龄2年年检1次"
  193. if annualExamTime.After(nowTime) {
  194. if isExpire {
  195. annualExamTime = annualExamTime.AddDate(-2, 0, 0)
  196. }
  197. goto CALCANNUALEXAMTIME
  198. }
  199. }
  200. for i := 0; i < 30; i++ {
  201. annualExamYear += 1
  202. annualExamTime = annualExamTime.AddDate(1, 0, 0)
  203. annualExamDesc = "4年以上车龄1年年检1次"
  204. if annualExamTime.After(nowTime) {
  205. if isExpire {
  206. annualExamTime = annualExamTime.AddDate(-1, 0, 0)
  207. }
  208. goto CALCANNUALEXAMTIME
  209. }
  210. }
  211. CALCANNUALEXAMTIME:
  212. item.Date = annualExamTime.Format("2006-01-02 15:04:05")
  213. item.Day = DeltaTimeByDay(annualExamTime, nowTime)
  214. detail.NumYear = annualExamYear
  215. detail.Desc = annualExamDesc
  216. return item, detail, nil
  217. }
  218. func CalcAnnualExamTimeRule1(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  219. return CalcAnnualExamTimeOperation(nowTime, cdrqTime, examTime, isExpire)
  220. }
  221. func CalcAnnualExamTimeRule2(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  222. return CalcAnnualExamTimeBigVehicle(nowTime, cdrqTime, examTime, isExpire)
  223. }
  224. func CalcAnnualExamTimeRule3(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  225. return CalcAnnualExamTime(nowTime, cdrqTime, examTime, isExpire)
  226. }
  227. func CalcAnnualExamTimeRule4(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  228. return CalcAnnualExamTimeMotor(nowTime, cdrqTime, examTime, isExpire)
  229. }
  230. func CalcAnnualExamTimeRule5(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  231. var item ItemTime
  232. var detail AnnualExamDetail
  233. // 年检时间
  234. annualExamString := ""
  235. var annualExamYear int64 = 0
  236. annualExamDesc := ""
  237. if examTime != 0 {
  238. tempTime := time.Unix(examTime/1000, 0)
  239. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), tempTime.Month(), DayCount(int(tempTime.Year()), int(tempTime.Month())))
  240. } else {
  241. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), cdrqTime.Month(), DayCount(int(cdrqTime.Year()), int(cdrqTime.Month())))
  242. }
  243. annualExamTime, err := time.Parse("2006-01-02", annualExamString)
  244. if err != nil {
  245. return item, detail, errors.TimeCalcFailed
  246. }
  247. for i := 0; i < 30; i++ {
  248. annualExamYear += 1
  249. annualExamTime = annualExamTime.AddDate(1, 0, 0)
  250. annualExamDesc = "每年年检1次"
  251. if annualExamTime.After(nowTime) {
  252. if isExpire {
  253. annualExamTime = annualExamTime.AddDate(-1, 0, 0)
  254. }
  255. goto CALCANNUALEXAMTIME
  256. }
  257. }
  258. CALCANNUALEXAMTIME:
  259. item.Date = annualExamTime.Format("2006-01-02 15:04:05")
  260. item.Day = DeltaTimeByDay(annualExamTime, nowTime)
  261. detail.NumYear = annualExamYear
  262. detail.Desc = annualExamDesc
  263. return item, detail, nil
  264. }
  265. func CalcAnnualExamTimeRule6(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  266. var item ItemTime
  267. var detail AnnualExamDetail
  268. // 年检时间
  269. annualExamString := ""
  270. var annualExamYear int64 = 0
  271. annualExamDesc := ""
  272. if examTime != 0 {
  273. tempTime := time.Unix(examTime/1000, 0)
  274. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), tempTime.Month(), DayCount(int(tempTime.Year()), int(tempTime.Month())))
  275. } else {
  276. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), cdrqTime.Month(), DayCount(int(cdrqTime.Year()), int(cdrqTime.Month())))
  277. }
  278. annualExamTime, err := time.Parse("2006-01-02", annualExamString)
  279. if err != nil {
  280. return item, detail, errors.TimeCalcFailed
  281. }
  282. for i := 0; i < 30; i++ {
  283. if i%2 == 0 {
  284. annualExamYear += 1
  285. }
  286. annualExamTime = annualExamTime.AddDate(0, 6, 0)
  287. annualExamDesc = "每6个月年检1次"
  288. if annualExamTime.After(nowTime) {
  289. if isExpire {
  290. annualExamTime = annualExamTime.AddDate(0, -6, 0)
  291. }
  292. goto CALCANNUALEXAMTIME
  293. }
  294. }
  295. CALCANNUALEXAMTIME:
  296. item.Date = annualExamTime.Format("2006-01-02 15:04:05")
  297. item.Day = DeltaTimeByDay(annualExamTime, nowTime)
  298. detail.NumYear = annualExamYear
  299. detail.Desc = annualExamDesc
  300. return item, detail, nil
  301. }
  302. func CalcAnnualExamTime(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  303. var item ItemTime
  304. var detail AnnualExamDetail
  305. // 年检时间
  306. annualExamString := ""
  307. var annualExamYear int64 = 0
  308. annualExamDesc := ""
  309. if examTime != 0 {
  310. tempTime := time.Unix(examTime/1000, 0)
  311. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), tempTime.Month(), DayCount(int(tempTime.Year()), int(tempTime.Month())))
  312. } else {
  313. annualExamString = fmt.Sprintf("%d-%0.2d-%0.2d", cdrqTime.Year(), cdrqTime.Month(), DayCount(int(cdrqTime.Year()), int(cdrqTime.Month())))
  314. }
  315. annualExamTime, err := time.Parse("2006-01-02", annualExamString)
  316. if err != nil {
  317. return item, detail, errors.TimeCalcFailed
  318. }
  319. for i := 0; i < 5; i++ {
  320. annualExamYear += 2
  321. annualExamTime = annualExamTime.AddDate(2, 0, 0)
  322. annualExamDesc = "10年车龄2年年检1次"
  323. if annualExamTime.After(nowTime) {
  324. if isExpire {
  325. annualExamTime = annualExamTime.AddDate(-2, 0, 0)
  326. }
  327. goto CALCANNUALEXAMTIME
  328. }
  329. }
  330. for i := 0; i < 5; i++ {
  331. annualExamYear += 1
  332. annualExamTime = annualExamTime.AddDate(1, 0, 0)
  333. annualExamDesc = "10年以上车龄1年年检1次"
  334. if annualExamTime.After(nowTime) {
  335. if isExpire {
  336. annualExamTime = annualExamTime.AddDate(-1, 0, 0)
  337. }
  338. goto CALCANNUALEXAMTIME
  339. }
  340. }
  341. for i := 0; i < 30; i++ {
  342. if i%2 == 0 {
  343. annualExamYear += 1
  344. }
  345. annualExamTime = annualExamTime.AddDate(0, 6, 0)
  346. annualExamDesc = "15年以上车龄每6个月年检1次"
  347. if annualExamTime.After(nowTime) {
  348. if isExpire {
  349. annualExamTime = annualExamTime.AddDate(0, -6, 0)
  350. }
  351. goto CALCANNUALEXAMTIME
  352. }
  353. }
  354. CALCANNUALEXAMTIME:
  355. item.Date = annualExamTime.Format("2006-01-02 15:04:05")
  356. item.Day = DeltaTimeByDay(annualExamTime, nowTime)
  357. detail.NumYear = annualExamYear
  358. detail.Desc = annualExamDesc
  359. return item, detail, nil
  360. }
  361. func CalcAnnualExamTimeRule7(nowTime time.Time, cdrqTime time.Time, examTime int64, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  362. return CalcAnnualExamTime(nowTime, cdrqTime, examTime, isExpire)
  363. }
  364. func calcAnnualExamTimeByRule(nowTime time.Time, cdrqTime time.Time, rule int, isExpire bool) (ItemTime, AnnualExamDetail, error) {
  365. if rule == rule1 {
  366. return CalcAnnualExamTimeRule1(nowTime, cdrqTime, 0, isExpire)
  367. }
  368. if rule == rule2 {
  369. return CalcAnnualExamTimeRule2(nowTime, cdrqTime, 0, isExpire)
  370. }
  371. if rule == rule3 {
  372. return CalcAnnualExamTimeRule3(nowTime, cdrqTime, 0, isExpire)
  373. }
  374. if rule == rule4 {
  375. return CalcAnnualExamTimeRule4(nowTime, cdrqTime, 0, isExpire)
  376. }
  377. if rule == rule5 {
  378. return CalcAnnualExamTimeRule5(nowTime, cdrqTime, 0, isExpire)
  379. }
  380. if rule == rule6 {
  381. return CalcAnnualExamTimeRule6(nowTime, cdrqTime, 0, isExpire)
  382. }
  383. return CalcAnnualExamTimeRule7(nowTime, cdrqTime, 0, isExpire)
  384. }
  385. var OperationProperty = map[string]string{"B": "", "C": "", "D": "", "E": "", "F": "", "G": "", "R": "", "T": ""}
  386. var UnoperationProperty = map[string]string{"A": "", "H": "", "I": "", "J": "", "K": "", "L": "", "M": "", "N": "", "U": ""}
  387. func isOperation(usePropery string) bool {
  388. if usePropery == "" {
  389. return false
  390. }
  391. bytes := []byte(usePropery)
  392. for _, v := range bytes {
  393. s := fmt.Sprintf("%c", v)
  394. if _, ok := OperationProperty[s]; ok == false {
  395. return false
  396. }
  397. }
  398. return true
  399. }
  400. func isUnoperation(usePropery string) bool {
  401. if usePropery == "" {
  402. return false
  403. }
  404. bytes := []byte(usePropery)
  405. for _, v := range bytes {
  406. s := fmt.Sprintf("%c", v)
  407. if _, ok := UnoperationProperty[s]; ok == false {
  408. return false
  409. }
  410. }
  411. return true
  412. }
  413. func isRule1(useProperty string, vehicleType string, seat int, reg time.Time) bool {
  414. if isOperation(useProperty) == false {
  415. return false
  416. }
  417. if vehicleType == "K10" ||
  418. vehicleType == "K11" ||
  419. vehicleType == "K12" ||
  420. vehicleType == "K13" ||
  421. vehicleType == "K14" ||
  422. vehicleType == "K15" ||
  423. vehicleType == "K16" ||
  424. vehicleType == "K17" ||
  425. vehicleType == "K20" ||
  426. vehicleType == "K21" ||
  427. vehicleType == "K22" ||
  428. vehicleType == "K23" ||
  429. vehicleType == "K24" ||
  430. vehicleType == "K25" ||
  431. vehicleType == "K26" ||
  432. vehicleType == "K27" ||
  433. vehicleType == "K30" ||
  434. vehicleType == "K31" ||
  435. vehicleType == "K32" ||
  436. vehicleType == "K33" ||
  437. vehicleType == "K34" ||
  438. vehicleType == "K39" ||
  439. vehicleType == "K40" ||
  440. vehicleType == "K41" ||
  441. vehicleType == "K42" ||
  442. vehicleType == "K43" {
  443. return true
  444. }
  445. return false
  446. }
  447. func isRule2(useProperty string, vehicleType string, seat int, reg time.Time) bool {
  448. if strings.Contains(vehicleType, "G") ||
  449. strings.Contains(vehicleType, "Q") ||
  450. strings.Contains(vehicleType, "B") ||
  451. strings.Contains(vehicleType, "H") {
  452. return true
  453. }
  454. if isUnoperation(useProperty) == false {
  455. return false
  456. }
  457. if vehicleType == "K10" ||
  458. vehicleType == "K11" ||
  459. vehicleType == "K12" ||
  460. vehicleType == "K13" ||
  461. vehicleType == "K14" ||
  462. vehicleType == "K15" ||
  463. vehicleType == "K16" ||
  464. vehicleType == "K17" ||
  465. vehicleType == "K20" ||
  466. vehicleType == "K21" ||
  467. vehicleType == "K22" ||
  468. vehicleType == "K23" ||
  469. vehicleType == "K24" ||
  470. vehicleType == "K25" ||
  471. vehicleType == "K26" ||
  472. vehicleType == "K27" {
  473. return true
  474. }
  475. return false
  476. }
  477. func isRule3(useProperty string, vehicleType string, seat int, reg time.Time) bool {
  478. if isUnoperation(useProperty) == false {
  479. return false
  480. }
  481. if vehicleType == "K30" ||
  482. vehicleType == "K31" ||
  483. vehicleType == "K32" ||
  484. vehicleType == "K33" ||
  485. vehicleType == "K34" ||
  486. vehicleType == "K40" ||
  487. vehicleType == "K41" ||
  488. vehicleType == "K42" ||
  489. vehicleType == "K43" ||
  490. vehicleType == "K39" ||
  491. vehicleType == "K49" {
  492. return true
  493. }
  494. return false
  495. }
  496. func isRule4(useProperty string, vehicleType string, seat int, reg time.Time) bool {
  497. if strings.Contains(vehicleType, "M") {
  498. return true
  499. }
  500. return false
  501. }
  502. func isRule5(useProperty string, vehicleType string, seat int, reg time.Time) bool {
  503. if strings.Contains(vehicleType, "T") ||
  504. strings.Contains(vehicleType, "J") ||
  505. strings.Contains(vehicleType, "X") ||
  506. strings.Contains(vehicleType, "Z") {
  507. return true
  508. }
  509. return false
  510. }
  511. func isRule6(useProperty string, vehicleType string, seat int, reg time.Time) bool {
  512. if useProperty == "O" ||
  513. useProperty == "P" ||
  514. useProperty == "Q" ||
  515. useProperty == "S" {
  516. return true
  517. }
  518. if vehicleType == "K18" ||
  519. vehicleType == "K28" ||
  520. vehicleType == "K38" {
  521. return true
  522. }
  523. return false
  524. }
  525. func isRule7(useProperty string, vehicleType string, seat int, reg time.Time, casualty bool) bool {
  526. if useProperty != "A" {
  527. return false
  528. }
  529. if seat >= 10 || seat <= 0 {
  530. return false
  531. }
  532. if casualty == true {
  533. return false
  534. }
  535. if reg.Year() < 2010 {
  536. return false
  537. }
  538. if reg.Year() == 2010 && reg.Month() < 9 {
  539. return false
  540. }
  541. if vehicleType == "K16" ||
  542. vehicleType == "K26" ||
  543. vehicleType == "K30" ||
  544. vehicleType == "K31" ||
  545. vehicleType == "K32" ||
  546. vehicleType == "K33" ||
  547. vehicleType == "K34" ||
  548. vehicleType == "K40" ||
  549. vehicleType == "K41" ||
  550. vehicleType == "K42" ||
  551. vehicleType == "K43" {
  552. return true
  553. }
  554. return false
  555. }
  556. func getRule(useProperty string, vehicleType string, seat int, reg time.Time, casualty bool) int {
  557. if isRule7(useProperty, vehicleType, seat, reg, casualty) {
  558. return rule7
  559. }
  560. if isRule1(useProperty, vehicleType, seat, reg) {
  561. return rule1
  562. }
  563. if isRule2(useProperty, vehicleType, seat, reg) {
  564. return rule2
  565. }
  566. if isRule3(useProperty, vehicleType, seat, reg) {
  567. return rule3
  568. }
  569. if isRule4(useProperty, vehicleType, seat, reg) {
  570. return rule4
  571. }
  572. if isRule5(useProperty, vehicleType, seat, reg) {
  573. return rule5
  574. }
  575. if isRule6(useProperty, vehicleType, seat, reg) {
  576. return rule6
  577. }
  578. return -1
  579. }
  580. func getAnnualInsepctionTimeInfoNew(vehicleType, initialRegistrationDate string, approvedNumber int, useProperty string, casualty, isExpire bool) (annualInfo GetAnnualInspectionTimeInfo, err error) {
  581. vehicleType = strings.ToUpper(vehicleType)
  582. useProperty = strings.ToUpper(useProperty)
  583. timeLayout := "2006-01-02"
  584. nowTime := time.Now()
  585. loc, _ := time.LoadLocation("Local")
  586. array := strings.Split(initialRegistrationDate, " ")
  587. if len(array) > 0 {
  588. initialRegistrationDate = array[0]
  589. }
  590. cdrqTime, err := time.ParseInLocation(timeLayout, initialRegistrationDate, loc)
  591. if err != nil {
  592. return annualInfo, err
  593. }
  594. rule := getRule(useProperty, vehicleType, approvedNumber, cdrqTime, casualty)
  595. if rule < 0 {
  596. return annualInfo, errors.AnnualRuleError
  597. }
  598. iterm, detail, err := calcAnnualExamTimeByRule(nowTime, cdrqTime, rule, isExpire)
  599. if err != nil {
  600. return annualInfo, err
  601. }
  602. array = strings.Split(iterm.Date, " ")
  603. annualInfo.TimeDate = fmt.Sprintf("%s到期", array[0])
  604. annualInfo.TimeRemain = fmt.Sprintf("剩%d天", iterm.Day)
  605. annualInfo.TimeYear = fmt.Sprintf("第%d年", detail.NumYear)
  606. annualInfo.Rule = detail.Desc
  607. if rule == rule7 {
  608. //可免检
  609. if strings.Contains(detail.Desc, "10年车龄2年年检1次") && !casualty {
  610. annualInfo.CanBeUncheck = true
  611. }
  612. }
  613. if iterm.Day < 90 {
  614. //距年审90天内可年审
  615. annualInfo.CanBeInspection = true
  616. }
  617. return annualInfo, nil
  618. }
  619. // 年检时间计算
  620. func calcAnnualExamTimeNew(reqMap map[string]interface{}, isExpire bool) {
  621. vehicleType, _ := getMapValueString("vehicle_type", reqMap)
  622. initialRegistrationDate, _ := getMapValueString("initial_registration_date", reqMap)
  623. insuranceFirstDate, _ := getMapValueString("insurance_first_date", reqMap)
  624. useProperty, _ := getMapValueString("use_property", reqMap)
  625. approvedNumber := getMapValueInt("approved_number", reqMap)
  626. if initialRegistrationDate != "" ||
  627. insuranceFirstDate != "" {
  628. VehicleType := "K33"
  629. if vehicleType != "" {
  630. VehicleType = vehicleType
  631. }
  632. InitialRegistrationDate := ""
  633. if initialRegistrationDate == "" {
  634. InitialRegistrationDate = insuranceFirstDate
  635. } else {
  636. InitialRegistrationDate = initialRegistrationDate
  637. }
  638. if len(InitialRegistrationDate) > DataLen {
  639. InitialRegistrationDate = InitialRegistrationDate[:DataLen]
  640. }
  641. annualInfo, err := getAnnualInsepctionTimeInfoNew(VehicleType, InitialRegistrationDate, approvedNumber, useProperty, false, isExpire)
  642. if err == nil {
  643. reqMap["inspection_result_effective_to"] = annualInfo.TimeDate[:DataLen] + " 00:00:00"
  644. }
  645. /*vReq := dip_service.ServiceGetAnnualInspectionTimeNewReq{VehicleType: VehicleType, RegDate: InitialRegistrationDate, SeatNumber: vehicle.ApprovedNumber, UseProperty: vehicle.UseProperty, Casualty: false, IsExpire: isExpire}
  646. vReply, err := rpc_apis.Service.ServiceGetAnnualInspectionTimeNew(context.Background(), &vReq)
  647. if err != nil || vReply.ErrCode != 0 {
  648. } else {
  649. vehicle.InspectionResultEffectiveTo = vReply.Info.TimeDate[:dutils.DataLen] + " 00:00:00"
  650. }*/
  651. }
  652. }
  653. func CalcAnnualExamExpireTime(reqMap map[string]interface{}) {
  654. inspectionResultEffectiveTo, isExist := getMapValueString("inspection_result_effective_to", reqMap)
  655. if !isExist {
  656. return
  657. }
  658. state, _ := getMapValueString("state", reqMap)
  659. if inspectionResultEffectiveTo != "" {
  660. lastTime, _ := time.Parse(DaySecLayout, inspectionResultEffectiveTo)
  661. if strings.Contains(state, "E") ||
  662. strings.Contains(state, "K") ||
  663. strings.Contains(state, "P") ||
  664. strings.Contains(state, "Q") {
  665. if lastTime.After(time.Now()) {
  666. calcAnnualExamTimeNew(reqMap, true)
  667. }
  668. } else if lastTime.Before(time.Now()) {
  669. calcAnnualExamTimeNew(reqMap, false)
  670. }
  671. } else {
  672. if strings.Contains(state, "E") ||
  673. strings.Contains(state, "K") ||
  674. strings.Contains(state, "P") ||
  675. strings.Contains(state, "Q") {
  676. calcAnnualExamTimeNew(reqMap, true)
  677. } else {
  678. calcAnnualExamTimeNew(reqMap, false)
  679. }
  680. }
  681. }