<?php
namespace Mail\Lib;

use Dever;

class Manage
{
	#代理商日数据统计
	public function day_api(){
        $day = Dever::input('day');
        if($day){
            $start = strtotime($day);
            $end = $start+86400-1;
        }else{
            $start=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $end = $start+86400-1;
        }
        
        $time = Dever::maketime(date('Y-m-d'),'00:00:00');
        $where = array();
        $where['fstart'] = $start;
        $where['fend'] = $end;
        $w = array();
        $fstart = Dever::input('start', date('Y-m-01', $start));
        $w['fstart'] = Dever::maketime($fstart . ' 00:00:00');
        $w['fend'] = strtotime(date('Y-m-d 23:59:59',
                strtotime($fstart."+1 month - 1 day")));;
        $data = array();
        $data['start'] = date('Y-m-d',$start);
        $data['day'] = $this->content_api($where,1);
        $data['money'] = $this->c_api($where);
        $data['week'] = $this->zhou_api($start);
        $data['all'] = $this->content_api($w,2);
        $data['all_money'] = $this->c_api($w);
        $data['time'] = $this->day_time_api($w);
        $data['data'] = Dever::json_encode($data['time']);
        $data['money_time'] = $this->money_time_api($w);
        $data['qu_money'] = Dever::json_encode($data['money_time']);
        $data['xzhou'] = $this->button_day_api($w,1);
        $data['zhou'] = Dever::json_encode($data['xzhou']);
        
        return Dever::render('census', $data);
	}

