xingzai 5 ヶ月 前
コミット
eb16d153ee

+ 30 - 0
models/article_history_record_all.go

@@ -24,6 +24,8 @@ type CygxArticleHistoryRecordAll struct {
 	Platfor          int       `description:"PV阅读记录来源,1:查研观向,2:策略平台"`
 	IsDel            int       `description:"是否删除"`
 	RegisterPlatform int       `description:"来源"`
+	CompanyStatus    string    `description:"公司状态"`
+	SellerName       string    `description:"所属销售"`
 }
 
 // 获取数量
@@ -82,3 +84,31 @@ func GetCygxArticleHistoryRecordAllListNoLimit(condition string, pars []interfac
 	_, err = o.Raw(sql, pars).QueryRows(&items)
 	return
 }
+
+// AddCygxArticleHistoryRecordAllMulti 批量添加
+func AddCygxArticleHistoryRecordAllMulti(itemsCelue []*CygxCelueArticleHistoryRecord, items []*CygxArticleHistoryRecordAll) (err error) {
+	o, err := orm.NewOrm().Begin()
+	if err != nil {
+		return
+	}
+	defer func() {
+		if err == nil {
+			o.Commit()
+		} else {
+			o.Rollback()
+		}
+	}()
+
+	//批量插入新的关联数据
+	if len(itemsCelue) > 0 {
+		//批量添加流水信息
+		_, err = o.InsertMulti(len(itemsCelue), itemsCelue)
+	}
+
+	//批量插入新的关联数据
+	if len(items) > 0 {
+		//批量添加流水信息
+		_, err = o.InsertMulti(len(items), items)
+	}
+	return
+}

+ 14 - 1
models/celue_article_history_record.go

@@ -3,6 +3,7 @@ package models
 import (
 	"fmt"
 	"github.com/beego/beego/v2/client/orm"
+	"hongze/hongze_cygx/utils"
 	"strconv"
 	"time"
 )
@@ -38,6 +39,16 @@ func GetCeLueArticleCountById(celueHistoryId int) (count int, err error) {
 	return
 }
 
