ConveyCron.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class ConveyCron extends Base
  5. {
  6. protected $createTime = 'create_time';
  7. protected $updateTime = 'update_time';
  8. protected $table = 'convey_cron';
  9. protected $validateName = 'ConveyCron';
  10. public function updates(){
  11. $data = request()->post();
  12. $data['org_id'] =cur_org_id();
  13. $data['create_user_id'] = is_login();
  14. // halt($data);
  15. $result = validate($this->validateName)->check($data,[],'');
  16. if(true !== $result){
  17. $this->error = validate($this->validateName)->getError();
  18. return false;
  19. }
  20. $id = $data['id'];
  21. unset($data['id']);
  22. if($id > 0){
  23. $data['update_time'] = date('Y-m-d H:i:s');
  24. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  25. }else{
  26. $data['create_time'] = date('Y-m-d H:i:s');
  27. $ret = $this->allowField(true)->save($data);
  28. }
  29. if(!$ret){
  30. $this->error = '操作失败';
  31. return false;
  32. }
  33. return true;
  34. }
  35. public function plan($orgId = 0){
  36. $curday = date("Ymd");
  37. $week = date("w");
  38. $map[] = ['del','=',0];
  39. $map[] = ['week','=',$week];
  40. $map[] = ['update_day','<',$curday];
  41. if($orgId > 0){
  42. $map[] = ['org_id','=',$orgId];
  43. }
  44. $lists = Db::name('convey_cron')->where($map)->limit(100)->fetchSql(false)->select();
  45. // halt($lists);
  46. $lists = $lists?$lists:[];
  47. Db::startTrans();
  48. try{
  49. foreach ($lists as $k=>$v){
  50. $conveyCate = Db::name('convey_cate')->where('id',$v['type'])->find();
  51. $conveyTime = Db::name('time')->where('id',$conveyCate['time_id'])->find();
  52. $odata = [
  53. 'org_id' => $v['org_id'],
  54. 'sn' => get_unique_sn(get_config('sn_prefix')),
  55. 'dep_id' => $v['dep_id'],
  56. 'user_id' => $v['create_user_id'],
  57. 'order_mode' => empty($v['to_user_ids'])?1:4,
  58. 'content' => $v['content'],
  59. 'create_time' => getTime(),
  60. 'source_type' => 2,
  61. 'work_type_mode' => 3,
  62. 'create_yyyy' => date("Y"),
  63. 'create_yyyymm' => date("Ym"),
  64. 'create_yyyymmdd' => date("Ymd"),
  65. ];
  66. if($odata['order_mode'] == 4){
  67. $odata['send_time'] = getTime();
  68. }
  69. $orderId = Db::name('orders')->insertGetId($odata);
  70. if($orderId <= 0){
  71. exception("操作失败1");
  72. }
  73. $xqtimeint = strtotime(date("Y-m-d")." ".$v['xq_time']);
  74. $xqtime =date("Y-m-d H:i:s",$xqtimeint);
  75. $ywctimeint = $xqtimeint + $conveyTime['bz_time']*60;
  76. $ywctime =date("Y-m-d H:i:s",$ywctimeint);
  77. $ocdata = [
  78. 'order_id' => $orderId,
  79. 'type' => $v['type'],
  80. 'start' => $v['start'],
  81. 'end' => $v['end'],
  82. 'xq_time' => $xqtime,
  83. 'ywc_time' => $ywctime,
  84. 'device_id' => $v['device_id'],
  85. 'priority' => $v['priority'],
  86. ];
  87. $ocId = Db::name('order_convey')->insertGetId($ocdata);
  88. if($ocId <= 0){
  89. exception("操作失败2");
  90. }
  91. if($conveyCate['cate'] == 1){ // 病人运送
  92. $ocpdata = [
  93. 'order_id' => $orderId,
  94. 'bed_number' => $v['bed_number'],
  95. 'name' => $v['name']
  96. ];
  97. $ocpId = Db::name('order_convey_patient')->insertGetId($ocpdata);
  98. if($ocpId <= 0){
  99. exception("操作失败3");
  100. }
  101. }
  102. if($v['tjaddrs']){
  103. $addrs = explode(',',$v['tjaddrs']);
  104. foreach ($addrs as $kk=>$vv){
  105. $ocedata = [
  106. 'order_id' => $orderId,
  107. 'addr' => $vv,
  108. 'order_convey_id' => $ocId,
  109. 'create_time' => getTime()
  110. ];
  111. $oceId = Db::name('order_convey_end')->insertGetId($ocedata);
  112. if($oceId <= 0){
  113. exception("操作失败4");
  114. }
  115. }
  116. }
  117. if(!empty($v['to_user_ids'])){
  118. $uids = explode(',',$v['to_user_ids']);
  119. $sns = [];
  120. foreach ($uids as $kk=>$vv){
  121. $tododata = [
  122. 'order_id' => $orderId,
  123. 'to_user_id' => $vv,
  124. 'todo_content' => $v['todo_content'],
  125. 'org_id' => $v['org_id'],
  126. 'create_time' => getTime(),
  127. 'todo_mode' => 1,
  128. 'work_type_mode' => 3,
  129. 'create_yyyy' => date("Y"),
  130. 'create_yyyymm' => date("Ym"),
  131. 'create_yyyymmdd' => date("Ymd"),
  132. ];
  133. $tododata['sn'] = get_unique_sn(get_config('sn_prefix'));
  134. while (true){
  135. if(in_array($tododata['sn'],$sns)){
  136. $tododata['sn'] = get_unique_sn(get_config('sn_prefix'));
  137. }else{
  138. $sns[] = $tododata['sn'];
  139. break;
  140. }
  141. }
  142. $todoId = Db::name('todo')->insertGetId($tododata);
  143. if($todoId<=0){
  144. exception("操作失败5");
  145. }
  146. }
  147. }
  148. Db::name('convey_cron')->where('id',$v['id'])->update(['update_day'=>$curday,'update_time'=>getTime()]);
  149. }
  150. Db::commit();
  151. return true;
  152. }catch (\Exception $exception){
  153. trace($exception->getMessage());
  154. Db::rollback();
  155. return false;
  156. }
  157. }
  158. }