OrderService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\admin\service\order;
  3. use app\model\Order;
  4. use app\model\OrderSheet;
  5. use app\model\SysDept;
  6. use support\Db;
  7. use support\exception\BusinessException;
  8. use support\Redis;
  9. use Yansongda\Pay\Log;
  10. use Yansongda\Pay\Pay;
  11. class OrderService
  12. {
  13. /**
  14. * @Desc 自动确认收货
  15. * @Author Gorden
  16. * @Date 2024/4/11 16:09
  17. *
  18. * @return void
  19. */
  20. public static function AutomaticReceipt()
  21. {
  22. try {
  23. Db::beginTransaction();
  24. $timeUnix = strtotime("-7 days");
  25. $orders = Order::where('order_status_system', 'SIGNED')
  26. ->where('order_addtimes', '<', $timeUnix)
  27. ->get();
  28. foreach ($orders as $order) {
  29. // 订单主表
  30. Order::where('order_id', $order->order_id)->update([
  31. 'order_is_complete' => 'Y',
  32. 'order_status_system' => 'CONFIRM',
  33. 'order_status_storage'=> 'DONE'
  34. ]);
  35. // 订单详情表
  36. OrderSheet::where('join_sheet_order_id', $order->order_id)->update(['order_sheet_status' => 'DONE']);
  37. // 7天后自动完成 order_is_complete=Y
  38. // $redis = Redis::connection();
  39. // $key = Order::AUTOMATIC_COMPLETE_PREFIX . date('Ymd', strtotime("+7 days"));
  40. // $redis->sadd($key, $order->order_id);
  41. }
  42. Db::commit();
  43. } catch (\Exception $e) {
  44. Db::rollBack();
  45. }
  46. }
  47. /**
  48. * @Desc 自动完成订单
  49. * @Author Gorden
  50. * @Date 2024/7/16 9:37
  51. *
  52. * @return void
  53. */
  54. public static function AutomaticComplete()
  55. {
  56. Db::beginTransaction();
  57. try {
  58. $redis = Redis::connection();
  59. $key = Order::AUTOMATIC_COMPLETE_PREFIX . date('Ymd');
  60. $orderIds = $redis->smembers($key);
  61. foreach ($orderIds as $orderId){
  62. $order = Order::where('order_id',$orderId)
  63. ->select('order_is_complete','order_category','order_status_system')
  64. ->first();
  65. if ($order && $order->order_is_complete != 'Y' && $order->order_category != 'RETURN' && in_array($order->order_status_system,['RECVING','SIGNED','CONFIRM'])){
  66. // 更新主表
  67. Order::where('order_id',$orderId)->update(['order_is_complete'=>'Y','order_status_system'=>'CONFIRM','order_status_storage'=>'DONE']);
  68. // sheet表
  69. OrderSheet::where('join_sheet_order_id',$orderId)->update(['order_sheet_status'=>'DONE']);
  70. }
  71. }
  72. $redis->del($key);
  73. Db::commit();
  74. } catch (\Exception $e) {
  75. dump($e->getMessage());
  76. Db::rollBack();
  77. }
  78. }
  79. public static function checkPayingOrder()
  80. {
  81. try {
  82. Db::beginTransaction();
  83. $timeUnix = strtotime("-30 minutes");
  84. $orders = Order::where('order_status_system', 'PAYING')
  85. ->where('order_category','<>','DISHES')
  86. ->where('order_addtimes', '<', $timeUnix)
  87. ->get();
  88. foreach ($orders as $order) {
  89. // 订单主表
  90. Order::where('order_id', $order->order_id)->update([
  91. 'order_is_complete' => 'Y',
  92. 'order_status_system' => 'CANCEL',
  93. 'order_status_payment' => 'CANCEL'
  94. ]);
  95. // 订单详情表
  96. OrderSheet::where('join_sheet_order_id', $order->order_id)->update(['order_sheet_status' => 'CANCEL']);
  97. }
  98. Db::commit();
  99. } catch (\Exception $e) {
  100. Db::rollBack();
  101. }
  102. }
  103. public static function generateWriteOffData($params)
  104. {
  105. return [
  106. 'charge' => [
  107. 'charge_amount' => 1,
  108. 'charge_content' => $params['order_remark'] ?? '',
  109. 'charge_user_id' => $params['write_off_member_id'],
  110. 'charge_premises' => $params['dept_premises_id'],
  111. ],
  112. 'member_id' => $params['join_order_member_id']
  113. ];
  114. }
  115. /**
  116. * 微信支付宝扫码支付
  117. */
  118. public static function qrcodePay($params)
  119. {
  120. $qrcodeNbr = $params['qrcode_nbr'];
  121. $prefix = substr($qrcodeNbr,0,2);
  122. // 微信支付
  123. if(in_array($prefix,[10,11,12,13,14,15])){
  124. $payData = [
  125. 'out_trade_no' => $params['orderGroupId'],
  126. 'body' => 'subject-测试',
  127. 'total_fee' => 0.01,//$params['order_sheet_pay']*100,
  128. 'auth_code' => $params['qrcode_nbr'],
  129. ];
  130. $result = Pay::wechat(config('payment.wxpay'))->app($payData)->getContent();
  131. dump($result);
  132. Log::info("WXPAY_RETURN",$result);
  133. }else if(in_array($prefix,[25,26,27,28,29,30])){
  134. $payData = [
  135. 'out_trade_no' => $params['orderGroupId'],
  136. 'total_amount' => 0.01,//$params['order_sheet_pay'],
  137. 'subject' => '万悦康养订单',
  138. 'auth_code' => $params['qrcode_nbr'],
  139. ];
  140. $result = Pay::alipay(config('payment.alipay'))->pos($payData);
  141. dump($result);
  142. Log::info("ALIPAY_RETURN",$result);
  143. }else{
  144. throw new BusinessException("二维码无效");
  145. }
  146. return $result;
  147. }
  148. }