| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace addons\BusinessCard\common\models;
- use addons\BusinessCard\common\enums\CardEnums;
- use common\enums\StatusEnum;
- /**
- * This is the model class for table "qimall_addons_business_card_user_score".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $user_id 成为客户的USERID
- * @property int $status 状态[1启用 0禁用 -1删除]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新事件
- * @property int $score 分数
- */
- class BusinessCardSecretUserScore extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_business_card_user_score';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'status', 'created_at', 'updated_at', 'score'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'score' => 'Score',
- ];
- }
- /**
- * 分数记录
- * @param int $mall_id
- * @param int $user_id
- * @param int $log_type
- * @return void
- * @throws \Exception
- */
- public static function setData(int $mall_id, int $user_id, int $log_type)
- {
- $score = CardEnums::USER_SCORES[$log_type] ?? 0;
- if ($score <= 0) return;
- $model = BusinessCardSecretUserScore::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->one();
- if (empty($model)) {
- $model = new BusinessCardSecretUserScore();
- $model->mall_id = $mall_id;
- $model->user_id = $user_id;
- $model->score = 0;
- }
- $model->score = $model->score + $score;
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- }
- /**
- * 获取用户分数
- * @param int $mall_id
- * @param int $user_id
- * @return false|int|string
- */
- public static function getScoreByUserId(int $mall_id, int $user_id)
- {
- return BusinessCardSecretUserScore::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->select('score')->scalar() ?? 0;
- }
- }
|