<?php

namespace app\api\controller\notify;

use app\adminapi\logic\works\ServiceWorkLogic;
use app\api\controller\BaseApiController;
use app\api\logic\LoginLogic;
use app\api\logic\ServiceOrderLogic;
use app\api\validate\UserConfirmValidate;
use app\common\enum\ThirdTypeEnum;
use app\common\logic\ThirdOrderLogic;
use app\common\model\master_worker\MasterWorker;
use app\common\model\recharge\RechargeOrder;
use app\common\model\user\User;
use app\common\model\works\ServiceWork;
use think\Exception;
use think\facade\Log;

/**
 *
 * Class UserConfirmController
 * @package app\api\controller\notify
 */
class UserConfirmController extends BaseApiController
{

    public array $notNeedLogin = ['confirmDoor','confirmPriceDetail'];

    public function confirmDoor()
    {
        $params = (new UserConfirmValidate())->post()->goCheck('confirmDoor');
        try {
            $work_sn = decrypt($params['code'], \think\facade\Config::get('project.work_sn_key'));
            $work = ServiceWork::where(['work_sn'=>$work_sn,'mobile'=>$params['phone']])->findOrEmpty();
            if($work->isEmpty()){
                throw new Exception('工单不存在');
            }
            $params['user_id'] = $work['master_worker_id'];//工程师信息
            $params['user_info'] = MasterWorker::where('id',$work['master_worker_id'])->field('worker_number,real_name')->findOrEmpty();//工程师信息
            $params['work_sn'] = $work_sn;
            $result = ServiceWorkLogic::confirmDoor($params);
            if($work->third_type == 1){
                $meituan_result = ThirdOrderLogic::updateorderfulfillinfo($work,ThirdTypeEnum::MEITUAN_ARRIVED);
                Log::write('美团确认上门'.json_encode($meituan_result));
            }
            if (false === $result) {
                throw new Exception(ServiceWorkLogic::getError());
            }
        }catch (\Exception $e){
            return $this->fail($e->getMessage());
        }
        return $this->success('已确认上门');
    }



    public function confirmPriceDetail()
    {
        $params = (new UserConfirmValidate())->post()->goCheck('confirmDoor');
        try {
            $work_sn = decrypt($params['code'], \think\facade\Config::get('project.work_sn_key'));
            $work = ServiceWork::where(['work_sn'=>$work_sn,'mobile'=>$params['phone']])->findOrEmpty();
            if($work->isEmpty()){
                throw new Exception('工单不存在');
            }
            // 先登录得到用户信息
            $userInfo = LoginLogic::login(['account'=>$params['phone'],'scene'=>2,'terminal'=>2]);
            $userId = User::where('mobile',$params['phone'])->value('id');
            $sn = RechargeOrder::where('work_id',$work->id)->where('user_id',$work->user_id)->value('sn');
            $result = ServiceOrderLogic::detail([
                'sn' => $sn,
                'user_id' => $userId,
                'user_info' => $userInfo,
                'domain'=>$this->request->domain()
            ]);
            if (false === $result) {
                throw new Exception(ServiceOrderLogic::getError());
            }
            $result['user_info'] =  $userInfo;
            return $this->data($result);
        }catch (\Exception $e){
            return $this->fail($e->getMessage());
        }
    }







}