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; } } }