123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- <?php
- namespace app\manage\controller;
- use think\Controller;
- use think\Db;
- use think\Session;
- use think\Config;
- use think\Cookie;
- use think\Request;
- use app\common\library\SysLogs;
- use app\common\library\UUIDs;
- use PHPExcel_IOFactory;
- use PHPExcel;
- class Doctors extends Base {
- public function index() {
- return $this->fetch();
- }
- public function datas() {
- $request = Request::instance()->param();
- $username = isset($request["username"]) ? $request["username"] : null;
- $status = isset($request["status"]) ? $request["status"] : null;
- $institution_id = isset($request["insId"]) ? $request["insId"] : null;
- $whereArr = array();
- if (!empty($username)) {
- $whereArr["username"] = array("like", $username . "%");
- }
- if ($status != null) {
- $whereArr["status"] = $status;
- }
- if (!empty($institution_id)) {
- $whereArr["institution_id"] = $institution_id;
- }
- $page = empty($_GET["page"]) ? 1 : $_GET["page"];
- $pagesize = empty($_GET["rows"]) ? 1 : $_GET["rows"];
- if (empty($page) || $page < 1) {
- $page = 1;
- }
- if (empty($pagesize) || $pagesize < 1) {
- $pagesize = 30;
- }
- $info = DB::table('doctors')->where($whereArr)->page($page, $pagesize)->select();
- foreach ($info as $k => $v) {
- $iname = DB::table('institution')->where('id', $v['institution_id'])->field('name')->find();
- $info[$k]['institution_name'] = $iname['name'];
- $dname = DB::table('department')->where('id', $v['department_id'])->field('department_name')->find();
- $info[$k]['department_name'] = $dname['department_name'];
- }
- $num = DB::table('doctors')->where($whereArr)->count();
- $data = array();
- $data['total'] = $num;
- $data['rows'] = $info;
- echo json_encode($data);
- }
- /**
- * 编辑窗口
- * @return type
- */
- public function edit() {
- $doctors = null;
- if (isset($_GET["id"])) {
- $id = $_GET["id"];
- if ($id != null) {
- $doctors = Db::table("doctors")->where("id", $id)->find();
- if (count($doctors) > 0) {
- if (empty($doctors['doctor_role'])) {
- $doctors['doctor_role'] = array();
- } else {
- $doctors['doctor_role'] = explode(',', $doctors['doctor_role']);
- }
- $this->assign("doctors", $doctors);
- $exam_class = explode(',',$doctors['exam_class']);
- $this->assign('examcla',$exam_class);
- // 查询医生的分类
- $doctorsCla = Db::table("doctor_class")->where("doctor_id", $id)->find();
- if (!empty($doctorsCla)) {
- $doctorClaStr = $doctorsCla["doctor_class"];
- if (!empty($doctorClaStr)) {
- $dc_arr = explode(",", $doctorClaStr);
- $this->assign("doctorcla", $dc_arr);
- }
- }
- }
- }
- }
- $doctorClas = Db::table("constant")->where("parent_id", "doctor_class")->order("ordernum", "1")->select();
- $this->assign('doctorclas', $doctorClas);
- $examCla = DB::table('constant')->where('parent_id','exam_class')->select();
- $this->assign('examclass',$examCla);
- $institution = DB::table('institution')->select();
- $this->assign('institution', $institution);
- $depWheres = array();
- if ($doctors != null) {
- $depWheres["institution_id"] = $doctors["institution_id"];
- }
- $department = DB::table('department')->where($depWheres)->select();
- $this->assign('department', $department);
- return $this->fetch('edit');
- }
- public function save() {
- $request = Request::instance();
- $params = $request->param();
- if (!isset($params["doctorcla"])) {
- echo "fail:doctorcla";
- return;
- }
- $doctorcla = $params["doctorcla"];
- unset($params["doctorcla"]);
- if(isset($params['doctor_role']) && !empty($params['doctor_role'])){
- $params['doctor_role'] = implode(',', $params['doctor_role']);
- }
- $password = $params['password'];
- if(isset($params['examcla'])){
- $params['exam_class'] = implode(',',$params['examcla']);
- unset($params['examcla']);
- }
- if (!empty($password) && strlen($password) < 30) {
- $params['password'] = md5($password);
- } else {
- unset($params['password']);
- }
- if (empty($params['id'])) {
- unset($params['id']);
- $id = UUIDs::uuid16();
- $params['id'] = $id;
- $a = DB::table('doctors')->insert($params);
- if (isset($params["department_id"])) {
- $this->saveDoctorCla($id, $doctorcla, $params["department_id"]);
- SysLogs::log("doctors", "C", json_encode($params));
- }
- return 'insert_ok;' . $id;
- } else {
- $a = DB::table('doctors')->where('id', $params['id'])->update($params);
- if (isset($params["department_id"])) {
- $this->saveDoctorCla($params["id"], $doctorcla, $params["department_id"]);
- }
- SysLogs::log("doctors", "U", $params['id'] . " --> " . json_encode($params));
- return 'success';
- }
- }
- protected function saveDoctorCla($doctor_id, $doctorcla, $depid) {
- // doctorcla
- Db::table("doctor_class")->where("doctor_id", $doctor_id)->delete();
- if (isset($doctorcla) && count($doctorcla) > 0) {
- $newrow = array();
- $newrow["id"] = UUIDs::uuid16();
- $newrow["doctor_id"] = $doctor_id;
- $newrow["department_id"] = $depid;
- $doctorclaStr = json_encode($doctorcla);
- $doctorclaStr = str_replace('"', '', $doctorclaStr);
- $doctorclaStr = str_replace('[', '', $doctorclaStr);
- $doctorclaStr = str_replace(']', '', $doctorclaStr);
- $doctorclaStr = str_replace(" ", "", $doctorclaStr);
- $newrow["doctor_class"] = $doctorclaStr;
- $newrow["status"] = "1";
- Db::table("doctor_class")->insert($newrow);
- SysLogs::log("doctors", "C", $doctor_id . " doctorclass --> " . $doctorclaStr);
- }
- }
- /**
- * 软删除记录
- */
- public function delete() {
- if (isset($_GET["ids"])) {
- $ids = $_GET["ids"];
- if (!empty($ids)) {
- $idArr = explode(",", $ids);
- if (count($idArr) > 0) {
- Db::table("doctors")->where("id", "in", $idArr)->update(['status' => 1]);
- SysLogs::log("doctors", "U", $ids . " status 修改为 1 ");
- }
- echo "delete_ok";
- return;
- }
- }
- echo "fail";
- }
- /**
- * 显示权限编辑窗口
- * @return type
- */
- public function permissions() {
- $id = is_string($_GET["id"]) ? $_GET["id"] : null;
- if ($id != null) {
- // 得到医生信息
- $doctor = Db::table("doctors")->where("id", $id)->find();
- if (empty($doctor)) {
- echo "no doctor !";
- return;
- }
- $this->assign("doctor", $doctor);
- $this->assign("id", $id);
- }
- // 查找已有权限(菜单)
- $permitsMenus = Db::table("dr_cla_permission")->where("doctor_id", $id)->where("type", "1")->select();
- $permitMenuIdArr = array();
- if (count($permitsMenus) > 0) {
- foreach ($permitsMenus as $key => $val) {
- array_push($permitMenuIdArr, $val["pass"]);
- }
- $this->assign("permitMenuIdArr", json_encode($permitMenuIdArr));
- }
- // 查找已有权限(写报告)
- $permitsReport = Db::table("dr_cla_permission")->where("doctor_id", $id)->where("type", "2")->find();
- if (!empty($permitsReport)) {
- $this->assign("permitReport", $permitsReport["pass"]);
- } else {
- $this->assign("permitReport", "0");
- }
- return $this->fetch("permissions");
- }
- /**
- * 保存菜单权限
- */
- public function saveMenuPermit() {
- $doctorId = empty($_GET["id"]) ? null : $_GET["id"];
- $menuIds = empty($_GET["ids"]) ? null : $_GET["ids"];
- if (empty($doctorId) || empty($menuIds)) {
- echo "fail, empty doctorId or menuId";
- return;
- }
- $menuIdArr = explode(",", $menuIds);
- $menus = Db::table("menu")->whereIn("id", $menuIdArr)->select();
- if (count($menus) < 1) {
- // 没有找到菜单
- echo "fail, no menu found ";
- return;
- } else {
- // 给用户赋值
- // 查看是否已经有了权限
- $menuIdArr = array();
- foreach ($menus as $key => $val) {
- array_push($menuIdArr, $val["id"]);
- }
- // 将已有权限全部清空
- Db::table("dr_cla_permission")->where("doctor_id", $doctorId)->where("type", "1")->delete();
- SysLogs::log("dr_cla_permission", "D", "where (doctor_id = " . $doctorId . "type = 1) delete ");
- // 如果没有,添加
- foreach ($menuIdArr as $key => $val) {
- $newRow = array();
- $newRow["id"] = UUIDs::uuid16();
- $newRow["type"] = "1";
- $newRow["pass"] = $val;
- $newRow["doctor_id"] = $doctorId;
- Db::table("dr_cla_permission")->insert($newRow);
- SysLogs::log("dr_cla_permission", "C", json_encode($newRow));
- }
- }
- echo "ok";
- }
- /**
- * 保存报告权限
- */
- public function saveReportPermit() {
- $doctorId = isset($_GET["id"]) ? $_GET["id"] : null;
- $permitReport = isset($_GET["report"]) ? $_GET["report"] : null;
- if ($doctorId == null || $permitReport == null) {
- echo "fail";
- return;
- }
- $permitData = Db::table("dr_cla_permission")->where("doctor_id", $doctorId)->where("type", "2")->find();
- if (!empty($permitData)) {
- // 如果已经有报告权限的配置,
- // 只更新就好
- $permitData["pass"] = $permitReport;
- Db::table("dr_cla_permission")->update($permitData);
- SysLogs::log("dr_cla_permission", "U", json_encode($permitData));
- } else {
- // 新建权限记录
- $newRow = array();
- $newRow["id"] = UUIDs::uuid16();
- $newRow["doctor_id"] = $doctorId;
- $newRow["type"] = "2";
- $newRow["pass"] = $permitReport;
- Db::table("dr_cla_permission")->insert($newRow);
- SysLogs::log("dr_cla_permission", "C", json_encode($newRow));
- }
- echo "ok";
- }
- /**
- * 查询全部菜单
- */
- public function menudata() {
- $rootMenuData = array();
- // 准备根节点
- $rootMenuData["id"] = "root";
- $rootMenuData["pId"] = "0";
- $rootMenuData["name"] = "菜单(根节点)";
- $rootMenuData["url"] = "";
- $rootMenuData["open"] = "true";
- // 查询全部数据
- $menuData = $info = DB::table('menu')->select();
- $jsonarray = array();
- if ($menuData != null) {
- foreach ($menuData as $k => $val) {
- $parent_id = $val["parent_id"];
- unset($val["parent_id"]);
- // 处理parent_id为pId,为前端菜单上下级关系展示处理
- $val['pId'] = $parent_id;
- $val['open '] = "true";
- array_push($jsonarray, $val);
- }
- }
- // 将根节点添加到树
- array_unshift($jsonarray, $rootMenuData);
- // 返回JSON数据
- echo json_encode($jsonarray);
- }
- public function import(){
- $objPHPExcel = new PHPExcel();
- //获取表单上传文件
- $file = request()->file('excel');
- $info = $file->validate(['size'=>99999,'ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
- if($info){
- $exclePath = $info->getSaveName(); //获取文件名
- $file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $exclePath; //上传文件的地址
- $objReader = new \PHPExcel_Reader_Excel2007();
- if(!$objReader->canRead($file_name)){
- $objReader = PHPExcel_IOFactory::createReader('Excel5');
- }
- $obj_PHPExcel =$objReader->load($file_name); //加载文件内容,编码utf-8
- echo "<pre>";
- $excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
- array_shift($excel_array); //删除第一个数组(标题);
- $data = [];
- $dj = array();
- $yc = array();
- $bgqx = array();
- $bg = array();
- foreach($excel_array as $k=>$v) {
- if(empty($v[2])){
- continue;
- }
- $data[$k]['id'] = UUIDs::uuid16();
- $data[$k]['realname'] = $v[0];
- $data[$k]['doctor_title'] = isset($v[1]) ? $v[1] : '';
- $data[$k]['phone'] = $v[2];
- $data[$k]['username'] = $v[2];
- $data[$k]['department_id'] = $this->getdid(isset($v[3]) ? $v[3] : '',$v[4]);
- $data[$k]['institution_id'] = $v[4];
- $data[$k]['is_report'] = 0;
- $data[$k]['password'] = md5('123456');
- // 报告权限
- $bgqx[$k]['doctor_id'] = $data[$k]['id'];
- $bgqx[$k]['department_id'] = $data[$k]['department_id'];
- $bgqx[$k]['doctor_class'] = $v[5];
- $bgqx[$k]['id'] = UUIDs::uuid16();
- if(!empty($v[6])){
- // 本地报告权限
- $bg[$k]['id'] = UUIDs::uuid16();
- $bg[$k]['doctor_id'] = $data[$k]['id'];
- $bg[$k]['pass'] = '80ddb7d09ebc44b4';
- $bg[$k]['type'] = 1;
- }
- if(!empty($v[7])){
- // 登记列表权限
- $dj[$k]['id'] = UUIDs::uuid16();
- $dj[$k]['doctor_id'] = $data[$k]['id'];
- $dj[$k]['pass'] = '0eac42ef01de23ff';
- $dj[$k]['type'] = 1;
- }
- if(!empty($v[8])){
- // 远程列表权限
- $yc[$k]['id'] = UUIDs::uuid16();
- $yc[$k]['doctor_id'] = $data[$k]['id'];
- $yc[$k]['pass'] = '0aa9d7b2fdf7268e';
- $yc[$k]['type'] = 1;
- }
- }
- $success=Db::name('doctors')->insertAll($data); //批量插入数据
- $pbg = DB::table('doctor_class')->insertAll($bgqx); //批评插入报告权限
- $pyc = DB::table('dr_cla_permission')->insertAll($yc); //批评插入远程列表权限
- $pdj = DB::table('dr_cla_permission')->insertAll($dj); //批评插入登记列表权限
- $pbgl = DB::table('dr_cla_permission')->insertAll($bg); //批评插入报告列表权限
- SysLogs::log("doctors", "c", '导入医生名单并分配权限,');
- $this->success($success.'条插入成功');
- // $error=$i-$success;
- // echo "总{$i}条,成功{$success}条,失败{$error}条。";
- }else{
- // 上传失败获取错误信息
- echo $file->getError();
- }
- }
- // $depa_name 科室名称 $ins_id 医疗机构id
- public function getdid($depa_name,$ins_id){
- if(empty($depa_name)){
- return '';
- }
- $info = DB::table('department')->where('institution_id',$ins_id)->where('department_name',$depa_name)->find();
- if($info){
- return $info['id'];
- }else{
- $dinfo = array();
- $dinfo['id'] = UUIDs::uuid16();
- $dinfo['department_name'] = $depa_name;
- $dinfo['institution_id'] = $ins_id;
- $dinfo['parent_id'] = 'root';
- $dinfo['is_report'] = 0;
- DB::table('department')->insert($dinfo);
- return $dinfo['id'];
- }
- }
- }
|