<?php


namespace app\controller\api\entropybarter;
use think\facade\Db;

class Order_236
{
    /**12171
     * @param $order
     * @param $type 1 线上 2线下
     * 订单
     * 修改用户 类型 比例  完成
     */
    public function order($order, $type)
    {
//        Db::name('log')->insert(['data'=>'order_236进来了---']);
        $uid=$order['uid'];
        $mer_id=$order['mer_id'];
        $table_name='entropy_barter_user_236';
        $eb_data=Db::name($table_name)->where('uid',$uid)->find();
        $weizhi=strpos($order['money'],'.');
        if($weizhi){
            $money=substr($order['money'],'0',$weizhi);
        }else{
            $money=$order['money'];
        }
        if($type==1){
            $bill_type=1;
        }else{
            $bill_type=2;
        }
        $bill_data=[
            'number'=>$money,
            'to_id'=>$eb_data['eb_id'],
            'type'=>1,
            'order_no'=>$order['order_no'],
            'remarks'=>'消费'.$money.'元得'.$money.'积分',
            'pm'=>1,
            'status'=>$bill_type,
            'ctime'=>time(),
            'on_line'=>$type,
            'mer_id'=>$mer_id,
            'commission_scale'=>3
        ];
        $this->eb_user_bill($bill_data);
        if($type==2){
            Db::name($table_name)->where('eb_id',$eb_data['eb_id'])->inc('integral',$money)->update();
            Db::name($table_name)->where('eb_id',$eb_data['eb_id'])->inc('cumulative_money',$order['money'])->update();
            //升级
            $this->shengji($eb_data['eb_id'],$table_name);
        }
        /**
         * 奖励开始
         */
        //直推奖励
        $zhitui=$this->zhitui($order,$eb_data,$type,$money,$bill_type,$mer_id,$table_name);
        if($zhitui==false){
            return false;
        }
        //辅导奖
        $fudaojiang=$this->fudaojaing($order,$zhitui,$type,$money,$bill_type,$mer_id,$table_name);
        if($fudaojiang==false){
            return false;
        }
        //管理奖
        $this->gaunlijiang($order,$fudaojiang,$type,$money,$bill_type,$mer_id,$table_name);
        /**
         * 奖励结束
         */
    }

    public function fudaojaing($order,$eb_data,$type,$money,$bill_type,$mer_id,$table_name){
        $top_data=Db::name($table_name)->where('eb_id',$eb_data['top_eb_id'])->find();
        if(!$top_data){
//            Db::name('log')->insert(['data'=>'用户上级无推荐人']);
            return false;
        }
        //辅导奖比例
        $bill=$this->fudaojaing_bili($top_data);
        if($bill==0){
//            Db::name('log')->insert(['data'=>'用户上级的没有累计消费满365']);
            return false;
        }
        $money=bcmul($money,$bill,2);
        if($type==1){
            $bill_data=[
                'number'=>$money,
                'to_id'=>$top_data['eb_id'],
                'type'=>2,
                'order_no'=>$order['order_no'],
                'remarks'=>'辅导奖：'.$money,
                'pm'=>1,
                'status'=>$bill_type,
                'ctime'=>time(),
                'on_line'=>$type,
                'mer_id'=>$mer_id,
                'commission_scale'=>3
            ];
            $this->eb_user_bill($bill_data);
        }elseif($type==2){
            $bill_data=[
                'number'=>$money,
                'to_id'=>$top_data['eb_id'],
                'type'=>2,
                'order_no'=>$order['order_no'],
                'remarks'=>'辅导奖：'.$money,
                'pm'=>1,
                'status'=>$bill_type,
                'ctime'=>time(),
                'on_line'=>$type,
                'mer_id'=>$mer_id,
                'commission_scale'=>3
            ];
            $this->eb_user_bill($bill_data);
            Db::name($table_name)->where('eb_id',$top_data['eb_id'])->inc('wallet',$money)->update();
        }
        return $top_data;
    }

    public function fudaojaing_bili($eb_data)
    {
        switch ($eb_data['type']){
            case 2:
                $bili=0.1;
                break;
            case 3:
                $bili=0.15;
                break;
            default:
                $bili=0;

        }
        return $bili;
    }
    public function gaunlijiang($order,$eb_data,$type,$money,$bill_type,$mer_id,$table_name){
        //管理奖励比例
        $bill=$this->guanlijiangli_bili($eb_data);
        if($bill==0){
//            Db::name('log')->insert(['data'=>'用户上级的上级没有管理奖励']);
            return false;
        }
        $money=bcmul($money,$bill,2);
        if($type==1){
            $bill_data=[
                'number'=>$money,
                'to_id'=>$eb_data['eb_id'],
                'type'=>2,
                'order_no'=>$order['order_no'],
                'remarks'=>'管理奖：'.$money,
                'pm'=>1,
                'status'=>$bill_type,
                'ctime'=>time(),
                'on_line'=>$type,
                'mer_id'=>$mer_id,
                'commission_scale'=>3
            ];
            $this->eb_user_bill($bill_data);
        }elseif($type==2){
            $bill_data=[
                'number'=>$money,
                'to_id'=>$eb_data['eb_id'],
                'type'=>2,
                'order_no'=>$order['order_no'],
                'remarks'=>'管理奖：'.$money,
                'pm'=>1,
                'status'=>$bill_type,
                'ctime'=>time(),
                'on_line'=>$type,
                'mer_id'=>$mer_id,
                'commission_scale'=>3
            ];
            $this->eb_user_bill($bill_data);
            Db::name($table_name)->where('eb_id',$eb_data['eb_id'])->inc('wallet',$money)->update();
        }
        return $eb_data;
    }

