xingzai před 1 rokem
rodič
revize
d6dc9ebf8d

+ 6 - 1
controllers/company.go

@@ -4821,6 +4821,11 @@ func (this *CompanyController) Receive() {
 			go company.AddCompanyProductTryOutUpdateLog(companyProductTryOutUpdateLog, companyReportPermissionList)
 		}
 
+		//流失转试用,企业下面的用户自动添加产业关注
+		{
+			go cygxService.AddUserFllowCompanyLossToTryOut(req.CompanyId)
+		}
+
 	} else {
 		//跨部门领取
 		var startDateTime, endDateTime time.Time
@@ -9290,7 +9295,7 @@ func (this *CompanyTodoController) CompanyActivitySpecialPointsBill() {
 		} else {
 			if v.DoType == 1 && v.Way != 4 && v.Way != 3 {
 				item.Content = item.ActivityName + "--报名"
-			} else if v.DoType == 2 && v.Way != 4 && v.Way != 3  {
+			} else if v.DoType == 2 && v.Way != 4 && v.Way != 3 {
 				item.Content = item.ActivityName + "--取消报名"
 			} else if v.Way == 4 {
 				item.Content = item.ActivityName + "--活动取消"

+ 40 - 0
models/cygx/cygx_user.go

@@ -139,6 +139,46 @@ func GetCygxCompanyUserList(userCondition, keyWord, kwywordcondition, condition,
 	return
 }
 
+type CygxCompanyUserAndSellerResp struct {
+	UserId      int    `description:"用户ID"`
+	Mobile      string `description:"手机号"`
+	Email       string `description:"邮箱"`
+	CompanyId   int    `description:"公司id"`
+	CompanyName string `description:"公司名称"`
+	RealName    string `description:"姓名"`
+	SellerName  string `description:"销售名称"`
+}
+
+// 给所有已绑定手机号的客户(除流失)列表
+func GetCygxCompanyUserListByNoLoss() (items []*CygxCompanyUserAndSellerResp, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT
+			u.user_id,
+			u.mobile,
+			u.email,
+			u.real_name,
+			c.company_name,
+			c.company_id,
+			m.real_name AS seller_name 
+		FROM
+			wx_user AS u
+			INNER JOIN company AS c ON c.company_id = u.company_id
+			INNER JOIN company_product AS cp ON cp.company_id = c.company_id
+			INNER JOIN admin AS m ON m.admin_id = cp.seller_id
+			INNER JOIN user_seller_relation AS sr ON sr.user_id = u.user_id 
+		WHERE
+			1 = 1 
+			AND c.enabled = 1 
+			AND cp.STATUS != '流失' 
+			AND cp.product_id = 2 
+			AND sr.product_id = 2 
+		GROUP BY
+			u.user_id `
+
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}
+
 // 对于上面的SQL的拆分优化查询速度
 func GetCygxCompanyUserListSplit(userIds string) (items []*CygxCompanyUser, err error) {
 	o := orm.NewOrm()

+ 21 - 5
models/cygx/industry_fllow.go

@@ -21,7 +21,23 @@ type CygxIndustryFllow struct {
 	Source                 int       `description:"来源1查研观向,2查研观向小助手,3勾选全部赛道的用户进行自动关注"`
 }
 
-//关注产业列表
+// 列表
+func GetCygxIndustryFllowList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxIndustryFllow, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT * FROM cygx_industry_fllow WHERE 1= 1 `
+	if condition != "" {
+		sql += condition
+	}
+	if startSize+pageSize > 0 {
+		sql += ` LIMIT ?,?  `
+		_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
+	} else {
+		_, err = o.Raw(sql, pars).QueryRows(&items)
+	}
+	return
+}
+
+// 关注产业列表
 func GetCygxIndustryFllowByUser(uid, startSize, pageSize int) (items []*UserInteraction, err error) {
 	o := orm.NewOrm()
 	sql := `SELECT
@@ -40,7 +56,7 @@ func GetCygxIndustryFllowByUser(uid, startSize, pageSize int) (items []*UserInte
 	return
 }
 
-//关注产业数量
+// 关注产业数量
 func GetCygxIndustryFllowCount(uid int) (count int, err error) {
 	o := orm.NewOrm()
 	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_industry_fllow  WHERE   user_id = ?  AND type = 1  `
@@ -48,7 +64,7 @@ func GetCygxIndustryFllowCount(uid int) (count int, err error) {
 	return
 }
 
-//判断用户是否关注这些产业ID
+// 判断用户是否关注这些产业ID
 func GetCygxIndustryFllowCountByUser(uid int, industrialManagementd string) (count int, err error) {
 	o := orm.NewOrm()
 	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_industry_fllow  WHERE   user_id = ?  AND type = 1 AND  industrial_management_id IN (` + industrialManagementd + `)`
@@ -56,7 +72,7 @@ func GetCygxIndustryFllowCountByUser(uid int, industrialManagementd string) (cou
 	return
 }
 
-//GetUserFllowIndustrialListByUserIdAndIndustrial 通过用户ID 跟产业ID获取用户关注的产业列表
+// GetUserFllowIndustrialListByUserIdAndIndustrial 通过用户ID 跟产业ID获取用户关注的产业列表
 func GetUserFllowIndustrialListByUserIdAndIndustrial(userIds, industrials string) (items []*CygxIndustryFllow, err error) {
 	o := orm.NewOrm()
 	sql := `SELECT * FROM cygx_industry_fllow 
@@ -67,7 +83,7 @@ func GetUserFllowIndustrialListByUserIdAndIndustrial(userIds, industrials string
 	return
 }
 
-//批量添加
+// 批量添加
 func AddCygxIndustryFllowMulti(items []*CygxIndustryFllow) (err error) {
 	o := orm.NewOrm()
 	if len(items) > 0 {

+ 37 - 23
services/cygx/industrial_management.go

@@ -3,7 +3,6 @@ package cygx
 import (
 	"errors"
 	"fmt"
-	"hongze/hz_crm_api/models"
 	"hongze/hz_crm_api/models/cygx"
 	"hongze/hz_crm_api/services/alarm_msg"
 	"hongze/hz_crm_api/utils"
@@ -68,27 +67,30 @@ func AddUserIndustryFllowByNewId(industrialManagementId int) (err error) {
 		}
 	}()
 	//获取小助手提交过勾选项的用户
-	var condition string
-	condition = " AND  is_refuse = 0 "
-	chooseSendtList, e := cygx.GetCygxXzsChooseSend(condition)
-	if e != nil {
-		err = errors.New("GetCygxXzsChooseSend , Err: " + e.Error())
-		return
-	}
-	var mobiles []string
-	for _, v := range chooseSendtList {
-		if v.Mobile != "" {
-			mobiles = append(mobiles, v.Mobile)
-		}
-	}
+	//var condition string
+	//condition = " AND  is_refuse = 0 "
+	//chooseSendtList, e := cygx.GetCygxXzsChooseSend(condition)
+	//if e != nil {
+	//	err = errors.New("GetCygxXzsChooseSend , Err: " + e.Error())
+	//	return
+	//}
+	//var mobiles []string
+	//for _, v := range chooseSendtList {
+	//	if v.Mobile != "" {
+	//		mobiles = append(mobiles, v.Mobile)
+	//	}
+	//}
 
-	userList, e := models.GetWxUserListByUserMobileHaveCompany(mobiles)
+	//userList, e := models.GetWxUserListByUserMobileHaveCompany(mobiles)
+	userList, e := cygx.GetCygxCompanyUserListByNoLoss() //需求池859 2023-08-29 目前数量大概在 16000条
 	if e != nil {
 		err = errors.New("GetWxUserListByUserMobileHaveCompany" + e.Error())
 		return
 	}
+	var numk int
 	var industryFllowItems []*cygx.CygxIndustryFllow
 	for _, vUser := range userList {
+		numk++
 		item := new(cygx.CygxIndustryFllow)
 		item.IndustrialManagementId = industrialManagementId
 		item.UserId = vUser.UserId
@@ -102,16 +104,28 @@ func AddUserIndustryFllowByNewId(industrialManagementId int) (err error) {
 		item.CreateTime = time.Now()
 		item.ModifyTime = time.Now()
 		industryFllowItems = append(industryFllowItems, item)
+		//一次插入五千条
+		if numk%5000 == 0 {
+			e = cygx.AddCygxIndustryFllowMulti(industryFllowItems)
+			if e != nil {
+				err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
+				return
+			}
+			industryFllowItems = make([]*cygx.CygxIndustryFllow, 0)
+			numk = 0
+		}
 	}
-	e = cygx.AddCygxIndustryFllowMulti(industryFllowItems)
-	if e != nil {
-		err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
-		return
+	if len(industryFllowItems) > 0 {
+		e = cygx.AddCygxIndustryFllowMulti(industryFllowItems)
+		if e != nil {
+			err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
+			return
+		}
 	}
 
-	//更新用户关注产业的标签
-	for _, v := range industryFllowItems {
-		IndustryFllowUserLabelLogAdd(v.IndustrialManagementId, 0, v.UserId)
-	}
+	////更新用户关注产业的标签
+	//for _, v := range industryFllowItems {
+	//	IndustryFllowUserLabelLogAdd(v.IndustrialManagementId, 0, v.UserId)
+	//}
 	return
 }

+ 122 - 0
services/cygx/industry_fllow.go

@@ -0,0 +1,122 @@
+package cygx
+
+import (
+	"errors"
+	"fmt"
+	"hongze/hz_crm_api/models"
+	"hongze/hz_crm_api/models/cygx"
+	"hongze/hz_crm_api/services/alarm_msg"
+	"strconv"
+	"time"
+)
+
+//func init() {
+//	AddUserFllowCompanyLossToTryOut(1431)
+//}
+
+// 流失客户转试用给下面的用户自动添加产业关注
+func AddUserFllowCompanyLossToTryOut(companyId int) {
+	var err error
+	defer func() {
+		if err != nil {
+			fmt.Println(err)
+			go alarm_msg.SendAlarmMsg("流失客户转试用给下面的用户自动添加产业关注。失败 ErrMsg:"+err.Error()+"companyId :"+strconv.Itoa(companyId), 2)
+		}
+	}()
+
+	//获取机构下所有的用户
+	userList, e := models.GetWxUserListCompanyId(companyId)
+	if e != nil {
+		err = errors.New("GetWxUserListByUserMobileHaveCompany" + e.Error())
+		return
+	}
+	var condition string
+	var pars []interface{}
+	//所有的产业
+	industrNamelist, e := cygx.GetTopOneMonthArtReadNumIndustryAll(condition, pars)
+	if e != nil {
+		err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
+		return
+	}
+	condition = " AND company_id =  ? "
+	pars = append(pars, companyId)
+	userFllowList, e := cygx.GetCygxIndustryFllowList(condition, pars, 0, 0)
+	if e != nil {
+		err = errors.New("GetCygxIndustryFllowList, Err: " + e.Error())
+		return
+	}
+	mapUserFllow := make(map[string]bool) // 用户已经关注了某些产业
+
+	for _, v := range userFllowList {
+		mapUserFllow[fmt.Sprint("Uid_", v.UserId, "IndustrialManagementId_", v.IndustrialManagementId)] = true //用户ID与产业ID组成唯一索引Map
+	}
+
+	var numk int
+	var industryFllowItems []*cygx.CygxIndustryFllow
+	var Items []*cygx.CygxIndustryFllow
+
+	for _, vUser := range userList {
+		for _, vIndustry := range industrNamelist {
+			if mapUserFllow[fmt.Sprint("Uid_", vUser.UserId, "IndustrialManagementId_", vIndustry.IndustrialManagementId)] {
+				continue
+			}
+			item := new(cygx.CygxIndustryFllow)
+			item.IndustrialManagementId = vIndustry.IndustrialManagementId
+			item.UserId = vUser.UserId
+			item.Email = vUser.Email
+			item.Mobile = vUser.Mobile
+			item.RealName = vUser.RealName
+			item.CompanyId = vUser.CompanyId
+			item.CompanyName = vUser.CompanyName
+			item.Source = 3 // 通过程序或者管理员手动添加的关注
+			item.Type = 1
+			item.CreateTime = time.Now()
+			item.ModifyTime = time.Now()
+			Items = append(Items, item)
+		}
+	}
+
+	//如果小于五千条就直接插入,大于五千条就批量插入
+	if len(Items) < 5000 {
+		e = cygx.AddCygxIndustryFllowMulti(Items)
+		if e != nil {
+			err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
+			return
+		}
+	} else {
+		for _, vUser := range Items {
+			numk++
+			item := new(cygx.CygxIndustryFllow)
+			item.IndustrialManagementId = vUser.IndustrialManagementId
+			item.UserId = vUser.UserId
+			item.Email = vUser.Email
+			item.Mobile = vUser.Mobile
+			item.RealName = vUser.RealName
+			item.CompanyId = vUser.CompanyId
+			item.CompanyName = vUser.CompanyName
+			item.Source = 3 // 通过程序或者管理员手动添加的关注
+			item.Type = 1
+			item.CreateTime = time.Now()
+			item.ModifyTime = time.Now()
+			industryFllowItems = append(industryFllowItems, item)
+			//一次插入五千条
+			if numk%5000 == 0 {
+				e = cygx.AddCygxIndustryFllowMulti(industryFllowItems)
+				if e != nil {
+					err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
+					return
+				}
+				industryFllowItems = make([]*cygx.CygxIndustryFllow, 0)
+				numk = 0
+			}
+		}
+		if len(industryFllowItems) > 0 {
+			e = cygx.AddCygxIndustryFllowMulti(industryFllowItems)
+			if e != nil {
+				err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
+				return
+			}
+		}
+	}
+	return
+}