| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- /**
- * 复购逻辑-冻结账户嗨值
- */
- namespace addons\HiGo\console\controllers;
- use addons\HiGo\common\enums\HiGoEnum;
- use addons\HiGo\common\logic\WalletLogic;
- use addons\HiGo\common\models\HigoWallet;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\helpers\LockHelper;
- use common\models\common\CapitalLog;
- use common\models\mall\Mall;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\user\User;
- use yii\console\Controller;
- use yii\console\ExitCode;
- class RepurchaseCrontabController extends Controller
- {
- /**
- * 检测会员上笔订单,超出时间,冻结账户嗨值
- * @return void
- */
- public function actionIndex()
- {
- try {
- \Yii::$app->db->close();
- $lock_key = RedisKeyEnum::suffix('lock_higo_user_hi_value');
- $identification = uniqid();
- if (!LockHelper::lock($lock_key, $identification, 60, 10)) throw new \Exception('检测会员上笔订单,超出时间,冻结账户嗨值进入处理阶段,当前访问过多');
- $mall_list = Mall::getEnableMallList(true);
- $mall_ids = array_column($mall_list, 'id');
- if (empty($mall_ids)) {
- echo '没有可用商城,不做处理 ... ' . PHP_EOL;
- return ExitCode::UNSPECIFIED_ERROR;
- }
- $redis = \Yii::$app->redis;
- foreach ($mall_ids as $mall_id) {
- if (MallAddons::isInstall($mall_id, HiGoEnum::ADDONS_NAME) == 0) continue;
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'marketing');
- if (!$setting || !$setting['new_order_enable'] || !$setting['new_order_days']) continue;
- $order_start_time = strtotime('-'.$setting['new_order_days'].' day');
- $where = [
- 'and',
- ['mall_id' => $mall_id],
- ['>', 'hi_value', 0],
- ['status' => StatusEnum::ENABLED],
- ];
- foreach (HigoWallet::find()->where($where)->each(500) as $user_wallet) {
- //查询用户上一笔订单
- $user_id = $user_wallet->user_id;
- $last_order = Order::find()->where(['user_id' => $user_id,'mall_id' => $mall_id])->andWhere(['>','pay_time',$order_start_time])->one();
- if($last_order) continue;
- //未查询到订单,冻结嗨值
- echo '这是会员ID:'.$user_id;
- //注册赠送嗨值
- $desc = '冻结会员嗨值';
- $wallet_log = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $user_wallet->hi_value,
- 'desc' => $desc,
- 'source_table' => User::tableName(),
- 'source_table_id' => $user_id,
- 'from_type' => HiGoEnum::FROM_TYPE_REPURCHASE_ACTIVE,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- 'is_freeze' => 1,
- 'from_normal_to_freeze' => 1,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- ];
- WalletLogic::handleInQueue($wallet_log);
- }
- }
- LockHelper::uLock($lock_key,$identification);
- return ExitCode::OK;
- } catch (\Exception $e) {
- $msg = '检测会员上笔订单,超出时间,冻结账户嗨值,执行结束失败任务处理出错:' . $e->getMessage() . PHP_EOL;
- echo $msg;
- \Yii::error($msg);
- \Yii::getLogger()->flush(true);
- return ExitCode::UNSPECIFIED_ERROR;
- }
- }
- }
|