50], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'coupon_id' => 'Coupon ID', 'reward_type' => 'Reward Type', 'balance' => 'Balance', 'score' => 'Score', 'red_packet' => 'Red Packet', 'reward_currency_type' => 'Reward Currency Type', 'number' => 'Number', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } /** * @desc:订单使用优惠券,赠送奖励 * @Author: hua * @Date: 2021/12/24 * @Time: 10:48 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $order_id * @param $order_detail_ids */ public static function giveReward($order_id, $order_detail_ids) { try { if (!empty($order_detail_ids)) { $order = Order::findOne(['id' => $order_id, 'status' => StatusEnum::ENABLED, 'pay_status' => OrderStatusEnum::PAY, 'is_recycle' => 0]); if (empty($order)) return; // 当前订单使用的优惠券 $used_coupons = AddonsCouponUseLog::find() ->alias('cul') ->select('ac.is_giving_reward,ac.is_consume_reward,cul.coupon_id,cul.user_coupon_id,cul.id') ->join('LEFT JOIN', AddonsCoupon::tableName() . ' as ac', 'ac.id = cul.coupon_id') ->where(['cul.order_id' => $order_id]) ->andWhere(['or', ['ac.is_giving_reward' => 1], ['ac.is_consume_reward' => 1]]) ->asArray() ->all(); if (empty($used_coupons)) return; $reward_list = []; foreach ($used_coupons as $coupon) { if (1 == $coupon['is_consume_reward']) { // 优惠券开启了使用奖励 $desc = '优惠券使用奖励'; $user_id = $order->user_id; $user_coupon = AddonsCouponUserRelate::findOne(['id' => $coupon['user_coupon_id'], 'is_use' => 1, 'is_send_reward' => 0]); if (!empty($user_coupon)) { $used_reward = AddonsCouponReward::find() ->where(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['coupon_id'], 'reward_type' => AddonsCouponEnum::REWARD_TYPE_CONSUME]) ->asArray() ->one(); $used_reward['user_id'] = $user_id; $used_reward['desc'] = $desc; $used_reward['from_type'] = UserWalletEnum::CONSUME; $used_reward['capital_type'] = UserWalletEnum::USE_COUPON_GIVE; $used_reward['reward_scenario'] = AddonsCouponEnum::REWARD_TYPE_GIVING; $used_reward['source_table_id'] = $coupon['id']; $reward_list[] = $used_reward; } } if (1 == $coupon['is_giving_reward']) { // 优惠券开启了转赠奖励 $desc = '优惠券转赠奖励'; $user_coupon = AddonsCouponUserRelate::findOne(['id' => $coupon['user_coupon_id'], 'is_giving' => 0, 'giving_status' => 0, 'is_use' => 1, 'is_send_reward' => 0]); if (!empty($user_coupon)) { $shared_log = AddonsCouponShareLog::findOne(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['coupon_id'], 'get_user_coupon_id' => $coupon['user_coupon_id'], 'receive_user_id' => $order->user_id, 'giving_status' => 2]); if (empty($shared_log)) continue; $user_id = $shared_log->giving_user_id; // 如果转出会员的 id=0 说明是平台赠送的优惠券,不发放转赠奖励 if (!empty($user_id)) { $used_reward = AddonsCouponReward::find() ->where(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['coupon_id'], 'reward_type' => AddonsCouponEnum::REWARD_TYPE_GIVING]) ->asArray() ->one(); $used_reward['user_id'] = $user_id; $used_reward['desc'] = $desc; $used_reward['from_type'] = UserWalletEnum::CONSUME; $used_reward['capital_type'] = UserWalletEnum::GIVING_COUPON_REWARD; $used_reward['reward_scenario'] = AddonsCouponEnum::REWARD_TYPE_CONSUME; $used_reward['source_table_id'] = $coupon['id']; $reward_list[] = $used_reward; } } } } $insert_wallet = []; foreach ($reward_list as $value) { $balance = floatval($value['balance']); $score = floatval($value['score']); $red_packet = floatval($value['red_packet']); $currency = floatval($value['number']); if (!empty($balance)) { $insert_wallet[] = [ 'mall_id' => $order['mall_id'], 'user_id' => $value['user_id'], 'is_frozen' => false, 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE, 'money' => $balance, 'desc' => $value['desc'] . '余额 ' . $balance . ' 元', 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => $value['from_type'], 'capital_type' => $value['capital_type'], ]; } if (!empty($score)) { $insert_wallet[] = [ 'mall_id' => $order['mall_id'], 'user_id' => $value['user_id'], 'is_frozen' => false, 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE, 'money' => $score, 'desc' => $value['desc'] . ' ' . $score . ' 积分', 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => $value['from_type'], 'capital_type' => $value['capital_type'], ]; } if (!empty($red_packet)) { if (MallAddons::isInstall($order['mall_id'], RedpackEnum::ADDONS_NAME)) { // 红包 RedpackWallet::setData([ 'mall_id' => $order['mall_id'], 'user_id' => $value['user_id'], 'is_frozen' => true, 'money' => $red_packet, 'source_table' => Order::tableName(), 'source_table_id' => $order_id, 'from_type' => RedpackEnum::FROM_USE_COUPON, 'desc' => "订单ID: {$order_id}" . $value['desc'], ]); }; } if (!empty($currency)) { // 系统虚拟货币 } } if ($insert_wallet) { $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new \Exception(UserWalletService::getStaticError()); // 更新 qimall_addons_coupon_user_relate 表奖励状态 $user_coupon_ids = array_column($used_coupons, 'user_coupon_id'); $rst = AddonsCouponUserRelate::updateAll(['is_send_reward' => 1, 'coupon_status' => AddonsCouponEnum::STATUS_FAILURE], ['id' => $user_coupon_ids, 'is_use' => 1]); } } return true; } catch (\Exception $e) { Yii::error('优惠券奖励发放失败'); Yii::error($e); echo 'AddonsCouponReward:giveReward:' . $e->getMessage(); } } /** * @name: * @msg: 保存数据 * @param {int} $mall_id * @param {int} $coupon_id * @param {int} $reward_type 奖励类型 * @param {array} $rewards 具体奖励 * @return {*} */ public static function setData(int $mall_id, int $coupon_id, int $reward_type, array $rewards): bool { if (empty($mall_id) || empty($coupon_id) || empty($reward_type) || empty($rewards)) { self::$static_error = '参数错误'; return false; } $model = self::findOne(['mall_id' => $mall_id, 'coupon_id' => $coupon_id, 'reward_type' => $reward_type]); if (empty($model)) { $model = new self(); $model->loadDefaultValues(); $model->mall_id = $mall_id; $model->coupon_id = $coupon_id; } foreach ($rewards as $key => $val) { $model->$key = $val; } $model->reward_type = $reward_type; if (!$model->save()) { self::$static_error = '保存奖励数据失败:' . $model->getErrorMessage(); return false; } return true; } }