<?php
namespace app\admin\controller\medical;

use app\admin\controller\BaseController;
use app\admin\server\medical\SubjectServer;

class Subject extends BaseController
{

    /**
     * Notes:获取预约科目列表
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/29
     */
    public function getSubjectList()
    {
        [$page,$limit] = $this->getPage();
        $keywords = $this->request->get('keywords');
        $result         = SubjectServer::getSubjectList($page,$limit,$keywords);
        return json_success($result, '成功');
    }

    /**
     * Notes:修改预约科目
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/29
     */
    public function updateSubject()
    {
        $subject_id      = $this->request->post('subject_id');
        $subject_pid    = $this->request->post('subject_pid','');
        $subject_name     = $this->request->post('subject_name','');
        $subject_type    = $this->request->post('subject_type','');
        $subject_sort    = $this->request->post('subject_sort','');
        $this->validateCheck('medical\SubjectValidate', ['subject_id'=>$subject_id], 'update');
        $result         = SubjectServer::updateSubject($subject_id, $subject_pid, $subject_name, $subject_type, $subject_sort);
        return json_success($result, '修改成功');

    }

    /**
     * Notes:删除预约科目
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/27
     */
    public function delSubject()
    {
        $subject_id = $this->request->get('subject_id');
        $this->validateCheck('medical\SubjectValidate', ['subject_id' => $subject_id], 'info');
        //查询科目下是否有子集
        $children = SubjectServer::childrenCheck($subject_id);
        if (!empty($children)){
            throw new \Exception('该预约科目下有子科目，请先删除子科目！');
        }
        $result         = SubjectServer::delSubject($subject_id);
        if ($result){
            return json_success($result, '删除成功');
        }else{
            throw new \Exception('删除失败！');
        }

    }

    /**
     * Notes:添加预约科目
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/27
     */
    public function addSubject()
    {
        $subject_pid    = $this->request->post('subject_pid');
        $subject_name     = $this->request->post('subject_name');
        $subject_type    = $this->request->post('subject_type');
        $subject_sort    = $this->request->post('subject_sort');
        $this->validateCheck('medical\SubjectValidate', ['subject_pid' => $subject_pid, 'subject_name' => $subject_name, 'subject_type' => $subject_type], 'add');
        $result         = SubjectServer::insertSubject($subject_pid, $subject_name, $subject_type, $subject_sort);
        return json_success($result, '添加成功');
    }

    /**
     * Notes:查询预约科目详情
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/27
     */
    public function subjectInfo()
    {
        $subject_id = $this->request->get('subject_id');
        $this->validateCheck('medical\SubjectValidate', ['subject_id' => $subject_id], 'info');
        $result         = SubjectServer::subjectInfo($subject_id);
        if (!empty($result))
        return json_success($result, '成功');
    }

    /**
     * Notes:添加医生关联科目列表
     * @return \support\Response
     * User: yym
     * Date: 2022/10/26
     */
    public function getDoctorSubjectList()
    {
        return json_success(SubjectServer::getDoctorSubjectList(), '获取成功');
    }
}
