Institution.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. <?php
  2. namespace app\admin\controller\institution;
  3. use app\admin\controller\syslog\SysLog;
  4. use app\admin\library\DingTalk;
  5. use app\admin\model\doctors\DoctorModel;
  6. use app\admin\model\institution\InsDockingField;
  7. use app\admin\model\institution\InsMonitoring;
  8. use app\admin\service\auth\AdminService;
  9. use app\admin\service\institution\InstitutionService;
  10. use app\common\controller\Backend;
  11. use app\common\library\SysLogs;
  12. use fast\Http;
  13. use think\Config;
  14. use think\Db;
  15. use think\Exception;
  16. use think\exception\DbException;
  17. use think\exception\PDOException;
  18. use think\exception\ValidateException;
  19. use think\response\Json as JsonAlias;
  20. use think\Session;
  21. /**
  22. * 医疗机构管理
  23. *
  24. * @icon fa fa-institution
  25. */
  26. class Institution extends Backend
  27. {
  28. /**
  29. * Institution模型对象
  30. * @var \app\admin\model\institution\Institution
  31. */
  32. protected $model = null;
  33. protected $rulelist = [];
  34. protected $noNeedRight = ['levelselectlist','institutionselectlist','agentselectlist','saleselectlist','departselectlist','doctorselectlist','institutioncontactlist', 'initremotedoctor', 'getlnglat', 'conditiondict'];
  35. protected $noNeedLogin = ['checkmonitoring'];
  36. /**
  37. * 快速搜索时执行查找的字段
  38. */
  39. protected $searchFields = ['id','name'];
  40. /**
  41. * 代理商权限组id
  42. * @var string
  43. */
  44. protected $agentGroupId = '3';
  45. /**
  46. * @var string
  47. */
  48. protected $saleGroupIds = [
  49. 6,
  50. 7
  51. ];
  52. protected $charge_mode = [
  53. 'HZFF' => 1,
  54. 'YYTG' => 2,
  55. 'YYDS' => 3
  56. ];
  57. public function _initialize()
  58. {
  59. parent::_initialize();
  60. $this->model = new \app\admin\model\institution\Institution;
  61. }
  62. /**
  63. * 列表
  64. * @return string|JsonAlias
  65. * @throws Exception
  66. * @author matielong
  67. */
  68. public function index()
  69. {
  70. // 设置过滤方法
  71. $this->request->filter(['strip_tags']);
  72. if ($this->request->isAjax()) {
  73. //如果发送的来源是Selectpage,则转发到Selectpage
  74. if ($this->request->request('keyField')) {
  75. return $this->selectpage();
  76. }
  77. list($where, $sort, $order, $offset, $limit) = $this->buildparams($this->searchFields,true);
  78. // 只查看自己机构以及下级机构的信息
  79. $childInsIds = $this->auth->getMyInsId();
  80. if($childInsIds == false){
  81. $more = false;
  82. } else {
  83. $more = ['institution.id' => ['in', $childInsIds]];
  84. }
  85. // 关联
  86. $join = [['fa_admin', 'institution.agent_id = fa_admin.id', 'LEFT']];
  87. $field = ['institution.*','fa_admin.nickname as agent_name'];
  88. try{
  89. $total = $this->model
  90. ->join($join)
  91. ->where($more)
  92. ->where($where)
  93. ->count();
  94. $list = $this->model
  95. ->join($join)
  96. ->where($more)
  97. ->where($where)
  98. ->order($sort, $order)
  99. ->limit($offset, $limit)
  100. ->field($field)
  101. ->select();
  102. foreach ($list as $k=>$v)
  103. {
  104. if(is_object($v))
  105. {
  106. foreach ($v->toArray() as $k1=>$v1)
  107. {
  108. $list[$k][strtolower($k1)] = $v1;
  109. }
  110. }
  111. }
  112. } catch ( Exception $e){
  113. $this->error($e->getMessage());
  114. }
  115. $result = array("total" => $total, "rows" => $list);
  116. return json($result);
  117. }
  118. return $this->view->fetch();
  119. }
  120. /**
  121. * 添加
  122. */
  123. public function add()
  124. {
  125. if ($this->request->isPost()) {
  126. $params = $this->request->post("row/a");
  127. if ($params) {
  128. $params = $this->preExcludeFields($params);
  129. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  130. $params[$this->dataLimitField] = $this->auth->id;
  131. }
  132. $result = false;
  133. Db::startTrans();
  134. try {
  135. //是否采用模型验证
  136. if ($this->modelValidate) {
  137. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  138. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  139. $this->model->validateFailException(true)->validate($validate);
  140. }
  141. if(empty($params['install_time'])){
  142. $params['install_time'] = null;
  143. }
  144. // 机构ID是否存在
  145. $is_exist = $this->model->where('id', $params['id'])->value('id');
  146. if($is_exist){
  147. $this->error('机构ID已存在!');
  148. }
  149. // 去掉顶级节点 0
  150. // $params['parent_institution'] = $params['parent_institution'] == '0' ? '0' : '';
  151. // 省市区
  152. $area = explode('/',$params['area']);
  153. $params['ris_province'] = $area[0] ?? '';
  154. $params['ris_city'] = $area[1] ?? '';
  155. $params['ris_district'] = $area[2] ?? '';
  156. foreach ($params['institution_condition'] as $k=>$v)
  157. {
  158. if($v == '-1')
  159. {
  160. unset($params['institution_condition'][$k]);
  161. }
  162. }
  163. $filter = [];
  164. if(!empty($params['exam_datetime']))
  165. {
  166. $filter['exam_datetime'] = $params['exam_datetime'];
  167. }
  168. unset($params['exam_datetime']);
  169. if(!empty($params['exam_class']))
  170. {
  171. $filter['exam_class'] = $params['exam_class'];
  172. }
  173. unset($params['exam_class']);
  174. $params['filter'] = json_encode($filter);
  175. $params['institution_condition'] = implode(',',$params['institution_condition']);
  176. $params['custom_field'] = implode(',',$params['custom_field']);
  177. unset($params['area']);
  178. if($params['sale_id']){
  179. $names = model('Admin')
  180. ->whereIn('id',explode(',', $params['sale_id']))
  181. ->column('nickname');
  182. $params['sale_name'] = implode(',', $names);
  183. }
  184. $arr = [];
  185. foreach ($params as $k=>$v){
  186. $arr[strtoupper($k)] = $v;
  187. }
  188. $result = $this->model->allowField(true)->save($arr);
  189. // 添加机构账号对应关系
  190. $uid = $this->auth->getUserInfo()['id'];
  191. $institution_id = $params['id'];
  192. $insert = [
  193. [
  194. 'uid' => $uid ,
  195. 'institution_id' => $institution_id
  196. ]
  197. ];
  198. if($params['sale_id']){
  199. foreach (explode(',', $params['sale_id']) as $sale_id){
  200. $insert[] = [
  201. 'uid' => $sale_id ,
  202. 'institution_id' => $institution_id
  203. ];
  204. }
  205. }
  206. model('AuthInstitutionAccess')->insertAll($insert);
  207. SysLog::recode("institution", "C", $params);
  208. Db::commit();
  209. } catch (ValidateException $e) {
  210. Db::rollback();
  211. $this->error($e->getMessage());
  212. } catch (PDOException $e) {
  213. Db::rollback();
  214. $this->error($e->getMessage());
  215. } catch (Exception $e) {
  216. Db::rollback();
  217. $this->error($e->getMessage());
  218. }
  219. if ($result !== false) {
  220. $this->success();
  221. } else {
  222. $this->error(__('No rows were inserted'));
  223. }
  224. }
  225. $this->error(__('Parameter %s can not be empty', ''));
  226. }
  227. $this->view->conditionDict = $this->conditionDict();
  228. $this->view->customField = Config::get('ins_custom_field');
  229. return $this->view->fetch();
  230. }
  231. /**
  232. * 编辑
  233. */
  234. public function edit($ids = null)
  235. {
  236. $row = $this->model->get($ids);
  237. if (!$row) {
  238. $this->error(__('No Results were found'));
  239. }
  240. $adminIds = $this->getDataLimitAdminIds();
  241. if (is_array($adminIds)) {
  242. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  243. $this->error(__('You have no permission'));
  244. }
  245. }
  246. if ($this->request->isPost()) {
  247. $params = $this->request->post("row/a");
  248. if ($params) {
  249. $params = $this->preExcludeFields($params);
  250. $result = false;
  251. Db::startTrans();
  252. try {
  253. //是否采用模型验证
  254. if ($this->modelValidate) {
  255. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  256. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  257. $row->validateFailException(true)->validate($validate);
  258. }
  259. // 去掉顶级节点 0
  260. // $params['parent_institution'] = $params['parent_institution'] == '0' ? '' : $params['parent_institution'];
  261. // 省市区
  262. if(empty($params['install_time'])){
  263. $params['install_time'] = null;
  264. }
  265. $area = explode('/',$params['area']);
  266. $params['ris_province'] = $area[0] ?? '';
  267. $params['ris_city'] = $area[1] ?? '';
  268. $params['ris_district'] = $area[2] ?? '';
  269. foreach ($params['institution_condition'] as $k=>$v)
  270. {
  271. if($v == '-1')
  272. {
  273. unset($params['institution_condition'][$k]);
  274. }
  275. }
  276. $filter = [];
  277. if(!empty($params['exam_datetime']))
  278. {
  279. $filter['exam_datetime'] = $params['exam_datetime'];
  280. }
  281. unset($params['exam_datetime']);
  282. if(!empty($params['exam_class']))
  283. {
  284. $filter['exam_class'] = $params['exam_class'];
  285. }
  286. unset($params['exam_class']);
  287. $params['filter'] = json_encode($filter);
  288. $params['institution_condition'] = implode(',',$params['institution_condition']);
  289. $params['custom_field'] = implode(',',$params['custom_field']);
  290. unset($params['area']);
  291. if($params['sale_id']){
  292. $params['sale_name'] = model('Admin')->where('id',$params['sale_id'])->value('nickname');
  293. $res = $this->updateSaleInsId($row['sale_id'], $params['sale_id'], $params['id']);
  294. if($res === false){
  295. Db::rollback();
  296. $this->error('更新机构绑定的销售人员失败');
  297. }
  298. }
  299. $result = $this->model->where('id',$ids)->update($params);
  300. $params['old_id'] = $ids;
  301. SysLog::recode("institution", "U", $params);
  302. Db::commit();
  303. } catch (ValidateException $e) {
  304. Db::rollback();
  305. $this->error($e->getMessage());
  306. } catch (PDOException $e) {
  307. Db::rollback();
  308. $this->error($e->getMessage());
  309. } catch (Exception $e) {
  310. Db::rollback();
  311. $this->error($e->getMessage());
  312. }
  313. if ($result !== false) {
  314. $this->success();
  315. } else {
  316. $this->error(__('No rows were updated'));
  317. }
  318. }
  319. $this->error(__('Parameter %s can not be empty', ''));
  320. }
  321. // $row['parent_institution'] = empty($row['parent_institution']) ? 0 : $row['parent_institution'];
  322. $row['area'] = $row['ris_province'] . '/' . $row['ris_city'] . '/' . $row['ris_district'];
  323. if($row['institution_condition'] === '')
  324. {
  325. $row['institution_condition'] = ['-1'];
  326. }else{
  327. $row['institution_condition'] = explode(',',$row['institution_condition']);
  328. }
  329. if($row['custom_field'] === '')
  330. {
  331. $row['custom_field'] = [];
  332. }else{
  333. $row['custom_field'] = explode(',',$row['custom_field']);
  334. }
  335. $filter_arr = json_decode($row['filter'],true);
  336. $row['exam_class'] = $filter_arr['exam_class'] ?? '';
  337. $row['exam_datetime'] = $filter_arr['exam_datetime'] ?? '';
  338. $this->view->assign("row", $row);
  339. $this->view->customField = Config::get('ins_custom_field');
  340. $this->view->conditionDict = $this->conditionDict();
  341. return $this->view->fetch();
  342. }
  343. /**
  344. * 删除
  345. */
  346. public function del($ids = "")
  347. {
  348. if ($ids) {
  349. $pk = $this->model->getPk();
  350. $adminIds = $this->getDataLimitAdminIds();
  351. if (is_array($adminIds)) {
  352. $this->model->where($this->dataLimitField, 'in', $adminIds);
  353. }
  354. $list = $this->model->where($pk, 'in', $ids)->select();
  355. $count = 0;
  356. Db::startTrans();
  357. try {
  358. foreach ($list as $k => $v) {
  359. $count += $v->delete();
  360. model('AuthInstitutionAccess')->where('institution_id',$v['id'])->delete();
  361. }
  362. SysLog::recode("institution", "D", $ids);
  363. Db::commit();
  364. } catch (PDOException $e) {
  365. Db::rollback();
  366. $this->error($e->getMessage());
  367. } catch (Exception $e) {
  368. Db::rollback();
  369. $this->error($e->getMessage());
  370. }
  371. if ($count) {
  372. $this->success();
  373. } else {
  374. $this->error(__('No rows were deleted'));
  375. }
  376. }
  377. $this->error(__('Parameter %s can not be empty', 'ids'));
  378. }
  379. /**
  380. * 等级下拉
  381. * @author matielong
  382. */
  383. public function levelSelectList()
  384. {
  385. try{
  386. $list = $this->model
  387. ->group('institution_level')
  388. ->column('institution_level');
  389. } catch ( DbException $exception){
  390. $this->error($exception->getMessage());
  391. }
  392. $data = [];
  393. foreach ($list as $v){
  394. if(empty($v)) continue;
  395. $data[] = ['id'=>$v,'name'=>$v];
  396. }
  397. $data = ['searchlist' => $data];
  398. $this->success('success', null, $data);
  399. }
  400. /**
  401. * 机构搜索列表
  402. * @return JsonAlias
  403. * @author matielong
  404. */
  405. public function institutionSelectList()
  406. {
  407. try{
  408. // 当前可见机构
  409. if($this->auth->isSuperAdmin()){
  410. $more = false;
  411. } else {
  412. $admin = $this->auth->getUserInfo();
  413. $childInsIds = $admin['institution']['my_institution'];
  414. $more = ['id' => ['in', $childInsIds]];
  415. }
  416. // 获取列表
  417. $total = $this->model
  418. ->where($more)
  419. ->count();
  420. $list = $this->model
  421. ->where($more)
  422. ->field('id,name,parent_institution')
  423. ->select();
  424. foreach ($list as $key => &$val){
  425. $val['index'] = $key;
  426. }
  427. // 格式化
  428. return json(array("total" => $total, "rows" => $list));
  429. } catch ( Exception $exception){
  430. $this->error($exception->getMessage());
  431. }
  432. }
  433. /**
  434. * 代理商下拉列表
  435. * @return JsonAlias
  436. * @author matielong
  437. */
  438. public function agentSelectList()
  439. {
  440. try {
  441. $uid = model('AuthGroupAccess')
  442. ->where('group_id', $this->agentGroupId)
  443. ->column('uid');
  444. // 查询
  445. $where = [
  446. 'id' => ['in', $uid],
  447. 'status' => '1'
  448. ];
  449. $total = model('Admin')
  450. ->where($where)
  451. ->count();
  452. $list = model('Admin')
  453. ->where($where)
  454. ->field('id,username as name')
  455. ->select();
  456. return json(array("total" => $total, "rows" => $list));
  457. } catch (Exception $exception) {
  458. $this->error($exception->getMessage());
  459. }
  460. }
  461. /**
  462. * 代表销售下拉列表
  463. * @return JsonAlias
  464. * @author matielong
  465. */
  466. public function saleSelectList()
  467. {
  468. try {
  469. $uid = model('AuthGroupAccess')
  470. ->whereIn('group_id', $this->saleGroupIds)
  471. ->column('uid');
  472. // 查询
  473. $where = [
  474. 'id' => ['in', $uid],
  475. 'status' => '1'
  476. ];
  477. $total = model('Admin')
  478. ->where($where)
  479. ->count();
  480. $list = model('Admin')
  481. ->where($where)
  482. ->field('id,nickname as name')
  483. ->select();
  484. return json(array("total" => $total, "rows" => $list));
  485. } catch (Exception $exception) {
  486. $this->error($exception->getMessage());
  487. }
  488. }
  489. /**
  490. * 科室下拉选择
  491. * @param $institution_id
  492. * @return JsonAlias
  493. * @author matielong
  494. */
  495. public function departSelectList($institution_id)
  496. {
  497. try{
  498. $total = model('Depart','model\institution')
  499. ->where('institution_id',$institution_id)
  500. ->count();
  501. $list = model('Depart','model\institution')
  502. ->where('institution_id',$institution_id)
  503. ->field('id,department_name as name')
  504. ->order('order_num')
  505. ->select();
  506. foreach ($list as $key => &$val){
  507. $val['index'] = $key;
  508. }
  509. return json(array("total" => $total, "rows" => $list));
  510. } catch ( Exception $exception){
  511. $this->error($exception->getMessage());
  512. }
  513. }
  514. // 当前医院医生下拉
  515. public function doctorSelectList($institution_id)
  516. {
  517. try{
  518. $total = DoctorModel::where('institution_id',$institution_id)
  519. ->count();
  520. $list = DoctorModel::where('institution_id',$institution_id)
  521. ->field('id,realname as name')
  522. ->order('createdAt')
  523. ->select();
  524. return json(array("total" => $total, "rows" => $list));
  525. } catch ( Exception $exception){
  526. $this->error($exception->getMessage());
  527. }
  528. }
  529. /**
  530. * 上下级机构选择
  531. * @param $level
  532. * @return bool|JsonAlias
  533. */
  534. public function institutionContactList($level)
  535. {
  536. try{
  537. // 当前可见机构
  538. if($this->auth->isSuperAdmin()){
  539. $more = false;
  540. } else {
  541. $admin = $this->auth->getUserInfo();
  542. $childInsIds = $admin['institution']['child_institution'];
  543. $more = ['id' => ['in', $childInsIds]];
  544. }
  545. // 判断上级下级
  546. switch ($level){
  547. case 'lower':
  548. // $where = ['super_level' => 0];
  549. $where = false;
  550. break;
  551. case 'super':
  552. $where = ['super_level' => 1];
  553. $more = false;
  554. break;
  555. default:
  556. return false;
  557. }
  558. // 获取列表
  559. $total = $this->model
  560. ->where($more)
  561. ->where($where)
  562. ->count();
  563. $list = $this->model
  564. ->where($more)
  565. ->where($where)
  566. ->field('id,name,parent_institution')
  567. ->select();
  568. foreach ($list as $key => &$val){
  569. $val['index'] = $key;
  570. }
  571. // 格式化
  572. return json(array("total" => $total, "rows" => $list));
  573. } catch ( Exception $exception){
  574. $this->error($exception->getMessage());
  575. }
  576. }
  577. /**
  578. * 获取远程医生
  579. * @param $institution_id
  580. * @return false|\PDOStatement|string|\think\Collection
  581. */
  582. public function initRemoteDoctor($institution_id)
  583. {
  584. try {
  585. $where = [
  586. 'institution_id' => $institution_id,
  587. 'status' => '1',
  588. ];
  589. $remote_doctor_id = '3002';
  590. $total =
  591. model('DoctorModel','model\doctors')
  592. ->where($where)
  593. ->whereLike('doctor_role',"%$remote_doctor_id%")
  594. ->count();
  595. $list = model('DoctorModel','model\doctors')
  596. ->where($where)
  597. ->whereLike('doctor_role',"%$remote_doctor_id%")
  598. ->field('id,realname as name')
  599. ->select();
  600. $ids = implode(',', array_column($list, 'id'));
  601. return json(array("total" => $total, "rows" => $list, "ids" => $ids));
  602. } catch ( Exception $exception){
  603. $this->error($exception->getMessage());
  604. }
  605. }
  606. /**
  607. * 更新收费模式
  608. * @param $ids
  609. * @return string
  610. * @throws DbException
  611. * @throws Exception
  612. */
  613. public function setChargeMode($ids)
  614. {
  615. $ins = $this->model->get($ids);
  616. if($this->request->isPost()){
  617. $info = $this->request->post();
  618. if(!in_array($info['charge_mode'], $this->charge_mode)){
  619. $this->error('收费模式传值错误');
  620. }
  621. if($info['film_price'] < 0){
  622. $this->error('金额传值错误');
  623. }
  624. $res = $this->model->where('id', $ids)->update([
  625. 'charge_mode' => $info['charge_mode'],
  626. 'film_price' => $info['film_price'] * 100,
  627. 'supplement_pay' => $info['supplement_pay']
  628. ]);
  629. if(!$res){
  630. $this->error('更新失败');
  631. }
  632. $this->success();
  633. return true;
  634. }
  635. $this->view->ins = $ins;
  636. return $this->view->fetch('charge_mode');
  637. }
  638. /**
  639. * 更新机构绑定的销售人员
  640. * @param $old_sale_id
  641. * @param $new_sale_id
  642. * @param $ins_id
  643. * @return bool|int|string
  644. */
  645. protected function updateSaleInsId($old_sale_id, $new_sale_id, $ins_id)
  646. {
  647. if($old_sale_id == $new_sale_id){
  648. return true;
  649. }
  650. $res = model('AuthInstitutionAccess')
  651. ->whereIn('uid', explode(',', $old_sale_id))
  652. ->where('institution_id', $ins_id)
  653. ->delete();
  654. if(!$res){
  655. return false;
  656. }
  657. $insert = [];
  658. foreach (explode(',', $new_sale_id) as $sale_id){
  659. $insert[] = [
  660. 'uid' => $sale_id,
  661. 'institution_id' => $ins_id,
  662. ];
  663. }
  664. $res = model('AuthInstitutionAccess')->insertAll($insert);
  665. return $res;
  666. }
  667. /**
  668. * 获取经纬度
  669. */
  670. public function getLngLat()
  671. {
  672. $name = $this->request->post('name');
  673. $ak = '6e7njING4jPNrRGjG2RibUcv37oWGzPi';
  674. $output = 'json';
  675. $api = Config::get('baidu_url');
  676. $url = $api."/geocoding/v3/?address=$name&output=$output&ak=$ak";
  677. $data = Http::get($url);
  678. if(!empty($data)){
  679. ini_set('precision',-1);
  680. $po = json_decode($data, true);
  681. $this->success('ok',null,[
  682. 'lng' => $po['result']['location']['lng'],
  683. 'lat' => $po['result']['location']['lat']
  684. ]);
  685. }
  686. $this->error();
  687. }
  688. protected function conditionDict()
  689. {
  690. return [
  691. '-1' => '未知' ,
  692. '0' => '手机' ,
  693. '1' => '身份证' ,
  694. '2' => '病历号' ,
  695. '3' => '检查号' ,
  696. '4' => '住院号' ,
  697. '5' => '门诊号' ,
  698. '6' => '病人id' ,
  699. ];
  700. }
  701. public function topUp($ids)
  702. {
  703. $ins = $this->model->get($ids);
  704. if(!$ins){
  705. return false;
  706. }
  707. if($this->request->isPost()){
  708. $money = (float) $this->request->post('money');
  709. if(!is_float($money)){
  710. $this->error('金额必须为一个数字');
  711. }
  712. if($money === 0){
  713. $this->error('金额不可以为0');
  714. }
  715. if($money < 0 && $ins['current_money'] < abs($money * 100))
  716. {
  717. $this->error('余额不足,无法扣除');
  718. }
  719. $acc = strlen(explode('.', $money)[1] ?? '');
  720. if($acc > 2){
  721. $this->error('小数点后最多2位');
  722. }
  723. $admin = Session::get('admin');
  724. $insert = [
  725. 'institution_id' => $ins['id'] ,
  726. 'institution_name' => $ins['name'] ,
  727. 'old_amount' => $ins['current_money'] ,
  728. 'amount' => $money * 100 ,
  729. 'user_id' => $admin['id'] ,
  730. 'user_name' => $admin['nickname'] ,
  731. 'created_time' => date('Y-m-d H:i:s')
  732. ];
  733. $new_amount = $ins['current_money'] + ($money * 100);
  734. Db::transaction(function () use ($ids, $new_amount, $insert){
  735. Db::table('ins_top_up_record')->insert($insert);
  736. $this->model->where('id', $ids)
  737. ->update([
  738. 'current_money' => $new_amount
  739. ]);
  740. });
  741. $this->success();
  742. }
  743. $record = Db::table('ins_top_up_record')
  744. ->order('id desc')
  745. ->where('institution_id', $ids)
  746. ->select();
  747. $this->view->record = $record;
  748. $this->view->ins = $ins;
  749. $this->view->money = $ins['current_money'] / 100;
  750. return $this->view->fetch('top_up');
  751. }
  752. //已废弃接口
  753. public function setDict($ids)
  754. {
  755. $ins = $this->model->get($ids);
  756. $model = model(InsDockingField::class);
  757. $dict = $model
  758. ->where('institution_id', $ids)
  759. ->column('field, name');
  760. $default = [
  761. '0' => ['field' => 'phone', 'name' => '手机号'],
  762. '1' => ['field' => 'card_num', 'name' => '身份证'],
  763. '2' => ['field' => 'patient_num', 'name' => '病历号'],
  764. '3' => ['field' => 'accession_num', 'name' => '检查号'],
  765. '4' => ['field' => 'hopitalized_no', 'name' => '住院号'],
  766. '5' => ['field' => 'out_patient', 'name' => '门诊号'],
  767. ];
  768. if($this->request->isPost()){
  769. $row = $this->request->post('row/a');
  770. Db::startTrans();
  771. foreach ($row as $key => $val){
  772. $where = [
  773. 'institution_id' => $ids,
  774. 'field' => $key
  775. ];
  776. $exist = $model->where($where)->find();
  777. if ($exist) {
  778. if(empty($val)){
  779. $res = $exist->delete();
  780. } else {
  781. $res = $exist->where($where)->update(['name' => $val]);
  782. }
  783. } else {
  784. if(empty($val)){
  785. continue;
  786. }
  787. $res = $model->where($where)->insert([
  788. 'institution_id' => $ids,
  789. 'name' => $val,
  790. 'field' => $key,
  791. ]);
  792. }
  793. if($res === false){
  794. Db::rollback();
  795. $this->error('操作失败');
  796. }
  797. }
  798. Db::commit();
  799. $this->success();
  800. }
  801. $this->view->ins = $ins;
  802. $this->view->assign([
  803. 'ins' => $ins,
  804. 'dict' => $dict,
  805. 'default' => $default,
  806. ]);
  807. return $this->view->fetch('set_dict');
  808. }
  809. public function setMonitoring($ids)
  810. {
  811. $ins = $this->model->get($ids);
  812. $user = $this->auth->getUserInfo();
  813. $mon_model = model(InsMonitoring::class);
  814. $mon = $mon_model->where('ins_id',$ids)->find();
  815. if($this->request->isPost()){
  816. $info = $this->request->post();
  817. $save = [
  818. 'ins_id' => $info['ins_id'],
  819. 'ins_name' => $ins['name'],
  820. 'open' => $info['open'],
  821. 'admin_id' => $user['id'],
  822. 'safe_time' => json_encode($info['safe_time']),
  823. 'warning_time' => $info['warning_time'],
  824. 'notice_type' => $info['notice_type']
  825. ];
  826. if ($mon) {
  827. $res = $mon_model->where('id', $mon['id'])->update($save);
  828. } else {
  829. $res = $mon_model->insert($save);
  830. }
  831. if(!$res){
  832. $this->error();
  833. }
  834. $this->success();
  835. }
  836. $this->view->ins = $ins;
  837. $this->view->user = $user;
  838. $this->view->mon = $mon;
  839. $this->view->status = $mon;
  840. $this->view->safe_time = $mon ? json_decode($mon['safe_time'], true) : [];
  841. return $this->view->fetch('set_monitoring');
  842. }
  843. public function checkMonitoring($token = '')
  844. {
  845. if(!$token){
  846. return 0;
  847. }
  848. if($token !== '9hfghn0fsdfa099eq31235n23nakdksad*^'){
  849. return 0;
  850. }
  851. $time = time();
  852. $ins_data = model(InsMonitoring::class)
  853. ->where('open', 1)
  854. ->select();
  855. foreach ($ins_data as $val){
  856. $warning_second = $val['warning_time'] * 60;
  857. if(!$warning_second){
  858. continue;
  859. }
  860. // 安全区间判断
  861. $safe_range = json_decode($val['safe_time'], true);
  862. $safe = false;
  863. $time_arr = [];
  864. foreach ($safe_range as $range){
  865. if($range[0] && $range[1]){
  866. $start = strtotime($range[0]);
  867. $end = strtotime($range[1]);
  868. $tom_start = $start + (3600 * 24);
  869. $tom_end = $end + (3600 * 24);
  870. $time_arr[] = $start;
  871. $time_arr[] = $end;
  872. $time_arr[] = $tom_start;
  873. $time_arr[] = $tom_end;
  874. if(($time > $start && $time < $end) || ($time < $start && $time > $end) || ($time > $tom_start && $time < $tom_end) || ($time < $tom_start && $time > $tom_end)){
  875. $safe = true;
  876. continue;
  877. }
  878. }
  879. }
  880. if($safe){
  881. continue;
  882. }
  883. $last_upload_time = InstitutionService::getLastUploadTime($val['ins_id']);
  884. if(!$last_upload_time){
  885. continue;
  886. }
  887. if($time - $last_upload_time > $warning_second){
  888. // 判断最(最后上传时间 ~ 当前预警时时间)区间是否存在安全时间段
  889. $closest = 0;
  890. foreach ($time_arr as $sa_time){
  891. if($last_upload_time <= $sa_time && $sa_time <= $time){
  892. $closest = $sa_time;
  893. }
  894. }
  895. if($closest === 0 || $time - $closest >= $warning_second){
  896. // 达到预警条件
  897. $res = InstitutionService::saveException($val['ins_id'], $val['ins_name'], $last_upload_time, $time);
  898. if($res && InstitutionService::$push === true){
  899. $datetime = date('Y-m-d H:i:s', $last_upload_time);
  900. DingTalk::instance()->sendInstitutionNotice($val['ins_name'], $datetime, []);
  901. }
  902. }
  903. }
  904. }
  905. return 1;
  906. }
  907. public function filterList()
  908. {
  909. $data = [
  910. ['id'=>'exam_datetime','name'=>'检查时间'],
  911. ['id'=>'exam_class','name'=>'检查类型']
  912. ];
  913. return json(array("total" => 2, "rows" => $data));
  914. }
  915. }