    public function zhou_api($time='',$format="Y-m-d"){
        $time = $time != '' ? $time : time();
        $week = date('w', $time);
        $date = [];
        for ($i=1; $i<=7; $i++){
            $date[$i] = date($format ,strtotime( '+' . $i-$week .' days', $time));
        }
        $start = strtotime($date[1]);
        $end = strtotime($date[7])+86400-1;
        $where = array();
        $where['fstart'] = $start;
        $where['fend'] = $end;
        
        $data = array();
        $data['week'] = date('Y.m.d',$start).' - '.date('Y.m.d',$end);
        $data['all'] = $this->content_api($where,2);
        $data['all_money'] = $this->c_api($where);
        return $data;
    }
    #按时间统计
	public function content_api($where,$type){
		$data = array();
        if($type == 2){
            $data['time'] = date('Y.m',$where['fstart']);
        }elseif($type == 1){
            $data['time'] = date('Y.m.d',$where['fstart']);
        }
        #确认代理商订单数
        $where['status'] = 4;
        $data['num'] = Dever::db('agent/order')->getNum($where);
        #确认订单总费用
        $money = Dever::db('agent/order')->getCash($where);
        if($money['total']){
        	$data['money'] = $money['total'];
        }else{
        	$data['money'] = 0;
        }
        #确认代理费
        $dl_money = Dever::db('agent/order')->getDailiCash($where);
        if($dl_money['total']){
            $data['dl_money'] = $dl_money['total'];
        }else{
            $data['dl_money'] = 0;
        }
        #确认软件服务费
        $soft_money = Dever::db('agent/order')->getSoftCash($where);
        if($soft_money['total']){
            $data['soft_money'] = $soft_money['total'];
        }else{
            $data['soft_money'] = 0;
        }
        $wh['status'] = 3;
        $wh['fstart'] = $where['fstart'];
        $wh['fend'] = $where['fend'];
        $cash = Dever::db('agent/soft_cash')->getTotal($wh);
        if($cash['total']){
            $data['cash'] = $cash['total'];
        }else{
            $data['cash'] = 0;
        }
        $data['soft_cash'] = $data['soft_money'] + $data['cash'];

        #期权价值
        $option_money = Dever::db('agent/order')->getOptionPrice($where);
        if($option_money['total']){
            $data['option_money'] = $option_money['total'];
        }else{
            $data['option_money'] = 0;
        }
        return $data;
    }
    #各角色的金额,数量,原始期权价值,软件服务费
    public function c_api($where){
        $role = Dever::db('setting/role')->getAll(array('id'=>1,'state'=>1));
        foreach($role as $k => $v){
            $where['role'] = $v['id'];
            $data[$k]['name'] = $v['name'];
            $where['status'] = 2;
            $data[$k]['num'] = Dever::db('agent/member')->getNum($where);
            $where['status'] = 4;
            $dl = Dever::db('agent/order')->getSum($where);
            if($dl['total']){
                $data[$k]['money'] = $dl['total'];
            }else{
                $data[$k]['money'] = 0;
            }
            #期权价值:
            $option_money = Dever::db('agent/order')->getOptionPrice($where);
            if($option_money['total']){
                $data[$k]['option_money'] = $option_money['total'];
            }else{
                $data[$k]['option_money'] = 0;
            }
            #软件服务费
            $soft_money = Dever::db('agent/order')->getSoftCash($where);
            if($soft_money['total']){
                $data[$k]['soft_money'] = $soft_money['total'];
            }else{
                $data[$k]['soft_money'] = 0;
            }
            $wh['role'] = $v['id'];
            $wh['status'] = 3;
            $wh['fstart'] = $where['fstart'];
            $wh['fend'] = $where['fend'];
            $cash = Dever::db('agent/soft_cash')->getTotal($wh);
            if($cash['total']){
                $data[$k]['cash'] = $cash['total'];
            }else{
                $data[$k]['cash'] = 0;
            }
        }
        return $data;
    }
    #数据趋势
    public function day_time_api($w){
        $time = date('m',time()) - date('m',$w['fstart']);
        $start = date('Y-m-01',$w['fstart']);
        if($time<=0){
            $w['fend'] = time();
        }elseif($time>0){
            $w['fend'] = strtotime(date('Y-m-d 23:59:59',
                strtotime($start."+1 month - 1 day")));
        }
      $days = ($w['fend']-$w['fstart'])/86400+1;
      // 保存每天日期
      $day = array();
      $data = array();
      for($i=0; $i<$days; $i++){
        $day[] =  array($w['fstart']+(86400*$i),$w['fstart']+(86400*$i)+86400);
      }
      $res = array();
      $res['num'] = array();
      foreach($day as $k => $v){
        $where['fstart'] = $v[0];
        $where['fend'] =$v[1]-1;
        $data[$k]['time'] = date('m.d',$v[0]);
        #确认代理商订单数
        $where['status'] = 4;
        $res['num'][] = Dever::db('agent/order')->getNum($where);
      }
      // print_R($where);die;
      $data = array();
      $data['name'] = '数量';
      $data['type'] = 'line';
      // $data['stack'] = 'Total';
      $data['data'] = $res['num'];
      return $data;
    }
    #金额趋势
    public function money_time_api($w){
        $time = date('m',time()) - date('m',$w['fstart']);
        $start = date('Y-m-01',$w['fstart']);
        if($time<=0){
            $w['fend'] = time();
        }elseif($time>0){
            $w['fend'] = strtotime(date('Y-m-d 23:59:59',
                strtotime($start."+1 month - 1 day")));
        }
      $days = ($w['fend']-$w['fstart'])/86400+1;
      // 保存每天日期
      $day = array();
      $data = array();
      for($i=0; $i<$days; $i++){
        $day[] =  array($w['fstart']+(86400*$i),$w['fstart']+(86400*$i)+86400);
      }
      $res = array();
      $res['money'] = array();
      foreach($day as $k => $v){
        $where['fstart'] = $v[0];
        $where['fend'] =$v[1]-1;
        $data[$k]['time'] = date('m.d',$v[0]);
        #确认订单总费用
        $money = Dever::db('agent/order')->getCash($where);
        if($money['total']){
            $res['money'][] = $money['total'];
        }else{
            $res['money'][] = 0;
        }
      }
      $money = array();
      $money['name'] = '金额';
      $money['type'] = 'line';
      $money['data'] = $res['money'];
      return $money;
    }
    #趋势底部日期
    public function button_day_api($w,$type){
        $time = date('m',time()) - date('m',$w['fstart']);
        $start = date('Y-m-01',$w['fstart']);
        if($time<=0){
            $w['fend'] = time();
        }elseif($time>0){
            $w['fend'] = strtotime(date('Y-m-d 23:59:59',
                strtotime($start."+1 month - 1 day")));
        }
        $days = ($w['fend']-$w['fstart'])/86400+1;
        if($type ==1){
            $daysd = $days-1;
        }else{
            $daysd = $days;
        }
      // 保存每天日期
      $day = array();
      $data = array();
      for($i=0; $i<$daysd; $i++){
        $day[] = date('m.d',$w['fstart']+(86400*$i));
      }
      $data['type'] = 'category';
      $data['boundaryGap'] = false;
      $data['date'] = $day;
      return $day;
    }
    #代理商日统计
    public function index_api(){
        $title = '代理商日数据统计';
        $data = Dever::db('mail/feature')->find(array('key'=>'lib/cron.day'));
        $email = explode(',',$data['email_id']);
        foreach($email as $k => $v){
            $res[] = Dever::db('mail/email')->find(array('id'=>$v));
        }
        $content = $this->index_api();
        foreach($res as $key => $val){
            $this->send($val['email'], $val['name'], $title, $content);
        }
    }
    
