ScoreBonusUserWeight.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace addons\ScoreBonus\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\user\User;
  5. use common\models\user\UserWallet;
  6. use addons\ScoreBonus\common\models\ScoreBonusUserWeightLog;
  7. use Exception;
  8. use Yii;
  9. /**
  10. * This is the model class for table "qimall_addons_score_bonus_capital_pool".
  11. *
  12. * @property int $id id
  13. * @property int $mall_id 商城id
  14. * @property int $user_id 用户id
  15. * @property int $weight 权重
  16. * @property float $total_income 当前分红池总额
  17. * @property int $status 状态
  18. * @property int|null $created_at 添加时间
  19. * @property int|null $updated_at 修改时间
  20. */
  21. class ScoreBonusUserWeight extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return 'qimall_addons_score_bonus_user_weight';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'weight', 'user_id'], 'required'],
  37. [['mall_id', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['total_income'], 'number'],
  39. ];
  40. }
  41. public function getUser()
  42. {
  43. return $this->hasOne(User::class, ['id' => 'user_id']);
  44. }
  45. public function getUserWallet()
  46. {
  47. return $this->hasOne(UserWallet::class, ['user_id' => 'user_id'])
  48. ->alias('uw')
  49. ->where(['<>', 'uw.status', StatusEnum::DELETE])
  50. ->select('uw.user_id,uw.score');
  51. // return $this->hasOne(UserWallet::class, ['user_id' => 'user_id'])->andWhere(['<>', 'status', StatusEnum::DELETE])->select('user_id,score');
  52. }
  53. public static function setData($params, $is_log = true)
  54. {
  55. try {
  56. if ($params['weight'] == 0) throw new \Exception("变动权重不能为0");
  57. $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
  58. if (empty($model)) {
  59. $model = new self();
  60. $model->mall_id = $params['mall_id'];
  61. $model->user_id = $params['user_id'];
  62. $model->total_income = 0;
  63. $model->weight = 0;
  64. $model->loadDefaultValues();
  65. }
  66. $before_weight = $model['weight'];
  67. $model['weight'] += $params['weight'];
  68. $after_weight = $model['weight'];
  69. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  70. // 增加变动记录
  71. if ($is_log) {
  72. $res = ScoreBonusUserWeightLog::setData([
  73. 'mall_id' => $params['mall_id'],
  74. 'user_id' => $params['user_id'],
  75. 'type' => $params['type'],
  76. 'change_weight' => abs($params['weight']),
  77. 'before_weight' => $before_weight,
  78. 'after_weight' => $after_weight,
  79. 'desc' => $params['desc'] ?? '',
  80. ]);
  81. if ($res == false) throw new \Exception(ScoreBonusUserWeightLog::getStaticError());
  82. }
  83. return true;
  84. } catch (\Exception $e) {
  85. self::setStaticError($e->getMessage());
  86. return false;
  87. }
  88. }
  89. public static function setCreateData($params)
  90. {
  91. try {
  92. $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
  93. if (empty($model)) {
  94. $model = new self();
  95. $model->mall_id = $params['mall_id'];
  96. $model->user_id = $params['user_id'];
  97. $model->total_income = 0;
  98. $model->weight = 0;
  99. $model->loadDefaultValues();
  100. }
  101. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  102. return $model;
  103. } catch (\Exception $e) {
  104. self::setStaticError($e->getMessage());
  105. return false;
  106. }
  107. }
  108. }