<?php
namespace app\workerapi\logic;
use app\common\logic\BaseLogic;
use app\common\model\bank_account\BankAccount;
use app\common\model\master_worker\MasterWorker;
use app\common\model\master_worker\MasterWorkerInfo;
use think\Exception;

/**
 * @author 林海涛
 * @date 2024/7/10 下午4:29
 */

class MasterWorkerInfoLogic extends BaseLogic
{

    public static function getIdCard($userId)
    {
        return MasterWorkerInfo::where([['worker_id', '=', $userId]])->findOrEmpty()->toArray();
    }

    public static function changeIdCard($params,$userId)
    {
        try{

            if($params['id_card_front_img']=='https://weixiu.zhongdunzhizhao.com/static/wxapp/master_image/zm.png' || $params['id_card_opposite_img'] == 'https://weixiu.zhongdunzhizhao.com/static/wxapp/master_image/fm.png'){
                throw new Exception('请上传身份证正反面照片');
            }

            $where = [['worker_id', '=', $userId]];
            $userInfo = MasterWorkerInfo::where($where)->findOrEmpty();
            if ($userInfo->isEmpty()) {
                $userInfo = new MasterWorkerInfo();
                $userInfo->worker_id = $userId;
            }
            $userInfo->real_name = $params['real_name'];
            $userInfo->id_card = $params['id_card'];
            $userInfo->id_card_front_img = $params['id_card_front_img'];
            $userInfo->id_card_opposite_img = $params['id_card_opposite_img'];
            $userInfo->mobile = $params['mobile'];
            $userInfo->address = $params['address'];
            $userInfo->audit_state = 0;
            $masterWorker = MasterWorker::findOrEmpty($userId);
            if (!$masterWorker->isEmpty()) {
                $masterWorker->real_name =  $userInfo->real_name;
                $masterWorker->save();
            }
            $userInfo->save();
            return true;
        } catch(\Exception $e){
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * 绑定银行卡
     * @param $params
     * @param $userId
     * @return bool
     * @author 林海涛
     * @date 2024/7/11 下午4:41
     */
    public static function bindBankAccount($params,$userId)
    {
        try{
            $where = [['worker_id', '=', $userId]];
            $bankAccount = BankAccount::where($where)->findOrEmpty();
            if ($bankAccount->isEmpty()) {
                $bankAccount = new BankAccount();
                $bankAccount->worker_id = $userId;
            }
            $bankAccount->account_holder = $params['account_holder'];
            $bankAccount->bank_name = $params['bank_name'];
            $bankAccount->province = $params['province'];
            $bankAccount->city = $params['city'];
            $bankAccount->opening_branch = $params['opening_branch'];
            $bankAccount->account = $params['account'];
            $bankAccount->mobile = $params['mobile'];
            $bankAccount->bank_image = !empty($params['bank_image'])?$params['bank_image']:'';
            $bankAccount->audit_state = 0;
            $bankAccount->save();
            return true;
        } catch(\Exception $e){
            self::setError($e->getMessage());
            return false;
        }
    }


    public static function bankAccountInfo($userId)
    {
          $where = [['worker_id', '=', $userId]];
          return BankAccount::where($where)->withoutField(['user_id','worker_id'])->findOrEmpty()->toArray();
    }
}