123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 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
- }
|