<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\logic;

use app\common\model\master_worker\MasterWorker;
use think\facade\Db;

/**
 * MasterWorkerExamineLogic逻辑
 * Class MasterWorkerExamineLogic
 * @package app\common\logic
 */
class MasterWorkerExamineLogic extends BaseLogic
{
    /**
     * @notes 查询更新工程师信息状态
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/07/17 15:20
     */
    public static function updateEngineerInformation(int $worker_id): bool
    {
        Db::startTrans();
        try {
            $resData = MasterWorker::with(['workerInfo','workerAgree','bankAccount'])->where('id', $worker_id)->findOrEmpty()->toArray();

            if(!isset($resData['workerInfo']['id']) || !isset($resData['workerAgree']['id']) || !isset($resData['bankAccount']['id'])) {
                $audit_state = 3;
            }elseif ((int)$resData['workerInfo']['audit_state'] === 0 || (int)$resData['workerAgree']['audit_state'] === 0 || (int)$resData['bankAccount']['audit_state'] === 0){
                $audit_state = 0;
            }elseif ((int)$resData['workerInfo']['audit_state'] === 2 || (int)$resData['workerAgree']['audit_state'] === 2 || (int)$resData['bankAccount']['audit_state'] === 2){
                $audit_state = 2;
            }else{
                $audit_state = 1;
            }
            MasterWorker::where('id', $worker_id)->update([
                'audit_state' => $audit_state,
            ]);
            Db::commit();

            if($audit_state == 1){
                // 工程师基本信息审核通知【给工程师的通知】
                event('Notice',  [
                    'scene_id' => 123,
                    'params' => [
                        'user_id' => 0,
                        'mobile' => $resData['mobile'],
                    ]
                ]);
            }
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }
}