123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- <?php
- namespace App\Http\Admin;
- use Illuminate\Support\Facades\Request;
- use Illuminate\Support\Facades\DB;
- class OrderController extends AdminBaseController
- {
- public function __construct()
- {
- parent::__construct();
- $this->setWebsite(__NAMESPACE__);
- }
- public function analysis()
- {
- $yesterday_time = strtotime(date('Y-m-d', strtotime("-1 day")));
- $today_time = strtotime(date('Y-m-d'));
- // 昨天订单总数
- $yesterdayOrderTotal = $this->totalOrderNum($yesterday_time);
- // 今天订单总数
- $todayOrderTotal = $this->totalOrderNum($today_time);
- ########################################################################################
- // 昨天用户量
- $yesterdayMemberTotal = $this->totalMember($yesterday_time);
- // 今天用户量
- $todayMemberTotal = $this->totalMember($today_time);
- ########################################################################################
- // 昨天支付金额
- $yesterdayMoneyTotal = $this->totalMoneyNum($yesterday_time);
- // 今天支付金额
- $todayMoneyTotal = $this->totalMoneyNum($today_time);
- ########################################################################################
- $monthFirstDay = strtotime(date("Y-m-1 00:00:00"));
- $monthLastDay = strtotime(date("Y-m-" . date("t") . " 23:59:59"));
- // 本月的订单总数
- $monthOrderTotal = $this->totalOrderNum($monthFirstDay, $monthLastDay);
- // 本月的交易金额
- $monthMoneyTotal = $this->totalMoneyNum($monthFirstDay, $monthLastDay);
- ########################################################################################
- // 获取上个月第一天的时间戳
- $firstDayOfLastMonth = strtotime("first day of last month", $monthFirstDay);
- $lastDayOfLastMonth = strtotime("last day of last month", $monthLastDay);
- // 上月的订单总数
- $lastMonthOrderTotal = $this->totalOrderNum($firstDayOfLastMonth, $lastDayOfLastMonth);
- // 上月的交易金额
- $lastMonthMoneyTotal = $this->totalMoneyNum($firstDayOfLastMonth, $lastDayOfLastMonth);
- ########################################################################################
- // 总收入数
- $todayMoneyAll = $this->totalMoneyNum();
- // 总用户数
- $todayMemberAll = $this->totalMember();
- return responseMessage(1001, '', [
- 'yesterdayOrderTotal' => round($yesterdayOrderTotal, 2),
- 'todayOrderTotal' => round($todayOrderTotal, 2),
- 'yesterdayMemberTotal' => round($yesterdayMemberTotal, 2),
- 'todayMemberTotal' => round($todayMemberTotal, 2),
- 'yesterdayMoneyTotal' => round($yesterdayMoneyTotal, 2),
- 'todayMoneyTotal' => round($todayMoneyTotal, 2),
- 'monthOrderTotal' => round($monthOrderTotal, 2),
- 'monthMoneyTotal' => round($monthMoneyTotal, 2),
- 'lastMonthOrderTotal' => round($lastMonthOrderTotal, 2),
- 'lastMonthMoneyTotal' => round($lastMonthMoneyTotal, 2),
- 'todayMoneyAll' => round($todayMoneyAll, 2),
- 'todayMemberAll' => round($todayMemberAll, 2),
- ]);
- }
- /**
- * 按年统计
- *
- * @return array
- */
- public function statisByYear($year)
- {
- $arr = [];
- for ($m = 1; $m <= 12; $m++) {
- $monthFirstDay = strtotime(date($year . "-" . $m . "-1 00:00:00"));
- $monthLastDay = strtotime(date($year . "-" . $m . "-" . date('t',strtotime($year . "-" . $m)) . " 23:59:59"));
- $arr[] = $this->totalMoneyNum($monthFirstDay, $monthLastDay);
- }
- return $arr;
- }
- /**
- * 最近7天的订单量 todo 优化查询方式
- *
- */
- public function sevenDayOrder()
- {
- $days = [];
- $count = [];
- $total = [];
- for ($i = 0; $i < 7; $i++) {
- $day = date('Y-m-d', strtotime('-' . $i . ' day'));
- array_unshift($days, $day);
- array_unshift($count, $this->totalOrderNum(strtotime($day)));
- array_unshift($total, $this->totalMoneyNum(strtotime($day)));
- }
- return responseMessage(1001, '', ['days' => $days, 'count' => $count, 'total' => $total]);
- }
- /**
- * 获取支付金额
- *
- * @param int $start_time
- * @param int $end_time
- * @return mixed
- */
- public function totalMoneyNum(int $start_time = 0, int $end_time = 0): mixed
- {
- if (empty($end_time)) {
- $end_time = $start_time + 24 * 3600;
- }
- ########## 软件
- $data1 = $this->getOrderSum('order_product', $start_time, $end_time);
- return $data1;
- }
- /**
- * 注册人数
- */
- private function totalMember(int $start_time = 0, int $end_time = 0)
- {
- if (empty($start_time)) {
- return DB::table('user')->count();
- } else {
- if (empty($end_time)) {
- $end_time = $start_time + 24 * 3600;
- }
- return DB::table('user')->where('is_delete', 0)->whereBetween('created_at', [$start_time, $end_time])->count();
- }
- }
- // 所有的订单数量
- private function totalOrderNum($start_time, int $end_time = 0)
- {
- ########## 软件
- $data1 = $this->getOrderNum('order_product', $start_time, $end_time);
- return $data1;
- }
- /**
- * @param string $table
- * @param int $start_time
- * @param int $end_time
- * @return array|int
- */
- private function getOrderNum(string $table, int $start_time = 0, int $end_time = 0): int|array
- {
- if (empty($start_time)) {
- return DB::table($table)->count();
- } else {
- if (empty($end_time)) {
- $end_time = $start_time + 24 * 3600;
- }
- return DB::table($table)->where('order_status', 2)->where('is_delete', 0)->whereBetween('created_at', [ $start_time, $end_time])->count();
- }
- }
- /**
- * @param string $table
- * @param int $start_time
- * @param int $end_time
- * @return mixed
- */
- private function getOrderSum(string $table, int $start_time, int $end_time = 0): mixed
- {
- if (empty($start_time)) {
- return DB::table($table)->where('order_status', 2)->where('is_delete', 0)->sum('order_amount_total');
- } else {
- if (empty($end_time)) {
- $end_time = $start_time + 24 * 3600;
- }
- return DB::table($table)->where('order_status', 2)->where('is_delete', 0)->whereBetween('created_at', [$start_time, $end_time])->sum('order_amount_total');
- }
- }
- public function softOrder()
- {
- $mid = Request::input('mid');
- if (empty($mid)) {
- return responseMessage(2001, '参数错误');
- }
- // 获取商品信息
- $productInfo = DB::table('product')->where('mid', $mid)->first();
- if (empty($productInfo)) {
- return responseMessage(2001, '参数错误2');
- }
- $days = [];
- $countArr = [];
- for ($i = 0; $i < 15; $i++) {
- $day = date('Y-m-d', strtotime('-' . $i . ' day'));
- array_unshift($days, $day);
- $start_time = strtotime($day);
- $end_time = $start_time + 24 * 3600;
- $count = DB::table('order_product')
- ->where('product_id', $productInfo['id'])
- ->where('order_status', 2)
- ->where('is_delete', 0)
- ->whereBetween('created_at', [$start_time, $end_time])->count();
- array_unshift($countArr, $count);
- }
- return responseMessage(1001, '', ['days' => $days, 'count' => $countArr]);
- }
- // 按小时统计订单数量
- public function softHour()
- {
- $mid = Request::input('mid');
- if (empty($mid)) {
- return responseMessage(2001, '参数错误');
- }
- // 获取商品信息
- $productInfo = DB::table('product')->where('mid', $mid)->first();
- if (empty($productInfo)) {
- return responseMessage(2001, '参数错误2');
- }
- $countArr = [];
- for ($i = 0; $i < 5; $i++) {
- $day = date('Y-m-d', strtotime('-' . $i . ' day'));
- $days[] = $day;
- $count = $this->getHourData($day, $productInfo['id']);
- $countArr[] = [
- "name" => $day,
- "type" => 'line',
- // "stack" => 'Total',
- "data" => $count
- ];
- }
- $hours = [];
- for ($i = 0; $i < 24; $i++) {
- $hours[] = $i + 1;
- }
- return responseMessage(1001, '', ['days' => $days, 'hours' => $hours, 'count' => $countArr]);
- }
- private function getHourData($day, $productId)
- {
- $start_time = strtotime($day);
- $countArr = [];
- for ($i = 0; $i < 24; $i++) {
- $end_time = $start_time + 3600;
- $count = DB::table('order_product')
- ->where('product_id', $productId)
- ->where('order_status', 2)
- ->where('is_delete', 0)
- ->whereBetween('created_at', [$start_time, $end_time])->count();
- $countArr[] = $count;
- $start_time = $end_time;
- }
- return $countArr;
- }
- ##################################################################################################################
- ### 订单退款 ################################################################################################
- ##################################################################################################################
- /**
- * 确认退款
- * 注意:
- * 1、交易时间超过一年的订单无法提交退款
- * 2、微信支付退款支持单笔交易分多次退款,多次退款需要提交原支付订单的商户订单号和设置不同的退款单号。申请退款总金额不能超过订单金额。 一笔退款失败后重新提交,请不要更换退款单号,请使用原商户退款单号
- *
- * 退款有一定延时,用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。
- *
- * @return String
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- */
- public function ensureRefund()
- {
- $requestData = Request::all();
- $md5_id = $requestData['mid'] ?? '';
- $price = $requestData['price'] ?? '';
- $order_type = $requestData['order_type'] ?? '';
- if (empty($md5_id) || empty($price) || empty($order_type)) {
- return responseMessage(2001, '非法提交!');
- }
- if ($order_type == 1) { // 电商
- $orderAlias = $this->setAliasCommerceOrder;
- $aliasOrderProduct = $this->setAliasCommerceOrderProduct;
- } elseif ($order_type == 2) { // 预约
- $orderAlias = $this->setAliasTostoreOrder;
- $aliasOrderProduct = $this->setAliasTostoreOrderProduct;
- } else {
- return responseMessage(2003, '该产品不支持!');
- }
- // 获取订单信息 order_id
- $orderService = new MaterialService(new EavSetService($orderAlias));
- $orderInfo = $orderService->elasticClientService()->getInfo($refundInfo['order_id']);
- if (empty($orderInfo)) {
- return responseMessage(2005, '该订单不存在,请重试!');
- }
- if ($price > $orderInfo['order_amount_total']) {
- return responseMessage(2006, '修改的价格,必须小于或等于原价');
- }
- $payConfig = $this->getPayConfig($orderInfo['pay_channel'], $this->storeId);
- if (empty($payConfig)) {
- return $this->responseMessage(2007, '该商户还没有配置支付信息,暂时不能支付!');
- }
- //pay_type 1 微信小程序 2 微信公众号 3支付宝
- if ($orderInfo['pay_channel'] == 1) {
- $app = $this->wxGateway($payConfig, $this->storeId);
- } else {
- $app = $this->wechatGateway($payConfig, $this->storeId);
- }
- $pay_info = json_decode($orderInfo['pay_info'], true);
- $result = $app->refund->byTransactionId($pay_info['transaction_id'], $refund_no, $orderInfo['order_amount_total'] * 100, $price * 100, [
- 'refund_desc' => $refundInfo['refund_reason']
- ]);
- if ($result['return_code'] == 'SUCCESS') {
- if ($result['result_code'] == 'SUCCESS') {
- // 更新退款状态和信息
- $refundOrderService->update($refundInfo['id'], [
- 'status' => 5, // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
- 'refund_true_price' => $price,
- 'refund_info' => json_encode($result)
- ]);
- // 更新退款产品的状态
- $orderProductService = new MaterialService(new EavSetService($aliasOrderProduct));
- $refundInfo['product_id'] = intval($refundInfo['product_id']);
- if ($refundInfo['product_id']) { // 拼团的订单,该字段为空值
- $orderProductService->update($refundInfo['product_id'], [
- 'status' => 5 // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
- ]);
- }
- // 消息通知
- // dispatch(new SendMessage($this->storeId, 'REFUND_SUCCESS', ['orderNo' => $orderInfo['order_no']], $orderInfo['member_id']));
- return responseMessage(1001, '退款操作成功!');
- } else {
- return responseMessage(2011, '操作失败:' . $result['err_code'] . '-' . $result['err_code_des']);
- }
- } else {
- return responseMessage(2010, '操作失败:' . $result['return_msg']);
- }
- }
- /**
- * 通过微信接口,检测该订单的退款状态
- */
- public function checkRefundOrder()
- {
- $requestData = Request::all();
- $refund_no = $requestData['refund_no'] ?? '';
- $md5_id = $requestData['mid'] ?? '';
- $order_type = $requestData['order_type'] ?? '';
- if (empty($md5_id) || empty($refund_no) || empty($order_type)) {
- return $this->responseMessage(2001, '非法提交!');
- }
- if ($order_type == 1) { // 电商
- $refundAlias = $this->setAliasCommerceRefund;
- $orderAlias = $this->setAliasCommerceOrder;
- $aliasOrderProduct = $this->setAliasCommerceOrderProduct;
- } elseif ($order_type == 2) { // 预约
- $refundAlias = $this->setAliasTostoreRefund;
- $orderAlias = $this->setAliasTostoreOrder;
- $aliasOrderProduct = $this->setAliasTostoreOrderProduct;
- } elseif ($order_type == 3) { // 餐饮
- $refundAlias = $this->setAliasTakeoutRefund;
- $orderAlias = $this->setAliasTakeoutOrder;
- $aliasOrderProduct = $this->setAliasTakeoutOrderProduct;
- } else {
- return $this->responseMessage(2003, '该产品不支持!');
- }
- // 获取原订单信息 order_id
- $orderService = new MaterialService(new EavSetService($orderAlias));
- $orderInfo = $orderService->elasticClientService()->getInfo($refundInfo['order_id']);
- if (empty($orderInfo)) {
- return $this->responseMessage(2005, '该订单不存在,请重试!');
- }
- if ($orderInfo['status'] == 3) {
- return $this->responseMessage(2014, '退款完成');
- }
- $payConfig = $this->getPayConfig($orderInfo['pay_channel'], $this->storeId);
- if (empty($payConfig)) {
- return $this->responseMessage(2007, '该商户还没有配置支付信息,暂时不能支付!');
- }
- //pay_type 1 微信小程序 2 微信公众号 3支付宝
- if ($orderInfo['pay_channel'] == 1) {
- $app = $this->wxGateway($payConfig, $this->storeId);
- } else {
- $app = $this->wechatGateway($payConfig, $this->storeId);
- }
- $pay_info = json_decode($orderInfo['pay_info'], true);
- $result = $app->refund->queryByTransactionId($pay_info['transaction_id']);
- if ($result['return_code'] == 'SUCCESS') {
- if ($result['result_code'] == 'SUCCESS') {
- // 更新退款状态和信息
- $refundOrderService->update($refundInfo['id'], [
- 'status' => 3, // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
- ]);
- //更新总订单的状态 订单状态 1未付款2已付款3已发货4已完成5交易关闭6退款申请中7卖家退款中8退款完成订单关闭9拒绝退款
- $orderService->update($orderInfo['id'], ['order_status' => 8]);
- // 更新退款产品的状态
- $orderProductService = new MaterialService(new EavSetService($aliasOrderProduct));
- $orderProductService->update($refundInfo['product_id'], [
- 'status' => 3 // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
- ]);
- return $this->responseMessage(1001, '已退款!');
- } else {
- return $this->responseMessage(2011, '');
- }
- } else {
- return $this->responseMessage(2010, '');
- }
- }
- }
|