| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- namespace addons\DoubleCopy\common\models;
- use addons\DoubleCopy\common\enums\DoubleCopyEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\models\common\CapitalLog;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use common\models\user\UserPartitionRelationship;
- use services\common\UserWalletService;
- use Yii;
- use yii\db\Exception;
- use yii\db\Expression;
- /**
- * This is the model class for table "{{%addons_double_copy_user}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $user_id 用户ID
- * @property int $level 等级
- * @property float $commission 累计佣金,单位:元
- * @property float $frozen_commission 累计冻结佣金,单位:元
- * @property float $frozen_fund 冻结基金,单位:元
- * @property int $help_num 助力用户数量
- * @property int $team_num 团队数量(代理+老板)
- * @property int $pid 上级uid
- * @property int $topid 上级老板id
- * @property int $pos 相对上级的位置
- * @property int $upgrade_status 1条件升级,2购买指定商品升级,3手动升级
- * @property int $upgrade_level_at 股东等级升级时间
- * @property int|null $is_thaw 解冻状态[0:否,1:全部解冻,2:部分解冻]
- * @property float $thaw_percent 上次解冻比例,单位:百分比
- * @property float $achievement 业绩,单位:元
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property float $score 已结算积分
- * @property float $frozen_score 未结算积分
- * @property float $frozen_fund_score 冻结基金积分
- * @property int $is_virtual 是否虚拟用户
- * @property int $upgrade_boss_at 成为老板以上等级的时间
- */
- class DoubleCopyUser extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_double_copy_user}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'level', 'pid', 'topid', 'pos', 'help_num', 'team_num', 'upgrade_status', 'upgrade_level_at', 'is_thaw', 'status', 'created_at', 'updated_at','is_height_commission', 'is_virtual', 'upgrade_boss_at'], 'integer'],
- [['commission', 'frozen_commission', 'frozen_fund', 'achievement', 'thaw_percent','height_commission'], 'number'],
- [['score', 'frozen_score', 'frozen_fund_score',], 'number'],
- [['goods_info',], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_id' => '用户ID',
- 'level' => '等级',
- 'commission' => '累计佣金,单位:元',
- 'frozen_commission' => '累计冻结佣金,单位:元',
- 'frozen_fund' => '冻结基金,单位:元',
- 'pid' => '上级uid',
- 'topid' => '上级老板id',
- 'pos' => '相对上级的位置',
- 'help_num' => '助力用户数量',
- 'team_num' => '团队数量(代理+老板)',
- 'upgrade_status' => '1条件升级,2购买指定商品升级,3手动升级',
- 'upgrade_level_at' => '股东等级升级时间',
- 'is_thaw' => '解冻状态[0:否,1:全部解冻,2:部分解冻]',
- 'thaw_percent' => '上次解冻比例,单位:百分比',
- 'achievement' => '业绩,单位:元',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- 'goods_info' => '商品信息',
- 'is_virtual' => '是否虚拟用户',
- 'upgrade_boss_at' => '成为老板以上等级的时间'
- ];
- }
- /**
- * 获取用户
- * @Author: lun
- * @DateTime: 2022/5/14 0014 15:31
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,mobile,avatar_url,parent_id');
- }
- /**
- * 获取用户
- * @Author: lun
- * @DateTime: 2022/5/14 0014 15:31
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getPuser()
- {
- return $this->hasOne(User::class, ['id' => 'pid'])->select('id,nickname,mobile,avatar_url,parent_id');
- }
- /**
- * 获取用户
- * @Author: lun
- * @DateTime: 2022/5/14 0014 15:31
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \yii\db\ActiveQuery
- */
- public function getTopuser()
- {
- return $this->hasOne(User::class, ['id' => 'topid'])->select('id,nickname,mobile,avatar_url,parent_id');
- }
- public function getMarket()
- {
- return $this->hasMany(self::class, ['pid' => 'user_id'])->where(['status' => StatusEnum::ENABLED])->andWhere(['<=', 'pos', 2])->select('user_id,pid,achievement,is_virtual')->orderBy('pos asc');
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2022/5/14 0014 16:49
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return array|bool
- */
- public static function setData(array $params){
- try{
- if(!empty($params['id'])){
- $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
- if(empty($model)) throw new \Exception('等级不存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $params['mall_id'];
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- return $model->toArray();
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 批量查询
- * @Author: lun
- * @DateTime: 2022/5/18 0018 11:11
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $offset
- * @param $limit
- * @param $tree
- * @param array $params
- * @return bool|mixed
- */
- public static function batchGetTeamData($offset, $limit, $tree, $params = [])
- {
- $params['where'][] = ['like', 'tree', "{$tree},%", false];
- $params['offset'] = ($offset - 1) * ($limit - 1);
- $params['limit'] = $limit;
- return UserPartitionRelationship::lists($params);
- }
- /**
- * 更新用户资金
- * @Author: lun
- * @DateTime: 2022/5/18 0018 11:58
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $users
- * @return bool
- */
- public static function batchAddCommission($users)
- {
- try {
- foreach ($users as $uid => $item) {
- $model = self::findOne(['user_id' => $uid, 'status' => StatusEnum::ENABLED]);
- if ($model) {
- foreach ($item as $field => $commission) {
- $model->$field += $commission;
- }
- if (!$model->save()) throw new \Exception($model->getError());
- }
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 推广中心数据获取
- * @Author: lun
- * @DateTime: 2022/5/18 0018 16:04
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return mixed
- */
- 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['is_show_at_center'])) $data['is_show_at_center'] = $basic['is_show_at_center'];
- if ($is_install) {
- $model = self::findOne([
- 'user_id' => $params['user_id'],
- 'status' => StatusEnum::ENABLED,
- ]);
- // 获取分红池收益
- $pool_income = DoubleCopyPoolUser::find()->select('pool_income')->where(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED])->scalar() ?? 0;
- // 获取冻结佣金
- CapitalLog::$capitalType = "commission_frozen";
- $frozen_commission = CapitalLog::find()->where(['user_id' => $params['user_id'], 'from_type' => DoubleCopyEnum::ADDONS_NAME, 'status' => StatusEnum::DISABLED])->sum('change_num') ?: 0;
- $commission = $model['commission'] ?: 0;
- $data['total_commission'] = $commission + $frozen_commission + $pool_income;// 累计 = 累计可用 + 冻结 + 分红池收益
- $data['is_install'] = $is_install;
- }
- return $data;
- }
- /**
- * 查询用户是否已达标
- * @Author: lun
- * @DateTime: 2022/5/18 0018 17:36
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $user_id
- * @param $thaw_num
- * @return int[]
- */
- public static function getForzenUser($user_id, $thaw_num)
- {
- // 数据初始化
- $data = [
- 'has_data' => false,
- 'is_thaw' => 0,
- 'boss_num' => 0,
- 'agent_num' => 0,
- 'help_count' => 0,// 已助力人数
- ];
- $list = self::find()->select('user_id,level,team_num')->where(['pid' => $user_id, 'pos' => [1,2], 'status' => StatusEnum::ENABLED])->asArray()->all();
- if (empty($list)) return $data;
- $data['has_data'] = true;
- $team_num = 0;
- foreach ($list as $item) {
- // 查询缺少的老板数量
- if ($item['level'] == DoubleCopyEnum::LEVEL_AGENT) {
- $data['boss_num']++;
- } else {
- $data['help_count']++;// 等级大于等于老板则表示老板已助力人数
- }
- // 查询缺少的经理数量
- $team_num += $item['team_num'];
- }
- if ($team_num < $thaw_num) {
- $data['agent_num'] = $thaw_num - $team_num;
- $data['help_count'] += $team_num;
- } else {
- $data['help_count'] += $thaw_num;
- }
- // 老板+经理数量都达标则此用户达标
- if ($data['boss_num'] == 0 && $data['agent_num'] == 0) $data['is_thaw'] = 1;
- return $data;
- }
- //检查额外解冻条件是否达标
- public static function checkUnForzen($user_id, $setting)
- {
- $data = [
- 'is_thaw' => 0,
- ];
- //获取1,2号位数据
- $list = self::find()->select('user_id,level')->where(['pid' => $user_id, 'pos' => [1,2], 'status' => StatusEnum::ENABLED])->asArray()->all();
- foreach ($list as $key => $value) {
- if ($value['level'] < $setting['first_level']) return $data;
- }
- //获取直推下线符合条件的人数
- $push_count = self::find()->select('user_id,level')->where(['pid' => $user_id, 'status' => StatusEnum::ENABLED])->andWhere(['>=', 'level', $setting['second_level']])->count();
- if ($push_count < $setting['push_num']) return $data;
- $data['is_thaw'] = 1;
- return $data;
- }
- /**
- * 保存业绩
- * @Author: lun
- * @DateTime: 2022/9/8 0008 17:28
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $user_id
- * @param $params
- * @throws Exception
- */
- public static function saveAchievement($user_id, $params)
- {
- $update_data['updated_at'] = time();
- foreach ($params as $key => $value) {
- $symbol = $value >= 0 ? '+' : '-';
- $update_data[$key] = new Expression($key.$symbol.abs($value));
- }
- $res = self::updateAll($update_data, ['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (!$res) throw new Exception("修改用户{$user_id}业绩失败");
- }
- }
|