ScoreExpansionRecommendRewardOrder.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace addons\ScoreExpansion\common\models;
  3. use common\enums\StatusEnum;
  4. use Exception;
  5. use RuntimeException;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_score_expansion_recommend_reward_order".
  9. *
  10. * @property int $id
  11. * @property int $mall_id
  12. * @property int $user_id 下单User Id
  13. * @property string|null $user_ids 第一轮之后定下来的关系链
  14. * @property string|null $exclude_user_ids 第二轮开始谁不满足条件排除的user id,存在这里面的user id之后循环都不会奖励
  15. * @property int $order_id 订单ID
  16. * @property int $order_detail_id 订单详情ID
  17. * @property int $reward_period 奖励周期 0每天
  18. * @property float $frozen_score_issued_total 已发放的冻结积分
  19. * @property float $reward_frozen_score 每轮奖励多少个冻结积分
  20. * @property int $reward_person_count 奖励人数
  21. * @property int $loop_num 循环轮数
  22. * @property int $current_loop_num 当前循环到第几轮了
  23. * @property int $current_reward_person_index 本轮奖励到第几人
  24. * @property int $is_end 是否结束
  25. * @property int $last_exec_date 最后一次执行奖励时间
  26. * @property int $status 状态[-1:删除;0:禁用;1启用]
  27. * @property int $created_at
  28. * @property int $updated_at
  29. */
  30. class ScoreExpansionRecommendRewardOrder extends \common\models\base\BaseActiveRecord
  31. {
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public static function tableName()
  36. {
  37. return 'qimall_addons_score_expansion_recommend_reward_order';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['mall_id', 'user_id', 'order_id', 'order_detail_id', 'reward_period', 'reward_person_count', 'loop_num', 'current_loop_num', 'current_reward_person_index', 'is_end', 'last_exec_date', 'status', 'created_at', 'updated_at'], 'integer'],
  46. [['user_ids', 'exclude_user_ids'], 'safe'],
  47. [['frozen_score_issued_total', 'reward_frozen_score'], 'number'],
  48. [['reward_person_count', 'loop_num'], 'required'],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'mall_id' => 'Mall ID',
  59. 'user_id' => 'User ID',
  60. 'user_ids' => 'User Ids',
  61. 'exclude_user_ids' => 'Exclude User Ids',
  62. 'order_id' => 'Order ID',
  63. 'order_detail_id' => 'Order Detail ID',
  64. 'reward_period' => 'Reward Period',
  65. 'frozen_score_issued_total' => 'Frozen Score Issued Total',
  66. 'reward_frozen_score' => 'Reward Frozen Score',
  67. 'reward_person_count' => 'Reward Person Count',
  68. 'loop_num' => 'Loop Num',
  69. 'current_loop_num' => 'Current Loop Num',
  70. 'current_reward_person_index' => 'Current Reward Person Index',
  71. 'is_end' => 'Is End',
  72. 'last_exec_date' => 'Last Exec Date',
  73. 'status' => 'Status',
  74. 'created_at' => 'Created At',
  75. 'updated_at' => 'Updated At',
  76. ];
  77. }
  78. /**
  79. * @param array $params
  80. *
  81. * @return bool|self
  82. */
  83. public static function setData(array $params)
  84. {
  85. try {
  86. if (!empty($params['id'])) {
  87. $model = static::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  88. if (!$model) {
  89. throw new RuntimeException('数据不存在或已删除');
  90. }
  91. if ($model->mall_id != $params['mall_id']) {
  92. throw new RuntimeException('不允许操作此数据');
  93. }
  94. } else {
  95. $model = new static();
  96. $model->loadDefaultValues();
  97. }
  98. if (!$model->load($params, '')) {
  99. throw new RuntimeException($model->getErrorMessage());
  100. }
  101. if (!$model->save()) {
  102. throw new RuntimeException($model->getErrorMessage());
  103. }
  104. return $model;
  105. } catch (Exception $e) {
  106. static::$static_error = $e->getMessage();
  107. return false;
  108. }
  109. }
  110. public function getRecommendRewardDetails()
  111. {
  112. return $this->hasOne(ScoreExpansionRecommendRewardDetails::class, ['recommend_reward_order_id' => 'id'])
  113. ->where(['status' => StatusEnum::ENABLED]);
  114. }
  115. /**
  116. * 移动到下一个奖励人身上
  117. *
  118. * @return void
  119. */
  120. public function next()
  121. {
  122. if ($this->current_loop_num <= 1) {
  123. if (!$this->user_ids) {
  124. $this->is_end = StatusEnum::ENABLED;
  125. return;
  126. }
  127. if ($this->current_loop_num + 1 > $this->loop_num) {
  128. $this->is_end = StatusEnum::ENABLED;
  129. } else {
  130. $this->current_loop_num++;
  131. }
  132. return;
  133. }
  134. if (!isset($this->user_ids[$this->current_reward_person_index + 1])) {
  135. if ($this->current_loop_num + 1 > $this->loop_num) {
  136. $this->is_end = StatusEnum::ENABLED;
  137. } else {
  138. $this->current_loop_num++;
  139. $this->current_reward_person_index = 0;
  140. }
  141. } else {
  142. $this->current_reward_person_index++;
  143. }
  144. }
  145. }