Explorar o código

Merge branch 'master' of https://app.yhyue.com/moapp/jy_docs

xuzhiheng %!s(int64=4) %!d(string=hai) anos
pai
achega
dd787f2059

+ 5 - 8
rpc/stdlib/internal/logic/docofflogic.go

@@ -3,7 +3,7 @@ package logic
 import (
 	stdlibService "app.yhyue.com/moapp/jydocs/services/stdlib"
 	"context"
-	"fmt"
+	"log"
 
 	"app.yhyue.com/moapp/jydocs/rpc/stdlib/internal/svc"
 	"app.yhyue.com/moapp/jydocs/rpc/stdlib/stdlib"
@@ -27,11 +27,8 @@ func NewDocOffLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DocOffLogi
 
 func (l *DocOffLogic) DocOff(in *stdlib.DocChangeReq) (*stdlib.DocChangeResp, error) {
 	// todo: add your logic here and delete this line
-	err := stdlibService.ChangeDocumentOff(in.Id, in.Reason)
-	fmt.Print(err)
-	if err == nil {
-		return &stdlib.DocChangeResp{State: true}, nil
-	} else {
-		return &stdlib.DocChangeResp{State: false}, nil
-	}
+	status := stdlibService.ChangeDocumentOff(in.Id, in.Reason)
+	log.Println(status,"返回值-----------")
+	return &stdlib.DocChangeResp{State: status}, nil
+
 }

+ 2 - 10
rpc/stdlib/internal/logic/doconlogic.go

@@ -5,8 +5,6 @@ import (
 	"app.yhyue.com/moapp/jydocs/rpc/stdlib/stdlib"
 	stdlibService "app.yhyue.com/moapp/jydocs/services/stdlib"
 	"context"
-	"fmt"
-
 	"github.com/tal-tech/go-zero/core/logx"
 )
 
@@ -26,12 +24,6 @@ func NewDocOnLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DocOnLogic
 
 func (l *DocOnLogic) DocOn(in *stdlib.DocChangeReq) (*stdlib.DocChangeResp, error) {
 	// todo: add your logic here and delete this line
-	err := stdlibService.ChangeDocumentOn(in.Id, in.Reason)
-	fmt.Println(err)
-	if err == nil {
-		return &stdlib.DocChangeResp{State: true}, nil
-	} else {
-		return &stdlib.DocChangeResp{State: false}, nil
-	}
-
+	status := stdlibService.ChangeDocumentOn(in.Id, in.Reason)
+	return &stdlib.DocChangeResp{State: status}, nil
 }

+ 15 - 5
rpc/stdlib/test/doc_test.go

@@ -12,18 +12,28 @@ import (
 )
 
 func Test_DocOn(t *testing.T) {
-	ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
+	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
 	std := stdlibclient.NewStdlib(zrpc.MustNewClient(zrpc.RpcClientConf{Etcd: discov.EtcdConf{Hosts: []string{"192.168.3.240:2379"}, Key: "jydocs.stdlib.rpc"}}))
 	req := &stdlib.DocChangeReq{Id: "123", Reason: 1}
 	res, err := std.DocOn(ctx, req)
 	log.Println("err ", err)
-	log.Println("req ", res.State)
+	if res.State == true {
+		log.Println("req true")
+	} else {
+		log.Println("req false", res)
+	}
 }
 func Test_DocOff(t *testing.T) {
-	ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
+	ctx, _ := context.WithTimeout(context.Background(), 120*time.Second)
 	std := stdlibclient.NewStdlib(zrpc.MustNewClient(zrpc.RpcClientConf{Etcd: discov.EtcdConf{Hosts: []string{"192.168.3.240:2379"}, Key: "jydocs.stdlib.rpc"}}))
 	req := &stdlib.DocChangeReq{Id: "123", Reason: 10}
 	res, err := std.DocOff(ctx, req)
 	log.Println("err ", err)
-	log.Println("req ", res.State)
-}
+	if res.State == true {
+		log.Println("req true")
+	} else {
+		log.Println("req false", res)
+	}
+	//log.Println("err ", err)
+	//log.Println("req ", res)
+}

+ 33 - 21
services/stdlib/docChangeService.go

@@ -1,28 +1,30 @@
 package stdlib
 
 import (
-	"errors"
-	"time"
-
+	elastic "app.yhyue.com/moapp/jybase/esv7"
 	jyDocsRpcUtil "app.yhyue.com/moapp/jydocs/services/util"
+	"errors"
 	"gorm.io/gorm"
+	"log"
+	"time"
 )
 
 const DocOn = 1
 const DocOff = 0
 
 // 上架文档
-func ChangeDocumentOn(id string, reason int32) error {
+func ChangeDocumentOn(id string, reason int32) bool {
 	// 查文档
 	docData := map[string]interface{}{}
 	rs := jyDocsRpcUtil.GetJyDocsDB().Table("doc").Where("id=? and isDelete=0", id).Find(&docData)
 	if rs.Error != nil {
-		return rs.Error
+		log.Println("上架文档 error:"+id, rs.Error)
+		return false
 	} else {
 		if docData["id"] == nil {
-			return errors.New("文档不存在")
+			log.Println("上架文档 error:"+id, "文档不存在")
+			return false
 		}
-
 	}
 	// 上架
 	err := jyDocsRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
@@ -35,26 +37,32 @@ func ChangeDocumentOn(id string, reason int32) error {
 		if err := tx.Exec("insert into doc_change_record (docId,date,downOrUp,reason) values (?,?,?,?)", id, date, DocOn, reason).Error; err != nil {
 			return err
 		}
-		// todo es加数据
-		//if rs := elastic.Save("index", "doc", docData); rs != true {
-		//	return errors.New("发布到检索库失败")
-		//}
+		docData["_id"] = docData["id"]
+		// todo es加数据 索引待替换 测试
+		if rs := elastic.Save("doc_lib", "doc", docData); rs != true {
+			return errors.New("发布到检索库失败")
+		}
 		return nil
 	})
-
-	return err
+	log.Println("上架文档 error:"+id, err)
+	if err != nil {
+		return false
+	}
+	return true
 }
 
 // 下架文档
-func ChangeDocumentOff(id string, reason int32) error {
+func ChangeDocumentOff(id string, reason int32) bool {
 	// 查文档
 	docData := map[string]interface{}{}
 	rs := jyDocsRpcUtil.GetJyDocsDB().Table("doc").Where("id=? and isDelete=0", id).Find(&docData)
 	if rs.Error != nil {
-		return rs.Error
+		log.Println("下架文档 error:"+id, rs.Error)
+		return false
 	} else {
 		if docData["id"] == nil {
-			return errors.New("文档不存在")
+			log.Println("下架文档 error:"+id, "文档不存在")
+			return false
 		}
 	}
 	// 下架
@@ -68,12 +76,16 @@ func ChangeDocumentOff(id string, reason int32) error {
 		if err := tx.Exec("insert into doc_change_record (docId,date,downOrUp,reason) values (?,?,?,?)", id, date, DocOff, reason).Error; err != nil {
 			return err
 		}
-		//todo 从es删除数据
-		//if rs := elastic.DelById("index", "doc", id); rs != true {
-		//	return errors.New("从检索库删除失败")
-		//}
+		//todo 从es删除数据 索引待替换 及测试
+		if rs := elastic.DelById("doc_lib", "doc", id); rs != true {
+			return errors.New("从检索库删除失败")
+		}
 		return nil
 	})
 
-	return err
+	if err != nil {
+		log.Println("上架文档 error:"+id, err)
+		return false
+	}
+	return true
 }