<?php

namespace app\job;

use think\queue\Job;
use think\facade\Log;
use think\facade\Db;
use app\common\api\ChuanglanSmsApi;
use app\common\model\MessageRecord;
use app\common\api\TalentLogApi;
use app\common\api\TalentConditionApi;
use app\common\api\TalentState;
use app\enterprise\model\Talent as TalentModel;
use app\common\state\ProjectState;
use app\common\state\CommonConst;
use app\common\state\MainState;

/**
 * Description of Recovery
 *
 * @author sgq
 */
class Recovery {

    public function fire(Job $job, $data) {
        if ($this->deal($data)) {
            $job->delete();
            return true;
        }
        if ($job->attempts() >= 3) {
            $job->delete();
            return false;
        }
        $job->release(10); //10秒后重试
    }

    /**
     * 处理业务逻辑
     * @param type $data
     * @return bool
     */
    public function deal($data): bool {
        switch ($data["type"]) {
            case 1:
                //撤销工作单位变更审核通过
                $talentId = $data["id"];
                $where = [];
                $where[] = ["talentId", "=", $talentId];
                $where[] = ["checkState", "=", 3]; //通过状态才可以撤销
                $changeRecord = Db::table("un_talent_workunit_change")->where($where)->find();
                if ($changeRecord) {
                    Db::startTrans();
                    try {
                        unset($where);
                        $where[] = ["type", "=", ProjectState::WORKCHANGE];
                        $where[] = ["mainId", "=", $changeRecord["id"]];
                        $where[] = ["state", "=", 2];
                        $rejectLog = Db::table("new_talent_checklog")->where($where)->find();
                        $oldCheckState = $rejectLog ? 9 : 1; //推算之前的状态
                        $checkMsg = "因审核有误，退回至待审核";

                        $updWorkChange["id"] = $changeRecord["id"];
                        $updWorkChange["checkState"] = $oldCheckState;
                        $updWorkChange["checkMsg"] = $checkMsg;
                        $updWorkChange["passTime"] = null;
                        Db::table("un_talent_workunit_change")->save($updWorkChange);

                        $log["id"] = getStringId();
                        $log["active"] = 1;
                        $log["state"] = 1;
                        $log["step"] = 11;
                        $log["stateChange"] = MainState::getStateName($changeRecord["checkState"]) . "->" . MainState::getStateName($oldCheckState);
                        $log["type"] = ProjectState::WORKCHANGE;
                        $log["mainId"] = $changeRecord["id"];
                        $log["description"] = $checkMsg;
                        $log["createUser"] = "系统";
                        $log["createTime"] = date("Y-m-d H:i:s");
                        Db::table("new_talent_checklog")->insert($log);

                        //修改人才库信息
                        $upd["id"] = $talentId;
                        $upd["enterprise_id"] = $changeRecord["oldEnterpriseId"];
                        $upd["labor_contract_rangetime"] = $changeRecord["oldStartTime"] . " - " . $changeRecord["oldEndTime"];
                        $upd["cur_entry_time"] = $changeRecord["oldAnyTime"];
                        $upd["active"] = 2;
                        $upd["cur_quit_time"] = $changeRecord["quitTime"];
                        $upd["position"] = $changeRecord["oldPost"];
                        Db::table("new_talent_info")->save($upd);

                        $talentLog["id"] = getStringId();
                        $talentLog["active"] = 1;
                        $talentLog["step"] = 21;
                        $talentLog["type"] = ProjectState::TALENT;
                        $talentLog["mainId"] = $talentId;
                        $talentLog["description"] = "因审核有误，该工作单位变更退回至待审核。";
                        $talentLog["createUser"] = "系统";
                        $talentLog["createTime"] = date("Y-m-d H:i:s");
                        Db::table("new_talent_checklog")->insert($talentLog);
                        Db::commit();
                        return true;
                    } catch (\Exception $e) {
                        Db::rollback();
                        Log::write($e->getMessage(), "error");
                    }
                }
                break;
        }
        return false;
    }

}
