123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- <?php
- namespace app\api\servies;
- use app\api\actions\ZskkCache;
- use app\api\dao\doctor\DoctorDao;
- use app\api\model\institution\InstitutionModel;
- use app\api\model\rota\RotaModel;
- use app\common\base\service\BaseService;
- use app\common\base\actions\IBaseAction;
- use app\common\base\actions\IBaseLog;
- use app\api\actions\ZskkDefaultAction;
- use app\api\actions\ZskkDefaultLog;
- use app\common\library\uploadToCloud;
- use think\Db;
- /**
- * 后台控制器基类
- * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
- */
- class ZskkDefaultService extends BaseService implements IBaseAction, IBaseLog {
- use ZskkDefaultAction;
- use ZskkDefaultLog;
- use ZskkCache;
- protected $logName = "ZskkDefaultService";
- /**
- * 获取列表模糊搜索 where 条件
- * @param $params
- * @param $field
- * @return array|bool
- */
- public function getListFuzzyWhere($params, $field)
- {
- $where = false;
- if(isset($params['fuzzy_search']) && !empty($params['fuzzy_search'])){
- $where = '';
- $value = $params['fuzzy_search'];
- foreach ($field as $k=>$v){
- $where .= '`'. $v . '`' . ' LIKE '. ' \'%'. $value . '%\' ';
- // $where .= '`'. $v . '`' . ' = '. ' \''. $value . '\' ';
- if($k < count($field)-1){
- $where .= ' OR ';
- }
- }
- }
- return $where;
- }
- /**
- * 获取列表具体搜索 where 条件
- * @param $params
- * @param $field
- * @return array|bool
- */
- public function getListSpecificWhere($params, $field)
- {
- $where = false;
- if(isset($params['more_search']) && !empty($params['more_search'])){
- $more_search = $params['more_search'];
- $where = [];
- foreach ($more_search as $k=>$v){
- if(in_array($k, $field)){
- if($v['type'] == 'datetime'){
- if(empty($v['value'][0])){
- continue;
- }
- if($v['value'][0] == $v['value'][1]){
- $where[$k] = $v['value'][0];
- }else{
- $where[] = [$k,'between',[$v['value'][0], $v['value'][1]]];
- }
- }else{
- // 根据 type 获取具体的 where 条件
- $value = $this->formatSpecificWhere($v);
- if($value){
- $where[$k] = $value;
- }
- }
- }
- }
- }
- return $where;
- }
- //处理sex
- public function getSex($sexData)
- {
- $sex = '';
- switch ($sexData){
- case '男':
- $sex = 'M';
- break;
- case '女':
- $sex = 'F';
- break;
- case 'M':
- $sex = 'M';
- break;
- case 'F':
- $sex = 'F';
- break;
- default :
- $sex = $sexData;
- break;
- }
- return $sex;
- }
- /**
- * 格式化集体搜索 where 值
- * @param $value
- * @return array|bool
- * @author 马铁龙
- */
- public function formatSpecificWhere($value)
- {
- if(isset($value['type']) && !empty($value['type']) && isset($value['value']) && !empty($value['value'])){
- switch ($value['type']){
- case 'datetime':
- if(!isset($value['value'][0]) || empty($value['value'][0]) || !isset($value['value'][1]) || empty($value['value'][1])){
- return false;
- }
- return ['between',[$value['value'][0], $value['value'][1]]];
- case 'text' :
- if(!empty($value['value'])){
- return $value['value'];
- }
- return false;
- case 'in' :
- if(!empty($value['value'])){
- return ['in',explode(',', $value['value'])];
- }
- return false;
- case 'like':
- if(!empty($value['value'])){
- return [['like','%'.$value['value'].'%']];
- }
- return false;
- default:
- return false;
- }
- }
- return false;
- }
- public function getStageKey($report_id, $user_id)
- {
- return 'stage' . $report_id . $user_id;
- }
- public function curlJsonPost($url,$data)
- {
- $header = array(
- 'Authorization:'.'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJRCI6IjM0YmMxNjFmNWNjYmMyNzgyZGFkODkwYjYyNzE0Y2NiZjhhMzY0NzciLCJwYXRoIjoiXC9oaXNcL3YxXC90aWJldFwvbWVkaWNhbGNlbnRlcmFnZW50c1wvcHJlcGFyYXRpb25zIiwidXRjdGltZSI6IjIwMTktMDYtMjVUMDI6MjE6MjVaIn0.TZwTQxqvpXtBkE51fbzi2YYREO1LylH4EiQYWJ00i4o',
- 'Content-Type:'.'application/json;'
- );
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- public function curlJsonYbPost($url,$data)
- {
- $header = array(
- 'Authorization:'.'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJRCI6IjM0YmMxNjFmNWNjYmMyNzgyZGFkODkwYjYyNzE0Y2NiZjhhMzY0NzciLCJwYXRoIjoiXC9oaXNcL3YxXC90aWJldFwvbWVkaWNhbGNlbnRlcmFnZW50c1wvcHJlcGFyYXRpb25zIiwidXRjdGltZSI6IjIwMTktMDYtMjVUMDI6MjE6MjVaIn0.TZwTQxqvpXtBkE51fbzi2YYREO1LylH4EiQYWJ00i4o',
- 'Content-Type:'.'application/json;'
- );
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_TIMEOUT, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- function curlPost($url, $data) {
- //初使化init方法
- $ch = curl_init();
- //指定URL
- curl_setopt($ch, CURLOPT_URL, $url);
- //设定请求后返回结果
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- //声明使用POST方式来进行发送
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
- //发送什么数据呢
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- //忽略证书
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- //忽略header头信息
- curl_setopt($ch, CURLOPT_HEADER, 0);
- //设置超时时间
- // curl_setopt($ch, CURLOPT_TIMEOUT, 10);
- //发送请求
- $output = curl_exec($ch);
- //关闭curl
- curl_close($ch);
- //返回数据
- return $output;
- }
- // public function curl_get($url){
- // //初始化
- // $curl = curl_init();
- // //设置抓取的url
- // curl_setopt($curl, CURLOPT_URL, $url);
- // //设置头文件的信息作为数据流输出
- // curl_setopt($curl, CURLOPT_HEADER, 0);
- // //设置获取的信息以文件流的形式返回,而不是直接输出。
- // curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- // // 重定向
- // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
- // //执行命令
- // $data = curl_exec($curl);
- // //关闭URL请求
- // curl_close($curl);
- // return $data;
- // }
- function msectime() {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- return $msectime;
- }
- function request_by_curl($remote_server, $post_string,$header = array ('Content-Type: application/json;charset=utf-8')) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $remote_server);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
- // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
- // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- public function new_wechat_push($openid,$pushType,$content)
- {
- $c = urlencode($content);
- $url = "https://wechat.pacsonline.cn/wx_patient/api/sendMsg?openid=$openid&pushType=$pushType&content=$c";
- $data = $this->curl_get($url);
- return $data;
- }
- function curl_get($url){
- $header = array(
- 'Accept: application/json',
- );
- $curl = curl_init();
- //设置抓取的url
- curl_setopt($curl, CURLOPT_URL, $url);
- //设置头文件的信息作为数据流输出
- curl_setopt($curl, CURLOPT_HEADER, 0);
- // 超时设置,以秒为单位
- curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
- // 超时设置,以毫秒为单位
- // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
- // 设置请求头
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- //设置获取的信息以文件流的形式返回,而不是直接输出。
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- //执行命令
- $data = curl_exec($curl);
- return $data;
- // 显示错误信息
- // if (curl_error($curl)) {
- // print "Error: " . curl_error($curl);
- // } else {
- // // 打印返回的内容
- // curl_close($curl);
- // }
- }
- //计算总权重
- public function sum($arr)
- {
- $total = 0;
- foreach ($arr as $value) {
- $total += $value['weight'];
- }
- return $total;
- }
- // 处理队列
- public function handle_queue($institution)
- {
- //获取排队信息
- $queue = $this->getCache($institution.'_queue_'.date('Ymd'));
- //使用到了第几个
- $using = $this->getCache($institution.'_using_'.date('Ymd'));
- //医院每一轮的数量
- $allWeight = $this->getCache($institution.'_weight_'.date('Ymd'));
- if(empty($queue))
- {
- $insInfo = InstitutionModel::where('id',$institution)->find();
- if(empty($insInfo))
- {
- return "";
- }
- if($insInfo['is_auto_mode'] == '0')
- {
- // 没有开启自动分配
- return '';
- }
- $toady = date('Ymd');
- $rota = RotaModel::where('institution_id',$institution)->where('rota_datetime',$toady)->select()->toArray();
- if(empty($rota))
- {
- //没有今天的队列消息
- return '';
- }
- $using_num = 1;
- //获取医生列表
- $list = DB::table('doctors')->where('institution_id',$institution)->order('weight desc')->select();
- $arr = [];
- $use = [];
- foreach ($list as $k=>$v)
- {
- $arr[$v['id']] = $v['weight'];
- $use[$v['id']] = $v['weight'];
- }
- arsort($arr);
- arsort($use);
- // 获取权重总数
- $all = $this->sum($list);
- // 存储总权重
- $this->setCache($institution.'_weight_'.date('Ymd'),$all,86400);
- $data = [];
- for($i=1;$i<=$all;$i++)
- {
- if($i == 1)
- {
- //获取当前权重最大的一个医生
- $did = array_search(max($arr), $arr);
- $data[$i] = $did;
- //当前医生可使用-1
- $use[$did]--;
- }else{
- $second = [];
- //获取当前可使用权重最大的一个医生
- $did = array_search(max($use), $use);
- if($use[$did] == $arr[$did])
- {
- //当前医生权重已使用为最大权重 代表使用率为0 则分配给当前医生
- $data[$i] = $did;
- $use[$did] --;
- }else{
- $secondArray = $use;
- unset($second[$did]);
- $second = array_search(max($secondArray), $secondArray);
- if($use[$second] == $arr[$second])
- {
- //当前医生权重已使用为0 则分配给当前医生
- $data[$i] = $second;
- $use[$second] --;
- }else{
- if($use[$did] == $use[$second])
- {
- //存在多个未使用权重相同的情况
- $third = 0;
- $lastDoctor = $use[$second];
- foreach ($use as $k=>$v)
- {
- if($third == 0)
- {
- $lastDoctor = $k;
- $third = $v;
- }else{
- if($third == $v)
- {
- $lastDoctor = $k;
- }
- }
- }
- // 多个医生剩余权重使用数相同的情况 则分配给初始权重最低的那一个
- $data[$i] = $lastDoctor;
- $use[$lastDoctor]--;
- }else{
- $data[$i] = $did;
- $use[$did]--;
- }
- }
- }
- }
- }
- $using_num++;
- // 存储轮换使用的个数
- $this->setCache($institution.'_using_'.date('Ymd'),$using_num);
- // 存储医生队列的信息
- $this->setCache($institution.'_queue_'.date('Ymd'),$data,86400);
- //第一次进来
- return $data[$using_num];
- }else{
- $useing_num = $using+1;
- if ($useing_num == $allWeight)
- {
- // 满足一轮权重轮换
- $this->setCache($institution.'_using_'.date('Ymd'),0);
- }else{
- $this->setCache($institution.'_using_'.date('Ymd'),$useing_num);
- }
- return $queue[$useing_num];
- }
- }
- public function downLoad($url, $path,$create) {
- $pic_local = $path.'.dcm';
- if (!file_exists($create)) {
- mkdir($create, 0777,true);
- @chmod($create, 0777);
- }
- ob_start(); //打开输出
- readfile($url); //输出图片文件
- $img = ob_get_contents(); //得到浏览器输出
- ob_end_clean(); //清除输出并关闭
- file_put_contents($pic_local, $img);
- return $pic_local;
- }
- //获取毫秒级年月日时分秒
- public function ts_time($format = 'u', $utimestamp = null) {
- if (is_null($utimestamp)){
- $utimestamp = microtime(true);
- }
- $timestamp = floor($utimestamp);
- $milliseconds = round(($utimestamp - $timestamp) * 1000);
- return date('Y-m-d H:i:s.'.str_pad(preg_replace('`(?<!\\\\)u`', $milliseconds, $format),3,0), $timestamp);
- }
- public function makeFileUrl($file,$type,$fileType='attachment',$expires='+12 hours',$bucket='')
- {
- $data = $file;
- if($type == '2')
- {
- // 移动云普通文件
- if(!empty($file))
- {
- $upload = new uploadToCloud();
- $data = $upload->makeUrl($file,$fileType,$expires,$bucket);
- }
- }elseif ($type == '1')
- {
- // 普通ris文件
- if(!empty($file))
- {
- $data = $file;
- }
- }elseif ($type == '3')
- {
- // 远程诊断视频
- if(!empty($file))
- {
- $upload = new uploadToCloud();
- $data = $upload->makeCloudUrl($file,$fileType,2,$expires);
- }
- }elseif ($type == '4')
- {
- // dcm影像压缩包
- if(!empty($file))
- {
- $upload = new uploadToCloud();
- $data = $upload->makeCloudUrl($file,$fileType,3,$expires);
- }
- }
- return $data;
- }
- }
|