| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace addons\ElectronCoupon\console\controllers;
- use addons\ElectronCoupon\common\models\ElectronCouponGiveLogs;
- use addons\ElectronCoupon\common\models\ElectronCouponUserCoupon;
- use common\enums\StatusEnum;
- use common\models\mall\Mall;
- use yii\console\Controller;
- use yii\console\ExitCode;
- use yii\db\Exception;
- class IndexController extends Controller
- {
- public function actionIndex()
- {
- try {
- // 获取当前系统所有可用的 mall_id
- $malls = Mall::getEnableMallList();
- foreach ($malls as $mall) {
- $params = [
- 'where' => [
- ['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;
- }
- }
- }
|