| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- declare (strict_types = 1);
- namespace app\api\command;
- use app\common\model\UserModel;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\facade\Db;
- class TeamDymicFree extends Command
- {
- private $arr_addinc = [];
- protected function configure()
- {
- // 团队收益
- $this->setName('team_dymic_free')
- ->setDescription('the team_cymic_free command');
- }
- protected function execute(Input $input, Output $output)
- {
- // 给用户添加today_money 数组
- $this->arr_addinc = array();
- Db::startTrans();
- try {
- $list = Db::name('box_handle')->where('is_team_handle', '0')->order('id desc')->select();
- $model_user = new UserModel();
- $id_backs = [];
- foreach ($list as $key=>&$val){
- // 这是测试 上线要干掉
- $output->writeln('key'.$key);
- $user_info = Db::name('user')->where('id', $val['uid'])->find();
- if($user_info && $user_info['pid'] > 0){
- // $output->writeln('key'.$val['id']);
- $id_backs[] = $val['id'];
- // $total_income = $val['num'] * env('boxsetting.ONE_BOX_PRICE');
- // $output->writeln(json_encode($setting_arr));
- $settings = Db::name('level_setting')->order('id asc')->field('level,dynamic_scale,zt_money')->select()->toArray();
- $setting_arr = array();
- $setting_arr_zt = array();
- foreach ($settings as $key1=>$val1){
- $setting_arr[$val1['level']] = $val1['dynamic_scale'];
- $setting_arr_zt[$val1['level']] = $val1['zt_money'];
- }
- // $output->writeln('key'.json_encode($setting_arr));
- // $output->writeln('key'.json_encode($setting_arr_zt));
- // 计算直推收益
- $pidinfo = $model_user->where('id', $user_info['pid'])->find();
- // $output->writeln('key'.$pidinfo['level']);
- if ($pidinfo['has_fd'] == 1){
- if ($pidinfo['level'] == 0) {
- $money = env('TEAMSETTING.DICRECT_MONEY');
- edit_user_money(5, $pidinfo['id'], $money, 0, $val['id']);
- // Db::name('user')->where('id', $pidinfo['id'])->inc('today_money', floatval($money))->update();
- $this->setUserIncMoney($pidinfo['id'], floatval($money), $input, $output);
- }else{
- // sr_throw(json_encode($setting_arr_zt));
- $money = $setting_arr_zt[$pidinfo['level']];
- edit_user_money(5, $pidinfo['id'], $money, 0, $val['id']);
- // Db::name('user')->where('id', $pidinfo['id'])->inc('today_money', floatval($money))->update();
- $this->setUserIncMoney($pidinfo['id'], floatval($money), $input, $output);
- }
- }
- $path = $user_info['path'];
- // 计算团队并且 除开扶持号
- if (!empty($path)){
- $arr = explode(',', $path);
- $arr = array_reverse($arr);
- $ids = $arr;
- $ids = implode(',',$ids);
- $order= 'field(id,'.$ids.')';
- $users = Db::name('user')->where('id','in' ,$arr)->field('id,level')->order(Db::raw($order))->select()->toArray();
- // 分钱的数组 ['uid'=>'money']
- $get_money_arr = array();
- $cur_bobi = 0;
- $cur_maxlevel = 0;
- foreach ($users as $key1=>$val1){
- if ($key1 == 0){
- if ($val1['level']>0){
- $bobi = $setting_arr[$val1['level']];
- $get_money_arr[$val1['id']] = $bobi;
- $cur_maxlevel = $val1['level'];
- $cur_bobi += $bobi;
- }
- }else{
- if ($val1['level'] > $users[$key1-1]['level']){
- if ($val1['level'] > $cur_maxlevel){
- $bobi = $setting_arr[$val1['level']] - $cur_bobi;
- if ($bobi > 0){
- $get_money_arr[$val1['id']] = $bobi;
- $cur_bobi += $bobi;
- }
- }
- } else{
- }
- }
- }
- // $output->writeln('get_money:'.json_encode($get_money_arr));
- // 计算前面相同等级收益
- foreach ($users as $key1=>$val1){
- if ($key1 != 0){
- if ($val1['level'] == $users[$key1-1]['level']){
- if (isset($get_money_arr[$users[$key1-1]['id']])){
- if ($get_money_arr[$users[$key1-1]['id']] > 0){
- $last_bobi = $get_money_arr[$users[$key1-1]['id']];
- $get_money_arr[$val1['id']] = $last_bobi * 0.1;
- }
- }
- }
- }
- }
- // $get_money_arr 用户团队最终金额
- foreach ($get_money_arr as $key1=>$val1){
- // $get_money_arr[$key1] = sprintf("%.4f",substr(sprintf("%.5f", $val1/100*$total_income), 0, -2));
- $get_money_arr[$key1] = sprintf("%.4f",substr(sprintf("%.5f", $val1), 0, -2));
- }
- // $output->writeln(json_encode($get_money_arr));
- foreach ($get_money_arr as $key1=>$val1){
- // $get_money_arr 里面是计算百分比
- $cur_info = Db::name('user')->where('id', $key1)->find();
- if ($cur_info['has_fd'] == 1){
- edit_user_money(7, $key1, $val1);
- // Db::name('user')->where('id', $key1)->inc('today_money', floatval($val1))->update();
- $this->setUserIncMoney($key1, floatval($val1), $input, $output);
- }
- }
- }
- }else{
- $id_backs[] = $val['id'];
- }
- }
- // 改变所有已经处理状态
- Db::name('box_handle')->whereIn('id', $id_backs)->save([
- 'is_team_handle'=>1
- ]);
- // sr_throw(json_encode($this->arr_addinc));
- foreach ($this->arr_addinc as $key=>$val){
- Db::name('user')->where('id', $key)->inc('today_money', floatval($val))->update();
- }
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- $output->writeln('error:'.$e->getMessage());
- }
- return 22;
- }
- private function setUserIncMoney($key, $money, $input, $output){
- // $output->writeln('key'.$key.'--money:'.$money);
- if (isset($this->arr_addinc[strval($key)]) && $this->arr_addinc[strval($key)] > 0){
- $this->arr_addinc[strval($key)] = ($this->arr_addinc[strval($key)] + $money);
- }else{
- $this->arr_addinc[strval($key)] = $money;
- }
- }
- }
|