OrderController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. namespace App\Http\Admin;
  3. use Illuminate\Support\Facades\Request;
  4. use Illuminate\Support\Facades\DB;
  5. class OrderController extends AdminBaseController
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. $this->setWebsite(__NAMESPACE__);
  11. }
  12. public function analysis()
  13. {
  14. $yesterday_time = strtotime(date('Y-m-d', strtotime("-1 day")));
  15. $today_time = strtotime(date('Y-m-d'));
  16. // 昨天订单总数
  17. $yesterdayOrderTotal = $this->totalOrderNum($yesterday_time);
  18. // 今天订单总数
  19. $todayOrderTotal = $this->totalOrderNum($today_time);
  20. ########################################################################################
  21. // 昨天用户量
  22. $yesterdayMemberTotal = $this->totalMember($yesterday_time);
  23. // 今天用户量
  24. $todayMemberTotal = $this->totalMember($today_time);
  25. ########################################################################################
  26. // 昨天支付金额
  27. $yesterdayMoneyTotal = $this->totalMoneyNum($yesterday_time);
  28. // 今天支付金额
  29. $todayMoneyTotal = $this->totalMoneyNum($today_time);
  30. ########################################################################################
  31. $monthFirstDay = strtotime(date("Y-m-1 00:00:00"));
  32. $monthLastDay = strtotime(date("Y-m-" . date("t") . " 23:59:59"));
  33. // 本月的订单总数
  34. $monthOrderTotal = $this->totalOrderNum($monthFirstDay, $monthLastDay);
  35. // 本月的交易金额
  36. $monthMoneyTotal = $this->totalMoneyNum($monthFirstDay, $monthLastDay);
  37. ########################################################################################
  38. // 获取上个月第一天的时间戳
  39. $firstDayOfLastMonth = strtotime("first day of last month", $monthFirstDay);
  40. $lastDayOfLastMonth = strtotime("last day of last month", $monthLastDay);
  41. // 上月的订单总数
  42. $lastMonthOrderTotal = $this->totalOrderNum($firstDayOfLastMonth, $lastDayOfLastMonth);
  43. // 上月的交易金额
  44. $lastMonthMoneyTotal = $this->totalMoneyNum($firstDayOfLastMonth, $lastDayOfLastMonth);
  45. ########################################################################################
  46. // 总收入数
  47. $todayMoneyAll = $this->totalMoneyNum();
  48. // 总用户数
  49. $todayMemberAll = $this->totalMember();
  50. return responseMessage(1001, '', [
  51. 'yesterdayOrderTotal' => round($yesterdayOrderTotal, 2),
  52. 'todayOrderTotal' => round($todayOrderTotal, 2),
  53. 'yesterdayMemberTotal' => round($yesterdayMemberTotal, 2),
  54. 'todayMemberTotal' => round($todayMemberTotal, 2),
  55. 'yesterdayMoneyTotal' => round($yesterdayMoneyTotal, 2),
  56. 'todayMoneyTotal' => round($todayMoneyTotal, 2),
  57. 'monthOrderTotal' => round($monthOrderTotal, 2),
  58. 'monthMoneyTotal' => round($monthMoneyTotal, 2),
  59. 'lastMonthOrderTotal' => round($lastMonthOrderTotal, 2),
  60. 'lastMonthMoneyTotal' => round($lastMonthMoneyTotal, 2),
  61. 'todayMoneyAll' => round($todayMoneyAll, 2),
  62. 'todayMemberAll' => round($todayMemberAll, 2),
  63. ]);
  64. }
  65. /**
  66. * 按年统计
  67. *
  68. * @return array
  69. */
  70. public function statisByYear($year)
  71. {
  72. $arr = [];
  73. for ($m = 1; $m <= 12; $m++) {
  74. $monthFirstDay = strtotime(date($year . "-" . $m . "-1 00:00:00"));
  75. $monthLastDay = strtotime(date($year . "-" . $m . "-" . date('t',strtotime($year . "-" . $m)) . " 23:59:59"));
  76. $arr[] = $this->totalMoneyNum($monthFirstDay, $monthLastDay);
  77. }
  78. return $arr;
  79. }
  80. /**
  81. * 最近7天的订单量 todo 优化查询方式
  82. *
  83. */
  84. public function sevenDayOrder()
  85. {
  86. $days = [];
  87. $count = [];
  88. $total = [];
  89. for ($i = 0; $i < 7; $i++) {
  90. $day = date('Y-m-d', strtotime('-' . $i . ' day'));
  91. array_unshift($days, $day);
  92. array_unshift($count, $this->totalOrderNum(strtotime($day)));
  93. array_unshift($total, $this->totalMoneyNum(strtotime($day)));
  94. }
  95. return responseMessage(1001, '', ['days' => $days, 'count' => $count, 'total' => $total]);
  96. }
  97. /**
  98. * 获取支付金额
  99. *
  100. * @param int $start_time
  101. * @param int $end_time
  102. * @return mixed
  103. */
  104. public function totalMoneyNum(int $start_time = 0, int $end_time = 0): mixed
  105. {
  106. if (empty($end_time)) {
  107. $end_time = $start_time + 24 * 3600;
  108. }
  109. ########## 软件
  110. $data1 = $this->getOrderSum('order_product', $start_time, $end_time);
  111. return $data1;
  112. }
  113. /**
  114. * 注册人数
  115. */
  116. private function totalMember(int $start_time = 0, int $end_time = 0)
  117. {
  118. if (empty($start_time)) {
  119. return DB::table('user')->count();
  120. } else {
  121. if (empty($end_time)) {
  122. $end_time = $start_time + 24 * 3600;
  123. }
  124. return DB::table('user')->where('is_delete', 0)->whereBetween('created_at', [$start_time, $end_time])->count();
  125. }
  126. }
  127. // 所有的订单数量
  128. private function totalOrderNum($start_time, int $end_time = 0)
  129. {
  130. ########## 软件
  131. $data1 = $this->getOrderNum('order_product', $start_time, $end_time);
  132. return $data1;
  133. }
  134. /**
  135. * @param string $table
  136. * @param int $start_time
  137. * @param int $end_time
  138. * @return array|int
  139. */
  140. private function getOrderNum(string $table, int $start_time = 0, int $end_time = 0): int|array
  141. {
  142. if (empty($start_time)) {
  143. return DB::table($table)->count();
  144. } else {
  145. if (empty($end_time)) {
  146. $end_time = $start_time + 24 * 3600;
  147. }
  148. return DB::table($table)->where('order_status', 2)->where('is_delete', 0)->whereBetween('created_at', [ $start_time, $end_time])->count();
  149. }
  150. }
  151. /**
  152. * @param string $table
  153. * @param int $start_time
  154. * @param int $end_time
  155. * @return mixed
  156. */
  157. private function getOrderSum(string $table, int $start_time, int $end_time = 0): mixed
  158. {
  159. if (empty($start_time)) {
  160. return DB::table($table)->where('order_status', 2)->where('is_delete', 0)->sum('order_amount_total');
  161. } else {
  162. if (empty($end_time)) {
  163. $end_time = $start_time + 24 * 3600;
  164. }
  165. return DB::table($table)->where('order_status', 2)->where('is_delete', 0)->whereBetween('created_at', [$start_time, $end_time])->sum('order_amount_total');
  166. }
  167. }
  168. public function softOrder()
  169. {
  170. $mid = Request::input('mid');
  171. if (empty($mid)) {
  172. return responseMessage(2001, '参数错误');
  173. }
  174. // 获取商品信息
  175. $productInfo = DB::table('product')->where('mid', $mid)->first();
  176. if (empty($productInfo)) {
  177. return responseMessage(2001, '参数错误2');
  178. }
  179. $days = [];
  180. $countArr = [];
  181. for ($i = 0; $i < 15; $i++) {
  182. $day = date('Y-m-d', strtotime('-' . $i . ' day'));
  183. array_unshift($days, $day);
  184. $start_time = strtotime($day);
  185. $end_time = $start_time + 24 * 3600;
  186. $count = DB::table('order_product')
  187. ->where('product_id', $productInfo['id'])
  188. ->where('order_status', 2)
  189. ->where('is_delete', 0)
  190. ->whereBetween('created_at', [$start_time, $end_time])->count();
  191. array_unshift($countArr, $count);
  192. }
  193. return responseMessage(1001, '', ['days' => $days, 'count' => $countArr]);
  194. }
  195. // 按小时统计订单数量
  196. public function softHour()
  197. {
  198. $mid = Request::input('mid');
  199. if (empty($mid)) {
  200. return responseMessage(2001, '参数错误');
  201. }
  202. // 获取商品信息
  203. $productInfo = DB::table('product')->where('mid', $mid)->first();
  204. if (empty($productInfo)) {
  205. return responseMessage(2001, '参数错误2');
  206. }
  207. $countArr = [];
  208. for ($i = 0; $i < 5; $i++) {
  209. $day = date('Y-m-d', strtotime('-' . $i . ' day'));
  210. $days[] = $day;
  211. $count = $this->getHourData($day, $productInfo['id']);
  212. $countArr[] = [
  213. "name" => $day,
  214. "type" => 'line',
  215. // "stack" => 'Total',
  216. "data" => $count
  217. ];
  218. }
  219. $hours = [];
  220. for ($i = 0; $i < 24; $i++) {
  221. $hours[] = $i + 1;
  222. }
  223. return responseMessage(1001, '', ['days' => $days, 'hours' => $hours, 'count' => $countArr]);
  224. }
  225. private function getHourData($day, $productId)
  226. {
  227. $start_time = strtotime($day);
  228. $countArr = [];
  229. for ($i = 0; $i < 24; $i++) {
  230. $end_time = $start_time + 3600;
  231. $count = DB::table('order_product')
  232. ->where('product_id', $productId)
  233. ->where('order_status', 2)
  234. ->where('is_delete', 0)
  235. ->whereBetween('created_at', [$start_time, $end_time])->count();
  236. $countArr[] = $count;
  237. $start_time = $end_time;
  238. }
  239. return $countArr;
  240. }
  241. ##################################################################################################################
  242. ### 订单退款 ################################################################################################
  243. ##################################################################################################################
  244. /**
  245. * 确认退款
  246. * 注意:
  247. * 1、交易时间超过一年的订单无法提交退款
  248. * 2、微信支付退款支持单笔交易分多次退款,多次退款需要提交原支付订单的商户订单号和设置不同的退款单号。申请退款总金额不能超过订单金额。 一笔退款失败后重新提交,请不要更换退款单号,请使用原商户退款单号
  249. *
  250. * 退款有一定延时,用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。
  251. *
  252. * @return String
  253. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  254. */
  255. public function ensureRefund()
  256. {
  257. $requestData = Request::all();
  258. $md5_id = $requestData['mid'] ?? '';
  259. $price = $requestData['price'] ?? '';
  260. $order_type = $requestData['order_type'] ?? '';
  261. if (empty($md5_id) || empty($price) || empty($order_type)) {
  262. return responseMessage(2001, '非法提交!');
  263. }
  264. if ($order_type == 1) { // 电商
  265. $orderAlias = $this->setAliasCommerceOrder;
  266. $aliasOrderProduct = $this->setAliasCommerceOrderProduct;
  267. } elseif ($order_type == 2) { // 预约
  268. $orderAlias = $this->setAliasTostoreOrder;
  269. $aliasOrderProduct = $this->setAliasTostoreOrderProduct;
  270. } else {
  271. return responseMessage(2003, '该产品不支持!');
  272. }
  273. // 获取订单信息 order_id
  274. $orderService = new MaterialService(new EavSetService($orderAlias));
  275. $orderInfo = $orderService->elasticClientService()->getInfo($refundInfo['order_id']);
  276. if (empty($orderInfo)) {
  277. return responseMessage(2005, '该订单不存在,请重试!');
  278. }
  279. if ($price > $orderInfo['order_amount_total']) {
  280. return responseMessage(2006, '修改的价格,必须小于或等于原价');
  281. }
  282. $payConfig = $this->getPayConfig($orderInfo['pay_channel'], $this->storeId);
  283. if (empty($payConfig)) {
  284. return $this->responseMessage(2007, '该商户还没有配置支付信息,暂时不能支付!');
  285. }
  286. //pay_type 1 微信小程序 2 微信公众号 3支付宝
  287. if ($orderInfo['pay_channel'] == 1) {
  288. $app = $this->wxGateway($payConfig, $this->storeId);
  289. } else {
  290. $app = $this->wechatGateway($payConfig, $this->storeId);
  291. }
  292. $pay_info = json_decode($orderInfo['pay_info'], true);
  293. $result = $app->refund->byTransactionId($pay_info['transaction_id'], $refund_no, $orderInfo['order_amount_total'] * 100, $price * 100, [
  294. 'refund_desc' => $refundInfo['refund_reason']
  295. ]);
  296. if ($result['return_code'] == 'SUCCESS') {
  297. if ($result['result_code'] == 'SUCCESS') {
  298. // 更新退款状态和信息
  299. $refundOrderService->update($refundInfo['id'], [
  300. 'status' => 5, // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
  301. 'refund_true_price' => $price,
  302. 'refund_info' => json_encode($result)
  303. ]);
  304. // 更新退款产品的状态
  305. $orderProductService = new MaterialService(new EavSetService($aliasOrderProduct));
  306. $refundInfo['product_id'] = intval($refundInfo['product_id']);
  307. if ($refundInfo['product_id']) { // 拼团的订单,该字段为空值
  308. $orderProductService->update($refundInfo['product_id'], [
  309. 'status' => 5 // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
  310. ]);
  311. }
  312. // 消息通知
  313. // dispatch(new SendMessage($this->storeId, 'REFUND_SUCCESS', ['orderNo' => $orderInfo['order_no']], $orderInfo['member_id']));
  314. return responseMessage(1001, '退款操作成功!');
  315. } else {
  316. return responseMessage(2011, '操作失败:' . $result['err_code'] . '-' . $result['err_code_des']);
  317. }
  318. } else {
  319. return responseMessage(2010, '操作失败:' . $result['return_msg']);
  320. }
  321. }
  322. /**
  323. * 通过微信接口,检测该订单的退款状态
  324. */
  325. public function checkRefundOrder()
  326. {
  327. $requestData = Request::all();
  328. $refund_no = $requestData['refund_no'] ?? '';
  329. $md5_id = $requestData['mid'] ?? '';
  330. $order_type = $requestData['order_type'] ?? '';
  331. if (empty($md5_id) || empty($refund_no) || empty($order_type)) {
  332. return $this->responseMessage(2001, '非法提交!');
  333. }
  334. if ($order_type == 1) { // 电商
  335. $refundAlias = $this->setAliasCommerceRefund;
  336. $orderAlias = $this->setAliasCommerceOrder;
  337. $aliasOrderProduct = $this->setAliasCommerceOrderProduct;
  338. } elseif ($order_type == 2) { // 预约
  339. $refundAlias = $this->setAliasTostoreRefund;
  340. $orderAlias = $this->setAliasTostoreOrder;
  341. $aliasOrderProduct = $this->setAliasTostoreOrderProduct;
  342. } elseif ($order_type == 3) { // 餐饮
  343. $refundAlias = $this->setAliasTakeoutRefund;
  344. $orderAlias = $this->setAliasTakeoutOrder;
  345. $aliasOrderProduct = $this->setAliasTakeoutOrderProduct;
  346. } else {
  347. return $this->responseMessage(2003, '该产品不支持!');
  348. }
  349. // 获取原订单信息 order_id
  350. $orderService = new MaterialService(new EavSetService($orderAlias));
  351. $orderInfo = $orderService->elasticClientService()->getInfo($refundInfo['order_id']);
  352. if (empty($orderInfo)) {
  353. return $this->responseMessage(2005, '该订单不存在,请重试!');
  354. }
  355. if ($orderInfo['status'] == 3) {
  356. return $this->responseMessage(2014, '退款完成');
  357. }
  358. $payConfig = $this->getPayConfig($orderInfo['pay_channel'], $this->storeId);
  359. if (empty($payConfig)) {
  360. return $this->responseMessage(2007, '该商户还没有配置支付信息,暂时不能支付!');
  361. }
  362. //pay_type 1 微信小程序 2 微信公众号 3支付宝
  363. if ($orderInfo['pay_channel'] == 1) {
  364. $app = $this->wxGateway($payConfig, $this->storeId);
  365. } else {
  366. $app = $this->wechatGateway($payConfig, $this->storeId);
  367. }
  368. $pay_info = json_decode($orderInfo['pay_info'], true);
  369. $result = $app->refund->queryByTransactionId($pay_info['transaction_id']);
  370. if ($result['return_code'] == 'SUCCESS') {
  371. if ($result['result_code'] == 'SUCCESS') {
  372. // 更新退款状态和信息
  373. $refundOrderService->update($refundInfo['id'], [
  374. 'status' => 3, // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
  375. ]);
  376. //更新总订单的状态 订单状态 1未付款2已付款3已发货4已完成5交易关闭6退款申请中7卖家退款中8退款完成订单关闭9拒绝退款
  377. $orderService->update($orderInfo['id'], ['order_status' => 8]);
  378. // 更新退款产品的状态
  379. $orderProductService = new MaterialService(new EavSetService($aliasOrderProduct));
  380. $orderProductService->update($refundInfo['product_id'], [
  381. 'status' => 3 // 状态 1退款申请中 2拒绝退款 3退款完成 4取消退款 5 退款中
  382. ]);
  383. return $this->responseMessage(1001, '已退款!');
  384. } else {
  385. return $this->responseMessage(2011, '');
  386. }
  387. } else {
  388. return $this->responseMessage(2010, '');
  389. }
  390. }
  391. }