+func GetCeLueArticleListByIds(celueHistoryIds []int) (items []*CygxArticleHistoryRecordAll, err error) {
+	if len(celueHistoryIds) == 0 {
+		return
+	}
+	o := orm.NewOrm()
+	sql := `SELECT celue_history_id FROM cygx_celue_article_history_record WHERE celue_history_id IN (` + utils.GetOrmInReplace(len(celueHistoryIds)) + `) `
+	_, err = o.Raw(sql, celueHistoryIds).QueryRows(&items)
+	return
+}
+
 type CygxCelueArticleHistoryRecord struct {
 	Id             int       `orm:"column(id);pk"`
 	ArticleId      string    `description:"文章ID"`
@@ -47,9 +58,11 @@ type CygxCelueArticleHistoryRecord struct {
 	Mobile         string    `description:"手机号"`
 	CompanyName    string    `description:"公司名称"`
 	RealName       string    `description:"用户姓名"`
+	CompanyStatus  string    `description:"公司状态"`
+	SellerName     string    `description:"所属销售"`
 }
 
-//新增
+// 新增
 func AddCeLueArticle(item *CygxCelueArticleHistoryRecord, mapMobileArticleId map[string]int) (lastId int64, err error) {
 	o, err := orm.NewOrm().Begin()
 	if err != nil {

+ 34 - 0
models/company/company_product.go

@@ -119,6 +119,40 @@ func GetCompanyProductAndCompanyListByCondition(condition string, pars []interfa
 	return
 }
 
+type CompanyProductUser struct {
+	CompanyId   int    `description:"客户id"`
+	ProductId   int    `description:"产品id"`
+	CompanyName string `description:"客户名称"`
+	Status      string `description:"客户状态"`
+	SellerId    int    `description:"销售id"`
+	SellerName  string `description:"销售名称"`
+	Mobile      string `description:"手机号"`
+	UserId      int
+	RealName    string `description:"用户实际名称"`
+	Email       string `description:"邮箱"`
+}
+
+// 获取列表
+func GetCompanyProductListByMobiles(mobiles []string) (items []*CompanyProductUser, err error) {
+	lenArr := len(mobiles)
+	if lenArr == 0 {
+		return
+	}
+	o := orm.NewOrmUsingDB("weekly_report")
+	sql := ` SELECT
+			u.mobile,
+			p.seller_name,
+			p.status,
+			c.company_name
+		FROM
+			company_product AS p
+			INNER JOIN company AS c ON c.company_id = p.company_id 
+			INNER JOIN wx_user AS u ON u.company_id = p.company_id 
+		WHERE u.mobile IN (` + utils.GetOrmInReplace(lenArr) + `) `
+	_, err = o.Raw(sql, mobiles).QueryRows(&items)
+	return
+}
+
 // 获取权益客户下的正式、试用、永续客户。
 func GetRaiCompanyProductCompanyids() (items []*CompanyProduct, err error) {
 	sql := ` SELECT  company_id  FROM  company_product WHERE  product_id = 2 AND status IN  ('正式','试用','永续') AND  company_id > 1  `

+ 117 - 32
services/article.go

@@ -8,6 +8,7 @@ import (
 	"github.com/PuerkitoBio/goquery"
 	"github.com/beego/beego/v2/client/orm"
 	"hongze/hongze_cygx/models"
+	"hongze/hongze_cygx/models/company"
 	"hongze/hongze_cygx/utils"
 	"html"
 	"net/url"
@@ -583,6 +584,7 @@ func PushViewRecordNewRedisData(reportViewRecord *ReportViewRecord, companyId in
 
 // 获取策略平台报告阅读数据
 func GetCeLueArticlePv(cont context.Context) (err error) {
+	//func GetCeLueArticlePv() (err error) {
 	defer func() {
 		if err != nil {
 			go utils.SendAlarmMsg("同步策略平台阅读数据失败", 2)
@@ -620,55 +622,138 @@ func GetCeLueArticlePv(cont context.Context) (err error) {
 			mapMobileArticleId[fmt.Sprint(v.Mobile, "_", v.ArticleId)] = v.ArticleId
 		}
 	}
+
+	var celueHistoryIds []int
+	var mobiles []string
+	for _, v := range chartResult.Data {
+		celueHistoryIds = append(celueHistoryIds, v.CelueHistoryId)
+		mobiles = append(mobiles, v.Mobile)
+	}
+
+	listcelueHistory, e := models.GetCeLueArticleListByIds(celueHistoryIds)
+	if e != nil {
+		err = errors.New("GetCeLueArticleListByIds, Err: " + e.Error())
+		return
+	}
+
+	mapCelueHistoryIds := make(map[int]int)
+	for _, v := range listcelueHistory {
+		mapCelueHistoryIds[v.CelueHistoryId] = v.CelueHistoryId
+	}
+
+	listMobileCompany, e := company.GetCompanyProductListByMobiles(mobiles)
+	if e != nil {
+		err = errors.New("GetCompanyProductListByMobiles, Err: " + e.Error())
+		return
+	}
+
+	mapUserCompany := make(map[string]*company.CompanyProductUser)
+
+	for _, v := range listMobileCompany {
+		mapUserCompany[v.Mobile] = v
+	}
+
+	var celueArticleHistoryItems []*models.CygxCelueArticleHistoryRecord
+	var allRecordItems []*models.CygxArticleHistoryRecordAll
 	for _, v := range chartResult.Data {
+		articleId, _ := strconv.Atoi(v.ArticleId)
+		if articleId == 0 {
+			continue
+		}
 		//fmt.Println(v.ArticleId)
 		item := new(models.CygxCelueArticleHistoryRecord)
 		item.CelueHistoryId = v.CelueHistoryId
 		item.Mobile = v.Mobile
 		item.ArticleId = v.ArticleId
+		if v.CompanyName != nil {
+			item.CompanyName = v.CompanyName.RealName
+		}
 		if v.CrmUser != nil {
 			item.RealName = v.CrmUser.RealName
 		}
-		if v.CompanyName != nil {
-			item.CompanyName = v.CompanyName.RealName
+
+		if mapUserCompany[v.Mobile] != nil {
+			mapUser := mapUserCompany[v.Mobile]
+			item.CompanyName = mapUser.CompanyName
+			item.CompanyStatus = mapUser.Status
+			item.SellerName = mapUser.SellerName
 		}
+
 		item.CreateDateApi = time.Now()
 		t1, _ := time.Parse("2006-01-02T15:04:05Z", v.CreateDate)
 		item.CreateTime = t1.Add(+time.Hour * 8).Format(utils.FormatDateTime)
-		count, err := models.GetCeLueArticleCountById(v.CelueHistoryId)
-		if err != nil && err.Error() != utils.ErrNoRow() {
-			return err
-		}
-		if count == 0 {
-			_, err := models.AddCeLueArticle(item, mapMobileArticleId)
-			if err != nil {
-				fmt.Println(err)
-				return err
-			}
-		}
+		//email=?,company_id=?,company_name=?,user_id=?,real_name=?
+		if mapCelueHistoryIds[v.CelueHistoryId] == 0 {
+			celueArticleHistoryItems = append(celueArticleHistoryItems, item)
+			//写入记录到总的统计表
+			record := new(models.CygxArticleHistoryRecordAll)
+
+			record.ArticleId = articleId
+			record.CelueHistoryId = item.CelueHistoryId
+			record.CreateTime = item.CreateTime
+			record.ModifyTime = time.Now()
+			record.CreateDateApi = time.Now()
+
+			if mapUserCompany[v.Mobile] != nil {
+				mapUser := mapUserCompany[v.Mobile]
+				record.Mobile = mapUser.Mobile
+				record.CompanyName = mapUser.CompanyName
+				record.CompanyStatus = mapUser.Status
+				record.SellerName = mapUser.SellerName
+				record.CompanyId = mapUser.CompanyId
+				record.RealName = mapUser.RealName
+				record.UserId = mapUser.UserId
+				record.Email = mapUser.Email
+			}
+			record.Platfor = 2
+			record.Source = "CELUE"
+			if mapMobileArticleId[fmt.Sprint(item.Mobile, "_", item.ArticleId)] == articleId {
+				record.IsDel = 1
+			}
+			allRecordItems = append(allRecordItems, record)
+		}
+		//count, err := models.GetCeLueArticleCountById(v.CelueHistoryId)
+		//if err != nil && err.Error() != utils.ErrNoRow() {
+		//	return err
+		//}
+		//if count == 0 {
+		//	_, err := models.AddCeLueArticle(item, mapMobileArticleId)
+		//	if err != nil {
+		//		fmt.Println(err)
+		//		return err
+		//	}
+		//}
 	}
 
-	//处理同步过来的阅读记录所属用户
-	var condition string
-	condition = ` AND create_time  > ` + "'" + startTime + "'"
-	listArticlePv, err := models.GetArticleHistoryRecordAllByMobileList(condition)
-	if err != nil {
-		fmt.Println("GetArticleHistoryRecordAllByMobileList ,Err" + err.Error())
-	}
-	for _, v := range listArticlePv {
-		if v.Mobile != "" {
-			user, err := models.GetWxUserItemByMobile(v.Mobile)
-			if err != nil && err.Error() != utils.ErrNoRow() {
-				fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
-			}
-			if user != nil {
-				err = models.UpdateCygxArticleHistoryRecordAll(user)
-				if err != nil {
-					fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
-				}
-			}
+	if len(celueArticleHistoryItems) > 0 {
+		e = models.AddCygxArticleHistoryRecordAllMulti(celueArticleHistoryItems, allRecordItems)
+		if e != nil {
+			err = errors.New("AddCygxArticleHistoryRecordAllMulti, Err: " + e.Error())
+			return
 		}
 	}
+
+	//处理同步过来的阅读记录所属用户
+	//var condition string
+	//condition = ` AND create_time  > ` + "'" + startTime + "'"
+	//listArticlePv, err := models.GetArticleHistoryRecordAllByMobileList(condition)
+	//if err != nil {
+	//	fmt.Println("GetArticleHistoryRecordAllByMobileList ,Err" + err.Error())
+	//}
+	//for _, v := range listArticlePv {
+	//	if v.Mobile != "" {
+	//		user, err := models.GetWxUserItemByMobile(v.Mobile)
+	//		if err != nil && err.Error() != utils.ErrNoRow() {
+	//			fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
+	//		}
+	//		if user != nil {
+	//			err = models.UpdateCygxArticleHistoryRecordAll(user)
+	//			if err != nil {
+	//				fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
+	//			}
+	//		}
+	//	}
+	//}
 	return
 
 }