<?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 Messenger
 *
 * @author sgq
 */
class Messenger {

    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:
                //通知单位审核注册
                break;
            case 2:
                //通知人才申报初审人员
                try {
                    $userId = $data["userId"];
                    $name = $data["name"];
                    $phone = $data["phone"];
                    $count = $data["count"];
                    $template = sprintf("【晋江市人才服务平台】您的部门有%d条人才认定申报记录待初审，请及时登录审批系统处理。", $count);
                    $smsapi = new ChuanglanSmsApi();
                    $result = $smsapi->sendSMS($phone, $template);
                    $result = json_decode($result, true);
                    $id = getStringId();
                    $record_data = [
                        'id' => $id,
                        'userId' => $userId,
                        'bizId' => $result["msgId"],
                        'type' => 1,
                        'smsType' => 2,
                        'name' => $name,
                        'phone' => $phone,
                        'params' => "初级审核",
                        'templateCode' => $template,
                        'state' => $result['code'] == 0 ? 2 : 3,
                        'sendingDate' => date("Y-m-d H:i:s", time()),
                        'createTime' => date("Y-m-d H:i:s", time()),
                        'msg' => $result['errorMsg']
                    ];

                    MessageRecord::create($record_data);
                    return true;
                } catch (\think\Exception $e) {
                    Log::error(json_encode($data));
                    Log::error($e->getMessage());
                    return false;
                }
                break;
            case 3:
                //通知人才申报部门审核人员
                try {
                    $userId = $data["userId"];
                    $name = $data["name"];
                    $phone = $data["phone"];
                    $count = $data["count"];
                    $template = sprintf("【晋江市人才服务平台】您的部门有%d条人才认定申报记录待部门审核，请及时登录审批系统处理。", $count);
                    $smsapi = new ChuanglanSmsApi();
                    $result = $smsapi->sendSMS($phone, $template);
                    $result = json_decode($result, true);
                    $id = getStringId();
                    $record_data = [
                        'id' => $id,
                        'userId' => $userId,
                        'bizId' => $result["msgId"],
                        'type' => 1,
                        'smsType' => 2,
                        'name' => $name,
                        'phone' => $phone,
                        'params' => "部门审核",
                        'templateCode' => $template,
                        'state' => $result['code'] == 0 ? 2 : 3,
                        'sendingDate' => date("Y-m-d H:i:s", time()),
                        'createTime' => date("Y-m-d H:i:s", time()),
                        'msg' => $result['errorMsg']
                    ];

                    MessageRecord::create($record_data);
                    return true;
                } catch (\think\Exception $e) {
                    Log::error(json_encode($data));
                    Log::error($e->getMessage());
                    return false;
                }
                break;
            case 4:
                //通知人才申报复审人员
                try {
                    $userId = $data["userId"];
                    $name = $data["name"];
                    $phone = $data["phone"];
                    $count = $data["count"];
                    $template = sprintf("【晋江市人才服务平台】您的部门有%d条人才认定申报记录待复审，请及时登录审批系统处理。", $count);
                    $smsapi = new ChuanglanSmsApi();
                    $result = $smsapi->sendSMS($phone, $template);
                    $result = json_decode($result, true);
                    $id = getStringId();
                    $record_data = [
                        'id' => $id,
                        'userId' => $userId,
                        'bizId' => $result["msgId"],
                        'type' => 1,
                        'smsType' => 2,
                        'name' => $name,
                        'phone' => $phone,
                        'params' => "复审",
                        'templateCode' => $template,
                        'state' => $result['code'] == 0 ? 2 : 3,
                        'sendingDate' => date("Y-m-d H:i:s", time()),
                        'createTime' => date("Y-m-d H:i:s", time()),
                        'msg' => $result['errorMsg']
                    ];

                    MessageRecord::create($record_data);
                    return true;
                } catch (\think\Exception $e) {
                    Log::error(json_encode($data));
                    Log::error($e->getMessage());
                    return false;
                }
                break;
            case 5:
                //变更通过通知代理人                
                try {
                    $userId = $data["userId"];
                    $phone = $data["phone"];
                    $template = $data["template"];
                    $smsapi = new ChuanglanSmsApi();
                    $result = $smsapi->sendSMS($phone, $template);
                    $result = json_decode($result, true);
                    $recordId = getStringId();
                    $record_data = [
                        'id' => $recordId,
                        'userId' => $userId,
                        'bizId' => $result["msgId"],
                        'type' => 2,
                        'smsType' => 1,
                        'phone' => $phone,
                        'params' => '人才层次变更公示',
                        'templateCode' => $template,
                        'state' => $result['code'] == 0 ? 2 : 3,
                        'sendingDate' => date("Y-m-d H:i:s", time()),
                        'createTime' => date("Y-m-d H:i:s", time()),
                        'msg' => $result['errorMsg']
                    ];
                    MessageRecord::create($record_data);
                    return true;
                } catch (\think\Exception $e) {
                    Log::error(json_encode($data));
                    Log::error($e->getMessage());
                    return false;
                }
                break;
            case 6:
                //通用短信                
                try {
                    $userId = $data["userId"];
                    $phone = $data["phone"];
                    $template = $data["template"];
                    $userType = $data["userType"];
                    $processName = $data["processName"];
                    $smsapi = new ChuanglanSmsApi();
                    $result = $smsapi->sendSMS($phone, $template);
                    $result = json_decode($result, true);
                    $recordId = getStringId();
                    $record_data = [
                        'id' => $recordId,
                        'userId' => $userId,
                        'bizId' => $result["msgId"],
                        'type' => $userType,
                        'smsType' => 1,
                        'phone' => $phone,
                        'params' => $processName,
                        'templateCode' => $template,
                        'state' => $result['code'] == 0 ? 2 : 3,
                        'sendingDate' => date("Y-m-d H:i:s", time()),
                        'createTime' => date("Y-m-d H:i:s", time()),
                        'msg' => $result['errorMsg']
                    ];
                    MessageRecord::create($record_data);
                    return true;
                } catch (\think\Exception $e) {
                    Log::error(json_encode($data));
                    Log::error($e->getMessage());
                    return false;
                }
                break;
            case 109:
                //注销审核通过通知
                try {
                    $id = $data["userId"];
                    $ep = \app\common\api\EnterpriseApi::getOne($id);
                    if ($ep && $ep["agentPhone"]) {
                        $template = sprintf("【晋江市人才服务平台】您申请的账号注销已通过审核，账号注销成功。");
                        $smsapi = new ChuanglanSmsApi();
                        $result = $smsapi->sendSMS($ep["agentPhone"], $template);
                        $result = json_decode($result, true);
                        $id = getStringId();
                        $record_data = [
                            'id' => getStringId(),
                            'userId' => $id,
                            'bizId' => $result["msgId"],
                            'type' => 2,
                            'smsType' => 2,
                            'name' => $ep["name"],
                            'phone' => $ep["agentPhone"],
                            'params' => "账号注销审核",
                            'templateCode' => $template,
                            'state' => $result['code'] == 0 ? 2 : 3,
                            'sendingDate' => date("Y-m-d H:i:s", time()),
                            'createTime' => date("Y-m-d H:i:s", time()),
                            'msg' => $result['errorMsg']
                        ];

                        MessageRecord::create($record_data);
                        return true;
                    }
                } catch (\think\Exception $e) {
                    Log::error(json_encode($data));
                    Log::error($e->getMessage());
                    return false;
                }
                break;
        }
        return false;
    }

}
