Browse Source

feat:修改

wangchuanjin 1 year ago
parent
commit
ef3aaa8abd

+ 3 - 3
resourceBrushLibrary/config.json

@@ -36,11 +36,11 @@
     ],
     "resourcesKey": "resourcescenter.rpc"
   },
-  "timeSource": "0/3 * * * * ?",
   "messageKey": "message.rpc",
   "userCenterKey":"usercenter.rpc",
   "webdomain": "https://jybx2-webtest.jydev.jianyu360.com",
   "sendMsgPool":5,
+  "givePool":5,
   "limitTimeActivity": "08:00",
   "checkPointExpire":"00:01",
   "checkPointExpirePool":5,
@@ -49,13 +49,13 @@
 		"expireDay":3,
 		"title":"做任务赚好礼挑战即将过期",
 		"content":"请及时完成任务并领取超级订阅7天体验,获取更多采购商机!",
-		"msgType":1,
+		"msgType":2,
 		"appid":"10000",
 		"pcUrl":"/home/work-bench/app/points/earn",
 		"mobileUrl":"/jy_mobile/points/earn"
 	},
 	"adpExpire":{
-		"wxTplId":"lnvey70BWSiqY-h6tDRxw3Ez_3fExFkIRoWRTijp5zs",
+		"wxTplId":"hLfZRFW9nOnXJ7uFoebshbHGqGyQszCnI13piLS4Kcc",
 		"expireDay":2,
 		"title":"您兑换的附件下载权益将于%d天内失效",
 		"content":"尊敬的用户,您有%d个兑换的附件下载权益将于%d天内失效,戳我使用",

+ 7 - 1
resourceBrushLibrary/config/config.go

@@ -16,6 +16,7 @@ import (
 
 var (
 	Config       *config
+	TaskConfig   *taskConfig
 	Mysql        *mysql.Mysql
 	Mysql_Point  *mysql.Mysql
 	Mgo_qfw      *mongodb.MongodbSim
@@ -48,11 +49,11 @@ type config struct {
 	} `json:"mongodb"`
 	Tidb                 *mysqlConf `json:"tidb"`
 	ResourceEtcdConf     etcdConf   `json:"resourceEtcdConf"`
-	TimeSource           string     `json:"timeSource"`
 	MessageKey           string
 	UserCenterKey        string
 	Webdomain            string
 	SendMsgPool          int
+	GivePool             int
 	LimitTimeActivity    string
 	CheckPointExpire     string
 	CheckPointExpirePool int
@@ -82,9 +83,14 @@ type mongo struct {
 	Password string
 }
 
+type taskConfig struct {
+	LastTime int64 `json:"lastTime"`
+}
+
 func init() {
 	//程序配置文件
 	qu.ReadConfig(&Config)
+	qu.ReadConfig(&TaskConfig)
 	Mysql = &mysql.Mysql{
 		Address:      Config.Mysql.Address,
 		UserName:     Config.Mysql.UserName,

+ 3 - 5
resourceBrushLibrary/entity/adp.go

@@ -23,10 +23,8 @@ func (a *Adp) Run() {
 	util.SimpleCrontab(false, Config.LimitTimeActivity, func() {
 		pool := make(chan bool, Config.SendMsgPool)
 		wait := &sync.WaitGroup{}
-		now := time.Now()
-		nowStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
-		end := nowStart.AddDate(0, 0, Config.Messages.AdpExpire.ExpireDay-1)
-		endTime := date.FormatDate(&end, date.Date_Full_Layout)
+		end := time.Now().AddDate(0, 0, Config.Messages.AdpExpire.ExpireDay-1)
+		endTime := date.FormatDate(&end, date.Date_Short_Layout)
 		all := map[string]int{}
 		lock := &sync.Mutex{}
 		Tidb.SelectByBath(1, func(l *[]map[string]interface{}) bool {
@@ -73,7 +71,7 @@ func (a *Adp) Run() {
 				wxUrl := "/front/sess/" + Se.EncodeString(accountId+","+util.If(IsObjectIdHex(accountId), "_id", "positionId").(string)+","+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(Config.Messages.AdpExpire.MobileUrl)
 				appUrl := "/jyapp/free/sess/" + Se.EncodeString(accountId+","+util.If(IsObjectIdHex(accountId), "_id", "positionId").(string)+","+strconv.Itoa(int(time.Now().Unix()))+",") + "__" + Se.EncodeString(Config.Messages.AdpExpire.MobileUrl)
 				title := fmt.Sprintf(Config.Messages.AdpExpire.Title, Config.Messages.AdpExpire.ExpireDay)
-				content := fmt.Sprintf(Config.Messages.AdpExpire.Content, Config.Messages.AdpExpire.ExpireDay, number)
+				content := fmt.Sprintf(Config.Messages.AdpExpire.Content, number, Config.Messages.AdpExpire.ExpireDay)
 				SendMsg(&message.MultipleSaveMsgReq{
 					UserIds:    mgoUserId,
 					Title:      title,

+ 68 - 57
resourceBrushLibrary/entity/order.go

@@ -1,100 +1,111 @@
 package entity
 
 import (
+	"fmt"
 	"log"
 	. "resourceBrushLibrary/config"
+	"sync"
 	"time"
 
 	"app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenterclient"
 	qu "app.yhyue.com/moapp/jybase/common"
+	. "app.yhyue.com/moapp/jybase/date"
+	. "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
 )
 
 func OrderResource() {
 	now := time.Now()
-	start := now.Add(-time.Minute * 1).Unix()
-	//start := now.AddDate(-1, 0, 0).Unix()
-	log.Println("user", start, now.Unix())
+	end := now.Unix()
+	pool := make(chan bool, Config.GivePool)
+	wait := &sync.WaitGroup{}
+	log.Println("user", TaskConfig.LastTime, end)
 	it := Mgo_qfw.GetMgoConn().DB("qfw").C("user").Find(map[string]interface{}{
 		"i_appid": 2,
 		"i_vip_status": map[string]interface{}{
 			"$gt": 0,
 		},
 		"l_vip_starttime": map[string]interface{}{
-			"$gte": start,
-			"$lt":  now.Unix(),
+			"$gte": TaskConfig.LastTime,
+			"$lt":  end,
 		},
 	}).Select(map[string]interface{}{
-		"_id":             1,
-		"l_vip_starttime": 1,
-	}).Sort("_id").Iter()
-	currentYear, currentMonth, _ := now.Date()
-	currentLocation := now.Location()
-	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
-	lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
-	endTime := lastOfMonth.Format("2006-01-02")
-	log.Println("user", it)
-	for user := make(map[string]interface{}); it.Next(&user); {
-		//每个用户生成10个附件下载包
-		userId := qu.InterfaceToStr(user["_id"])
-		vipTime := time.Unix(qu.Int64All(user["l_vip_starttime"]), 0).Format("2006-01-02")
-		Purchase.PurchaseUserBalance(resourcesCenterclient.Resources{
-			AppId:        "10000",
-			UserId:       userId,
-			ResourceType: "附件下载包",
-			AccountId:    userId,
-			Name:         "附件下载包",
-			Number:       number,
-			Spec:         "个",
-			EndTime:      endTime,
-			VipTime:      vipTime,
-			Model:        3,
-		})
+		"_id":           1,
+		"l_vip_endtime": 1,
+	}).Iter()
+	endTime := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).AddDate(0, 1, -1).Format(Date_Short_Layout)
+	for m := make(map[string]interface{}); it.Next(&m); {
+		pool <- true
+		wait.Add(1)
+		go func(user map[string]interface{}) {
+			defer qu.Catch()
+			defer func() {
+				<-pool
+				wait.Done()
+			}()
+			//每个用户生成10个附件下载包
+			userId := qu.InterfaceToStr(user["_id"])
+			l_vip_endtime := qu.Int64All(user["l_vip_endtime"])
+			Purchase.PurchaseUserBalance(resourcesCenterclient.Resources{
+				AppId:        "10000",
+				UserId:       userId,
+				ResourceType: "附件下载包",
+				AccountId:    userId,
+				Name:         "附件下载包",
+				Number:       number,
+				Spec:         "个",
+				EndTime:      endTime,
+				VipTime:      FormatDateByInt64(&l_vip_endtime, Date_Short_Layout),
+				Model:        3,
+			})
+		}(m)
 	}
 	//企业版超级订阅处理
-	log.Println("ent_user", start, now.Unix())
+	log.Println("ent_user", TaskConfig.LastTime, end)
 	itEnt := Mgo_qfw.GetMgoConn().DB("qfw").C("ent_user").Find(map[string]interface{}{
 		"i_vip_status": map[string]interface{}{
 			"$gt": 0,
 		},
 		"l_vip_starttime": map[string]interface{}{
-			"$gte": start,
-			"$lt":  now.Unix(),
+			"$gte": TaskConfig.LastTime,
+			"$lt":  end,
 		},
 	}).Select(map[string]interface{}{
-		"_id":             1,
-		"l_vip_starttime": 1,
-		"i_entid":         1,
-	}).Sort("_id").Iter()
+		"_id":           1,
+		"l_vip_endtime": 1,
+		"i_userid":      1,
+	}).Iter()
 	log.Println("ent_user", itEnt)
-	for user := make(map[string]interface{}); itEnt.Next(&user); {
-		entId := user["i_entid"]
-		entData := Mysql.FindOne("entniche_info", map[string]interface{}{
-			"id": entId,
-		}, "phone", "")
-		if entData == nil {
-			log.Println(entId, "企业已经不存在")
-			continue
-		}
-		phone := (*entData)["phone"]
-		positionList := Tidb.SelectBySql("select  c.id from  base_person  a     INNER JOIN base_user b on  a.id=b.person_id and    a.contact=?     INNER JOIN base_position c  on  b.id=c.user_id and  c.ent_id=? ", phone, entId)
-		if len(*positionList) > 0 {
-			userId := qu.InterfaceToStr((*positionList)[0]["id"])
+	for m := make(map[string]interface{}); itEnt.Next(&m); {
+		pool <- true
+		wait.Add(1)
+		go func(user map[string]interface{}) {
+			defer qu.Catch()
+			defer func() {
+				<-pool
+				wait.Done()
+			}()
+			identity := IdentityByEntUserId(Tidb, Mysql, qu.Int64All(user["i_userid"]))
+			if identity == nil {
+				log.Println("没有找到该用户身份信息", user)
+				return
+			}
 			//每个用户生成10个附件下载包
-			//企业id查询
-			vipTime := time.Unix(qu.Int64All(user["l_vip_starttime"]), 0).Format("2006-01-02")
+			l_vip_endtime := qu.Int64All(user["l_vip_endtime"])
 			Purchase.PurchaseUserBalance(resourcesCenterclient.Resources{
 				AppId:        "10000",
-				UserId:       userId,
+				UserId:       fmt.Sprint(identity.PositionId),
 				ResourceType: "附件下载包",
-				AccountId:    userId,
+				AccountId:    fmt.Sprint(identity.PositionId),
 				Name:         "附件下载包",
 				Number:       number,
 				Spec:         "个",
 				EndTime:      endTime,
-				VipTime:      vipTime,
+				VipTime:      FormatDateByInt64(&l_vip_endtime, Date_Short_Layout),
 				Model:        3,
 			})
-		}
+		}(m)
 	}
-
+	wait.Wait()
+	TaskConfig.LastTime = end
+	qu.WriteSysConfig(TaskConfig)
 }

+ 1 - 1
resourceBrushLibrary/entity/point.go

@@ -32,7 +32,7 @@ func (p *Point) Run() {
 				Mysql_Point.InsertBySql(`insert into integral_flow (userId,pointType,sourceType,point,createTime,endDate,appId,sort) values (?,?,?,?,?,?,?,?)`, userId, 2004, "2004", timePoints, date.NowFormat(date.Date_Full_Layout), endDate, appId, -1)
 			}(util.ObjToString((*l)[0]["appId"]), util.ObjToString((*l)[0]["userId"]), util.ObjToString((*l)[0]["endDate"]), util.IntAll((*l)[0]["timePoints"]))
 			return true
-		}, `select appId,userId,timePoints,endDate from integral_solde where timePoints>0 and end_time=?`, date.FormatDate(&end, date.Date_Short_Layout))
+		}, `select appId,userId,timePoints,endDate from integral_solde where timePoints>0 and endDate=?`, date.FormatDate(&end, date.Date_Short_Layout))
 		wait.Wait()
 	})
 }

+ 27 - 2
resourceBrushLibrary/entity/purchaseBalance.go

@@ -2,24 +2,49 @@ package entity
 
 import (
 	"context"
+	"fmt"
 	"log"
 	. "resourceBrushLibrary/config"
+	"sync"
+	"time"
 
 	"app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenterclient"
+	qu "app.yhyue.com/moapp/jybase/common"
 )
 
 type PurchaseBalance struct{}
 
 var Purchase *PurchaseBalance
+var allGiven = map[string]int64{}
+var allGivenLock = &sync.Mutex{}
+
+func init() {
+	go qu.SimpleCrontab(false, "01:00", func() {
+		allGivenLock.Lock()
+		defer allGivenLock.Unlock()
+		//缓存40天,清理40天之前的数据
+		for k, v := range allGiven {
+			if v < time.Now().Unix()-3456000 {
+				delete(allGiven, k)
+			}
+		}
+	})
+}
 
 // 资源新增
 // 查询数据有没有新增过
-func (this *PurchaseBalance) PurchaseUserBalance(inteReq resourcesCenterclient.Resources) (resp resourcesCenterclient.Response) {
+func (this *PurchaseBalance) PurchaseUserBalance(inteReq resourcesCenterclient.Resources) {
+	key := fmt.Sprintf("%s_%s_%s_%d", inteReq.UserId, inteReq.ResourceType, inteReq.EndTime, inteReq.Model)
+	allGivenLock.Lock()
+	if allGiven[key] > 0 {
+		return
+	}
+	allGiven[key] = time.Now().Unix()
+	allGivenLock.Unlock()
 	//判断资源之前有没有新增
 	_resp, err := ResourcesRpc.PurchaseUserBalance(context.Background(), &inteReq)
 	if err != nil {
 		log.Println("BalanceCheck error:", err)
 	}
 	log.Println(inteReq.UserId, _resp)
-	return *_resp
 }

+ 79 - 63
resourceBrushLibrary/entity/resource.go

@@ -2,85 +2,101 @@ package entity
 
 import (
 	"fmt"
+	"log"
 	. "resourceBrushLibrary/config"
-	"time"
+	"sync"
 
 	"app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenterclient"
 	qu "app.yhyue.com/moapp/jybase/common"
+	. "app.yhyue.com/moapp/jybase/date"
+	. "app.yhyue.com/moapp/jybase/mongodb"
+	. "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
 )
 
 var number = int64(10)
 
-func PersonResource() {
+func PersonResource(endTime string) {
 	it := Mgo_qfw.GetMgoConn().DB("qfw").C("user").Find(map[string]interface{}{
 		"i_appid": 2,
 		"i_vip_status": map[string]interface{}{
 			"$gt": 0,
 		},
 	}).Select(map[string]interface{}{
-		"_id":             1,
-		"l_vip_starttime": 1,
-	}).Sort("_id").Iter()
-	now1 := time.Now()
-	currentYear, currentMonth, _ := now1.Date()
-	currentLocation := now1.Location()
-	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
-	lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
-	endTime := lastOfMonth.Format("2006-01-02")
-	for user := make(map[string]interface{}); it.Next(&user); {
-		//每个用户生成10个附件下载包
-		userId := qu.InterfaceToStr(user["_id"])
-		vipTime := time.Unix(qu.Int64All(user["l_vip_starttime"]), 0).Format("2006-01-02")
-		Purchase.PurchaseUserBalance(resourcesCenterclient.Resources{
-			AppId:        "10000",
-			UserId:       userId,
-			ResourceType: "附件下载包",
-			AccountId:    userId,
-			Name:         "附件下载包",
-			Number:       number,
-			Spec:         "个",
-			EndTime:      endTime,
-			VipTime:      vipTime,
-			Model:        3,
-		})
+		"_id":           1,
+		"l_vip_endtime": 1,
+	}).Iter()
+	pool := make(chan bool, Config.GivePool)
+	wait := &sync.WaitGroup{}
+	for m := make(map[string]interface{}); it.Next(&m); {
+		pool <- true
+		wait.Add(1)
+		go func(user map[string]interface{}) {
+			defer qu.Catch()
+			defer func() {
+				<-pool
+				wait.Done()
+			}()
+			//每个用户生成10个附件下载包
+			userId := BsonIdToSId(user["_id"])
+			l_vip_endtime := qu.Int64All(user["l_vip_endtime"])
+			Purchase.PurchaseUserBalance(resourcesCenterclient.Resources{
+				AppId:        "10000",
+				UserId:       userId,
+				ResourceType: "附件下载包",
+				AccountId:    userId,
+				Name:         "附件下载包",
+				Number:       number,
+				Spec:         "个",
+				EndTime:      endTime,
+				VipTime:      FormatDateByInt64(&l_vip_endtime, Date_Short_Layout),
+				Model:        3,
+			})
+		}(m)
 	}
+	wait.Wait()
 }
 
-func EntResource() {
-	now1 := time.Now()
-	currentYear, currentMonth, _ := now1.Date()
-	currentLocation := now1.Location()
-	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
-	lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
-	endTime := lastOfMonth.Format("2006-01-02")
-	sql := fmt.Sprintf("SELECT    b.ent_id,    c.phone,    b.ent_user_id,    a.product_type,    a.start_time,    a.end_time     "+
-		"FROM    entniche_wait_empower a  "+
-		"  INNER JOIN entniche_power b ON ( a.id = b.wait_empower_id )  "+
-		"  INNER JOIN entniche_user c ON ( b.ent_user_id = c.id )   "+
-		"  WHERE    b.STATUS = 1  and  a.product_type='VIP订阅'    AND a.end_time > '%s'", time.Now().Format("2006-01-02 15:04:05"))
-	Mysql.SelectByBath(200, func(l *[]map[string]interface{}) bool {
-		for _, v := range *l {
-			//职位信息查询
-			phone := v["phone"]
-			entId := v["ent_id"]
-			positionList := Tidb.SelectBySql("select  c.id from  base_person  a     INNER JOIN base_user b on  a.id=b.person_id and    a.contact=?     INNER JOIN base_position c  on  b.id=c.user_id and  c.ent_id=? ", phone, entId)
-			if len(*positionList) > 0 {
-				userId := qu.InterfaceToStr((*positionList)[0]["id"])
-				vipTime := time.Unix(qu.Int64All((*positionList)[0]["end_time"]), 0).Format("2006-01-02")
-				Purchase.PurchaseUserBalance(resourcesCenterclient.Resources{
-					AppId:        "10000",
-					UserId:       userId,
-					ResourceType: "附件下载包",
-					AccountId:    userId,
-					Name:         "附件下载包",
-					Number:       number,
-					Spec:         "个",
-					EndTime:      endTime,
-					VipTime:      vipTime,
-					Model:        3,
-				})
+func EntResource(endTime string) {
+	it := Mgo_qfw.GetMgoConn().DB("qfw").C("ent_user").Find(map[string]interface{}{
+		"i_vip_status": map[string]interface{}{
+			"$gt": 0,
+		},
+	}).Select(map[string]interface{}{
+		"_id":           1,
+		"l_vip_endtime": 1,
+		"i_userid":      1,
+	}).Iter()
+	pool := make(chan bool, Config.GivePool)
+	wait := &sync.WaitGroup{}
+	for m := make(map[string]interface{}); it.Next(&m); {
+		pool <- true
+		wait.Add(1)
+		go func(user map[string]interface{}) {
+			defer qu.Catch()
+			defer func() {
+				<-pool
+				wait.Done()
+			}()
+			//每个用户生成10个附件下载包
+			identity := IdentityByEntUserId(Tidb, Mysql, qu.Int64All(user["i_userid"]))
+			if identity == nil {
+				log.Println("没有找到该用户身份信息", user)
+				return
 			}
-		}
-		return true
-	}, sql)
+			l_vip_endtime := qu.Int64All(user["l_vip_endtime"])
+			Purchase.PurchaseUserBalance(resourcesCenterclient.Resources{
+				AppId:        "10000",
+				UserId:       fmt.Sprint(identity.PositionId),
+				ResourceType: "附件下载包",
+				AccountId:    fmt.Sprint(identity.PositionId),
+				Name:         "附件下载包",
+				Number:       number,
+				Spec:         "个",
+				EndTime:      endTime,
+				VipTime:      FormatDateByInt64(&l_vip_endtime, Date_Short_Layout),
+				Model:        3,
+			})
+		}(m)
+	}
+	wait.Wait()
 }

+ 4 - 2
resourceBrushLibrary/main.go

@@ -25,8 +25,10 @@ func MonthlyTask() {
 	// 添加要执行的任务
 	c.AddFunc(config.Config.MonthlyTask, func() {
 		// 执行任务的代码
-		entity.PersonResource()
-		//entity.EntResource()
+		now := time.Now()
+		endTime := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).AddDate(0, 1, -1).Format("2006-01-02")
+		entity.PersonResource(endTime)
+		entity.EntResource(endTime)
 	})
 	// 启动Cron调度器
 	c.Start()

+ 1 - 0
resourceBrushLibrary/task.json

@@ -0,0 +1 @@
+{"lastTime":0}