Browse Source

get user perfomance total

abiao 5 years ago
parent
commit
014d8c51f6

+ 62 - 0
go/gopath/src/fohow.com/apps/controllers/railsadmin_controller/user_perfomance_controller.go

@@ -0,0 +1,62 @@
+package railsadmin_controller
+
+import (
+	"fohow.com/apps"
+	"fohow.com/apps/models/summary_model"
+	"fohow.com/apps/models/user_model"
+	"github.com/astaxie/beego"
+	"strconv"
+)
+
+//订单商品汇总
+func (self *RailsadminController) UserPerfomance() {
+
+	_id := self.Ctx.Input.Param(":id")
+	id, _ := strconv.ParseInt(_id, 10, 64)
+
+	beego.BeeLogger.Warn("UserPerfomance id:(%d)", id)
+
+	userSummary := summary_model.GetPerfomanceById(id, false)
+	if userSummary == nil {
+		self.ReturnError(404, apps.NoExist, "", nil)
+	}
+	beginTime := userSummary.BeginDate.Unix()
+	endTime := userSummary.EndDate.Unix()
+	wxUserId := userSummary.WxUserId
+	//统计个人业绩
+	//普通订单业绩
+	orderPerfomance := summary_model.GetSumOrderPerfomance(wxUserId, int64(0), beginTime, endTime)
+	//店长专区订单业绩
+	shopOrderPerfomance := summary_model.GetSumOrderPerfomance(wxUserId, int64(2), beginTime, endTime)
+	//开店业绩
+	shopPerfomance := summary_model.GetSumShopPerfomance(wxUserId, beginTime, endTime)
+	//充值业绩
+	balanceOrderPerfomance := summary_model.GetSumBalancePerfomance(wxUserId, beginTime, endTime)
+
+	//统计团队业绩
+	inviteList := user_model.GetAllWxUsersByInviteId(wxUserId, true)
+	orderPerfomance, shopOrderPerfomance, shopPerfomance, balanceOrderPerfomance = GetTeamPerfomance(inviteList, orderPerfomance, shopOrderPerfomance, shopPerfomance, balanceOrderPerfomance, beginTime, endTime)
+	userSummary.OrderPerfomance = orderPerfomance
+	userSummary.ShopOrderPerfomance = shopOrderPerfomance
+	userSummary.ShopPerfomance = shopPerfomance
+	userSummary.BalancePerfomance = balanceOrderPerfomance
+
+	userSummary.Save()
+
+	self.ServeJSON()
+}
+
+func GetTeamPerfomance(list []*user_model.WxUser, orderPerfomance, shopOrderPerfomance, shopPerfomance, balanceOrderPerfomance, beginTime, endTime int64) (OrderPerfomance, ShopOrderPerfomance, ShopPerfomance, BalanceOrderPerfomance int64) {
+
+	//递归获取
+	for _, item := range list {
+		orderPerfomance += summary_model.GetSumOrderPerfomance(item.Id, int64(0), beginTime, endTime)
+		shopOrderPerfomance += summary_model.GetSumOrderPerfomance(item.Id, int64(2), beginTime, endTime)
+		shopPerfomance += summary_model.GetSumShopPerfomance(item.Id, beginTime, endTime)
+		balanceOrderPerfomance += summary_model.GetSumOrderPerfomance(item.Id, int64(0), beginTime, endTime)
+
+		secondList := user_model.GetWxUsersByInviteIdAll(item.Id, false)
+		GetTeamPerfomance(secondList, orderPerfomance, shopOrderPerfomance, shopPerfomance, balanceOrderPerfomance, beginTime, endTime)
+	}
+	return orderPerfomance, shopOrderPerfomance, shopPerfomance, balanceOrderPerfomance
+}

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

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

+ 119 - 0
go/gopath/src/fohow.com/apps/models/summary_model/user_perfomance.go

