[ ['mall_id' => $mall['id']], ['status' => StatusEnum::ENABLED], ], ]; $list = ElectronCouponGiveLogs::lists($params, false); foreach ($list as $item) { $created_at = $item['created_at']; $times = $created_at + $item['share_expiry'] * 3600; //时间到,退还给赠与者 if (time() >= $times) $this->fallback($item); } } return ExitCode::OK; } catch (\Exception $e) { var_dump($e->getMessage()); \Yii::getLogger()->flush(true); return false; } } public function fallback($item) { $t = \Yii::$app->db->beginTransaction(); try { $type = $item['active_time'] == 0 ? 0 : 1; //该券是否被激活 $model = ElectronCouponUserCoupon::getOne([['e_coupon_id' => $item['e_coupon_id'], 'user_id' => $item['give_user_id'], 'id' => $item['user_e_coupon_id']]], false); if ($type == 0) { $model->receive_amount++; } else { $model->active_amount++; } $model->num++; $res = $model->save(); if ($res == false) throw new Exception($model->getErrorMessage()); $model = ElectronCouponUserCoupon::findOne(['e_coupon_id' => $item['e_coupon_id'], 'user_id' => $item['user_id'], 'id' => $item['accept_user_e_coupon_id']]); if ($type == 0) { $model->receive_amount--; if ($model->receive_amount < 0) $model->receive_amount = 0; } else { $model->active_amount--; if ($model->active_amount < 0) $model->active_amount = 0; } $model->num--; if ($model->num < 0) $model->num = 0; $res = $model->save(); if ($res == false) throw new Exception($model->getErrorMessage()); $item->status = StatusEnum::DISABLED; $item->save(); $t->commit(); return true; } catch (\Exception $e) { $t->rollBack(); return false; } } }