<?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;

/**
 * Description of Enterprise
 * 企业相关事务处理
 * @author sgq
 */
class Enterprise {

    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 {
        $type = $data["type"];
        switch ($type) {
            case 1:
                $id = $data["id"];
                //注册成功，检查是否重新注册，如果是，继承账号所有人才数据和申报数据
                $ep = \app\common\api\EnterpriseApi::getOne($id);
                if ($ep && $ep["checkState"] == 3) {
                    $where = [];
                    $where[] = ["idCard", "=", $ep["idCard"]];
                    $where[] = ["checkState", "=", 3];
                    $closeRecord = \app\common\model\EnterpriseCloseAccount::where($where)->order("passTime desc")->find();
                    if ($closeRecord && $closeRecord["enterpriseId"]) {
                        $oldEpId = $closeRecord["enterpriseId"];
                        //开始转移数据
                        Db::startTrans();
                        try {
                            unset($where);
                            //生活补贴
                            $where[] = ["enterpriseId", "=", $oldEpId];
                            $upd["enterpriseId"] = $id;
                            $upd["type"] = $ep["type"];
                            $upd["enterpriseName"] = $ep["name"];
                            Db::table("md_living_allowance_info")->where($where)->save($upd);
                            //津补贴
                            Db::table("un_talent_allowance_info")->where($where)->save($upd);

                            //积分
                            unset($where);
                            unset($upd);
                            $where[] = ["enterprise_id", "=", $oldEpId];
                            $upd["enterprise_id"] = $id;
                            $upd["type"] = $ep["type"];
                            Db::table("new_integral_log")->where($where)->save($upd);
                            unset($upd["type"]);
                            Db::table("new_integral_record")->where($where)->save($upd);
                            Db::table("new_integral_veto")->where($where)->save($upd);

                            //人才
                            unset($where);
                            unset($upd);
                            $where[] = ["enterprise_id", "=", $oldEpId];
                            $upd["enterprise_id"] = $id;
                            Db::table("new_talent_info")->where($where)->save($upd);

                            //信息变更
                            unset($where);
                            unset($upd);
                            $where[] = ["enterpriseId", "=", $oldEpId];
                            $upd["enterpriseId"] = $id;
                            $upd["type"] = $ep["type"];
                            Db::table("un_talent_basic_change")->where($where)->save($upd);
                            $upd["enterpriseName"] = $ep["name"];
                            Db::table("un_talent_type_change")->where($where)->save($upd);
                            Db::table("un_talent_quit")->where($where)->save($upd);
                            Db::table("un_talent_bank_change")->where($where)->save($upd);

                            //工作变更情况1 作为变更前时候
                            unset($where);
                            unset($upd);
                            $where[] = ["oldEnterpriseId", "=", $oldEpId];
                            $upd["oldEnterpriseId"] = $id;
                            Db::table("un_talent_workunit_change")->where($where)->save($upd);

                            //工作变更情况2 作为变更后时候
                            unset($where);
                            unset($upd);
                            $where[] = ["newEnterpriseId", "=", $oldEpId];
                            $upd["newEnterpriseId"] = $id;
                            $upd["type"] = $ep["type"];
                            Db::table("un_talent_workunit_change")->where($where)->save($upd);


                            //other
                            unset($where);
                            unset($upd);
                            $where[] = ["enterpriseId", "=", $oldEpId];
                            $upd["enterpriseId"] = $id;
                            Db::table("un_talent_allowance_project")->where($where)->save($upd);
                            Db::table("un_talent_allowancecontract_detail")->where($where)->save($upd);
                            $upd["type"] = $ep["type"];
                            Db::table("un_childschool_fees")->where($where)->save($upd);
                            Db::table("un_house_renting")->where($where)->save($upd);
                            Db::table("un_house_renting_fees")->where($where)->save($upd);
                            Db::table("un_house_renting_purchase_fees")->where($where)->save($upd);
                            Db::table("un_housepurchase")->where($where)->save($upd);
                            Db::table("un_houser_renting_purchase_apply")->where($where)->save($upd);
                            Db::table("un_medical_subsidy")->where($where)->save($upd);
                            Db::table("un_training_subsidy")->where($where)->save($upd);
                            Db::table("un_major_projects")->where($where)->save($upd);
                            Db::commit();
                            return true;
                        } catch (\think\Exception $e) {
                            Db::rollback();
                            Log::error(json_encode($data));
                            Log::error($e->getMessage());
                            return false;
                        }
                    }
                }
                break;
        }
        return false;
    }

}
