<?php

namespace Option\Lib;

use Dever;

include('/share/lib/php/dever_package/excel/src/PHPExcel.php');

class Import
{
    # 导入账户
    public function get_api()
    {
        Dever::config('base')->hook = true;
        $file = Dever::data() . 'q.xlsx';

        $data = Dever::excelImport($file, 1, 5, 'excel');

        $table = array();
        $table['head'] = array('账户类型', '待交付', '已交付总额', '已发放剩余', '未发放', '已兑付');
        $table['body'] = array();
        $result['已导入'] = array
        (
            'type' => 'table',
            'content' => $table,
        );
        $html = Dever::show('', $result, false, false);
        echo $html;die;


        if ($data) {
            $member = array();
            foreach ($data as $k => $v) {
                $state = preg_match(Dever::rule('mobile'), $v['C']);
                if ($state) {
                    $key = $v['A'];
                    $where['key'] = $v['A'];
                    $where['mobile'] = $v['C'];
                    $where['clear'] = true;
                    $info = Dever::db('option/member')->find($where);
                    $update = $where;
                    $update['idcard'] = $v['D'];
                    $update['name'] = $v['B'];
                    $update['main'] = $v['E'] == 1 ? 1 : 2;
                    if (!$info) {
                        $update['status'] = 2;
                        $id = Dever::db('option/member')->insert($update);
                    } else {
                        $update['where_id'] = $info['id'];
                        $id = Dever::db('option/member')->update($update);
                    }
                    if ($id && $update['main'] == 1) {
                        if (!isset($member[$key])) {
                            $member[$key] = array();
                        }
                        $account = Dever::db('option/member')->find(array('mid' => $id));
                        if (!$account) {
                            $member[$key] = Dever::db('option/account')->insert(array('mid' => $id, 'audit' => 1));
                        } else {
                            $member[$key] = $account['id'];
                        }
                    }
                }
            }

            if ($member) {
                foreach ($member as $k => $v) {
                    $where = array();
                    $where['option_key'] = $k;
                    $where['set_aid'] = $v;
                    Dever::db('option/member')->updates($where);
                }
            }
        }
        $table = Dever::table($result);
        return $table;
    }

    # 导入交付记录
    public function get_jiaofu_api()
    {
        Dever::config('base')->hook = true;

        $file = Dever::data() . 'qiquan.xlsx';

        $data = Dever::excelImport($file, 2, 5, 'excel');

        if ($data) {
            foreach ($data as $k => $v) {
                $state = preg_match(Dever::rule('mobile'), $v['A']);
                if ($state && $v['C'] > 0) {
                    $where['mobile'] = $v['A'];
                    $type_name = $v['D'];
                    if ($type_name == '期权') {
                        $type = 1;
                    } else {
                        $type = 4;
                    }
                    $info = Dever::db('option/member')->find($where);
                    if ($info) {
                        $account = Dever::db('option/account')->find($info['aid']);
                        if ($account && $account['status'] < 3) {
                            Dever::load('option/lib/cash')->up($info['id'], $account['id'], $type, $v['C'], $v['E'], false);
                        }
                    }
                }
            }
        }
        return 'ok';
    }

    # 导入发放记录
    public function get_fafang_api()
    {
        Dever::config('base')->hook = true;
        $file = Dever::data() . 'qiquan.xlsx';

        $data = Dever::excelImport($file, 3, 5, 'excel');

        if ($data) {
            foreach ($data as $k => $v) {
                $state = preg_match(Dever::rule('mobile'), $v['A']);
                if ($state && $v['C'] > 0) {
                    $where['mobile'] = $v['A'];
                    $type_name = $v['D'];
                    if ($type_name == '期权') {
                        $type = 2;
                    } else {
                        $type = 5;
                    }
                    $info = Dever::db('option/member')->find($where);
                    if ($info) {
                        $account = Dever::db('option/account')->find($info['aid']);
                        if ($account && $account['status'] < 3) {
                            Dever::load('option/lib/cash')->up($info['id'], $account['id'], $type, $v['C'], $v['E'], false);
                        }
                    }
                }
            }
        }
        return 'ok';
    }
}