50], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'store_id' => 'Store 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', 'status' => 'Status', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } public static function setData(int $mall_id, int $coupon_id, int $store_id, int $reward_type, array $rewards): bool { if (empty($mall_id) || empty($coupon_id) || empty($reward_type) || empty($rewards) || empty($store_id)) { self::$static_error = '参数错误'; return false; } $model = self::findOne(['mall_id' => $mall_id, 'coupon_id' => $coupon_id, 'store_id' => $store_id, 'reward_type' => $reward_type]); if (empty($model)) { $model = new self(); $model->loadDefaultValues(); $model->mall_id = $mall_id; $model->store_id = $store_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; } public static function giveReward($order_id, $order_detail_ids) { try { if (!empty($order_detail_ids)) { $order = StoreOrder::findOne(['id' => $order_id, 'status' => StatusEnum::ENABLED, 'pay_status' => OrderStatusEnum::PAY, 'is_recycle' => 0]); if (empty($order)) return; // 当前订单使用的优惠券 $store_coupon_use_logs = StoreCouponUseLog::lists(['where' => [['order_id' => $order_id]]]); if (empty($store_coupon_use_logs)) return; $user_coupon_ids = array_column($store_coupon_use_logs, 'user_coupon_id'); $store_coupon_user_relate_list = StoreCouponUserRelate::lists(['where' => [['id' => $user_coupon_ids]]]); $platform_coupon_ids = $store_coupon_ids = []; $platform_coupon_arr = $store_coupon_arr = []; foreach ($store_coupon_user_relate_list as $item) { switch ($item['from_type']) { case 1: $platform_coupon_ids[] = $item['coupon_id']; $platform_coupon_arr[$item['coupon_id']] = $item['id']; break; case 2: $store_coupon_ids[] = $item['coupon_id']; $store_coupon_arr[$item['coupon_id']] = $item['id']; break; } } $reward_list = []; if (!empty($platform_coupon_ids)) { $platform_coupon_list = AddonsCoupon::find()->where(['id' => $platform_coupon_ids]) ->andWhere(['or', ['is_giving_reward' => 1], ['is_consume_reward' => 1]]) ->all(); if (!empty($platform_coupon_list)) { foreach ($platform_coupon_list as $coupon) { if (1 == $coupon['is_consume_reward']) { // 优惠券开启了使用奖励 $desc = '优惠券使用奖励'; $user_id = $order->user_id; $used_reward = AddonsCouponReward::find() ->where(['mall_id' => $order->mall_id, 'coupon_id' => $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 = '优惠券转赠奖励'; $shared_log = AddonsCouponShareLog::findOne(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['id'], 'get_user_coupon_id' => $platform_coupon_arr[$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['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; } } } } } if (!empty($store_coupon_ids)) { $store_coupon_list = StoreCoupon::find()->where(['id' => $store_coupon_ids]) ->andWhere(['or', ['is_giving_reward' => 1], ['is_consume_reward' => 1]]) ->all(); if (!empty($store_coupon_list)) { foreach ($store_coupon_list as $coupon) { if (1 == $coupon['is_consume_reward']) { // 优惠券开启了使用奖励 $desc = '优惠券使用奖励'; $user_id = $order->user_id; $used_reward = StoreCouponReward::find() ->where(['mall_id' => $order->mall_id, 'coupon_id' => $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 = '优惠券转赠奖励'; $shared_log = StoreCouponShareLog::findOne(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['id'], 'get_user_coupon_id' => $store_coupon_arr[$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 = StoreCouponReward::find() ->where(['mall_id' => $order->mall_id, 'coupon_id' => $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']); 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' => StoreOrder::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' => StoreOrder::tableName(), 'source_table_id' => $order_id, 'from_type' => $value['from_type'], 'capital_type' => $value['capital_type'], ]; } } if ($insert_wallet) { $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new \Exception(UserWalletService::getStaticError()); // 更新 qimall_addons_coupon_user_relate 表奖励状态 $rst = StoreCouponUserRelate::updateAll(['is_send_reward' => 1, 'coupon_status' => AddonsCouponEnum::STATUS_FAILURE], ['id' => $user_coupon_ids, 'is_use' => 1]); } } return true; } catch (\Exception $e) { echo 'StoreCouponReward:giveReward:' . $e->getMessage().$e->getLine().$e->getFile(); } } }