下次发送短信提醒

This commit is contained in:
karl 2024-08-02 10:14:31 +08:00
parent 8330e6af81
commit 4de6ed33f5
3 changed files with 175 additions and 8 deletions

View File

@ -5,6 +5,7 @@ namespace app\command;
use app\common\Error;
use app\model\Orders as OrdersModel;
use app\server\Orders;
use app\server\Orders as ServerOrders;
use support\Redis;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
@ -33,7 +34,7 @@ class NotUsedOrderSms extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->output($output, self::$defaultName . " started");
$this->output($output, self::$defaultName . " started, " . date('Y-m-d H:i:s'));
$orders = $this->orders();
$total = count($orders);
@ -49,20 +50,36 @@ class NotUsedOrderSms extends Command
continue;
}
$result = Orders::reminderOrders($order);
$text = '已发送提醒。';
if (Error::is($result)) {
$text = '发送短信失败,' . $result['message'] ?? '';
if (ServerOrders::isDaishiyong($order)) {
$order = ServerOrders::syncFromThirdV2($order);
}
if ( $order &&
($order->os == 1 && $order->order_status != 3)
|| ($order->os == 2 && $order->order_status != 4)
|| ($order->os == 3 && $order->order_status != 1)
) {
$this->output($output, "单号 {$order->sn} 状态已修改");
continue;
}
// $result = Orders::reminderOrders($order);
$text = '已发送提醒。';
// if (Error::is($result)) {
// $text = '发送短信失败,' . $result['message'] ?? '';
// }
$this->output($output, "单号 {$order->sn} {$text}");
// $order->next_remind_time = $order->next_remind_time + 10 * 24 * 60 * 60;
// $order->save();
}
return self::SUCCESS;
}
private function output(OutputInterface $output, string $message)
private function output(OutputInterface $output, string $message)
{
$output->writeln(sprintf('{"time":"%s", "message":"%s"}', date('Y-m-d H:i:s'), $message));
}
@ -72,7 +89,12 @@ class NotUsedOrderSms extends Command
*/
private function orders(): array
{
$list = OrdersModel::where('admin_id', '>', 0)->whereRaw(OrdersModel::AllOssStatusSql[1])->select()->all();
$startTime = strtotime(date('Y-m-d H:i:00'));
$endTime = strtotime(date('Y-m-d H:i:59'));
$list = OrdersModel::where('admin_id', '>', 0)->whereRaw(OrdersModel::AllOssStatusSql[1])
->whereBetween('next_remind_time', [$startTime, $endTime])
->select()->all();
return $list;
}
}

View File

@ -0,0 +1,100 @@
<?php
namespace app\command;
use app\common\Error;
use app\model\Orders as OrdersModel;
use app\server\Orders;
use support\Redis;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
class OrderNextRemindTime extends Command
{
protected static $defaultName = 'order:next_remind';
protected static $defaultDescription = '更新下次短信提示时间';
/**
* @return void
*/
protected function configure()
{
// $this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->output($output, self::$defaultName . " started, " . date('Y-m-d H:i:s'));
$orders = $this->orders();
$total = count($orders);
$this->output($output, "got {$total} orders");
if (1 > $total) {
return self::SUCCESS;
}
foreach($orders as $order) {
if (empty($order->mobile)) {
continue;
}
$orderTime = (int)($order->create_at / 1000);
$orderTime = date('Y-m-d H:i:s', $orderTime);
$next_remind_time = $this->getNextPeriodDateTime($orderTime);
$this->output($output, "单号 {$order->sn} 下单时间:{$orderTime} 下次提醒时间:{$next_remind_time}");
$order->next_remind_time = strtotime($next_remind_time);
$order->save();
}
return self::SUCCESS;
}
private function getNextPeriodDateTime($startDateTime, $periodDays = 10) {
$startDate = new \DateTime($startDateTime);
$currentDate = new \DateTime();
$periodSeconds = $periodDays * 24 * 60 * 60;
$secondsSinceStart = $currentDate->getTimestamp() - $startDate->getTimestamp();
$completedPeriods = floor($secondsSinceStart / $periodSeconds);
if ($completedPeriods < 0) {
$completedPeriods = 0;
}
$nextPeriodTimestamp = $startDate->getTimestamp() + (($completedPeriods + 1) * $periodSeconds);
$nextPeriodDate = (new \DateTime())->setTimestamp($nextPeriodTimestamp);
return $nextPeriodDate->format('Y-m-d H:i:s');
}
private function output(OutputInterface $output, string $message)
{
$output->writeln(sprintf('{"time":"%s", "message":"%s"}', date('Y-m-d H:i:s'), $message));
}
/**
* @return OrdersModel[]
*/
private function orders(): array
{
$list = OrdersModel::where('admin_id', '>', 0)->where('next_remind_time', 0)->whereRaw(OrdersModel::AllOssStatusSql[1])->select()->all();
return $list;
}
}

View File

@ -143,4 +143,49 @@ class Orders {
return;
}
/**
* 同步第三方返回
* @param OrdersModel $order
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function syncFromThirdV2(OrdersModel $order)
{
$got = null;
switch ($order->os) {
case 1:
$mt = new Meituan();
$it = $mt->get(1, null, null, $order->sn);
if ($it) {
$got = $it[0];
}
break;
case 3:
$dy = new Douyin();
$it = $dy->get(1, null, null, $order->sn);
if ($it) {
$got = $it[0];
}
break;
}
if (is_null($got)) {
Log::info(__METHOD__ . ": get from os is null", [$order]);
return $order;
}
$back = $order->save($got);
if ($back) {
self::finance(0, $order->id, $order->asset_price);
}
return $order;
}
}