LevelService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace services\member;
  3. use Yii;
  4. use common\helpers\ArrayHelper;
  5. use common\components\Service;
  6. use common\enums\StatusEnum;
  7. use common\models\member\Account;
  8. use common\models\member\Level;
  9. use common\models\member\Member;
  10. use common\enums\MemberLevelUpgradeTypeEnum;
  11. use common\enums\MemberLevelAutoUpgradeTypeEnum;
  12. /**
  13. * Class LevelService
  14. * @package services\member
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class LevelService extends Service
  18. {
  19. /**
  20. * @var int $timeout 过期时间
  21. */
  22. private $timeout = 20;
  23. /**
  24. * @param Member $member
  25. * @return bool|Level|mixed|\yii\db\ActiveRecord
  26. */
  27. public function getLevelByMember(Member $member)
  28. {
  29. /** @var Account $account */
  30. $account = $member->account;
  31. return $this->getLevel(
  32. $member->current_level,
  33. $member->level_expiration_time,
  34. $account->consume_money,
  35. $account->accumulate_integral,
  36. $account->accumulate_growth
  37. );
  38. }
  39. /**
  40. * 获取用户可升等级信息
  41. *
  42. * @param int $current_level 当前级别
  43. * @param int $level_expiration_time 过期时间
  44. * @param float $money 累计消费金额
  45. * @param int $integral 累计积分
  46. * @param int $growth 累计成长值
  47. * @return Level|false|mixed|\yii\db\ActiveRecord
  48. */
  49. public function getLevel(int $current_level, int $level_expiration_time, float $money, int $integral, int $growth)
  50. {
  51. if (!($levels = $this->getLevelForCache())) {
  52. return false;
  53. }
  54. // 未开启自动升级
  55. $config = Yii::$app->services->memberLevelConfig->one(0);
  56. if ($config->auto_upgrade_type == MemberLevelAutoUpgradeTypeEnum::CLOSE) {
  57. return false;
  58. }
  59. // 开启会员才可升级且会员已过期
  60. if (
  61. $config->auto_upgrade_type == MemberLevelAutoUpgradeTypeEnum::VIP_AUTO &&
  62. $level_expiration_time < time()
  63. ) {
  64. return false;
  65. }
  66. foreach ($levels as $level) {
  67. if (!$this->getMiddle($level, $config->upgrade_type, $money, $integral, $growth)) {
  68. continue;
  69. }
  70. if ($current_level < $level->level) {
  71. return $level;
  72. }
  73. }
  74. return false;
  75. }
  76. /**
  77. * 根据商户id获取等级列表
  78. * @param int $merchant_id
  79. * @return array|Level[]|mixed|\yii\db\ActiveRecord[]
  80. */
  81. public function getLevelForCache()
  82. {
  83. $key = 'levelList';
  84. if (!($list = Yii::$app->cache->get($key))) {
  85. $list = $this->findAll();
  86. Yii::$app->cache->set($key, $list, $this->timeout);
  87. }
  88. return $list;
  89. }
  90. /**
  91. * @return array
  92. */
  93. public function getMap()
  94. {
  95. $list = ArrayHelper::arraySort($this->findAll(0), 'level');
  96. return ArrayHelper::map($list, 'level', 'name');
  97. }
  98. /**
  99. * @return array|\yii\db\ActiveRecord[]
  100. */
  101. public function findAll()
  102. {
  103. return Level::find()
  104. ->where(['status' => StatusEnum::ENABLED])
  105. ->andFilterWhere(['merchant_id' => 0])
  106. ->orderBy(['level' => SORT_DESC, 'id' => SORT_DESC])
  107. ->all();
  108. }
  109. /**
  110. * @return array|\yii\db\ActiveRecord[]
  111. */
  112. public function findAllByEdit()
  113. {
  114. return Level::find()
  115. ->where(['status' => StatusEnum::ENABLED])
  116. ->orderBy(['level' => SORT_ASC, 'id' => SORT_DESC])
  117. ->all();
  118. }
  119. /**
  120. * @param $level
  121. * @return array|\yii\db\ActiveRecord|null
  122. */
  123. public function findByLevel($level)
  124. {
  125. return Level::find()
  126. ->where(['status' => StatusEnum::ENABLED])
  127. ->andWhere(['level' => $level])
  128. ->one();
  129. }
  130. /**
  131. * @param Level $level
  132. * @param float $money 累计消费
  133. * @param int $integral 累计积分
  134. * @return array|bool|mixed
  135. */
  136. private function getMiddle(Level $level, $auto_upgrade_type, $money, $integral, $growth)
  137. {
  138. if (!$level) {
  139. return false;
  140. }
  141. switch ($auto_upgrade_type) {
  142. case MemberLevelUpgradeTypeEnum::CONSUMPTION_INTEGRAL:
  143. if (abs($integral) >= $level->integral) {
  144. return true;
  145. }
  146. break;
  147. case MemberLevelUpgradeTypeEnum::CONSUMPTION_MONEY:
  148. if (abs($money) >= $level->money) {
  149. return true;
  150. }
  151. break;
  152. case MemberLevelUpgradeTypeEnum::CONSUMPTION_GROWTH:
  153. if (abs($growth) >= $level->growth) {
  154. return true;
  155. }
  156. break;
  157. }
  158. return false;
  159. }
  160. }
粤ICP备19079148号