diff --git a/app/admin/controller/WorkbenchController.php b/app/admin/controller/WorkbenchController.php index 5449b6df..8fd11a57 100644 --- a/app/admin/controller/WorkbenchController.php +++ b/app/admin/controller/WorkbenchController.php @@ -226,6 +226,7 @@ class WorkbenchController extends BaseAdminController return $this->data($data); } //-------------------------------商品统计---------------------------------------// + /** * 商品概况 */ @@ -536,6 +537,7 @@ class WorkbenchController extends BaseAdminController } //-------------------------------用户统计---------------------------------------// + /** * 获取用户概况 */ diff --git a/app/common/logic/store_order/StoreOrderLogic.php b/app/common/logic/store_order/StoreOrderLogic.php index 6aa9803f..a5b75043 100644 --- a/app/common/logic/store_order/StoreOrderLogic.php +++ b/app/common/logic/store_order/StoreOrderLogic.php @@ -4,16 +4,13 @@ namespace app\common\logic\store_order; use app\common\enum\OrderEnum; use app\common\logic\BaseLogic; -use app\common\enum\PayEnum; use app\common\enum\YesNoEnum; use app\common\model\dict\DictType; use app\common\model\order\Cart; use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_order\StoreOrder; use app\common\model\store_order_cart_info\StoreOrderCartInfo; -use app\common\model\store_product\StoreProduct; use app\common\model\store_product_unit\StoreProductUnit; -use app\common\model\user\UserAddress; use app\common\service\pay\PayService; use Exception; use support\Cache; @@ -41,7 +38,7 @@ class StoreOrderLogic extends BaseLogic self::$total = 0; /** 计算价格 */ foreach ($cart_select as $k => $v) { - $find = StoreBranchProduct::where(['product_id' => $v['product_id'],'store_id'=>Request()->adminInfo['store_id']])->field('store_name,image,unit,price')->find(); + $find = StoreBranchProduct::where(['product_id' => $v['product_id'], 'store_id' => Request()->adminInfo['store_id']])->field('store_name,image,unit,price')->find(); if (!$find) { continue; } @@ -51,7 +48,7 @@ class StoreOrderLogic extends BaseLogic $cart_select[$k]['old_cart_id'] = $v['id']; $cart_select[$k]['cart_num'] = $v['cart_num']; $cart_select[$k]['verify_code'] = $params['verify_code'] ?? ''; - $cart_select[$k]['store_id'] =Request()->adminInfo['store_id'] ?? ''; + $cart_select[$k]['store_id'] = Request()->adminInfo['store_id'] ?? ''; $cart_select[$k]['staff_id'] = Request()->adminInfo['id'] ?? ''; $cartInfo = $cart_select[$k]; $cartInfo['name'] = $find['store_name']; @@ -78,11 +75,11 @@ class StoreOrderLogic extends BaseLogic // $discountRate = '0.99';//首单逻辑 $discountRate = $check['remark']; $discountRate = bcdiv($discountRate, '100', 2); - $pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2); + $pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2); } else { $discountRate = bcdiv(1, '100', 2); - $pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2); + $pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2); } if (!empty(self::$total) && !empty($pay_price)) { bcscale(2); @@ -106,6 +103,7 @@ class StoreOrderLogic extends BaseLogic } return ['order' => $order, 'cart_list' => $cart_select]; } + /** * 创建新订单 * @return Object|bool|array @@ -209,9 +207,9 @@ class StoreOrderLogic extends BaseLogic * @param $extra * @return float|\think\db\Query */ - public function storeOrderSumByDate($storeId, $start, $end, $extra = []) + public function storeOrderSumByDate($storeId, $start, $end, $extra = [], $field = 'pay_price') { - return StoreOrder::where('store_id', $storeId)->where('paid', 1)->where($extra)->whereBetweenTime('create_time', $start, $end)->sum('pay_price'); + return StoreOrder::where('store_id', $storeId)->where('paid', 1)->where($extra)->whereBetweenTime('pay_time', $start, $end)->sum($field); } /** @@ -222,14 +220,14 @@ class StoreOrderLogic extends BaseLogic * @return bool * @throws Exception */ - public function refund($order_sn,$refund_money,$total) + public function refund($order_sn, $refund_money, $total) { try { $wechat = new PayService(1); $time = time(); $order = [ 'out_trade_no' => $order_sn, - 'out_refund_no' => 'BO'.$time, + 'out_refund_no' => 'BO' . $time, 'amount' => [ 'refund' => $refund_money, 'total' => $total, @@ -244,14 +242,14 @@ class StoreOrderLogic extends BaseLogic ]; - $res = $wechat->wechat->refund($order); - if($res['status'] == 'PROCESSING'){ + $res = $wechat->wechat->refund($order); + if ($res['status'] == 'PROCESSING') { return true; } return false; } catch (Exception $e) { - \support\Log::info($e->extra['message']?? $e->getMessage()); - throw new \Exception($e->extra['message']?? $e->getMessage()); + \support\Log::info($e->extra['message'] ?? $e->getMessage()); + throw new \Exception($e->extra['message'] ?? $e->getMessage()); } } diff --git a/app/store/controller/WorkbenchController.php b/app/store/controller/WorkbenchController.php index 36bb9e10..64484173 100644 --- a/app/store/controller/WorkbenchController.php +++ b/app/store/controller/WorkbenchController.php @@ -30,7 +30,7 @@ class WorkbenchController extends BaseAdminController } #[ - ApiDoc\Title('商品统计'), + ApiDoc\Title('商品统计(暂时不用)'), ApiDoc\url('/store/workbench/product'), ApiDoc\Method('GET'), ApiDoc\NotHeaders(), @@ -42,13 +42,14 @@ class WorkbenchController extends BaseAdminController ] public function product() { - $storeId = $this->request->adminInfo['store_id']; - $result = WorkbenchLogic::index($storeId); + $params = $this->request->get(); + $params['store_id'] = $this->request->adminInfo['store_id']; + $result = WorkbenchLogic::product($params); return $this->data($result); } #[ - ApiDoc\Title('配送统计'), + ApiDoc\Title('配送统计(暂时不用)'), ApiDoc\url('/store/workbench/delivery'), ApiDoc\Method('GET'), ApiDoc\NotHeaders(), @@ -61,8 +62,98 @@ class WorkbenchController extends BaseAdminController public function delivery() { $storeId = $this->request->adminInfo['store_id']; - $result = WorkbenchLogic::index($storeId); + $result = WorkbenchLogic::delivery($storeId); return $this->data($result); } + #[ + ApiDoc\Title('商品统计-概况'), + ApiDoc\url('/store/workbench/get_basic'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Author('中国队长'), + ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'), + ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'), + ApiDoc\Header(ref: [Definitions::class, "token"]), + ApiDoc\ResponseSuccess("data", type: "array"), + ] + public function get_basic(\app\admin\controller\WorkbenchController $workbench) + { + $params = $this->request->get(); + $params['store_id'] = $this->request->adminInfo['store_id']; + return $workbench->get_basic(); + } + + #[ + ApiDoc\Title('商品统计-图表'), + ApiDoc\url('/store/workbench/get_trend'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Author('中国队长'), + ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'), + ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'), + ApiDoc\Header(ref: [Definitions::class, "token"]), + ApiDoc\ResponseSuccess("data", type: "array"), + ] + public function get_trend(\app\admin\controller\WorkbenchController $workbench) + { + $params = $this->request->get(); + $params['store_id'] = $this->request->adminInfo['store_id']; + return $workbench->get_trend(); + } + + #[ + ApiDoc\Title('商品统计-排行'), + ApiDoc\url('/store/workbench/get_product_ranking'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Author('中国队长'), + ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'), + ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'), + ApiDoc\Header(ref: [Definitions::class, "token"]), + ApiDoc\ResponseSuccess("data", type: "array"), + ] + public function get_product_ranking(\app\admin\controller\WorkbenchController $workbench) + { + $params = $this->request->get(); + $params['store_id'] = $this->request->adminInfo['store_id']; + return $workbench->get_product_ranking(); + } + + #[ + ApiDoc\Title('用户统计-概况'), + ApiDoc\url('/store/workbench/get_user_basic'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Author('中国队长'), + ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'), + ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'), + ApiDoc\Header(ref: [Definitions::class, "token"]), + ApiDoc\ResponseSuccess("data", type: "array"), + ] + public function get_user_basic(\app\admin\controller\WorkbenchController $workbench) + { + $params = $this->request->get(); + $params['store_id'] = $this->request->adminInfo['store_id']; + return $workbench->get_user_basic(); + } + + #[ + ApiDoc\Title('用户统计-图表'), + ApiDoc\url('/store/workbench/get_user_trend'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Author('中国队长'), + ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'), + ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'), + ApiDoc\Header(ref: [Definitions::class, "token"]), + ApiDoc\ResponseSuccess("data", type: "array"), + ] + public function get_user_trend(\app\admin\controller\WorkbenchController $workbench) + { + $params = $this->request->get(); + $params['store_id'] = $this->request->adminInfo['store_id']; + return $workbench->get_user_trend(); + } + } diff --git a/app/store/logic/WorkbenchLogic.php b/app/store/logic/WorkbenchLogic.php index e4f73541..8084b86f 100644 --- a/app/store/logic/WorkbenchLogic.php +++ b/app/store/logic/WorkbenchLogic.php @@ -18,8 +18,11 @@ 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\order\Cart; use app\common\model\store_cash_finance_flow\StoreCashFinanceFlow; use app\common\model\store_order\StoreOrder; +use app\common\model\store_order_cart_info\StoreOrderCartInfo; +use app\common\model\store_visit\StoreVisit; use app\common\service\ConfigService; use app\common\service\FileService; @@ -50,10 +53,12 @@ class WorkbenchLogic extends BaseLogic $data['cash_amount'] = StoreCashFinanceFlow::where('store_id', $params['store_id'])->whereBetweenTime('create_time', $startTime, $endTime)->sum('cash_price'); //核销订单金额 $data['verify_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, ['shipping_type' => 2]); + //门店收益金额 + $data['income_amount'] = $orderLogic->storeOrderSumByDate($params['store_id'], $startTime, $endTime, [], 'profit'); //门店成交用户数 $data['user_number'] = StoreOrder::where('store_id', $params['store_id']) ->where('paid', 1) - ->whereBetweenTime('create_time', $startTime, $endTime) + ->whereBetweenTime('pay_time', $startTime, $endTime) ->group('uid') ->count(); if ($dateDiff->days == 1) { @@ -132,8 +137,8 @@ class WorkbenchLogic extends BaseLogic ]; $data['order_list'] = StoreOrder::with('user')->where('store_id', $params['store_id']) ->where('paid', 1) - ->whereBetweenTime('create_time', $startTime, $endTime) - ->order('create_time', 'desc') + ->whereBetweenTime('pay_time', $startTime, $endTime) + ->order('pay_time', 'desc') ->limit(10) ->select()->toArray(); $data['pay_type'] = [ @@ -289,4 +294,141 @@ class WorkbenchLogic extends BaseLogic ]; } + public static function product($params) + { + $data = []; + $storeId = $params['store_id']; + $startTime = $params['start_time']; + $endTime = $params['end_time']; + $endTime = date('Y-m-d', strtotime($endTime) + 86400); + $dateDiff = (new \DateTime($endTime))->diff(new \DateTime($startTime)); + $data['visit_count'] = StoreVisit::where('store_id', $storeId)->whereBetweenTime('create_time', $startTime, $endTime)->count(); + $data['visit_user_count'] = StoreVisit::where('store_id', $storeId)->whereBetweenTime('create_time', $startTime, $endTime)->group('uid')->count(); + $data['add_cart_count'] = Cart::where('store_id', $storeId)->whereBetweenTime('create_time', $startTime, $endTime)->withTrashed()->group('product_id')->count(); + $data['add_order_count'] = StoreOrderCartInfo::where('store_id', $storeId)->whereBetweenTime('create_time', $startTime, $endTime)->withTrashed()->group('product_id')->count(); + $data['pay_count'] = StoreOrder::alias('t1') + ->join('store_order_cart_info t2', 't1.id = t2.oid') + ->where('t1.store_id', $storeId) + ->where('paid', 1) + ->whereBetweenTime('pay_time', $startTime, $endTime) + ->group('product_id') + ->count(); + $data['pay_amount'] = StoreOrder::where('store_id', $storeId) + ->where('paid', 1) + ->whereBetweenTime('pay_time', $startTime, $endTime) + ->sum('pay_price'); + $data['cost_amount'] = StoreOrder::where('store_id', $storeId) + ->where('paid', 1) + ->whereBetweenTime('pay_time', $startTime, $endTime) + ->sum('cost'); + $data['refund_amount'] = StoreOrder::where('store_id', $storeId) + ->where('paid', 1) + ->where('refund_status', '>', 0) + ->whereBetweenTime('pay_time', $startTime, $endTime) + ->sum('pay_price'); + $data['refund_count'] = StoreOrder::alias('t1') + ->join('store_order_cart_info t2', 't1.id = t2.oid') + ->where('t1.store_id', $storeId) + ->where('paid', 1) + ->where('refund_status', '>', 0) + ->whereBetweenTime('pay_time', $startTime, $endTime) + ->group('product_id') + ->count(); + $payUserCount = StoreOrder::where('store_id', $storeId) + ->where('paid', 1) + ->whereBetweenTime('pay_time', $startTime, $endTime) + ->group('uid') + ->count(); + if ($data['visit_user_count'] == 0) { + $data['trans_rate'] = 0; + } else { + $data['trans_rate'] = bcdiv($payUserCount * 100, $data['visit_user_count'], 2); + } + //商品图表统计开始,未完成 + if ($dateDiff->days == 1) { + $group = 'HOUR(pay_time)'; + $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 ($dateDiff->days <= 31) { + $group = 'DAY(pay_time)'; + $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)'; + $i = 0; + $month = 0; + if ($dateDiff->y > 0) { + $month = $dateDiff->y * 12; + } + if ($dateDiff->m > 0) { + $month += $dateDiff->m; + } + if ($dateDiff->d > 0) { + $month += 1; + } + while ($i < $month) { + $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) + ->where('store_id', $params['store_id']) + ->where('paid', 1) + ->whereBetweenTime('pay_time', $startTime, $endTime) + ->group($group) + ->select() + ->toArray(); + $userList = StoreOrder::field($field . ',count(uid) as user_num') + ->where('store_id', $params['store_id']) + ->where('paid', 1) + ->whereBetweenTime('pay_time', $startTime, $endTime) + ->group($group . ',uid') + ->select() + ->toArray(); + $orderList = reset_index($orderList, 'pay_time'); + $userList = reset_index($userList, 'pay_time'); + $orderListTmp = []; + $userListTmp = []; + $range = []; + foreach ($timeRange as $item) { + $range[] = $item; + if (!isset($orderList[$item])) { + $orderListTmp[$item] = 0; + } else { + $orderListTmp[$item] = $orderList[$item]['pay_price']; + } + if (!isset($userList[$item])) { + $userListTmp[$item] = 0; + } else { + $userListTmp[$item] = $userList[$item]['user_num']; + } + } + $data['statistics'] = [ + 'range' => $range, + 'data' => [ + 'order_amount' => array_values($orderListTmp), + 'user_number' => array_values($userListTmp) + ] + ]; + //商品图表统计结束 + + // 商品排行榜,未完成 + $data['rank_list'] = StoreOrder::with('user')->where('store_id', $params['store_id']) + ->where('paid', 1) + ->whereBetweenTime('create_time', $startTime, $endTime) + ->order('create_time', 'desc') + ->limit(10) + ->select()->toArray(); + return $data; + } + }