浏览代码

Merge branch 'master' of http://8.136.199.33:3000/hongze/hongze_task

hongze 3 年之前
父节点
当前提交
e764d488c1
共有 7 个文件被更改,包括 207 次插入5 次删除
  1. 12 0
      go.sum
  2. 39 0
      models/company_end_date.go
  3. 9 0
      models/company_product.go
  4. 1 0
      models/db.go
  5. 136 2
      services/report_push.go
  6. 5 1
      services/task.go
  7. 5 2
      utils/config.go

+ 12 - 0
go.sum

@@ -1,6 +1,8 @@
 cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
+github.com/PuerkitoBio/goquery v1.6.1 h1:FgjbQZKl5HTmcn4sKBgvx8vv63nhyhIpv7lJpFGCWpk=
+github.com/PuerkitoBio/goquery v1.6.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
 github.com/PuerkitoBio/goquery v1.7.1 h1:oE+T06D+1T7LNrn91B4aERsRIeCLJ/oPSa6xB9FPnz4=
 github.com/PuerkitoBio/goquery v1.7.1/go.mod h1:XY0pP4kfraEmmV1O7Uf6XyjoslwsneBbgeDjLYuN8xY=
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
@@ -9,8 +11,12 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
 github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
 github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
 github.com/alicebob/miniredis v2.5.0+incompatible/go.mod h1:8HZjEj4yU0dwhYHky+DxYx+6BMjkBbe5ONFIF1MXffk=
+github.com/aliyun/aliyun-oss-go-sdk v2.1.8+incompatible h1:hLUNPbx10wawWW7DeNExvTrlb90db3UnnNTFKHZEFhE=
+github.com/aliyun/aliyun-oss-go-sdk v2.1.8+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
 github.com/aliyun/aliyun-oss-go-sdk v2.1.10+incompatible h1:D3gwOr9qUUmyyBRDbpnATqu+EkqqmigFd3Od6xO1QUU=
 github.com/aliyun/aliyun-oss-go-sdk v2.1.10+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
+github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo=
+github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
 github.com/andybalholm/cascadia v1.2.0 h1:vuRCkM5Ozh/BfmsaTm26kbjm0mIOM3yS5Ek/F5h18aE=
 github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxBp0T0eFw1RUQY=
 github.com/antlr/antlr4 v0.0.0-20190325153624-837aa60e2c47 h1:Lp5nUoQzppfVmfZadpzAytNyb5IMtxyOJLzoQS5dExg=
@@ -222,6 +228,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
 golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
 golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
 golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
 golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
@@ -243,6 +251,7 @@ golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7w
 golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
 golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
@@ -250,9 +259,12 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
+golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
 golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
 golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba h1:O8mE0/t419eoIwhTFpKVkHiTs/Igowgfkj25AcZrtiE=
+golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
 golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs=
 golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

+ 39 - 0
models/company_end_date.go

@@ -0,0 +1,39 @@
+package models
+
+import (
+	"rdluck_tools/orm"
+	"time"
+)
+
+// CompanyEndDate 客户产品结束日期表(临时表,只是用来做同步同花顺的截止日期使用)
+type CompanyEndDate struct {
+	Id         int       `description:"自增id"`
+	CompanyId  int       `description:"客户id"`
+	ProductId  int       `description:"产品id"`
+	Status     string    `description:"客户状态:试用,永续,冻结,流失,正式,潜在"`
+	EndDate    time.Time `description:"结束日期"`
+	CreateTime time.Time `description:"记录生成时间"`
+}
+
+// GetAllCompanyProductEndDate 根据获取客户结束日期表列表(临时表,只是用来做同步同花顺的截止日期使用)
+func GetAllCompanyProductEndDate(startDate, endDate string) (list []*CompanyEndDate, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT * from company_end_date WHERE create_time >= ? and create_time < ? `
+	_, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
+	return
+}
+
+// GetCompanyEndDate 查询客户产品结束日期记录
+func GetCompanyEndDate(createTime string) (item *CompanyEndDate, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT * from company_end_date WHERE create_time > ? limit 1`
+	err = o.Raw(sql, createTime).QueryRow(&item)
+	return
+}
+
+// AddCompanyEndDate 添加客户产品结束日期记录
+func AddCompanyEndDate(companyEndDate *CompanyEndDate) (err error) {
+	o := orm.NewOrm()
+	_, err = o.Insert(companyEndDate)
+	return
+}

+ 9 - 0
models/company_product.go

@@ -163,6 +163,7 @@ type CompanyProduct struct {
 	LoseReason       string    `description:"流失原因"`
 	LossTime         time.Time `description:"流失时间"`
 	CompanyType      string    `description:"客户类型"`
+	OpenCode         string    `description:"开放给第三方的编码,不让第三方定位我们的客户信息"`
 }
 
 //获取产品详情
@@ -215,3 +216,11 @@ WHERE
 	_, err = o.Raw(sql, sellerId, endDate, endDate).QueryRows(&items)
 	return
 }
