Browse Source

fix:初始化活动rpc

duxin 2 years ago
parent
commit
df3fc7fff3
5 changed files with 101 additions and 96 deletions
  1. 3 2
      src/config.yaml
  2. 1 1
      src/go.mod
  3. 2 2
      src/go.sum
  4. 95 57
      src/jfw/jyutil/classroomiInfo.go
  5. 0 34
      src/jfw/jyutil/jyutil.go

+ 3 - 2
src/config.yaml

@@ -1,7 +1,8 @@
 etcd:
   hosts:
-  - 192.168.3.149:2379
+  - 192.168.3.206:2379
 userCenterKey: "usercenter.rpc" #用户中台rpc
 powerCheckCenterKey: "powercheck.rpc" #权益校验中台
 resourceCenterKey: "resource.rpc" #资源中台
-entManageApplication: "entmanageapplication.rpc" #企业管理中台
+entManageApplication: "entmanageapplication.rpc" #企业管理中台
+activityKey: "activity.rpc" #营销中台rpc

+ 1 - 1
src/go.mod

@@ -3,6 +3,7 @@ module jy/src
 go 1.18
 
 require (
+	app.yhyue.com/moapp/jyMarketing v0.0.2-0.20230307011807-2ef7ef8e0c09
 	app.yhyue.com/moapp/jybase v0.0.0-20230117032034-ad7c00ffe11a
 	app.yhyue.com/moapp/jypkg v0.0.0-20230309102709-f1b34292afaa
 	bp.jydev.jianyu360.cn/BaseService/userCenter v1.2.13
@@ -16,7 +17,6 @@ require (
 
 require (
 	app.yhyue.com/moapp/esv1 v0.0.0-20220414031211-3da4123e648d // indirect
-	app.yhyue.com/moapp/jyMarketing v0.0.2-0.20230304035551-21bb1eedf547 // indirect
 	app.yhyue.com/moapp/message v0.0.0-20221223100203-6402e389d9ae // indirect
 	bp.jydev.jianyu360.cn/BaseService/entManageApplication v0.0.0-20230214091519-89a98c01ab0e // indirect
 	bp.jydev.jianyu360.cn/BaseService/powerCheckCenter v0.0.0-20230222052351-9d6fad062447 // indirect

+ 2 - 2
src/go.sum

@@ -1,7 +1,7 @@
 app.yhyue.com/moapp/esv1 v0.0.0-20220414031211-3da4123e648d h1:WPsYuuptAd3UEgN+jPzpnsDe/OvcshDUUtOTZPYGSJ8=
 app.yhyue.com/moapp/esv1 v0.0.0-20220414031211-3da4123e648d/go.mod h1:91/lSD/hS+ckMVP3WdidRzDhC60lLMdyce9QHy0cSMA=
-app.yhyue.com/moapp/jyMarketing v0.0.2-0.20230304035551-21bb1eedf547 h1:cCmWQW8DUBD2nuZNDz9aIe6MrlioxTbdaA2YiJhlzjY=
-app.yhyue.com/moapp/jyMarketing v0.0.2-0.20230304035551-21bb1eedf547/go.mod h1:JvIs8uKjdT963+7JnZGIEcL4ctBiBjwkoz0kNyigE78=
+app.yhyue.com/moapp/jyMarketing v0.0.2-0.20230307011807-2ef7ef8e0c09 h1:ckax2O7nHCFa0RD1qM7cUYaPNQQpR9j+kimXdyDOiQk=
+app.yhyue.com/moapp/jyMarketing v0.0.2-0.20230307011807-2ef7ef8e0c09/go.mod h1:JvIs8uKjdT963+7JnZGIEcL4ctBiBjwkoz0kNyigE78=
 app.yhyue.com/moapp/jyPoints v1.1.1/go.mod h1:SvP8p5L3jGrejHiH2LXfgCg/NPlFiKBC5Yd0gsI12FU=
 app.yhyue.com/moapp/jybase v0.0.0-20220427020729-974c1a148186/go.mod h1:qNRA0sHuYqcLoYoP8irpaWnW9YsXixe6obBIkwaXpD0=
 app.yhyue.com/moapp/jybase v0.0.0-20230117032034-ad7c00ffe11a h1:wD4aWPSYdiX1cIP4lzzPD2s7fYhKa3muIf97l9tonJE=

+ 95 - 57
src/jfw/jyutil/classroomiInfo.go

@@ -1,13 +1,20 @@
 package jyutil
 
 import (
+	"app.yhyue.com/moapp/jyMarketing/rpc/activity"
 	util "app.yhyue.com/moapp/jybase/common"
 	. "app.yhyue.com/moapp/jybase/date"
 	mgo "app.yhyue.com/moapp/jybase/mongodb"
 	"app.yhyue.com/moapp/jybase/redis"
 	"bytes"
+	"context"
 	"encoding/json"
 	"fmt"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/os/gcfg"
+	"github.com/gogf/gf/v2/os/gctx"
+	"github.com/zeromicro/go-zero/core/discov"
+	"github.com/zeromicro/go-zero/zrpc"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 	"html/template"
 	"io/ioutil"
@@ -42,6 +49,7 @@ type HelpColumn struct {
 }
 
 func init() {
+	go ActivityInit()
 	log.Println("开始初始化column")
 	ColumnRelationship = make(map[string]Navigation)
 	industryInfoUrl, _ := config.Sysconfig["industryInfoUrl"].(map[string]interface{})
@@ -81,6 +89,43 @@ func init() {
 	}
 }
 
+var Activity activity.Activity
+
+// 活动初始化
+func ActivityInit() interface{} {
+	log.Println("开始初始化活动rpc")
+	g.Cfg().GetAdapter().(*gcfg.AdapterFile).SetFileName("config.yaml")
+	var ctx = gctx.New()
+	Activity = activity.NewActivity(zrpc.MustNewClient(zrpc.RpcClientConf{
+		Etcd: discov.EtcdConf{
+			Hosts: g.Cfg().MustGet(ctx, "etcd.hosts").Strings(),
+			Key:   g.Cfg().MustGet(ctx, "activityKey").String(),
+		},
+	}))
+	redis.Del("other", "lotteryArray_jySchool")
+	resp, err := Activity.GetAllLottery(context.Background(), &activity.Request{
+		AppId:       "10000",
+		ProductCode: "111",
+	})
+	log.Println(resp, err)
+	if err != nil {
+		log.Println("奖券获取失败")
+		return false
+	}
+	lotteryMap := map[string][]*activity.LotteryJson{}
+	for _, activityValue := range resp.ActivityJson {
+		for _, lotteryValue := range activityValue.LotteryJson {
+			for _, productValue := range lotteryValue.UseProductList {
+				lotteryValue.UseProductList = []*activity.ProductJson{}
+				lotteryMap[productValue.ProductCode] = append(lotteryMap[productValue.ProductCode], lotteryValue)
+			}
+		}
+	}
+	redis.PutKV("lotteryArray_jySchool", lotteryMap)
+	log.Println("活动rpc初始化完毕")
+	return true
+}
+
 // Course 剑鱼课堂首页课程 & 帮助中心热门视频 classify:0:中标比听课视频 1:帮助中心视频(获取所有)
 func Course(num, iType int) *[]map[string]interface{} {
 	//热门课程
@@ -116,7 +161,7 @@ func Course(num, iType int) *[]map[string]interface{} {
 				(*LatestCourses)[key]["isExpired"] = false
 			}
 			productPrice := util.Float64All(value["i_price"])
-			fool, discountPrice := OptimalSelection(util.InterfaceToStr(value["_id"]), productPrice, true)
+			fool, discountPrice := OptimalSelection(util.InterfaceToStr(value["_id"]), productPrice)
 			fmt.Println("OptimalSelection=====", fool, discountPrice, value["_id"])
 			(*LatestCourses)[key]["isDiscount"] = fool
 			if fool {
@@ -150,38 +195,63 @@ func Course(num, iType int) *[]map[string]interface{} {
 }
 
 // 奖券详情
-func OptimalSelection(id string, price float64, fool bool) (bool, float64) {
+func OptimalSelection(id string, price float64) (bool, float64) {
 	discountPrice := float64(0)
-	if fool {
-		//查询奖券信息
-		Getlottery(id)
-	}
-	lotteryArrayStr := redis.Get("other", "lotteryArray_"+id)
-	lotteryArray := []interface{}{}
-	if lotteryArrayStr != nil {
-		lotteryArray = lotteryArrayStr.([]interface{})
-	}
-	for _, value := range lotteryArray {
-		useProductJson := value.(map[string]interface{})
-		full := util.Float64All(useProductJson["Full"])
+	productStr := redis.Get("other", "lotteryArray_jySchool")
+	fmt.Println("333333333333", productStr)
+	lotteryMap := &map[string]interface{}{}
+	if productStr != nil {
+		lotteryMap = util.ObjToMap(productStr)
+	}
+	lotteryStr := (*lotteryMap)[id]
+	if lotteryStr == nil {
+		return false, discountPrice
+	}
+	lotteryList := util.ObjArrToMapArr(lotteryStr.([]interface{}))
+	for _, lottery := range lotteryList {
+		lotteryType := util.Int64All(lottery["lotteryType"])
+		full := util.Float64All(lottery["full"])
 		full *= 100
-		if full <= price {
-			lotteryType := util.Int64All(useProductJson["lotteryType"])
-			reduce := util.Float64All(useProductJson["Reduce"]) * 100
-			discount := util.Float64All(useProductJson["discount"])
-			if lotteryType == 0 {
-				//满减
-				if reduce > discountPrice {
-					discountPrice = reduce
-				}
-			} else {
-				//满折
+		//类型:0满减、1折扣券、2满赠、3促销、4限时折扣、5限时减免
+		switch lotteryType {
+		case 1: //满折
+			if full <= price {
+				discount := util.Float64All(lottery["discount"])
 				preferential := (1 - discount/10) * price
 				preferential = math.Trunc(preferential*1e0 + 0.5)
 				if preferential > discountPrice {
 					discountPrice = preferential
 				}
 			}
+			break
+		case 3: //3促销
+			promotionalPrice := util.Float64All(lottery["promotionalPrice"])
+			if util.Float64All(price-promotionalPrice) > discountPrice {
+				discountPrice = util.Float64All(price - promotionalPrice)
+			}
+			break
+		case 4: //4限时折扣
+			discount := util.Float64All(lottery["discount"])
+			preferential := (1 - discount/10) * price
+			preferential = math.Trunc(preferential*1e0 + 0.5)
+			if preferential > discountPrice {
+				discountPrice = preferential
+			}
+			break
+		case 5: //5限时减免
+			reduce := util.Float64All(lottery["reduce"])
+			if reduce > discountPrice {
+				discountPrice = reduce
+			}
+			break
+		default:
+			if full <= price {
+				reduce := util.Float64All(lottery["reduce"]) * 100
+				if reduce > discountPrice {
+					discountPrice = reduce
+				}
+			}
+			break
 		}
 	}
 	if discountPrice > 0 {
@@ -190,38 +260,6 @@ func OptimalSelection(id string, price float64, fool bool) (bool, float64) {
 	return false, discountPrice
 }
 
-// Getlottery 课程奖券获取
-func Getlottery(courseId string) {
-	var appheader = "application/x-www-form-urlencoded"
-	activityApi, _ := config.Sysconfig["activityApi"].(string)
-	url := activityApi + "/activityLottery"
-	param := "userId=jianyuCourse&appId=10000&productCode=" + courseId + "&receivingLocation=1"
-	resp, status, _ := Http_M("GET", url, appheader, param, 20)
-	lotteryArray := []interface{}{}
-	lotteryArrayKey := "lotteryArray" + "_" + courseId
-	fmt.Println("11111111", url)
-	if status != 200 && (resp == nil || len(resp) <= 0) {
-		redis.PutKV(lotteryArrayKey, lotteryArray)
-		return
-	}
-	log.Println(resp, "获取活动下奖券")
-	if resp["Data"] == nil {
-		redis.PutKV(lotteryArrayKey, lotteryArray)
-		return
-	}
-	lotteryArray = resp["Data"].([]interface{})
-	if resp["Activity"] == nil {
-		redis.PutKV(lotteryArrayKey, lotteryArray)
-		return
-	}
-	activityJson := resp["Activity"].(map[string]interface{})
-	if activityJson["UseProductList"] == nil {
-		redis.PutKV(lotteryArrayKey, lotteryArray)
-		return
-	}
-	redis.PutKV(lotteryArrayKey, lotteryArray)
-}
-
 // Library 文库
 func Library(num int) (map[string]interface{}, error) {
 	if data := redis.Get("other", fmt.Sprintf("library_%d", num)); data != nil {

+ 0 - 34
src/jfw/jyutil/jyutil.go

@@ -2,7 +2,6 @@ package jyutil
 
 import (
 	"app.yhyue.com/moapp/jypkg/compatible"
-	"bytes"
 	"encoding/json"
 	"fmt"
 	"io/ioutil"
@@ -224,36 +223,3 @@ func NewClient() *http.Client {
 	}
 	return &http.Client{Transport: transport}
 }
-
-func Http_M(hrefType, href, contentType, param string, timeout int) (map[string]interface{}, int, int64) {
-	t1 := time.Now().UnixNano()
-	client := &http.Client{}
-	client.Timeout = time.Duration(timeout) * time.Second
-	req, _ := http.NewRequest(hrefType, href, bytes.NewReader([]byte{}))
-	req.Header.Set("Content-Type", contentType)
-	resp, err := client.Do(req)
-	if hrefType == "GET" {
-		resp, err = http.Get(href + "?" + param)
-	} else {
-		resp, err = http.Post(href+"?"+param, contentType, nil)
-	}
-	if err != nil {
-		return nil, -1, 0
-	}
-	robots, err := ioutil.ReadAll(resp.Body)
-	resp.Body.Close()
-	if err != nil {
-		return nil, -1, 0
-	}
-	var resData = map[string]interface{}{}
-	json.Unmarshal(robots, &resData)
-	t2 := time.Now().UnixNano()
-	return resData, resp.StatusCode, t2 - t1
-}
-
-func IterToMap(obj interface{}) []map[string]interface{} {
-	m := []map[string]interface{}{}
-	j, _ := json.Marshal(obj)
-	_ = json.Unmarshal(j, &m)
-	return m
-}