Maintain.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <?php
  2. namespace app\company\controller;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. use app\common\model\Maintain as Maintainmodel;
  6. /**
  7. * 维保流程
  8. */
  9. class Maintain extends Apic
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['index','info'];
  13. protected $table = 'maintain';
  14. //待审核 0
  15. //报价评估 20,22
  16. //报价审核 30
  17. //待处理 40,50,60,70,80,90,92
  18. //已完成 100
  19. //首页统计
  20. public function index(){
  21. if($this->auth->company_id == 1){
  22. return $this->liuzhuan_index();
  23. }
  24. $rs = [
  25. 'status_0' => 0,
  26. 'status_20' => 0,
  27. 'status_30' => 0,
  28. 'status_40' => 0,
  29. 'status_100' => 0,
  30. 'score' => 5,
  31. ];
  32. $list = Db::name($this->table)->field('status,count(id) as number')
  33. // ->where('status','NEQ',2)
  34. ->where('company_id',$this->auth->company_id)->group('status')->select();
  35. if(!empty($list)){
  36. foreach($list as $key => $val){
  37. if($val['status'] == 0){
  38. $rs['status_0'] = $val['number'];
  39. }
  40. if(in_array($val['status'],[20,22])){
  41. $rs['status_20'] += $val['number'];
  42. }
  43. if($val['status'] == 30){
  44. $rs['status_30'] = $val['number'];
  45. }
  46. if(in_array($val['status'],[40,50,60,70,80,90,92])){
  47. $rs['status_40'] += $val['number'];
  48. }
  49. /*if($val['status'] == 100){
  50. $rs['status_100'] = $val['number'];
  51. }*/
  52. if(in_array($val['status'],[2,100])){
  53. $rs['status_100'] += $val['number'];
  54. }
  55. }
  56. }
  57. //本周服务评分
  58. $score = Db::name($this->table)
  59. ->where('status',100)
  60. ->where('company_id',$this->auth->company_id)
  61. ->whereTime('eva_time','week')
  62. /*->select(false);echo $score;exit;*/
  63. ->avg('eva_score');
  64. $rs['score'] = $score;
  65. $this->success(1,$rs);
  66. }
  67. //首页统计
  68. public function liuzhuan_index(){
  69. $rs = [
  70. 'status_0' => 0,
  71. 'status_20' => 0,
  72. 'status_30' => 0,
  73. 'status_40' => 0,
  74. 'status_100' => 0,
  75. 'score' => 5,
  76. ];
  77. $list = Db::name($this->table)->field('status,count(id) as number')
  78. ->where('liuzhuan_status',1)->group('status')->select();
  79. if(!empty($list)){
  80. foreach($list as $key => $val){
  81. if($val['status'] == 0){
  82. $rs['status_0'] = $val['number'];
  83. }
  84. if(in_array($val['status'],[20,22])){
  85. $rs['status_20'] += $val['number'];
  86. }
  87. if($val['status'] == 30){
  88. $rs['status_30'] = $val['number'];
  89. }
  90. if(in_array($val['status'],[40,50,60,70,80,90,92])){
  91. $rs['status_40'] += $val['number'];
  92. }
  93. /*if($val['status'] == 100){
  94. $rs['status_100'] = $val['number'];
  95. }*/
  96. if(in_array($val['status'],[2,100])){
  97. $rs['status_100'] += $val['number'];
  98. }
  99. }
  100. }
  101. //本周服务评分
  102. $score = Db::name($this->table)
  103. ->where('status',100)
  104. ->where('liuzhuan_status',1)
  105. ->whereTime('eva_time','week')
  106. /*->select(false);echo $score;exit;*/
  107. ->avg('eva_score');
  108. $rs['score'] = $score;
  109. $this->success(1,$rs);
  110. }
  111. //列表
  112. public function lists(){
  113. if($this->auth->company_id == 1){
  114. return $this->liuzhuan_lists();
  115. }
  116. $orderno = input('orderno','');
  117. $projectname = input('projectname','');
  118. $header = input('header','');
  119. $status = input('status','ALL'); //默认ALL
  120. $map = [
  121. 'mt.company_id' => $this->auth->company_id,
  122. ];
  123. if(!empty($orderno)){
  124. $map['mt.orderno'] = $orderno;
  125. }
  126. if(!empty($projectname)){
  127. $map['uc.projectname'] = ['LIKE','%'.$projectname.'%'];
  128. }
  129. if(!empty($header)){
  130. $map['uc.header'] = ['LIKE','%'.$header.'%'];
  131. }
  132. if($status != 'ALL'){
  133. $map['mt.status'] = $status;
  134. if($status == 20){
  135. $map['mt.status'] = ['IN',[20,22]];
  136. }
  137. if($status == 40){
  138. $map['mt.status'] = ['IN',[40,50,60,70,80,90,92]];
  139. }
  140. if($status == 100){
  141. $map['mt.status'] = ['IN',[2,100]];
  142. }
  143. }
  144. $field = ['mt.id','mt.orderno','uc.projectname','mt.info','uc.header','mt.status','mt.price','mt.liuzhuan_status','company.companyname'];
  145. $list = Db::name($this->table)->alias('mt')
  146. ->join('user_company uc','mt.uc_id = uc.id','LEFT')
  147. ->join('company','mt.company_id = company.id','LEFT')
  148. ->field($field)
  149. ->where($map)->order('mt.id desc')
  150. ->paginate();
  151. $total = $list->total();
  152. $list = $list->items();
  153. if(!empty($list)){
  154. $maintain_model = new Maintainmodel();
  155. foreach($list as $key => $val){
  156. $list[$key]['status_text'] = $maintain_model->status_data($val['status']);
  157. //pc用户,已流转订单,直接显示流转中
  158. if($this->auth->company_id != 1 && in_array($val['status'],[0,20,22,30,40]) && $val['liuzhuan_status'] == 1){
  159. $list[$key]['status_text'] = '流转中';
  160. }
  161. $list[$key]['status_colorType'] = $maintain_model->status_colorType($val['status']);
  162. //假状态
  163. $list[$key]['fake_status'] = $this->fake_status($val['status']);
  164. $list[$key]['fake_status_text'] = $this->fake_status_data($val['status']);
  165. }
  166. }
  167. $rs = [
  168. 'list' => $list,
  169. 'total'=> $total,
  170. ];
  171. $this->success(1,$rs);
  172. }
  173. //流转列表
  174. public function liuzhuan_lists(){
  175. $orderno = input('orderno','');
  176. $projectname = input('projectname','');
  177. $header = input('header','');
  178. $status = input('status','ALL'); //默认ALL
  179. $map = [
  180. 'mt.liuzhuan_status' => 1,
  181. ];
  182. if(!empty($orderno)){
  183. $map['mt.orderno'] = $orderno;
  184. }
  185. if(!empty($projectname)){
  186. $map['uc.projectname'] = ['LIKE','%'.$projectname.'%'];
  187. }
  188. if(!empty($header)){
  189. $map['uc.header'] = ['LIKE','%'.$header.'%'];
  190. }
  191. if($status != 'ALL'){
  192. $map['mt.status'] = $status;
  193. if($status == 20){
  194. $map['mt.status'] = ['IN',[20,22]];
  195. }
  196. if($status == 40){
  197. $map['mt.status'] = ['IN',[40,50,60,70,80,90,92]];
  198. }
  199. if($status == 100){
  200. $map['mt.status'] = ['IN',[2,100]];
  201. }
  202. }
  203. $field = ['mt.id','mt.orderno','uc.projectname','mt.info','uc.header','mt.status','mt.price','mt.liuzhuan_status','company.companyname'];
  204. $list = Db::name($this->table)->alias('mt')
  205. ->join('user_company uc','mt.uc_id = uc.id','LEFT')
  206. ->join('company','mt.company_id = company.id','LEFT')
  207. ->field($field)
  208. ->where($map)->order('mt.id desc')
  209. ->paginate();
  210. $total = $list->total();
  211. $list = $list->items();
  212. if(!empty($list)){
  213. $maintain_model = new Maintainmodel();
  214. foreach($list as $key => $val){
  215. $list[$key]['status_text'] = $maintain_model->status_data($val['status']);
  216. $list[$key]['status_colorType'] = $maintain_model->status_colorType($val['status']);
  217. //假状态
  218. $list[$key]['fake_status'] = $this->fake_status($val['status']);
  219. $list[$key]['fake_status_text'] = $this->fake_status_data($val['status']);
  220. }
  221. }
  222. $rs = [
  223. 'list' => $list,
  224. 'total'=> $total,
  225. ];
  226. $this->success(1,$rs);
  227. }
  228. //整合过的状态
  229. private function fake_status_data($status){
  230. $data = [
  231. 0 => '待报价',
  232. 2 => '已取消',
  233. 20 => '评估报价',
  234. 22 => '评估报价',
  235. 30 => '用户待确认',
  236. 40 => '待处理',
  237. 50 => '待处理',
  238. 60 => '待处理',
  239. 70 => '待处理',
  240. 80 => '待处理',
  241. 90 => '待处理',
  242. 92 => '待处理',
  243. 100 => '已完成',
  244. ];
  245. return isset($data[$status]) ? $data[$status] : $status;
  246. }
  247. //整合过的状态
  248. private function fake_status($status){
  249. $data = [
  250. 0 => 0,
  251. 2 => 2,
  252. 20 => 20,
  253. 22 => 20,
  254. 30 => 30,
  255. 40 => 40,
  256. 50 => 40,
  257. 60 => 40,
  258. 70 => 40,
  259. 80 => 40,
  260. 90 => 40,
  261. 92 => 40,
  262. 100 => 100,
  263. ];
  264. return isset($data[$status]) ? $data[$status] : $status;
  265. }
  266. //详情
  267. public function info(){
  268. $id = input('id',0);
  269. $info = Db::name($this->table)->alias('mt')
  270. ->join('user_company uc','mt.uc_id = uc.id','LEFT')
  271. ->join('user','mt.user_id = user.id','LEFT')
  272. ->join('worker','mt.worker_id = worker.id','LEFT')
  273. ->field('mt.*,uc.projectname,uc.header,uc.header_mobile,uc.projectaddress,user.nickname as user_nickname,user.mobile as user_mobile,worker.truename as worker_truename,worker.mobile as worker_mobile')
  274. ->where('mt.id',$id)
  275. ->find();
  276. $maintain_model = new Maintainmodel();
  277. $info['status_text'] = $maintain_model->status_data($info['status']);
  278. //pc用户,已流转订单,直接显示流转中
  279. if($this->auth->company_id != 1 && in_array($info['status'],[0,20,22,30,40]) && $info['liuzhuan_status'] == 1){
  280. $info['status_text'] = '流转中';
  281. }
  282. $info['status_colorType'] = $maintain_model->status_colorType($info['status']);
  283. //假状态
  284. $info['fake_status'] = $this->fake_status($info['status']);
  285. $info['fake_status_text'] = $this->fake_status_data($info['status']);
  286. $info['filedata'] = json_decode($info['filedata'],true);
  287. //追加报价历史
  288. $baojia = Db::name('maintain_baojia')->alias('bj')
  289. ->field('bj.*,admin.nickname as baojia_nickname,audit.nickname as baojia_audit_nickname')
  290. ->join('pc_admin admin','bj.baojia_staffid = admin.id','LEFT')
  291. ->join('pc_admin audit','bj.baojia_audit_staffid = audit.id','LEFT')
  292. ->where('order_id',$id)->order('id desc')->select();
  293. if(!empty($baojia)){
  294. foreach($baojia as $key => $val){
  295. $baojia[$key]['status_text'] = $maintain_model->baojia_status_data($val['status']);
  296. }
  297. }
  298. $info['baojia_list'] = $baojia;
  299. //追加材料列表
  300. $info['cailiao_list'] = Db::name('maintain_cailiao')->field('id,name,number,danwei,images')->where('order_id',$id)->order('id desc')->select();
  301. //追加多次维修+对应进度历史
  302. $last_jindulist = [];
  303. $new_jindulist = [];
  304. $jindu_list = Db::name('maintain_jindu')->alias('jd')
  305. ->join('worker','jd.worker_id = worker.id','LEFT')
  306. ->field('jd.id,jd.worker_id,jd.weixiu_times,jd.title,jd.images,jd.createtime,worker.avatar,worker.truename')
  307. ->where('jd.order_id',$id)
  308. ->order('jd.id desc')->select();
  309. if(!empty($jindu_list)){
  310. for($i=$jindu_list[0]['weixiu_times'];$i>=1;$i--){
  311. foreach($jindu_list as $key => $val){
  312. if($i == $val['weixiu_times']){
  313. $new_jindulist[$i][] = $val;
  314. }
  315. }
  316. }
  317. foreach($new_jindulist as $key => $val){
  318. $last_jindulist[] = [
  319. 'title' => '第'.$val[0]['weixiu_times'].'次',
  320. 'child' => $val
  321. ];
  322. }
  323. }
  324. $info['jindu'] = $last_jindulist;
  325. $this->success(1,$info);
  326. }
  327. //提交报价
  328. public function baojia(){
  329. $id = input('id',0);
  330. $baojia_file = input('baojia_file','');
  331. $baojia_filename = input('baojia_filename','');
  332. if(empty($baojia_file) || empty($baojia_filename)){
  333. $this->error();
  334. }
  335. Db::startTrans();
  336. $info = Db::name($this->table)->where('id',$id)->lock(true)->find();
  337. if(empty($info)){
  338. Db::rollback();
  339. $this->error('没找到该信息,请刷新重试');
  340. }
  341. if($info['company_id'] != $this->auth->company_id){
  342. Db::rollback();
  343. $this->error('没找到该信息,请刷新重试');
  344. }
  345. if($info['status'] == 20){
  346. Db::rollback();
  347. $this->error('当前订单报价正在审核中');
  348. }
  349. if($info['status'] != 22 && $info['status'] != 0){
  350. Db::rollback();
  351. $this->error('当前订单不能报价');
  352. }
  353. //报价
  354. $nowtime = time();
  355. $baojia_data = [
  356. 'order_id' => $info['id'],
  357. 'company_id' => $info['company_id'],
  358. 'user_id' => $info['user_id'],
  359. 'baojia_staffid' => $this->auth->id,
  360. 'baojia_time' => $nowtime,
  361. 'baojia_filename' => $baojia_filename,
  362. 'baojia_file' => $baojia_file,
  363. 'status' => 20,
  364. 'updatetime' => $nowtime,
  365. ];
  366. $baojia_id = Db::name('maintain_baojia')->insertGetId($baojia_data);
  367. if(!$baojia_id){
  368. Db::rollback();
  369. $this->error('报价失败,请重试');
  370. }
  371. //
  372. $update = [
  373. 'status' => 20,
  374. 'updatetime' => $nowtime,
  375. ];
  376. $rs_update = Db::name($this->table)->where('id',$id)->update($update);
  377. if($rs_update === false){
  378. Db::rollback();
  379. $this->error('报价失败,请重试');
  380. }
  381. //
  382. Db::commit();
  383. $this->success();
  384. }
  385. //审核报价
  386. public function baojia_audit(){
  387. $baojia_id = input('baojia_id',0);
  388. $status = input('status',2);
  389. $reason = input('reason','');
  390. Db::startTrans();
  391. $nowtime = time();
  392. $baojia_info = Db::name('maintain_baojia')->where('id',$baojia_id)->lock(true)->find();
  393. if(empty($baojia_info)){
  394. Db::rollback();
  395. $this->error('没找到该信息,请刷新重试');
  396. }
  397. $info = Db::name($this->table)->where('id',$baojia_info['order_id'])->lock(true)->find();
  398. if(empty($info)){
  399. Db::rollback();
  400. $this->error('没找到该信息,请刷新重试');
  401. }
  402. if($baojia_info['status'] != 20){
  403. Db::rollback();
  404. $this->error('状态错误不能审核,请刷新重试');
  405. }
  406. if($info['status'] != 20){
  407. Db::rollback();
  408. $this->error('状态错误不能审核,请刷新重试');
  409. }
  410. if($status == 1){
  411. //通过
  412. $baojia_update = [
  413. 'status' => 30,
  414. 'baojia_audit_staffid' => $this->auth->id,
  415. 'baojia_audit_time' => $nowtime,
  416. 'updatetime' => $nowtime,
  417. ];
  418. }else{
  419. //驳回
  420. $baojia_update = [
  421. 'status' => 22,
  422. 'baojia_audit_staffid' => $this->auth->id,
  423. 'baojia_audit_time' => $nowtime,
  424. 'baojia_audit_reason' => $reason,
  425. 'updatetime' => $nowtime,
  426. ];
  427. }
  428. $rs_audit = Db::name('maintain_baojia')->where('id',$baojia_id)->update($baojia_update);
  429. if($rs_audit === false){
  430. Db::rollback();
  431. $this->error('审核失败,请重试');
  432. }
  433. //订单
  434. $update = [
  435. 'status' => $baojia_update['status'],
  436. 'updatetime' => $nowtime,
  437. ];
  438. $rs_update = Db::name($this->table)->where('id',$baojia_info['order_id'])->update($update);
  439. if($rs_update === false){
  440. Db::rollback();
  441. $this->error('审核失败,请重试');
  442. }
  443. //
  444. Db::commit();
  445. $this->success();
  446. }
  447. //流转到总公司
  448. public function liuzhuan(){
  449. $id = input('id',0);
  450. Db::startTrans();
  451. $info = Db::name($this->table)->where('id',$id)->lock(true)->find();
  452. if(empty($info)){
  453. Db::rollback();
  454. $this->error('没找到该信息,请刷新重试');
  455. }
  456. if($info['company_id'] != $this->auth->company_id){
  457. Db::rollback();
  458. $this->error('没找到该信息,请刷新重试');
  459. }
  460. if(!in_array($info['status'],[0,20,22,30,40])){
  461. Db::rollback();
  462. $this->error('当前订单状态,不能流转');
  463. }
  464. if(!empty($info['worker_id'])){
  465. Db::rollback();
  466. $this->error('已经指派了师傅,不能流转了');
  467. }
  468. //
  469. $update = [
  470. 'liuzhuan_status' => 1,
  471. 'updatetime' => time(),
  472. ];
  473. $rs_update = Db::name($this->table)->where('id',$id)->update($update);
  474. if($rs_update === false){
  475. Db::rollback();
  476. $this->error('流转失败,请重试');
  477. }
  478. //
  479. Db::commit();
  480. $this->success();
  481. }
  482. //指派师傅
  483. public function zhipai(){
  484. //检查师傅
  485. $worker_id = input('worker_id',0);
  486. $worker = Db::name('worker')->where('id',$worker_id)->where('status',1)->where('company_id',$this->auth->company_id)->find();
  487. if(empty($worker)){
  488. $this->error('没找到该师傅');
  489. }
  490. //检查订单
  491. $id = input('id',0);
  492. Db::startTrans();
  493. $info = Db::name($this->table)->where('id',$id)->lock(true)->find();
  494. if(empty($info)){
  495. Db::rollback();
  496. $this->error('没找到该信息,请刷新重试');
  497. }
  498. if($info['company_id'] != $this->auth->company_id){
  499. Db::rollback();
  500. $this->error('没找到该信息,请刷新重试');
  501. }
  502. /*if(in_array($info['status'],[20,22,30])){
  503. Db::rollback();
  504. $this->error('报价未确认,还不能指派师傅');
  505. }
  506. if($info['status'] != 0 && $info['status'] != 40 ){
  507. Db::rollback();
  508. $this->error('当前订单状态,不能指派师傅');
  509. }*/
  510. if(!in_array($info['status'],[0,20,22,30,40])){
  511. Db::rollback();
  512. $this->error('当前订单状态,不能指派师傅');
  513. }
  514. if(!empty($info['worker_id'])){
  515. Db::rollback();
  516. $this->error('已经指派了师傅');
  517. }
  518. //
  519. $update = [
  520. 'worker_id' => $worker_id,
  521. 'status' => 50,
  522. 'updatetime' => time(),
  523. ];
  524. $rs_update = Db::name($this->table)->where('id',$id)->update($update);
  525. if($rs_update === false){
  526. Db::rollback();
  527. $this->error('指派失败,请重试');
  528. }
  529. //
  530. Db::commit();
  531. $this->success();
  532. }
  533. //修改报价金额
  534. public function set_price(){
  535. $id = input('id',0);
  536. $price = input('price',0);
  537. $update = [
  538. 'price' => $price,
  539. 'updatetime' => time(),
  540. ];
  541. $rs_update = Db::name($this->table)->where('id',$id)->update($update);
  542. $this->success();
  543. }
  544. }