| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace app\job\event;
- use think\facade\Cache;
- use app\job\model\plus\assemble\Bill as BillModel;
- use app\common\library\helper;
- /**
- * 拼团任务行为管理
- */
- class AssembleBill
- {
- private $model;
- /**
- * 执行函数
- */
- public function handle()
- {
- try {
- $this->model = new BillModel();
- $cacheKey = "task_space_assemble_bill_task";
- if (!Cache::has($cacheKey)) {
- // 将已过期的拼团任务标记为已结束
- $this->closeAssemble();
- Cache::set($cacheKey, time(), 10);
- }
- } catch (\Throwable $e) {
- echo 'ERROR AssembleBill: ' . $e->getMessage() . PHP_EOL;
- log_write('AssembleBill TASK : ' . '__ ' . $e->getMessage(), 'task');
- }
- return true;
- }
- /**
- * 到期订单未拼团成功自动关闭,并退款
- */
- private function closeAssemble()
- {
- // 获取到期未拼团成功的订单
- $billList = $this->model->getCloseIds(0);
- $billIds = helper::getArrayColumn($billList, 'assemble_bill_id');
- if (!empty($billIds)) {
- //关闭订单
- $this->model->close($billIds);
- }
- // 记录日志
- $this->dologs('closeAssemble fail', [
- 'billIds' => json_encode($billIds),
- 'error' => $this->model->getError()
- ]);
- // 获取到期未拼团成功的订单,自动成团
- $billList = $this->model->getCloseIds(1);
- $billIds = helper::getArrayColumn($billList, 'assemble_bill_id');
- if (!empty($billIds)) {
- //关闭订单
- $this->model->success($billIds);
- }
- // 退款
- $this->model->orderRefund();
- // 记录日志
- $this->dologs('closeAssemble success', [
- 'billIds' => json_encode($billIds),
- 'error' => $this->model->getError()
- ]);
- return true;
- }
- /**
- * 记录日志
- */
- private function dologs($method, $params = [])
- {
- $value = 'behavior assemble_bill Task --' . $method;
- foreach ($params as $key => $val)
- $value .= ' --' . $key . ' ' . $val;
- return log_write($value, 'task');
- }
- }
|