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

use app\admin\model\MedicalCareSubject;

class SubjectServer
{
    /**
     * Notes:获取业务员列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/27
     */
    public static function getSubjectList($page, $limit,string $keywords)
    {
        [$list,$count] =  MedicalCareSubject::getSubjectList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['subject_create_time'] = date('Y-m-d H:i:s', $v['subject_create_time']);
                if (!empty($v['children'])){
                    foreach ($v['children'] as $key => $val){
                        $list[$k]['children'][$key]['subject_create_time'] = date('Y-m-d H:i:s', $val['subject_create_time']);
                    }
                }
            }
        }

        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:修改业务员
     * @param string $subject_name
     * @param string $subject_type
     * @param string $subject_sort
     * @param int $subject_id
     * @param int $subject_pid
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function updateSubject($subject_id, $subject_pid, $subject_name, $subject_type, $subject_sort)
    {
        MedicalCareSubject::affairBegin();
        try {
            $where = [];
            $where['subject_id'] = $subject_id;
            $data = [];
            if (!empty($subject_name)){
                $data['subject_name'] = $subject_name;
            }
            if (!empty($subject_pid)){
                $data['subject_pid'] = $subject_pid;
            }
            if (!empty($subject_type)){
                $data['subject_type'] = $subject_type;
            }
            if (!empty($subject_sort)){
                $data['subject_sort'] = $subject_sort;
            }
            $result = MedicalCareSubject::where($where)->update($data);
            if ($result !== false){
                MedicalCareSubject::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MedicalCareSubject::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除业务员
     * @param int $subject_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function delSubject($subject_id)
    {
        MedicalCareSubject::affairBegin();
        try {
            $where = [];
            $where['subject_id'] = $subject_id;
            $data['subject_del'] = 1;
            $result = MedicalCareSubject::where($where)->update($data);
            if (!empty($result)){
                MedicalCareSubject::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MedicalCareSubject::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询子集
     * @param int $subject_id
     * @return array
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function childrenCheck($subject_id)
    {
        $where = [];
        $where['subject_pid'] = $subject_id;
        $where['subject_del'] = 0;
        $result = MedicalCareSubject::where($where)->select()->get()->toArray();
        return $result;
    }

    /**
     * Notes: 添加业务员
     * @param int $subject_pid
     * @param string $subject_name
     * @param int $subject_type
     * @param int $subject_sort
     * @return int
     * User: ZQ
     * Date: 2022/9/14
     */
    public static function insertSubject($subject_pid, $subject_name, $subject_type, $subject_sort)
    {

        MedicalCareSubject::affairBegin();
        try {
            $data = [];
            $data['subject_name'] = $subject_name;
            $data['subject_pid'] = $subject_pid;
            $data['subject_type'] = $subject_type;
            $data['subject_sort'] = $subject_sort;
            $data['subject_create_time'] = time();
            $result =  MedicalCareSubject::insertGetId($data);
            if (!empty($result)){
                MedicalCareSubject::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MedicalCareSubject::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询业务员
     * @param int $subject_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function subjectInfo($subject_id)
    {
        $where = [];
        $where['subject_del'] = 0;
        $where['subject_id'] = $subject_id;
        $result = MedicalCareSubject::where($where)->first();
        if (!empty($result)){
            $result['subject_create_time'] = date('Y-m-d H:i:s',$result['subject_create_time']);
        }
        return $result;
    }

    /**
     * Notes:修改状态
     * @param int $subject_status
     * @param int $subject_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updateStatus($subject_id, $subject_status)
    {
        MedicalCareSubject::affairBegin();
        try {
            $where = [];
            $where['subject_id'] = $subject_id;
            $data = [];
            $data['subject_status'] = $subject_status;
            $result = MedicalCareSubject::where($where)->update($data);
            if ($result !== false){
                MedicalCareSubject::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MedicalCareSubject::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:获取列表
     * @return array
     * User: yym
     * Date: 2022/10/26
     */
    public static function getDoctorSubjectList()
    {
        $list = MedicalCareSubject::getDoctorSubjectList();
        $data = array();
        if(!empty($list))
        {
            foreach ($list as $k => $item)
            {
                if(isset($item['children']) && !empty($item['children']))
                {
                    foreach ($item['children'] as $row)
                    {
                        switch ($row['subject_type']){
                            case 2://特需
                                $type = '特需';
                                break;
                            case 1://专家
                                $type = '专家';
                                break;
                            default:
                                $type = '普通';
                                break;
                        }
                        $row['subject_name'] = $type . '【' . $item['subject_name'] . '/' . $row['subject_name'] . '】';
                        $data[] = $row;
                    }
                }
            }
        }
        return $data;
    }
}
