<?php
namespace Api\Controller;

use \Think\Controller;
/**
 * 分销控制器
 */
class ShareLogicController extends Controller
{

  //检索商城分销开关
  public static function check_share($business_id)
  {
    //0关闭 1开始
    $check=M('distribution_config')->where(array('admin_id' => $business_id))->field('switch')->find();
    return $check['switch'];
  }

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

  //我的下线
  public function share_friend()
  {
    $user_id = I('post.user_id');

    if (empty($user_id)) $this->ajaxReturn(['status' => -1, 'msg' => '用户id为空']);

    $one=M('users')->where('share_uid='.$user_id)->field('id, nickname, avatarurl, count_sale')->select();

    $two=array();
    // foreach($one as $i=>$val){
    // $fri=M('users')->where('share_uid='.$val['id'])->field('id, nickname, avatarurl, count_sale')->select();
    // array_push($two,$fri);
    // }
    $pid = array();
    foreach ($one as $key => $value) {
      $pid[] = $value['id'];
    }
    if (!empty($pid)) {
      $where_two['share_uid'] = ['in',$pid];
      $two = M('users')->where($where_two)->field('id, nickname, avatarurl, count_sale')->select();
    }
    $data['one']=$one;
    $data['two']=$two;
    $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $data]);
  }

  //佣金明细
  public function share_detail()
  {
    $user_id = I('post.user_id');
    $type = I('post.type');

    if (empty($user_id)) $this->ajaxReturn(['status' => -1, 'msg' => '用户id为空']);
    // if (empty($type)) $this->ajaxReturn(['status' => -1, 'msg' => '明细状态为空']); 

    $where='user_id='.$user_id;
    if($type==1){
      $where.=" and status=0";
    }
    if($type==2){
      $where.=" and status=1";
    }

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

    if (empty($fri)) $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);

    foreach ($fri as $key => &$value) {
      $value['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
    }

    $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $fri]);
  }

  //支付，生成佣金记录
  public static function create_share($order_sn, $business_id)
  {
    $price=M('order')->where('order_sn='.$order_sn)->getField('price'); //获取订单实际消费金额
    $user_id=M('order')->where('order_sn='.$order_sn)->getField('uid'); //获取用户id

    $config = M('distribution_config')->where(array('admin_id' => $business_id))->field('one_share, two_share')->find();
    
    $one = $config['one_share'];
    $two = $config['two_share'];

    $up_id=M('users')->where('id='.$user_id)->getField('share_uid');
    $where_shareSearch = array("order_sn"=>$order_sn,"user_id"=>$up_id);
    $shareInfo = M('distribution_rec')->where($where_shareSearch)->find();
    if ($shareInfo) {
      return "";
    }
    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('distribution_rec')->add($add);
      $upup_id=M('users')->where('id='.$up_id)->getField('share_uid');
      if($upup_id){
        $add1['user_id']=$upup_id;
        $add1['order_sn']=$order_sn;
        $add1['order_money']=$price;
        $add1['money']= ($price*$two);
        $add1['status']=0;
        $add1['addtime']=time();
        M('distribution_rec')->add($add1);
      }
    }
  }

  // //支付后 分成佣金记录 //团购入口
  // public static 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('distribution_config')->field('one_share')->find()['one_share'];
  //   $two=M('distribution_config')->field('two_share')->find()['two_share'];

  //   $up_id=M('users')->where('id='.$user_id)->getField('share_uid');

  //   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('distribution_rec')->add($add);
  //     $upup_id=M('users')->where('id='.$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('distribution_rec')->add($add1);
  //     }
  //   }
  // }

  //收货返回佣金
  public static function send_share($order_sn)
  {
    $rec=M('distribution_rec')->where('order_sn='.$order_sn)->select();
    if (count($rec) > 2) {
      return "";
    }
    foreach($rec as $i => $val){
      M('distribution_rec')->where('id='.$val['id'])->setField("status",1);//修改佣金状态
      M('users')->where('id='.$val['user_id'])->setInc('count_sale',$val['money']);//累计提现佣金
      M('users')->where('id='.$val['user_id'])->setInc('share_money',$val['money']);//可用提现佣金
    }
  }
  
  public static function build_order_no()
  {
    return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
  }

  /**
   * [cash_apply 佣金提现]
   * @AuthorHTL
   * @DateTime  2018-03-13T10:04:34+0800
   * @return    [type]                   [description]
   */
  public function cash_apply()
  {
    $data = I('post.');

    if (empty($data['user_id'])) $this->ajaxReturn(['status' => -1, 'msg' => '用户id为空']);

    if (empty($data['money'])) $this->ajaxReturn(['status' => -1, 'msg' => '金额为空']);

    $add['user_id']=$data['user_id'];
    $add['money']=$data['money'];
    $add['name']=$data['name'];
    $add['card_number']=$data['card_number'];
    $add['bank']=$data['bank'];
    $add['addtime']=time();
    $add['status']=0;

    $userInfo = M("users")->where(array("id"=>$data['user_id']))->find();
    if ($userInfo['share_money'] < $data['money']) {
      $this->ajaxReturn(['status' => -1, 'msg' => '提交金额异常，请检查剩余可用佣金']);
    }

    M()->startTrans();
    try{
      // 添加提现记录
      $applyRes = M('cash_apply')->add($add);
      // 账户减可用佣金
      $usersRes = M('users')->where(array("id"=>$data['user_id']))->setDec("share_money",$data['money']);
    }catch(\Exception $e){
      M()->rollback();
    }
    if($applyRes && $usersRes){
      M()->commit();
      $this->ajaxReturn(['status' => 1, 'msg' => '已提交申请！等待审核']);
    }else{
      $this->ajaxReturn(['status' => -1, 'msg' => '提交失败！']);
    }
  }

  //分销身份申请
  public function share_apply(){
    $data = I('post.');

    if (empty($data['user_id'])) $this->ajaxReturn(['status' => -1, 'msg' => '用户id为空']);

    $add['status']=0;
    $add['addtime']=time();
    $add['name']=$data['name'];
    $add['phone']=$data['phone'];

    $vers=M('distribution_apply')->where('user_id='.$data['user_id'].' and status=0')->find();
    if($vers){
      $this->ajaxReturn(['status' => -1, 'msg' => '已提交申请！等待审核']);
    }
    $add['user_id']=$data['user_id'];
    M('distribution_apply')->add($add);
    $this->ajaxReturn(['status' => -1, 'msg' => '提交成功!等待审核']);
  }

  /**
   * 佣金提现明细
   * @param  string $user_id [description]
   */
  public function cash_detail($user_id = "", $status = ""){
    if (empty($status)) {
      $list = M("cash_apply")->where("user_id = %d",$user_id)->select();
    }else{
      $list = M("cash_apply")->where("user_id = %d AND status",array($user_id,$status))->select();
    }
    foreach ($list as $key => $value) {
      $list[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
    }
    if (empty($list)) {
      $this->ajaxReturn(array("status"=>-1,"msg"=>'没有数据'));
    }else{
      $this->ajaxReturn(array("status"=>-1,"msg"=>'',"list"=>$list));
    }
  }
}