<?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 = [];
        if($week == 0){
            $week = 7;
        }
        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'] = 4;
            $data[$k]['num'] = Dever::db('agent/order')->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'] = 1;
        $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'] = 1;
        $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 caigou($where){
        $data = Dever::db('shop/buy_order')->getNewAll($where);
        $res = array();
        $money = 0;
        $res['money'] = 0;
        if($data && isset($data['price']) && $data['price']){
            $res['price'] = $data['price'];
        }else{
            $res['price'] = 0;
        }
        
        foreach($data as $k => $v){
            // $goods_id,$skuid
            $order_goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $v['id']));
            $c_price = 0;
            foreach($order_goods as $k1 => $v1){
                $goods = Dever::load('goods/lib/info')->getPayInfo($v1['goods_id'], $v1['sku_id']);
                if($goods['c_price'] && $goods['c_price'] >= 0){
                    $c_price += $goods['c_price'] * $v1['num'];
                }
            }
            $res['money'] += ($v['price'] - $c_price);
        }
        return $res;
    }
    #零售
    public function ls($where,$type=false){
        $shop = Dever::db('mail/area_entry')->getNewAll($where);
        $data = array();
        $data['price'] = 0;
        foreach($shop as $k => $v){
            if ($type == 1) {
                $shop[$k]['shop'] = Dever::db('shop/info')->find(array('city'=>$where['city'],'id'=>$v['shop_id']));
            } elseif ($type == 2) {
                $shop[$k]['shop'] = Dever::db('shop/info')->find(array('county'=>$where['county'],'id'=>$v['shop_id']));
            } elseif ($type == 3) {
                $shop[$k]['shop'] = Dever::db('shop/info')->find(array('town'=>$where['town'],'id'=>$v['shop_id']));
            }
            
        }
        foreach($shop as $k => $v){
            if(!$v['shop']){
                unset($shop[$k]);
            }
        }
        foreach($shop as $k=>$v){
            $data['price'] +=$v['cprice'];
        }
        $data['num'] = count($shop);
        return $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();
        $data = Dever::db('shop/info')->getAreaAll(array('state'=>1));
        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'];
                $jia = $this->caigou($where);
                // $data[$k]['citys']['money'] = Dever::db('shop/buy_order')->getSerachMoney($where);
                $data[$k]['citys']['price']= $jia['money'] * 0.01;
                // $data[$k]['citys']['price']= round($data[$k]['citys']['money']['total']) * 0.3*0.01;
                $mid = Dever::db('agent/member')->getAreaAll(array('area'=>$data[$k]['citys']['area'],'role'=>2));
                $w['citys']['area'] = $v['city'];
               
                if($mid && $mid['id']){
                    $data[$k]['citys']['mid'] = $mid['id'];
                }else{
                     $data[$k]['citys']['mid'] = '-1';
                }
                
                $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')->getSerachTMoney($where);
                    $data[$k]['citys']['t_fprice'] = $data[$k]['citys']['price'];
                }else{
                    $data[$k]['citys']['t_price'] = 0;
                    $data[$k]['citys']['t_fprice'] = 0;
                }
                #零售店
                $shop_area = $this->ls($where,1);
                $data[$k]['citys']['l_price'] =$shop_area['price'];
                $data[$k]['citys']['l_fprice'] =$shop_area['price'] * 0.01;
                $data[$k]['citys']['l_num'] = $shop_area['num'];
                $data[$k]['citys']['price'] = $data[$k]['citys']['price'] + $data[$k]['citys']['l_fprice'];
                $w['mid'] = $data[$k]['citys']['mid'];
                $w['area'] = $data[$k]['citys']['area'];
                $w['price'] = $data[$k]['citys']['price'];
                $w['c_price'] = $jia['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'];
                $w['t_fprice'] = $data[$k]['citys']['t_fprice'];
                $w['l_num'] = $data[$k]['citys']['l_num'];
                $w['l_price'] = $shop_area['price'];
                $w['l_fprice'] = $data[$k]['citys']['l_fprice'];

                $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);
                $jia = $this->caigou($where);
                $data[$k]['countys']['price']= $jia['money'] *0.03;
                // $data[$k]['countys']['price']= round($data[$k]['countys']['money']['total']) * 0.3*0.03;
                $mid = Dever::db('agent/member')->getAreaAll(array('area'=>$data[$k]['countys']['area'],'role'=>3));
                if($mid && $mid['id']){
                    $data[$k]['countys']['mid'] = $mid['id'];
                }else{
                    $data[$k]['countys']['mid'] = '-1';
                }
                $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);
                    $data[$k]['countys']['t_fprice'] = $data[$k]['countys']['price'];
                }else{
                    $data[$k]['countys']['t_price'] = 0;
                    $data[$k]['countys']['t_fprice'] = 0;
                }

                #零售店
                $where['county'] = $v['county'];
                $shop_area = $this->ls($where,2);
                $data[$k]['countys']['l_price'] = $shop_area['price'];
                $data[$k]['countys']['l_fprice'] = $shop_area['price'] * 0.03;
                $data[$k]['countys']['l_num'] = $shop_area['num'];
                $data[$k]['countys']['price'] = $data[$k]['countys']['price'] + $data[$k]['countys']['l_fprice'];

                $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'] = $jia['price'];
                // $data[$k]['countys']['money']['total'];
                $w['t_num'] = $data[$k]['countys']['t_num'];
                $w['t_price'] = $data[$k]['countys']['t_price']['total'];
                $w['t_fprice'] = $data[$k]['countys']['t_fprice'];
                $w['l_num'] = $data[$k]['countys']['l_num'];
                $w['l_price'] = $shop_area['price'];
                $w['l_fprice'] = $data[$k]['countys']['l_fprice'];
                $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);
                $jia = $this->caigou($where);
                $data[$k]['towns']['price'] = $jia['money'] * 0.05;
                // $data[$k]['towns']['price']= round($data[$k]['towns']['money']['total']) * 0.3*0.05;
                $mid = Dever::db('agent/member')->getAreaAll(array('area'=>$data[$k]['towns']['area'],'role'=>4));
                if($mid && $mid['id']){
                    $data[$k]['towns']['mid'] = $mid['id'];
                }else{
                    $data[$k]['towns']['mid'] = '-1';
                }
                
                $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);
                    $data[$k]['towns']['t_fprice'] = $data[$k]['towns']['price'];
                }else{
                    $data[$k]['towns']['t_price'] = 0;
                    $data[$k]['towns']['t_fprice'] = 0;
                }

                #零售店
                $where['town'] = $v['town'];
                $shop_area = $this->ls($where,3);
                $data[$k]['towns']['l_price'] =$shop_area['price'];
                $data[$k]['towns']['l_fprice'] =$shop_area['price'] * 0.05;
                $data[$k]['towns']['l_num'] = $shop_area['num'];
                $data[$k]['towns']['price'] = $data[$k]['towns']['price'] + $data[$k]['towns']['l_fprice'];

                $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'] = $jia['price'];
                // $data[$k]['towns']['money']['total'];
                $w['t_num'] = $data[$k]['towns']['t_num'];
                $w['t_price'] = $data[$k]['towns']['t_price']['total'];
                $w['t_fprice'] = $data[$k]['towns']['t_fprice'];
                $w['l_num'] = $data[$k]['towns']['l_num'];
                $w['l_price'] = $shop_area['price'];
                $w['l_fprice'] = $data[$k]['towns']['l_fprice'];
                $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_api(){
    //     #零售店的采购金额*0.4
    //     $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;
    //     }
    //     $shop_area = Dever::db('mail/area_entry')->getAll($where);
    //     $res = array();
    //     if($shop_area){
    //         foreach($shop_area as $k => $v){
    //             if($v['area']){
    //                $area = explode(',',$v['area']);
    //                if($area && isset($area[0])){
    //                     $res[$k]['province'] = $area[0];
    //                }
    //                if ($area && isset($area[1])){
    //                     $res[$k]['city'] = $area[1];
    //                }else{
    //                     $res[$k]['city'] = 0;
    //                }
    //                if($area && isset($area[2])){
    //                     $res[$k]['county'] = $area[2];
    //                }else{
    //                     $res[$k]['county'] = 0;
    //                }
    //                if($area && isset($area[3])){
    //                      $res[$k]['town'] = $area[3];
    //                }else{
    //                     $res[$k]['town'] = 0;
    //                }
    //             }
    //         }
    //     }
        
    //     $data = array_values($data);
    //     $data = array_merge($data,$res);
    //     // print_R($data);die;
    //     $result=array_unique($data, SORT_REGULAR);
    //     $rest=array();
    //     foreach($data as $k=>$v){
    //         $rest[$v['county']]=$v;
    //     }
    //     $data=array_values($rest);
    //     foreach ($data as $k => $v) {
    //       $edition[] = $v['province'];
    //     }
    //     array_multisort($edition, SORT_ASC, $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'];
               
    //             if($mid && $mid['id']){
    //                 $data[$k]['citys']['mid'] = $mid['id'];
    //             }else{
    //                  $data[$k]['citys']['mid'] = '';
    //             }
                
    //             $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')->getSerachTMoney($where);
                    
    //             }else{
    //                 $data[$k]['citys']['t_price'] = 0;
    //             }
    //             #零售店
    //             $entry = Dever::db('mail/area_entry')->getNum($where);
    //             if ($entry && $entry['num']){
    //                 $data[$k]['citys']['l_num'] = $entry['num'];
    //                 $data[$k]['citys']['num'] = $data[$k]['citys']['num'] + $data[$k]['citys']['l_num'];
    //             }else{
    //                 $data[$k]['citys']['l_num'] = 0;
    //             }
    //             $entry_price = Dever::db('mail/area_entry')->getPrice($where);
    //             if ($entry_price && $entry_price['cprice'] ){
    //                 $data[$k]['citys']['l_price'] =  $entry_price['cprice'] * 0.4;
    //                 $data[$k]['citys']['price'] = $data[$k]['citys']['price'] + $data[$k]['citys']['l_price'];
    //             }else{
    //                 $data[$k]['citys']['l_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'];
    //             $w['l_num'] = $data[$k]['citys']['l_num'];
    //             $w['l_price'] = $data[$k]['citys']['l_price'];

    //             $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);
    //             if($mid && $mid['id']){
    //                 $data[$k]['countys']['mid'] = $mid['id'];
    //             }else{
    //                 $data[$k]['countys']['mid'] = '';
    //             }
    //             $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;
    //             }

    //             #零售店
    //             $entry = Dever::db('mail/area_entry')->getNum($where);
    //             if ($entry && $entry['num']){
    //                 $data[$k]['countys']['l_num'] = $entry['num'];
    //                 $data[$k]['countys']['num'] = $data[$k]['countys']['num'] + $data[$k]['countys']['l_num'];
    //             }else{
    //                 $data[$k]['countys']['l_num'] = 0;
    //             }
    //             $entry_price = Dever::db('mail/area_entry')->getPrice($where);
    //             if ($entry_price && $entry_price['cprice'] ){
    //                 $data[$k]['countys']['l_price'] =  $entry_price['cprice'] * 0.4;
    //                 $data[$k]['countys']['price'] = $data[$k]['countys']['price'] + $data[$k]['countys']['l_price'];
    //             }else{
    //                 $data[$k]['countys']['l_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'];
    //             $w['l_num'] = $data[$k]['countys']['l_num'];
    //             $w['l_price'] = $data[$k]['countys']['l_price'];
    //             $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);
    //             if($mid && $mid['id']){
    //                 $data[$k]['towns']['mid'] = $mid['id'];
    //             }else{
    //                 $data[$k]['towns']['mid'] = '';
    //             }
                
    //             $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;
    //             }

    //             #零售店
    //             $entry = Dever::db('mail/area_entry')->getNum($where);
    //             if ($entry && $entry['num']){
    //                 $data[$k]['towns']['l_num'] = $entry['num'];
    //                 $data[$k]['towns']['num'] = $data[$k]['towns']['num'] + $data[$k]['towns']['l_num'];
    //             }else{
    //                 $data[$k]['towns']['l_num'] = 0;
    //             }
    //             $entry_price = Dever::db('mail/area_entry')->getPrice($where);
    //             if ($entry_price && $entry_price['cprice'] ){
    //                 $data[$k]['towns']['l_price'] =  $entry_price['cprice'] * 0.4;
    //                 $data[$k]['towns']['price'] = $data[$k]['towns']['price'] + $data[$k]['towns']['l_price'];
    //             }else{
    //                 $data[$k]['towns']['l_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'];
    //             $w['l_num'] = $data[$k]['towns']['l_num'];
    //             $w['l_price'] = $data[$k]['towns']['l_price'];
    //             $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);
        $res = '-';
        if($info['mid']){
            $member = Dever::db('agent/member')->find($info['mid']);
            if ($member) {
                $res = $member['name']."<br/>".$member['mobile'];
            }
        }
        return $res;
    }

    public function entry_member($id){
        $info = Dever::db('mail/area_entry')->find($id);
        if($info['shop_id']){
            $member = Dever::db('shop/info')->find($info['shop_id']);
            // print_R($member);die;
            $res = $member['name'].'<br/>'.$member['sid'];
        }else{
            $res = '-';
        }
        return $res;
    }

    public function updateArea_entry($id,$name,$data){
        $sid = Dever::param('sid',$data);
        $month = Dever::param('month',$data);
        if ($id && $sid) {
            $info = Dever::db('mail/area_entry')->find($id);
            $member = Dever::db('shop/info')->find(array('sid'=>$sid));
            // print_R($member);die;
            $where['where_id'] = $id ;
            $where['shop_id'] = $member['id'];
            $where['month'] = strtotime($month);
            Dever::db('mail/area_entry')->update($where);
        }
    }
}