Maintain.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Maintain as Maintainmodel;
  5. use think\Db;
  6. /**
  7. * 保修
  8. */
  9. class Maintain extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. //报修
  14. public function addone()
  15. {
  16. $info = input('info','');
  17. $filedata = input('filedata','','htmlspecialchars_decode');
  18. $mobile = input('mobile','');
  19. $address = input('address','');
  20. //视频类型追加缩略图
  21. if(!empty($filedata)){
  22. $filedata = json_decode($filedata,true);
  23. if(is_array($filedata) && !empty($filedata)){
  24. foreach($filedata as $key => $file){
  25. if($file['type'] == 'video'){
  26. $file_url = explode('.', $file['url']);
  27. unset($file_url[count($file_url) - 1]);
  28. $file['images_thumb'] = implode('.', $file_url) . '_0.jpg';
  29. }else{
  30. $file['images_thumb'] = $file['url'];
  31. }
  32. $filedata[$key] = $file;
  33. }
  34. $filedata = json_encode($filedata);
  35. }else{
  36. $filedata = '';
  37. }
  38. }else{
  39. $filedata = '';
  40. }
  41. $nowtime = time();
  42. //
  43. if(empty($this->auth->company_id)){
  44. $this->error('您还没绑定维保公司');
  45. }
  46. $uc_id = Db::name('user_company')->where('user_id',$this->auth->id)->where('company_id',$this->auth->company_id)->value('id');
  47. if(empty($uc_id)){
  48. $this->error('您还没绑定维保公司');
  49. }
  50. //写入
  51. $data = [
  52. 'orderno' => createUniqueNo('',''),
  53. 'user_id' => $this->auth->id,
  54. 'company_id' => $this->auth->company_id,
  55. 'uc_id' => $uc_id,
  56. 'createtime' => $nowtime,
  57. 'updatetime' => $nowtime,
  58. 'info' => $info,
  59. 'filedata' => $filedata,
  60. 'mobile' => $mobile,
  61. 'address' => $address,
  62. 'status' => 0,
  63. ];
  64. $order_id = Db::name('maintain')->insertGetId($data);
  65. $this->success('提交成功', $order_id);
  66. }
  67. //列表
  68. public function lists(){
  69. //待报价, 0
  70. //报价审核,30
  71. //维修中,40
  72. //已完成,100
  73. $status = input('status',0); //默认待审核
  74. $map = [
  75. 'user_id' => $this->auth->id,
  76. 'status' => $status,
  77. ];
  78. if($status == 0){
  79. $map['status'] = ['IN',[0,20,22]];
  80. }
  81. if($status == 40){
  82. $map['status'] = ['IN',[40,50,60,70,80,90,92]];
  83. }
  84. $field = ['id','orderno','createtime','info','filedata','status','eva_time'];
  85. $list = Db::name('maintain')->field($field)
  86. ->where($map)->order('id desc')
  87. ->autopage()->select();
  88. if(!empty($list)){
  89. //负责人的电话
  90. $header_mobile = Db::name('user_company')->where('user_id',$this->auth->id)->value('header_mobile');
  91. $maintain_model = new Maintainmodel();
  92. foreach($list as $key => $val){
  93. $list[$key]['header_mobile'] = $header_mobile;
  94. $list[$key]['status_text'] = $maintain_model->status_data($val['status']);
  95. }
  96. }
  97. $this->success(1,$list);
  98. }
  99. //详情
  100. public function info(){
  101. $id = input('id',0);
  102. $map = [
  103. 'user_id' => $this->auth->id,
  104. 'id' => $id,
  105. ];
  106. $field = ['id','orderno','createtime','info','filedata','mobile','address','status','finishtime','worker_id','shangmen_time','eva_info','eva_time','eva_score','weixiu_id'];
  107. $info = Db::name('maintain')->field($field)
  108. ->where($map)
  109. ->find();
  110. if(empty($info)){
  111. $this->error('没找到该订单');
  112. }
  113. $maintain_model = new Maintainmodel();
  114. $info['status_text'] = $maintain_model->status_data($info['status']);
  115. //负责人的电话
  116. $header_mobile = Db::name('user_company')->where('user_id',$this->auth->id)->value('header_mobile');
  117. $info['header_mobile'] = $header_mobile;
  118. //追加维修师傅
  119. $info['worker_info'] = [];
  120. if($info['worker_id'] != 0){
  121. $worker_info = Db::name('worker')->field('avatar,truename,mobile')->where('id',$info['worker_id'])->find();
  122. if(!empty($worker_info)){
  123. $worker_info['avatar'] = localpath_to_netpath($worker_info['avatar']);
  124. $info['worker_info'] = $worker_info;
  125. }
  126. }
  127. //追加进度
  128. //追加多次维修+对应进度历史
  129. $last_jindulist = [];
  130. $new_jindulist = [];
  131. $jindu_list = Db::name('maintain_jindu')->alias('jd')
  132. ->join('worker','jd.worker_id = worker.id','LEFT')
  133. ->field('jd.id,jd.worker_id,jd.weixiu_times,jd.title,jd.images,jd.createtime,worker.avatar,worker.truename')
  134. ->where('jd.order_id',$id)
  135. ->order('jd.id desc')->select();
  136. $jindu_list = list_domain_image($jindu_list,['avatar','images']);
  137. if(!empty($jindu_list)){
  138. for($i=$jindu_list[0]['weixiu_times'];$i>=1;$i--){
  139. foreach($jindu_list as $key => $val){
  140. if($i == $val['weixiu_times']){
  141. $new_jindulist[$i][] = $val;
  142. }
  143. }
  144. }
  145. foreach($new_jindulist as $key => $val){
  146. $last_jindulist[] = [
  147. 'title' => '第'.$val[0]['weixiu_times'].'次',
  148. 'child' => $val
  149. ];
  150. }
  151. }
  152. $info['jindu'] = $last_jindulist;
  153. $this->success(1, $info);
  154. }
  155. //取消上报
  156. public function cancel(){
  157. $id = input('id',0);
  158. $map = [
  159. 'user_id' => $this->auth->id,
  160. 'id' => $id,
  161. ];
  162. $info = Db::name('maintain')
  163. ->where($map)
  164. ->find();
  165. if(empty($info)){
  166. $this->error('不存在的订单');
  167. }
  168. if($info['status'] >= 40){ //报价都审核过了,可派师傅了
  169. $this->error('现在已经不能取消了');
  170. }
  171. $nowtime = time();
  172. $update = [
  173. 'status' => 2,
  174. 'canceltime' => $nowtime, //取消时间
  175. 'finishtime' => $nowtime,
  176. 'updatetime' => $nowtime,
  177. ];
  178. $rs = Db::name('maintain')
  179. ->where($map)->update($update);
  180. $this->success('取消成功');
  181. }
  182. //报价详情
  183. public function baojia_info(){
  184. $order_id = input('order_id',0);
  185. //找出最新报价日志
  186. $baojia_log = Db::name('maintain_baojia')->field('id,baojia_filename,baojia_file')->where('order_id',$order_id)->where('status',30)->order('id desc')->find();
  187. $baojia_log = info_domain_image($baojia_log,['baojia_file']);
  188. $this->success(1,$baojia_log);
  189. }
  190. //报价确认
  191. public function baojia_confirm(){
  192. $id = input('order_id',0);
  193. Db::startTrans();
  194. //检查订单
  195. $map = [
  196. 'user_id' => $this->auth->id,
  197. 'id' => $id,
  198. ];
  199. $info = Db::name('maintain')->where($map)->lock(true)->find();
  200. if(empty($info)){
  201. Db::rollback();
  202. $this->error('不存在的订单');
  203. }
  204. if($info['status'] != 30){ //用户待确认
  205. Db::rollback();
  206. $this->success('订单错误,请刷新重试');
  207. }
  208. //找出最新报价日志
  209. $baojia_log = Db::name('maintain_baojia')->where('order_id',$id)->where('status',30)->order('id desc')->find();
  210. $nowtime = time();
  211. //更新订单
  212. $update = [
  213. 'status' => 40,
  214. 'updatetime' => $nowtime,
  215. 'baojia_confirmtime' => $nowtime, //报价确认时间
  216. ];
  217. //更新报价记录
  218. $update_baojia = [
  219. 'status' => 40,
  220. 'updatetime' => $nowtime,
  221. 'baojia_useraudit_time' => $nowtime,
  222. ];
  223. $rs1 = Db::name('maintain')->where('id',$id)->update($update);
  224. if($rs1 === false){
  225. Db::rollback();
  226. $this->error('确认失败');
  227. }
  228. $rs2 = Db::name('maintain_baojia')->where('id',$baojia_log['id'])->update($update_baojia);
  229. if($rs2 === false){
  230. Db::rollback();
  231. $this->error('确认失败');
  232. }
  233. Db::commit();
  234. $this->success('报价已确认');
  235. }
  236. //验收
  237. public function yanshou(){
  238. $id = input('order_id',0);
  239. $status = input('status',2);//1=通过,2=拒绝
  240. $reason = input('reason','','trim');
  241. //必填
  242. if($status == 2 && empty($reason)){
  243. $this->error('请输入拒绝原因');
  244. }
  245. Db::startTrans();
  246. //检查订单
  247. $map = [
  248. 'user_id' => $this->auth->id,
  249. 'id' => $id,
  250. ];
  251. $info = Db::name('maintain')->where($map)->lock(true)->find();
  252. if(empty($info)){
  253. Db::rollback();
  254. $this->error('不存在的订单');
  255. }
  256. if($info['status'] != 90){ //师傅完成
  257. Db::rollback();
  258. $this->success('订单错误,请刷新重试');
  259. }
  260. $nowtime = time();
  261. //更新订单
  262. //更新维修
  263. if($status == 2){
  264. $update = [
  265. 'status' => 92, // '用户验收驳回'
  266. 'updatetime' => $nowtime,
  267. 'finishtime' => $nowtime,
  268. ];
  269. $update_weixiu = [
  270. 'status' => 92, // '用户验收驳回'
  271. 'updatetime' => $nowtime,
  272. 'audit_time' => $nowtime,
  273. 'audit_reason' => $reason,
  274. ];
  275. $remark = '验收已驳回';
  276. $jindutitle = $remark.':'.$reason;
  277. }else{
  278. $update = [
  279. 'status' => 100, //用户验收通过
  280. 'updatetime' => $nowtime,
  281. 'finishtime' => $nowtime,
  282. ];
  283. $update_weixiu = [
  284. 'status' => 100, //用户验收通过
  285. 'updatetime' => $nowtime,
  286. 'audit_time' => $nowtime,
  287. ];
  288. $jindutitle = $remark = '验收已通过';
  289. }
  290. //最后一个轮回,追加验收进度
  291. $jindu = [
  292. 'order_id' => $info['id'],
  293. 'company_id' => $info['company_id'],
  294. 'user_id' => $info['user_id'],
  295. 'worker_id' => $info['worker_id'],
  296. 'weixiu_times' => $info['weixiu_times'],
  297. 'weixiu_id' => $info['weixiu_id'],
  298. 'title' => $jindutitle,
  299. 'createtime' => $nowtime,
  300. ];
  301. $jindu_id = Db::name('maintain_jindu')->insertGetId($jindu);
  302. if(!$jindu_id){
  303. Db::rollback();
  304. $this->error('验收失败');
  305. }
  306. $rs1 = Db::name('maintain')->where('id',$id)->update($update);
  307. if($rs1 === false){
  308. Db::rollback();
  309. $this->error('验收失败');
  310. }
  311. $rs2 = Db::name('maintain_weixiu')->where('id',$info['weixiu_id'])->update($update_weixiu);
  312. if($rs2 === false){
  313. Db::rollback();
  314. $this->error('验收失败');
  315. }
  316. Db::commit();
  317. $this->success($remark);
  318. }
  319. //评价
  320. public function evaluate(){
  321. $id = input('order_id',0);
  322. $eva_info = input('eva_info','');
  323. $eva_score = input('eva_score',5);
  324. //检查订单
  325. $map = [
  326. 'user_id' => $this->auth->id,
  327. 'id' => $id,
  328. ];
  329. $info = Db::name('maintain')->where($map)->find();
  330. if(empty($info)){
  331. $this->error('不存在的订单');
  332. }
  333. if($info['status'] != 100){
  334. $this->success('订单未验收通过,请刷新重试');
  335. }
  336. if($info['eva_time'] != 0){
  337. $this->success('订单已评价,无需重复评价');
  338. }
  339. //更新
  340. $update = [
  341. 'eva_info' => $eva_info,
  342. 'eva_score' => $eva_score,
  343. 'eva_time' => time(),
  344. ];
  345. $rs1 = Db::name('maintain')->where('id',$id)->update($update);
  346. //维保公司的平均分修改
  347. $this->success('评价完成');
  348. }
  349. }