Selaa lähdekoodia

订单统计功能

abiao 5 vuotta sitten
vanhempi
commit
b9b3cff20d

+ 35 - 0
go/gopath/src/fohow.com/apps/controllers/railsadmin_controller/order_static_controller.go

@@ -0,0 +1,35 @@
+package railsadmin_controller
+
+import (
+	"fohow.com/apps"
+	"fohow.com/apps/models/order_model"
+	"fohow.com/apps/models/order_static_model"
+	"github.com/astaxie/beego"
+	"strconv"
+)
+
+//订单商品汇总
+func (self *RailsadminController) OrderStatic() {
+
+	_id := self.Ctx.Input.Param(":id")
+	id, _ := strconv.ParseInt(_id, 10, 64)
+	beego.BeeLogger.Warn("OrderStatic id:(%d)", id)
+	orderStatic := order_static_model.GetStaticById(id, false)
+	if orderStatic == nil {
+		self.ReturnError(404, apps.NoExist, "", nil)
+	}
+	bDateUninx := orderStatic.BeginDate.Unix()
+	eDateUninx := orderStatic.EndDate.Unix()
+	state := orderStatic.State
+	IsSend := orderStatic.IsSend
+	orderDetails := order_model.GetStaticOrderDetails(bDateUninx, eDateUninx, state, IsSend)
+
+	for _, item := range orderDetails {
+		total := item.Count * item.Price
+		detail := new(order_static_model.OrderStaticDetail).Create(orderStatic.Id, item.ProductId, total, item.Count, item.ProductName, state, IsSend, orderStatic.BeginDate, orderStatic.EndDate)
+		if detail == nil {
+			self.ReturnError(403, apps.CreateOrderFail, "", nil)
+		}
+	}
+	self.ServeJSON()
+}

+ 28 - 0
go/gopath/src/fohow.com/apps/models/order_model/order_detail.go

@@ -1,6 +1,9 @@
 package order_model
 
 import (
+	"fmt"
+	"strings"
+
 	// "fmt"
 	"github.com/astaxie/beego"
 	// "strconv"
@@ -103,3 +106,28 @@ func GetAllDetailsOrderId(oId string) (items []*OrderDetail) {
 	}
 	return items
 }
+
+//获取商品销售统计
+func GetStaticOrderDetails(bDateUnix, eDateUnix int64, state string, isSend bool) (details []*OrderDetail) {
+	sql := "select sum(ot.nums) as nums ,price,ot.product_id,ot.product_name  from order_details ot left join orders o on ot.order_id=o.id where "
+	if bDateUnix > 0 {
+		s := fmt.Sprintf(" UNIX_TIMESTAMP(o.created_at)>=%d", bDateUnix)
+		sql = strings.Join([]string{sql, s}, " ")
+	}
+	if eDateUnix > 0 {
+		s := fmt.Sprintf(" UNIX_TIMESTAMP(o.created_at)<=%d", eDateUnix)
+		sql = strings.Join([]string{sql, s}, " ")
+	}
+	if len(state) > 0 {
+		s := fmt.Sprintf("and o.status=%s", state)
+		sql = strings.Join([]string{sql, s}, " ")
+	}
+	s := "and ot.is_send=? group by ot.product_id order by product_id desc"
+	sql = strings.Join([]string{sql, s}, " ")
+
+	beego.BeeLogger.Warn("sql=[%s]", sql)
+	if _, err := orm.NewOrm().Raw(sql, isSend).QueryRows(&details); err != nil {
+		return nil
+	}
+	return details
+}

+ 9 - 0
go/gopath/src/fohow.com/apps/models/order_static_model/init.go

@@ -0,0 +1,9 @@
+package order_static_model
+
+import (
+	"github.com/astaxie/beego/orm"
+)
+
+func init() {
+	orm.RegisterModel(new(OrderStatic))
+}

+ 97 - 0
go/gopath/src/fohow.com/apps/models/order_static_model/order_static.go

