WH01243 4 жил өмнө
parent
commit
bca362c00b

+ 14 - 7
rpc/userlib/internal/logic/docdownloadlogic.go

@@ -25,9 +25,10 @@ func NewDocDownloadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DocDo
 		Logger: logx.WithContext(ctx),
 	}
 }
+
 //转存操作
 func (l *DocDownloadLogic) DocDownload(in *userlib.UserCollectRequest) (*userlib.UserCollectResponse, error) {
-	b := userLibService.UserDocDownload(
+	b, msg := userLibService.UserDocDownload(
 		&model.UserDoc{
 			UserId:          in.UserId,
 			DocId:           in.DocId,
@@ -40,11 +41,17 @@ func (l *DocDownloadLogic) DocDownload(in *userlib.UserCollectRequest) (*userlib
 			DocPageSize:     int(in.DocPageSize),
 			DocSummary:      in.DocSummary,
 			DocSourceUserId: in.SourceUserId,
-		}, int(in.Cost),config.Configs.FileSystemConf.Etcd.Hosts,config.Configs.FileSystemConf.Etcd.Key)
+		}, int(in.Cost), config.Configs.FileSystemConf.Etcd.Hosts, config.Configs.FileSystemConf.Etcd.Key)
 	log.Printf("用户文档收藏,userId:[%s],docId:[%s] 是否成功:[%v]", in.UserId, in.DocId, b)
-	return &userlib.UserCollectResponse{
-		Code:    200,
-		Message: "success",
-	}, nil
-	return &userlib.UserCollectResponse{}, nil
+	if b {
+		return &userlib.UserCollectResponse{
+			Code:    int32(1),
+			Message: msg,
+		}, nil
+	} else {
+		return &userlib.UserCollectResponse{
+			Code:    int32(0),
+			Message: msg,
+		}, nil
+	}
 }

+ 5 - 4
rpc/userlib/test/userLib_test.go

