DistributionUpgradeCount.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace addons\Distribution\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "qimall_addons_distribution_upgrade_count".
  9. *
  10. * @property int $id
  11. * @property int|null $mall_id 商城ID
  12. * @property int|null $user_id 服务商ID
  13. * @property int|null $direct_num 好友人数
  14. * @property float|null $achievement 服务商团队业绩,单位:元
  15. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int|null $created_at 创建时间
  17. * @property int|null $updated_at 修改时间
  18. */
  19. class DistributionUpgradeCount extends BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_distribution_upgrade_count}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'user_id', 'direct_num', 'status', 'created_at', 'updated_at'], 'integer'],
  35. [['achievement'], 'number'],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'mall_id' => '商城ID',
  46. 'user_id' => '服务商ID',
  47. 'direct_num' => '好友人数',
  48. 'achievement' => '服务商团队业绩,单位:元',
  49. 'status' => '状态[-1:删除;0:禁用;1启用]',
  50. 'created_at' => '创建时间',
  51. 'updated_at' => '修改时间',
  52. ];
  53. }
  54. public static function setData($mall_id, $user_id, $direct_num = 0, $achievement = 0)
  55. {
  56. try {
  57. $model = self::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  58. if (empty($model)) {
  59. $model = new self();
  60. $model->mall_id = $mall_id;
  61. $model->user_id = $user_id;
  62. $model->loadDefaultValues();
  63. }
  64. !empty($direct_num) && $model->direct_num += $direct_num;
  65. !empty($achievement) && $model->achievement += $achievement;
  66. if (!$model->save()) throw new Exception($model->getErrorMessage());
  67. return true;
  68. } catch (\Exception $e) {
  69. self::setStaticError($e->getMessage());
  70. return false;
  71. }
  72. }
  73. }