lijian 6 gadi atpakaļ
vecāks
revīzija
3ca8300f72

+ 2 - 0
pkg/models/application.go

@@ -24,6 +24,8 @@ type Application struct {
 	AppDomain string `sql:"type:varchar(200);not null;"`
 	// vendor id
 	VendorID uint
+	AppIcon  string
+	AppType  string
 }
 
 // Validate 验证规则

+ 34 - 0
services/knowoapi/controllers/application.go

@@ -2,7 +2,10 @@ package controllers
 
 import (
 	"sparrow/pkg/models"
+	"sparrow/pkg/rpcs"
+	"sparrow/pkg/server"
 	"sparrow/services/knowoapi/services"
+	"strings"
 
 	"github.com/kataras/iris"
 )
@@ -23,6 +26,21 @@ func (a *AppController) Post() {
 		return
 	}
 	app.VendorID = a.Token.getVendorID(a.Ctx)
+	if app.AppIcon != "" {
+		//move image
+		args := &rpcs.ArgsMoveFile{
+			Source: app.AppIcon,
+			Target: "application",
+		}
+		reply := &rpcs.ReplyMoveFile{}
+		err := server.RPCCallByName("fileaccess", "FileAccess.MoveFile", args, reply)
+		if err != nil {
+			server.Log.Error(err)
+			app.AppIcon = ""
+		} else {
+			app.AppIcon = reply.FilePath
+		}
+	}
 	err := a.Service.Create(app)
 	if err != nil {
 		responseError(a.Ctx, ErrDatabase, err.Error())
@@ -91,6 +109,22 @@ func (a *AppController) Put() {
 		badRequest(a.Ctx, err)
 		return
 	}
+	// check update image
+	if strings.Contains(app.AppIcon, "tmp") {
+		//move image
+		args := &rpcs.ArgsMoveFile{
+			Source: app.AppIcon,
+			Target: "application",
+		}
+		reply := &rpcs.ReplyMoveFile{}
+		err := server.RPCCallByName("fileaccess", "FileAccess.MoveFile", args, reply)
+		if err != nil {
+			server.Log.Error(err)
+			app.AppIcon = ""
+		} else {
+			app.AppIcon = reply.FilePath
+		}
+	}
 	result, err := a.Service.Update(app)
 	if err != nil {
 		responseError(a.Ctx, ErrDatabase, err.Error())

+ 18 - 0
services/knowoapi/controllers/produdct.go

@@ -5,6 +5,7 @@ import (
 	"sparrow/pkg/rpcs"
 	"sparrow/pkg/server"
 	"sparrow/services/knowoapi/services"
+	"strings"
 
 	"github.com/kataras/iris"
 )
@@ -79,6 +80,23 @@ func (a *ProductController) Put() {
 		responseError(a.Ctx, ErrNormal, "非法操作")
 		return
 	}
+	// check update image
+	if strings.Contains(product.ProductImage, "tmp") {
+		//move image
+		args := &rpcs.ArgsMoveFile{
+			Source: product.ProductImage,
+			Target: "product",
+		}
+		reply := &rpcs.ReplyMoveFile{}
+		err := server.RPCCallByName("fileaccess", "FileAccess.MoveFile", args, reply)
+		if err != nil {
+			server.Log.Error(err)
+			product.ProductImage = ""
+		} else {
+			product.ProductImage = reply.FilePath
+		}
+
+	}
 	pro, err := a.Service.Update(product)
 	if err != nil {
 		responseError(a.Ctx, ErrDatabase, err.Error())

+ 1 - 1
services/knowoapi/model/alert.go

@@ -46,7 +46,7 @@ func (a *Alert) Update(vendorid uint, alert *models.AlarmRule) (data models.Alar
 	if _, ok := cache.Get(key); ok {
 		cache.Delete(key)
 	}
-	err = a.db.Model(&data).Update(alert).Where("vendor_id = ?", vendorid).Error
+	err = a.db.Model(&data).Save(alert).Where("vendor_id = ?", vendorid).Error
 	if err == nil {
 		cache.Set(key, &data)
 	}

+ 1 - 1
services/knowoapi/model/application.go

@@ -82,6 +82,6 @@ func (a *Application) Update(app *models.Application) (data models.Application,
 		cache.Delete(key)
 	}
 	cache.Set(key, app)
-	err = a.db.Model(&data).Update(app).Error
+	err = a.db.Model(&data).Save(app).Error
 	return
 }