- ExportMTWaybills
- Obj2Excel
This commit is contained in:
@@ -3,8 +3,11 @@ package orderman
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/scheduler/basesch"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/excel"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"github.com/astaxie/beego/orm"
|
||||
)
|
||||
@@ -15,6 +18,12 @@ const (
|
||||
defPageSize = 50
|
||||
)
|
||||
|
||||
type tMTWaybillExport struct {
|
||||
model.Waybill
|
||||
StoreName string
|
||||
StoreID int `orm:"column(store_id)"`
|
||||
}
|
||||
|
||||
func (c *OrderManager) GetStoreOrderInfo(storeID string, lastHours int, fromStatus, toStatus, offset, pageSize int) (orders []*model.GoodsOrderExt, err error) {
|
||||
globals.SugarLogger.Debugf("GetStoreOrderInfo storeID:%s", storeID)
|
||||
if lastHours > maxLastHours {
|
||||
@@ -167,3 +176,44 @@ func (c *OrderManager) GetOrderWaybillInfo(vendorOrderID string, vendorID int) (
|
||||
globals.SugarLogger.Infof("GetOrderWaybillInfo orderID:%s failed with error:%v", vendorOrderID, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (c *OrderManager) ExportMTWaybills(fromDateStr, toDateStr string) (excelContent []byte, err error) {
|
||||
globals.SugarLogger.Debugf("ExportMTWaybills from:%s to:%s", fromDateStr, toDateStr)
|
||||
|
||||
fromDate := utils.Str2Time(fromDateStr)
|
||||
if toDateStr == "" {
|
||||
toDateStr = fromDateStr
|
||||
}
|
||||
toDate := utils.Str2Time(toDateStr)
|
||||
var waybills []*tMTWaybillExport
|
||||
sql := `
|
||||
SELECT t1.*, t2.store_name, IF(t2.store_id <> 0, t2.store_id, t2.jx_store_id) store_id
|
||||
FROM waybill t1
|
||||
JOIN goods_order t2 ON t1.vendor_order_id = t2.vendor_order_id
|
||||
WHERE t1.waybill_vendor_id = 102 AND t1.status = 105 AND t1.waybill_created_at >= ? AND t1.waybill_created_at <= ?
|
||||
ORDER BY t1.id
|
||||
`
|
||||
db := dao.GetDB()
|
||||
if err = dao.GetRows(db, &waybills, sql, fromDate, toDate); err == nil {
|
||||
config := []*excel.Obj2ExcelSheetConfig{
|
||||
&excel.Obj2ExcelSheetConfig{
|
||||
Title: "Sheet1",
|
||||
Data: waybills,
|
||||
CaptionList: []string{
|
||||
"VendorWaybillID",
|
||||
"WaybillVendorID",
|
||||
"VendorOrderID",
|
||||
"OrderVendorID",
|
||||
"StoreName",
|
||||
"StoreID",
|
||||
"CourierName",
|
||||
"Status",
|
||||
"DesiredFee",
|
||||
"WaybillCreatedAt",
|
||||
},
|
||||
},
|
||||
}
|
||||
return excel.Obj2Excel(config), nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
80
business/jxutils/excel/excel.go
Normal file
80
business/jxutils/excel/excel.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"github.com/360EntSecGroup-Skylar/excelize"
|
||||
"github.com/fatih/structs"
|
||||
)
|
||||
|
||||
type Obj2ExcelSheetConfig struct {
|
||||
Title string
|
||||
Data interface{}
|
||||
CaptionList []string
|
||||
}
|
||||
|
||||
func Obj2Excel(sheetList []*Obj2ExcelSheetConfig) []byte {
|
||||
globals.SugarLogger.Debug("Obj2Excel")
|
||||
excelFile := excelize.NewFile()
|
||||
for sheetIndex, sheetConfig := range sheetList {
|
||||
obj := sheetConfig.Data
|
||||
|
||||
typeInfo := reflect.TypeOf(obj)
|
||||
if typeInfo.Kind() == reflect.Ptr {
|
||||
typeInfo = typeInfo.Elem()
|
||||
}
|
||||
if typeInfo.Kind() != reflect.Slice {
|
||||
panic("obj must be slice type")
|
||||
}
|
||||
typeInfo = typeInfo.Elem()
|
||||
if typeInfo.Kind() == reflect.Ptr {
|
||||
typeInfo = typeInfo.Elem()
|
||||
}
|
||||
if typeInfo.Kind() != reflect.Struct && typeInfo.Kind() != reflect.Map {
|
||||
panic("obj must be slice of map or struct")
|
||||
}
|
||||
|
||||
valueInfo := reflect.Indirect(reflect.ValueOf(obj))
|
||||
if sheetIndex == 0 {
|
||||
sheetName := excelFile.GetSheetName(1)
|
||||
excelFile.SetSheetName(sheetName, sheetConfig.Title)
|
||||
} else {
|
||||
excelFile.NewSheet(sheetConfig.Title)
|
||||
}
|
||||
for index, name := range sheetConfig.CaptionList {
|
||||
excelFile.SetCellStr(sheetConfig.Title, genAxis(0, index), name)
|
||||
}
|
||||
for i := 0; i < valueInfo.Len(); i++ {
|
||||
var mapData map[string]interface{}
|
||||
if typeInfo.Kind() == reflect.Struct {
|
||||
mapData = jxutils.FlatMap(structs.Map(valueInfo.Index(i).Interface()))
|
||||
} else {
|
||||
mapData = valueInfo.Index(i).Interface().(map[string]interface{})
|
||||
}
|
||||
for index, name := range sheetConfig.CaptionList {
|
||||
globals.SugarLogger.Debug(sheetConfig.Title, " ", genAxis(i+1, index), " ", fmt.Sprintf("%v", mapData[name]))
|
||||
excelFile.SetCellStr(sheetConfig.Title, genAxis(i+1, index), fmt.Sprintf("%v", mapData[name]))
|
||||
}
|
||||
}
|
||||
}
|
||||
buf := &bytes.Buffer{}
|
||||
excelFile.Write(buf)
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func getStructMemberNames(typeInfo reflect.Type) (nameList []string) {
|
||||
nameList = make([]string, typeInfo.NumField())
|
||||
for i := 0; i < typeInfo.NumField(); i++ {
|
||||
nameList[i] = typeInfo.Field(i).Name
|
||||
globals.SugarLogger.Debug(typeInfo.Field(i).Tag)
|
||||
}
|
||||
return nameList
|
||||
}
|
||||
|
||||
func genAxis(row, col int) string {
|
||||
return fmt.Sprintf("%c%d", col+65, row+1)
|
||||
}
|
||||
26
business/jxutils/excel/excel_test.go
Normal file
26
business/jxutils/excel/excel_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestObj2Excel(t *testing.T) {
|
||||
// kk := make([]*model.SkuName, 1)
|
||||
// kk[0] = &model.SkuName{
|
||||
// Name: "haha",
|
||||
// }
|
||||
|
||||
kk := []map[string]interface{}{
|
||||
map[string]interface{}{
|
||||
"key1": 1,
|
||||
"key2": 2,
|
||||
"key3": 3,
|
||||
},
|
||||
}
|
||||
cc := &Obj2ExcelSheetConfig{
|
||||
Title: "Title",
|
||||
Data: kk,
|
||||
CaptionList: []string{"key1", "key2", "key3"},
|
||||
}
|
||||
Obj2Excel([]*Obj2ExcelSheetConfig{cc})
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/orderman"
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/scheduler/defsch"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
@@ -168,3 +174,33 @@ func (c *OrderController) GetOrderWaybillInfo() {
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
// @Param token header string true "认证token"
|
||||
|
||||
// @Title 导出美团运单
|
||||
// @Description 导出美团运单
|
||||
// @Param fromDate query string true "开始日期(包含),格式(2006-01-02)"
|
||||
// @Param toDate query string false "结束日期(包含),格式(2006-01-02)"
|
||||
// @Success 200 {object} controllers.CallResult
|
||||
// @Failure 200 {object} controllers.CallResult
|
||||
// @router /ExportMTWaybills [get]
|
||||
func (c *OrderController) ExportMTWaybills() {
|
||||
var content []byte
|
||||
var fromDate, toDate string
|
||||
c.callExportMTWaybills(func(params *tOrderExportMTWaybillsParams) (retVal interface{}, errCode string, err error) {
|
||||
fromDate = params.FromDate
|
||||
toDate = params.ToDate
|
||||
if toDate == "" {
|
||||
toDate = fromDate
|
||||
}
|
||||
content, err = orderman.FixedOrderManager.ExportMTWaybills(params.FromDate, params.ToDate)
|
||||
globals.SugarLogger.Debug(err)
|
||||
return retVal, ignoreCode, err
|
||||
})
|
||||
if content != nil {
|
||||
c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/vnd.ms-excel")
|
||||
fileName := strings.Replace(fmt.Sprintf("attachment;filename=美团运单表%s至%sat%s.xlsx", fromDate, toDate, utils.Time2Str(time.Now())), " ", "-", 1)
|
||||
c.Ctx.ResponseWriter.Header().Set("content-disposition", fileName)
|
||||
c.Ctx.ResponseWriter.Write(content)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,6 +103,14 @@ func init() {
|
||||
MethodParams: param.Make(),
|
||||
Params: nil})
|
||||
|
||||
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"],
|
||||
beego.ControllerComments{
|
||||
Method: "ExportMTWaybills",
|
||||
Router: `/ExportMTWaybills`,
|
||||
AllowHTTPMethods: []string{"get"},
|
||||
MethodParams: param.Make(),
|
||||
Params: nil})
|
||||
|
||||
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"],
|
||||
beego.ControllerComments{
|
||||
Method: "FinishedPickup",
|
||||
|
||||
Reference in New Issue
Block a user