ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
> # 按天、周、月、年 - vplog.created_at >= DATE(NOW()) - INTERVAL 6 DAY ``` SELECT DATE_FORMAT( created_at, '%m-%d' ) AS name, COUNT( id ) AS num FROM q_visit_patrol WHERE created_at BETWEEN DATE_SUB( NOW(), INTERVAL 6 DAY ) AND NOW() GROUP BY DATE_FORMAT(created_at,'%Y-%m-%d') ``` ``` switch types { case 1: where["created_at BETWEEN DATE_SUB(NOW(), INTERVAL ? DAY) AND NOW()"] = 6 fields = "DATE_FORMAT(created_at, '%m-%d') AS name,COUNT(id) AS value" groupBy = "DATE_FORMAT(created_at, '%Y-%m-%d')" case 2: where["created_at BETWEEN DATE_SUB(NOW(), INTERVAL ? DAY) AND NOW()"] = 29 fields = "DATE_FORMAT(created_at, '%m-%d') AS name,COUNT(id) AS value" groupBy = "DATE_FORMAT(created_at, '%Y-%m-%d')" case 3: where["STR_TO_DATE(month, '%Y-%m') BETWEEN DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL ? MONTH) AND DATE_FORMAT(NOW(), '%Y-%m-01')"] = 6 fields = "month AS name,COUNT(id) AS value" groupBy = "month" case 4: where["STR_TO_DATE(month, '%Y-%m') BETWEEN DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL ? MONTH) AND DATE_FORMAT(NOW(), '%Y-%m-01')"] = 12 fields = "month AS name,COUNT(id) AS value" groupBy = "month" } result, err := dao.QifuQVisitPatrol. Fields(fields). Where(where). Group(groupBy). All() ``` - 折线图固定数据 ~~~ //折线图固定数据 func GetDayStrMap(types int) []map[string]interface{}{ //classify 1近一周;2近一月;3近半年;4近一年 var dataArr = make([]map[string]interface{}, 0) now := time.Now() switch types { case 1: for i := 6; i >= 0; i-- { // 获取当前日期并格式化为 "07-05" 的形式 date := now.AddDate(0, 0, -i).Format("01-02") // 将日期作为键,初始值为0作为值存储到 map 中 dataArr = append(dataArr, map[string]interface{}{"name": date, "value":0}) } case 2: for i := 29; i >= 0; i-- { // 获取当前日期并格式化为 "07-05" 的形式 date := now.AddDate(0, 0, -i).Format("01-02") // 将日期作为键,初始值为0作为值存储到 map 中 dataArr = append(dataArr, map[string]interface{}{"name": date, "value":0}) } case 3: for i := 5; i >= 0; i-- { // 获取当前年月并格式化为 "2023-07" 的形式 date := now.AddDate(0, -i, 0).Format("2006-01") // 将日期作为键,初始值为0作为值存储到 map 中 dataArr = append(dataArr, map[string]interface{}{"name": date, "value":0}) } case 4: for i := 11; i >= 0; i-- { // 获取当前年月并格式化为 "2023-07" 的形式 date := now.AddDate(0, -i, 0).Format("2006-01") // 将日期作为键,初始值为0作为值存储到 map 中 dataArr = append(dataArr, map[string]interface{}{"name": date, "value":0}) } } return dataArr } ~~~