ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 需求描述:项目中首页用几个图表来展示销售数据,根据不同的时间范围、时间维度来查询,有些时间段没有数据的,图表中各项数据显示为0。 ``` /** * * 终端报表 * User: wenhu8000 */ function getReport() { #--获取公共参数 $d = $this->getCommonData();#halt($d); #--查询条件 /*$where['RD.i_code'] = ['>', -1];*/ #--排序 $order = "total DESC"; #--时间范围 if (!empty($d['range'])) { $time = $this->getTimeRange($d['range']); $d['staTime'] = $time['staTime']; $d['endTime'] = $time['endTime']; $order = "times"; } #--时间维度 if (!empty($d['times'])) { $times = $this->getTimes($d['times']); $order = "times"; } else { $times = "%Y-%m"; $d['times'] = "month"; } #--计算出起始时间按不同时间维度的差,最小为 1 $limit = Db::query("SELECT TIMESTAMPDIFF(".$d['times'].",?,?) as num from table1 limit 1",[$d['staTime'],$d['endTime']])[0]['num']?:1;#halt($limit); #--查询数据 $sql = Db::table("(SELECT DATE_FORMAT(@date := DATE_ADD(@date, INTERVAL + 1 " . $d['times'] . "),'" . $times . "') times FROM ( SELECT @date := DATE_ADD('" . $d['staTime'] . "', INTERVAL - 1 " . $d['times'] . ") FROM rdr1 LIMIT " . $limit . " ) time)") ->alias('TI') ->field("TI.times,COUNT(OR.field2) as orderNum,SUM(OR.total) as total,COUNT(DISTINCT RD.i_code) as iNum") ->join('table1 I', "I.s_code='". $this->aInfo["shop_code"] ."'", 'LEFT') ->join('table2 OR', "INSTR(OR.datefield,TI.times)>0 && OR.datefield BETWEEN '" . $d['staTime'] . "' and '" . $d['endTime'] . "'", 'LEFT') ->join('table3 RD', "RD.field1=OR.field2", 'LEFT') ->group("times") ->order($order) ->buildSql(); #halt($sql); $res = Db::table($sql)->alias('TT')->paginate($d['limit'], $d['paginate'], ['page' => $d['page']])->toArray(); #halt($res); #--返回结果 if (empty($res['data'])) { return ['code' => '1', 'data' => [], 'msg' => '无数据!']; } else { #--合计 $res['orderSum'] = array_sum(array_column($res['data'], 'orderNum')); $res['totalSum'] = array_sum(array_column($res['data'], 'total')); $res['iSum'] = array_sum(array_column($res['data'], 'iNum')); return ['code' => '0', 'data' => $res]; } } /** * * 获取特定时间范围用于查询 * User: wenhu8000 * @name: 时间范围名称 */ function getTimeRange($name) { #--时间范围 switch (true) { #--去年 case $name === "lastYear": $d['staTime'] = date('Y-01-01 00:00:00', strtotime('-1 year')); $d['endTime'] = date('Y-01-01 00:00:00', strtotime('+0 year')); break; #--上季度 case $name === "lastQuarter": $num = (ceil(date("n") / 3) * 3 - 3) + 1; $d['staTime'] = date('Y-m-01 00:00:00', strtotime('-' . (date("n") - $num + 3) . " month")); $d['endTime'] = date('Y-m-01 00:00:00', strtotime('+' . ($num - date("n")) . " month")); break; #--上月 case $name === "lastMonth": $d['staTime'] = date('Y-m-01 00:00:00', strtotime('-1 month')); $d['endTime'] = date('Y-m-01 00:00:00', strtotime('+0 month')); break; #--上周 case $name === "lastWeek": $d['staTime'] = date("Y-m-d 00:00:00", strtotime(date("Y") . 'W' . (date("W") - 1))); $d['endTime'] = date("Y-m-d 00:00:00", strtotime(date("Y") . 'W' . date("W"))); break; #--昨天 case $name === "yesterday": $d['staTime'] = date('Y-m-d 00:00:00', strtotime('-1 day')); $d['endTime'] = date('Y-m-d 00:00:00', strtotime('+0 day')); break; #--今年 case $name === "year": $d['staTime'] = date('Y-01-01 00:00:00', strtotime('+0 year')); $d['endTime'] = date('Y-01-01 00:00:00', strtotime('+1 year')); break; #--本季度 case $name === "quarter": $num = (ceil(date("n") / 3) * 3 - 3) + 1; $d['staTime'] = date('Y-m-01 00:00:00', strtotime('-' . (date("n") - $num) . " month")); $d['endTime'] = date('Y-m-01 00:00:00', strtotime('+' . ($num + 3 - date("n")) . " month")); break; #--本月 case $name === "month": $d['staTime'] = date('Y-m-01 00:00:00', strtotime('+0 month')); $d['endTime'] = date('Y-m-01 00:00:00', strtotime('+1 month')); break; #--本周 case $name === "week": $d['staTime'] = date("Y-m-d 00:00:00", strtotime(date("Y") . 'W' . date("W"))); $d['endTime'] = date("Y-m-d 00:00:00", strtotime(date("Y") . 'W' . (date("W") + 1))); break; #--默认返回当天范围 default: $d['staTime'] = date('Y-m-d 00:00:00', strtotime('+0 day')); $d['endTime'] = date('Y-m-d 00:00:00', strtotime('+1 day')); } return $d; } /** * * 获取不同时间维度,用于分组查询 * User: wenhu8000 * @name: 时间维度名称 */ function getTimes($name) { #--时间维度 switch (true) { #--按月 case $name === "month": $d = "%Y-%m"; break; #--按周 case $name === "week": $d = "%Y-%v"; break; #--按日 case $name === "day": $d = "%Y-%m-%d"; break; #--按时 case $name === "hour": $d = "%Y-%m-%d %H"; break; #--默认按月 default: $d = "%Y-%m"; } return $d; } ```