123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- declare (strict_types = 1);
- namespace App\Task;
- use App\Model\Ad;
- use App\Model\Order;
- use App\Model\OrderAd;
- use Hyperf\Contract\StdoutLoggerInterface;
- use Hyperf\DbConnection\Db;
- use Hyperf\Di\Annotation\Inject;
- use Psr\Log\LoggerInterface;
- class EndOrderAdminTask
- {
-
- private $logger;
- public function __construct(LoggerInterface $logger)
- {
- date_default_timezone_set('Asia/Shanghai');
- $this->logger = $logger;
- }
- public function __invoke()
- {
-
- $currentTime = date('Y-m-d H:i:s');
- $orderIds = Order::where('status', 1)
- ->where('edtime', '<=', $currentTime)
- ->pluck('id')
- ->toArray();
-
- $orderIdsTO2 = Order::where('status', 1)
- ->where('sttime', '<=', $currentTime)
- ->where('edtime', '>=', $currentTime)
- ->pluck('id')
- ->toArray();
-
-
-
-
-
-
- if ($orderIds) {
- $this->logger->info('需要处理结束单ID列表:' . implode(', ', $orderIds));
- $this->OverOder($orderIds);
- }
- if ($orderIdsTO2) {
- $this->logger->info('需要处理开始单ID列表:' . implode(', ', $orderIdsTO2));
- $this->startOder($orderIdsTO2);
- }
-
-
-
- $orders = Order::where('status', 6)
- ->get();
- $expiredOrderIds = [];
- foreach ($orders as $order) {
-
- $exploredTime = date('Y-m-d H:i:s', strtotime($order->created_at . ' +24 hours'));
- $this->logger->info('exploredTime: ' . $exploredTime . ' 当前时间' . $order->created_at);
-
- if (time() > strtotime($exploredTime)) {
- $expiredOrderIds[] = $order->id;
- }
- }
- if ($expiredOrderIds) {
- $this->logger->info('需要处理过期的订单ID列表:' . implode(', ', $expiredOrderIds));
-
-
- $this->ExpiredOrder($expiredOrderIds);
- }
- }
-
- private function singleOverOrder(array $data)
- {
- $order = Order::where('id', $data['id'])->first();
- if (empty($order)) {
- $this->logger->warning("没有找到订单ID: {$data['id']}");
- return;
- }
- Db::beginTransaction();
- try {
- $order->status = 7;
- $order->ad_status = 7;
- $order->save();
-
- $orderAds = OrderAd::where('order_id', $data['id'])->get();
- if ($orderAds->isEmpty()) {
- $this->logger->warning("没有找到订单ID: {$data['id']} 的 order_ad 记录");
- return;
- }
-
- OrderAd::where('order_id', $data['id'])
- ->update(['status' => 7]);
-
- Ad::where('order_id', $data['id'])->delete();
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- $this->logger->error("处理订单ID: {$data['id']} 时发生错误: " . $e->getMessage());
- }
- }
- private function startOder(array $data): void
- {
- Db::beginTransaction();
- try {
-
- Order::whereIn('id', $data)->update(['status' => 1, 'ad_status' => 8]);
- OrderAd::whereIn('order_id', $data)->update(['status' => 1]);
- $pid = OrderAd::whereIn('order_id', $data)->pluck('pid')->toArray();
- Ad::whereIn('pid', $pid)->update(['status' => 1]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- $this->logger->error("处理启用订单ID: {$data['id']} 时发生错误: " . $e->getMessage());
- }
- }
- private function OverOder(array $data): void
- {
- Db::beginTransaction();
- try {
-
- Order::whereIn('id', $data)->update(['status' => 7, 'ad_status' => 7]);
-
- OrderAd::whereIn('order_id', $data)->update(['status' => 7]);
-
- $pid = OrderAd::whereIn('order_id', $data)->pluck('pid')->toArray();
- Ad::whereIn('pid', $pid)->update(['status' => 2]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- $this->logger->error("处理结束订单ID: {$data['id']} 时发生错误: " . $e->getMessage());
- }
- }
- private function ExpiredOrder(array $data): void
- {
- Db::beginTransaction();try {
-
- Order::whereIn('id', $data)->update(['status' => 5, 'ad_status' => 5]);
-
- OrderAd::whereIn('order_id', $data)->update(['status' => 5]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- $this->logger->error("处理过期订单ID: {$data['id']} 时发生错误: " . $e->getMessage());
- }
- }
- }
|