- GetOrders.

This commit is contained in:
gazebo
2018-10-09 17:09:34 +08:00
parent 4203dd5b29
commit c8a0f9b59f
4 changed files with 130 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
package orderman
import (
"strconv"
"time"
"git.rosy.net.cn/baseapi/utils"
@@ -184,7 +185,7 @@ func (c *OrderManager) ExportMTWaybills(fromDateStr, toDateStr string) (excelCon
if toDateStr == "" {
toDateStr = fromDateStr
}
toDate := utils.Str2Time(toDateStr)
toDate := utils.Str2Time(toDateStr).Add(24 * time.Hour)
var waybills []*tMTWaybillExport
sql := `
SELECT t1.*, t2.store_name, IF(t2.store_id <> 0, t2.store_id, t2.jx_store_id) store_id
@@ -217,3 +218,97 @@ func (c *OrderManager) ExportMTWaybills(fromDateStr, toDateStr string) (excelCon
}
return nil, err
}
func (c *OrderManager) GetOrders(fromDateStr, toDateStr string, params map[string]interface{}) (pagedInfo *model.PagedInfo, err error) {
globals.SugarLogger.Debugf("GetOrders from:%s to:%s", fromDateStr, toDateStr)
fromDate := utils.Str2Time(fromDateStr)
if toDateStr == "" {
toDateStr = fromDateStr
}
toDate := utils.Str2Time(toDateStr).Add(24 * time.Hour)
pageSize := defPageSize
if params["pageSize"] != nil {
pageSize = params["pageSize"].(int)
if pageSize == 0 {
pageSize = 999999999
}
}
offset := 0
if params["offset"] != nil {
offset = params["offset"].(int)
}
sql := `
SELECT SQL_CALC_FOUND_ROWS t1.*, t2.status waybill_status, t2.courier_name, t2.courier_mobile
FROM goods_order t1
LEFT JOIN waybill t2 ON t1.vendor_waybill_id = t2.vendor_waybill_id AND t1.waybill_vendor_id = t2.waybill_vendor_id
WHERE t1.order_created_at >= ? AND t1.order_created_at < ?
`
sqlParams := []interface{}{
fromDate,
toDate,
}
if params["keyword"] != nil {
keyword := params["keyword"].(string)
keywordLike := "%" + keyword + "%"
sql += `
AND (t1.store_name LIKE ? OR t1.vendor_order_id LIKE ? OR t1.vendor_store_id LIKE ?
OR t1.consignee_name LIKE ? OR t1.consignee_mobile LIKE ? OR t1.consignee_address LIKE ?
OR t2.vendor_waybill_id LIKE ? OR t2.courier_name LIKE ? OR t2.courier_mobile LIKE ?
`
sqlParams = append(sqlParams, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike)
if keywordInt64, err2 := strconv.ParseInt(keyword, 10, 64); err2 == nil {
sql += " OR t1.store_id = ? OR t1.jx_store_id = ?"
sqlParams = append(sqlParams, keywordInt64, keywordInt64)
}
sql += ")"
}
if params["vendorID"] != nil {
sql += " AND t1.vendor_id = ?"
sqlParams = append(sqlParams, params["vendorID"])
}
if params["storeIDs"] != nil {
var storeIDs []int
if err = utils.UnmarshalUseNumber([]byte(params["storeIDs"].(string)), &storeIDs); err != nil {
return nil, err
}
sql += " AND IF(t1.jx_store_id != 0, t1.jx_store_id, t1.store_id) IN (" + dao.GenQuestionMarks(len(storeIDs)) + ")"
sqlParams = append(sqlParams, storeIDs)
}
if params["statuss"] != nil {
var statuss []int
if err = utils.UnmarshalUseNumber([]byte(params["statuss"].(string)), &statuss); err != nil {
return nil, err
}
sql += " AND t1.status IN (" + dao.GenQuestionMarks(len(statuss)) + ")"
sqlParams = append(sqlParams, statuss)
}
sql += `
ORDER BY t1.vendor_order_id
LIMIT ? OFFSET ?
`
sqlParams = append(sqlParams, pageSize, offset)
var orders []*model.GoodsOrderExt
pagedInfo = &model.PagedInfo{}
db := dao.GetDB()
dao.Begin(db)
defer func() {
if r := recover(); r != nil {
dao.Rollback(db)
panic(r)
}
}()
if err = dao.GetRows(db, &orders, sql, sqlParams...); err == nil {
countInfo := &struct{ Ct int }{}
if err = dao.GetRow(db, countInfo, "SELECT FOUND_ROWS() ct"); err == nil {
pagedInfo.TotalCount = countInfo.Ct
pagedInfo.Data = orders
}
}
dao.Commit(db)
return pagedInfo, err
}