| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace addons\UpgradeGift\common\services;
- use addons\UpgradeGift\common\enums\GiftEnum;
- use addons\UpgradeGift\common\models\UpgradeGiftGoods;
- use common\enums\AddonsEnum;
- use Yii;
- use yii\helpers\Json;
- /**
- * 升级赠送商品service
- */
- class UpgradeGiftGoodsService extends BaseService
- {
- /**
- * @var array $config 奖品配置
- */
- public $config;
- /**
- * 获取待领取记录,返回礼品id和商品id(如果配置修改,商品id找不到了则跳过)
- *
- * @return array
- */
- public function getUnclaimedGift(): array
- {
- $defaultRes = ['is_have' => false];
- if (!$this->getIsEnable()) {
- return $defaultRes;
- }
- $unclaimedGift = UpgradeGiftGoods::getUnclaimedGift($this->user_id);
- if (!$unclaimedGift) {
- return $defaultRes;
- }
- $data = $this->getUnclaimedGiftGoodsIdByLevel(array_column($unclaimedGift, 'level'));
- if ($data) {
- $defaultRes['is_have'] = true;
- return array_merge($defaultRes, $data);
- }
- return $defaultRes;
- }
- /**
- * 根据等级返回当前要领取的商品(因为要拿最新配置的商品,所以不能数据库存储)
- *
- * @param array $levels
- *
- * @return array
- */
- public function getUnclaimedGiftGoodsIdByLevel(array $levels): array
- {
- if (!$this->getIsEnable()) {
- return [];
- }
- $detail = $this->getConfig();
- if (
- empty($detail) ||
- empty($detail['gift_type']) ||
- $detail['gift_type'] !== GiftEnum::GIFT_TYPE_USER ||
- empty($detail['setting'])
- ) {
- return [];
- }
- $detail['setting'] = array_filter($detail['setting'], function ($item) {
- if (
- !isset($item['mall_id'], $item['type'], $item['level']) ||
- $item['mall_id'] != $this->mall_id ||
- $item['type'] !== GiftEnum::SETTING_GOODS
- ) {
- return false;
- }
- return true;
- });
- if (empty($detail['setting'])) {
- return [];
- }
- $levelGoodsId = array_column($detail['setting'], 'id', 'level');
- foreach ($levels as $level) {
- if (isset($levelGoodsId[$level])) {
- return [
- 'level' => $level,
- 'goods_id' => $levelGoodsId[$level]
- ];
- }
- }
- return [];
- }
- /**
- * @return array
- */
- public function getConfig(): array
- {
- if (!isset($this->config)) {
- $detail = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_UPGRADEGIFT, GiftEnum::GROUP_NAME . "." . GiftEnum::GIFT_TYPE_USER);
- $this->config = empty($detail) ? [] : Json::decode($detail);
- }
- return $this->config;
- }
- /**
- * 用户升级奖励是否开启
- *
- * @return bool
- */
- public function getIsEnable(): bool
- {
- return $this->isInstall() && !empty($this->getConfig()['status']);
- }
- }
|