<?php
/**
 * 公共管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Admin\Controller;
use Common\Controller\HanchaoController;
class PublicController extends HanchaoController{
    public function _initialize()
    {
        parent::_initialize();
        $this->assign('admin_info', session('admin_info'));
        if (!$this->admin_info) {
            redirect(U('Login/index'));
        }
		check_shop_num();
    }
    /** 后台主页*/
    public function main()
    {
        $code = M('Sms')->where('status = 1')->getField('code');
        $smsclass = new \Common\Vendor\Sms($code);
        $result=$smsclass->balance();
        $this->assign('shu',$result['count']);
        $security_info = array();
        if (file_exists(__ROOT__ . "/install.php")) {
            $security_info[] = "强烈建议删除安装文件夹,点击<a href='" . U('Public/delete_install') . "'>【删除】</a>";
        }
        if (APP_DEBUG == true) {
            $security_info[] = "强烈建议您网站上线后，建议关闭 DEBUG （前台错误提示）";
        } else {
            $security_info[] = "欢迎使用汉潮B2B2C多用户商城系统!";
        }
        if (count($security_info) <= 0) {
            $this->assign('no_security_info', 0);
        } else {
            $this->assign('no_security_info', 1);
        }
        $this->assign('security_info', $security_info);
        //首页数据统计
        $this->statistics();
        $this->info();
        //会员折线统计
        $this->member_trend();
        //订单折线统计
        $this->order_trend();
        //注册数统计
        $this->num_count();
        //获取正在进行的活动
        $this->acting();
        //系统信息
        $server_info = array(
	      '授权信息' => "<span id=\"hanchao_authname\"></span>",
	      '程序版本' => HANCHAO_VERSION.' '.HANCHAO_REVISION." 更新时间 : ".HANCHAO_RELEASE." <span id=\"hanchao_authname\"></span>",
	      '操作系统' => PHP_OS, //PHP_OS,
	      'PHP版本' => PHP_VERSION,
	      '开发团队'=>'石家庄久途网络科技有限公司',
	      );
        $this->assign('server_info', $server_info);
        //获取站点信息
        $site_info=session('site_info');
        $site_info['show_time']=1;
        if($site_info['is_try']==1){
            if(NOW_TIME>$site_info['end_time']){
                $site_info['pack_name']='尝鲜版';
                $site_info['show_time']=0;
            }else{
                $site_info['pack_name']='体验版';
                $site_info['show_time']=0;
            }
        }else{
            if(NOW_TIME>$site_info['end_time']){
                $site_info['pack_name']='尝鲜版';
            }else {
                $site_info['pack_name'] = $site_info['pack_name'];
            }
        }
        $this->assign($site_info);
        $this->display();
    }
    public function info(){
        $map_zc=$map_in=$map_t=$map=$map_c=$map_g=array();
        $start_time = strtotime(date('Y-m-d',NOW_TIME));
        $date_from=$start_time;
        $date_to =$start_time+86399;
        $map['status'] = array('in','1,2,3,6,8');
        $map['pay_status'] = 3;
        $map['type'] = array('in','1,6,11,12,13,14');
        $map['is_delete']=0;
        $map['addtime']=array('between',array($date_from,$date_to));
        $map_t['addtime']=array('between',array($date_from,$date_to));
        $map_c['addtime']=array('between',array($date_from,$date_to));
        $map_g['sell_time']=array('between',array($date_from,$date_to));
        $map_in['change_time']=array('between',array($date_from,$date_to));
        $map_t['status']=1;
        $map_t['is_delete']=0;
        $tui=M('RefundOrder')->where($map_t)->field('sum(money) as tui_money,count(id) as tui_num')->find();//退款
        $this->assign($tui);
        $map['in_online']=0;
        $xs_order=M('OrderInfo')->where($map)->field('sum(order_money) as xs_money,count(id) as xs_num')->find();//线上订单
        $this->assign($xs_order);

        $map_c['pay_status'] = 3;
        $map_c['type'] = array('in','2,5');
        $chong=M('OrderInfo')->where($map_c)->field('sum(order_money) as c_money,count(id) as c_num')->find();//会员充值
        $this->assign($chong);
        unset($map_c['in_online']);

        //总收入
        $map_in['change_type']=0;
        $map_in['type']=1;
        $income=M('ShopbalanceLog')->where($map_in)->sum('change_num');
        //总支出
        $map_in['change_type']=1;
        $map_in['type']=1;
        $outcome=M('ShopbalanceLog')->where($map_in)->sum('change_num');
        $this->assign('income',$income?$income:0);
        $this->assign('outcome',$outcome?$outcome:0);
    }
    //首页数据统计
    private function statistics(){
        $today=strtotime(date('Y-m-d',NOW_TIME));
        $tomorrow=$today+86400;
        $yesterday=$today-86400;

        //今日新增销售额\今日新增订单
        $today_order_map['type'] = array('IN','1,3,6,11,12,13,14');
        $today_order_map['pay_time'] = array('between',array($today,$tomorrow));
        $today_order=M('OrderInfo')->where($today_order_map)->field('count(id) as today_order_count,sum(order_money) as order_money')->find();
        $this->assign($today_order);

        //昨日新增销售额\昨日新增订单
        $yesterday_order_map['type'] = array('IN','1,3,6,11,12,13,14');
        $yesterday_order_map['pay_time'] = array('between',array($yesterday,$today));
        $yesterday_order=M('OrderInfo')->where($yesterday_order_map)->field('count(id) as yesterday_order_count,sum(order_money) as yesterday_order_money')->find();
        $this->assign($yesterday_order);

        //今日新增会员
        $map=array();
        $map['reg_time'] = array('between',array($today,$tomorrow));
        $member_count=M('Member')->where($map)->count();
        $this->assign('member_count',$member_count);

        //昨日新增会员
        $map=array();
        $map['reg_time'] = array('between',array($yesterday,$today));
        $yesterday_member_count=M('Member')->where($map)->count();
        $this->assign('yesterday_member_count',$yesterday_member_count);

        //库存预警
        $map=array();
        $map['_string']='a.goods_number > g.limit_count or a.goods_number < g.lower_count';
        $map['g.limit_count']=array('neq',0);
        $map['g.lower_count']=array('neq',0);
        $map['g.is_delete']=0;
        $join='LEFT JOIN __GOODS__ g ON g.id=a.goods_id';
        $stock_count=M('ShopsGoodsStock')->alias('a')->where($map)->join($join)->count();
        $this->assign('stock_count',$stock_count);

        //待付款
        $map=array();
        $map['type']= array('IN','6,14,3');
        $map['pay_status']=array('neq',3);
        $map['status']=1;
        $map['is_delete']=0;
        $map['shipping_status']=1;
        $unpay_count=M('OrderInfo')->where($map)->count();
        $this->assign('unpay_count',$unpay_count);
        //待备货
        $map=array();
        $map['type']= array('IN','6,14,3');
        $map['pay_status']=3;
        $map['status']=2;
        $map['shipping_status']=1;
        $map['is_delete']=0;
        $pre_count=M('OrderInfo')->where($map)->count();
        $this->assign('pre_count',$pre_count);
        //待发货
        $map=array();
        $map['type']= array('IN','6,14,3');
        $map['pay_status']=3;
        $map['status']=2;
        $map['shipping_status']=5;
        $map['is_delete']=0;
        $unsend_count=M('OrderInfo')->where($map)->count();
        $this->assign('unsend_count',$unsend_count);
        //待退货退款
        $map=array();
        $map['type']= array('IN','6,14,3');
        $map['pay_status']=3;
        $map['status']=array('in','6,8');
        $map['is_delete']=0;
        $return_count=M('OrderInfo')->where($map)->count();
        $this->assign('return_count',$return_count);
        //待提现
        $map=array();
        $map['status']=0;
        $withdrawals_count=M('MemberWithdrawals')->where($map)->count();
        $this->assign('withdrawals_count',$withdrawals_count);

        //待发货订单
        $order_fh_map['type'] = array('IN','6,13,14,3');
        $order_fh_map['goods_allmoney'] = array('gt',0);
        $order_fh_map['send_type']=array('neq',1);
        $order_fh_map['shipping_status'] = array('in','1,2,5');
        $order_fh_map['status'] = 2;
        $order_fh_map['pay_status'] = 3;
        $order_fh_count=M('OrderInfo')->where($order_fh_map)->count('id');
        $this->assign('order_fh_count',$order_fh_count);
        //网上销售总金额,总订单
        $all_money_map['type'] = array('IN','1,3,6,14');
        $all_money_map['pay_time'] = array('between',array($today,$tomorrow));
        $all_money_map['status'] = array('in','1,2,3,6,8');
        $all_money=M('OrderInfo')->where($all_money_map)->field('sum(order_money) as all_money,count(id) as all_num')->find();
        $this->assign($all_money);
        //今日新增充值数
        $today_cz_map['type'] = array('IN','2');
        $today_cz_map['pay_time'] = array('between',array($today,$tomorrow));
        $today_cz_count=M('OrderInfo')->where($today_cz_map)->count('id');
        $this->assign('today_cz_count',$today_cz_count);
        //今日新增充值金额
        $today_cz_money_map['type'] = array('IN','2');
        $today_cz_money_map['pay_time'] = array('between',array($today,$tomorrow));
        $today_cz_money=M('OrderInfo')->where($today_cz_money_map)->sum('order_amount');
        $this->assign('today_cz_money',$today_cz_money);
        //货流通知
        $map=array();
        $map['status']=array('lt',2);
        $freight_num=M('InvoicingOrder')->where($map)->count();
        $this->assign('freight_num',$freight_num);
    }
    public function acting(){
        //会员特权
        $hytq_map['status'] = 1;
        $hytq_map['begin_time'] = array('LT',NOW_TIME);
        $hytq_map['_string'] = 'end_time >'.NOW_TIME.' or end_time = 0';
        $hytq = M('ManypointsRule')->where($hytq_map)->count('id');
        $this->assign('hytq',$hytq);
        //小票抽奖
        $xpcj_map['status'] = 1;
        $xpcj_map['begin_time'] = array('LT',NOW_TIME);
        $xpcj_map['end_time'] = array('GT',NOW_TIME);
        $xpcj = M('ReceiptDraw')->where($xpcj_map)->count('id');
        $this->assign('xpcj',$xpcj);
        //仓库限时促销
        $xscx_map['is_group'] = 0;
        $xscx_map['is_limit_time'] = 1;
        $xscx_map['status'] = 1;
        $xscx_map['start_time'] = array('LT',NOW_TIME);
        $xscx_map['end_time'] = array('GT',NOW_TIME);
        $xscx = M('PromotAct')->where($xscx_map)->count('id');
        $this->assign('xscx',$xscx);
        //仓库代金券
        $mddjq_map['bonu_type'] = 2;
        $mddjq_map['use_start_date'] = array('LT',NOW_TIME);
        $mddjq_map['use_end_date'] = array('GT',NOW_TIME);
        $mddjq = M('Bonu')->where($mddjq_map)->count('id');
        $this->assign('mddjq',$mddjq);
        //满减活动
        $mdmj_map['id'] = 2;
        $mdmj = M('TakeoutPromote')->where($mdmj_map)->count('id');
        $this->assign('mdmj',$mdmj);
        //首单立减
        $sdlj_map['id'] = 1;
        $sdlj = M('TakeoutPromote')->where($sdlj_map)->count('id');
        $this->assign('sdlj',$sdlj);
        //全民发券
        $qmfq_map['start_time'] = array('LT',NOW_TIME);
        $qmfq_map['end_time'] = array('GT',NOW_TIME);
        $qmfq = M('ReceiptDraw')->where($qmfq_map)->count('id');
        $this->assign('qmfq',$qmfq);
    }
    private function member_trend(){
        $map=array();
        $time=quick_time(7);
        $map['reg_time']=array('between',array($time[0],$time[1]));
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        for($i=0;$i<7;$i++){
            $day_arr[date('Y-m-d',$now_time-$i*86400)] = date('Y-m-d',$now_time-$i*86400); //每隔一天赋值给数组
        }
        //会员
        $map['status'] = 1;
        $map['is_tmp'] = 0;
        $info['member'] = M('Member')->where($map)->field('count(*) as num,reg_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(reg_time),'%Y年%m月%d天')")->select();
        unset($map['is_tmp']);
        //粉丝
        $map['is_follow'] = 1;
        $info['fans'] = M('Member')->where($map)->field('count(*) as num,reg_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(reg_time),'%Y年%m月%d天')")->select();
        $name = array("粉丝","会员");
        if($info['member'] || $info['fans']){
            foreach($info as $key => $value){
                if(!empty($value)){
                    foreach($value as $k=>$v){
                        $file[$key][date('Y-m-d',$v['mon'])]=$v['num'];
                        $file_time[$key][]=date('Y-m-d',$v['mon']);
                    }
                }else{
                    $file[$key][date('Y-m-d',$v['mon'])] =0;
                    $file_time[$key][]=date('Y-m-d',$v['mon']);
                }
            }unset($value,$key,$k,$v);
        }
        $new_day=array_diff($day_arr,$file_time['member']);
        foreach($new_day as $v){
            $file['member'][$v]=0;
        }unset($v);
        $new_fans_day=array_diff($day_arr,$file_time['fans']);
        foreach($new_fans_day as $v){
            $file['fans'][$v]=0;
        }unset($v);
        ksort($file['member']);
        ksort($file['fans']);
        //得到折线图
        $cz_count = array(
            "legend"=>array('data'=>array($name[0],$name[1])),
            "xaxis"=>array("type"=>"category","data"=>array_keys($file['member'])),
            "series"=>array(
                0=>array("name"=>$name[0],"type"=>"bar","stack"=>$name[0],"data"=>array_values($file['fans'])),
                1=>array("name"=>$name[1],"type"=>"bar","stack"=>$name[1],"data"=>array_values($file['member'])),
            ),
        );
        $ec = new \Common\Vendor\Echarts();
        $this->assign('Area_count_member',$ec->show('Area_count_member',$cz_count,'bar','新增会员趋势分析'));
    }
    //订单统计折线图
    private function order_trend(){
        $time=quick_time(7);
        $map['addtime']=array('between',array($time[0],$time[1]));
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        for($i=0;$i<7;$i++){
	        $day_arr[date('Y-m-d',$now_time-$i*86400)] = date('Y-m-d',$now_time-$i*86400); //每隔一天赋值给数组
        }
        $file_profit=$file_money=$file_time=$file_num=$new_day=$info=array();
        $map['status'] = array('in','1,2,3,6,8');
        $map['pay_status'] = 3;
        $map['type'] = array('in','1,6,11,12,13,14');
        $map['is_delete']=0;
        $info=M('OrderInfo')->where($map)->field("count(id) as number,sum(order_money) as money,sum(profit) as profit,DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d') as mon")->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d')")->select();
        //每个折线的图标代表什么
        $name = array("订单数","订单金额","利润");
        if($info){
            foreach($info as $key => $value){
                if(!empty($value)){
                    $file_num[$value['mon']]=$value['number'];
                    $file_time[$value['mon']]=$value['mon'];
                    $file_money[$value['mon']]=$value['money'];
                    $file_profit[$value['mon']]=$value['profit'];
                }
            }unset($value,$key);
        }
        $new_day=array_diff($day_arr,$file_time);
        foreach($new_day as $v){
	  $file_num[$v]=0;
	  $file_money[$v]=0;
	  $file_profit[$v]=0;
        }unset($v);
        ksort($file_num);
        ksort($file_money);
        ksort($file_profit);
        //得到折线图
        $cz_count = array(
	  "legend"=>array('data'=>array($name[0],$name[1],$name[2])),
	  "xaxis"=>array("type"=>"category","data"=>array_keys($file_num)),
	  "series"=>array(
	      0=>array("name"=>$name[0],"type"=>"line","stack"=>$name[0],'itemStyle'=>array(),"data"=>array_values($file_num)),
	      1=>array("name"=>$name[1],"type"=>"line","stack"=>$name[1],"data"=>array_values($file_money)),
	      2=>array("name"=>$name[2],"type"=>"line","stack"=>$name[2],"data"=>array_values($file_profit)),
	  ),
        );
        $ec = new \Common\Vendor\Echarts();
        $this->assign('Area_count',$ec->show('Area_countm', $cz_count,'line','订单明细统计'));
    }
    /**
      *注册数统计
      */
    private function num_count(){
        $today=strtotime(date('Y-m-d',NOW_TIME));
        $tomorrow=$today+86400;
        for($i=0;$i<5;$i++){
            if($i>=0){
                $today=strtotime(date('Y-m-d',strtotime("-".$i." day")));
            }
            $map['follow_time']=array('between',array($today,$tomorrow));
            $map['reg_time']=array('between',array($today,$tomorrow));
            $map['is_follow']=1;
            $count[$i]['time']=$today;
            $count[$i]['f_count']=M('Member')->where($map)->count('id');
            unset($map['follow_time']);
            $map['reg_time']=array('between',array($today,$tomorrow));
            $map['is_follow']=0;
            $count[$i]['g_count']=M('Member')->where($map)->count('id');
            unset($map['is_follow']);
            $map['is_tmp'] = 0;
            $count[$i]['m_count']=M('Member')->where($map)->count('id');
            $count[$i]['sum'] = $count[$i]['f_count'] + $count[$i]['g_count'] + $count[$i]['m_count'];
        }
        $this->assign('count',$count);
    }
    public function wxtoken(){
        if(IS_POST){
            $verify_name=I('post.verify_name');
            if(empty($verify_name)){$this->error('请填写权限文件名');return;}
            if(strlen($verify_name)!=16){$this->error('权限文件名错误');return;}
            $file_name='MP_verify_'.$verify_name.'.txt';
            if(file_exists($file_name)){$this->error('权限文件已存在');return;}
            file_put_contents($file_name,$verify_name);
            if(file_exists($file_name)){
                $data=array('verify_name'=>$verify_name);
                cookie('verify_config',$data);
                $this->success('保存成功!');
            }else{
                $this->error('保存失败!');
            }
            
        }else{
            $randtoken = C('WX_TOKEN');
            if (!$randtoken) {
                $randtoken = randStr(10);
            }
            $cookie=cookie('verify_config');
            $this->assign($cookie);
            $this->assign('wxtoken', $randtoken);
            $this->display();
        }
    }
   //任务队列
	public function auto_exec(){
		$map=array();
        $map['classname']='Popup';
        $map['status']='1';
        $map['next_time']=array('elt',NOW_TIME);
		$info=M('Cron')->where($map)->field(true)->find();
        if(empty($info))return false;
		if(NOW_TIME>$info['next_time'] && NOW_TIME>=$info['start_time'] && $info['status']==1){
		     updatetask($info);             
			 $class=new \Common\Vendor\Cron\Popup;
             $ajax_box=$class->index($info);                         
             $this->ajaxReturn($ajax_box);
		}
	}
}
?>