| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace addons\OperationCenter\common\models;
- use Yii;
- use yii\db\Expression;
- /**
- * This is the model class for table "{{%addons_operation_center_user_affiliation}}".
- *
- * @property int $id
- * @property int $mall_id Mall id
- * @property int $user_id 用户ID
- * @property int $center_id 选择运营中心ID
- * @property int|null $deleted_at
- * @property int|null $updated_at
- * @property int|null $created_at
- */
- class UserAffiliation extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_operation_center_user_affiliation}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'center_id'], 'required'],
- [['mall_id', 'user_id', 'center_id', 'deleted_at', 'updated_at', 'created_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'center_id' => 'Center ID',
- 'deleted_at' => 'Deleted At',
- 'updated_at' => 'Updated At',
- 'created_at' => 'Created At',
- ];
- }
- /**
- * @return ActiveQueryInterface the relational query object.
- */
- public function getCenter()
- {
- return $this->hasOne(UserCenter::class, ['id' => 'center_id']);
- }
- /**
- * 获取用户所属
- *
- * @param integer $user_id
- * @return static|null
- */
- public static function getUserAffiliationByUserId(int $user_id)
- {
- return static::find()
- ->andWhere(['user_id' => $user_id])
- ->one();
- }
- /**
- * 获取当前选择的运营中心
- *
- * @param integer $user_id
- * @param float $lon
- * @param float $lat
- * @return array
- */
- public static function getUserAffiliationCenterByUserId(int $user_id, float $lon, float $lat)
- {
- $center_id = static::find()
- ->andWhere(['user_id' => $user_id])
- ->select('center_id')
- ->scalar();
- if (empty($center_id)) {
- return [];
- }
- return UserCenter::find()
- ->andWhere(['id' => $center_id])
- ->select([
- 'id', 'center_name', 'province', 'city',
- 'district', 'town', 'operation_address', 'logo',
- UserCenter::getDistanceSql($lon, $lat)
- ])
- ->asArray()
- ->all();
- }
- /**
- * 删除所属
- *
- * @return boolean
- */
- public function delSelect()
- {
- return $this->delete();
- }
- /**
- * 添加选择
- *
- * @param integer $mall_id
- * @param integer $user_id
- * @param integer $center_id
- * @return true|static
- */
- public static function addSelect(
- int $mall_id, int $user_id, int $center_id
- )
- {
- $model = new static;
- $model->load(compact('mall_id', 'user_id', 'center_id'), '');
- return $model->save();
- }
- }
|