Maintain.php 20 KB

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