company_report_record.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package company
  2. import (
  3. "context"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/utils"
  7. "strconv"
  8. "sync"
  9. "time"
  10. )
  11. var companyLock sync.Mutex
  12. func AddCompanyReportRecord(cont context.Context) (err error) {
  13. companyLock.Lock()
  14. defer func() {
  15. if err != nil {
  16. fmt.Println("AddReportRecord Err:" + err.Error())
  17. }
  18. }()
  19. endDate := time.Now().Format(utils.FormatDate)
  20. productId := 1
  21. // 查找已经记录的ficc的记录
  22. existList, err := models.GetExistCompanyReportRecord(endDate, productId)
  23. if err != nil {
  24. return
  25. }
  26. existMap := make(map[int]*models.CompanyReportRecord)
  27. for _, v := range existList {
  28. existMap[v.CompanyId] = v
  29. }
  30. // 查找当前ficc的客户列表
  31. list, err := models.GetCompanyList(productId)
  32. if err != nil {
  33. return
  34. }
  35. for _, v := range list {
  36. if info, ok := existMap[v.CompanyId]; ok { //修改
  37. //校验字段是否变更,如果有变更,那么就更新
  38. if !checkUpdate(info, v) {
  39. continue
  40. }
  41. utils.FileLog.Info("update company:" + strconv.Itoa(v.CompanyId))
  42. recordWhereParams := make(map[string]interface{})
  43. //recordWhereParams["company_id"] = v.CompanyId
  44. //recordWhereParams["product_id"] = productId
  45. //recordWhereParams["create_date"] = endDate
  46. recordWhereParams["company_report_record_id"] = info.CompanyReportRecordId
  47. recordUpdateParams := make(map[string]interface{})
  48. recordUpdateParams["company_name"] = v.CompanyName
  49. recordUpdateParams["status"] = v.Status
  50. recordUpdateParams["start_date"] = v.StartDate
  51. recordUpdateParams["end_date"] = v.EndDate
  52. recordUpdateParams["seller_id"] = v.SellerId
  53. recordUpdateParams["seller_name"] = v.SellerName
  54. recordUpdateParams["credit_code"] = v.CreditCode
  55. recordUpdateParams["modify_time"] = time.Now()
  56. err = models.UpdateCompanyReportRecord(recordWhereParams, recordUpdateParams)
  57. } else { //新增
  58. utils.FileLog.Info("add company:" + strconv.Itoa(v.CompanyId))
  59. item := new(models.CompanyReportRecord)
  60. item.CompanyId = v.CompanyId
  61. item.ProductId = v.ProductId
  62. item.CompanyProductId = v.CompanyProductId
  63. item.CompanyName = v.CompanyName
  64. item.Status = v.Status
  65. item.StartDate = v.StartDate
  66. item.EndDate = v.EndDate
  67. item.SellerId = v.SellerId
  68. item.SellerName = v.SellerName
  69. item.CreditCode = v.CreditCode
  70. item.CreateDate = time.Now().Format(utils.FormatDate)
  71. item.CreateTime = time.Now()
  72. item.ModifyTime = time.Now()
  73. _, err = models.AddCompanyReportRecord(item)
  74. if err != nil {
  75. return
  76. }
  77. }
  78. }
  79. companyLock.Unlock()
  80. return err
  81. }
  82. // checkUpdate 判断是否要更新
  83. func checkUpdate(companyReportRecordInfo, companyInfo *models.CompanyReportRecord) bool {
  84. // 潜在用户过滤
  85. if companyInfo.CompanyId == 1 {
  86. return false
  87. }
  88. //状态
  89. if companyReportRecordInfo.Status != companyInfo.Status {
  90. return true
  91. }
  92. //客户名称
  93. if companyReportRecordInfo.CompanyName != companyInfo.CompanyName {
  94. return true
  95. }
  96. //有效开始时间
  97. if companyReportRecordInfo.StartDate != companyInfo.StartDate {
  98. return true
  99. }
  100. //有效结束时间
  101. if companyReportRecordInfo.EndDate != companyInfo.EndDate {
  102. return true
  103. }
  104. //所属销售
  105. if companyReportRecordInfo.SellerId != companyInfo.SellerId {
  106. return true
  107. }
  108. //社会信用码
  109. if companyReportRecordInfo.CreditCode != companyInfo.CreditCode {
  110. return true
  111. }
  112. return false
  113. }
  114. func AddRaiCompanyReportRecord(cont context.Context) (err error) {
  115. companyLock.Lock()
  116. defer func() {
  117. if err != nil {
  118. fmt.Println("AddReportRecord Err:" + err.Error())
  119. }
  120. }()
  121. endDate := time.Now().Format(utils.FormatDate)
  122. productId := 2
  123. // 查找已经记录的rai的记录
  124. existList, err := models.GetExistCompanyReportRecord(endDate, productId)
  125. if err != nil {
  126. return
  127. }
  128. existMap := make(map[int]*models.CompanyReportRecord)
  129. for _, v := range existList {
  130. existMap[v.CompanyId] = v
  131. }
  132. // 查找当前rai的客户列表
  133. list, err := models.GetCompanyList(productId)
  134. if err != nil {
  135. return
  136. }
  137. for _, v := range list {
  138. if info, ok := existMap[v.CompanyId]; ok { //修改
  139. //校验字段是否变更,如果有变更,那么就更新
  140. if !checkUpdate(info, v) {
  141. continue
  142. }
  143. utils.FileLog.Info("update company:" + strconv.Itoa(v.CompanyId))
  144. recordWhereParams := make(map[string]interface{})
  145. //recordWhereParams["company_id"] = v.CompanyId
  146. //recordWhereParams["product_id"] = productId
  147. //recordWhereParams["create_date"] = endDate
  148. recordWhereParams["company_report_record_id"] = info.CompanyReportRecordId
  149. recordUpdateParams := make(map[string]interface{})
  150. recordUpdateParams["company_name"] = v.CompanyName
  151. recordUpdateParams["status"] = v.Status
  152. recordUpdateParams["start_date"] = v.StartDate
  153. recordUpdateParams["end_date"] = v.EndDate
  154. recordUpdateParams["seller_id"] = v.SellerId
  155. recordUpdateParams["seller_name"] = v.SellerName
  156. recordUpdateParams["credit_code"] = v.CreditCode
  157. recordUpdateParams["modify_time"] = time.Now()
  158. err = models.UpdateCompanyReportRecord(recordWhereParams, recordUpdateParams)
  159. } else { //新增
  160. utils.FileLog.Info("add company:" + strconv.Itoa(v.CompanyId))
  161. item := new(models.CompanyReportRecord)
  162. item.CompanyId = v.CompanyId
  163. item.ProductId = v.ProductId
  164. item.CompanyProductId = v.CompanyProductId
  165. item.CompanyName = v.CompanyName
  166. item.Status = v.Status
  167. item.StartDate = v.StartDate
  168. item.EndDate = v.EndDate
  169. item.SellerId = v.SellerId
  170. item.SellerName = v.SellerName
  171. item.CreditCode = v.CreditCode
  172. item.CreateDate = time.Now().Format(utils.FormatDate)
  173. item.CreateTime = time.Now()
  174. item.ModifyTime = time.Now()
  175. _, err = models.AddCompanyReportRecord(item)
  176. if err != nil {
  177. return
  178. }
  179. }
  180. }
  181. companyLock.Unlock()
  182. return err
  183. }