<?php
namespace Logic\Logic;
use Think\Model;
class ShareLogic {

  //检索商城分销开关
  public function check_share(){
    //0关闭 1开始
    $check=M('share_config')->where('id=1')->getField('switch');
    return $check;
  }

  //检测用户是否为分销员
  public function check_user_share($user_id){
    $where='uid='.$user_id;
    $check=M('wx_users')->where($where)->getField('share_status');
    return $check;
  }

  //我的下线
  public function share_friend($user_id){

    $one=M('wx_users')->where('share_uid='.$user_id)->select();

    $two=array();
    foreach($one as $i=>$val){
    $fri=M('wx_users')->where('share_uid='.$val['uid'])->select();
    array_push($two,$fri);
    }
    $data['one']=$one;
    $data['two']=$two;
    // dump($data);exit;
    return $data;
  }

  //佣金明细
  public function share_detail($user_id,$type){
    $where='user_id='.$user_id;
    if($type==1){
      $where.=" and status=0";
    }
    if($type==2){
      $where.=" and status=1";
    }
    if($type==3){
      $where.=" and status=2";
    }

    $fri=M('share_rec')->where($where)->order('addtime desc')->select();
    

  }


  //支付后 分成佣金记录

  public function create_share($order_sn){

    $price=M('order')->where('order_sn='.$order_sn)->getField('gold'); //获取订单消费金额
  $user_id=M('order')->where('order_sn='.$order_sn)->getField('uid'); //获取用户id

    $one=M('share_config')->where('id=1')->getField('one_share');
    $two=M('share_config')->where('id=1')->getField('two_share');

    $up_id=M('wx_users')->where('uid='.$user_id)->getField('share_uid');
    // echo $up_id;exit;
    if($up_id){
      $add['user_id']=$up_id;
      $add['order_sn']=$order_sn;
      $add['order_money']=$price;
      $add['money']=($price*$one);
      $add['status']=0;
      $add['addtime']=time();
      M('share_rec')->add($add);
      $upup_id=M('wx_users')->where('uid='.$up_id)->getField('share_uid');
      if($upup_id){
        $add1['user_id']=$up_id;
        $add1['order_sn']=$order_sn;
        $add1['order_money']=$price;
        $add1['money']=($price*$two);
        $add1['status']=0;
        $add1['addtime']=time();
        M('share_rec')->add($add1);
      }
    }
    
  }
  //支付后 分成佣金记录 //团购入口

  public function create_share_group($pay_no){

    $price=M('group_order_log')->where('pay_no='.$pay_no)->getField('pay_price'); //获取订单消费金额
  $user_id=M('group_order_log')->where('pay_no='.$pay_no)->getField('user_id'); //获取用户id

    $one=M('share_config')->where('id=1')->getField('one_share');
    $two=M('share_config')->where('id=1')->getField('two_share');

    $up_id=M('wx_users')->where('uid='.$user_id)->getField('share_uid');
    // echo $up_id;exit;
    if($up_id){
      $add['user_id']=$up_id;
      $add['order_sn']=$pay_no;
      $add['order_money']=$price;
      $add['money']=($price*$one);
      $add['status']=0;
      $add['addtime']=time();
      M('share_rec')->add($add);
      $upup_id=M('wx_users')->where('uid='.$up_id)->getField('share_uid');
      if($upup_id){
        $add1['user_id']=$up_id;
        $add1['order_sn']=$pay_no;
        $add1['order_money']=$price;
        $add1['money']=($price*$two);
        $add1['status']=0;
        $add1['addtime']=time();
        M('share_rec')->add($add1);
      }
    }
    
  }

  //收货返回佣金
  public function send_share($order_sn){

    $rec=M('share_rec')->where('order_sn='.$order_sn)->select();
    foreach($rec as $i => $val){
      M('share_rec')->where('order_sn='.$order_sn)->setField($status,1);//修改佣金状态
      M('wx_users')->where('uid='.$val['user_id'])->setInc('count_sale',$val['money']);//累计可用提现佣金
    }


  }
  

    

    public function build_order_no()
    {
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }


  public function cash_apply(){
    $add['user_id']=$_REQUEST['user_id'];
    $add['money']=$_REQUEST['money'];
    $add['name']=$_REQUEST['name'];
    $add['card_number']=$_REQUEST['card_number'];
    $add['bank']=$_REQUEST['bank'];
    $add['addtime']=time();
    $add['status']=0;

    $rows=M('cash_apply')->add($add);
    if($rows){
      echo 10000;exit;
    }else{
      echo 20000;exit;
    }
  }

}