Browse Source

运营统计-设备统计

sixian 2 years ago
parent
commit
22af78aabf

+ 10 - 4
app/operation/controller/deviceCountController.go

@@ -16,7 +16,7 @@ type deviceCtl struct{}
 func (c deviceCtl) CountDevice(ctx *gin.Context) {
 	value, _ := ctx.Get(middleware.Authorization)
 	claims := value.(*middleware.Claims)
-	var req model.ResuestDeviceCountFilter
+	var req model.RequestDeviceCountFilter
 	err := ctx.ShouldBindQuery(&req)
 	if err != nil {
 		ctx.JSON(http.StatusOK, err)
@@ -33,13 +33,19 @@ func (c deviceCtl) CountDevice(ctx *gin.Context) {
 func (c deviceCtl) MonthList(ctx *gin.Context) {
 	value, _ := ctx.Get(middleware.Authorization)
 	claims := value.(*middleware.Claims)
-	var req model.ResuestDeviceCountFilter
+	var req model.RequestDeviceCountFilter
 	err := ctx.ShouldBindQuery(&req)
 	if err != nil {
 		ctx.JSON(http.StatusOK, err)
 		return
 	}
-	records, err := service.DeviceCountService.GetMonthList(claims.TenantId, req)
+	var records interface{}
+	err = nil
+	if req.QueryType == "day" {
+		records, err = service.DeviceCountService.GetDayList(claims.TenantId, req)
+	} else {
+		records, err = service.DeviceCountService.GetMonthList(claims.TenantId, req)
+	}
 	if err != nil {
 		ctx.JSON(http.StatusOK, err)
 		return
@@ -50,7 +56,7 @@ func (c deviceCtl) MonthList(ctx *gin.Context) {
 func (c deviceCtl) YearList(ctx *gin.Context) {
 	value, _ := ctx.Get(middleware.Authorization)
 	claims := value.(*middleware.Claims)
-	var req model.ResuestDeviceCountFilter
+	var req model.RequestDeviceCountFilter
 	err := ctx.ShouldBindQuery(&req)
 	if err != nil {
 		ctx.JSON(http.StatusOK, err)

+ 45 - 2
app/operation/dao/viewsAllCodeDeviceDao.go

@@ -41,7 +41,7 @@ func (v ViewsAllCodeDevice) GetDeviceCount() ([]ViewsAllCodeDevice, error) {
 }
 
 // 按月统计
-func (v ViewsAllCodeDevice) GetMonthDeviceCount(req model.ResuestDeviceCountFilter) ([]ViewsAllCodeDevice, error) {
+func (v ViewsAllCodeDevice) GetMonthDeviceCount(req model.RequestDeviceCountFilter) ([]ViewsAllCodeDevice, error) {
 	var list []ViewsAllCodeDevice
 	sql := `
 SELECT
@@ -85,7 +85,7 @@ GROUP BY
 }
 
 // 按年统计
-func (v ViewsAllCodeDevice) GetYearDeviceCount(req model.ResuestDeviceCountFilter) ([]ViewsAllCodeDevice, error) {
+func (v ViewsAllCodeDevice) GetYearDeviceCount(req model.RequestDeviceCountFilter) ([]ViewsAllCodeDevice, error) {
 	var list []ViewsAllCodeDevice
 	sql := `
 SELECT
@@ -127,3 +127,46 @@ GROUP BY
 	err := Db.Debug().Model(&v).Raw(sql, v.TenantId).Scan(&list).Error
 	return list, err
 }
+
+func (v ViewsAllCodeDevice) GetDayDeviceCount(req model.RequestDeviceCountFilter) ([]ViewsAllCodeDevice, error) {
+	var list []ViewsAllCodeDevice
+	sql := `
+SELECT
+	month_time,
+	sum(count_t) AS count_t,
+	SUM(CASE device_type WHEN 1 THEN count_t ELSE 0 END) as device_type1_count_t,
+	SUM(CASE device_type WHEN 2 THEN count_t ELSE 0 END) as device_type2_count_t,
+	SUM(CASE device_type WHEN 3 THEN count_t ELSE 0 END) as device_type3_count_t,
+	SUM(CASE device_type WHEN 4 THEN count_t ELSE 0 END) as device_type4_count_t,
+	SUM(CASE device_type WHEN 5 THEN count_t ELSE 0 END) as device_type5_count_t,
+	SUM(CASE device_type WHEN 6 THEN count_t ELSE 0 END) as device_type6_count_t,
+	SUM(CASE device_type WHEN 7 THEN count_t ELSE 0 END) as device_type7_count_t,
+	SUM(CASE device_type WHEN 8 THEN count_t ELSE 0 END) as device_type8_count_t,
+	SUM(CASE device_type WHEN 9 THEN count_t ELSE 0 END) as device_type9_count_t,
+	SUM(CASE device_type WHEN 10 THEN count_t ELSE 0 END) as device_type10_count_t,
+	SUM(CASE device_type WHEN 11 THEN count_t ELSE 0 END) as device_type11_count_t,
+	SUM(CASE device_type WHEN 12 THEN count_t ELSE 0 END) as device_type12_count_t,
+	SUM(CASE device_type WHEN 13 THEN count_t ELSE 0 END) as device_type13_count_t,
+	SUM(CASE device_type WHEN 14 THEN count_t ELSE 0 END) as device_type14_count_t,
+	SUM(CASE device_type WHEN 15 THEN count_t ELSE 0 END) as device_type15_count_t
+FROM
+	(
+	SELECT
+		device_type,
+		count(*) AS count_t,
+		date_format( create_time, '%Y-%m-%d' ) AS month_time 
+	FROM
+		view_all_code 
+	where tenant_id=? and create_time >= ? and create_time<=?
+	GROUP BY
+		device_type,
+		month_time 
+	ORDER BY
+		month_time DESC 
+	) a 
+GROUP BY
+	month_time
+`
+	err := Db.Debug().Model(&v).Raw(sql, v.TenantId, req.StartTime, req.EndTime).Scan(&list).Error
+	return list, err
+}

+ 2 - 1
app/operation/model/deviceCount.go

@@ -1,6 +1,7 @@
 package model
 
-type ResuestDeviceCountFilter struct {
+type RequestDeviceCountFilter struct {
 	StartTime string `form:"queryStartDate"`
 	EndTime   string `form:"queryEndDate"`
+	QueryType string `form:"queryType"`
 }

+ 10 - 3
app/operation/service/deviceCoutService.go

@@ -10,7 +10,7 @@ var DeviceCountService = new(deviceCountService)
 
 type deviceCountService struct{}
 
-func (s deviceCountService) CountDevice(tenantId int, req model.ResuestDeviceCountFilter) ([]dao.ViewsAllCodeDevice, error) {
+func (s deviceCountService) CountDevice(tenantId int, req model.RequestDeviceCountFilter) ([]dao.ViewsAllCodeDevice, error) {
 	list := dao.ViewsAllCodeDevice{
 		TenantId: tenantId,
 	}
@@ -27,16 +27,23 @@ func (s deviceCountService) CountDevice(tenantId int, req model.ResuestDeviceCou
 	return counts, nil
 }
 
-func (s deviceCountService) GetYearList(tenantId int, req model.ResuestDeviceCountFilter) ([]dao.ViewsAllCodeDevice, error) {
+func (s deviceCountService) GetYearList(tenantId int, req model.RequestDeviceCountFilter) ([]dao.ViewsAllCodeDevice, error) {
 	list := dao.ViewsAllCodeDevice{
 		TenantId: tenantId,
 	}
 	return list.GetYearDeviceCount(req)
 }
 
-func (s deviceCountService) GetMonthList(tenantId int, req model.ResuestDeviceCountFilter) ([]dao.ViewsAllCodeDevice, error) {
+func (s deviceCountService) GetMonthList(tenantId int, req model.RequestDeviceCountFilter) ([]dao.ViewsAllCodeDevice, error) {
 	list := dao.ViewsAllCodeDevice{
 		TenantId: tenantId,
 	}
 	return list.GetMonthDeviceCount(req)
 }
+
+func (s deviceCountService) GetDayList(tenantId int, req model.RequestDeviceCountFilter) ([]dao.ViewsAllCodeDevice, error) {
+	list := dao.ViewsAllCodeDevice{
+		TenantId: tenantId,
+	}
+	return list.GetDayDeviceCount(req)
+}