uniquecheck.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package service
  2. import (
  3. "sync"
  4. "go-common/app/interface/main/mcn/model/mcnmodel"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. "go-common/library/time"
  8. )
  9. var (
  10. lastLoadMcnUniqueTime time.Time
  11. )
  12. //UniqueCheck check unique
  13. type UniqueCheck struct {
  14. // all the values is mcn id
  15. PhoneMap map[string]int64
  16. IDCardMap map[string]int64
  17. CompanyNameMap map[string]int64
  18. CompanyLicenseIDMap map[string]int64
  19. lock sync.Mutex
  20. }
  21. //NewUniqueCheck new checker
  22. func NewUniqueCheck() *UniqueCheck {
  23. return &UniqueCheck{
  24. PhoneMap: make(map[string]int64),
  25. IDCardMap: make(map[string]int64),
  26. CompanyNameMap: make(map[string]int64),
  27. CompanyLicenseIDMap: make(map[string]int64),
  28. }
  29. }
  30. //CheckIsUniqe check is unique
  31. func (u *UniqueCheck) CheckIsUniqe(req *mcnmodel.McnApplyReq) (err error) {
  32. if req == nil {
  33. return
  34. }
  35. u.lock.Lock()
  36. defer u.lock.Unlock()
  37. if v, ok := u.PhoneMap[req.ContactPhone]; ok {
  38. if req.McnMid != v {
  39. err = ecode.MCNUpBindUpDuplicatePhone
  40. return
  41. }
  42. }
  43. if v, ok := u.IDCardMap[req.ContactIdcard]; ok {
  44. if req.McnMid != v {
  45. err = ecode.MCNUpBindUpDuplicateIDCard
  46. return
  47. }
  48. }
  49. if v, ok := u.CompanyNameMap[req.CompanyName]; ok {
  50. if req.McnMid != v {
  51. err = ecode.MCNUpBindUpDuplicateCompanyName
  52. return
  53. }
  54. }
  55. if v, ok := u.CompanyLicenseIDMap[req.CompanyLicenseID]; ok {
  56. if req.McnMid != v {
  57. err = ecode.MCNUpBindUpDuplicateCompanyLicenseID
  58. return
  59. }
  60. }
  61. return
  62. }
  63. //AddItem add item from db
  64. func (u *UniqueCheck) AddItem(sign *mcnmodel.McnSign) {
  65. u.lock.Lock()
  66. defer u.lock.Unlock()
  67. u.PhoneMap[sign.ContactPhone] = sign.McnMid
  68. u.IDCardMap[sign.ContactIdcard] = sign.McnMid
  69. u.CompanyNameMap[sign.CompanyName] = sign.McnMid
  70. u.CompanyLicenseIDMap[sign.CompanyLicenseID] = sign.McnMid
  71. }
  72. func (s *Service) loadMcnUniqueCache() {
  73. var list []*mcnmodel.McnSign
  74. var err = s.mcndao.GetMcnDB().
  75. Select("mcn_mid, company_name, company_license_id, contact_idcard, contact_phone, mtime").
  76. Where("mtime>?", lastLoadMcnUniqueTime).
  77. Find(&list).Error
  78. if err != nil {
  79. log.Warn("cannot get unique, err=%s", err)
  80. return
  81. }
  82. for _, v := range list {
  83. s.uniqueChecker.AddItem(v)
  84. if lastLoadMcnUniqueTime < v.Mtime {
  85. lastLoadMcnUniqueTime = v.Mtime
  86. }
  87. }
  88. }