123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace app\common\model;
- use think\Db;
- class ConveyCron extends Base
- {
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- protected $table = 'convey_cron';
- protected $validateName = 'ConveyCron';
- public function updates(){
- $data = request()->post();
- $data['org_id'] =cur_org_id();
- $data['create_user_id'] = is_login();
- // halt($data);
- $result = validate($this->validateName)->check($data,[],'');
- if(true !== $result){
- $this->error = validate($this->validateName)->getError();
- return false;
- }
- $id = $data['id'];
- unset($data['id']);
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data);
- }
- if(!$ret){
- $this->error = '操作失败';
- return false;
- }
- return true;
- }
- public function plan($orgId = 0){
- $curday = date("Ymd");
- $week = date("w");
- $map[] = ['del','=',0];
- $map[] = ['week','=',$week];
- $map[] = ['update_day','<',$curday];
- if($orgId > 0){
- $map[] = ['org_id','=',$orgId];
- }
- $lists = Db::name('convey_cron')->where($map)->limit(100)->fetchSql(false)->select();
- // halt($lists);
- $lists = $lists?$lists:[];
- Db::startTrans();
- try{
- foreach ($lists as $k=>$v){
- $conveyCate = Db::name('convey_cate')->where('id',$v['type'])->find();
- $conveyTime = Db::name('time')->where('id',$conveyCate['time_id'])->find();
- $odata = [
- 'org_id' => $v['org_id'],
- 'sn' => get_unique_sn(get_config('sn_prefix')),
- 'dep_id' => $v['dep_id'],
- 'user_id' => $v['create_user_id'],
- 'order_mode' => empty($v['to_user_ids'])?1:4,
- 'content' => $v['content'],
- 'create_time' => getTime(),
- 'source_type' => 2,
- 'work_type_mode' => 3,
- 'create_yyyy' => date("Y"),
- 'create_yyyymm' => date("Ym"),
- 'create_yyyymmdd' => date("Ymd"),
- ];
- if($odata['order_mode'] == 4){
- $odata['send_time'] = getTime();
- }
- $orderId = Db::name('orders')->insertGetId($odata);
- if($orderId <= 0){
- exception("操作失败1");
- }
- $xqtimeint = strtotime(date("Y-m-d")." ".$v['xq_time']);
- $xqtime =date("Y-m-d H:i:s",$xqtimeint);
- $ywctimeint = $xqtimeint + $conveyTime['bz_time']*60;
- $ywctime =date("Y-m-d H:i:s",$ywctimeint);
- $ocdata = [
- 'order_id' => $orderId,
- 'type' => $v['type'],
- 'start' => $v['start'],
- 'end' => $v['end'],
- 'xq_time' => $xqtime,
- 'ywc_time' => $ywctime,
- 'device_id' => $v['device_id'],
- 'priority' => $v['priority'],
- ];
- $ocId = Db::name('order_convey')->insertGetId($ocdata);
- if($ocId <= 0){
- exception("操作失败2");
- }
- if($conveyCate['cate'] == 1){ // 病人运送
- $ocpdata = [
- 'order_id' => $orderId,
- 'bed_number' => $v['bed_number'],
- 'name' => $v['name']
- ];
- $ocpId = Db::name('order_convey_patient')->insertGetId($ocpdata);
- if($ocpId <= 0){
- exception("操作失败3");
- }
- }
- if($v['tjaddrs']){
- $addrs = explode(',',$v['tjaddrs']);
- foreach ($addrs as $kk=>$vv){
- $ocedata = [
- 'order_id' => $orderId,
- 'addr' => $vv,
- 'order_convey_id' => $ocId,
- 'create_time' => getTime()
- ];
- $oceId = Db::name('order_convey_end')->insertGetId($ocedata);
- if($oceId <= 0){
- exception("操作失败4");
- }
- }
- }
- if(!empty($v['to_user_ids'])){
- $uids = explode(',',$v['to_user_ids']);
- $sns = [];
- foreach ($uids as $kk=>$vv){
- $tododata = [
- 'order_id' => $orderId,
- 'to_user_id' => $vv,
- 'todo_content' => $v['todo_content'],
- 'org_id' => $v['org_id'],
- 'create_time' => getTime(),
- 'todo_mode' => 1,
- 'work_type_mode' => 3,
- 'create_yyyy' => date("Y"),
- 'create_yyyymm' => date("Ym"),
- 'create_yyyymmdd' => date("Ymd"),
- ];
- $tododata['sn'] = get_unique_sn(get_config('sn_prefix'));
- while (true){
- if(in_array($tododata['sn'],$sns)){
- $tododata['sn'] = get_unique_sn(get_config('sn_prefix'));
- }else{
- $sns[] = $tododata['sn'];
- break;
- }
- }
- $todoId = Db::name('todo')->insertGetId($tododata);
- if($todoId<=0){
- exception("操作失败5");
- }
- }
- }
- Db::name('convey_cron')->where('id',$v['id'])->update(['update_day'=>$curday,'update_time'=>getTime()]);
- }
- Db::commit();
- return true;
- }catch (\Exception $exception){
- trace($exception->getMessage());
- Db::rollback();
- return false;
- }
- }
- }
|