<?php

declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use think\facade\Log;
use app\common\api\TalentState;
use app\enterprise\model\Talent;
use app\common\api\MenuApi;
use app\admin\model\SysRelation;
use app\admin\model\User;

/**
 * 复审检查
 */
class ReVerifyChecker extends Command {

    protected function configure() {
        // 指令配置
        $this->setName('ReVerifyChecker')
                ->setDescription('ReVerify Checker');
    }

    protected function execute(Input $input, Output $output) {
        $whereRaw = sprintf("(tl.state in (-14,14,15,16)) or (tl.state=12 and ti.pass_dept_check=0) or (tl.state=10 and ti.pass_dept_check=1) or (tl.state=10 and (tc.companyIds is null or tc.companyIds = ''))");
        //$whereRaw = sprintf("(ti.checkState in (14,15,16)) or (ti.checkState=12 and ti.pass_dept_check=0) or (ti.checkState=10 and ti.pass_dept_check=1) or (ti.checkState=10 and (tc.companyIds is null or tc.companyIds = ''))");
        $where[] = ["ti.checkState", "in", [TalentState::FST_VERIFY_PASS, TalentState::DEPT_VERIFY_PASS]];
        $where[] = ["e.type", "=", 1]; //只对晋江人才相关部门发送短信
        $list = Talent::alias("ti")
                        ->leftJoin("new_talent_condition tc", "tc.id=ti.talent_condition")
                        ->leftJoin("un_enterprise e", "e.id=ti.enterprise_id")
                        ->leftJoin("(select description,mainId,last_state,new_state,state,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)->whereRaw($whereRaw)->group("e.type")->field("count(*) as count,e.type")->select()->toArray();
        $codes = ["talentInfo_thirdCheck"];
        $menuIds = MenuApi::getMenuIdsByCodes($codes);
        $where = [];
        $where[] = ["menuid", "in", $menuIds];
        $roleIds = SysRelation::where($where)->group("roleid")->having("count(*)=" . count($codes))->column("roleid");
        foreach ($list as $item) {
            $where = [];
            $where[] = ["type", "=", $item["type"]];
            $where[] = ["status", "=", 1];
            $where[] = ["roleid", "<>", 1];
            $regstr = ",(" . implode("|", $roleIds) . "),";
            $whereRaw = "concat(',',roleid,',') REGEXP '$regstr'";
            $users = User::where($where)->whereRaw($whereRaw)->select()->toArray();
            foreach ($users as $user) {
                if ($item["count"] > 0 && $user["phone"]) {
                    queue("app\job\Messenger", ["type" => 4, "userId" => $user["id"], "name" => $user["name"], "phone" => $user["phone"], "count" => $item["count"]]);
                }
            }
        }
    }

}
