diff --git a/app/common/logic/store_order/StoreOrderLogic.php b/app/common/logic/store_order/StoreOrderLogic.php index 7ecca4bd0..6aa9803f8 100644 --- a/app/common/logic/store_order/StoreOrderLogic.php +++ b/app/common/logic/store_order/StoreOrderLogic.php @@ -201,6 +201,18 @@ class StoreOrderLogic extends BaseLogic return StoreOrder::where($where)->sum('pay_price'); } + /** + * 订单统计 + * @param $storeId + * @param $start + * @param $end + * @param $extra + * @return float|\think\db\Query + */ + public function storeOrderSumByDate($storeId, $start, $end, $extra = []) + { + return StoreOrder::where('store_id', $storeId)->where('paid', 1)->where($extra)->whereBetweenTime('create_time', $start, $end)->sum('pay_price'); + } /** * 退款 diff --git a/app/store/logic/WorkbenchLogic.php b/app/store/logic/WorkbenchLogic.php index db3e62ccc..9df3aa60e 100644 --- a/app/store/logic/WorkbenchLogic.php +++ b/app/store/logic/WorkbenchLogic.php @@ -15,6 +15,7 @@ namespace app\store\logic; +use app\common\enum\PayEnum; use app\common\logic\BaseLogic; use app\common\logic\store_order\StoreOrderLogic; use app\common\model\store_order\StoreOrder; @@ -39,42 +40,47 @@ class WorkbenchLogic extends BaseLogic public static function index($params) { $data = []; - $orderLogic = new StoreOrderLogic(); - $data['order_amount'] = $orderLogic->storeOrderSumByWhere([ - 'store_id' => $params['store_id'], - 'paid' => 1, - ]); - $data['cashier_amount'] = $orderLogic->storeOrderSumByWhere([ - 'store_id' => $params['store_id'], - 'paid' => 1, - 'shipping_type' => 3, - ]); - $data['delivery_amount'] = $orderLogic->storeOrderSumByWhere([ - 'store_id' => $params['store_id'], - 'paid' => 1, - 'shipping_type' => 1, - ]); - $data['verify_amount'] = $orderLogic->storeOrderSumByWhere([ - 'store_id' => $params['store_id'], - 'paid' => 1, - 'shipping_type' => 2, - ]); $startTime = $params['start_time']; $endTime = $params['end_time']; - $days = (new \DateTime($endTime))->diff(new \DateTime($startTime))->days; - $days += 1; - if ($days == 1) { + $endTime = date('Y-m-d', strtotime($endTime) + 86400); + $dateDiff = (new \DateTime($endTime))->diff(new \DateTime($startTime)); + if ($dateDiff->days > 366) { + throw new \Exception('时间范围不能超过一年'); + } + $orderLogic = new StoreOrderLogic(); + $data['order_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime); + $data['balance_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['pay_type' => PayEnum::BALANCE_PAY]); + $data['cashier_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['shipping_type' => 3]); + $data['delivery_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['shipping_type' => 1]); + $data['verify_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['shipping_type' => 2]); + $data['user_number'] = StoreOrder::where('store_id', $params['store_id']) + ->where('paid', 1) + ->whereBetweenTime('create_time', $startTime, $endTime) + ->group('uid') + ->count(); + if ($dateDiff->days == 1) { $group = 'HOUR(pay_time)'; - $timeRange = [0, 23]; - $endTime = date('Y-m-d H:i:s', strtotime($endTime) + 86400); + $i = 0; + while ($i < 24) { + $timeRange[] = date('H', strtotime("+$i hours", strtotime($startTime))); + $i++; + } $field = 'from_unixtime(pay_time,"%H") as pay_time,sum(pay_price) as pay_price'; - } elseif ($days <= 31) { + } elseif ($dateDiff->days <= 31) { $group = 'DAY(pay_time)'; - $timeRange = [1, $days]; + $i = 0; + while ($i < $dateDiff->days) { + $timeRange[] = date('m-d', strtotime("+$i days", strtotime($startTime))); + $i++; + } $field = 'from_unixtime(pay_time,"%m-%d") as pay_time,sum(pay_price) as pay_price'; } else { $group = 'MONTH(pay_time)'; - $timeRange = [1, (int)date('n')]; + $i = 0; + while ($i <= $dateDiff->m) { + $timeRange[] = date('Y-m', strtotime("+$i months", strtotime($startTime))); + $i++; + } $field = 'from_unixtime(pay_time,"%Y-%m") as pay_time,sum(pay_price) as pay_price'; } $orderList = StoreOrder::field($field) @@ -96,17 +102,17 @@ class WorkbenchLogic extends BaseLogic $orderListTmp = []; $userListTmp = []; $range = []; - for ($i = $timeRange[0]; $i <= $timeRange[1]; $i++) { - $range[] = $i; - if (!isset($orderList[$i])) { - $orderListTmp[$i] = 0; + foreach ($timeRange as $item) { + $range[] = $item; + if (!isset($orderList[$item])) { + $orderListTmp[$item] = 0; } else { - $orderListTmp[$i] = $orderList[$i]['pay_price']; + $orderListTmp[$item] = $orderList[$item]['pay_price']; } - if (!isset($userList[$i])) { - $userListTmp[$i] = 0; + if (!isset($userList[$item])) { + $userListTmp[$item] = 0; } else { - $userListTmp[$i] = $userList[$i]['user_num']; + $userListTmp[$item] = $userList[$item]['user_num']; } } $data['statistics'] = [