<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use think\facade\Db;

/**
 * Description of System
 *
 * @author sgq
 */
class System extends AdminController {

    /**
     * 生成缓存
     * @return type
     */
    public function cache() {
        $step = $this->request->param("step");
        if ($step) {
            $cache_kvs = [
                ["func" => "\app\common\model\Company::cache", "stepName" => "部门"],
                ["func" => "\app\common\model\IntegralProject::cache", "stepName" => "积分项目"],
                ["func" => "\app\common\model\IntegralItem::cache", "stepName" => "积分标准"]
            ];
            $cache = $cache_kvs[$step - 1];
            if ($cache) {
                $time1 = microtime(true);
                $cache["func"]();
                $time2 = microtime(true);
                $runtime = round($time2 - $time1, 2);
                $step++;
                echo '<script>window.parent.setText("<span class=\"highlight\">' . $cache["stepName"] . '</span>缓存成功！[用时 ' . $runtime . 's]");</script>';
                echo '<script>window.location.href="/admin/system/cache/step/' . $step . '";</script>';
            } else {
                echo '<script>window.parent.setText("缓存创建结束！");</script>';
            }
        } else {
            return view();
        }
    }

    public function add_talent_type_change_in_queue($id) {
        queue("app\job\Talent", ["type" => 5, "id" => $id]);
    }

    public function work_unit_change_recovery() {
        $id = $this->request["id"];
        if ($id) {
            queue("app\job\Recovery", ["id" => $id, "type" => 1]);
            echo "【撤销工作变更审核通过】任务已加入处理队列";
            exit();
        }
        echo "请传/admin/system/work_unit_change_recovery/id/new_talent_info.id";
    }

    /**
     * 恢复自动审核失败
     */
    public function recovery() {
        $batch = 2022; //得更改批次
        $type = 1; //人才类型 1：晋江人才 2：电路人才

        $where = [];
        $where[] = ["ti.apply_year", "=", $batch];
        $where[] = ["ti.checkState", "=", \app\common\api\TalentState::FST_VERIFY_FAIL];
        $where[] = ["e.type", "=", $type];
        $where[] = ["tl.createUser", "=", "系统"];
        $where[] = ["tl.state", "=", \app\common\api\TalentState::FST_VERIFY_FAIL];
        $where[] = ["ti.delete", "=", 0];
        $list = \app\enterprise\model\Talent::alias("ti")
                        ->leftJoin("un_enterprise e", "e.id=ti.enterprise_id")
                        ->leftJoin("(select mainId,last_state,new_state,state,createUser,createTime from new_talent_checklog where md5(concat(createTime,mainId,`type`)) in (select md5(concat(max(createTime),mainId,`type`)) from `new_talent_checklog` where `type`=1 and `step` is null and active=1 and typeFileId is null group by mainId,`type`)) tl", "`tl`.`mainId`=ti.id")
                        ->where($where)
                        ->field("ti.*,e.type as enterpriseType")
                        ->select()->toArray();
        if ($list) {
            foreach ($list as $ti) {
                queue("app\job\Talent", ["type" => 3, "talentInfo" => $ti]);
            }
        }
    }

    /**
     * 导入旧的电路人才到新库
     *
      -1 复核不通过 173
      1 待提交 4
      7 待初审 22
      10 初审驳回 44
      20 复审驳回 1
      25 初审通过 71
      35 复核通过，待核查征信 1281
     * isPublic 5：待发证 6：已发证
     * active 1：在职 2：离职
     * isEffect 1：有效 4：失效 其它：已过期
     * 需要增加字段
     * cur_quit_time离职时间
     * active在职状态
     * identifyMonth认证月份
     * identifyExpireTime
     * oldId 旧id
     */
    public function import_old_jcdl() {
        $where = [];
        $where[] = ["type", "=", 2];
        $where[] = ["name", "=", "杨忠宪"];
        $list = Db::table("un_talent_info")->where($where)->select();
        $count = session("oldJcjlTalentInfoToNewTable");
        $count += count($list);
        session("oldJcjlTalentInfoToNewTable", $count);
        foreach ($list as $item) {
            queue("app\job\Worker", ["type" => 99, "talent_info" => $item]);
        }
        echo "加入队列成功";
    }

    /**
     * 生成人才积分基础分日志
     */
    public function generate_integral_base_log() {
        $where = [];
        $where[] = ["e.type", "=", 2];
        $where[] = ["ti.checkState", "=", \app\common\api\TalentState::CERTIFICATED];
        $list = Db::table("new_talent_info")->alias("ti")->leftJoin("un_enterprise e", "e.id=ti.enterprise_id")->field("ti.*")->where($where)->order("ti.talent_arrange asc")->select();
        $already_in = [];
        foreach ($list as $item) {
            $unique_md5 = md5($item["card_type"] . "_" . $item["card_number"]);
            if (!in_array($unique_md5, $already_in)) {
                queue("app\job\Talent", ["type" => 4, "id" => $item["id"]]);
                $already_in[] = $unique_md5;
            }
        }
        echo "加入队列成功";
    }

    /**
     * 导入电路人才条款
     */
    public function import_old_ic_conditions() {
        $where = [];
        $where[] = ["type", "=", 2];
        $oldConditions = Db::table("un_identify_condition")->where($where)->select()->toArray();
        foreach ($oldConditions as $oldCondition) {
            queue("app\job\Worker", ["type" => 98, "id" => $oldCondition["id"]]);
        }
    }

    /**
     * 设置电路条款与旧版一致启用状态
     */
    public function set_ic_condition_active() {
        $where = [];
        $where[] = ["type", "=", 2];
        $upd["active"] = 2;
        Db::table("new_talent_condition")->where($where)->update($upd);

        $where = [];
        $where[] = ["type", "=", 2];
        $where[] = ["active", "=", 1];
        $oldActiveConditions = Db::table("un_identify_condition")->where($where)->select()->toArray();
        $oldActiveConditioinNames = array_column($oldActiveConditions, "name");


        $where = [];
        $where[] = ["type", "=", 2];
        $where[] = ["name", "in", $oldActiveConditioinNames];
        $upd["active"] = 1;
        Db::table("new_talent_condition")->where($where)->update($upd);
    }

}
