0
0

Hiddendanger.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. <?php
  2. namespace app\common\model;
  3. use app\common\util\AppMsg;
  4. use think\Db;
  5. use think\Exception;
  6. class Hiddendanger extends Base
  7. {
  8. public $table = 'hiddendanger';
  9. protected $validateName = 'Hiddendanger';
  10. public $from = [
  11. 0=>'隐患',
  12. 1=>'一键呼叫',
  13. 2=>'投诉',
  14. ];
  15. public function addSave($info){
  16. $data=[
  17. 'org_id'=>$info['orgId'],
  18. 'dep_id'=>$info['depId'],
  19. 'create_user_id'=>$info['createUserId'],
  20. 'content'=>$info['content'],
  21. 'images'=>isset($info['images'])?$info['images']:'',
  22. 'voices'=>isset($info['voices'])?$info['voices']:'',
  23. 'videos'=>isset($info['videos'])?$info['videos']:'',
  24. 'create_time'=>getTime(),
  25. 'create_yyyy'=>date('Y'),
  26. 'create_yyyymm'=>date('Ym'),
  27. 'create_yyyymmdd'=>date('Ymd'),
  28. ];
  29. if(!$data['content'] && !$data['voices'] && !$data['videos']){
  30. $this->error = '请填写信息';
  31. return false;
  32. }
  33. $ret = $this->insert($data);
  34. return $ret;
  35. }
  36. public function lists($page,$size,$orgId,$userId){
  37. $list = Db::name($this->table)
  38. ->field('id,content,create_time,status,order_id')
  39. ->where([
  40. 'del'=>0,
  41. 'org_id'=>$orgId,
  42. 'create_user_id'=>$userId,
  43. ])
  44. ->order('id','desc')
  45. ->page($page,$size)
  46. ->select();
  47. foreach ($list as $k=>$v){
  48. $list[$k]['status_text'] = $this->formatStatus($v['status'],$v['order_id'],$v['id']);
  49. }
  50. return $list;
  51. }
  52. public function formatStatus($status,$orderId,$id){
  53. $statusTxt = '';
  54. if($status == 0){
  55. $statusTxt = '待处理';
  56. }else if($status == 1){
  57. if($orderId > 0){
  58. $info = Db::name('hiddendanger')
  59. ->where('id',$id)
  60. ->find();
  61. if($info['to_from']==0){
  62. $order = Db::name('orders')->where('id',$orderId)->find();
  63. if(in_array($order['order_mode'],[3,5,6])){
  64. $statusTxt = '已处理';
  65. }else{
  66. $statusTxt = '处理中(已转单)';
  67. }
  68. }else if($info['to_from']==1){
  69. $statusTxt = '已处理(已转单)';
  70. }
  71. }else{
  72. $statusTxt = '已处理';
  73. }
  74. }
  75. return $statusTxt;
  76. }
  77. public function detail($id){
  78. $info = Db::name($this->table)
  79. ->field('content,create_time,images,voices,videos,create_user_id,org_id,dep_id,note,order_id,status,id,to_from')
  80. ->where('id',$id)
  81. ->find();
  82. if(!$info){
  83. return false;
  84. }
  85. $info['status_text'] = $this->formatStatus($info['status'],$info['order_id'],$info['id']);
  86. if($info['order_id'] > 0 && $info['to_from']==0){
  87. $info['work_type_mode'] = Db::name('orders')->where('id',$info['order_id'])->value('work_type_mode');
  88. }else{
  89. $info['work_type_mode'] = 98;
  90. }
  91. $info['dep'] = Db::name('dep')
  92. ->where('id',$info['dep_id'])
  93. ->value('title');
  94. $user=Db::name('user')
  95. ->field('real_name,mobile')
  96. ->where('id',$info['create_user_id'])
  97. ->find();
  98. if(!$user) {
  99. $info['realName'] = '';
  100. $info['mobile'] = '';
  101. }
  102. $info['realName']=$user['real_name'];
  103. $info['mobile']=$user['mobile'];
  104. return $info?$info:'';
  105. }
  106. //派单
  107. public function send($id, $userId, $data) {
  108. $hiddendanger = Db::name($this->table)->where('id', $id)->where('del',0)->find();
  109. $odata['dep_id'] = $hiddendanger['dep_id'];
  110. $odata['content'] = $hiddendanger['content'];
  111. $odata['images'] = $hiddendanger['images'];
  112. $odata['videos'] = $hiddendanger['videos'];
  113. $odata['voices'] = $hiddendanger['videos'];
  114. $odata['source_type'] = 2;
  115. $odata['from'] = 2;
  116. $odata['user_id'] = $hiddendanger['create_user_id'];
  117. $odata['order_mode'] = 4;
  118. $odata['work_type_mode'] = $data['work_type_mode'];
  119. $odata['create_time'] = date('Y-m-d H:i:s');
  120. $odata['send_time'] = date('Y-m-d H:i:s');
  121. $odata['create_yyyy'] = date('Y');
  122. $odata['create_yyyymm'] = date('Ym');
  123. $odata['create_yyyymmdd'] = date('Ymd');
  124. $odata['is_deal'] = 0;
  125. $odata['org_id'] = $data['org_id'];
  126. $odata['sn'] = get_unique_sn(get_config('sn_prefix'));
  127. if($hiddendanger['status'] == 1){
  128. $this->error = '已处理不能转单';
  129. return false;
  130. }
  131. $data['userType'] = isset($data['userType']) ? $data['userType'] : 0;
  132. if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){
  133. $this->error = '请选择转单类型';
  134. return false;
  135. }
  136. $config = Db::name('config')
  137. ->where('name','web_order_transfer_type')
  138. ->value('value');
  139. if(empty($config)){
  140. $workType = [];
  141. }else{
  142. $workType = explode('|',$config);
  143. }
  144. if(!in_array($data['work_type_mode'],$workType)){
  145. $this->error = '当前类型不能进行转单';
  146. return false;
  147. }
  148. if($data['userType']==0){
  149. $data['to_user_id'] = !isset($data['to_user_id'])?$data['to_user_id'.$data['work_type_mode']]:$data['to_user_id'];
  150. }else{
  151. $data['to_roles_id'] =!isset($data['to_roles_id'])? $data['to_roles_id'.$data['work_type_mode']]:$data['to_roles_id'];
  152. }
  153. if ($data['work_type_mode'] == 3) {
  154. $result = validate('OrdersConvey')->check($data, []);
  155. if (true !== $result) {
  156. $this->error = validate('OrdersConvey')->getError();
  157. return false;
  158. }
  159. }
  160. $pusharr = []; // 需要推送给工人
  161. $pushdis = []; // 需要推送给二级调度
  162. if ($data['userType'] == 0) {
  163. if (!isset($data['to_user_id']) || empty($data['to_user_id'])) {
  164. $this->error = '请选择执行人';
  165. return false;
  166. }
  167. if ($data['work_type_mode'] == 1) {
  168. if (!isset($data['type_id']) || empty($data['type_id'])) {
  169. $this->error = '请选择报修事项';
  170. return false;
  171. }
  172. }
  173. }
  174. else {
  175. if(two_dispatch_off($data['org_id'])!=1){
  176. $this->error = '二级调度开关未开启';
  177. return false;
  178. }
  179. if (!isset($data['to_roles_id']) || empty($data['to_roles_id'])) {
  180. $this->error = '请选择二级调度';
  181. return false;
  182. }
  183. }
  184. $cl_remark = $data['cl_remark'];
  185. unset($data['cl_remark']);
  186. $this->startTrans();
  187. try {
  188. $orderId = Db::name('orders')->insertGetId($odata);
  189. if(!$orderId){
  190. \exception('派单失败');
  191. }
  192. $order = Db::name('orders')->where('id',$orderId)->find();
  193. $save1 = Db::name($this->table)->where('id',$id)->update(['note'=>$cl_remark,'order_id'=>$orderId,'status'=>1,'update_time'=>getTime()]);
  194. if(!$save1){
  195. \exception('投诉更新失败');
  196. }
  197. if(!$order){
  198. $this->error = '派单失败';
  199. return false;
  200. }
  201. if ($data['userType'] == 0) {
  202. $send_user_num = model('Orders')->sendUserNum($data['work_type_mode'],$data['org_id']);
  203. $users = explode(',', $data['to_user_id']);
  204. if($send_user_num==1 && count($users) >1){
  205. $this->error = '执行人只能选择单人';
  206. return false;
  207. }
  208. $to_user_id = array_unique($users);
  209. $todoData = [
  210. 'order_id' => $orderId,
  211. 'todo_content' => $data['todo_content'],
  212. 'create_user_id' => $order['user_id'],
  213. 'org_id' => $data['org_id'],
  214. 'create_time' => getTime(),
  215. 'todo_mode' => 1,
  216. 'work_type_mode' => $data['work_type_mode'],
  217. 'create_yyyy' => date('Y'),
  218. 'create_yyyymm' => date('Ym'),
  219. 'create_yyyymmdd' => date('Ymd'),
  220. ];
  221. $taskData = [
  222. 'org_id' => $data['org_id'],
  223. 'type' => 1,
  224. 'start_time' => getTime(),
  225. 'create_time' => getTime(),
  226. ];
  227. if($order['work_type_mode']==1){
  228. $orderRepair = Db::name('order_repair')
  229. ->where('order_id',$orderId)
  230. ->find();
  231. $taskData['priority'] = !empty($orderRepair) && $orderRepair['repair_priority']==3?1:0;
  232. }else if ($order['work_type_mode']==3){
  233. $order_convey = Db::name('order_convey')
  234. ->where('order_id',$orderId)
  235. ->find();
  236. $taskData['priority'] = !empty($order_convey) && $order_convey['priority']==3?2:0;
  237. }
  238. $sns = [];
  239. foreach ($to_user_id as $k => $v) {
  240. if(empty($v)){
  241. continue;
  242. }
  243. $todoData['to_user_id'] = $v;
  244. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  245. while (true){
  246. if(in_array($todoData['sn'],$sns)){
  247. $todoData['sn'] = get_unique_sn(get_config('sn_prefix'));
  248. }else{
  249. $sns[] = $todoData['sn'];
  250. break;
  251. }
  252. }
  253. $res = Db::name('todo')
  254. ->insertGetId($todoData);
  255. if (!$res) {
  256. \exception('执行人:' . $v . '派单失败');
  257. }
  258. $pusharr[] = [
  259. 'user_id' => $v,
  260. 'todo_id' => $res
  261. ];
  262. $taskData['user_id'] = $v;
  263. $taskData['bus_id'] = $res;
  264. $res = Db::name('task')
  265. ->insert($taskData);
  266. if (!$res) {
  267. \exception('执行人:' . $v . '任务保存失败');
  268. }
  269. }
  270. if($hiddendanger['order_id'] > 0){
  271. if($order['from']==3 && $order['is_deal']==0){
  272. $rr = ['order_mode' => 4, 'work_type_mode'=>$data['work_type_mode'],'send_time' => getTime()];
  273. }else{
  274. $rr = ['order_mode' => 4, 'send_time' => getTime()];
  275. }
  276. $res = Db::name('orders')->where('id', $orderId)
  277. ->update($rr);
  278. if (!$res) {
  279. \exception('订单更新失败');
  280. }
  281. }
  282. }
  283. else {
  284. $pRolesId = explode(',',$data['to_roles_id']);
  285. $pp = [];
  286. foreach ($pRolesId as $k=>$v){
  287. $logData = [
  288. 'user_id' => $userId,
  289. 'to_user_id' =>0,
  290. 'create_time' => getTime(),
  291. 'content' => isset($data['todo_content']) ? $data['todo_content'] : '',
  292. 'order_id' => $orderId,
  293. 'roles_id' => $v
  294. ];
  295. $pp[] = $logData;
  296. }
  297. $pushdis = Db::name('user_roles')
  298. ->where('roles_id','in',$pRolesId)
  299. ->column('user_id');
  300. $res = Db::name('dispatch_log')
  301. ->insertAll($pp);
  302. if (!$res) {
  303. \exception('派发失败');
  304. }
  305. if($order['from']==3 && $order['is_deal']==0){
  306. $rrs = ['is_deal' => 1, 'work_type_mode'=>$data['work_type_mode']];
  307. }else{
  308. $rrs = ['is_deal' => 1];
  309. }
  310. $res = Db::name('orders')->where('id', $orderId)
  311. ->update($rrs);
  312. if (!$res) {
  313. \exception('订单更新失败');
  314. }
  315. }
  316. if ($order['work_type_mode'] == 1) {//报修订单
  317. $rData = [];
  318. if (isset($data['type_id']) && !empty($data['type_id'])) {
  319. $rData['type_id'] = $data['type_id'];
  320. }
  321. if (isset($data['address_id']) && !empty($data['address_id'])) {
  322. $rData['address_id'] = $data['address_id'];
  323. }
  324. if (!empty($rData)) {
  325. $old = Db::name('order_repair')
  326. ->where('order_id', $orderId)
  327. ->find();
  328. if ($old) {
  329. $rData['update_time'] = getTime();
  330. $res = Db::name('order_repair')
  331. ->where('order_id', $orderId)
  332. ->update($rData);
  333. }
  334. else {
  335. $rData['order_id'] = $orderId;
  336. $res = Db::name('order_repair')
  337. ->insert($rData);
  338. }
  339. if (!$res) {
  340. \exception('保存维修扩展失败');
  341. }
  342. }
  343. }else{
  344. $old = Db::name('order_repair')
  345. ->where('order_id', $orderId)
  346. ->find();
  347. if ($old) {
  348. $res = Db::name('order_repair')
  349. ->where('order_id', $orderId)
  350. ->delete();
  351. if (!$res) {
  352. \exception('删除维修扩展失败');
  353. }
  354. }
  355. }
  356. if($order['from'] == 2 && $order['is_deal']==0){//一键呼叫 转单运送
  357. if ($data['work_type_mode'] == 3) {
  358. if (!$data['start'] || !$data['end']) {
  359. $this->error = '取件与送达地点不能为空';
  360. return false;
  361. }
  362. if ($data['start'] == $data['end']) {
  363. $this->error = '取件与送达地点不能是同一地点';
  364. return false;
  365. }
  366. if (!$data['xq_time'] || !$data['ywc_time']) {
  367. $this->error = '应完成时间应和需求时间不能为空';
  368. return false;
  369. }
  370. if ($data['xq_time'] >= $data['ywc_time']) {
  371. $this->error = '应完成时间应大于需求时间';
  372. return false;
  373. }
  374. if (strtotime($data['xq_time']) < time() - 10 * 60) {
  375. $this->error = '需求时间已过时';
  376. return false;
  377. }
  378. if (!$data['type']) {
  379. $this->error = '运送类型不能为空';
  380. return false;
  381. }
  382. if(!$data['name']){
  383. $this->error = '联系人不能为空';
  384. return false;
  385. }
  386. if(!$data['phone']){
  387. $this->error = '联系人电话不能为空';
  388. return false;
  389. }
  390. $ysData = array(
  391. 'order_id' => $orderId,
  392. 'type' => $data['type'],
  393. 'start' => $data['start'],
  394. 'end' => $data['end'],
  395. 'xq_time' => $data['xq_time'],
  396. 'ywc_time' => $data['ywc_time'],
  397. 'name' => $data['name'],
  398. 'phone' => $data['phone'],
  399. 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0,
  400. 'priority' => empty($data['priority']) ? 0 : $data['priority']
  401. );
  402. $check = Db::name('order_convey')
  403. ->where('order_id',$orderId)
  404. ->find();
  405. if($check){
  406. Db::name('order_convey')
  407. ->where('order_id',$orderId)
  408. ->update($ysData);
  409. }else{
  410. $oCid = Db::name('order_convey')
  411. ->insertGetId($ysData);
  412. if (!$oCid) {
  413. exception('运送信息保存失败');
  414. }
  415. }
  416. $conveyCate = Db::name('convey_cate')
  417. ->where('id', $data['type'])
  418. ->find();
  419. if ($conveyCate['cate'] == 1) {
  420. $patient = [
  421. 'order_id' => $orderId,
  422. 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '',
  423. 'name' => isset($data['p_name']) ? $data['p_name'] : '',
  424. 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '',
  425. 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0,
  426. 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0,
  427. ];
  428. $cp = Db::name('order_convey_patient')
  429. ->where('order_id',$orderId)
  430. ->find();
  431. if($cp){
  432. Db::name('order_convey_patient')
  433. ->where('order_id',$orderId)
  434. ->update($patient);
  435. }else{
  436. $ocPid = Db::name('order_convey_patient')
  437. ->insertGetId($patient);
  438. if (!$ocPid) {
  439. exception('保存运送病人扩展失败');
  440. }
  441. }
  442. }
  443. }else{
  444. Db::name('order_convey')
  445. ->where('order_id',$orderId)
  446. ->delete();
  447. Db::name('order_convey_patient')
  448. ->where('order_id',$orderId)
  449. ->delete();
  450. }
  451. }
  452. $this->commit();
  453. if($pusharr){
  454. foreach ($pusharr as $k=>$v){
  455. // 极光推送
  456. send_jpush([$v['user_id']],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$v['todo_id']]);
  457. }
  458. }
  459. if($pushdis){
  460. foreach ($pushdis as $k=>$v){
  461. // 极光推送
  462. send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$order['id']]);
  463. }
  464. }
  465. // 极光推送
  466. send_jpush([$order['user_id']],AppMsg::PUSH_USER_ORDER_SEND,'',['id'=>$order['id']]);
  467. return true;
  468. } catch (Exception $e) {
  469. $this->rollback();
  470. $this->error = $e->getMessage();
  471. return false;
  472. }
  473. }
  474. //转单
  475. public function zd_send($id, $data,$userId) {
  476. $hiddendanger = Db::name($this->table)->where('id', $id)->where('del',0)->find();
  477. if($hiddendanger['order_id'] > 0){
  478. $this->error = '该隐患上报已转单,';
  479. return false;
  480. }
  481. if($data['work_type_mode']==98){
  482. $odata['org_id'] = $data['org_id'];
  483. $odata['dep_id'] = $hiddendanger['dep_id'];
  484. $odata['content'] = $hiddendanger['content'];
  485. $odata['images'] = $hiddendanger['images'];
  486. $odata['videos'] = $hiddendanger['videos'];
  487. $odata['voices'] = $hiddendanger['videos'];
  488. $odata['from'] = 2;
  489. $odata['create_user_id'] = $hiddendanger['create_user_id'];
  490. $odata['create_time'] = date('Y-m-d H:i:s');
  491. $odata['create_yyyy'] = date('Y');
  492. $odata['create_yyyymm'] = date('Ym');
  493. $odata['create_yyyymmdd'] = date('Ymd');
  494. $this->startTrans();
  495. try {
  496. $orderId = Db::name('complain')->insertGetId($odata);
  497. if (!$orderId) {
  498. \exception('投诉订单创建失败');
  499. }
  500. $res = $this->where('id',$hiddendanger['id'])
  501. ->update(['status'=>1,'to_from'=>1,'bus_type'=>2,'update_time'=>getTime(),
  502. 'hand_user_id'=>$userId,'order_id'=>$orderId]);
  503. if (!$res) {
  504. \exception('隐患订单更新失败');
  505. }
  506. $this->commit();
  507. return true;
  508. }catch (Exception $e) {
  509. $this->rollback();
  510. $this->error = $e->getMessage();
  511. return false;
  512. }
  513. }else{
  514. $odata['dep_id'] = $hiddendanger['dep_id'];
  515. $odata['content'] = $hiddendanger['content'];
  516. $odata['images'] = $hiddendanger['images'];
  517. $odata['videos'] = $hiddendanger['videos'];
  518. $odata['voices'] = $hiddendanger['videos'];
  519. $odata['from'] = 2;
  520. $odata['user_id'] = $hiddendanger['create_user_id'];
  521. $odata['order_mode'] = 1;
  522. $odata['source_type'] = $data['work_type_mode']==0?4:2;
  523. $odata['create_time'] = date('Y-m-d H:i:s');
  524. $odata['send_time'] = date('Y-m-d H:i:s');
  525. $odata['create_yyyy'] = date('Y');
  526. $odata['create_yyyymm'] = date('Ym');
  527. $odata['create_yyyymmdd'] = date('Ymd');
  528. $odata['is_deal'] = 0;
  529. $odata['org_id'] = $data['org_id'];
  530. $odata['sn'] = get_unique_sn(get_config('sn_prefix'));
  531. if($hiddendanger['status'] == 1){
  532. $this->error = '已处理不能转单';
  533. return false;
  534. }
  535. if(!isset($data['work_type_mode']) || $data['work_type_mode'] <0){
  536. $this->error = '请选择转单类型';
  537. return false;
  538. }
  539. $config = Db::name('config')
  540. ->where('name','web_order_transfer_type')
  541. ->value('value');
  542. if(empty($config)){
  543. $workType = [];
  544. }else{
  545. $workType = explode('|',$config);
  546. }
  547. if(!in_array($data['work_type_mode'],$workType)){
  548. $this->error = '当前类型不能进行转单';
  549. return false;
  550. }
  551. if ($data['work_type_mode'] == 3) {
  552. $result = validate('OrdersConvey')->check($data, []);
  553. if (true !== $result) {
  554. $this->error = validate('OrdersConvey')->getError();
  555. return false;
  556. }
  557. }
  558. if ($data['work_type_mode'] == 1) {
  559. if (!isset($data['type_id']) || empty($data['type_id'])) {
  560. $this->error = '请选择报修事项';
  561. return false;
  562. }
  563. }
  564. $cl_remark = $data['cl_remark'];
  565. unset($data['cl_remark']);
  566. $this->startTrans();
  567. try {
  568. $orderId = Db::name('orders')->insertGetId($odata);
  569. if (!$orderId) {
  570. \exception('订单更新失败');
  571. }
  572. $order = Db::name('orders')->where('id',$orderId)->find();
  573. $save1 = Db::name($this->table)->where('id',$id)->update(['order_id'=>$orderId,'bus_type'=>1,'note'=>$cl_remark,'status'=>1,'update_time'=>getTime()]);
  574. if(!$save1){
  575. \exception('隐患预警更新失败');
  576. }
  577. if($order['work_type_mode'] == 3){
  578. // $score = empty($data['score'])?0:floatval($data['score']);
  579. // Db::name('order_convey')->where('order_id',$orderId)->update(['score'=>$score]);
  580. }
  581. if ($order['work_type_mode'] == 1) {//报修订单
  582. $rData = [];
  583. if (isset($data['type_id']) && !empty($data['type_id'])) {
  584. $rData['type_id'] = $data['type_id'];
  585. }
  586. if (isset($data['address_id']) && !empty($data['address_id'])) {
  587. $rData['address_id'] = $data['address_id'];
  588. }
  589. if (!empty($rData)) {
  590. $old = Db::name('order_repair')
  591. ->where('order_id', $orderId)
  592. ->find();
  593. if ($old) {
  594. $rData['update_time'] = getTime();
  595. $res = Db::name('order_repair')
  596. ->where('order_id', $orderId)
  597. ->update($rData);
  598. }
  599. else {
  600. $rData['order_id'] = $orderId;
  601. $res = Db::name('order_repair')
  602. ->insert($rData);
  603. }
  604. if (!$res) {
  605. \exception('保存维修扩展失败');
  606. }
  607. }
  608. }else{
  609. $old = Db::name('order_repair')
  610. ->where('order_id', $orderId)
  611. ->find();
  612. if ($old) {
  613. $res = Db::name('order_repair')
  614. ->where('order_id', $orderId)
  615. ->delete();
  616. if (!$res) {
  617. \exception('删除维修扩展失败');
  618. }
  619. }
  620. }
  621. if ($data['work_type_mode'] == 3) {
  622. if (!$data['start'] || !$data['end']) {
  623. $this->error = '取件与送达地点不能为空';
  624. return false;
  625. }
  626. if ($data['start'] == $data['end']) {
  627. $this->error = '取件与送达地点不能是同一地点';
  628. return false;
  629. }
  630. if (!$data['xq_time'] || !$data['ywc_time']) {
  631. $this->error = '应完成时间应和需求时间不能为空';
  632. return false;
  633. }
  634. if ($data['xq_time'] >= $data['ywc_time']) {
  635. $this->error = '应完成时间应大于需求时间';
  636. return false;
  637. }
  638. if (strtotime($data['xq_time']) < time() - 10 * 60) {
  639. $this->error = '需求时间已过时';
  640. return false;
  641. }
  642. if (!$data['type']) {
  643. $this->error = '运送类型不能为空';
  644. return false;
  645. }
  646. // if(!$data['name']){
  647. // $this->error = '联系人不能为空';
  648. // return false;
  649. // }
  650. // if(!$data['phone']){
  651. // $this->error = '联系人电话不能为空';
  652. // return false;
  653. // }
  654. $score = empty($data['score'])?0:floatval($data['score']);
  655. $ysData = array(
  656. 'order_id' => $orderId,
  657. 'type' => $data['type'],
  658. 'start' => $data['start'],
  659. 'end' => $data['end'],
  660. 'xq_time' => $data['xq_time'],
  661. 'ywc_time' => $data['ywc_time'],
  662. 'name' => $data['name'],
  663. 'phone' => $data['phone'],
  664. 'score' => $score,
  665. 'device_id' => isset($data['device_id']) ? (int)$data['device_id'] : 0,
  666. 'priority' => empty($data['priority']) ? 0 : $data['priority']
  667. );
  668. $check = Db::name('order_convey')
  669. ->where('order_id',$orderId)
  670. ->find();
  671. if($check){
  672. Db::name('order_convey')
  673. ->where('order_id',$orderId)
  674. ->update($ysData);
  675. }else{
  676. $oCid = Db::name('order_convey')
  677. ->insertGetId($ysData);
  678. if (!$oCid) {
  679. exception('运送信息保存失败');
  680. }
  681. }
  682. $conveyCate = Db::name('convey_cate')
  683. ->where('id', $data['type'])
  684. ->find();
  685. if ($conveyCate['cate'] == 1) {
  686. $patient = [
  687. 'order_id' => $orderId,
  688. 'bed_number' => isset($data['bed_number']) ? $data['bed_number'] : '',
  689. 'name' => isset($data['p_name']) ? $data['p_name'] : '',
  690. 'ba_number' => isset($data['ba_number']) ? $data['ba_number'] : '',
  691. 'gender' => isset($data['gender']) && !empty($data['gender']) ? $data['gender'] : 0,
  692. 'back' => isset($data['back']) && !empty($data['back']) ? $data['back'] : 0,
  693. ];
  694. $cp = Db::name('order_convey_patient')
  695. ->where('order_id',$orderId)
  696. ->find();
  697. if($cp){
  698. Db::name('order_convey_patient')
  699. ->where('order_id',$orderId)
  700. ->update($patient);
  701. }else{
  702. $ocPid = Db::name('order_convey_patient')
  703. ->insertGetId($patient);
  704. if (!$ocPid) {
  705. exception('保存运送病人扩展失败');
  706. }
  707. }
  708. }
  709. }else{
  710. Db::name('order_convey')
  711. ->where('order_id',$orderId)
  712. ->delete();
  713. Db::name('order_convey_patient')
  714. ->where('order_id',$orderId)
  715. ->delete();
  716. }
  717. $this->commit();
  718. return true;
  719. } catch (Exception $e) {
  720. $this->rollback();
  721. $this->error = $e->getMessage();
  722. return false;
  723. }
  724. }
  725. }
  726. }