    #代理商统计发送邮件
    public function send($email, $username, $title, $content)
    {
        $config = Dever::config('base', 'project')->email;

        Dever::apply('src/PHPMailer', 'email');
        Dever::apply('src/Exception', 'email');
        Dever::apply('src/SMTP', 'email');
        $mail = new \PHPMailer\PHPMailer\PHPMailer();

        $mail->isSMTP();
        //$mail->SMTPDebug = 2;
        $mail->CharSet = 'UTF-8';
        $mail->Host = $config['smtp'];
        $mail->Port = 465;
        $mail->SMTPSecure = 'ssl';
        $mail->SMTPAuth = true;
        $mail->Username = $config['username'];
        $mail->Password = $config['password'];
        $mail->setFrom($config['from'][0], '统计');
        $mail->addAddress($email, $username);
        $mail->Subject = "=?utf-8?B?" . base64_encode($title) . "?=";
        $mail->Body = $content;
        $mail->isHTML(true);
        //$mail->addAttachment($file);
        if (!$mail->send()) {
            Dever::alert("Mailer Error: " . $mail->ErrorInfo);
        } else {
            return 'ok';
        }
    }
    #期权统计
    public function option_api(){
        #主账户数量
        $data['account'] = Dever::db('option/account')->getCount(array('state'=>1));
        #所有数量
        $data['member'] = Dever::db('option/member')->getCount(array('state'=>1));
        #已认证完成
        $data['y_num'] = Dever::db('option/account')->getCount(array('audit'=>3,'state'=>1));
        #尚未任何交付的
        $data['no_num'] = Dever::db('option/bill_jiaofu')->getCount(array('status'=>2,'state'=>1));
        #原始期权待交付
        $yq_nonum = Dever::db('option/bill_jiaofu')->getCash(array('status'=>2,'type'=>2,'state'=>1));
        if($yq_nonum['total']){
            $data['yq_nonum'] = $yq_nonum['total'];
        }else{
            $data['yq_nonum'] = 0;
        }
        
        #原始期权已交付未发放
        $yq_nof = Dever::db('option/bill_jiaofu')->getCash(array('status'=>1,'type'=>2,'state'=>1));
        
        if($yq_nof['total']){
            $data['yq_nof'] = $yq_nof['total'];
        }else{
            $data['yq_nof'] = 0;
        }
        #原始期权已发放
        $yq_fnum = Dever::db('option/bill_fafang')->getCash(array('status'=>1,'type'=>2,'state'=>1));
        if($yq_fnum['total']){
            $data['yq_fnum'] = $yq_fnum['total'];
        }else{
            $data['yq_fnum'] = 0;
        }
        
        #期权待交付
        $q_nonum = Dever::db('option/bill_jiaofu')->getCash(array('status'=>2,'type'=>1,'state'=>1));
        if($q_nonum['total']){
            $data['q_nonum'] = $q_nonum['total'];
        }else{
            $data['q_nonum'] = 0;
        }
        
        #期权已交付未发放
        $q_nof = Dever::db('option/bill_jiaofu')->getCash(array('status'=>1,'type'=>1,'state'=>1));
        if($q_nof['total']){
            $data['q_nof'] = $q_nof['total'];
        }else{
            $data['q_nof'] = 0;
        }
        
        #期权已发放
        $q_fnum = Dever::db('option/bill_fafang')->getCash(array('status'=>1,'type'=>1,'state'=>1));
        if($q_fnum['total']){
            $data['q_fnum'] = $q_fnum['total'];
        }else{
            $data['q_fnum'] = 0;
        }
        
        return Dever::render('qiquan', $data);
    }
    #代理商期权待交付	
    public function option_nopay_api($start,$end,$type=false){
        $start = Dever::input('start',$start);
        $end = Dever::input('end',$end);
        if($start && $end){
            $start = strtotime($start);
            $end = strtotime($end);
        }else{
            $start=Dever::maketime(date('Y-m-01') . ' 00:00:00');
            $end = strtotime(date('Y-m-d 23:59:59',
                strtotime(date('Y-m-01')."+1 month - 1 day")));
        }
        $where['start'] = $start;
        $where['end'] = $end;
        $where['state'] = 1;
        $rest = array();
        $rest['start'] = date('Y-m-d',$start);
        $rest['end'] = date('Y-m-d',$end);
        $rest['data'] = array();
        $data = Dever::db('agent/order')->getAll($where);
        if($data){
            foreach($data as $k => $v){
                #单独购买的软件服务费
                $cash = Dever::db('agent/soft_cash')->getTotal(array('fstart'=>$start,'fend'=>$end,'mid'=>$v['mid']));
                 $data[$k]['buy_cash'] = $cash['total'];
                 $option = Dever::db('agent/soft_cash')->getPrice(array('fstart'=>$start,'fend'=>$end,'mid'=>$v['mid']));
                 $data[$k]['y_price'] = $option['total'];
            }
            $rest['page'] = Dever::page("current");
            $rest['data'] = $data;
            if($type == 1){
                return $rest['data'];
            }else{
                return Dever::render('option_nopay', $rest);
            }
        }else{
            return Dever::render('option_nopay', $rest);
        }
        
        
    }
    #代理商期权待交付导出
    public function out_option_nopay_api(){
        $start = Dever::input('start');
        $end = Dever::input('end');
        if($start && $end){
            $start = strtotime($start);
            $end = strtotime($end);
        }else{
            $start=Dever::maketime(date('Y-m-01') . ' 00:00:00');
            $end = strtotime(date('Y-m-d 23:59:59',
                strtotime(date('Y-m-01')."+1 month - 1 day")));
        }
        $data = $this->option_nopay_api($start,$end,1);
        $header = array('用户姓名', '手机号', '身份证号', '软件服务费(订单)', '软件服务费(购买)', '原始期权', '期权赠送');
        if($data){
            $body = array();
            foreach($data as $k => $v){
                $d = array
                (
                    $v['name'],
                    $v['mobile'],
                    $v['idcard'],
                    $v['soft_cash'],
                    $v['buy_cash'],
                    $v['y_price'],
                    $v['option_price'],
                );
                $body[] = $d;
            }
            $file = '代理商期权待交付';
            Dever::excelExport($body, $header, $file);
        }
    }
    #代理商期权发放统计
    public function option_grant_api($start,$end,$type=false){
        $start = Dever::input('start',$start);
        $end = Dever::input('end',$end);
        if($start && $end){
            $start = strtotime($start);
            $end = strtotime($end);
        }else{
            $start=Dever::maketime(date('Y-m-01') . ' 00:00:00');
            $end = strtotime(date('Y-m-d 23:59:59',
                strtotime(date('Y-m-01')."+1 month - 1 day")));
        }
        $data = array();
        $data['start'] = date('Y-m-d',$start);
        $data['end'] = date('Y-m-d',$end);
        $res = Dever::db('agent/member')->getAll(array('start'=>$start,'end'=>$end,'state'=>1));
        foreach($res as $k=>$v){
            $role = Dever::db('setting/role')->find($v['role']);
            $res[$k]['role_name'] = $role['name'];
            $res[$k]['cv_num'] = Dever::db('agent/member')->getNum(array('parent_mid'=>$v['id'],'fstart'=>$start,'fend'=>$end,'role'=>8,'state'=>1));
            $res[$k]['ck_num'] = Dever::db('agent/member')->getNum(array('parent_mid'=>$v['id'],'fstart'=>$start,'fend'=>$end,'role'=>6,'state'=>1));
            $res[$k]['cash'] = Dever::db('bill/cash')->getCash(array('mid'=>$v['id'],'type'=>'1,2','state'=>1));
        }
        $data['page'] = Dever::page("current");
        $data['data'] = $res;
        if($type == 1){
            return $res;
        }else{
            return Dever::render('option_grant', $data);
        }
    }
    #期权发放导出
    public function out_option_grant_api(){
        $start = Dever::input('start');
        $end = Dever::input('end');
        if($start && $end){
            $start = strtotime($start);
            $end = strtotime($end);
        }else{
            $start=Dever::maketime(date('Y-m-01') . ' 00:00:00');
            $end = strtotime(date('Y-m-d 23:59:59',
                strtotime(date('Y-m-01')."+1 month - 1 day")));
        }
        $data = $this->option_grant_api($start,$end,1);
        $header = array('用户姓名', '手机号', '身份证号', '角色', '完成团队业绩', '完成直推创v', '完成直推创客');
        if($data){
            $body = array();
            foreach($data as $k => $v){
                $d = array
                (
                    $v['name'],
                    $v['mobile'],
                    $v['idcard'],
                    $v['role_name'],
                    $v['group_sell'],
                    $v['cv_num'],
                    $v['ck_num'],
                );
                $body[] = $d;
            }
            $file = '代理商期权发放';
            Dever::excelExport($body, $header, $file);
        }
    }
    #软件服务费数据统计
    public function soft_cash_api(){
        $start = Dever::input('start');
        $end = Dever::input('end');
        $where = array();
        if($start && $end){
            $where['fstart'] = strtotime($start.'00:00:00');
            $where['fend'] = strtotime($end.'23:59:59');
        }else{
            $where['fstart']=Dever::maketime(date('Y-m-01') . ' 00:00:00');
            $where['fend'] = strtotime(date('Y-m-d 23:59:59',
                strtotime(date('Y-m-01')."+1 month - 1 day")));
        }
        $where['state'] = 1;
        $data = array();
        $data['start'] = date('Y-m-d',$where['fstart']);
        $data['end'] = date('Y-m-d',$where['fend']);
        #软件服务费的已确认数量
        $where['soft_cash']=1;
        $where['status'] = 4;
        $order = Dever::db('agent/order')->getSoftNum($where);
        $where['status'] = 3;
        $soft_cash = Dever::db('agent/soft_cash')->getSoftNum($where);
        $data['num'] = $order + $soft_cash;
        #软件服务费未确认的数量
        $where['status'] = '-2,-1,1,2,3';
        $order = Dever::db('agent/order')->getSoftNoNum($where);
        $where['status'] = '1,2';
        $soft_cash = Dever::db('agent/soft_cash')->getSoftNoNum($where);
        $data['no_num'] = $order + $soft_cash;
        #总数量
        $data['total_num'] = $data['num'] + $data['no_num'];
        
        #已确认软件服务费
        $where['status'] = 4;
        $order = Dever::db('agent/order')->getSoftCash($where);
        $where['status'] = 3;
        $soft_cash = Dever::db('agent/soft_cash')->getSoftCash($where);
        $data['money'] = $order['total'] + $soft_cash['total'];
        #未确认软件服务费
        $where['status'] = '-2,-1,1,2,3';
        $order = Dever::db('agent/order')->getSoftNoCash($where);
        $where['status'] = '1,2';
        $soft_cash = Dever::db('agent/soft_cash')->getSoftNoCash($where);
        $data['no_money'] = $order['total'] + $soft_cash['total'];
        #总钱数
        $data['total_money'] = $data['money'] + $data['no_money'];
        #合同已确认数量:
        $where['status'] = 2;
        $data['a_num'] = Dever::db('agent/member_agreement')->getNum($where);
        #合同未确认数量:
        $where['status'] = 2;
        $data['a_nonum'] = Dever::db('agent/member_agreement')->getNoNum($where);
        $data['a_totalnum'] = $data['a_num'] + $data['a_nonum'];
        #合同已确认软件服务费
        $where['status'] = 2;
        $price = Dever::db('agent/member_agreement')->getPrice($where);
        if($price['total']){
            $data['a_price'] = $price['total'];
        }else{
            $data['a_price'] = 0;
        }
        
        #合同未确认软件服务费
        $where['status'] = 2;
        $noprice = Dever::db('agent/member_agreement')->getNoPrice($where);
        if($noprice['total']){
            $data['a_noprice'] = $noprice['total'];
        }else{
            $data['a_noprice'] = 0;
        }
        
        #总钱数
        $data['total_price'] = $data['a_price'] + $data['a_noprice'];
        return Dever::render('soft_cash', $data);
    }
    #区域分润
    public function area_api(){
        $month = Dever::input('month', date('Y-m'));
        $start_time = $month .'-01 00:00:00';
        $where = array();
        $end_time = date('Y-m-d', strtotime($start_time. ' +1 month -1 day')).' 23:59:59';
        $where['start'] = Dever::maketime($start_time);
        $where['end'] = Dever::maketime($end_time);
        $where['state'] = 1;
        
        $data = array();
        $shop = Dever::db('shop/info')->getAreaAll(array('state'=>1));
        foreach($shop as $k=>$v){
            $data[$v['city']]=$v;
        }
        $data = array_values($data);
        foreach($data as $k => $v){
            $w['month'] = $where['start'];
            #城市
            if($v['city']>0){
                $where['area'] = $v['city'];
                $data[$k]['citys']['area'] = $v['province'].','.$v['city'];
                $data[$k]['citys']['money'] = Dever::db('shop/buy_order')->getSerachMoney($where);

                $data[$k]['citys']['price']= round($data[$k]['citys']['money']['total']) * 0.3*0.01;
                $mid = Dever::db('agent/member')->getAreaAll($where);
                $w['citys']['area'] = $v['city'];
                $data[$k]['citys']['mid'] = $mid['id'];
                $where['city'] = $v['city'];
                $data[$k]['citys']['num'] = Dever::db('shop/info')->getSerachNum($where);
                #体验店
                $t_shop = Dever::db('shop/info')->getAreaAll(array('city'=>$v['city'],'type'=>1,'state'=>1));
                $data[$k]['citys']['type'] = array_column($t_shop,'id');
                $where['type_id'] = implode(',',$data[$k]['citys']['type']);
                $data[$k]['citys']['t_num'] = count($t_shop);
                if($data[$k]['citys']['t_num']>0){
                    $data[$k]['citys']['t_price'] = Dever::db('shop/buy_order')->getSerachMoney($where);
                    
                }else{
                    $data[$k]['citys']['t_price'] = 0;
                }

                $w['mid'] = $data[$k]['citys']['mid'];
                $w['area'] = $data[$k]['citys']['area'];
                $w['price'] = $data[$k]['citys']['price'];
                $w['c_price'] = $data[$k]['citys']['money']['total'];
                $w['num'] = $data[$k]['citys']['num'];
                $w['t_num'] = $data[$k]['citys']['t_num'];
                $w['t_price'] = $data[$k]['citys']['t_price']['total'];
                $shop = Dever::db('mail/area_stat')->find(array('month'=>$w['month'],'area'=>$data[$k]['citys']['area']));
                if(!$shop){
                    Dever::db('mail/area_stat')->insert($w);
                }else{
                    $w['where_id'] = $shop['id'];
                    Dever::db('mail/area_stat')->update($w);
                }
            }
            
            #区县
            if($v['county']>0){
                $where['area'] = $v['county'];
                $data[$k]['countys']['area'] = $v['province'].','.$v['city'].','.$v['county'];
                $data[$k]['countys']['money'] = Dever::db('shop/buy_order')->getSerachMoney($where);

                $data[$k]['countys']['price']= round($data[$k]['countys']['money']['total']) * 0.3*0.03;
                $mid = Dever::db('agent/member')->getAreaAll($where);
                $data[$k]['countys']['mid'] = $mid['id'];
                $wh['county'] = $v['county'];
                $data[$k]['countys']['num'] = Dever::db('shop/info')->getSerachNum($wh);
                // #体验店
                $t_shop = Dever::db('shop/info')->getAreaAll(array('county'=>$v['county'],'type'=>1,'state'=>1));
                $data[$k]['countys']['type'] = array_column($t_shop,'id');
                $where['type_id'] = implode(',',$data[$k]['countys']['type']);
                $data[$k]['countys']['t_num'] = count($t_shop);
                if($data[$k]['countys']['t_num']>0){
                    $data[$k]['countys']['t_price'] = Dever::db('shop/buy_order')->getSerachTMoney($where);
                }else{
                    $data[$k]['countys']['t_price'] = 0;
                }

                $w['mid'] = $data[$k]['countys']['mid'];
                $w['area'] = $data[$k]['countys']['area'];
                $w['price'] = $data[$k]['countys']['price'];
                $w['num'] = $data[$k]['countys']['num'];
                $w['c_price'] = $data[$k]['countys']['money']['total'];
                $w['t_num'] = $data[$k]['countys']['t_num'];
                $w['t_price'] = $data[$k]['countys']['t_price']['total'];
                $shop = Dever::db('mail/area_stat')->find(array('month'=>$w['month'],'area'=>$data[$k]['countys']['area']));
                if(!$shop){
                    Dever::db('mail/area_stat')->insert($w);
                }else{
                    $w['where_id'] = $shop['id'];
                    Dever::db('mail/area_stat')->update($w);
                }
            }
            #街道
            if($v['town']>0){
                $where['area'] = $v['town'];
                $data[$k]['towns']['area'] = $v['province'].','.$v['city'].','.$v['county'].','.$v['town'];
                $data[$k]['towns']['money'] = Dever::db('shop/buy_order')->getSerachMoney($where);
                $data[$k]['towns']['price']= round($data[$k]['towns']['money']['total']) * 0.3*0.05;
                $mid = Dever::db('agent/member')->getAreaAll($where);
                $data[$k]['towns']['mid'] = $mid['id'];
                $whe['town'] = $v['town'];
                $data[$k]['towns']['num'] = Dever::db('shop/info')->getSerachNum($whe);
                #体验店
                $t_shop = Dever::db('shop/info')->getAreaAll(array('town'=>$v['town'],'type'=>1,'state'=>1));
                $data[$k]['towns']['type'] = array_column($t_shop,'id');
                $where['type_id'] = implode(',',$data[$k]['towns']['type']);
                $data[$k]['towns']['t_num'] = count($t_shop);
                if($data[$k]['towns']['t_num']>0){
                    $data[$k]['towns']['t_price'] = Dever::db('shop/buy_order')->getSerachTMoney($where);
                }else{
                    $data[$k]['towns']['t_price'] = 0;
                }

                $w['mid'] = $data[$k]['towns']['mid'];
                $w['area'] = $data[$k]['towns']['area'];
                $w['price'] = $data[$k]['towns']['price'];
                $w['num'] = $data[$k]['towns']['num'];
                $w['c_price'] = $data[$k]['towns']['money']['total'];
                $w['t_num'] = $data[$k]['towns']['t_num'];
                $w['t_price'] = $data[$k]['towns']['t_price']['total'];
                $shop = Dever::db('mail/area_stat')->find(array('month'=>$w['month'],'area'=>$data[$k]['towns']['area']));
                if(!$shop){
                    Dever::db('mail/area_stat')->insert($w);
                }else{
                    $w['where_id'] = $shop['id'];
                    Dever::db('mail/area_stat')->update($w);
                }
            }
        }
        return 'ok';
    }
    public function area_member($id){
        $info = Dever::db('mail/area_stat')->find($id);
        if($info['mid']){
            $member = Dever::db('agent/member')->find($info['mid']);
            $res = $member['name'].'<br/>'.$member['mobile'];
        }else{
            $res = '-';
        }
        return $res;
    }
}