    public function guanlijiangli_bili($eb_data)
    {
        switch ($eb_data['type']){
            case 2:
                switch ($eb_data['department_type']){
                    case 1:
                        $bill=0.03;
                        break;
                    case 2:
                        $bill=0.05;
                        break;
                    case 3:
                        $bill=0.08;
                        break;
                    default:
                        $bill=0;
                }
                break;
            case 3:
                switch ($eb_data['department_type']){
                    case 0:
                        $bill=0.03;
                        break;
                    case 1:
                        $bill=0.05;
                        break;
                    case 2:
                        $bill=0.08;
                        break;
                    case 3:
                        $bill=0.10;
                        break;
                    default:
                        $bill=0;
                }
                break;
            default:
                $bill=0;
        }
        return $bill;
    }
    public function zhitui($order,$eb_data,$type,$money,$bill_type,$mer_id,$table_name)
    {
        $top_eb_data=Db::name($table_name)->where('eb_id',$eb_data['top_eb_id'])->find();
        if(!$top_eb_data){
//            Db::name('log')->insert(['data'=>'无上级推荐人']);
            return false;
        }
        if($top_eb_data['type']<2){
//            Db::name('log')->insert(['data'=>'上级推荐人没有消费满365']);
            return false;
        }
        $money=bcmul($money,'0.2',2);
        if($type==1){
            $bill_data=[
                'number'=>$money,
                'to_id'=>$top_eb_data['eb_id'],
                'type'=>2,
                'order_no'=>$order['order_no'],
                'remarks'=>'推荐奖励：'.$money,
                'pm'=>1,
                'status'=>$bill_type,
                'ctime'=>time(),
                'on_line'=>$type,
                'mer_id'=>$mer_id,
                'commission_scale'=>3
            ];
            $this->eb_user_bill($bill_data);
        }elseif($type==2){
            $bill_data=[
                'number'=>$money,
                'to_id'=>$top_eb_data['eb_id'],
                'type'=>2,
                'order_no'=>$order['order_no'],
                'remarks'=>'推荐奖励：'.$money,
                'pm'=>1,
                'status'=>$bill_type,
                'ctime'=>time(),
                'on_line'=>$type,
                'mer_id'=>$mer_id,
                'commission_scale'=>3
            ];
            $this->eb_user_bill($bill_data);
            Db::name($table_name)->where('eb_id',$top_eb_data['eb_id'])->inc('wallet',$money)->update();
        }
        return $top_eb_data;

    }
    //账单记录
    public function eb_user_bill($bill)
    {
        $insert=[
            'from_id'=>$bill['from_id']??0,
            'number'=>$bill['number'],
            'to_id'=>$bill['to_id'],
            'type'=>$bill['type'],
            'order_no'=>$bill['order_no'],
            'remarks'=>$bill['remarks'],
            'pm'=>$bill['pm'],
            'ctime'=>time(),
            'on_line'=>$bill['on_line'],
            'status'=>$bill['status'],
            'pm2_msg'=>$bill['pm2_msg']??'',
            'mer_id'=>$bill['mer_id'],
            'commission_scale'=>$bill['commission_scale']??0,
            'commission_type'=>$bill['commission_type']??0,
        ];
//        Db::name('log')->insert(['data'=>json_encode($insert)]);
        Db::name('entropy_barter_bill')->insert($insert);

    }

    public function shengji($eb_id,$table_name)
    {
        $eb_data=Db::name($table_name)->where('eb_id',$eb_id)->find();
        if($eb_data['cumulative_money']>=365&&$eb_data['cumulative_money']<9999){
            Db::name($table_name)->where('eb_id',$eb_data['eb_id'])->inc('type',1)->update();
            $top_top_data=Db::name($table_name)->where('eb_id',Db::name($table_name)->where('eb_id',$eb_data['top_eb_id'])->value('top_eb_id'))->find();
            if($top_top_data['type']>=1){
                Db::name($table_name)->where('eb_id',$top_top_data['eb_id'])->inc('department_num',1)->update();
                if($top_top_data['department_type']==0&&$top_top_data['department_num']+1>=9){
                    Db::name($table_name)->where('eb_id',$top_top_data['eb_id'])->inc('department_type',1)->update();
                }elseif($top_top_data['department_type']==1&&$top_top_data['department_num']+1>=66){
                    Db::name($table_name)->where('eb_id',$top_top_data['eb_id'])->inc('department_type',1)->update();
                }elseif($top_top_data['department_type']==2&&$top_top_data['department_num']+1>=99){
                    Db::name($table_name)->where('eb_id',$top_top_data['eb_id'])->inc('department_type',1)->update();
                }else{
//                    Db::name('log')->insert(['data'=>'已达到最高级']);
                }
            }
        }
    }
}
