<?php

namespace app\test\controller;

use app\common\Fun;
use QL\QueryList;
use think\Db;
use cmf\controller\HomeBaseController;

class IndexController extends HomeBaseController
{
    public function index()
    {
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment;Filename=报表.xls");
        $data = Db::table('recruit_entry')->where('rid', 6)->select()->toArray();

        $posts    = Db::table('recruit_post')->column('code,name', 'id');
        $recruits = Db::table('recruit')->column('name', 'id');

        $families_data = Db::table('recruit_family')->select()->toArray();
        $families      = [];
        foreach ($families_data as $v) {
            if (empty($families[$v['eid']])) {
                $families[$v['eid']] = '';
            }
            if (!empty($v['appellation'])) {
                $families[$v['eid']] .= $v['appellation'] . '-' . $v['realname'] . '-' . date('Y-m-d', $v['birthday']) . '-' . $v['political_attitudes'] . '-' . $v['work'] . '；';
            }
        }

        foreach ($data as &$v) {
            $v['post_code']     = $posts[$v['post_id']]['code'];
            $v['post_name']     = $posts[$v['post_id']]['name'];
            $v['recruits_name'] = $recruits[$v['rid']];
            $v['family']        = empty($families[$v['id']]) ? '' : $families[$v['id']];
            $v['birthday']      = Fun::getAgeByBirth($v['birthday']);
            $v['status_text']   = $v['status'] == 1 ? '审核中或报名成功' : '暂存或审核失败';
            $v['word_url']      = empty($v['word_url']) ? '' : 'http://www.jucai.gov.cn' . $v['word_url'];
            $v['front_card']    = $this->_dealImage($v['front_card']);
            $v['back_card']     = $this->_dealImage($v['back_card']);
            $v['diploma_img']   = $this->_dealImage($v['diploma_img']);
            $v['degree_img']    = $this->_dealImage($v['degree_img']);
            $v['xuexin_img']    = $this->_dealImage($v['xuexin_img']);
            $v['work_pro_img']  = $this->_dealImage($v['work_pro_img']);
            $v['work_approve']  = $this->_dealImage($v['work_approve']);

            unset($v);
        }

        $this->assign('data', $data);
        return $this->fetch();
    }

    private function _dealImage($image)
    {
        $arr = explode(',', $image);
        $res = [];
        foreach ($arr as $v) {
            if (strpos($v, 'blob:') === false) {
                $res[] = $v;
            }
        }

        if (empty($res)) {
            return '';
        } else {
            return implode('<br/><br/>', $res);
        }
    }

    public function t1()
    {
        $table = QueryList::get('http://rcyz.qxrc.com/index.php?g=portal&m=index&a=site_list')->find('table');

// 采集表的每行内容
        $tableRows = $table->find('tr:gt(0)')->map(function ($row) {
            $arr          = [];
            $arr['title'] = $row->find('td:eq(2) a')->text();
            $arr['href']  = 'http://rcyz.qxrc.com' . $row->find('td:eq(2) a')->attr('href');
            $arr['id']    = str_replace('http://rcyz.qxrc.com/index.php?g=activity&m=activity&a=site_portal&id=', '', $arr['href']);

            return $arr;
        });

        $res = $tableRows->all();
        foreach ($res as $v) {
            Db::table('psite')->insert([
                'id'   => $v['id'],
                'href' => $v['href'],
            ]);
        }
        return 'ok';
    }

    public function t2()
    {
        $site = Db::table('psite')->select();
        foreach ($site as $v) {
            $ql = QueryList::get($v['href']);

            $rt['town']    = str_replace('所在乡镇：', '', $ql->find('.head h4')->text());
            $rt['content'] = $ql->find('.intro-content')->text();
            $rt['contact'] = $ql->find('.comment-ranking p:eq(0) strong')->text();
            $rt['mobile']  = $ql->find('.comment-ranking p:eq(1) strong')->text();
            $rt['email']   = $ql->find('.comment-ranking p:eq(2) strong')->text();
            $rt['address'] = $ql->find('.comment-ranking p:eq(5) strong')->text();

            Db::name('activity_site')->where('id', $v['id'])->update($rt);
        }
    }