+
+// GetAllCompanyProduct 获取所有客户产品列表(永续、正式、试用、冻结)
+func GetAllCompanyProduct() (items []*CompanyProduct, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT * from company_product WHERE status in ("永续","正式","试用","冻结") `
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}

+ 1 - 0
models/db.go

@@ -75,5 +75,6 @@ func init() {
 		new(Classify),
 		new(ReportSendThsDetail),
 		new(Report),
+		new(CompanyEndDate),
 	)
 }

+ 136 - 2
services/report_push.go

@@ -19,7 +19,7 @@ import (
 //	SendReportToThs(report)
 //}
 
-// SendWaitReport 定时向同花顺推送报告
+// SendWaitReport 定时向同花顺推送报告(定时任务)
 func SendWaitReport(cont context.Context) (err error) {
 	defer func() {
 		if err != nil {
@@ -40,6 +40,87 @@ func SendWaitReport(cont context.Context) (err error) {
 	return
 }
 
+// SyncWxGroupEveryDay 每日定时向同花顺同步客户时间
+func SyncWxGroupEveryDay(cont context.Context) (err error) {
+	defer func() {
+		if err != nil {
+			fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "同步微信群组信息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
+			//go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "同步微信群组信息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
+		}
+	}()
+
+	//查询校验当天数据是否已经生成
+	_, err = models.GetCompanyEndDate(time.Now().Format(utils.FormatDate))
+	if err == nil || err.Error() != utils.ErrNoRow() {
+		err = errors.New(fmt.Sprint(time.Now().Format(utils.FormatDate), "当天数据已经同步"))
+		return
+	}
+
+	//永续、正式、试用、冻结
+	list, err := models.GetAllCompanyProduct()
+	if err != nil {
+		return
+	}
+	timeLoc, _ := time.LoadLocation("Asia/Shanghai")
+	//需要入库的数据
+	companyEndDateMap := make(map[string]*models.CompanyEndDate)
+	//实际需要推送的数据
+	companyEndDatePushMap := make(map[string]*models.CompanyEndDate)
+
+	//微信群组开放编号code
+	companyProductOpenCode := make(map[string]string)
+	for _, companyProduct := range list {
+		key := fmt.Sprint(companyProduct.CompanyId, "_", companyProduct.ProductId)
+
+		endDate, _ := time.ParseInLocation(utils.FormatDate, companyProduct.EndDate, timeLoc)
+		if companyProduct.Status == "冻结" {
+			endDate = time.Now().AddDate(0, 0, -1)
+		}
+		tmpCompanyEndDate := &models.CompanyEndDate{
+			CompanyId:  companyProduct.CompanyId,
+			ProductId:  companyProduct.ProductId,
+			Status:     companyProduct.Status,
+			EndDate:    endDate,
+			CreateTime: time.Now(),
+		}
+		companyEndDateMap[key] = tmpCompanyEndDate
+		companyEndDatePushMap[key] = tmpCompanyEndDate
+
+		companyProductOpenCode[key] = companyProduct.OpenCode
+	}
+
+	startDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
+	endDate := time.Now().Format(utils.FormatDate)
+	endDateList, err := models.GetAllCompanyProductEndDate(startDate, endDate)
+	for _, endDate := range endDateList {
+		key := fmt.Sprint(endDate.CompanyId, "_", endDate.ProductId)
+		if companyEndDate, ok := companyEndDatePushMap[key]; ok {
+			if companyEndDate.Status == "冻结" {
+				//获取当天的零点时间
+				endDateStr := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
+				companyEndDate.EndDate, _ = time.ParseInLocation(utils.FormatDate, endDateStr, timeLoc)
+			}
+			if companyEndDate.EndDate.Equal(endDate.EndDate) {
+				delete(companyEndDatePushMap, key)
+			}
+		}
+	}
+
+	for _, companyEndDate := range companyEndDatePushMap {
+		fmt.Println("待推送数据:", companyEndDate)
+		key := fmt.Sprint(companyEndDate.CompanyId, "_", companyEndDate.ProductId)
+		//同步至同花顺
+		if openCode, ok := companyProductOpenCode[key]; ok {
+			SyncWxGroup(openCode, companyEndDate.EndDate.Format(utils.FormatDate))
+		}
+	}
+
+	for _, companyEndDate := range companyEndDateMap {
+		models.AddCompanyEndDate(companyEndDate)
+	}
+	return
+}
+
 var permissionMap map[string]string = map[string]string{
 	"化里化外日评":    "原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱",
 	"股债日评":      "宏观,利率债,原油,PTA,MEG,织造终端,甲醇,聚烯烃,沥青,橡胶,苯乙烯,玻璃纯碱,钢材,铁矿,双焦(焦煤、焦炭),有色(铜、铝),有色(锌、铅),镍+不锈钢",
@@ -62,12 +143,13 @@ var permissionMap map[string]string = map[string]string{
 	"黑色百家谈":     "钢材,铁矿,双焦(焦煤、焦炭)",
 }
 
-//同花顺返回信息
+//TshResult 同花顺返回信息
 type TshResult struct {
 	ErrorCode int    `json:"error" description:"错误状态码"`
 	Message   string `json:"message" description:"提示信息"`
 }
 
+//
 func SendToThs(sendDetailId, reportId int, reportType string) (err error) {
 	switch reportType {
 	case "日度点评":
@@ -226,3 +308,55 @@ func SendThs(title, labelStr, abstract, jumpBaseUrl, logoUrl string) (err error)
 	}
 	return
 }
+
+// SyncWxGroup 同步同花顺 微信群信息
+func SyncWxGroup(openCompanyCode, deadline string) (err error) {
+	defer func() {
+		if err != nil {
+			//fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "同步同花顺微信群信息失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
+			go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "同步同花顺微信群信息失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
+		}
+	}()
+	pubKey := utils.THS_PubKey
+	sendUrl := utils.THS_SyncWxGroupUrl
+
+	openCompanyCode, err = gorsa.PublicEncrypt(openCompanyCode, pubKey)
+	if err != nil {
+		return
+	}
+	deadline, err = gorsa.PublicEncrypt(deadline, pubKey)
+	if err != nil {
+		return
+	}
+
+	//开始发送
+	client := http.Client{}
+	form := url.Values{}
+	form.Add("thirdWechatGroupId", openCompanyCode)
+	form.Add("deadline", deadline)
+
+	utils.FileLog.Info("SendThs parms:%s", form.Encode())
+	resp, err := client.PostForm(sendUrl, form)
+	if err != nil {
+		return
+	}
+	defer resp.Body.Close()
+
+	body, _ := ioutil.ReadAll(resp.Body)
+
+	fmt.Println(string(body))
+	utils.FileLog.Info("ThsResult parms:%s", string(body))
+
+	//同花顺接口返回数据
+	var tshResult TshResult
+	err = json.Unmarshal(body, &tshResult)
+	if err != nil {
+		err = errors.New(fmt.Sprint("同花顺接口返回数据转换成结构体异常,Err:", err))
+		return
+	}
+	if tshResult.ErrorCode != 1 {
+		err = errors.New(fmt.Sprint("发送数据到同花顺接口异常,result:", string(body)))
+		return
+	}
+	return
+}

+ 5 - 1
services/task.go

@@ -46,7 +46,7 @@ func Task() {
 	task.AddTask("存量客户数据统计", stackCompanyStatistic)
 
 	// 定时往同花顺推送报告
-	sendWaitReport := task.NewTask("checkDataInterface", "0 */1 * * * * ", SendWaitReport)
+	sendWaitReport := task.NewTask("sendWaitReport", "0 */1 * * * * ", SendWaitReport)
 	task.AddTask("定时往同花顺推送报告", sendWaitReport)
 
 	task.StartTask()
@@ -102,6 +102,10 @@ func releaseTask() {
 	refreshData := task.NewTask("refreshData", "0 1 0,19 * * *", RefreshData)
 	task.AddTask("refreshData", refreshData)
 
+	// 定时往同花顺同步微信群的截止日期(凌晨4点)
+	syncThsWxGroupEveryDay := task.NewTask("syncThsWxGroupEveryDay", "0 1 4 * * * ", SyncWxGroupEveryDay)
+	task.AddTask("定时往同花顺同步微信群的截止日期", syncThsWxGroupEveryDay)
+
 	//刷新计算指标数据
 	//refreshCalculateData := task.NewTask("refreshCalculateData", "0 15 0,19 * * *", RefreshCalculateData)
 	//task.AddTask("refreshCalculateData", refreshCalculateData)

+ 5 - 2
utils/config.go

@@ -39,8 +39,9 @@ var (
 )
 
 var (
-	THS_SendUrl string //同花顺地址url
-	THS_PubKey  string //同花顺公钥
+	THS_SendUrl        string //同花顺地址url
+	THS_SyncWxGroupUrl string //同花顺同步微信群url
+	THS_PubKey         string //同花顺公钥
 )
 
 func init() {
@@ -76,6 +77,7 @@ ZwIDAQAB
 		RemindTemplateId = "PaHpQjVffrvmeW8wiOUcqlEhnD1LXyU9FnPVF-Jg9wM"
 		//同花顺正式地址
 		THS_SendUrl = `https://board.10jqka.com.cn/gateway/ps/syncNews`
+		THS_SyncWxGroupUrl = `https://board.10jqka.com.cn/gateway/ps/syncWechatGroupInfo`
 	} else {
 		CompanyId = 16
 		RealName = "超级管理员"
@@ -86,6 +88,7 @@ ZwIDAQAB
 		RemindTemplateId = "9JYV6sHMJlu2EHRBIj_8ift6wkrrTb9_UO-M_-YXKBw"
 		//同花顺测试地址
 		THS_SendUrl = `https://mtest.10jqka.com.cn/gateway/ps/syncNews`
+		THS_SyncWxGroupUrl = `https://mtest.10jqka.com.cn/gateway/ps/syncWechatGroupInfo`
 	}
 }