| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- /**
- * 平台订单 定时任务
- */
- namespace console\controllers;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\models\platform\PlatformOrder;
- use common\models\platform\PlatformOrderStatisticsDay;
- use yii\console\Controller;
- use yii\console\ExitCode;
- class PlatformOrderCrontabController extends Controller
- {
- /**
- * 取消订单
- * 0 20 0 * * * *
- * @Author: lun
- * @DateTime: 2021/11/17 0017 20:38
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return int
- */
- public function actionCancelOrder(){
- try {
- $time = time();
- $where[] = ['=', 'order_status', StatusEnum::DISABLED];
- $where[] = ['<=', 'expire_time', $time];
- $select = "id,order_id,pay_money,source_table";
- $params = ['where' => $where, 'select' => $select];
- // 查询已过期的订单列表
- $list = PlatformOrder::lists($params);
- $tables = $poids = [];
- $total_pay_money = 0;// 取消的总订单金额
- $total_order_num = count($list);// 取消的总订单总数
- // 获取不同表需要更新的订单id
- foreach ($list as $item) {
- $tables[$item['source_table']][] = $item['order_id'];
- $poids[] = $item['id'];
- $total_pay_money += $item['pay_money'];
- }
- if (!empty($tables)) {
- $trans = \Yii::$app->db->beginTransaction();
- try {
- // 修改平台订单的订单状态
- \Yii::$app->db->createCommand()->update(PlatformOrder::tableName(), ['order_status' => (OrderStatusEnum::REPEAL)], 'id in('. implode(',', $poids) .')')->execute();
- // 循环订单数据
- foreach ($tables as $table => $oids) {
- // 批量修改过期订单
- \Yii::$app->db->createCommand()->update($table, [
- 'order_status' => OrderStatusEnum::REPEAL,
- 'close_time' => $time,
- ], 'id in('. implode(',', $oids) .')')->execute();
- }
- // 修改订单统计
- $res = PlatformOrderStatisticsDay::setData([
- 'cancel_order_num' => $total_order_num,
- 'cancel_order_money' => $total_pay_money,
- 'cancel_goods_num' => $total_order_num,
- ]);
- if ($res == false) throw new \Exception(PlatformOrderStatisticsDay::getStaticError());
- $trans->commit();
- } catch (\Exception $e) {
- $trans->rollBack();
- throw new \Exception($e->getMessage());
- }
- }
- echo '操作成功';
- return ExitCode::OK;
- } catch (\Exception $e) {
- echo $e->getMessage();
- $this->stderr('平台订单Error: ' . ExitCode::getReason(ExitCode::UNSPECIFIED_ERROR));
- return ExitCode::UNSPECIFIED_ERROR;
- }
- }
- }
|