<?php

namespace app\workerapi\controller;

use app\adminapi\logic\works\ServiceWorkLogic;
use app\common\model\master_worker\MasterWorker;
use app\workerapi\lists\MasterWorkerMessageLists;
use app\workerapi\logic\MasterWorkerMessageLogic;

class MasterWorkerMessageController extends BaseApiController
{

    /**
     * 统计工程师未读消息数量
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 林海涛
     * @date 2024/7/11 下午6:28
     */
    public function msgStatistics()
    {
       $data = MasterWorkerMessageLogic::msgStatistics($this->userId);
       return $this->success('操作成功', $data, 1, 0);

    }

    public function lists()
    {

        return $this->dataLists(new MasterWorkerMessageLists());
    }

    public function showMsg()
    {
        $validate = \think\facade\Validate::rule([
                'ids'  => 'require|array',
                'msg_type' => 'require'
            ]);
        if (!$validate->check(request()->all())) {
            return $this->fail($validate->getError());
        }
        MasterWorkerMessageLogic::showMsg(request()->all(),$this->userId);
        return $this->success('操作成功', [], 1, 0);
    }

    /**
     * 工程师接单提示
     * @return \think\response\Json
     */
    public function orderPrompt()
    {
        $result = ['confirm_code'=>0,'msg'=>'无'];
        $prompt_result = MasterWorkerMessageLogic::orderPrompt($this->userId);

        //检测是否开启接单
        $master_worker = MasterWorker::where('id',$this->userId)->findOrEmpty();
        if(!$master_worker->isEmpty() && $master_worker['accept_order_status'] == 0){
            $result = ['confirm_code'=>103,'msg'=>'您已关闭接单，如需接单，请先开启接单'];
            return $this->success('操作成功', $result, 1, 0);
        }

        //新工单领取
        if(!empty($prompt_result)){
            $result = ['confirm_code'=>101,'msg'=>'您有新的工单未领取，请点击领取','data'=>$prompt_result];
            return $this->success('操作成功', $result, 1, 0);
        }

        //预约时间更新确认
        $appoint_result = ServiceWorkLogic::getAppointmentNotice($this->userId);
        if(!empty($appoint_result)){
            $result = ['confirm_code'=>102,'msg'=>'您有工单更改了预约时间，请注意查看','data'=>$appoint_result];
            return $this->success('操作成功', $result, 1, 0);
        }


        return $this->success('操作成功', $result, 1, 0);
    }


}