go语言实战教程之 后台管理页面统计功能开发(2)

上节内容介绍了后台管理页面统计功能开发(1),从功能介绍,到接口请求分析和归类,最后是代码设计。通过上节内容的介绍,已经将业务逻辑和开发逻辑解释清楚,本节内容侧重于编程代码实现具体的功能。golang

当日增加数据功能、七日增加数据功能

经过浏览器工具调试会发现,当日增加功能和近7日增加数据使用的请求接口相同,只是传值较为特殊。以当日用户增加请求为例,当日用户增加数据请求接口以下:sql

/statis/user/NaN-NaN-NaN/count复制代码

可见,传递的值为NaN-NaN-NaN数据库

进而查看近七日增加数据请求。仍以用户增加请求为例,近七日中的某天数据增加请求接口以下:编程

/statis/user/2019-04-08/count复制代码

综合上述两种状况的分析,当日请求与近七日请求url相同,只是传值不一样。所以,在程序开发实现时,能够将当日增加数据和7日增加数据请求合并开发,仅对当日数据增加请求作单独的处理便可。浏览器

咱们已经定义过了StatisController结构体,用来实现统计数据的功能请求,以下所示:bash

type StatisController struct {
    //上下文环境对象
    Ctx iris.Context
​
    //统计功能的服务实现接口
    Service service.StatisService
​
    //session
    Session *sessions.Session
}复制代码
  • 路由组解析统计接口 咱们已经分析过接口,能够发现管理员,用户,及订单,三者的请求形式相同,所以能够采用路由组的方式解析统计接口,咱们定义解析的路由组以下:session

app.Party("/statis/{model}/{date}/")复制代码
  • 定义统计接口 根据咱们分析的接口的规则,咱们能够在StatisController结构体中定义GetCount方法用来处理,GetCount方法定义以下:数据结构

    func (sc *StatisController) GetCount() mvc.Result {
        // /statis/user/2019-03-10/count
        path := sc.Ctx.Path()
      
        var pathSlice []string
        if path != "" {
            pathSlice = strings.Split(path, "/")
        }
      
        //不符合请求格式
        if len(pathSlice) != 5 {
            return mvc.Response{
                Object: map[string]interface{}{
                    "status": utils.RECODE_FAIL,
                    "count":  0,
                },
            }
        }
      
        //将最前面的去掉
        pathSlice = pathSlice[1:]
        model := pathSlice[1]
        date := pathSlice[2]
        var result int64
        switch model {
        case "user":
            iris.New().Logger().Error(date) //时间
            result = sc.Service.GetUserDailyCount(date)
        case "order":
            result = sc.Service.GetOrderDailyCount(date)
        case "admin":
            result = sc.Service.GetAdminDailyCount(date)
        }
      
        return mvc.Response{
            Object: map[string]interface{}{
                "status": utils.RECODE_OK,
                "count":  result,
            },
        }
      }
    ​复制代码

GetCount方法经过解析请求URL,对不一样的请求类型进行分类调用不一样的功能方法,分别是:GetUserDailyCount,GetOrderDailyCount,GetAdminDailyCount。三者方法均由service.StatisService提供。在本节内容中,咱们以用户数据增加接口请求为例,代码实现以下:mvc

func (ss *statisService) GetUserDailyCount(date string) int64 {
​
    if date == "NaN-NaN-NaN" { //当日增加数据请求
        date = time.Now().Format("2006-01-02")
    }
​
    startDate, err := time.Parse("2006-01-02", date)
    if err != nil {
        return 0
    }
​
    endDate := startDate.AddDate(0, 0, 1)
    result, err := ss.Engine.Where(" register_time between ? and ? and del_flag = 0 ", startDate.Format("2006-01-02 15:04:05"), endDate.Format("2006-01-02 15:04:05")).Count(model.User{})
    if err != nil {
        return 0
    }
    return result
}复制代码

其余两个功能模块的实现相同,查询数据表不一样。详细内容在课程配套视频和源码中提供。app

SQL语句查询

在该功能中,日期字段在数据结构体定义中使用的是time.Time类型,在数据库中会被映射成DateTime类型。

  • 时间范围查询 在进行数据库查询时,时间的查询使用的sql语句是between ... and...。本节内容的功能查询语句为:

    select count(*) from user where register_time between start and end and del_flag = 0  复制代码
  • 日期格式 在golang语言中,日期格式使用time.Format方法来自定义时间的格式。可是,有一点须要注意,在输入Format标准时间格式时,要求的时间点必须是2006-01-02 15:04:05。若是不是该时间点,格式化出来的时间就会出现错误,这是须要注意的一点。

数据总数功能开发

数据总记录数功能请求是按照模块的功能来进行开发的,以管理员数据总记录请求为例,请求url以下:

/admin/count复制代码

在AdminController结构体中,定义GetCount方法用来获取管理员总数,方法详情以下:

func (ac *AdminController) GetCount() mvc.Result {
​
    count, err := ac.Service.GetAdminCount()
    if err != nil {
        return mvc.Response{
            Object: map[string]interface{}{
                "status":  utils.RECODE_FAIL,
                "message": utils.Recode2Text(utils.RESPMSG_ERRORADMINCOUNT),
                "count":   0,
            },
        }
    }
​
    return mvc.Response{
        Object: map[string]interface{}{
            "status": utils.RECODE_OK,
            "count":  count,
        },
    }
}复制代码

在该控制器处理请求的过程当中,调用到了AdminService提供的查询管理员总记录数的功能,GetAdminCount方法实现以下:

func (ac *adminSevice) GetAdminCount() (int64, error) {
    count, err := ac.engine.Count(new(model.Admin))
​
    if err != nil {
        panic(err.Error())
        return 0, err
    }
    return count, nil
}复制代码

本节内容开发完毕实现效果以下所示,具体代码在课程配套中提供:

相关文章
相关标签/搜索