| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- <?php
- namespace addons\OperationCenter\common\models;
- use common\enums\StatusEnum;
- use common\models\mall\MallAddons;
- use Yii;
- use yii\db\Expression;
- /**
- * This is the model class for table "{{%addons_operation_center_user_center}}".
- *
- * @property int $id
- * @property int $mall_id Mall Id
- * @property int $user_id User Id
- * @property int $level 等级
- * @property float $settlement_amount 结算金额
- * @property float $settlement_amount_total 总佣金
- * @property float $discard_amount 失效佣金
- * @property string|null $center_name 运营中心名称
- * @property string|null $logo logo
- * @property int|null $province_id 省份ID
- * @property string|null $province 省份
- * @property int|null $city_id 城市ID
- * @property string|null $city 城市
- * @property int|null $district_id 县区ID
- * @property string|null $district 县区
- * @property int|null $town_id 街镇ID
- * @property string|null $town 街镇
- * @property string|null $operation_address 详细地址
- * @property int $status
- * @property float|null $lon 经度
- * @property float|null $lat 纬度
- * @property int|null $deleted_at
- * @property int|null $updated_at
- * @property int|null $created_at
- */
- class UserCenter extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_operation_center_user_center}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'level', 'province_id', 'city_id', 'district_id', 'town_id', 'status', 'deleted_at', 'updated_at', 'created_at'], 'integer'],
- [['level'], 'required'],
- [['settlement_amount', 'settlement_amount_total', 'discard_amount', 'lon', 'lat'], 'number'],
- [['center_name'], 'string', 'max' => 32],
- [['logo', 'province', 'city', 'district', 'town', 'operation_address'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'level' => 'Level',
- 'settlement_amount' => 'Settlement Amount',
- 'settlement_amount_total' => 'Settlement Amount Total',
- 'discard_amount' => 'Discard Amount',
- 'center_name' => 'Center Name',
- 'logo' => 'Logo',
- 'province_id' => 'Province ID',
- 'province' => 'Province',
- 'city_id' => 'City ID',
- 'city' => 'City',
- 'district_id' => 'District ID',
- 'district' => 'District',
- 'town_id' => 'Town ID',
- 'town' => 'Town',
- 'operation_address' => 'Operation Address',
- 'status' => 'Status',
- 'lon' => 'Lon',
- 'lat' => 'Lat',
- 'deleted_at' => 'Deleted At',
- 'updated_at' => 'Updated At',
- 'created_at' => 'Created At',
- ];
- }
- /**
- * @return ActiveQueryInterface the relational query object.
- */
- public function getUser()
- {
- return $this->hasOne(MallUser::class, ['id' => 'user_id']);
- }
- /**
- * @return ActiveQueryInterface the relational query object.
- */
- public function getLevel()
- {
- return $this->hasOne(Level::class, ['level' => 'level', 'mall_id' => 'mall_id']);
- }
- /**
- * @return ActiveQueryInterface the relational query object.
- */
- public function getLevelInfo()
- {
- return $this->hasOne(Level::class, ['level' => 'level', 'mall_id' => 'mall_id']);
- }
- /**
- * @return ActiveQueryInterface the relational query object.
- */
- public function getApply()
- {
- return $this->hasOne(ApplyList::class, ['user_id' => 'user_id'])->orderBy('id desc');
- }
- /**
- * @return ActiveQueryInterface the relational query object.
- */
- public function getApplyInfo()
- {
- return $this->hasOne(ApplyList::class, ['user_id' => 'user_id'])
- ->andWhere(['status' => 2])
- ->orderBy('id desc');
- }
- /**
- * 获取推广中心
- *
- * @param integer $user_id
- * @return static|null
- */
- public static function getCenterByUserId(int $user_id)
- {
- return static::find()
- ->andWhere(['user_id' => $user_id])
- ->one();
- }
- /**
- * 获取推广中心
- *
- * @param integer $mall_id
- * @param integer $id
- * @return static|null
- */
- public static function getCenterById(int $mall_id, int $id)
- {
- return static::find()
- ->andWhere(['mall_id' => $mall_id])
- ->andWhere(['id' => $id])
- ->one();
- }
- /**
- * 查询距离
- *
- * @param float $lon
- * @param float $lat
- * @return Expression
- */
- public static function getDistanceSql(float $lon, float $lat)
- {
- $sql = <<<SQL
- ROUND(
- 6378.138 * 2 * ASIN(
- SQRT(
- POW(SIN(($lat * PI() / 180 - lat * PI() / 180 ) / 2), 2) +
- COS($lat * PI() / 180) * COS(lat * PI() / 180) *
- POW(SIN(($lon * PI() / 180 - lon * PI() / 180) / 2), 2)
- )
- ), 3
- ) AS distance
- SQL;
- return new Expression($sql);
- }
- /**
- * 获取地址
- *
- * @return string
- */
- public function getOperationAddress()
- {
- return $this->province
- . $this->city
- . $this->district
- . $this->town
- . $this->operation_address;
- }
- /**
- * 添加运营中心
- *
- * @param ApplyList $apply
- * @param Level $level
- * @return boolean
- */
- public static function addUserByApply(
- ApplyList $apply, Level $level, float $lon, float $lat
- )
- {
- $data['mall_id'] = $apply->mall_id;
- $data['user_id'] = $apply->user_id;
- $data['level'] = $level->level;
- $data['center_name'] = $apply->center_name;
- $data['logo'] = $apply->logo;
- $data['province_id'] = $apply->province_id;
- $data['province'] = $apply->province;
- $data['city_id'] = $apply->city_id;
- $data['city'] = $apply->city;
- $data['district_id'] = $apply->district_id;
- $data['district'] = $apply->district;
- $data['town_id'] = $apply->town_id;
- $data['town'] = $apply->town;
- $data['operation_address'] = $apply->operation_address;
- $data['status'] = $apply->status;
- $data['lon'] = $lon;
- $data['lat'] = $lat;
- $model = new static;
- $model->load($data, '');
- return $model->save();
- }
- /**
- * 添加总佣金
- *
- * @param integer $user_id
- * @param float $amount
- * @return float
- */
- public static function addSettlementAmountTotal(int $user_id, float $amount)
- {
- return static::updateAllCounters(
- ['settlement_amount_total' => $amount],
- ['user_id' => $user_id]
- );
- }
- /**
- * 添加总佣金
- *
- * @param integer $user_id
- * @param float $amount
- * @return float
- */
- public static function addSettlementAmount(int $user_id, float $amount)
- {
- return static::updateAllCounters(
- ['settlement_amount' => $amount],
- ['user_id' => $user_id]
- );
- }
- /**
- * 添加失效佣金
- *
- * @param integer $user_id
- * @param float $amount
- * @return float
- */
- public static function addDiscardAmount(int $user_id, float $amount)
- {
- return static::updateAllCounters(
- ['discard_amount' => $amount],
- ['user_id' => $user_id]
- );
- }
- /**
- * 设置等级
- *
- * @param Level $level
- * @return true|static
- */
- public function setLevel(Level $level)
- {
- $this->level = $level->level;
- // $this->upgrade_level_at = time();
- return $this->save();
- }
- /**
- * 当前是否有会员使用此等级
- *
- * @param Level $level
- * @return boolean
- */
- public static function hasLevel(Level $level)
- {
- return !empty(
- static::find()
- ->andWhere(['level' => $level->level])
- ->limit(1)
- ->select(['id'])
- ->one()
- );
- }
- /**
- * 推广中心数据显示
- * @param array $params
- * @return array
- */
- public static function getIncomeInfo(array $params)
- {
- $is_install = MallAddons::isInstall($params['mall_id'], $params['addons_name']);
- $data['total_commission'] = 0;
- $data['is_install'] = 0;
- $basic = \Yii::$app->services->setting->addons->get($params['mall_id'], $params['addons_name'], 'basic');
- $data['is_show_at_center'] = 1;
- if(isset($basic['show_center'])) $data['is_show_at_center'] = $basic['show_center'];
- if ($is_install) {
- $model = self::findOne([
- 'user_id' => $params['user_id'],
- 'status' => StatusEnum::ENABLED,
- ]);
- $data['total_commission'] = $model->settlement_amount_total ?? 0.00;// 累计总佣金
- $data['is_install'] = $is_install;
- }
- return $data;
- }
- }
|