UpgradeGiftGoodsService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace addons\UpgradeGift\common\services;
  3. use addons\UpgradeGift\common\enums\GiftEnum;
  4. use addons\UpgradeGift\common\models\UpgradeGiftGoods;
  5. use common\enums\AddonsEnum;
  6. use Yii;
  7. use yii\helpers\Json;
  8. /**
  9. * 升级赠送商品service
  10. */
  11. class UpgradeGiftGoodsService extends BaseService
  12. {
  13. /**
  14. * @var array $config 奖品配置
  15. */
  16. public $config;
  17. /**
  18. * 获取待领取记录,返回礼品id和商品id(如果配置修改,商品id找不到了则跳过)
  19. *
  20. * @return array
  21. */
  22. public function getUnclaimedGift(): array
  23. {
  24. $defaultRes = ['is_have' => false];
  25. if (!$this->getIsEnable()) {
  26. return $defaultRes;
  27. }
  28. $unclaimedGift = UpgradeGiftGoods::getUnclaimedGift($this->user_id);
  29. if (!$unclaimedGift) {
  30. return $defaultRes;
  31. }
  32. $data = $this->getUnclaimedGiftGoodsIdByLevel(array_column($unclaimedGift, 'level'));
  33. if ($data) {
  34. $defaultRes['is_have'] = true;
  35. return array_merge($defaultRes, $data);
  36. }
  37. return $defaultRes;
  38. }
  39. /**
  40. * 根据等级返回当前要领取的商品(因为要拿最新配置的商品,所以不能数据库存储)
  41. *
  42. * @param array $levels
  43. *
  44. * @return array
  45. */
  46. public function getUnclaimedGiftGoodsIdByLevel(array $levels): array
  47. {
  48. if (!$this->getIsEnable()) {
  49. return [];
  50. }
  51. $detail = $this->getConfig();
  52. if (
  53. empty($detail) ||
  54. empty($detail['gift_type']) ||
  55. $detail['gift_type'] !== GiftEnum::GIFT_TYPE_USER ||
  56. empty($detail['setting'])
  57. ) {
  58. return [];
  59. }
  60. $detail['setting'] = array_filter($detail['setting'], function ($item) {
  61. if (
  62. !isset($item['mall_id'], $item['type'], $item['level']) ||
  63. $item['mall_id'] != $this->mall_id ||
  64. $item['type'] !== GiftEnum::SETTING_GOODS
  65. ) {
  66. return false;
  67. }
  68. return true;
  69. });
  70. if (empty($detail['setting'])) {
  71. return [];
  72. }
  73. $levelGoodsId = array_column($detail['setting'], 'id', 'level');
  74. foreach ($levels as $level) {
  75. if (isset($levelGoodsId[$level])) {
  76. return [
  77. 'level' => $level,
  78. 'goods_id' => $levelGoodsId[$level]
  79. ];
  80. }
  81. }
  82. return [];
  83. }
  84. /**
  85. * @return array
  86. */
  87. public function getConfig(): array
  88. {
  89. if (!isset($this->config)) {
  90. $detail = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_UPGRADEGIFT, GiftEnum::GROUP_NAME . "." . GiftEnum::GIFT_TYPE_USER);
  91. $this->config = empty($detail) ? [] : Json::decode($detail);
  92. }
  93. return $this->config;
  94. }
  95. /**
  96. * 用户升级奖励是否开启
  97. *
  98. * @return bool
  99. */
  100. public function getIsEnable(): bool
  101. {
  102. return $this->isInstall() && !empty($this->getConfig()['status']);
  103. }
  104. }