Go language actual combat tutorial backstage management page statistical function development (2)

Go language actual combat tutorial backstage management page statistical function development (2)

The previous section introduced the statistical function development of the background management page (1), from the function introduction, to the interface request analysis and classification, and finally the code design. After the introduction of the previous section, the business logic and development logic have been explained clearly. The content of this section focuses on programming code to achieve specific functions.

Same-day growth data function, seven-day growth data function

Through the browser tool debugging, you will find that the growth function of the day and the request interface used for the growth data of the past 7 days are the same, but the value transfer is more special. Taking the user growth request of the day as an example, the user growth data request interface of the day is as follows:

/statis/user/NaN-NaN-NaN/count 

It can be seen that the passed value is NaN-NaN-NaN .

Then check the growth data request in the past seven days. Still taking the user growth request as an example, the data growth request interface for a certain day in the past seven days is as follows:

/statis/user/2019-04-08/count 

Based on the analysis of the above two situations, the request on the day is the same as the request for the past seven days, but the value is different. Therefore, when the program is developed and implemented, the current-day growth data and the 7-day growth data request can be combined and developed, and the data growth request of the current day can be processed separately.

We have defined the StatisController structure to implement the statistical data function request, as shown below:

type StatisController struct {
   //
    Ctx iris.Context
 
   //
    Service service.StatisService
 
   //session
    Session *sessions.Session
} 
  • Routing group analysis and statistics interface We have analyzed the interface, and we can find that the request form of the administrator, user, and order is the same. Therefore, we can use the routing group to analyze the statistics interface. We define the resolved routing group as follows:

app.Party("/statis/{model}/{date}/") 
  • Defining the statistics interface According to the rules of the interface we analyzed, we can define the GetCount method in the StatisController structure for processing. The GetCount method is defined as follows:

      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,
            },
        }
      }
      

The GetCount method classifies different request types by parsing the request URL and calls different function methods, namely: GetUserDailyCount, GetOrderDailyCount, and GetAdminDailyCount. The three methods are provided by service.StatisService. In this section, we take the user data growth interface request as an example. The code implementation is as follows:

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
} 

The implementation of the other two functional modules is the same, but the query data table is different. The detailed content is provided in the course supporting video and source code.

SQL query

In this function, the date field uses the time.Time type in the data structure definition, and it will be mapped to the DateTime type in the database.

  • When the time range during query database query, sql statement to query the use of time is the BETWEEN ... and ... . The functional query statement in this section is:

      select count(*) from user where register_time between start and end and del_flag = 0   
  • Date format In golang language, the date format uses the time.Format method to customize the format of the time. However, one thing to note is that when entering the Format standard time format, the required time point must be 2006-01-02 15:04:05 . If it is not at this point in time, there will be errors in the formatted time, which is a point to note.

Data total function development

The total data record function request is developed according to the function of the module. Taking the administrator data total record request as an example, the request URL is as follows:

/admin/count 

In the AdminController structure, define the GetCount method to obtain the total number of administrators. The method details are as follows:

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,
        },
    }
} 

In the process of processing the request, the controller calls the function of querying the total number of administrator records provided by AdminService. The GetAdminCount method is implemented as follows:

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
} 

After the content of this section has been developed, the implementation effects are as follows, and the specific code is provided in the course package: