Maintain.php 12 KB

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