package company import ( "context" "fmt" "hongze/hongze_task/models" "hongze/hongze_task/utils" "strconv" "sync" "time" ) var companyLock sync.Mutex func AddCompanyReportRecord(cont context.Context) (err error) { companyLock.Lock() defer func() { if err != nil { fmt.Println("AddReportRecord Err:" + err.Error()) } }() endDate := time.Now().Format(utils.FormatDate) productId := 1 // 查找已经记录的ficc的记录 existList, err := models.GetExistCompanyReportRecord(endDate, productId) if err != nil { return } existMap := make(map[int]*models.CompanyReportRecord) for _, v := range existList { existMap[v.CompanyId] = v } // 查找当前ficc的客户列表 list, err := models.GetCompanyList(productId) if err != nil { return } for _, v := range list { if info, ok := existMap[v.CompanyId]; ok { //修改 //校验字段是否变更,如果有变更,那么就更新 if !checkUpdate(info, v) { continue } utils.FileLog.Info("update company:" + strconv.Itoa(v.CompanyId)) recordWhereParams := make(map[string]interface{}) //recordWhereParams["company_id"] = v.CompanyId //recordWhereParams["product_id"] = productId //recordWhereParams["create_date"] = endDate recordWhereParams["company_report_record_id"] = info.CompanyReportRecordId recordUpdateParams := make(map[string]interface{}) recordUpdateParams["company_name"] = v.CompanyName recordUpdateParams["status"] = v.Status recordUpdateParams["start_date"] = v.StartDate recordUpdateParams["end_date"] = v.EndDate recordUpdateParams["seller_id"] = v.SellerId recordUpdateParams["seller_name"] = v.SellerName recordUpdateParams["credit_code"] = v.CreditCode recordUpdateParams["modify_time"] = time.Now() err = models.UpdateCompanyReportRecord(recordWhereParams, recordUpdateParams) } else { //新增 utils.FileLog.Info("add company:" + strconv.Itoa(v.CompanyId)) item := new(models.CompanyReportRecord) item.CompanyId = v.CompanyId item.ProductId = v.ProductId item.CompanyProductId = v.CompanyProductId item.CompanyName = v.CompanyName item.Status = v.Status item.StartDate = v.StartDate item.EndDate = v.EndDate item.SellerId = v.SellerId item.SellerName = v.SellerName item.CreditCode = v.CreditCode item.CreateDate = time.Now().Format(utils.FormatDate) item.CreateTime = time.Now() item.ModifyTime = time.Now() _, err = models.AddCompanyReportRecord(item) if err != nil { return } } } companyLock.Unlock() return err } // checkUpdate 判断是否要更新 func checkUpdate(companyReportRecordInfo, companyInfo *models.CompanyReportRecord) bool { // 潜在用户过滤 if companyInfo.CompanyId == 1 { return false } //状态 if companyReportRecordInfo.Status != companyInfo.Status { return true } //客户名称 if companyReportRecordInfo.CompanyName != companyInfo.CompanyName { return true } //有效开始时间 if companyReportRecordInfo.StartDate != companyInfo.StartDate { return true } //有效结束时间 if companyReportRecordInfo.EndDate != companyInfo.EndDate { return true } //所属销售 if companyReportRecordInfo.SellerId != companyInfo.SellerId { return true } //社会信用码 if companyReportRecordInfo.CreditCode != companyInfo.CreditCode { return true } return false } func AddRaiCompanyReportRecord(cont context.Context) (err error) { companyLock.Lock() defer func() { if err != nil { fmt.Println("AddReportRecord Err:" + err.Error()) } }() endDate := time.Now().Format(utils.FormatDate) productId := 2 // 查找已经记录的rai的记录 existList, err := models.GetExistCompanyReportRecord(endDate, productId) if err != nil { return } existMap := make(map[int]*models.CompanyReportRecord) for _, v := range existList { existMap[v.CompanyId] = v } // 查找当前rai的客户列表 list, err := models.GetCompanyList(productId) if err != nil { return } for _, v := range list { if info, ok := existMap[v.CompanyId]; ok { //修改 //校验字段是否变更,如果有变更,那么就更新 if !checkUpdate(info, v) { continue } utils.FileLog.Info("update company:" + strconv.Itoa(v.CompanyId)) recordWhereParams := make(map[string]interface{}) //recordWhereParams["company_id"] = v.CompanyId //recordWhereParams["product_id"] = productId //recordWhereParams["create_date"] = endDate recordWhereParams["company_report_record_id"] = info.CompanyReportRecordId recordUpdateParams := make(map[string]interface{}) recordUpdateParams["company_name"] = v.CompanyName recordUpdateParams["status"] = v.Status recordUpdateParams["start_date"] = v.StartDate recordUpdateParams["end_date"] = v.EndDate recordUpdateParams["seller_id"] = v.SellerId recordUpdateParams["seller_name"] = v.SellerName recordUpdateParams["credit_code"] = v.CreditCode recordUpdateParams["modify_time"] = time.Now() err = models.UpdateCompanyReportRecord(recordWhereParams, recordUpdateParams) } else { //新增 utils.FileLog.Info("add company:" + strconv.Itoa(v.CompanyId)) item := new(models.CompanyReportRecord) item.CompanyId = v.CompanyId item.ProductId = v.ProductId item.CompanyProductId = v.CompanyProductId item.CompanyName = v.CompanyName item.Status = v.Status item.StartDate = v.StartDate item.EndDate = v.EndDate item.SellerId = v.SellerId item.SellerName = v.SellerName item.CreditCode = v.CreditCode item.CreateDate = time.Now().Format(utils.FormatDate) item.CreateTime = time.Now() item.ModifyTime = time.Now() _, err = models.AddCompanyReportRecord(item) if err != nil { return } } } companyLock.Unlock() return err }