- SendFilesToStores
This commit is contained in:
83
business/jxstore/financial/financial.go
Normal file
83
business/jxstore/financial/financial.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package financial
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/weixinmsg"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"github.com/qiniu/api.v7/storage"
|
||||
)
|
||||
|
||||
type tUploadFileInfo struct {
|
||||
FileHeader *multipart.FileHeader
|
||||
StoreID int
|
||||
}
|
||||
|
||||
func SendFilesToStores(files []*multipart.FileHeader, isAsync bool, userName string) (msg string, err error) {
|
||||
fileList := make([]*tUploadFileInfo, len(files))
|
||||
for k, fileHeader := range files {
|
||||
fileList[k] = &tUploadFileInfo{
|
||||
FileHeader: fileHeader,
|
||||
}
|
||||
fileNameParts := strings.Split(fileHeader.Filename, "_")
|
||||
if len(fileNameParts) < 3 {
|
||||
return "", fmt.Errorf("文件名:%s不规范,没有包含三个必要的部分", fileHeader.Filename)
|
||||
}
|
||||
fileList[k].StoreID = int(utils.Str2Int64WithDefault(fileNameParts[0], 0))
|
||||
if fileList[k].StoreID < 100000 || fileList[k].StoreID > 1000000 {
|
||||
return "", fmt.Errorf("文件名:%s不规范,不以合法的京西门店ID开始", fileHeader.Filename)
|
||||
}
|
||||
}
|
||||
putPolicy := storage.PutPolicy{
|
||||
Scope: globals.QiniuBucket,
|
||||
Expires: 10 * 60,
|
||||
}
|
||||
upToken := putPolicy.UploadToken(api.QiniuAPI)
|
||||
cfg := &storage.Config{}
|
||||
task := tasksch.RunManagedTask("SendFilesToStores", false, nil, 0, 1, userName, func(batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
fileInfo := batchItemList[0].(*tUploadFileInfo)
|
||||
fileHeader := fileInfo.FileHeader
|
||||
storeID := fileInfo.StoreID
|
||||
file, err := fileHeader.Open()
|
||||
if err == nil {
|
||||
ret := storage.PutRet{}
|
||||
key := "storeBill_" + utils.Int2Str(storeID) + "_" + strings.ToLower(utils.GetUUID()) + path.Ext(fileHeader.Filename)
|
||||
formUploader := storage.NewFormUploader(cfg)
|
||||
err = formUploader.Put(context.Background(), &ret, upToken, key, file, fileHeader.Size, nil)
|
||||
file.Close()
|
||||
// globals.SugarLogger.Debug(utils.Format4Output(ret, false))
|
||||
if err == nil {
|
||||
db := dao.GetDB()
|
||||
billRec := &model.StoreBill{
|
||||
Date: time.Now(),
|
||||
Url: jxutils.ComposeQiniuResURL(ret.Key),
|
||||
StoreId: storeID,
|
||||
BillName: fileHeader.Filename,
|
||||
}
|
||||
if err = dao.CreateEntity(db, billRec); err == nil {
|
||||
err = weixinmsg.NotifySaleBill(storeID, billRec.Url)
|
||||
}
|
||||
}
|
||||
}
|
||||
return retVal, err
|
||||
}, fileList)
|
||||
|
||||
if isAsync {
|
||||
return "Running", nil
|
||||
}
|
||||
if _, err = task.GetResult(0); err == nil {
|
||||
return "Done", nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
Reference in New Issue
Block a user