yii 框架实现按天,月,年,自定义时间段统计数(2)
然后查询数据库
$query = Order::find(); if($type == 'day') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d %H") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y-%m-%d")' => $time]); } else if($type == 'month') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y-%m")' => ($time . '-' . $time2)]); } else if ($type == 'year') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y")' => $time]); } else if ($type == 'range') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['between', 'FROM_UNIXTIME(pay_at,"%Y-%m-%d")', $time, $time2]); } $data = $query->andWhere(['pay_status' => 2])->groupBy('char_time')->all();
按时间排列下
$dataArr = []; foreach ($data as $allKey => $allVal) { $dataArr[$allVal->char_time]['char_time'] = $allVal->char_time; $dataArr[$allVal->char_time]['total_order'] = $allVal->total_order; $dataArr[$allVal->char_time]['total_order_amount'] = bcdiv($allVal->total_order_amount, 100, 2); }
再按时间获取对应数据
foreach ($rangeTime as $key => $val) { if($type == 'range') { if (array_key_exists($val, $dataArr)) { $charCountDatas[] = $dataArr[$val]['total_order']; $charAmountDatas[] = $dataArr[$val]['total_order_amount']; } else { $charCountDatas[] = 0; $charAmountDatas[] = 0; } } else { $theNow = strlen($val) == 2 ? $val : '0' . $val; if($type == 'day') { $theTime = $time . ' ' . $theNow; } else if($type == 'month') { $theTime = $time . '-' . $time2 . '-' . $theNow; } else if($type == 'year') { $theTime = $time . '-' . $theNow; } if (array_key_exists($theTime, $dataArr)) { $charCountDatas[] = $dataArr[$theTime]['total_order']; $charAmountDatas[] = $dataArr[$theTime]['total_order_amount']; } else { $charCountDatas[] = 0; $charAmountDatas[] = 0; } } }
封装下
/** * 时间段内支付订单量及金额 * type 类型: day, month, year * time: 时间, day: 选择的时间; month: 表示年;year: 表示年; range: 第一个时间 * time2: 时间: day: ''; month: 表示月;year: ''; range: 第二个时间 * rangeTime 时间段 day: 1-24小时; month: 1-30天; year:1-12月,range: time和time2之间的天 */ public function getDayOrderPayChar($type, $time, $time2, $rangeTime) { $query = Order::find(); if($type == 'day') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d %H") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y-%m-%d")' => $time]); } else if($type == 'month') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y-%m")' => ($time . '-' . $time2)]); } else if ($type == 'year') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['FROM_UNIXTIME(pay_at,"%Y")' => $time]); } else if ($type == 'range') { $query = $query->select(['FROM_UNIXTIME(pay_at,"%Y-%m-%d") as char_time', 'COUNT(id) as total_order', 'SUM(pay_amount) as total_order_amount']) ->where(['>=', 'FROM_UNIXTIME(pay_at,"%Y-%m-%d")', $time]) ->andWhere(['<=', 'FROM_UNIXTIME(pay_at,"%Y-%m-%d")', $time2]); } $data = $query->andWhere(['pay_status' => 2])->groupBy('char_time')->all(); $dataArr = []; foreach ($data as $allKey => $allVal) { $dataArr[$allVal->char_time]['char_time'] = $allVal->char_time; $dataArr[$allVal->char_time]['total_order'] = $allVal->total_order; $dataArr[$allVal->char_time]['total_order_amount'] = bcdiv($allVal->total_order_amount, 100, 2); } $charCountDatas = []; $charAmountDatas = []; foreach ($rangeTime as $key => $val) { if($type == 'range') { if (array_key_exists($val, $dataArr)) { $charCountDatas[] = $dataArr[$val]['total_order']; $charAmountDatas[] = $dataArr[$val]['total_order_amount']; } else { $charCountDatas[] = 0; $charAmountDatas[] = 0; } } else { $theNow = strlen($val) == 2 ? $val : '0' . $val; if($type == 'day') { $theTime = $time . ' ' . $theNow; } else if($type == 'month') { $theTime = $time . '-' . $time2 . '-' . $theNow; } else if($type == 'year') { $theTime = $time . '-' . $theNow; } if (array_key_exists($theTime, $dataArr)) { $charCountDatas[] = $dataArr[$theTime]['total_order']; $charAmountDatas[] = $dataArr[$theTime]['total_order_amount']; } else { $charCountDatas[] = 0; $charAmountDatas[] = 0; } } } $res = [ 'count' => [ 'name' => Yii::t('backend', 'hour_order_pay_count_title'), 'color' => '#99CC33', 'charData' => $charCountDatas ], 'amount' => [ 'name' => Yii::t('backend', 'hour_order_pay_amount_title'), 'color' => '#99CC33', 'charData' => $charAmountDatas ] ]; return $res; }
内容版权声明:除非注明,否则皆为本站原创文章。