    public function t3()
    {
        set_time_limit(0);
        $site = Db::table('psite')->select();
        foreach ($site as $v) {
            $this->_getActivity($v['href'], $v['id']);
        }
    }

    private function _getActivity($url, $id, $page = 1)
    {
        $rt   = QueryList::get($url . '&p=' . $page)->rules([
            'href'  => ['.item-image a', 'href'],
            'image' => ['.item-image img', 'src'],
        ])->range('.row .span9 .span3')->query()->getData();
        $flag = QueryList::get($url . '&p=' . $page)->find('.pagination li:last')->text();
        $list = $rt->all();
        if (!empty($list)) {
            foreach ($list as $v) {
                Db::table('pactivity')->insert([
                    'site_id' => $id,
                    'href'    => 'http://rcyz.qxrc.com' . $v['href'],
                    'image'   => $v['image'],
                ]);
            }

        }

        if ($flag == '尾页') {
            $this->_getActivity($url, $id, $page + 1);
        }
    }

    public function t4()
    {
        set_time_limit(0);
        $list = Db::table('pactivity')->select();
        foreach ($list as $v) {
            $ql                 = QueryList::get($v['href']);
            $arr                = [];
            $arr['id']          = (int)str_replace('http://rcyz.qxrc.com/index.php?g=activity&m=activity&a=detail&id=', '', $v['href']);
            $arr['title']       = $ql->find('.article-box h2')->text();
            $arr['main_image']  = (string)$v['image'];
            $arr['start_time']  = strtotime($ql->find('.tc-box:eq(3)>div:eq(1)>div:eq(0) strong')->text());
            $end_time           = strtotime($ql->find('.tc-box:eq(3)>div:eq(1)>div:eq(1) strong')->text());
            $arr['end_time']    = $end_time > 0 ? $end_time : 0;
            $arr['address']     = $ql->find('.tc-box:eq(4)>div:eq(1) strong')->text();
            $arr['user_id']     = $v['site_id'];
            $arr['content']     = $ql->find('#article_content')->html();
            $info               = $ql->find('.article-infobox span:eq(0)')->text();
            $info               = explode('|', $info);
            $arr['create_time'] = strtotime(trim(str_replace('创建于：', '', $info[0])));
            $arr['author']      = trim(str_replace('作者：', '', $info[1]));
            $arr['status']      = 2;
            Db::name('activity')->insert($arr);
        }
        return 'ok';
    }

    public function t5()
    {
        $ql                 = QueryList::get('http://rcyz.qxrc.com/index.php?g=portal&m=index&a=review_list&p=1');
        $this->_getReviewList($ql);
        $ql                 = QueryList::get('http://rcyz.qxrc.com/index.php?g=portal&m=index&a=review_list&p=2');
        $this->_getReviewList($ql);
    }
    private function _getReviewList(QueryList $ql)
    {
        $rt   = $ql->rules([
            'href'  => ['.item-image a', 'href'],
            'image' => ['.item-image img', 'src'],
        ])->range('.tc-gridbox')->query()->getData();

        $list = $rt->all();
        foreach ($list as $v) {
            Db::table('preview')->insert([
                'href'    => 'http://rcyz.qxrc.com' . $v['href'],
                'image'   => $v['image'],
            ]);
        }
    }

    public function t6()
    {
        $list = Db::table('preview')->select();
        foreach ($list as $v) {
            $ql                 = QueryList::get($v['href']);
            $arr                = [];
            $arr['title'] = $ql->find('.article-box>h2')->text();
            $arr['content'] = $ql->find('#article_content')->html();
            $arr['main_image'] = $v['image'];
            $href = $ql->find('.row>.span3>div:eq(0)>div:eq(1)>div:eq(0) a')->attr('href');
            $arr['activity_id'] = (int)str_replace('/index.php?g=activity&m=activity&a=detail&id=','',$href);
            $arr['user_id'] = (int)Db::name('activity')->where('id',$arr['activity_id'])->value('user_id');
            $arr['create_time'] = time();
            Db::name('activity_review')->insert($arr);
        }
    }
}
