Maintain.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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'];
  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. $rs = [
  22. 'status_0' => 0,
  23. 'status_20' => 0,
  24. 'status_30' => 0,
  25. 'status_40' => 0,
  26. 'status_100' => 0,
  27. 'score' => 5,
  28. ];
  29. $list = Db::name($this->table)->field('status,count(id) as number')
  30. // ->where('status','NEQ',2)
  31. ->where('company_id',$this->auth->company_id)->group('status')->select();
  32. if(!empty($list)){
  33. foreach($list as $key => $val){
  34. if($val['status'] == 0){
  35. $rs['status_0'] = $val['number'];
  36. }
  37. if(in_array($val['status'],[20,22])){
  38. $rs['status_20'] += $val['number'];
  39. }
  40. if($val['status'] == 30){
  41. $rs['status_30'] = $val['number'];
  42. }
  43. if(in_array($val['status'],[40,50,60,70,80,90,92])){
  44. $rs['status_40'] += $val['number'];
  45. }
  46. /*if($val['status'] == 100){
  47. $rs['status_100'] = $val['number'];
  48. }*/
  49. if(in_array($val['status'],[2,100])){
  50. $rs['status_100'] += $val['number'];
  51. }
  52. }
  53. }
  54. //本周服务评分
  55. $score = Db::name($this->table)
  56. ->where('status',100)
  57. ->where('company_id',$this->auth->company_id)
  58. ->whereTime('eva_time','week')
  59. /*->select(false);echo $score;exit;*/
  60. ->avg('eva_score');
  61. $rs['score'] = $score;
  62. $this->success(1,$rs);
  63. }
  64. //列表
  65. public function lists(){
  66. $orderno = input('orderno','');
  67. $projectname = input('projectname','');
  68. $header = input('header','');
  69. $status = input('status','ALL'); //默认ALL
  70. $map = [
  71. 'mt.company_id' => $this->auth->company_id,
  72. ];
  73. if(!empty($orderno)){
  74. $map['mt.orderno'] = $orderno;
  75. }
  76. if(!empty($projectname)){
  77. $map['uc.projectname'] = ['LIKE','%'.$projectname.'%'];
  78. }
  79. if(!empty($header)){
  80. $map['uc.header'] = ['LIKE','%'.$header.'%'];
  81. }
  82. if($status != 'ALL'){
  83. $map['mt.status'] = $status;
  84. if($status == 20){
  85. $map['mt.status'] = ['IN',[20,22]];
  86. }
  87. if($status == 40){
  88. $map['mt.status'] = ['IN',[40,50,60,70,80,90,92]];
  89. }
  90. if($status == 100){
  91. $map['mt.status'] = ['IN',[2,100]];
  92. }
  93. }
  94. $field = ['mt.id','mt.orderno','uc.projectname','mt.info','uc.header','mt.status'];
  95. $list = Db::name($this->table)->alias('mt')
  96. ->join('user_company uc','mt.uc_id = uc.id','LEFT')
  97. ->field($field)
  98. ->where($map)->order('mt.id desc')
  99. ->paginate();
  100. $total = $list->total();
  101. $list = $list->items();
  102. if(!empty($list)){
  103. $maintain_model = new Maintainmodel();
  104. foreach($list as $key => $val){
  105. $list[$key]['status_text'] = $maintain_model->status_data($val['status']);
  106. $list[$key]['status_colorType'] = $maintain_model->status_colorType($val['status']);
  107. //假状态
  108. $list[$key]['fake_status'] = $this->fake_status($val['status']);
  109. $list[$key]['fake_status_text'] = $this->fake_status_data($val['status']);
  110. }
  111. }
  112. $rs = [
  113. 'list' => $list,
  114. 'total'=> $total,
  115. ];
  116. $this->success(1,$rs);
  117. }
  118. //整合过的状态
  119. private function fake_status_data($status){
  120. $data = [
  121. 0 => '待报价',
  122. 2 => '已取消',
  123. 20 => '评估报价',
  124. 22 => '评估报价',
  125. 30 => '报价审核',
  126. 40 => '待处理',
  127. 50 => '待处理',
  128. 60 => '待处理',
  129. 70 => '待处理',
  130. 80 => '待处理',
  131. 90 => '待处理',
  132. 92 => '待处理',
  133. 100 => '已完成',
  134. ];
  135. return isset($data[$status]) ? $data[$status] : $status;
  136. }
  137. //整合过的状态
  138. private function fake_status($status){
  139. $data = [
  140. 0 => 0,
  141. 2 => 2,
  142. 20 => 20,
  143. 22 => 20,
  144. 30 => 30,
  145. 40 => 40,
  146. 50 => 40,
  147. 60 => 40,
  148. 70 => 40,
  149. 80 => 40,
  150. 90 => 40,
  151. 92 => 40,
  152. 100 => 100,
  153. ];
  154. return isset($data[$status]) ? $data[$status] : $status;
  155. }
  156. //详情
  157. public function info(){
  158. $id = input('id',0);
  159. $info = Db::name($this->table)->alias('mt')
  160. ->join('user_company uc','mt.uc_id = uc.id','LEFT')
  161. ->join('user','mt.user_id = user.id','LEFT')
  162. ->join('worker','mt.worker_id = worker.id','LEFT')
  163. ->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')
  164. ->where('mt.id',$id)->where('mt.company_id',$this->auth->company_id)
  165. ->find();
  166. $maintain_model = new Maintainmodel();
  167. $info['status_text'] = $maintain_model->status_data($info['status']);
  168. $info['status_colorType'] = $maintain_model->status_colorType($info['status']);
  169. //假状态
  170. $info['fake_status'] = $this->fake_status($info['status']);
  171. $info['fake_status_text'] = $this->fake_status_data($info['status']);
  172. $info['filedata'] = json_decode($info['filedata'],true);
  173. //追加报价历史
  174. $baojia = Db::name('maintain_baojia')->alias('bj')
  175. ->field('bj.*,admin.nickname as baojia_nickname,audit.nickname as baojia_audit_nickname')
  176. ->join('pc_admin admin','bj.baojia_staffid = admin.id','LEFT')
  177. ->join('pc_admin audit','bj.baojia_audit_staffid = audit.id','LEFT')
  178. ->where('order_id',$id)->order('id desc')->select();
  179. if(!empty($baojia)){
  180. foreach($baojia as $key => $val){
  181. $baojia[$key]['status_text'] = $maintain_model->baojia_status_data($val['status']);
  182. }
  183. }
  184. $info['baojia_list'] = $baojia;
  185. //追加材料列表
  186. $info['cailiao_list'] = Db::name('maintain_cailiao')->field('id,name,number,danwei,images')->where('order_id',$id)->order('id desc')->select();
  187. //追加多次维修+对应进度历史
  188. $last_jindulist = [];
  189. $new_jindulist = [];
  190. $jindu_list = Db::name('maintain_jindu')->alias('jd')
  191. ->join('worker','jd.worker_id = worker.id','LEFT')
  192. ->field('jd.id,jd.worker_id,jd.weixiu_times,jd.title,jd.images,jd.createtime,worker.avatar,worker.truename')
  193. ->where('jd.order_id',$id)
  194. ->order('jd.id desc')->select();
  195. if(!empty($jindu_list)){
  196. for($i=$jindu_list[0]['weixiu_times'];$i>=1;$i--){
  197. foreach($jindu_list as $key => $val){
  198. if($i == $val['weixiu_times']){
  199. $new_jindulist[$i][] = $val;
  200. }
  201. }
  202. }
  203. foreach($new_jindulist as $key => $val){
  204. $last_jindulist[] = [
  205. 'title' => '第'.$val[0]['weixiu_times'].'次',
  206. 'child' => $val
  207. ];
  208. }
  209. }
  210. $info['jindu'] = $last_jindulist;
  211. $this->success(1,$info);
  212. }
  213. //提交报价
  214. public function baojia(){
  215. $id = input('id',0);
  216. $baojia_file = input('baojia_file','');
  217. $baojia_filename = input('baojia_filename','');
  218. if(empty($baojia_file) || empty($baojia_filename)){
  219. $this->error();
  220. }
  221. Db::startTrans();
  222. $info = Db::name($this->table)->where('id',$id)->lock(true)->find();
  223. if(empty($info)){
  224. Db::rollback();
  225. $this->error('没找到该信息,请刷新重试');
  226. }
  227. if($info['company_id'] != $this->auth->company_id){
  228. Db::rollback();
  229. $this->error('没找到该信息,请刷新重试');
  230. }
  231. if($info['status'] == 20){
  232. Db::rollback();
  233. $this->error('当前订单报价正在审核中');
  234. }
  235. if($info['status'] != 22 && $info['status'] != 0){
  236. Db::rollback();
  237. $this->error('当前订单不能报价');
  238. }
  239. //报价
  240. $nowtime = time();
  241. $baojia_data = [
  242. 'order_id' => $info['id'],
  243. 'company_id' => $info['company_id'],
  244. 'user_id' => $info['user_id'],
  245. 'baojia_staffid' => $this->auth->id,
  246. 'baojia_time' => $nowtime,
  247. 'baojia_filename' => $baojia_filename,
  248. 'baojia_file' => $baojia_file,
  249. 'status' => 20,
  250. 'updatetime' => $nowtime,
  251. ];
  252. $baojia_id = Db::name('maintain_baojia')->insertGetId($baojia_data);
  253. if(!$baojia_id){
  254. Db::rollback();
  255. $this->error('报价失败,请重试');
  256. }
  257. //
  258. $update = [
  259. 'status' => 20,
  260. 'updatetime' => $nowtime,
  261. ];
  262. $rs_update = Db::name($this->table)->where('id',$id)->update($update);
  263. if($rs_update === false){
  264. Db::rollback();
  265. $this->error('报价失败,请重试');
  266. }
  267. //
  268. Db::commit();
  269. $this->success();
  270. }
  271. //审核报价
  272. public function baojia_audit(){
  273. $baojia_id = input('baojia_id',0);
  274. $status = input('status',2);
  275. $reason = input('reason','');
  276. Db::startTrans();
  277. $nowtime = time();
  278. $baojia_info = Db::name('maintain_baojia')->where('id',$baojia_id)->lock(true)->find();
  279. if(empty($baojia_info)){
  280. Db::rollback();
  281. $this->error('没找到该信息,请刷新重试');
  282. }
  283. $info = Db::name($this->table)->where('id',$baojia_info['order_id'])->lock(true)->find();
  284. if(empty($info)){
  285. Db::rollback();
  286. $this->error('没找到该信息,请刷新重试');
  287. }
  288. if($baojia_info['status'] != 20){
  289. Db::rollback();
  290. $this->error('状态错误不能审核,请刷新重试');
  291. }
  292. if($info['status'] != 20){
  293. Db::rollback();
  294. $this->error('状态错误不能审核,请刷新重试');
  295. }
  296. if($status == 1){
  297. //通过
  298. $baojia_update = [
  299. 'status' => 30,
  300. 'baojia_audit_staffid' => $this->auth->id,
  301. 'baojia_audit_time' => $nowtime,
  302. 'updatetime' => $nowtime,
  303. ];
  304. }else{
  305. //驳回
  306. $baojia_update = [
  307. 'status' => 22,
  308. 'baojia_audit_staffid' => $this->auth->id,
  309. 'baojia_audit_time' => $nowtime,
  310. 'baojia_audit_reason' => $reason,
  311. 'updatetime' => $nowtime,
  312. ];
  313. }
  314. $rs_audit = Db::name('maintain_baojia')->where('id',$baojia_id)->update($baojia_update);
  315. if($rs_audit === false){
  316. Db::rollback();
  317. $this->error('审核失败,请重试');
  318. }
  319. //订单
  320. $update = [
  321. 'status' => $baojia_update['status'],
  322. 'updatetime' => $nowtime,
  323. ];
  324. $rs_update = Db::name($this->table)->where('id',$baojia_info['order_id'])->update($update);
  325. if($rs_update === false){
  326. Db::rollback();
  327. $this->error('审核失败,请重试');
  328. }
  329. //
  330. Db::commit();
  331. $this->success();
  332. }
  333. //指派师傅
  334. public function zhipai(){
  335. //检查师傅
  336. $worker_id = input('worker_id',0);
  337. $worker = Db::name('worker')->where('id',$worker_id)->where('status',1)->where('company_id',$this->auth->company_id)->find();
  338. if(empty($worker)){
  339. $this->error('没找到该师傅');
  340. }
  341. //检查订单
  342. $id = input('id',0);
  343. Db::startTrans();
  344. $info = Db::name($this->table)->where('id',$id)->lock(true)->find();
  345. if(empty($info)){
  346. Db::rollback();
  347. $this->error('没找到该信息,请刷新重试');
  348. }
  349. if($info['company_id'] != $this->auth->company_id){
  350. Db::rollback();
  351. $this->error('没找到该信息,请刷新重试');
  352. }
  353. /*if(in_array($info['status'],[20,22,30])){
  354. Db::rollback();
  355. $this->error('报价未确认,还不能指派师傅');
  356. }
  357. if($info['status'] != 0 && $info['status'] != 40 ){
  358. Db::rollback();
  359. $this->error('当前订单状态,不能指派师傅');
  360. }*/
  361. if(!in_array($info['status'],[0,20,22,30,40])){
  362. Db::rollback();
  363. $this->error('当前订单状态,不能指派师傅');
  364. }
  365. if(!empty($info['worker_id'])){
  366. Db::rollback();
  367. $this->error('已经指派了师傅');
  368. }
  369. //
  370. $update = [
  371. 'worker_id' => $worker_id,
  372. 'status' => 50,
  373. 'updatetime' => time(),
  374. ];
  375. $rs_update = Db::name($this->table)->where('id',$id)->update($update);
  376. if($rs_update === false){
  377. Db::rollback();
  378. $this->error('指派失败,请重试');
  379. }
  380. //
  381. Db::commit();
  382. $this->success();
  383. }
  384. }