瀏覽代碼

新增用户行为统计

xyxie 2 月之前
父節點
當前提交
40fafca3ea

+ 162 - 0
controllers/chart_collect_stat.go

@@ -0,0 +1,162 @@
+package controllers
+
+import (
+	"eta/eta_forum_admin/models"
+	"eta/eta_forum_admin/models/chart_collect"
+	"eta/eta_forum_admin/models/eta_business"
+	"eta/eta_forum_admin/utils"
+	"github.com/rdlucklib/rdluck_tools/paging"
+	"strconv"
+	"strings"
+	"time"
+)
+
+type ChartCollectStatController struct {
+	BaseAuthController
+}
+
+// SimpleList
+// @Title 单层分类列表
+// @Description 单层分类列表
+// @Success 200 {object} data_manage.EdbClassifyListResp
+// @router /stat/user [get]
+func (this *ChartCollectStatController) UserStat() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	// 默认查一级分类和一级分类下的指标信息,
+	// 如果是 子级分类,查询该子级分类的下一级分类和指标信息
+	// 增加标识判断是文件夹还是指标列表
+	startDate := this.GetString("StartDate")
+	endDate := this.GetString("EndDate")
+	etaBusinessIds := this.GetString("EtaBusinessIds")
+	pageSize, _ := this.GetInt("PageSize")
+	currentIndex, _ := this.GetInt("CurrentIndex")
+
+	sortType := this.GetString("SortType")
+	sortParam := this.GetString("SortParam")
+
+	var startSize int
+	if pageSize <= 0 {
+		pageSize = utils.PageSize20
+	}
+	if currentIndex <= 0 {
+		currentIndex = 1
+	}
+	startSize = paging.StartIndex(currentIndex, pageSize)
+
+	var condition string
+	var pars []interface{}
+
+	if etaBusinessIds != "" {
+		businessIdsSlice := make([]int, 0)
+		businessIds := strings.Split(etaBusinessIds, ",")
+		if len(businessIds) == 0 {
+			br.Msg = "请选择正确的商家"
+			return
+		}
+		for _, id := range businessIds {
+			if id != "" {
+				adminIdInt, e := strconv.Atoi(id)
+				if e != nil {
+					br.Msg = "请选择正确的商家"
+					return
+				}
+				businessIdsSlice = append(businessIdsSlice, adminIdInt)
+			}
+		}
+		if len(businessIdsSlice) > 0 {
+			condition += ` AND u.eta_business_id IN (` + utils.GetOrmInReplace(len(businessIdsSlice)) + `) `
+			pars = append(pars, businessIdsSlice)
+		}
+	}
+	if startDate != "" {
+		// 校验日期格式
+		_, err := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+		if err != nil {
+			br.Msg = "请选择正确的开始日期"
+			return
+		}
+		condition += ` AND s.collect_date >= ? `
+		pars = append(pars, startDate)
+	}
+	if endDate != "" {
+		// 校验日期格式
+		endTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
+		if err != nil {
+			br.Msg = "请选择正确的结束日期"
+			return
+		}
+		condition += ` AND s.collect_date < ? `
+		nextDate := endTime.AddDate(0, 0, 1).Format(utils.FormatDate)
+		pars = append(pars, nextDate)
+	}
+	order := ""
+	if sortParam == "CollectDate" {
+		if sortType == "desc" || sortType == "asc" {
+			order = sortParam + " " + sortType
+		}
+	}
+	statObj := new(chart_collect.UserCollectChartStat)
+
+	total, err := statObj.GetCountByCondition(condition, pars)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
+		return
+	}
+	page := paging.GetPaging(currentIndex, pageSize, total)
+	resp := new(models.UserListResp)
+	userList := make([]*models.BusinessUser, 0)
+	if total == 0 {
+		resp.List = userList
+		resp.Paging = page
+		br.Ret = 200
+		br.Success = true
+		br.Msg = "获取成功"
+		br.Data = resp
+		return
+	}
+	list, err := statObj.GetPageListByCondition(condition, pars, order, startSize, pageSize)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取数据失败,Err:" + err.Error()
+		return
+	}
+	// 查找商户信息
+	businessCodeStr := make([]string, 0)
+	businessCodeMap := make(map[string]string)
+	for _, v := range list {
+		if _, ok := businessCodeMap[v.BusinessCode]; ok {
+			continue
+		}
+		businessCodeStr = append(businessCodeStr, v.BusinessCode)
+	}
+	obj := new(eta_business.EtaBusiness)
+	condition1 := " AND business_code IN (" + utils.GetOrmInReplace(len(businessCodeStr)) + ") "
+	var pars1 []interface{}
+	pars1 = append(pars1, businessCodeStr)
+	businessList, err := obj.GetItemsByCondition(condition1, pars1, []string{"business_code", "business_name"}, "")
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取商户信息失败,Err:" + err.Error()
+		return
+	}
+	for _, v := range businessList {
+		businessCodeMap[v.BusinessCode] = v.BusinessName
+	}
+
+	for _, v := range list {
+		if businessName, ok := businessCodeMap[v.BusinessCode]; ok {
+			v.BusinessName = businessName
+		}
+	}
+	resp.List = userList
+	resp.Paging = page
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+	br.Data = resp
+}

