AddonsBoss.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. <?php
  2. namespace addons\Boss\common\models;
  3. use addons\Boss\common\enums\BossEnum;
  4. use addons\Boss\common\enums\LevelEnum;
  5. use common\enums\LevelUpgradeLogEnum;
  6. use common\enums\StatusEnum;
  7. use common\helpers\Base64Helper;
  8. use common\models\common\Addons;
  9. use common\models\mall\MallAddons;
  10. use common\models\user\User;
  11. use common\enums\RabbitMqEnum;
  12. use common\helpers\csv\CsvImportForm;
  13. use common\models\common\Download;
  14. use common\components\export\CsvTool;
  15. use common\enums\DownloadEnum;
  16. use common\models\user\UserPartitionRelationship;
  17. use Yii;
  18. use yii\db\Exception;
  19. /**
  20. * This is the model class for table "{{%addons_boss}}".
  21. *
  22. * @property int $id
  23. * @property int $mall_id 商城ID
  24. * @property int $user_id 用户ID
  25. * @property int $level 股东等级
  26. * @property float $commission 累计永久佣金,单位:元
  27. * @property float $extra_commission 累计额外佣金,单位:元
  28. * @property float $temp_extra_commission 额外分红金额,主要用于判断是否会导致超额
  29. * @property float $performance 累计团队营业额,单位:元
  30. * @property float $performance_commission 业绩佣金(额外佣金2)
  31. * @property float $total_commission 累计总佣金,单位:元
  32. * @property int $upgrade_status 1条件升级,2购买指定商品升级,3手动升级
  33. * @property int $upgrade_level_at 股东等级升级时间
  34. * @property string $remark 备注
  35. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  36. * @property int $created_at 创建时间
  37. * @property int $updated_at 修改时间
  38. * @property float $digital 累计数字币永久分红,单位:元
  39. * @property float $extra_digital 累计额外分红数字币,单位:元
  40. * @property float $total_digital 累计数字币,单位:元
  41. * @property float $bonus_pool_commission 奖金池佣金,单位:元
  42. * @property int $is_extra_bonus_alone_limit 是否开启额外分红独立限额
  43. * @property float $extra_bonus_limit 额外分红独立限额
  44. */
  45. class AddonsBoss extends \common\models\base\BaseActiveRecord
  46. {
  47. const ADDONS_NAME = 'Boss';
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public static function tableName()
  52. {
  53. return '{{%addons_boss}}';
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function rules()
  59. {
  60. return [
  61. [['mall_id', 'user_id', 'level', 'upgrade_status', 'upgrade_level_at', 'status', 'created_at', 'updated_at', 'is_extra_bonus_alone_limit'], 'integer'],
  62. [['commission', 'extra_commission', 'temp_extra_commission','performance','performance_commission', 'total_commission', 'total_digital', 'extra_digital', 'digital', 'bonus_pool_commission', 'extra_bonus_limit'], 'number'],
  63. [['remark'], 'string'],
  64. ];
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function attributeLabels()
  70. {
  71. return [
  72. 'id' => 'ID',
  73. 'mall_id' => '商城ID',
  74. 'user_id' => '用户ID',
  75. 'level' => '股东等级',
  76. 'commission' => '累计永久佣金,单位:元',
  77. 'extra_commission' => '累计额外佣金,单位:元',
  78. 'temp_extra_commission' => '额外分红金额,主要用于判断是否会导致超额',
  79. 'performance' => '累计团队业绩,单位:元',
  80. 'performance_commission' => '业绩佣金(额外佣金2)',
  81. 'total_commission' => '累计总佣金,单位:元',
  82. 'upgrade_status' => '1条件升级,2购买指定商品升级,3手动升级',
  83. 'upgrade_level_at' => '股东等级升级时间',
  84. 'remark' => '备注',
  85. 'status' => '状态[-1:删除;0:禁用;1启用]',
  86. 'created_at' => '创建时间',
  87. 'updated_at' => '修改时间',
  88. 'digital' => '累计数字币永久分红,单位:元',
  89. 'extra_digital' => '累计额外分红数字币,单位:元',
  90. 'total_digital' => '累计数字币,单位:元',
  91. 'bonus_pool_commission' => '奖金池佣金,单位:元',
  92. 'is_extra_bonus_alone_limit' => '是否开启额外分红独立限额',
  93. 'extra_bonus_limit' => '额外分红独立限额',
  94. ];
  95. }
  96. public static $head_list = [
  97. '0' => [
  98. 'field' => 'serial_number',
  99. 'field_name' => '序号',
  100. ],
  101. '1' => [
  102. 'field' => 'mobile',
  103. 'field_name' => '手机号码',
  104. ],
  105. '2' => [
  106. 'field' => 'level',
  107. 'field_name' => '等级权重',
  108. ]
  109. ];
  110. public static $max_count = 5000;
  111. public static $column_num = 3;
  112. /**
  113. * 导出字段
  114. */
  115. public static function exportFieldsList($field = null)
  116. {
  117. $fields = [
  118. 'user_id' => '用户ID',
  119. 'nickname' => '昵称',
  120. 'mobile' => '手机号',
  121. 'level' => '股东等级',
  122. 'level_id' => '股东等级id',
  123. 'parent_id' => '推荐人ID',
  124. 'parent_name' => '推荐人昵称',
  125. 'parent_mobile' => '推荐人手机号',
  126. 'create' => '创建时间',
  127. ];
  128. return $fields;
  129. }
  130. /**
  131. * @name:
  132. * @msg: 处理导出数据,这个方法是异步队列处理时使用的,请别修改
  133. * @param {array} $data
  134. * @return bool
  135. */
  136. public static function exportHandle(array $data)
  137. {
  138. $download_id = $data['download_id'] ?? 0;
  139. $download = Download::findOne(['id' => $download_id]);
  140. if($download){
  141. try{
  142. $params = json_decode($download->params,true);
  143. $filename = $download->name;
  144. $type = $download->type;
  145. $fields = $params['fields'];
  146. $fields_arr = self::exportFieldsList();
  147. $have_select_field_arr = [];
  148. foreach ($fields as $value){
  149. $have_select_field_arr[]=$fields_arr[$value];
  150. }
  151. $csv = new CsvTool();
  152. $csv->filename = $filename;
  153. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  154. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  155. $csv->header = $have_select_field_arr;
  156. $model = self::find();
  157. $model->where(['mall_id'=>$params['mall_id']]);
  158. $model->andWhere(['>=','status',StatusEnum::DISABLED]);
  159. $user_id = 0;
  160. $user_id_arr = [];
  161. //拼接查询条件
  162. if(!empty($params['user_id'])) $user_id = $params['user_id'];
  163. if(!empty($params['level'])) $model->andWhere( ['=', 'level', $params['level']]);
  164. if (!empty($params['keyword'])) {
  165. $condition['where'] = [[
  166. 'or',
  167. ['like', 'username', $params['keyword']],
  168. ['like', 'nickname', $params['keyword']],
  169. ['like', 'mobile', $params['keyword']]
  170. ]];
  171. $user_list = User::lists($condition);
  172. $user_id_arr = array_column($user_list,'id');
  173. }
  174. if($user_id&&$user_id_arr){
  175. $user_ids = array_intersect([$user_id],$user_id_arr);
  176. $model->andWhere(['in', 'user_id', $user_ids]);
  177. }else if($user_id){
  178. $model->andWhere(['in', 'user_id', $user_id]);
  179. }elseif($user_id_arr){
  180. $model->andWhere(['in', 'user_id', $user_id_arr]);
  181. }
  182. $model->with(['user','bossLevel'=>function($query){
  183. $query->where(['status'=>StatusEnum::ENABLED]);
  184. }]);
  185. //遍历组装数据
  186. foreach ($model->batch() as $list) {
  187. $parent_id_arr = $parent_arr = [];
  188. //查询上级昵称数组
  189. if(in_array('parent_id',$fields)||in_array('parent_name',$fields)||in_array('parent_mobile',$fields)){
  190. foreach ($list as $item){
  191. $parent_id_arr[] = $item['user']['parent_id'];
  192. }
  193. $parent_arr = User::lists(['where' => [['id' => array_unique($parent_id_arr)]], 'index' => 'id', 'select' => 'id,nickname,avatar_url,mobile,parent_id']);
  194. }
  195. $csv->data = array_map(function($agent)use($fields,$parent_arr){
  196. $row = [];
  197. //遍历所选字段
  198. foreach ($fields as $field){
  199. if(in_array($field,['nickname','mobile'])){
  200. $row[] = !empty($agent['user'][$field])?$agent['user'][$field]:'';
  201. }elseif(in_array($field,['level'])){
  202. $row[] = !empty($agent['bossLevel']['name'])?$agent['bossLevel']['name']:'';
  203. }elseif(in_array($field,['level_id'])){
  204. $row[] = !empty($agent['bossLevel']['level'])?$agent['bossLevel']['level']:'';
  205. }
  206. elseif(in_array($field,['parent_id'])){
  207. $row[] = !empty($agent['user']['parent_id'])?$agent['user']['parent_id']:'0';
  208. }
  209. elseif(in_array($field,['parent_name'])){
  210. $row[] = !empty($parent_arr[$agent['user']['parent_id']])?$parent_arr[$agent['user']['parent_id']]['nickname']:'平台';
  211. }
  212. elseif(in_array($field,['parent_mobile'])){
  213. $row[] = !empty($parent_arr[$agent['user']['parent_id']])?$parent_arr[$agent['user']['parent_id']]['mobile']:'平台';
  214. }
  215. elseif(in_array($field,['create'])){
  216. $row[] = !empty($agent["created_at"])? date('Y-m-d h:i:s',$agent['created_at']):'';
  217. }
  218. else{
  219. $row[] = !empty($agent[$field])?$agent[$field]:'';
  220. }
  221. }
  222. return $row;
  223. },$list);
  224. $csv->export();
  225. }
  226. $csv->updateDown($download,$params['mall_id']);
  227. return true;
  228. }catch(Exception $e){
  229. $download->status = 3;
  230. $res = $download->save();
  231. var_dump($e->getMessage());
  232. self::setStaticError($e->getMessage());
  233. return false;
  234. }
  235. }
  236. }
  237. public function getUser()
  238. {
  239. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id');
  240. }
  241. /**
  242. * @desc:关联股东等级
  243. * @Author: hua
  244. * @Date: 2021/11/5
  245. * @Time: 10:04
  246. * @Copyright: copyright (c) 2021 广东七件事集团
  247. * @return \yii\db\ActiveQuery
  248. */
  249. public function getBossLevel(){
  250. return $this->hasOne(AddonsBossLevel::class, ['level' => 'level','mall_id'=>'mall_id'])->andWhere(['<>','status',StatusEnum::DELETE]);
  251. }
  252. /**
  253. * 保存数据
  254. * @Author: lun
  255. * @DateTime: 2021/10/22 0022 17:13
  256. * @Copyright: copyright (c) 2021 广东七件事集团
  257. * @param array $params
  258. * @return AddonsBoss|bool
  259. */
  260. public static function setData(array $params){
  261. try{
  262. if(!empty($params['user_id'])){
  263. $model = self::findOne(['user_id' => $params['user_id'],'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  264. if(empty($model)) {
  265. $model = new self();
  266. $model->loadDefaultValues();
  267. $model->mall_id = $params['mall_id'];
  268. $model->upgrade_status = LevelEnum::UPGRADE_MANUAL;
  269. $model->upgrade_level_at = time();
  270. // 记录升级日志(如果创建的不是默认等级,则认为手动升级)
  271. if ($params['level'] > 0) {
  272. $upgrade_log_obj = new BossLevelUpgradeLog();
  273. $upgrade_log_obj->before_level = 0;
  274. $upgrade_log_obj->upgrade_type = LevelUpgradeLogEnum::MANUALLY_UPGRADE;
  275. }
  276. } else {
  277. // 记录升级日志
  278. if (
  279. !empty($params['upgrade_status']) &&
  280. $params['upgrade_status'] === LevelEnum::UPGRADE_MANUAL &&
  281. $model->level != $params['level']
  282. ) {
  283. $upgrade_log_obj = new BossLevelUpgradeLog();
  284. $upgrade_log_obj->before_level = $model->level;
  285. $upgrade_log_obj->upgrade_type = $params['level'] > $model->level ? LevelUpgradeLogEnum::MANUALLY_UPGRADE : LevelUpgradeLogEnum::DOWNGRADE_MANUALLY;
  286. }
  287. }
  288. if (isset($upgrade_log_obj)) {
  289. $upgrade_log_obj->admin_id = $params['admin_id'];
  290. $upgrade_log_obj->mall_id = $params['mall_id'];
  291. $upgrade_log_obj->user_id = $params['user_id'];
  292. $upgrade_log_obj->after_level = $params['level'];
  293. $upgrade_log_obj->save();
  294. }
  295. } else {
  296. throw new \Exception('股东数据不存在或已删除');
  297. }
  298. if(!$model->load($params,'') || !$model->save()){
  299. throw new Exception($model->getErrorMessage());
  300. }
  301. return $model;
  302. }catch(\Exception $e){
  303. self::setStaticError($e->getMessage());
  304. return false;
  305. }
  306. }
  307. /**
  308. * 获取股东列表
  309. * @Author: lun
  310. * @DateTime: 2021/10/25 0025 10:05
  311. * @Copyright: copyright (c) 2021 广东七件事集团
  312. * @param array $cond
  313. * @param array $with
  314. * @param bool $is_array
  315. * @param string $select
  316. * @return array|\yii\db\ActiveRecord[]
  317. */
  318. public static function getBoss(array $cond=[], array $with=[],bool $is_array=true,string $select='*'){
  319. $default_cond = [['<>','status',StatusEnum::DELETE]];
  320. $cond = array_merge($default_cond,$cond);
  321. $query = self::find()->select($select);
  322. self::paramPack(['where'=>$cond, 'with'=>$with],$query);
  323. $data = $query->asArray($is_array)->all();
  324. return $data;
  325. }
  326. /**
  327. * @desc:推广中心-调用
  328. * @Author: hua
  329. * @Date: 2021/11/12
  330. * @Time: 9:30
  331. * @Copyright: copyright (c) 2021 广东七件事集团
  332. * @param $params ['user_id' => 1, 'mall_id' => 5, 'addons_name' => 'Agent']
  333. * @return array
  334. */
  335. public static function getIncomeInfo(array $params){
  336. $is_install = MallAddons::isInstall($params['mall_id'], $params['addons_name']);
  337. $data['total_commission'] = 0;
  338. $data['is_install'] = 0;
  339. $basic = \Yii::$app->services->setting->addons->get($params['mall_id'], $params['addons_name'], 'basic');
  340. $data['is_show_at_center'] = 1;
  341. if(isset($basic['is_show_at_center'])) $data['is_show_at_center'] = $basic['is_show_at_center'];
  342. if($is_install){
  343. $model = self::findOne(['user_id'=>$params['user_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  344. $data['total_commission'] = $model['total_commission']??0;
  345. $data['is_install'] = $is_install;
  346. }
  347. return $data;
  348. }
  349. public static function getAddons(array $params){
  350. $is_install = MallAddons::isInstall($params['mall_id'], BossEnum::ADDONS_NAME);
  351. if($is_install){
  352. $detail = Addons::getOne([['name' => BossEnum::ADDONS_NAME], ['is_online' => 1]], true);
  353. return [
  354. 'addons_name'=>BossEnum::ADDONS_NAME,
  355. 'title'=>$detail['title'],
  356. ];
  357. }
  358. return [];
  359. }
  360. /**
  361. * 根据等级+升级类型统计人数
  362. * @Author:lun
  363. * @Date:2024-03-16 15:07
  364. * @Copyright:copyright(c)2024 广东七件事集团
  365. * @param $mall_id
  366. * @return array
  367. */
  368. public static function getBossCountByExtraUpgrade($mall_id)
  369. {
  370. $data = [];// 初始化返回值
  371. //获取权重等级数据
  372. $level = AddonsBossLevel::lists([
  373. 'where' => [
  374. ['mall_id' => $mall_id],
  375. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  376. ],
  377. 'index' => 'level',
  378. 'with' => ['setting'],
  379. ]);
  380. $list = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
  381. ->asArray()->all();
  382. if ($list) {
  383. foreach ($list as $item) {
  384. if ($level[$item['level']]['setting']['is_static_extra'] == 2 && $item['temp_extra_commission'] >= $level[$item['level']]['setting']['extra_limit_price']) continue;
  385. if (empty($data[$item['level'] . '_' . $item['upgrade_status']])) {
  386. $data[$item['level'] . '_' . $item['upgrade_status']] = 1;
  387. } else {
  388. $data[$item['level'] . '_' . $item['upgrade_status']] += 1;
  389. }
  390. }
  391. }
  392. return $data;
  393. }
  394. /**
  395. * 批量更新代理等级
  396. * @param $params
  397. * @return array|bool
  398. */
  399. public static function importLevel($params)
  400. {
  401. $t = \Yii::$app->db->beginTransaction();
  402. try {
  403. self::isCanImport($params['mall_id']);
  404. $csvImport = new CsvImportForm();
  405. $data = $csvImport->setDataForm($params)
  406. ->setMaxCount(self::$max_count)
  407. ->setImportFieldConfig(self::$head_list)
  408. ->setColumnNum(self::$column_num)
  409. ->readCsvData();
  410. if (!$data) throw new \Exception(CsvImportForm::getStaticError());
  411. $success_counts = $fail_counts = 0;
  412. $counts = count($data);
  413. $fail_arr = [];
  414. $success_user_id_arr = [];
  415. $merge_arr = [];
  416. foreach ($data as $item) {
  417. $param = $item;
  418. $merge_arr[] = $param;
  419. }
  420. self::batchUpdateLevel($params['mall_id'], $merge_arr, $success_counts, $fail_arr, $fail_counts, $success_user_id_arr);
  421. $param['counts'] = $counts;
  422. $param['success_counts'] = $success_counts;
  423. $param['fail_counts'] = $fail_counts;
  424. $param['status'] = 1;
  425. $param['mall_id']=$params['mall_id'];
  426. $import_log_model = BossImportLevelLog::setData($param);
  427. if (!$import_log_model) throw new \Exception(BossImportLevelLog::getStaticError());
  428. if (!empty($fail_arr)) {
  429. $res = BossImportLevelFailLog::setData($fail_arr, $import_log_model->id);
  430. if (!$res) throw new \Exception(BossImportLevelFailLog::getStaticError());
  431. }
  432. $t->commit();
  433. return ['counts' => $counts, 'success_counts' => $success_counts, 'fail_counts' => $fail_counts];
  434. } catch (\Exception $e) {
  435. var_dump($e->getMessage().$e->getFile());
  436. self::$static_error = $e->getMessage();
  437. $t->rollBack();
  438. return false;
  439. }
  440. }
  441. public static function importLevelSync($params)
  442. {
  443. $t = \Yii::$app->db->beginTransaction();
  444. try {
  445. self::isCanImport($params['mall_id']);
  446. $basePath = \Yii::$app->basePath;
  447. if (empty($_FILES) || !isset($_FILES['file'])) throw new \Exception('请上传csv文件');
  448. $fileName = $_FILES['file']['name'];
  449. $tmpName = $_FILES['file']['tmp_name'];
  450. $basePath = $basePath . '/web/uploads/';
  451. if (!is_dir($basePath))mkdir($basePath, 0775, true);
  452. $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
  453. if ($ext != 'csv') throw new \Exception('请上传csv文件');
  454. $uploadFile = $basePath . 'ADMIN-' . date('Y-m-d His') . '.' . $ext;
  455. //上传
  456. move_uploaded_file($tmpName, $uploadFile);
  457. $success_counts = $fail_counts = 0;
  458. $counts = 0;
  459. $param['counts'] = $counts;
  460. $param['success_counts'] = $success_counts;
  461. $param['fail_counts'] = $fail_counts;
  462. $param['upload_file'] = $uploadFile;
  463. $param['mall_id'] = $params['mall_id'];
  464. $user_import_log_model = BossImportLevelLog::setData($param);
  465. if (!$user_import_log_model) throw new \Exception(BossImportLevelLog::getStaticError());
  466. $t->commit();
  467. $params = ['id'=>$user_import_log_model['id']];
  468. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::TASK_QUEUE, RabbitMqEnum::TASK_QUEUE, $params, self::class, 'syncImportLevel');
  469. return ['counts' => $counts, 'success_counts' => $success_counts, 'fail_counts' => $fail_counts];
  470. } catch (\Exception $e) {
  471. self::$static_error = $e->getMessage();
  472. var_dump($e->getMessage());
  473. $t->rollBack();
  474. return false;
  475. }
  476. }
  477. public static function isCanImport($mall_id){
  478. $time = strtotime('2024-04-28 23:59:59');
  479. $res = BossImportLevelLog::find()->where(['mall_id'=>$mall_id,'status'=>3])->andWhere(['>=','created_at',$time])->one();
  480. if(!empty($res)) throw new \Exception('等上一次导入执行完再导入');
  481. }
  482. //异步执行
  483. public static function syncImportLevel($params){
  484. if(empty($params['id'])) return false;
  485. $model = BossImportLevelLog::findOne(['id' => $params['id']]);
  486. if (!empty($model['upload_file'])) {
  487. $upload_file = $model['upload_file'];
  488. $mall_id = $model['mall_id'];
  489. $cvsFile = fopen($upload_file, 'r'); //开始读取csv文件数据
  490. $fail_arr = [];
  491. $success_counts = $fail_counts =$counts = $i = 0;
  492. $counts = 0;
  493. $success_user_id_arr = [];
  494. $data = [];// 初始化数据
  495. while ($fileData = fgetcsv($cvsFile)) {
  496. $i++;
  497. if ($i == 1) continue;//过滤表头
  498. $counts++;
  499. $user_id=0;
  500. $level_id=0;
  501. foreach ($fileData as $k => &$val) {
  502. $val = mb_convert_encoding(trim($val), "UTF-8", "GBK");
  503. switch ($k) {
  504. case 1:
  505. $user_id = !empty($val)?$val:'';
  506. break;
  507. case 2:
  508. $level_id = !empty($val)?trim($val):0;
  509. break;
  510. }
  511. }
  512. $param['mobile'] = $user_id;
  513. $param['level'] = $level_id;
  514. $data[] = $param;
  515. }
  516. self::batchUpdateLevel($mall_id, $data, $success_counts, $fail_arr, $fail_counts);
  517. $model->counts = $counts;
  518. $model->success_counts = $success_counts;
  519. $model->fail_counts = $fail_counts;
  520. $model->status = 1;
  521. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  522. if (!empty($fail_arr)) {
  523. $res = BossImportLevelFailLog::setData($fail_arr, $model->id);
  524. if (!$res) throw new \Exception(BossImportLevelFailLog::getStaticError());
  525. }
  526. }
  527. }
  528. /**
  529. * 批量更新等级
  530. * @Author: zfh
  531. * @DateTime: 2024/4/28 0003 15:16
  532. * @Copyright: copyright (c) 2021 广东七件事集团
  533. * @param $mall_id
  534. * @param $data
  535. * @param int $success_counts
  536. * @param array $fail_arr
  537. * @param int $fail_counts
  538. * @throws Exception
  539. */
  540. public static function batchUpdateLevel($mall_id, $data, &$success_counts = 0, &$fail_arr = [], &$fail_counts = 0, &$success_user_id_arr = [])
  541. {
  542. foreach ($data as $item) {
  543. $fail_params = $item;
  544. $fail_params['maill_id']=$mall_id;
  545. $where = [];
  546. $where[] = ['mall_id' => $mall_id];
  547. $where[] = ['mobile' => $item['mobile']];
  548. $user = User::getUser($where,'id,mobile');
  549. if (empty($user)) {
  550. $fail_params['reason'] = '用户不存在';
  551. list($fail_arr, $fail_counts) = BossImportLevelFailLog::packFailArr($fail_params, $fail_arr, $fail_counts);
  552. } else if (empty(AddonsBossLevel::getOne([['level' => $item['level'],'status'=>1,'mall_id'=>$mall_id]]))) {
  553. $fail_params['reason'] = '股东等级id不存在';
  554. list($fail_arr, $fail_counts) = BossImportLevelFailLog::packFailArr($fail_params, $fail_arr, $fail_counts);
  555. } else {
  556. $agent = AddonsBoss::getOne([['user_id' => $user['id']]]);
  557. if ($agent) {
  558. $item['id'] = $agent['id'];
  559. }
  560. $item['mall_id']=$mall_id;
  561. $item['user_id']=$user['id'];
  562. $item['upgrade_level_at']=time();
  563. $item['admin_id'] = 0;
  564. $item['upgrade_status']=LevelEnum::UPGRADE_MANUAL;
  565. $res = AddonsBoss::setData($item);
  566. if ($res) {
  567. $success_user_id_arr[] = $res['id'];
  568. $success_counts++;
  569. } else {
  570. $fail_params['reason'] = AddonsBoss::getStaticError();
  571. list($fail_arr, $fail_counts) = BossImportLevelFailLog::packFailArr($fail_params, $fail_arr, $fail_counts);
  572. }
  573. }
  574. }
  575. }
  576. /**
  577. * 获取团队中对应层级有多少人数
  578. * @param $user_id //会员id
  579. * @param $level //查询层级
  580. * @return false|int|string|null
  581. */
  582. public static function getTeamlevelCount($user_id,$level){
  583. list($tree,$parent_deep,$all_tree, $all_tree_arr) = UserPartitionRelationship::getTree($user_id);
  584. $count = (new \yii\db\Query())
  585. ->select(['COUNT(DISTINCT u.id) as count'])
  586. ->from(['u' => 'qimall_addons_boss'])
  587. ->innerJoin(['upr' => 'qimall_user_partition_relationship'], 'u.user_id = upr.user_id') //股东身份还是看主商城会员关系链
  588. ->where(['u.level' => $level,'u.status'=>StatusEnum::ENABLED])
  589. ->andWhere(['like', 'upr.tree', "{$tree},%", false])
  590. ->scalar();
  591. return $count;
  592. }
  593. }