Commit 90330ea2 by mushishixian

导出excel服务

parent d2c46ed7
No preview for this file type
The file could not be displayed because it is too large.
...@@ -73,6 +73,7 @@ func Setup(filePath string) { ...@@ -73,6 +73,7 @@ func Setup(filePath string) {
mapTo("rabbitmq", RabbitMQSetting) mapTo("rabbitmq", RabbitMQSetting)
mapTo("es", ESSetting) mapTo("es", ESSetting)
mapTo("api", ApiSetting) mapTo("api", ApiSetting)
mapTo("oss", OssSetting)
RedisSetting.IdleTimeout = RedisSetting.IdleTimeout * time.Second RedisSetting.IdleTimeout = RedisSetting.IdleTimeout * time.Second
} }
......
...@@ -20,8 +20,10 @@ func BatchSaveMatchings(bomId int, matchingList []model.BomItemMatching) (err er ...@@ -20,8 +20,10 @@ func BatchSaveMatchings(bomId int, matchingList []model.BomItemMatching) (err er
tableEnd := string(bomIdStr[len(bomIdStr)-1]) tableEnd := string(bomIdStr[len(bomIdStr)-1])
tableName := "lie_bom_item_matching_" + tableEnd tableName := "lie_bom_item_matching_" + tableEnd
var isBuyBomItemIdList, noBuyBomItemIdList []int var isBuyBomItemIdList, noBuyBomItemIdList []int
for _, matching := range matchingList { for key, matching := range matchingList {
time.Sleep(500 * time.Millisecond) if key == 0 || key == 1 || key == 2 {
time.Sleep(5 * time.Second)
}
//先去数据库查询是否存在该记录,有的话修改,没有就新增 //先去数据库查询是否存在该记录,有的话修改,没有就新增
var match model.BomItemMatching var match model.BomItemMatching
model.Db.Table(tableName).Where("bom_item_id = ?", matching.BomItemID).First(&match) model.Db.Table(tableName).Where("bom_item_id = ?", matching.BomItemID).First(&match)
......
...@@ -13,16 +13,17 @@ import ( ...@@ -13,16 +13,17 @@ import (
type Response struct { type Response struct {
Code int `json:"code"` Code int `json:"code"`
Message int `json:"message"` Message string `json:"message"`
Data interface{} `json:"data"` Data []string `json:"data"`
} }
func UploadToOss(path, fileType, key string) (err error) { func UploadToOss(path, fileType string) (ossPath string, err error) {
if path == "" { if path == "" {
return nil return
} }
url := configs.ApiSetting.Upload url := configs.ApiSetting.Upload
uploadKey := configs.OssSetting.UploadKey uploadKey := configs.OssSetting.UploadKey
fmt.Println(uploadKey)
now := time.Now().Unix() now := time.Now().Unix()
nowStr := strconv.Itoa(int(now)) nowStr := strconv.Itoa(int(now))
params := req.Param{ params := req.Param{
...@@ -41,17 +42,22 @@ func UploadToOss(path, fileType, key string) (err error) { ...@@ -41,17 +42,22 @@ func UploadToOss(path, fileType, key string) (err error) {
resp, err := req.Post(url, req.FileUpload{ resp, err := req.Post(url, req.FileUpload{
File: file, File: file,
FieldName: "upload", FieldName: "upload",
FileName: "Bom_48.xlsx",
}, params) }, params)
if err != nil { if err != nil {
return err return
} }
fmt.Println(resp.String())
var response Response var response Response
if err = resp.ToJSON(&response); err != nil { if err = resp.ToJSON(&response); err != nil {
return errors.New(resp.String()) err = errors.New(resp.String())
return
} }
if response.Code != 200 { if response.Code != 200 {
err = errors.New(response.Message)
return return
} }
return nil if len(response.Data) == 2 {
ossPath = response.Data[0]
}
return
} }
...@@ -17,6 +17,8 @@ func Export(bomId int) (ossPath string, err error) { ...@@ -17,6 +17,8 @@ func Export(bomId int) (ossPath string, err error) {
return return
} }
t := time.Now() t := time.Now()
bomIdStr := strconv.Itoa(bomId)
filePath := "Bom_" + bomIdStr + ".xlsx"
f := excelize.NewFile() f := excelize.NewFile()
index := f.NewSheet("Sheet1") index := f.NewSheet("Sheet1")
f.SetActiveSheet(index) f.SetActiveSheet(index)
...@@ -58,12 +60,12 @@ func Export(bomId int) (ossPath string, err error) { ...@@ -58,12 +60,12 @@ func Export(bomId int) (ossPath string, err error) {
f.SetCellStyle("Sheet1", "E"+keyStr, "E"+keyStr, style) f.SetCellStyle("Sheet1", "E"+keyStr, "E"+keyStr, style)
f.SetCellValue("Sheet1", "E"+keyStr, "参数可能不完整,客服 将与您进一步确认,或调整商品") f.SetCellValue("Sheet1", "E"+keyStr, "参数可能不完整,客服 将与您进一步确认,或调整商品")
} }
bomIdStr := strconv.Itoa(bomId) err = f.SaveAs(filePath)
err = f.SaveAs("Bom_" + bomIdStr + ".xlsx")
if err != nil { if err != nil {
return return
} }
} }
ossPath,err = UploadToOss(filePath, "xlsx")
elapsed := time.Since(t) elapsed := time.Since(t)
fmt.Println(elapsed) fmt.Println(elapsed)
return return
......
package main package main
import (
"bom_server/internal/service"
"fmt"
)
func Export(bomId int)(result interface{}) {
ossPath, err := service.Export(bomId)
if err!=nil {
return err
}
fmt.Println(ossPath)
return "12123"
}
...@@ -3,20 +3,35 @@ package main ...@@ -3,20 +3,35 @@ package main
import ( import (
"bom_server/configs" "bom_server/configs"
"bom_server/internal/model" "bom_server/internal/model"
"bom_server/internal/service"
"flag" "flag"
"github.com/hprose/hprose-golang/rpc" "github.com/hprose/hprose-golang/rpc"
"net/http" "net/http"
) )
func Export(bomId int) (result interface{}) {
ossPath, err := service.Export(bomId)
response := make(map[string]interface{})
if err != nil {
response["err_code"] = -1
response["err_msg"] = err.Error()
} else {
response["err_code"] = 0
response["err_msg"] = "ok"
response["data"] = ossPath
}
return response
}
func main() { func main() {
var path string var path string
flag.StringVar(&path, "config", "conf/config.ini", "../conf/config.ini") flag.StringVar(&path, "config", "conf/config.ini", "../conf/config.ini")
flag.Parse() flag.Parse()
configs.Setup(path) configs.Setup(path)
model.Setup() model.Setup()
service := rpc.NewHTTPService() server := rpc.NewHTTPService()
service.AddFunction("export", Export, rpc.Options{}) server.AddFunction("export", Export)
err := http.ListenAndServe(":8080", service) err := http.ListenAndServe(":8080", server)
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
package main package main
import ( import (
"bom_server/configs"
"bom_server/internal/service"
"flag"
"fmt" "fmt"
"github.com/hprose/hprose-golang/rpc" "github.com/hprose/hprose-golang/rpc"
"time"
) )
type Stub struct { type Stub struct {
...@@ -14,6 +12,7 @@ type Stub struct { ...@@ -14,6 +12,7 @@ type Stub struct {
AsyncHello func(func(string, error), string) `name:"hello"` AsyncHello func(func(string, error), string) `name:"hello"`
} }
func testHprose() { func testHprose() {
client := rpc.NewClient("http://127.0.0.1:8080/") client := rpc.NewClient("http://127.0.0.1:8080/")
var stub *Stub var stub *Stub
...@@ -21,16 +20,20 @@ func testHprose() { ...@@ -21,16 +20,20 @@ func testHprose() {
//stub.AsyncHello(func(result string, err error) { //stub.AsyncHello(func(result string, err error) {
// fmt.Println(result, err) // fmt.Println(result, err)
//}, "async world") //}, "async world")
//fmt.Println(stub.Hello("sd"))
start:=time.Now()
fmt.Println(stub.Export(48)) fmt.Println(stub.Export(48))
fmt.Println(time.Now().Sub(start))
} }
func main() { func main() {
var path string //var path string
flag.StringVar(&path, "config", "conf/config.ini", "../conf/config.ini") //flag.StringVar(&path, "config", "conf/config.ini", "../conf/config.ini")
flag.Parse() //flag.Parse()
configs.Setup(path) //configs.Setup(path)
//model.Setup() //model.Setup()
//service.Export(48) //osspath,err:=service.Export(48)
service.UploadToOss("sadsa", "xlsx", "123213") //fmt.Println(err)
//testHprose() //fmt.Println(osspath)
testHprose()
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment