BusinessCardSecretUserScore.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace addons\BusinessCard\common\models;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use common\enums\StatusEnum;
  5. /**
  6. * This is the model class for table "qimall_addons_business_card_user_score".
  7. *
  8. * @property int $id
  9. * @property int $mall_id
  10. * @property int $user_id 成为客户的USERID
  11. * @property int $status 状态[1启用 0禁用 -1删除]
  12. * @property int $created_at 创建时间
  13. * @property int $updated_at 更新事件
  14. * @property int $score 分数
  15. */
  16. class BusinessCardSecretUserScore extends \common\models\base\BaseActiveRecord
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return 'qimall_addons_business_card_user_score';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['mall_id', 'user_id', 'status', 'created_at', 'updated_at', 'score'], 'integer'],
  32. ];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function attributeLabels()
  38. {
  39. return [
  40. 'id' => 'ID',
  41. 'mall_id' => 'Mall ID',
  42. 'user_id' => 'User ID',
  43. 'status' => 'Status',
  44. 'created_at' => 'Created At',
  45. 'updated_at' => 'Updated At',
  46. 'score' => 'Score',
  47. ];
  48. }
  49. /**
  50. * 分数记录
  51. * @param int $mall_id
  52. * @param int $user_id
  53. * @param int $log_type
  54. * @return void
  55. * @throws \Exception
  56. */
  57. public static function setData(int $mall_id, int $user_id, int $log_type)
  58. {
  59. $score = CardEnums::USER_SCORES[$log_type] ?? 0;
  60. if ($score <= 0) return;
  61. $model = BusinessCardSecretUserScore::find()
  62. ->where(['mall_id' => $mall_id])
  63. ->andWhere(['user_id' => $user_id])
  64. ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  65. ->one();
  66. if (empty($model)) {
  67. $model = new BusinessCardSecretUserScore();
  68. $model->mall_id = $mall_id;
  69. $model->user_id = $user_id;
  70. $model->score = 0;
  71. }
  72. $model->score = $model->score + $score;
  73. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  74. }
  75. /**
  76. * 获取用户分数
  77. * @param int $mall_id
  78. * @param int $user_id
  79. * @return false|int|string
  80. */
  81. public static function getScoreByUserId(int $mall_id, int $user_id)
  82. {
  83. return BusinessCardSecretUserScore::find()
  84. ->where(['mall_id' => $mall_id])
  85. ->andWhere(['user_id' => $user_id])
  86. ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  87. ->select('score')->scalar() ?? 0;
  88. }
  89. }