UserAffiliation.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace addons\OperationCenter\common\models;
  3. use Yii;
  4. use yii\db\Expression;
  5. /**
  6. * This is the model class for table "{{%addons_operation_center_user_affiliation}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id Mall id
  10. * @property int $user_id 用户ID
  11. * @property int $center_id 选择运营中心ID
  12. * @property int|null $deleted_at
  13. * @property int|null $updated_at
  14. * @property int|null $created_at
  15. */
  16. class UserAffiliation extends BaseActiveRecord
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%addons_operation_center_user_affiliation}}';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['mall_id', 'user_id', 'center_id'], 'required'],
  32. [['mall_id', 'user_id', 'center_id', 'deleted_at', 'updated_at', 'created_at'], 'integer'],
  33. ];
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function attributeLabels()
  39. {
  40. return [
  41. 'id' => 'ID',
  42. 'mall_id' => 'Mall ID',
  43. 'user_id' => 'User ID',
  44. 'center_id' => 'Center ID',
  45. 'deleted_at' => 'Deleted At',
  46. 'updated_at' => 'Updated At',
  47. 'created_at' => 'Created At',
  48. ];
  49. }
  50. /**
  51. * @return ActiveQueryInterface the relational query object.
  52. */
  53. public function getCenter()
  54. {
  55. return $this->hasOne(UserCenter::class, ['id' => 'center_id']);
  56. }
  57. /**
  58. * 获取用户所属
  59. *
  60. * @param integer $user_id
  61. * @return static|null
  62. */
  63. public static function getUserAffiliationByUserId(int $user_id)
  64. {
  65. return static::find()
  66. ->andWhere(['user_id' => $user_id])
  67. ->one();
  68. }
  69. /**
  70. * 获取当前选择的运营中心
  71. *
  72. * @param integer $user_id
  73. * @param float $lon
  74. * @param float $lat
  75. * @return array
  76. */
  77. public static function getUserAffiliationCenterByUserId(int $user_id, float $lon, float $lat)
  78. {
  79. $center_id = static::find()
  80. ->andWhere(['user_id' => $user_id])
  81. ->select('center_id')
  82. ->scalar();
  83. if (empty($center_id)) {
  84. return [];
  85. }
  86. return UserCenter::find()
  87. ->andWhere(['id' => $center_id])
  88. ->select([
  89. 'id', 'center_name', 'province', 'city',
  90. 'district', 'town', 'operation_address', 'logo',
  91. UserCenter::getDistanceSql($lon, $lat)
  92. ])
  93. ->asArray()
  94. ->all();
  95. }
  96. /**
  97. * 删除所属
  98. *
  99. * @return boolean
  100. */
  101. public function delSelect()
  102. {
  103. return $this->delete();
  104. }
  105. /**
  106. * 添加选择
  107. *
  108. * @param integer $mall_id
  109. * @param integer $user_id
  110. * @param integer $center_id
  111. * @return true|static
  112. */
  113. public static function addSelect(
  114. int $mall_id, int $user_id, int $center_id
  115. )
  116. {
  117. $model = new static;
  118. $model->load(compact('mall_id', 'user_id', 'center_id'), '');
  119. return $model->save();
  120. }
  121. }