<?php

namespace Active\Lib;

use Dever;
Class Excel {
	#活动订单的导出
  public function out_active_order($data){
    $header = array('订单单号', '姓名', '手机号', '活动名称', '购买数量', '使用数量','待核销数量','已核销数量','金额', '已退款数量', '已退款金额','支付状态', '支付类型','购买时间');
    $body = array();
    foreach ($data as $k => $v){
            $user = Dever::db('active/user')->find($v['uid']);
    	$member = Dever::load('active/lib/user')->agent($user);
    	$active = Dever::db('active/info')->find($v['active_id']);
    	$member_name = '';
    	$member_mobile = '';
    	if($user){
    		$member_name = $user['name'];
    		$member_mobile = $user['mobile'];
    	}
    	$active_name = '';
    	if($active && $active['name']){
    		$active_name = $active['name'];
    	}
    	$config_status = Dever::db('active/order')->config['config_status'];
    	$status = Dever::status($config_status,$v['status']);
    	$config_pay_type = Dever::db('active/order')->config['config_pay_type'];
    	$pay_type = Dever::status($config_pay_type,$v['pay_type']);
    	$cdate = '';
    	if($v['cdate']){
    		$cdate = date('Y-m-d H:i',$v['cdate']);
    	}
        $code_num = '';
        if ($v['code_num']<0){
            $code_num = 0;
        } else {
            $code_num = $v['code_num'];
        }
    	$yhecode = Dever::db('active/code')->getExcel(array('order_id' => $v['id'],'status'=>2));
        $dhecode = Dever::db('active/code')->getExcel(array('order_id' => $v['id'],'status'=>1));
        $tui_num = 0;
        if ($v['refund_num']) {
            $tui_num = $v['refund_num'];
        }
        $tui_money = 0;
        $tui = Dever::db('active/info_refund')->getEmoney(array('order_id'=>$v['id'],'status'=>2));
        if ($tui && isset($tui['price']) && $tui['price']) {
            $tui_money = $tui['price'];
        }
    	$d = array
    	(
    		$v['order_num'],
    		$member_name,
    		$member_mobile,
    		$active_name,
    		$v['num'],
            $code_num,
            $dhecode,
            $yhecode,
    		$v['price'],
            $tui_num,
            $tui_money,
    		$status,
    		$pay_type,
    		$cdate,
    	);
    	$body[] = $d;
    }
    $file = Dever::input('excel_name');
    return Dever::excelExport($body, $header, $file);
  }
    #活动核销码导出
    public function out_active_code($data)
    {
        $header = array('活动名称', '参加人姓名', '参加人手机号', '购买人姓名', '购买人手机号', '领取日期','座位号','核销状态');
        $body = array();
        foreach ($data as $k => $v) {
            $active_name = '';
            if ($v['active_id']) {
                $active = Dever::db('active/info')->find($v['active_id']);
                $active_name = $active['name'];
            }
            $join_name = '';
            $join_mobile = '';
            if ($v['join_uid']) {
                $user = Dever::db('active/user')->find($v['join_uid']);
                if ($user){
                    $join_name = $user['name'];
                    $join_mobile = $user['mobile'];
                }
            }
            $buy_name = '';
            $buy_mobile = '';
            if ($v['buy_uid']) {
                $user = Dever::db('active/user')->find($v['buy_uid']);
                if ($user){
                    $buy_name = $user['name'];
                    $buy_mobile = $user['mobile'];
                }
            }
            $cdate = '';
            if ($v['cdate']) {
                $cdate = date('Y-m-d H:i:s',$v['cdate']);
            }
            $seat_num = '';
            if ($v['seat_num']) {
                $seat_num = $v['seat_num'];
            }
            $status_name = '';
            if ($v['status'] == 1) {
                $status_name = '待核销';
            } elseif ($v['status'] == 2) {
                $status_name = '已核销';
            } elseif ($v['status'] == 3) {
                $status_name = '已作废';
            }
            $d = array
            (
                $active_name,
                $join_name,
                $join_mobile,
                $buy_name,
                $buy_mobile,
                $cdate,
                $seat_num,
                $status_name,
            );
            $body[] = $d;
        }
        $file = Dever::input('excel_name');
        return Dever::excelExport($body, $header, $file);
    }
    public function import($file)
    {
        $file = Dever::local($file);
        $data = Dever::excelImport($file,0);
        // print_R($data);die;
        if ($data) {
            $res = array();
            foreach($data as $k=>$v){
                if ($k>=2) {
                    
                    $active_name = $v['A'];
                    $mobile = trim($v['C']);
                    // $seat_num = $v['G'];
                    if (isset($v['G']) && $v['G']) {
                        $active = Dever::db('active/info')->find(array('name'=>$active_name));
                        $user = Dever::db('active/user')->find(array('mobile'=>$mobile));
                        $code = Dever::db('active/code')->find(array('active_id'=>$active['id'],'join_uid'=>$user['id']));
                        $where['where_id'] = $code['id'];
                        $where['seat_num'] = $v['G'];
                        Dever::db('active/code')->update($where);
                    }
                }
                
            }
        }
    }
    public function info_off($file)
    {
        $file = Dever::local($file);
        $data = Dever::excelImport($file,0);
        if ($data) {
            $res = array();
            foreach ($data as $k => $v) {
                if ($k >= 2) {
                    $where['name'] = $v['A'];
                    $where['mobile'] = $v['B'];
                    if ($where['mobile']) {
                        $where['pass'] = substr($where['mobile'],-6);
                    }
                    $info = Dever::db('active/info_off')->find(array('mobile'=>$where['mobile'],'clear'=>true));
                    
                    if (!$info){
                        Dever::db('active/info_off')->insert($where);
                    } else {
                         if ($info && $info['pass'] != sha1(substr($info['mobile'],-6))) {
                            $w['where_id'] = $info['id'];
                            $w['pass'] = substr($info['mobile'],-6);
                            Dever::db('active/info_off')->update($w);
                         }
                    }
                }
            }
        }
    }
}