| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- <?php
- namespace addons\CircleDistribution\common\models;
- use addons\CircleDistribution\common\enums\DistributionEnum;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\common\Addons;
- use common\models\common\CapitalLog;
- use common\models\common\Download;
- use common\enums\DownloadEnum;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use common\models\user\UserPartitionRelationship;
- use Exception;
- use common\components\export\CsvTool;
- /**
- * This is the model class for table "{{%addons_circle_distribution}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $user_id
- * @property string|null $remarks 备注
- * @property int $status
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $level 分销商等级
- * @property int $upgrade_level_at 分销商等级升级时间
- * @property float $total_commission 佣金
- * @property float $total_score 积分
- * @property User $user
- * @property int $upgrade_status
- */
- class Distribution extends BaseActiveRecord
- {
- const UPGRADE_STATUS_CONDITION = 1;
- const UPGRADE_STATUS_GOODS = 2;
- const UPGRADE_STATUS_MANUAL = 3;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_circle_distribution}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id'], 'required'],
- [['mall_id', 'user_id', 'status', 'created_at', 'updated_at', 'level', 'upgrade_level_at', 'upgrade_status'], 'integer'],
- [['remarks'], 'string'],
- [['total_commission','total_score'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'remarks' => '备注',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- 'level' => '分销商等级',
- 'upgrade_level_at' => '分销商等级升级时间',
- 'total_commission' => '总佣金',
- 'total_score' => '总积分',
- 'upgrade_status' => '升级方式,1条件升级 2 购买指定商品升级 3手动升级'
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- // public function getDistributionLevel()
- // {
- // return $this->hasOne(DistributionLevel::class, ['level' => 'level','mall_id'=>'mall_id'])->where(['status' => StatusEnum::ENABLED]);
- // }
- public function afterSave($insert, $changedAttributes)
- {
- return parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
- }
- /**
- * @desc:保存数据
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 18:07
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return Distribution|false|null
- */
- public static function setData(array $params)
- {
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new \Exception('服务不存在或已删除');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @desc:导出字段
- * @param array|null $field
- * @Author: hua
- * @Date: 2021/10/23
- * @Time: 18:03
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \string[][]
- */
- public static function exportFieldsList($field = null)
- {
- $fields = [
- 'a.user_id' => '用户ID',
- 'u.nickname' => '昵称',
- 'u.mobile' => '手机号',
- 'a.created_at' => '成为经销商时间',
- 'a.total_commission' => '累计佣金',
- 'a.level' => '经销商等级',
- 'u.nickname parent_name' => '推荐人',
- 'a.remarks' => '备注信息'
- ];
- $temp = [];
- foreach ($fields as $key => $val) {
- $showField = explode('.', $key)[1];
- if (strstr($showField, ' ')) {
- $showField = explode(' ', $showField)[1];
- }
- if ($field === null) {
- $temp[$showField] = $val;
- } else if (in_array($showField, $field)) {
- $temp[$key] = $val;
- }
- }
- return $temp;
- }
- public static function fieldsList($field = null)
- {
- $fields = [
- 'user_id' => '用户ID',
- 'nickname' => '昵称',
- 'mobile' => '手机号',
- 'created_at' => '成为分销商时间',
- 'total_commission' => '累计佣金',
- 'level' => '分销商等级',
- 'parent_name' => '推荐人',
- 'remarks' => '备注信息',
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- if (isset($fields[$val])) $temp[$val] = $fields[$val];
- }
- return $temp;
- }
- /**
- * @name:
- * @msg: 处理导出数据,这个方法是异步队列处理时使用的,请别修改
- * @param {array} $data
- * @return bool
- */
- public static function exportHandle(array $data)
- {
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if ($download) {
- try {
- $params = json_decode($download->params, true);
- $filename = $download->name;
- $type = $download->type;
- $fields = $params['fields'];
- $fields_arr = self::fieldsList();
- $have_select_field_arr = [];
- foreach ($fields as $value) {
- $have_select_field_arr[] = $fields_arr[$value];
- }
- $csv = new CsvTool();
- $csv->filename = $filename;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $csv->header = $have_select_field_arr;
- $model = self::find();
- $model->where(['mall_id' => $params['mall_id']]);
- $model->andWhere(['>=', 'status', StatusEnum::DISABLED]);
- $user_id = 0;
- $user_id_arr = [];
- //拼接查询条件
- if (!empty($params['user_id'])) $user_id = $params['user_id'];
- if (!empty($params['level'])) $model->andWhere(['=', 'level', $params['level']]);
- if (!empty($params['keyword'])) {
- $condition['where'] = [[
- 'or',
- ['like', 'username', $params['keyword']],
- ['like', 'nickname', $params['keyword']],
- ['like', 'mobile', $params['keyword']]
- ]];
- $user_list = User::lists($condition);
- $user_id_arr = array_column($user_list, 'id');
- }
- if ($user_id && $user_id_arr) {
- $user_ids = array_intersect([$user_id], $user_id_arr);
- $model->andWhere(['in', 'user_id', $user_ids]);
- } else if ($user_id) {
- $model->andWhere(['in', 'user_id', $user_id]);
- } elseif ($user_id_arr) {
- $model->andWhere(['in', 'user_id', $user_id_arr]);
- }
- $model->with(['user']);//关联查询
- //遍历组装数据
- foreach ($model->batch() as $list) {
- $parent_id_arr = $parent_arr = [];
- //查询上级昵称数组
- if (in_array('parent_name', $fields)) {
- foreach ($list as $item) {
- $parent_id_arr[] = $item['user']['parent_id'];
- }
- $parent_arr = UserPartitionRelationship::getParentsNickname($parent_id_arr);
- }
- $csv->data = array_map(function ($distribution) use ($fields, $parent_arr) {
- $row = [];
- //遍历所选字段
- foreach ($fields as $field) {
- if (in_array($field, ['created_at'])) {
- $row[] = !empty($distribution[$field]) ? date('Y-m-d H:i:s', $distribution[$field]) : '';
- } elseif (in_array($field, ['nickname', 'mobile'])) {
- $row[] = !empty($distribution['user'][$field]) ? $distribution['user'][$field] : '';
- } elseif (in_array($field, ['level'])) {
- //$row[] = !empty($distribution['distributionLevel']['name']) ? $distribution['distributionLevel']['name'] : '';
- } elseif (in_array($field, ['parent_name'])) {
- $row[] = !empty($parent_arr[$distribution['user']['parent_id']]) ? $parent_arr[$distribution['user']['parent_id']] : '平台';
- } else {
- $row[] = !empty($distribution[$field]) ? $distribution[$field] : '';
- }
- }
- return $row;
- }, $list);
- $csv->export();
- }
- //上传云存储
- $csv->updateDown($download, $params['mall_id']);
- return true;
- } catch (Exception $e) {
- $download->status = 3;
- $res = $download->save();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
- public static function getParams($getData)
- {
- $params = [];
- if (isset($getData['user_id']) && !empty($getData['user_id'])) $params['where'][] = ['=', 'u.id', $getData['user_id']];
- if (isset($getData['level']) && !empty($getData['level'])) $params['where'][] = ['=', 'a.level', $getData['level']];
- if (isset($getData['keyword']) && !empty($getData['keyword'])) {
- $params['where'] = [[
- 'or',
- ['like', 'u.username', $getData['keyword']],
- ['like', 'u.nickname', $getData['keyword']],
- ['like', 'u.mobile', $getData['keyword']]
- ]];
- }
- $params['where'][] = ['=', 'a.mall_id', $getData['mall_id']];
- $params['where'][] = ['in', 'a.status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $params['alias'] = 'a';
- $params['join'] = [['LEFT JOIN', User::tableName() . ' up', "up.id = u.parent_id"]];
- $params['joinWith'] = ['user' => function ($query) {
- $query->from(['u' => User::tableName()]);
- }];
- return $params;
- }
- /**
- * @desc:推广中心-调用
- * @Author: hua
- * @Date: 2021/11/12
- * @Time: 9:30
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params ['user_id' => 1, 'mall_id' => 5, 'addons_name' => 'Agent']
- * @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['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']]);
- $data['total_commission'] = $model['total_commission'] ?? 0;
- $data['is_install'] = $is_install;
- }
- return $data;
- }
- public static function getDistributionList($is_self_buy, $mall_id, $user_id)
- {
- $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id);
- $deep = ($is_self_buy == 1) ? 2 : 3;
- $new_parent_id_arr = [];
- for ($i = 0; $i < $deep; $i++) {
- $new_parent_id_arr[] = array_pop($parent_id_arr);
- }
- $where = ['user_id' => $new_parent_id_arr, 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED];
- $addons_distribution_list = Distribution::find()->where($where)->indexBy('user_id')->all();//查询分销商列表
- $distribution_list = [];
- foreach ($new_parent_id_arr as $k => $parent_id) {
- $distribution_list[$k] = [];
- if (!empty($addons_distribution_list[$parent_id])) $distribution_list[$k] = $addons_distribution_list[$parent_id];//按照关系排序
- }
- if ($is_self_buy) {
- $distribution = Distribution::findOne(['user_id' => $user_id]);
- if ($distribution) array_unshift($distribution_list, $distribution);
- }
- return $distribution_list;
- }
- public static function distributionInfo($user, $mall_id)
- {
- $configs = \Yii::$app->services->setting->addons->get($mall_id, DistributionEnum::ADDONS_NAME, 'basic');
- $distribution = Distribution::getOne([['user_id' => $user['id']], ['status' => StatusEnum::ENABLED], ['mall_id' => $mall_id]], true);
- if (empty($configs['is_apply'])) {
- //无条件
- if (empty($distribution)) {
- $params['user_id'] = $user['id'];
- $params['mall_id'] = $mall_id;
- $params['status'] = StatusEnum::ENABLED;
- $distribution = Distribution::setData($params);
- }
- }
- $data = [];
- $data['user_id'] = $user['id'];
- $data['nickname'] = empty($user->nickname) ? '' : $user->nickname;
- $data['avatar_url'] = empty($user->avatar_url) ? '' : $user->avatar_url;
- if (empty($distribution)) {
- $res = DistributionApply::getOne([['user_id' => $user['id']], ['status' => StatusEnum::ENABLED], ['mall_id' => $mall_id]], true, [], 'id desc');
- $setting = \Yii::$app->services->setting->addons->get($mall_id, DistributionEnum::ADDONS_NAME, 'basic');
- if (!empty($res)) {
- if ($res['check_status'] == 0) {
- $data['msg'] = '您已经申请了,请等待审核';
- $data['result'] = ['is_apply' => 2, 'msg' => '您已经申请了,请等待审核', 'is_distribution' => 0];
- return $data;
- }
- if ($res['check_status'] == 2) {
- $data['msg'] = $res['check_remark'];
- $data['result'] = ['is_apply' => 3, 'protocol' => $setting['protocol'], 'msg' => $res['check_remark'], 'is_distribution' => 0];
- }
- return $data;
- }
- $result = ['is_apply' => $setting['is_apply'], 'protocol' => $setting['protocol'], 'is_distribution' => 0];
- $data['msg'] = '您不是分销商';
- $data['result'] = $result;
- return $data;
- }
- $data['parent_name'] = '平台方';
- $data['parent_avatar_url'] = '';
- $data['parent_level_name'] = '';
- $parent_id = $user->parent_id ?? 0;
- if ($parent_id) {
- $parent = User::findOne($parent_id);
- $data['parent_name'] = $parent['nickname'] ?? '';
- $data['parent_avatar_url'] = $parent['avatar_url'] ?? '';
- $parent_distribution = Distribution::findOne(['user_id' => $parent_id]);
- //$data['parent_level_name'] = $parent_distribution->distributionLevel->name ?? '默认等级';
- }
- $data['is_distribution'] = 0;
- $data['level_name'] = '';
- $data['frozen_price'] = 0;
- $data['total_price'] = 0;
- $data['total_income'] = 0;
- $data['yesterday_price'] = 0;
- if ($distribution) {
- $data['is_distribution'] = 1;
- //$data['level_name'] = $distribution['distributionLevel']['name'] ?? '默认等级';
- $wallet_type = 'commission';
- $yesterday = date('Y-m-d', strtotime('-1 day'));
- $start_time = "{$yesterday} 00:00:00";
- $end_time = "{$yesterday} 23:59:59";
- CapitalLog::$capitalType = "{$wallet_type}_frozen";
- $where = ['user_id' => $user['id'], 'from_type' => DistributionEnum::ADDONS_NAME, 'status' => CapitalLog::STATUS_WAIT_SEND];
- $query = CapitalLog::find();
- $frozen_price = $query->where($where)->sum('change_num');
- $yesterday_frozen_price = $query->where($where)
- ->andWhere(['<=', 'created_at', $end_time])
- ->andWhere(['>=', 'created_at', $start_time])
- ->sum('change_num');
- if (empty($yesterday_frozen_price)) $yesterday_frozen_price = 0;
- CapitalLog::$capitalType = "{$wallet_type}";
- $where = ['user_id' => $user['id'], 'from_type' => DistributionEnum::ADDONS_NAME];
- $query = CapitalLog::find();
- $total_price = $query->where($where)->sum('change_num');
- $yesterday_total_price = $query->where($where)
- ->andWhere(['<=', 'created_at', $end_time])
- ->andWhere(['>=', 'created_at', $start_time])
- ->sum('change_num');
- if (empty($yesterday_total_price)) $yesterday_total_price = 0;
- $data['total_price'] = empty($total_price) ? 0 : $total_price;
- $data['frozen_price'] = empty($frozen_price) ? 0 : $frozen_price;
- $data['total_income'] = intval(($data['total_price'] + $data['frozen_price']) * 100) / 100;
- $data['yesterday_price'] = $yesterday_total_price + $yesterday_frozen_price;
- }
- $data['is_show_share_level'] = $configs['is_show_share_level'] ?? 0;
- $data['msg'] = '';
- return $data;
- }
- public static function getAddons(array $params){
- $addons_name = $params['addons_name'];
- $is_install = MallAddons::isInstall($params['mall_id'], $addons_name);
- if($is_install){
- $detail = Addons::getOne([['name' =>$addons_name], ['is_online' => 1]], true);
- return [
- 'addons_name'=>$addons_name,
- 'title'=>$detail['title'],
- ];
- }
- return [];
- }
- }
|