@@ -0,0 +1,119 @@
+package summary_model
+
+import (
+	"fmt"
+	"fohow.com/apps/models/order_model"
+
+	// "strings"
+	"time"
+
+	"fohow.com/cache"
+	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/orm"
+)
+
+const (
+	user_perfomances_tablename = "user_perfomances"
+)
+
+type UserPerfomance struct {
+	Id       int64 `orm:"column(id);pk"                                  json:"id"` // int(11)
+	WxUserId int64 `orm:"column(wx_user_id);null"                         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
+	OrderPerfomance     int64     `orm:"column(order_perfomance);null"                         json:"-"`      // int(11)
+	ShopOrderPerfomance int64     `orm:"column(shop_order_perfomance);null"                         json:"-"` // int(11)
+	ShopPerfomance      int64     `orm:"column(shop_perfomance);null"                         json:"-"`       // int(11)
+	BalancePerfomance   int64     `orm:"column(balance_perfomance);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 *UserPerfomance) TableName() string {
+	return user_perfomances_tablename
+}
+
+//获取检索条件
+func GetPerfomanceById(id int64, useCache bool) *UserPerfomance {
+	k := fmt.Sprintf("summary_model.GetStaticById.(%d)", id)
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).(*UserPerfomance); ok {
+			return ret
+		}
+	}
+	item := new(UserPerfomance)
+	if err := orm.NewOrm().QueryTable(item).Filter("id", id).One(item); err != nil {
+		beego.BeeLogger.Error("get perfomance by id=[%d] err=[%s]", id, err)
+		return nil
+	}
+
+	cache.Cache.Put(k, item, 5*time.Minute)
+	return item
+}
+
+func (self *UserPerfomance) Save() error {
+	if _, err := orm.NewOrm().Update(self); err != nil {
+		beego.BeeLogger.Error("Save perfomance id=[%d] .err=[%s]", self.Id, err)
+		return err
+	}
+	return nil
+}
+
+//获取某会员订单业绩
+func GetSumOrderPerfomance(wxUserId, orderType, beginTime, endTime int64) int64 {
+
+	type Ret struct {
+		Count int64
+	}
+	ret := &Ret{}
+	o := orm.NewOrm()
+	tbn := "orders"
+	sql := fmt.Sprintf("SELECT sum(`paied_price`-`freight`) as count FROM `%s` WHERE  paied_at >? and paied_at<? and  status in (?, ?, ?) ) and wx_user_id=? and order_type=?;", tbn)
+	err := o.Raw(sql, beginTime, endTime, order_model.STATUS_PROCESSING, order_model.STATUS_DISPATCH, order_model.STATUS_COMPLETE, wxUserId, orderType).QueryRow(ret)
+	if err != nil {
+		beego.BeeLogger.Error("GetSumOrderPerfomance err=[%s]", err)
+		return 0
+	}
+
+	return ret.Count
+}
+
+//获取某会员开店业绩
+func GetSumShopPerfomance(wxUserId, beginTime, endTime int64) int64 {
+
+	type Ret struct {
+		Count int64
+	}
+	ret := &Ret{}
+	o := orm.NewOrm()
+	tbn := "shop_applications"
+	sql := fmt.Sprintf("SELECT sum(`total`) as count FROM `%s` WHERE  UNIX_TIMESTAMP(pass_time)>? and UNIX_TIMESTAMP(pass_time)<? and  status=? and wx_user_id=? ;", tbn)
+	err := o.Raw(sql, beginTime, endTime, 1, wxUserId).QueryRow(ret)
+	if err != nil {
+		beego.BeeLogger.Error("GetSumShopPerfomance err=[%s]", err)
+		return 0
+	}
+
+	return ret.Count
+}
+
+//获取某会员充值订单业绩
+func GetSumBalancePerfomance(wxUserId, beginTime, endTime int64) int64 {
+
+	type Ret struct {
+		Count int64
+	}
+	ret := &Ret{}
+	o := orm.NewOrm()
+	tbn := "balance_orders"
+	sql := fmt.Sprintf("SELECT sum(`paied_price`) as count FROM `%s` WHERE  paied_at>? and paied_at<? and  status=? and wx_user_id=? ;", tbn)
+	err := o.Raw(sql, beginTime, endTime, 1, wxUserId).QueryRow(ret)
+	if err != nil {
+		beego.BeeLogger.Error("GetSumBalancePerfomance err=[%s]", err)
+		return 0
+	}
+
+	return ret.Count
+}