Complain.php 33 KB

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