| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace addons\ScoreBonus\common\models;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use common\models\user\UserWallet;
- use addons\ScoreBonus\common\models\ScoreBonusUserWeightLog;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_score_bonus_capital_pool".
- *
- * @property int $id id
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $weight 权重
- * @property float $total_income 当前分红池总额
- * @property int $status 状态
- * @property int|null $created_at 添加时间
- * @property int|null $updated_at 修改时间
- */
- class ScoreBonusUserWeight extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_score_bonus_user_weight';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'weight', 'user_id'], 'required'],
- [['mall_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['total_income'], 'number'],
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- public function getUserWallet()
- {
- return $this->hasOne(UserWallet::class, ['user_id' => 'user_id'])
- ->alias('uw')
- ->where(['<>', 'uw.status', StatusEnum::DELETE])
- ->select('uw.user_id,uw.score');
- // return $this->hasOne(UserWallet::class, ['user_id' => 'user_id'])->andWhere(['<>', 'status', StatusEnum::DELETE])->select('user_id,score');
- }
- public static function setData($params, $is_log = true)
- {
- try {
- if ($params['weight'] == 0) throw new \Exception("变动权重不能为0");
- $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->user_id = $params['user_id'];
- $model->total_income = 0;
- $model->weight = 0;
- $model->loadDefaultValues();
- }
- $before_weight = $model['weight'];
- $model['weight'] += $params['weight'];
- $after_weight = $model['weight'];
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- // 增加变动记录
- if ($is_log) {
- $res = ScoreBonusUserWeightLog::setData([
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'type' => $params['type'],
- 'change_weight' => abs($params['weight']),
- 'before_weight' => $before_weight,
- 'after_weight' => $after_weight,
- 'desc' => $params['desc'] ?? '',
- ]);
- if ($res == false) throw new \Exception(ScoreBonusUserWeightLog::getStaticError());
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- public static function setCreateData($params)
- {
- try {
- $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->user_id = $params['user_id'];
- $model->total_income = 0;
- $model->weight = 0;
- $model->loadDefaultValues();
- }
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|