浏览代码

修改潜在用户定时任务

xiexiaoyuan 2 年之前
父节点
当前提交
c1c37b58af
共有 2 个文件被更改,包括 66 次插入27 次删除
  1. 42 22
      models/free_viewer.go
  2. 24 5
      services/free_viewer.go

+ 42 - 22
models/free_viewer.go

@@ -1,39 +1,59 @@
 package models
 
-import "github.com/rdlucklib/rdluck_tools/orm"
+import (
+	"github.com/rdlucklib/rdluck_tools/orm"
+	"time"
+)
 
 type FreeViewerDetails struct {
 	UserId         int
 	RealName       string
 	Mobile         string
-	Note           string
-	CreatedTime    string
-	MaxCreatedTime string
+	CompanyName    string
+	LastTime       string
 	Email          string
+	ApplyRecordId  int
 }
 
 func GetFreeViewerDetails(startTime, endTime string) (items []*FreeViewerDetails, err error) {
-	sql := `select u.user_id,u.real_name,u.mobile,u.note,u.created_time,max(uvh.created_time) as max_created_time,u.email
-	from wx_user u
-	LEFT JOIN user_view_history uvh on u.user_id = uvh.user_id
-    INNER JOIN user_record AS c ON u.user_id=c.user_id
-	where u.company_id = 1
-	and u.is_deal=0
-	and u.apply_method<>2
-	and u.mobile is not null
-	AND u.mobile<>''
-	and u.created_time > ?
-	and u.created_time <= ?
-	AND c.create_platform<>4
-    AND u.is_deal = 0
-	group by u.user_id`
-	_, err = orm.NewOrm().Raw(sql, startTime, endTime).QueryRows(&items)
+	sql := `SELECT
+	a.user_id,
+	a.real_name,
+	a.mobile,
+	a.email,
+    y.apply_record_id,
+    IF(a.company_id > 1,b.company_name,a.note) AS company_name,
+	IF(y.apply_record_id > 0,y.create_time, a.created_time) as last_time
+FROM
+	wx_user AS a
+	LEFT JOIN company AS b ON a.company_id = b.company_id
+  LEFT JOIN (SELECT * from company_product where product_id = 1) AS bp ON a.company_id = bp.company_id
+	LEFT JOIN (SELECT user_record_id, create_platform, user_id from user_record) AS c ON a.user_id = c.user_id 
+	LEFT JOIN (SELECT * from yb_apply_record where apply_record_id in (SELECT max(apply_record_id) from yb_apply_record GROUP BY user_id)) as y on a.user_id = y.user_id
+WHERE
+	b.enabled = 1 
+	AND ( y.apply_record_id > 0 or a.company_id=1 or bp.company_product_id is null)
+	AND ( a.mobile IS NOT NULL ) 
+	AND ( a.mobile <> '' ) 
+	AND ( c.create_platform <> 4 OR c.create_platform IS NULL )
+    AND (y.status = "潜在用户" or y.status is null or y.status = "权益用户" or y.status = "流失" )
+	AND ((y.apply_record_id > 0 and y.create_time > ? and y.create_time <= ?) OR (y.apply_record_id is null AND a.created_time > ? and a.created_time <= ?) )
+  GROUP BY a.user_id ORDER BY last_time asc
+	`
+	_, err = orm.NewOrm().Raw(sql, startTime, endTime, startTime, endTime).QueryRows(&items)
 	return
 }
 
 // DealWxUser 处理用户标记状态
-func DealWxUser(userId int) (err error) {
-	sql := `update wx_user set is_deal = 1 where user_id=?`
-	_, err = orm.NewOrm().Raw(sql, userId).Exec()
+func DealWxUsers(userIds string) (err error) {
+	sql := `update wx_user set is_deal = 1 where user_id in (`+userIds+`) and is_deal=0`
+	_, err = orm.NewOrm().Raw(sql).Exec()
+	return
+}
+
+// DealFiccApply 处理用户申请标记状态
+func DealFiccApply(applyRecordIds string, dealTime time.Time) (err error) {
+	sql := `update yb_apply_record set op_status = 1, deal_time=?  where apply_record_id in (`+applyRecordIds+`) and op_status=0`
+	_, err = orm.NewOrm().Raw(sql, dealTime).Exec()
 	return
 }

+ 24 - 5
services/free_viewer.go

@@ -7,6 +7,8 @@ import (
 	"hongze/hongze_task/models"
 	"hongze/hongze_task/utils"
 	"os"
+	"strconv"
+	"strings"
 	"time"
 )
 
@@ -23,6 +25,8 @@ func FreeViewerDetail(cont context.Context) (err error) {
 	startTime := time.Now().AddDate(0, 0, -7).Format(utils.FormatDateTime)
 	endTime := time.Now().Format(utils.FormatDateTime)
 
+	userIdsStr := ""
+	applyRecordIdsStr := ""
 	items, err := models.GetFreeViewerDetails(startTime, endTime)
 	if err != nil {
 		return
@@ -54,14 +58,18 @@ func FreeViewerDetail(cont context.Context) (err error) {
 		cellB := row.AddCell()
 		cellB.Value = item.Mobile
 		cellC := row.AddCell()
-		cellC.Value = item.Note
+		cellC.Value = item.CompanyName
 		cellD := row.AddCell()
-		cellD.Value = item.CreatedTime
+		cellD.Value = item.LastTime
 		cellE := row.AddCell()
 		cellE.Value = item.Email
 
-		models.DealWxUser(item.UserId)
+		userIdsStr += ","+strconv.Itoa(item.UserId)
+		applyRecordIdsStr += ","+strconv.Itoa(item.ApplyRecordId)
 	}
+	userIdsStr = strings.Trim(userIdsStr, ",")
+	applyRecordIdsStr = strings.Trim(applyRecordIdsStr, ",")
+	_ = dealUser(userIdsStr, applyRecordIdsStr)
 
 	savePath := "free_viewer_details" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
 	err = file.Save("./" + savePath)
@@ -71,11 +79,22 @@ func FreeViewerDetail(cont context.Context) (err error) {
 	//发送邮件
 	fmt.Println("start send email")
 	sendResult := utils.SendEmailByHongze(title, "潜在客户回访记录',\"你好,上周潜在客户回访记录见附件。", utils.EmailSendToHzUsers, savePath, title+".xlsx")
-	//sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToMe,savePath)
+	//sendResult:=utils.SendEmailByHongze(title,"潜在客户回访记录',\"你好,上周潜在客户回访记录见附件。",utils.EmailSendToMe,savePath, title+".xlsx")
 	if sendResult {
-		os.Remove(savePath)
+		_ = os.Remove(savePath)
 	}
 	//fmt.Println("send result:", sendResult)
 	fmt.Println("end send email")
 	return nil
 }
+
+func dealUser(userIds, applyRecordIds string) (err error) {
+	if userIds !="" {
+		_ = models.DealWxUsers(userIds)
+	}
+	if applyRecordIds != "" {
+		nowTime := time.Now()
+		_ = models.DealFiccApply(applyRecordIds, nowTime)
+	}
+	return
+}