+ 4 - 0
controllers/eta_business/user.go

@@ -284,6 +284,7 @@ func (this *EtaBusinessUserController) AddUser() {
 	user.PositionStatus = req.PositionStatus
 	user.Enabled = 1
 	user.BusinessCode = req.BusinessCode
+	user.EtaBusinessId = businessInfo.EtaBusinessId
 	user.DepartmentName = req.DepartmentName
 	user.CreatedTime = time.Now()
 	user.RegisterTime = time.Now()
@@ -991,6 +992,7 @@ func (this *EtaBusinessUserController) Import() {
 						continue
 					}
 					item.BusinessCode = businessInfo.BusinessCode
+					item.EtaBusinessId = businessInfo.EtaBusinessId
 				}
 				if item.BusinessCode == "" && businessName != "" {
 					businessObj := new(eta_business.EtaBusiness)
@@ -1005,6 +1007,7 @@ func (this *EtaBusinessUserController) Import() {
 						continue
 					}
 					item.BusinessCode = businessInfo.BusinessCode
+					item.EtaBusinessId = businessInfo.EtaBusinessId
 				}
 				if _, ok := excelData[item.CountryCode+"_"+item.Mobile]; ok {
 					//将用户插入其中,然后退出当前循环,进入下一循环
@@ -1056,6 +1059,7 @@ func (this *EtaBusinessUserController) Import() {
 			userData.Position = user.Position
 			userData.PositionStatus = user.PositionStatus
 			userData.BusinessCode = user.BusinessCode
+			userData.EtaBusinessId = user.EtaBusinessId
 			userData.DepartmentName = user.DepartmentName
 			userData.CountryCode = user.CountryCode
 

+ 18 - 0
models/chart_collect/log.go

@@ -0,0 +1,18 @@
+package chart_collect
+
+import (
+	"time"
+)
+
+type ChartCollectLog struct {
+	Id                int       `orm:"column(id);pk"`
+	UserId            int       `description:"用户ID"`
+	BusinessCode      string    `description:"客户编码"`
+	RealName          string    `description:"用户姓名"`
+	ChartInfoId       int       `description:"图表ID"`
+	CollectState      int       `description:"-1 取消收藏,1收藏"`
+	CollectTime       time.Time `description:"收藏时间"`
+	CollectClassifyId int       `description:"图表分类ID(注意:此字段的注释可能与实际意图不符,原文为'图表ID')"`
+	ChartSource       int       `description:"1:ETA图库;2:商品价格曲线;3:相关性图"`
+	CreateTime        time.Time `description:"创建时间"`
+}

+ 91 - 0
models/chart_collect/stat.go

@@ -0,0 +1,91 @@
+package chart_collect
+
+import (
+	"github.com/beego/beego/v2/client/orm"
+	"github.com/rdlucklib/rdluck_tools/paging"
+	"time"
+)
+
+// 图表收藏每日汇总统计表
+type ChartCollectStat struct {
+	ChartCollectStatId int       `orm:"column(chart_collect_stat_id);pk"`
+	ChartInfoId        int       `description:"图表ID"`
+	CollectDate        string    `description:"收藏日期"`
+	CollectNum         int       `description:"收藏的图表数量"`
+	CreateTime         time.Time `description:"创建时间"`
+	ModifyTime         time.Time `description:"修改时间"`
+}
+
+// 根据图表ID和日期获取图表收藏统计信息
+func GetChartCollectStatByChartInfoIdAndCollectDate(chartInfoId int, collectDate string) (stat *ChartCollectStat, err error) {
+	o := orm.NewOrm()
+	sql := "SELECT * FROM chart_collect_stat WHERE chart_info_id = ? AND collect_date = ?"
+	err = o.Raw(sql, chartInfoId, collectDate).QueryRow(&stat)
+	return
+}
+
+// 用户收藏图表每日汇总表
+type UserCollectChartStat struct {
+	UserChartStatId int       `orm:"column(user_chart_stat_id);pk"`
+	UserId          int       `description:"用户ID"`
+	BusinessCode    string    `description:"客户编码"`
+	RealName        string    `description:"用户姓名"`
+	CollectDate     string    `description:"收藏日期"`
+	CollectNum      int       `description:"收藏的图表数量"`
+	ChartSource     int       `description:"1:ETA图库;2:商品价格曲线;3:相关性图"`
+	CreateTime      time.Time `description:"创建时间"`
+	ModifyTime      time.Time `description:"修改时间"`
+}
+
+// 根据用户ID和日期获取图表收藏统计信息
+func GetUserCollectChartStatByUserIdAndCollectDate(userId int, collectDate string) (stat *UserCollectChartStat, err error) {
+	o := orm.NewOrm()
+	sql := "SELECT * FROM chart_collect_stat WHERE user_id = ? AND collect_date = ?"
+	err = o.Raw(sql, userId, collectDate).QueryRow(&stat)
+	return
+}
+
+// 获取该用户数量
+func (u *UserCollectChartStat) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
+	o := orm.NewOrm()
+	tmpSql := `SELECT s.* FROM user_collect_chart_stat AS s LEFT JOIN user AS u on s.user_id=u.user_id WHERE 1=1  `
+	if condition != "" {
+		tmpSql += condition
+	}
+	sql := `SELECT COUNT(1) AS count FROM (` + tmpSql + `) AS c `
+	err = o.Raw(sql, pars).QueryRow(&count)
+	return
+}
+
+// 获取该用户列表
+func (u *UserCollectChartStat) GetPageListByCondition(condition string, pars []interface{}, order string, startSize, pageSize int) (items []*UserCollectChartStatItem, err error) {
+	o := orm.NewOrm()
+	tmpSql := `SELECT s.*, u.last_collect_chart_time, u.business_code, u.real_name, u.eta_business_id FROM user_collect_chart_stat AS s LEFT JOIN user AS u on s.user_id=u.user_id WHERE 1=1 `
+	if condition != "" {
+		tmpSql += condition
+	}
+	if order != "" {
+		tmpSql += ` ORDER BY ` + order + " s.chart_collect_stat_id DESC"
+	} else {
+		tmpSql += ` ORDER BY s.chart_collect_stat_id DESC`
+	}
+	tmpSql += ` Limit ?,?`
+	_, err = o.Raw(tmpSql, pars, startSize, pageSize).QueryRows(&items)
+	return
+}
+
+type UserStatListResp struct {
+	Paging *paging.PagingItem
+	List   []*UserCollectChartStatItem
+}
+
+type UserCollectChartStatItem struct {
+	UserId               int    `description:"用户ID"`
+	BusinessCode         string `description:"客户编码"`
+	RealName             string `description:"用户姓名"`
+	EtaBusinessId        int
+	BusinessName         string
+	CollectNum           int `description:"收藏的图表数量"`
+	CollectDate          string
+	LastCollectChartTime string
+}

+ 2 - 0
models/user.go

@@ -10,6 +10,7 @@ import (
 type User struct {
 	UserId               int `orm:"column(user_id);pk"`
 	BusinessCode         string
+	EtaBusinessId        int
 	UserName             string `description:"用户名"`
 	RealName             string `description:"姓名"`
 	Mobile               string
@@ -112,6 +113,7 @@ func GetUserPageListByCondition(condition string, pars []interface{}, startSize,
 type BusinessUser struct {
 	UserId          int
 	BusinessCode    string
+	EtaBusinessId   int
 	BusinessName    string
 	RealName        string `description:"姓名"`
 	Mobile          string

+ 9 - 0
routers/commentsRouter.go

@@ -502,6 +502,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_forum_admin/controllers:ChartCollectStatController"] = append(beego.GlobalControllerRouter["eta/eta_forum_admin/controllers:ChartCollectStatController"],
+        beego.ControllerComments{
+            Method: "UserStat",
+            Router: `/stat/user`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_forum_admin/controllers:ChartInfoController"] = append(beego.GlobalControllerRouter["eta/eta_forum_admin/controllers:ChartInfoController"],
         beego.ControllerComments{
             Method: "ChartInfoDetail",

+ 5 - 0
routers/router.go

@@ -61,6 +61,11 @@ func init() {
 				&help_doc.HelpDocController{},
 			),
 		),
+		web.NSNamespace("/chart_collect",
+			web.NSInclude(
+				&controllers.ChartCollectStatController{},
+			),
+		),
 	)
 	web.AddNamespace(ns)
 }