Maintain.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. ->join('company','mt.company_id = company.id','LEFT')
  274. ->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,company.companyname')
  275. ->where('mt.id',$id)
  276. ->find();
  277. $maintain_model = new Maintainmodel();
  278. $info['status_text'] = $maintain_model->status_data($info['status']);
  279. //pc用户,已流转订单,直接显示流转中
  280. if($info['company_id'] != 1 && in_array($info['status'],[0,20,22,30,40]) && $info['liuzhuan_status'] == 1){
  281. $info['status_text'] = '流转中';
  282. }
  283. $info['status_colorType'] = $maintain_model->status_colorType($info['status']);
  284. //假状态
  285. $info['fake_status'] = $this->fake_status($info['status']);
  286. $info['fake_status_text'] = $this->fake_status_data($info['status']);
  287. $info['filedata'] = json_decode($info['filedata'],true);
  288. //追加报价历史
  289. $baojia = Db::name('maintain_baojia')->alias('bj')
  290. ->field('bj.*,admin.nickname as baojia_nickname,audit.nickname as baojia_audit_nickname')
  291. ->join('pc_admin admin','bj.baojia_staffid = admin.id','LEFT')
  292. ->join('pc_admin audit','bj.baojia_audit_staffid = audit.id','LEFT')
  293. ->where('order_id',$id)->order('id desc')->select();
  294. if(!empty($baojia)){
  295. foreach($baojia as $key => $val){
  296. $baojia[$key]['status_text'] = $maintain_model->baojia_status_data($val['status']);
  297. }
  298. }
  299. $info['baojia_list'] = $baojia;
  300. //追加材料列表
  301. $info['cailiao_list'] = Db::name('maintain_cailiao')->field('id,name,number,danwei,images')->where('order_id',$id)->order('id desc')->select();
  302. //追加多次维修+对应进度历史
  303. $last_jindulist = [];
  304. $new_jindulist = [];
  305. $jindu_list = Db::name('maintain_jindu')->alias('jd')
  306. ->join('worker','jd.worker_id = worker.id','LEFT')
  307. ->field('jd.id,jd.worker_id,jd.weixiu_times,jd.title,jd.images,jd.createtime,worker.avatar,worker.truename')
  308. ->where('jd.order_id',$id)
  309. ->order('jd.id desc')->select();
  310. if(!empty($jindu_list)){
  311. for($i=$jindu_list[0]['weixiu_times'];$i>=1;$i--){
  312. foreach($jindu_list as $key => $val){
  313. if($i == $val['weixiu_times']){
  314. $new_jindulist[$i][] = $val;
  315. }
  316. }
  317. }
  318. foreach($new_jindulist as $key => $val){
  319. $last_jindulist[] = [
  320. 'title' => '第'.$val[0]['weixiu_times'].'次',
  321. 'child' => $val
  322. ];
  323. }
  324. }
  325. $info['jindu'] = $last_jindulist;
  326. $this->success(1,$info);
  327. }
  328. //提交报价
  329. public function baojia(){
  330. $id = input('id',0);
  331. $baojia_file = input('baojia_file','');
  332. $baojia_filename = input('baojia_filename','');
  333. if(empty($baojia_file) || empty($baojia_filename)){
  334. $this->error();
  335. }
  336. Db::startTrans();
  337. $info = Db::name($this->table)->where('id',$id)->lock(true)->find();
  338. if(empty($info)){
  339. Db::rollback();
  340. $this->error('没找到该信息,请刷新重试');
  341. }
  342. if($info['company_id'] != $this->auth->company_id){
  343. Db::rollback();
  344. $this->error('没找到该信息,请刷新重试');
  345. }
  346. if($info['status'] == 20){
  347. Db::rollback();
  348. $this->error('当前订单报价正在审核中');
  349. }
  350. if($info['status'] != 22 && $info['status'] != 0){
  351. Db::rollback();
  352. $this->error('当前订单不能报价');
  353. }
  354. //报价
  355. $nowtime = time();
  356. $baojia_data = [
  357. 'order_id' => $info['id'],
  358. 'company_id' => $info['company_id'],
  359. 'user_id' => $info['user_id'],
  360. 'baojia_staffid' => $this->auth->id,
  361. 'baojia_time' => $nowtime,
  362. 'baojia_filename' => $baojia_filename,
  363. 'baojia_file' => $baojia_file,
  364. 'status' => 20,
  365. 'updatetime' => $nowtime,
  366. ];
  367. $baojia_id = Db::name('maintain_baojia')->insertGetId($baojia_data);
  368. if(!$baojia_id){
  369. Db::rollback();
  370. $this->error('报价失败,请重试');
  371. }
  372. //
  373. $update = [
  374. 'status' => 20,
  375. 'updatetime' => $nowtime,
  376. ];
  377. $rs_update = Db::name($this->table)->where('id',$id)->update($update);
  378. if($rs_update === false){
  379. Db::rollback();
  380. $this->error('报价失败,请重试');
  381. }
  382. //
  383. Db::commit();
  384. $this->success();
  385. }
  386. //审核报价
  387. public function baojia_audit(){
  388. $baojia_id = input('baojia_id',0);
  389. $status = input('status',2);
  390. $reason = input('reason','');
  391. Db::startTrans();
  392. $nowtime = time();
  393. $baojia_info = Db::name('maintain_baojia')->where('id',$baojia_id)->lock(true)->find();
  394. if(empty($baojia_info)){
  395. Db::rollback();
  396. $this->error('没找到该信息,请刷新重试');
  397. }
  398. $info = Db::name($this->table)->where('id',$baojia_info['order_id'])->lock(true)->find();
  399. if(empty($info)){
  400. Db::rollback();
  401. $this->error('没找到该信息,请刷新重试');
  402. }
  403. if($baojia_info['status'] != 20){
  404. Db::rollback();
  405. $this->error('状态错误不能审核,请刷新重试');
  406. }
  407. if($info['status'] != 20){
  408. Db::rollback();
  409. $this->error('状态错误不能审核,请刷新重试');
  410. }
  411. if($status == 1){
  412. //通过
  413. $baojia_update = [
  414. 'status' => 30,
  415. 'baojia_audit_staffid' => $this->auth->id,
  416. 'baojia_audit_time' => $nowtime,
  417. 'updatetime' => $nowtime,
  418. ];
  419. }else{
  420. //驳回
  421. $baojia_update = [
  422. 'status' => 22,
  423. 'baojia_audit_staffid' => $this->auth->id,
  424. 'baojia_audit_time' => $nowtime,
  425. 'baojia_audit_reason' => $reason,
  426. 'updatetime' => $nowtime,
  427. ];
  428. }
  429. $rs_audit = Db::name('maintain_baojia')->where('id',$baojia_id)->update($baojia_update);
  430. if($rs_audit === false){
  431. Db::rollback();
  432. $this->error('审核失败,请重试');
  433. }
  434. //订单
  435. $update = [
  436. 'status' => $baojia_update['status'],
  437. 'updatetime' => $nowtime,
  438. ];
  439. $rs_update = Db::name($this->table)->where('id',$baojia_info['order_id'])->update($update);
  440. if($rs_update === false){
  441. Db::rollback();
  442. $this->error('审核失败,请重试');
  443. }
  444. //
  445. Db::commit();
  446. $this->success();
  447. }
  448. //流转到总公司
  449. public function liuzhuan(){
  450. $id = input('id',0);
  451. Db::startTrans();
  452. $info = Db::name($this->table)->where('id',$id)->lock(true)->find();
  453. if(empty($info)){
  454. Db::rollback();
  455. $this->error('没找到该信息,请刷新重试');
  456. }
  457. if($info['company_id'] != $this->auth->company_id){
  458. Db::rollback();
  459. $this->error('没找到该信息,请刷新重试');
  460. }
  461. if(!in_array($info['status'],[0,20,22,30,40])){
  462. Db::rollback();
  463. $this->error('当前订单状态,不能流转');
  464. }
  465. if(!empty($info['worker_id'])){
  466. Db::rollback();
  467. $this->error('已经指派了师傅,不能流转了');
  468. }
  469. //
  470. $update = [
  471. 'liuzhuan_status' => 1,
  472. 'updatetime' => time(),
  473. ];
  474. $rs_update = Db::name($this->table)->where('id',$id)->update($update);
  475. if($rs_update === false){
  476. Db::rollback();
  477. $this->error('流转失败,请重试');
  478. }
  479. //
  480. Db::commit();
  481. $this->success();
  482. }
  483. //指派师傅
  484. public function zhipai(){
  485. //检查师傅
  486. $worker_id = input('worker_id',0);
  487. $worker = Db::name('worker')->where('id',$worker_id)->where('status',1)->where('company_id',$this->auth->company_id)->find();
  488. if(empty($worker)){
  489. $this->error('没找到该师傅');
  490. }
  491. //检查订单
  492. $id = input('id',0);
  493. Db::startTrans();
  494. $info = Db::name($this->table)->where('id',$id)->lock(true)->find();
  495. if(empty($info)){
  496. Db::rollback();
  497. $this->error('没找到该信息,请刷新重试');
  498. }
  499. if($this->auth->company_id != 1 && $info['company_id'] != $this->auth->company_id){
  500. Db::rollback();
  501. $this->error('没找到该信息,请刷新重试');
  502. }
  503. /*if(in_array($info['status'],[20,22,30])){
  504. Db::rollback();
  505. $this->error('报价未确认,还不能指派师傅');
  506. }
  507. if($info['status'] != 0 && $info['status'] != 40 ){
  508. Db::rollback();
  509. $this->error('当前订单状态,不能指派师傅');
  510. }*/
  511. if(!in_array($info['status'],[0,20,22,30,40])){
  512. Db::rollback();
  513. $this->error('当前订单状态,不能指派师傅');
  514. }
  515. if(!empty($info['worker_id'])){
  516. Db::rollback();
  517. $this->error('已经指派了师傅');
  518. }
  519. //
  520. $update = [
  521. 'worker_id' => $worker_id,
  522. 'status' => 50,
  523. 'updatetime' => time(),
  524. ];
  525. $rs_update = Db::name($this->table)->where('id',$id)->update($update);
  526. if($rs_update === false){
  527. Db::rollback();
  528. $this->error('指派失败,请重试');
  529. }
  530. //
  531. Db::commit();
  532. $this->success();
  533. }
  534. //修改报价金额
  535. public function set_price(){
  536. $id = input('id',0);
  537. $price = input('price',0);
  538. $update = [
  539. 'price' => $price,
  540. 'updatetime' => time(),
  541. ];
  542. $rs_update = Db::name($this->table)->where('id',$id)->update($update);
  543. $this->success();
  544. }
  545. }