@@ -0,0 +1,97 @@
+package order_static_model
+
+import (
+	"fmt"
+	// "strings"
+	"time"
+
+	"fohow.com/cache"
+	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/orm"
+)
+
+const (
+	order_statics_tablename        = "order_statics"
+	order_static_details_tablename = "order_static_details"
+)
+
+type OrderStatic struct {
+	Id        int64     `orm:"column(id);pk"                                  json:"id"`  // int(11)
+	BeginDate time.Time `orm:"column(begin_date);null;type(datetime)"         json:"-"`   // datetime
+	EndDate   time.Time `orm:"column(end_date);null;type(datetime)"            json:"-"`  // datetime
+	State     string    `orm:"column(state);null"                             json:"-"`   // tinyint(1)
+	IsSend    bool      `orm:"column(is_send);null"                             json:"-"` // tinyint(1)
+	CreatedAt time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"`   // datetime
+	UpdatedAt time.Time `orm:"column(updated_at);auto_now;type(datetime)"     json:"-"`   // datetime
+}
+
+type OrderStaticDetail struct {
+	Id          int64     `orm:"column(id);pk"                                  json:"id"`   // int(11)
+	StaticId    int64     `orm:"column(static_id)"                                 json:"-"` // int(11)
+	BeginDate   time.Time `orm:"column(begin_date);null;type(datetime)"         json:"-"`    // datetime
+	EndDate     time.Time `orm:"column(end_date);null;type(datetime)"            json:"-"`   // datetime
+	State       string    `orm:"column(state);null"                             json:"-"`    // tinyint(1)
+	IsSend      bool      `orm:"column(is_send);null"                         json:"-"`      // tinyint(1)
+	ProductId   int64     `orm:"column(product_id);null"                        json:"-"`    // int(11)
+	ProductName string    `orm:"column(product_name);null"                     json:"-"`     // tinyint(1)
+	Nums        int64     `orm:"column(nums);null"                              json:"-"`    // int(11)
+	Total       int64     `orm:"column(total);null"                              json:"-"`   // int(11)
+	CreatedAt   time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"`    // datetime
+	UpdatedAt   time.Time `orm:"column(updated_at);auto_now;type(datetime)"     json:"-"`    // datetime
+}
+
+func (self *OrderStaticDetail) TableName() string {
+	return order_static_details_tablename
+}
+
+func (self *OrderStatic) TableName() string {
+	return order_statics_tablename
+}
+
+//获取检索条件
+func GetStaticById(id int64, useCache bool) *OrderStatic {
+	k := fmt.Sprintf("order_static_model.GetStaticById.(%d)", id)
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).(*OrderStatic); ok {
+			return ret
+		}
+	}
+	item := new(OrderStatic)
+	if err := orm.NewOrm().QueryTable(item).Filter("id", id).One(item); err != nil {
+		beego.BeeLogger.Error("get static by id=[%d] err=[%s]", id, err)
+		return nil
+	}
+
+	cache.Cache.Put(k, item, 5*time.Minute)
+	return item
+}
+
+//创建订单
+func (self *OrderStaticDetail) Create(staticId, pid, totalPrice, nums int64, pdName, state string, isSend bool, bDate, eDate time.Time) *OrderStaticDetail {
+	staticDetail := &OrderStaticDetail{
+		StaticId:    staticId,
+		BeginDate:   bDate,
+		EndDate:     eDate,
+		State:       state,
+		IsSend:      isSend,
+		ProductId:   pid,
+		ProductName: pdName,
+		Nums:        nums,
+		Total:       totalPrice,
+	}
+	id, err := orm.NewOrm().Insert(staticDetail)
+	if err != nil {
+		beego.BeeLogger.Error("insert order static detail err=[%s]", err)
+		return nil
+	}
+	staticDetail.Id = id
+	return staticDetail
+}
+
+func (self *OrderStaticDetail) Save() error {
+	if _, err := orm.NewOrm().Update(self); err != nil {
+		beego.BeeLogger.Error("SaveOrderStaticDetail id=[%d] .err=[%s]", self.Id, err)
+		return err
+	}
+	return nil
+}

+ 2 - 0
go/gopath/src/fohow.com/routers/routes.go

@@ -209,6 +209,8 @@ func init() {
 	beego.Router("/railsadmin/create_qrcode/:id([0-9]+)", &railsadmin_controller.RailsadminController{}, "get:CreateWxQrcode")
 	beego.Router("/railsadmin/create_qrcode/scene_str/:id([0-9]+)", &railsadmin_controller.RailsadminController{}, "get:CreateWxQrcodeWithSceneString")
 	beego.Router("/railsadmin/order/dispatch/:id([0-9]+)", &railsadmin_controller.RailsadminController{}, "get:OrderDispatch")
+	beego.Router("/railsadmin/order/static/:id([0-9]+)", &railsadmin_controller.RailsadminController{}, "get:OrderStatic")
+
 	//设置群主
 	beego.Router("/railsadmin/update/intro/:id([0-9]+)", &railsadmin_controller.RailsadminController{}, "get:UpdateIntroUser")
 	//生成小程序二维码