public function saveCoupon($data) { if ($data['start_time'] && $data['start_use_time']) { if ($data['start_use_time'] < $data['start_time']) { throw new AdminException(400513); } } if (!in_array((int)$data['receive_type'], [1, 2, 3, 4])) { throw new AdminException(400758); } if (!in_array((int)$data['is_permanent'], [0, 1])) { throw new AdminException(400758); } if (empty($data['coupon_title'])) { throw new AdminException(400759); } if ($data['end_time'] && $data['end_use_time']) { if ($data['end_use_time'] < $data['end_time']) { throw new AdminException(400514); } } $data['start_use_time'] = strtotime((string)$data['start_use_time']); $data['end_use_time'] = strtotime((string)$data['end_use_time']); $data['start_time'] = strtotime((string)$data['start_time']); $data['end_time'] = strtotime((string)$data['end_time']); $data['title'] = $data['coupon_title']; $data['remain_count'] = $data['total_count']; if ($data['receive_type'] == 2 || $data['receive_type'] == 3) { $data['is_permanent'] = 1; $data['total_count'] = 0; } $data['add_time'] = time(); $res = $this->dao->save($data); if ($data['product_id'] !== '' && $res) { $productIds = explode(',', $data['product_id']); $couponData = []; foreach ($productIds as $product_id) { $couponData[] = ['product_id' => $product_id, 'coupon_id' => $res->id]; } /** @var StoreCouponProductServices $storeCouponProductService */ $storeCouponProductService = app()->make(StoreCouponProductServices::class); $storeCouponProductService->saveAll($couponData); } if (!$res) throw new AdminException(100022); return (int)$res->id; }解读代码
时间: 2024-04-06 15:32:49 浏览: 24
这段代码是一个PHP函数,名为saveCoupon,用于保存优惠券的数据。函数接收一个$data参数,其中包含了要保存的优惠券信息。
函数首先进行了一些参数校验,比如判断开始时间和开始使用时间的先后顺序,优惠券的领取类型,是否永久有效等。
接着,函数对时间类型的参数进行了转换,将其转化为时间戳格式,并将一些参数进行了重命名。如果优惠券的领取类型是2或3,则将永久有效设置为1,总数设置为0。
最后,函数将数据保存到数据库中,并将保存的优惠券ID返回。如果保存失败,则抛出一个AdminException异常。如果优惠券信息中包含产品ID,则将产品ID和优惠券ID存储到另一张表中。
相关问题
文件目录均写在app/school_apps/emms下
好的,以下是在 `app/school_apps/emms` 目录下进行文件操作的异步任务队列案例:
首先,你需要在 `config/queue.php` 配置文件中添加队列连接和驱动设置,如使用 Redis 驱动:
```php
return [
'default' => 'redis',
'connections' => [
'redis' => [
'type' => 'redis',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 0,
'timeout' => 0,
'persistent' => false,
'prefix' => '',
],
],
];
```
接下来,在 `app/school_apps/emms/job` 目录下创建一个名为 `FileProcessJob.php` 的队列任务类,并添加以下内容:
```php
<?php
namespace app\school_apps\emms\job;
use think\queue\Job;
class FileProcessJob
{
public function handle(Job $job, $data)
{
// 获取传递的文件路径
$filePath = $data['file_path'];
// 处理文件逻辑
// 例如,可以进行文件读写、解析等操作
// 任务执行成功后删除任务
$job->delete();
}
}
```
然后,在控制器中调用队列任务。在你想要调用异步任务的控制器方法中添加以下代码:
```php
<?php
namespace app\school_apps\emms\controller;
use app\school_apps\emms\job\FileProcessJob;
use think\facade\Queue;
class FileController
{
public function upload()
{
// 上传文件逻辑
// ...
// 获取上传的文件路径
$filePath = '/path/to/uploaded/file.txt';
// 推送任务到队列
Queue::push(new FileProcessJob(['file_path' => $filePath]));
return '任务已推送到队列';
}
}
```
最后,在 `app/school_apps/emms/command` 目录下创建一个名为 `QueueWorker.php` 的命令文件,用于启动队列消费者。添加以下内容:
```php
<?php
namespace app\school_apps\emms\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Queue;
class QueueWorker extends Command
{
protected function configure()
{
$this->setName('emms:queue:work')
->setDescription('Start EMMS queue worker');
}
protected function execute(Input $input, Output $output)
{
// 持续监听队列并执行任务
Queue::listen();
}
}
```
现在,你可以通过运行以下命令启动队列消费者:
```shell
php think emms:queue:work
```
当你调用 `FileController` 控制器的 `upload` 方法时,文件处理任务会被推送到队列中,并由消费者进行处理。
请确保目录和命名空间的设置与你的实际项目相匹配。希望对你有所帮助!
CreatePreStudentPaperTask类将学生pre_student_info和template_id推到CreatePreStudentPaperTaskJob,CreatePreStudentPaperTaskJob负责生成录取通知书
好的,以下是一个基于ThinkPHP6的异步任务队列案例,用于生成录取通知书:
首先,创建一个名为 `CreatePreStudentPaperTask.php` 的类,该类负责将学生信息和模板ID推送到队列中。在 `app\job` 目录下创建该文件,并添加以下内容:
```php
<?php
namespace app\job;
use think\queue\Job;
class CreatePreStudentPaperTask
{
public function handle($data)
{
// 将学生信息和模板ID推送到队列中
$jobData = [
'student_info' => $data['student_info'],
'template_id' => $data['template_id'],
];
// 推送任务到队列
queue('CreatePreStudentPaperTaskJob', $jobData);
}
}
```
接下来,创建一个名为 `CreatePreStudentPaperTaskJob.php` 的类,该类负责生成录取通知书。在 `app\job` 目录下创建该文件,并添加以下内容:
```php
<?php
namespace app\job;
use think\queue\Job;
class CreatePreStudentPaperTaskJob
{
public function handle(Job $job, $data)
{
// 获取学生信息和模板ID
$studentInfo = $data['student_info'];
$templateId = $data['template_id'];
// 生成录取通知书的逻辑
// 这里可以执行生成通知书的操作
// 任务执行成功后删除任务
$job->delete();
}
}
```
然后,在控制器中调用队列任务。在你想要调用异步任务的控制器方法中添加以下代码:
```php
<?php
namespace app\controller;
use app\job\CreatePreStudentPaperTask;
use think\facade\Queue;
class CreatePreStudentPaperController
{
public function create()
{
// 学生信息和模板ID
$studentInfo = ['name' => '张三', 'age' => 18];
$templateId = 1;
// 创建预生成录取通知书任务并推送到队列
Queue::push(new CreatePreStudentPaperTask(), ['student_info' => $studentInfo, 'template_id' => $templateId]);
return '预生成录取通知书任务已推送到队列';
}
}
```
最后,你需要在 `app\command` 目录下创建一个名为 `QueueWorker.php` 的命令文件,用于启动队列消费者。添加以下内容:
```php
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Queue;
class QueueWorker extends Command
{
protected function configure()
{
$this->setName('queue:work')
->setDescription('Start queue worker');
}
protected function execute(Input $input, Output $output)
{
// 持续监听队列并执行任务
Queue::listen();
}
}
```
现在,你可以通过运行以下命令启动队列消费者:
```shell
php think queue:work
```
当你调用 `CreatePreStudentPaperController` 控制器的 `create` 方法时,生成录取通知书的任务会被推送到队列中,并由消费者进行处理。
请根据实际需求进行修改和扩展。希望对你有所帮助!