瀏覽代碼

wip:购买会员及购买文档

wangshan 1 年之前
父節點
當前提交
98bd5a8cfd

+ 8 - 5
rpc/partnerlib/crontab/service/docinInfo.go

@@ -11,6 +11,7 @@ import (
 	"encoding/hex"
 	"encoding/json"
 	"fmt"
+	"html"
 	"strings"
 	"time"
 )
@@ -30,8 +31,10 @@ func InsertDocinInfos(b []byte) (err error, lastId int64, expectTotal, actualTot
 			)
 			for _, v := range docinInfos.Data {
 				var (
+					productName       = html.UnescapeString(v.ProductName)
+					desc              = html.UnescapeString(v.Desc)
 					id                = fmt.Sprintf("%s-%d", entity.PartnerName, v.ProductId)
-					md5Id             = GetMD5(id, v.ProductName, v.Desc, v.FilePostfix) //判断数据是否已存在 md5
+					md5Id             = GetMD5(id, productName, v.Desc, v.FilePostfix)   //判断数据是否已存在 md5
 					price             = int(v.Price * entity.Multiple)                   //价格转换
 					docTags, docClass = partner.SwitchDocClass(v.PcatName, v.CatName, 1) //标签和分类
 					fileType          = GetDocFileType(v.FilePostfix)
@@ -53,7 +56,7 @@ func InsertDocinInfos(b []byte) (err error, lastId int64, expectTotal, actualTot
 					Id:              id,
 					UserId:          entity.PartnerName,
 					AppId:           entity.AppId,
-					DocName:         v.ProductName,
+					DocName:         productName,
 					DocFileType:     fileType,
 					DocFileSuffix:   v.FilePostfix,
 					DocFileSize:     int(v.FileSize),
@@ -68,7 +71,7 @@ func InsertDocinInfos(b []byte) (err error, lastId int64, expectTotal, actualTot
 					OssTxtId:        "",
 					Price:           price,
 					DownOrUp:        0,
-					DocSummary:      v.Desc,
+					DocSummary:      desc,
 					PreviewImgId:    common.InterfaceToStr(v.ProductId),
 					EncryptionLevel: 0,
 					Source:          2,                          //豆丁
@@ -81,9 +84,9 @@ func InsertDocinInfos(b []byte) (err error, lastId int64, expectTotal, actualTot
 					"docClass":     docClass,
 					"docFileSize":  int(v.FileSize),
 					"docFileType":  fileType,
-					"docName":      v.ProductName,
+					"docName":      productName,
 					"docPageSize":  v.PageCount,
-					"docSummary":   v.Desc,
+					"docSummary":   desc,
 					"docTags":      strings.Join(docTags, ","),
 					"downTimes":    v.DownloadCount,
 					"previewImgId": common.InterfaceToStr(v.ProductId),

+ 2 - 0
rpc/partnerlib/entity/entity.go

@@ -16,6 +16,8 @@ const (
 	DocUserOrderTab         = "doc_user_order"
 	DocClassTable           = "doc_class"
 	DocClassStatisticsTable = "doc_class_statistics"
+	OrderTable              = "jianyu.dataexport_order"
+	ProductType             = "剑鱼文库会员"
 )
 
 var (

+ 201 - 45
rpc/partnerlib/service/userBuy.go

@@ -8,9 +8,12 @@ import (
 	"app.yhyue.com/moapp/jy_docs/rpc/partnerlib/warn"
 	"app.yhyue.com/moapp/jy_docs/services/model"
 	"app.yhyue.com/moapp/jy_docs/services/partner"
+	"app.yhyue.com/moapp/jybase/common"
 	"app.yhyue.com/moapp/jybase/date"
 	"encoding/json"
 	"fmt"
+	"strings"
+	"time"
 )
 
 type BRes struct {
@@ -19,63 +22,216 @@ type BRes struct {
 	OrderNo string
 }
 
+//
+//func UBHandle(in *partnerlib.UserBuyVipRequest) (res *partnerlib.UBVRes, err error) {
+//	res = new(partnerlib.UBVRes)
+//	var (
+//		b       []byte
+//		docInfo = partner.DocsFindOne(in.DocId)
+//		userId  = in.PositionId
+//		id      = strings.ReplaceAll(in.DocId, fmt.Sprintf("%s-", entity.PartnerName), "")
+//	)
+//	//测试账号
+//	if IC.I.Docin.TestAccount > 0 {
+//		userId = IC.I.Docin.TestAccount
+//	}
+//	if docInfo.Id != "" {
+//		orderCode := util.GetOrderCode()
+//		h := NewHH(IC.I.Docin.Name, IC.I.Docin.Host, IC.I.Docin.BuyRecord.Name, IC.I.Docin.BuyRecord.Pathname, IC.I.Docin.BuyRecord.Method, map[string]interface{}{
+//			"orderNo":   orderCode,
+//			"productId": id,
+//			"userId":    userId,
+//			"price":     docInfo.Price,
+//			"priceVip":  docInfo.PriceVip,
+//			"type":      in.Type,
+//			"orderTime": date.NowFormat(entity.DateFullLayout),
+//		})
+//		b, err, _ = h.HttpFunc()
+//		go h.SaveDocinLogger(b, err, "req")
+//		if err == nil {
+//			//豆丁同步数据到mongo
+//			go h.SaveDocinLogger(b, err, "res")
+//			bRes := &BRes{}
+//			if err = json.Unmarshal(b, &bRes); err == nil {
+//				if bRes.Code == 0 {
+//					//更新文档购买记录
+//					if err = partner.InsertBuyDocOrder(&model.DocinUserOrder{
+//						PositionId:   in.PositionId,
+//						MgoUserId:    in.MgoUserId,
+//						Phone:        in.Phone,
+//						DocId:        docInfo.Id,
+//						OrderCode:    orderCode,
+//						Price:        docInfo.Price,
+//						PriceVip:     docInfo.PriceVip,
+//						PurchaseType: int(in.Type), //购买类型:1 币买文档,2 券买文档,3 月大会员,4 季大会员,5 年大会员
+//						State:        1,            //合作商:1:豆丁
+//						CreateDate:   date.NowFormat(date.Date_Full_Layout),
+//					}); err == nil {
+//						warn.SendMsgByWXURL(fmt.Sprintf("%s文档名称: %s,文档id:%s,购买订单编号:%s,请求信息:%v", entity.PartnerName, docInfo.DocName, docInfo.Id, orderCode, in))
+//					}
+//					res.OrderNo = bRes.OrderNo
+//					res.Msg = bRes.Msg
+//					res.Code = bRes.Code
+//					return
+//				}
+//				err = fmt.Errorf(entity.Res[res.Msg])
+//			}
+//		}
+//	} else {
+//		err = fmt.Errorf("参数异常")
+//	}
+//	res.Msg = err.Error()
+//	res.Code = -1
+//	return
+//}
+
+// 开通会员 + 购买精品文档
 func UBHandle(in *partnerlib.UserBuyVipRequest) (res *partnerlib.UBVRes, err error) {
+	//in.Type 0:开通豆丁会员并购买文档,1 币买文档,2 券买文档,3 月大会员,4 季大会员,5 年大会员
+	//in.Type ==0 判断文档是否是精品,如果文档是精品需要购买文档
 	res = new(partnerlib.UBVRes)
 	var (
 		b       []byte
 		docInfo = partner.DocsFindOne(in.DocId)
-		userId  = in.PositionId
-	)
-	//测试账号
-	if IC.I.Docin.TestAccount > 0 {
-		userId = IC.I.Docin.TestAccount
-	}
-	if docInfo.Id != "" {
-		orderCode := util.GetOrderCode()
-		h := NewHH(IC.I.Docin.Name, IC.I.Docin.Host, IC.I.Docin.BuyRecord.Name, IC.I.Docin.BuyRecord.Pathname, IC.I.Docin.BuyRecord.Method, map[string]interface{}{
-			"orderNo":   orderCode,
-			"productId": in.DocId,
+		id      = strings.ReplaceAll(in.DocId, fmt.Sprintf("%s-", entity.PartnerName), "")
+		price   = float64(docInfo.Price) / 10
+		userId  = common.If(IC.I.Docin.TestAccount > 0, IC.I.Docin.TestAccount, in.PositionId).(int64) //测试账号
+		docType = in.Type
+		params  = map[string]interface{}{
 			"userId":    userId,
-			"price":     docInfo.Price,
-			"priceVip":  docInfo.PriceVip,
-			"type":      in.Type,
 			"orderTime": date.NowFormat(entity.DateFullLayout),
-		})
-		b, err, _ = h.HttpFunc()
-		go h.SaveDocinLogger(b, err, "req")
-		if err == nil {
-			//豆丁同步数据到mongo
-			go h.SaveDocinLogger(b, err, "res")
-			bRes := &BRes{}
-			if err = json.Unmarshal(b, &bRes); err == nil {
-				if bRes.Code == 0 {
-					//更新文档购买记录
-					if err = partner.InsertBuyDocOrder(&model.DocinUserOrder{
-						PositionId:   in.PositionId,
-						MgoUserId:    in.MgoUserId,
-						Phone:        in.Phone,
-						DocId:        docInfo.Id,
-						OrderCode:    orderCode,
-						Price:        docInfo.Price,
-						PriceVip:     docInfo.PriceVip,
-						PurchaseType: int(in.Type), //购买类型:1 币买文档,2 券买文档,3 月大会员,4 季大会员,5 年大会员
-						State:        1,            //合作商:1:豆丁
-						CreateTime:   date.NowFormat(date.Date_Full_Layout),
-					}); err == nil {
-						warn.SendMsgByWXURL(fmt.Sprintf("%s文档名称: %s,文档id:%s,购买订单编号:%s,请求信息:%v", entity.PartnerName, docInfo.DocName, docInfo.Id, orderCode, in))
+			"type":      docType,
+		}
+		now      = time.Now()
+		cacheKey = fmt.Sprintf("docin_user_member_%d_%d", now.Day(), in.PositionId)
+		nextStep bool
+		resFunc  = func(params map[string]interface{}, duo *model.DocinUserOrder) (msg string, err error) {
+			h := NewHH(IC.I.Docin.Name, IC.I.Docin.Host, IC.I.Docin.BuyRecord.Name, IC.I.Docin.BuyRecord.Pathname, IC.I.Docin.BuyRecord.Method, params)
+			b, err, _ = h.HttpFunc()
+			go h.SaveDocinLogger(b, err, "req")
+			if err == nil && len(b) > 0 {
+				//豆丁同步数据到mongo
+				go h.SaveDocinLogger(b, err, "res")
+				bRes := &BRes{}
+				err = json.Unmarshal(b, &bRes)
+				if err == nil {
+					if bRes.Code == 0 {
+						//更新文档购买记录
+						if err = partner.InsertBuyDocOrder(duo); err != nil {
+							warn.SendMsgByWXURL(fmt.Sprintf("%s文档名称: %s,文档id:%s,购买订单编号:%s,请求信息:%v,异常信息:%s", entity.PartnerName, docInfo.DocName, docInfo.Id, params["orderNo"], in, err.Error()))
+						}
+						msg = bRes.Msg
+						return
 					}
-					res.OrderNo = bRes.OrderNo
-					res.Msg = bRes.Msg
-					res.Code = bRes.Code
-					return
+					err = fmt.Errorf(entity.Res[res.Msg])
+				}
+			}
+			err = fmt.Errorf("接口请求异常")
+			warn.SendMsgByWXURL(fmt.Sprintf("%s文档名称: %s,文档id:%s,购买会员编号:%s,请求信息:%v,异常信息:%s", entity.PartnerName, docInfo.DocName, docInfo.Id, params["orderNo"], in, err.Error()))
+			return
+		}
+	)
+	switch in.Type {
+	case 1: //1 币买文档
+		params["price"] = price
+		params["productId"] = in.DocId
+		nextStep = true
+	case 2: //2 券买文档
+		params["priceVip"] = docInfo.PriceVip
+		params["productId"] = in.DocId
+		nextStep = true
+	case 0: //0:开通豆丁会员并购买文档
+		//缓存获取 豆丁会员信息
+		if dStr := partner.GetUserOrderInfo(entity.RedisCode, cacheKey); dStr == "" {
+			//查库豆丁会员信息
+			doi, err := partner.GetDocOrderInfo(in.PositionId, in.MgoUserId, 2)
+			if err != nil { //查询异常
+				res.Code = -1
+				res.Msg = err.Error()
+				return res, err
+			}
+			switch doi.State { //会员状态:-1:不可用;1:正常
+			case 1: //不用再购买会员 并更新缓存
+				docType = int64(doi.PurchaseType) //3 月大会员,4 季大会员,5 年大会员
+				nextStep = true
+			default: //查看剑鱼订单,如果剑鱼订单有购买文库会员记录,购买信息保存到doc_user_order表 并更新缓存
+				jyDoi := partner.GetJYDocsOrderInfo(in.PositionId, in.MgoUserId)
+				if jyDoi.OrderCode == "" {
+					res.Code = -1
+					res.Msg = fmt.Sprintf("当前用户未购买文库会员")
+					return res, fmt.Errorf(res.Msg)
+				}
+				var orderCode = util.GetOrderCode()
+				//文库会员周期单位 同超级订阅单位:cycleunit(1:年 2:月 3:天 4:季)
+				switch jyDoi.CycleUnit {
+				case 1:
+					docType = 5
+				case 2:
+					docType = 3
+				case 4:
+					docType = 4
+				}
+				//购买信息保存到doc_user_order表
+				duo := &model.DocinUserOrder{
+					PositionId:   in.PositionId,
+					MgoUserId:    in.MgoUserId,
+					Phone:        in.Phone,
+					DocId:        docInfo.Id,
+					JyOrderCode:  jyDoi.OrderCode,
+					OrderCode:    orderCode,
+					StartDate:    jyDoi.StartDate,
+					EndDate:      jyDoi.EndDate,
+					Price:        docInfo.Price,
+					PriceVip:     docInfo.PriceVip,
+					PurchaseType: int(docType),  //购买类型:1 币买文档,2 券买文档,3 月大会员,4 季大会员,5 年大会员
+					State:        int(in.State), //合作商:1:豆丁
+					CreateDate:   date.NowFormat(date.Date_Full_Layout),
+					UpdateDate:   date.NowFormat(date.Date_Full_Layout),
+				}
+				params["type"] = docType
+				params["orderNo"] = orderCode
+				if msg, err := resFunc(params, duo); err == nil {
+					res.Msg = msg
+					nextStep = true
+				} else {
+					warn.SendMsgByWXURL(fmt.Sprintf("%s文档名称: %s,文档id:%s,购买会员编号:%s,请求信息:%v", entity.PartnerName, docInfo.DocName, docInfo.Id, params["orderNo"], in))
 				}
-				err = fmt.Errorf(entity.Res[res.Msg])
+			}
+			//更新用户会员信息缓存
+			partner.SetUserOrderInfo(entity.RedisCode, cacheKey, 24*60*60)
+		} else {
+			nextStep = true
+		}
+	}
+	if nextStep {
+		//是否需要购买文档--精品
+		if docInfo.ProductType == 2 {
+			var orderCode = util.GetOrderCode()
+			//购买信息保存到doc_user_order表
+			duo := &model.DocinUserOrder{
+				PositionId:   in.PositionId,
+				MgoUserId:    in.MgoUserId,
+				Phone:        in.Phone,
+				DocId:        docInfo.Id,
+				OrderCode:    orderCode,
+				Price:        docInfo.Price,
+				PriceVip:     docInfo.PriceVip,
+				PurchaseType: int(docType),  //购买类型:1 币买文档,2 券买文档,3 月大会员,4 季大会员,5 年大会员
+				State:        int(in.State), //合作商:1:豆丁
+				CreateDate:   date.NowFormat(date.Date_Full_Layout),
+				UpdateDate:   date.NowFormat(date.Date_Full_Layout),
+			}
+			params["orderNo"] = orderCode
+			params["productId"] = id
+			if msg, err := resFunc(params, duo); err == nil {
+				res.OrderNo = orderCode
+				res.Msg = msg
+				return res, nil
 			}
 		}
-	} else {
-		err = fmt.Errorf("参数异常")
+		return
 	}
-	res.Msg = err.Error()
+	res.Msg = fmt.Sprintf("参数异常")
 	res.Code = -1
 	return
 }

+ 31 - 10
services/model/partner.go

@@ -34,16 +34,21 @@ func (ud *Docin) TableName() string {
 
 // 精品文库订单信息表
 type DocinUserOrder struct {
-	PositionId   int64  `json:"position_id" gorm:"column:position_id"`
-	MgoUserId    string `json:"mgo_user_id" gorm:"column:mgo_user_id"`
-	Phone        string `json:"phone" gorm:"column:phone"`
-	DocId        string `json:"doc_id" gorm:"column:doc_id"`
-	OrderCode    string `json:"order_code" gorm:"column:order_code"`
-	Price        int    `json:"price" gorm:"column:price"`                 //价格
-	PriceVip     int    `json:"price_vip" gorm:"column:price_vip"`         //券
-	PurchaseType int    `json:"purchase_type" gorm:"column:purchase_type"` //购买类型:1 币买文档,2 券买文档,3 月大会员,4 季大会员,5 年大会员
-	State        int    `json:"state" gorm:"column:state"`
-	CreateTime   string `json:"create_time" gorm:"column:create_time"`
+	PositionId   int64  `json:"position_id" gorm:"column:position_id"`     //职位id
+	MgoUserId    string `json:"mgo_user_id" gorm:"column:mgo_user_id"`     //mgo user id
+	Phone        string `json:"phone" gorm:"column:phone"`                 // 手机号
+	DocId        string `json:"doc_id" gorm:"column:doc_id"`               // 文档id
+	JyOrderCode  string `json:"jy_order_code" gorm:"column:jy_order_code"` // 剑鱼文库会员订单号
+	OrderCode    string `json:"order_code" gorm:"column:order_code"`       // 合作方购买会员订单号
+	StartDate    string `json:"start_date" gorm:"column:start_date"`       // 合作方会员周期开始时间
+	EndDate      string `json:"end_date" gorm:"column:end_date"`           // 合作方会员周期结束时间
+	Price        int    `json:"price" gorm:"column:price"`                 // 价格
+	PriceVip     int    `json:"price_vip" gorm:"column:price_vip"`         // 券
+	PurchaseType int    `json:"purchase_type" gorm:"column:purchase_type"` // 购买类型:1 币买文档,2 券买文档,3 月大会员,4 季大会员,5 年大会员
+	State        int    `json:"state" gorm:"column:state"`                 // 合作方会员状态:-1:不可用;1:正常(定时任务更新此状态
+	Partner      int    `json:"partner" gorm:"column:partner"`             // 合作商:1:豆丁
+	CreateDate   string `json:"create_date" gorm:"column:create_date"`     // 创建时间
+	UpdateDate   string `json:"update_date" gorm:"column:update_date"`     // 更新时间
 }
 
 func (ud *DocinUserOrder) TableName() string {
@@ -60,3 +65,19 @@ type DocClass struct {
 func (dc *DocClass) TableName() string {
 	return "doc_class"
 }
+
+//type DocMember struct {
+//	PositionId int64
+//	MgoUserId  string
+//	Phone      string
+//	OrderCode  string
+//	State      int
+//	Partner    int
+//	StartDate  string
+//	EndDate    string
+//	CreateDate string
+//}
+//
+//func (dm *DocMember) TableName() string {
+//	return "doc_member"
+//}

+ 0 - 12
services/partner/docBuyOrder.go

@@ -1,12 +0,0 @@
-package partner
-
-import (
-	"app.yhyue.com/moapp/jy_docs/rpc/partnerlib/entity"
-	"app.yhyue.com/moapp/jy_docs/services/model"
-	jyDocRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
-)
-
-func InsertBuyDocOrder(orderInfo *model.DocinUserOrder) (err error) {
-	err = jyDocRpcUtil.GetJyDocsDB().Table(entity.DocUserOrderTab).Create(orderInfo).Error
-	return
-}

+ 42 - 0
services/partner/docsBuyOrder.go

@@ -0,0 +1,42 @@
+package partner
+
+import (
+	"app.yhyue.com/moapp/jy_docs/rpc/partnerlib/entity"
+	"app.yhyue.com/moapp/jy_docs/services/model"
+	jyDocRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
+)
+
+// 保存和合作商订单相关的信息
+func InsertBuyDocOrder(orderInfo *model.DocinUserOrder) (err error) {
+	err = jyDocRpcUtil.GetJyDocsDB().Table(entity.DocUserOrderTab).Create(orderInfo).Error
+	return
+}
+
+// 获取和合作商订单相关的信息
+func GetDocOrderInfo(positionId int64, mgoUserId string, state int) (orderInfo *model.DocinUserOrder, err error) {
+	var orderInfos = []*model.DocinUserOrder{}
+	err = jyDocRpcUtil.GetJyDocsDB().Table(entity.DocUserOrderTab).
+		Where("(position_id = ? AND mgo_user_id = ?) AND state = 1 AND purchase_type > ?", positionId, mgoUserId, state).
+		Order("update_date ASC").
+		Find(&orderInfos).Error
+	if err == nil && len(orderInfos) > 0 {
+		orderInfo = orderInfos[0]
+	}
+	return
+}
+
+// 剑鱼文库会员订单信息
+type OrderInfo struct {
+	CycleCount int
+	CycleUnit  int
+	OrderCode  string
+	StartDate  string
+	EndDate    string
+}
+
+// 获取剑鱼文库会员购买信息
+func GetJYDocsOrderInfo(positionId int64, mgoUserId string) (orderInfo *OrderInfo) {
+	var orders = []map[string]interface{}{}
+	jyDocRpcUtil.GetJyDocsDB().Table(entity.OrderTable).Where("order_status = 1 AND product_type = ? AND (user_id = ? OR user_id = ?)", entity.ProductType, positionId, mgoUserId).Select("filter,vip_starttime,vip_endtime").Find(&orders)
+	return
+}

+ 0 - 0
services/partner/docCheck.go → services/partner/docsCheck.go


+ 0 - 0
services/partner/docClass.go → services/partner/docsClass.go


+ 10 - 0
services/partner/redis.go

@@ -26,3 +26,13 @@ func SetUpdateTaskInfo(code, key, startDate string, expireTime int) bool {
 func GetUpdateTaskInfo(code, key string) string {
 	return redis.GetStr(code, key)
 }
+
+// 更新用户会员信息
+func SetUserOrderInfo(code, key string, expireTime int) bool {
+	return redis.Put(code, key, "docin", expireTime)
+}
+
+// 获取文档信息起始时间
+func GetUserOrderInfo(code, key string) string {
+	return redis.GetStr(code, key)
+}