<?php
namespace app\workerapi\controller;

use app\adminapi\logic\works\ServiceWorkLogic;
use app\common\model\works\IssueWork;
use app\common\model\works\ReturnWork;
use app\common\model\works\ServiceWork;
use app\workerapi\lists\HistoryWorkLists;
use app\workerapi\lists\IssueWorkLists;
use app\workerapi\lists\ReturnWorkLists;
use app\workerapi\lists\ServiceAssignWorkLists;
use app\workerapi\lists\ServiceWorkLists;
use app\workerapi\validate\ServiceWorkValidate;

/**
 * 工单系统
 */
class WorksController extends BaseApiController
{
    /**
     * 首页数量统计
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function statistics()
    {
        $result['service_work_count'] = ServiceWork::where(['master_worker_id'=>$this->userId])->whereIn('service_status','0,1,2')->where('work_status','<>',1)->count();
        $result['assign_work_count'] = ServiceWork::where(['master_worker_id'=>$this->userId,'work_status'=>0])->count();
        $result['pick_work_count'] = 0;
        $result['return_work_count'] = ReturnWork::where(['master_worker_id'=>$this->userId])->where('return_work_status','<>',2)->count();
        $result['issue_work_count'] = IssueWork::where(['master_worker_id'=>$this->userId])->where('issue_approval','<>',4)->count();
        $result['review_work_count'] = ServiceWork::where(['master_worker_id'=>$this->userId,'work_status'=>7,'service_status'=>3])->count();
        return $this->data($result);
    }

    /**
     * 服务工单列表-全部
     * @return \think\response\Json
     */
    public function serviceWorkList()
    {
        return $this->dataLists(new ServiceWorkLists());
    }

    /**
     * 派单
     * @return \think\response\Json
     */
    public function assignWorkList()
    {
        return $this->dataLists(new ServiceAssignWorkLists());
    }

    /**
     * 历史工单
     * @return \think\response\Json
     */
    public function historyWorkList()
    {
        return $this->dataLists(new HistoryWorkLists());
    }

    /**
     * 领取服务单
     * @return \think\response\Json
     */
    public function pickWork()
    {
        $params = (new ServiceWorkValidate())->post()->goCheck('pick', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = ServiceWorkLogic::pickWork($params);
        if (false === $result) {
            return $this->fail(ServiceWorkLogic::getError());
        }
        return $this->success('领取成功', [], 1, 1);
    }

    /**
     * 工单详情
     * @return \think\response\Json
     */
    public function detail()
    {
        $params = (new ServiceWorkValidate())->goCheck('detail',[
            'user_id' => $this->userId,
        ]);
        $result = ServiceWorkLogic::detail($params);
        if (false === $result) {
            return $this->fail(ServiceWorkLogic::getError());
        }
        return $this->data($result);
    }


    /**
     * 预约上门
     * @return \think\response\Json
     */
    public function appointWork()
    {
        $params = (new ServiceWorkValidate())->post()->goCheck('appoint', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = ServiceWorkLogic::appointWork($params);
        if (false === $result) {
            return $this->fail(ServiceWorkLogic::getError());
        }
        return $this->success('预约成功，等待上门', [], 1, 1);
    }

    /**
     * 师傅确认上门
     * @return \think\response\Json
     */
    public function confirmDoor()
    {
        $params = (new ServiceWorkValidate())->post()->goCheck('door', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = ServiceWorkLogic::confirmDoor($params);
        if (false === $result) {
            return $this->fail(ServiceWorkLogic::getError());
        }
        return $this->success('操作成功,师傅已上门', [], 1, 1);
    }

    /**
     * 师傅确认报价单
     * @return \think\response\Json
     */
    public function confirmPrice()
    {
        $params = (new ServiceWorkValidate())->post()->goCheck('price', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = ServiceWorkLogic::confirmPrice($params);
        if (false === $result) {
            return $this->fail(ServiceWorkLogic::getError());
        }
        return $this->success('操作成功,师傅已填写报价单，等待用户确认中', [], 1, 1);
    }

    /**
     * 师傅确认服务完成
     * @return \think\response\Json
     */
    public function confirmServiceFinish()
    {
        $params = (new ServiceWorkValidate())->post()->goCheck('finished', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = ServiceWorkLogic::confirmServiceFinish($params);
        if (false === $result) {
            return $this->fail(ServiceWorkLogic::getError());
        }
        return $this->success('操作成功,师傅已确认服务完成，等待用户确认中', [], 1, 1);
    }

    /**
     * 投诉工单列表
     *
     * @return \think\response\Json
     */
    public function issueWorkList()
    {
        return $this->dataLists(new IssueWorkLists());
    }

    /**
     * 返修工单列表
     * @return \think\response\Json
     */
    public function returnWorkList()
    {
        return $this->dataLists(new ReturnWorkLists());
    }
}