<?php

namespace app\web\controller;

use app\common\model\WorkerThird;
use app\common\model\Comjobs as ComjobsModel;
use app\common\model\Worker as WorkerModel;
use echowx\WxProgram;

class Active
{

    public function aiRecruit()
    {
        $map       = [];
        $map[]     = ['createtime', '<=', time()];
        $map[]     = ['status', 'in', '3,4'];
        $searchval = input('searchval/s', "");
        $whereor   = [];
        if (!empty($searchval)) {
            $whereor[] = ['title', 'like', '%' . $searchval . '%'];
            $lmap      = [];
            $lmap[]    = ['title', 'like', '%' . $searchval . '%'];
            $worklike  = WorkerModel::where($lmap)->select();

            $wkids = [];
            foreach ($worklike as $lk => $v) {
                $wkids[] = $v["id"];
            }
            $whereor[] = ['workerid', 'in', $wkids];
        }
        $cateid = input('cateid/s', "");
        if (!empty($cateid)) {
            $map[] = ['cateid', 'IN', $cateid];
        }
        $wtype = input('wtype/d', 0);
        if ($wtype != 0) {
            $map[] = ['wtype', '=', $wtype];
        }
        $rectype = input('rectype/d', 0);
        if ($rectype != 0) {
            $map[] = ['recruitment_cate', '=', $rectype];
        }
        $district = input('district/s', "");
        if (!empty($district)) {
            $map[] = ['district', '=', $district];
        }
        $orderby = ['status' => 'asc', 'updatetime' => 'desc', 'id' => 'desc'];
        $plist   = ComjobsModel::with(['worker', 'comjobsCate'])->where($map)->where(function ($q) use ($whereor) {
            $q->whereOr($whereor);
        })->order($orderby)->select();

        //数据处理
        //第三方id
        $third_ids   = [];
        $third_lists = [];
        foreach ($plist as $l) {
            if (!empty($l['third_id'])) {
                $third_ids[] = $l['third_id'];
            }
        }
        if (!empty($third_ids)) {
            $third_lists = WorkerThird::where('id', 'in', $third_ids)->column('*', 'id');
        }
        $plist = $plist->toArray();
        foreach ($plist as $k => $v) {
            if (!empty($v['third_id'])) {
                $plist[$k]['worker']['title'] = $third_lists[$v['third_id']]['name'];
            }
        }

        $res = $this->_split_arr($plist, 4);

        return view('active/ai_recruit', ['res' => $res]);
    }

    private function _split_arr($arr, $num)
    {
        $res  = [];
        $item = [];
        foreach ($arr as $k => $v) {
            if ($k % $num == 0 && $k != 0) {
                $res[] = $item;
                $item  = [];
            }
            $item[] = $v;
        }

        if (!empty($item)) {
            $res[] = $item;
        }

        return $res;
    }

    public function qrcode()
    {
        $id     = input('id');
        $userid = 0;
        $info   = ComjobsModel::where('id', $id)->find();
        if (empty($info)) {
            return '';
        }

        $filename       = $id . "_" . $userid . ".jpg";
        $dst_comjobspic = root_path("public/attachment/comjobspic") . $filename;
        if (!file_exists($dst_comjobspic)) {
            $wxprogram = new WxProgram();
            $wxprogram->wxacode_get_unlimited($id . "&" . $userid, "pages/comjobs/detail", 430, "attachment/comjobspic/" . $id . "_" . $userid . ".jpg");
        }

        header("Content-type: image/jpeg");
        $im = @imagecreatefromjpeg($dst_comjobspic);
        imagejpeg($im);
    }
}
	