@@ -1,6 +1,7 @@
 package test
 
 import (
+	"app.yhyue.com/moapp/jy_docs/rpc/userlib/userlib"
 	"app.yhyue.com/moapp/jy_docs/rpc/userlib/userlibclient"
 	"context"
 	"github.com/tal-tech/go-zero/core/discov"
@@ -57,13 +58,13 @@ func Test_UserDocDelete(t *testing.T)  {
 func Test_UserDocDwnload(t *testing.T) {
 	client := zrpc.MustNewClient(zrpc.RpcClientConf{
 		Etcd: discov.EtcdConf{
-			Hosts: []string{"127.0.0.1:2379"},
+			Hosts: []string{"127.0.0.1:2380"},
 			Key:   "jydocs.userlib.rpc",
 		},
 	})
 	userLib := userlibclient.NewUserLib(client)
 	resp, err := userLib.DocDownload(context.Background(), &userlibclient.UserCollectRequest{
-		DocId:        "1",
+		DocId:        "123",
 		UserId:       "wh5",
 		SourceUserId: "2",
 		DocName:      "2020招标文件白皮书.docx",
@@ -72,10 +73,10 @@ func Test_UserDocDwnload(t *testing.T) {
 		DocPageSize:  20,
 		DocCategory:  userlib.UserDocCategory_Collect,
 		DocFileSuffix:"docx",
-		Cost:100,
+		Cost:1000,
 	})
 	if err != nil {
 		log.Println("UserDoc Collect call error:", err)
 	}
-	log.Print("UserDoc Collect call response:", resp)
+	log.Print("UserDoc Collect call response:", resp.Message,resp.Code)
 }

+ 0 - 3
services/model/stdlib.go

@@ -27,9 +27,6 @@ type Doc struct {
 	Price            int       `json:"price"`
 	DownOrUp         int       `json:"downOrUp"`
 	DocSummary       string    `json:"docSummary"`
-	CreateAt         time.Time `json:"-" gorm:"autoCreateTime"` //标签autoCreateTime设置如果字段名字不为CreatAt时候自动插入当前时间
-	UpdateAt         time.Time `json:"-" gorm:"autoUpdateTime"`
-	DeletedAt        time.Time `json:"-" gorm:"column:delete_at"`
 }
 func (ud *Doc) TableName() string {
 	return "doc"

+ 32 - 11
services/userlib/userDocService.go

@@ -6,6 +6,7 @@ import (
 	"app.yhyue.com/moapp/jy_docs/services/model"
 	docRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
 	"context"
+	"errors"
 	"github.com/tal-tech/go-zero/core/discov"
 	"github.com/tal-tech/go-zero/zrpc"
 	"gorm.io/gorm"
@@ -39,8 +40,9 @@ func UserDocCollect(userDoc *model.UserDoc, cost int) bool {
 }
 
 //转存操作
-func UserDocDownload(userDoc *model.UserDoc, cost int,hosts []string,key string) bool {
+func UserDocDownload(userDoc *model.UserDoc, cost int,hosts []string,key string) (bool,string) {
 	log.Println("UserDocCollect exec ......")
+	msg:="转存成功"
 	err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
 		//用户文库表添加记录
 		//获取文档有关信息
@@ -48,11 +50,13 @@ func UserDocDownload(userDoc *model.UserDoc, cost int,hosts []string,key string)
 		userDocDownloadList := []model.UserDoc{}
 		err := docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and docId=? and docCategory=? and (isDelete=? or isDelete=?)", userDoc.UserId, userDoc.DocId, model.UserDocCategory_Download, model.UserDocStatus_Normal, model.UserDocStatus_LogicDelete).Find(&userDocDownloadList)
 		if err.Error != nil {
+			log.Println("查询转存记录失败:",err)
+			msg="查询转存记录失败"
 			return err.Error
 		}
 		if len(userDocDownloadList) > 0 {
-			return nil
-		}
+			msg="之前已经下载不能再次下载"
+			return errors.New("之前已经下载不能再次下载")}
 		//先扣除积分
 		//rpc
 		client := zrpc.MustNewClient(zrpc.RpcClientConf{
@@ -71,30 +75,43 @@ func UserDocDownload(userDoc *model.UserDoc, cost int,hosts []string,key string)
 		res, pointsErr := integralLib.IntegralConsume(context.Background(), req)
 		log.Println("err ", pointsErr)
 		log.Println("req ", res)
-		if (pointsErr != nil || res.Code == 0) {
+		if (pointsErr != nil ) {
+			log.Println("扣除积分失败:",pointsErr)
+			msg="扣除积分失败"
 			return pointsErr
 		}
-
+		if (res.Code == 0) {
+			log.Println("扣除积分失败:",pointsErr)
+			msg=res.Message
+			return  errors.New(res.Message)
+		}
 		//查询之前有无收藏记录 有收藏记录类型改为下载,状态改为未删除、有收藏记录插入一条新纪录
 		userDocCollectionList := []model.UserDoc{}
 		err = docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and docId=? and docCategory=? and (isDelete=? or isDelete=?)", userDoc.UserId, userDoc.DocId, model.UserDocCategory_Collect, model.UserDocStatus_Normal, model.UserDocStatus_LogicDelete).Find(&userDocCollectionList)
 		if err.Error != nil {
+			log.Println("收藏记录查询:",err)
+			msg="收藏记录查询"
 			return err.Error
 		}
 		if len(userDocCollectionList) > 0 {
 			//修改收藏为下载
 			if err := tx.Exec("update user_doc set docCategory=? , isDelete=?  ,update_at=?  where  id =?", model.UserDocCategory_Download, model.UserDocStatus_Normal, time.Now(), userDocCollectionList[0].ID).Error; err != nil {
+				log.Println("收藏记录更改为转存记录失败:",err)
+				msg="收藏记录更改为转存记录失败"
+				tx.Rollback()
 				return err
 			}
 			if err.Error != nil {
-				log.Println("userDocCollect error:", err)
+				log.Println("转存操作流水添加失败:", err)
+				msg="收藏记录更改为转存记录失败"
 				tx.Rollback()
 				return err.Error
 			}
 			//用户收藏、转存记录表添加记录
 			err := docRpcUtil.GetJyDocsDB().Exec("insert into download_collection_record (docId,userId,sourceUserId,category,cost,date) values (?,?,?,?,?,?)", userDoc.DocId, userDoc.UserId, userDoc.DocSourceUserId, model.UserDocCategory_Download, cost, time.Now())
 			if err.Error != nil {
-				log.Println("userDocCollect record insert error:", err)
+				log.Println("转存操作流水添加失败:", err)
+				msg="转存操作流水添加失败"
 				tx.Rollback()
 				return err.Error
 			}
@@ -103,16 +120,20 @@ func UserDocDownload(userDoc *model.UserDoc, cost int,hosts []string,key string)
 			userDoc.CreateAt = time.Now()
 			userDoc.UpdateAt = time.Now()
 			userDoc.DeletedAt = time.Now()
+			//获取文件基本信息
+
 			err = docRpcUtil.GetJyDocsDB().Create(userDoc)
 			if err.Error != nil {
-				log.Println("userDocCollect error:", err)
+				log.Println("转存操作添加失败:", err)
+				msg="转存操作添加失败"
 				tx.Rollback()
 				return err.Error
 			}
 			//用户收藏、转存记录表添加记录
 			err := docRpcUtil.GetJyDocsDB().Exec("insert into download_collection_record (docId,userId,sourceUserId,category,cost,date) values (?,?,?,?,?,?)", userDoc.DocId, userDoc.UserId, userDoc.DocSourceUserId, model.UserDocCategory_Download, cost, time.Now())
 			if err.Error != nil {
-				log.Println("userDocCollect record insert error:", err)
+				log.Println("转存操作流水添加失败:", err)
+				msg="转存操作流水添加失败"
 				tx.Rollback()
 				return err.Error
 			}
@@ -120,9 +141,9 @@ func UserDocDownload(userDoc *model.UserDoc, cost int,hosts []string,key string)
 		return nil
 	})
 	if err != nil {
-		return false
+		return false,msg
 	}
-	return true
+	return true,msg
 }
 func UserDocDelete(docId string) bool {
 	err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {