HappinessAgent.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace addons\Happiness\common\models;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\user\User;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_happiness_agent".
  9. *
  10. * @property int $id
  11. * @property int $user_id 用户ID
  12. * @property int $mall_id 商城ID
  13. * @property int $level 权重
  14. * @property int $status 状态
  15. * @property int $role 代理等级:1 省,2市,3区
  16. * @property int $district_id 区
  17. * @property int $province 省
  18. * @property int $city_id 市
  19. * @property int $parent_id 推广用户
  20. * @property string $region_name 区名称
  21. * @property int $created_at
  22. * @property int $updated_at
  23. */
  24. class HappinessAgent extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return 'qimall_addons_happiness_agent';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['user_id', 'mall_id', 'level', 'status', 'role', 'district_id', 'province_id', 'city_id', 'parent_id', 'created_at', 'updated_at'], 'integer'],
  40. [['level'], 'required'],
  41. [['region_name'], 'string', 'max' => 255],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'user_id' => 'User ID',
  52. 'mall_id' => 'Mall ID',
  53. 'level' => 'Level',
  54. 'status' => 'Status',
  55. 'role' => 'Role',
  56. 'district_id' => 'District_id ID',
  57. 'province_id' => 'Province ID',
  58. 'city_id' => 'City ID',
  59. 'parent_id' => 'Parent ID',
  60. 'region_name' => 'Region Name',
  61. 'created_at' => 'Created At',
  62. 'updated_at' => 'Updated At',
  63. ];
  64. }
  65. public static function setData($params)
  66. {
  67. try {
  68. if (!empty($params['user_id'] && !empty($params['city_id']) && !empty($params['role'])&& $params['role']==HappinessEnum::AGENT_CITY)) {
  69. $model = self::findOne(['user_id' => $params['user_id'],'role'=>HappinessEnum::AGENT_CITY,'city_id'=>$params['city_id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  70. }
  71. else if(!empty($params['user_id']) && !empty($params['district_id']) && !empty($params['role'])&& $params['role']==HappinessEnum::AGENT_DISTRICT) {
  72. $model = self::findOne(['user_id' => $params['user_id'],'role'=>HappinessEnum::AGENT_DISTRICT, 'district_id' => $params['district_id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  73. }
  74. else if(!empty($params['id']))
  75. $model = self::findOne($params['id']);
  76. else {
  77. $model = new self();
  78. $model->loadDefaultValues();
  79. }
  80. if(!$model)
  81. {
  82. $model = new self();
  83. $model->loadDefaultValues();
  84. }
  85. if (!$model->load($params, '') || !$model->save()) {
  86. throw new \Exception($model->getErrorMessage());
  87. }
  88. return $model;
  89. } catch (\Exception $e) {
  90. self::$static_error = $e->getMessage();
  91. return false;
  92. }
  93. }
  94. public function getUser()
  95. {
  96. return $this->hasOne(User::class, ['id' => 'user_id']);
  97. }
  98. public function getParent()
  99. {
  100. return $this->hasOne(User::class, ['id' => 'parent_id']);
  101. }
  102. }