Maintain.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. namespace app\api\controller\worker;
  3. use app\common\controller\Apiw;
  4. use app\common\model\Maintain as Maintainmodel;
  5. use think\Db;
  6. /**
  7. * 保修
  8. */
  9. class Maintain extends Apiw
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. //订单列表
  14. public function lists(){
  15. //待处理,50,60
  16. //进行中,70,80,92
  17. //待审核,90
  18. //已完成,100
  19. $status = input('status',0);
  20. $map = [
  21. 'm.worker_id' => $this->auth->id,
  22. 'm.status' => $status,
  23. ];
  24. if($status == 50){
  25. $map['m.status'] = ['IN',[50,60]];
  26. }
  27. if($status == 80){
  28. $map['m.status'] = ['IN',[70,80,92]];
  29. }
  30. $field = [
  31. 'm.id','m.orderno','m.info','m.filedata','m.mobile','m.address','m.status','m.cailiao_time','m.lingqu_time','m.shangmen_time','m.wancheng_time',
  32. 'user.contactname',
  33. 'uc.projectname'
  34. ];
  35. $list = Db::name('maintain')->alias('m')->field($field)
  36. ->join('user','m.user_id = user.id','LEFT')
  37. ->join('user_company uc','m.user_id = uc.user_id','LEFT')
  38. ->where($map)->order('m.id desc')
  39. ->autopage()->select();
  40. if(!empty($list)){
  41. $maintain_model = new Maintainmodel();
  42. foreach($list as $key => $val){
  43. $list[$key]['status_text'] = $maintain_model->status_data($val['status']);
  44. $list[$key]['filedata'] = !empty($val['filedata']) ? json_decode($val['filedata'],true) : [];
  45. }
  46. }
  47. $this->success(1,$list);
  48. }
  49. //材料清单
  50. public function cailiao_list(){
  51. $id = input('order_id',0);
  52. $result = [];
  53. $list = Db::name('maintain_cailiao')->field('id,order_id,name,number,danwei,images')->where('order_id',$id)->order('id desc')->select();
  54. if(!empty($list)){
  55. $result = list_domain_image($list,['images']);
  56. }
  57. $this->success(1, $result);
  58. }
  59. //进度列表
  60. public function jindu_list(){
  61. $id = input('order_id',0);
  62. $map = [
  63. 'worker_id' => $this->auth->id,
  64. 'id' => $id,
  65. ];
  66. $info = Db::name('maintain')
  67. ->where($map)
  68. ->find();
  69. //追加进度
  70. //追加多次维修+对应进度历史
  71. $last_jindulist = [];
  72. $new_jindulist = [];
  73. $jindu_list = Db::name('maintain_jindu')->alias('jd')
  74. ->join('worker','jd.worker_id = worker.id','LEFT')
  75. ->field('jd.id,jd.worker_id,jd.weixiu_times,jd.title,jd.images,jd.createtime,worker.avatar,worker.truename')
  76. ->where('jd.order_id',$id)
  77. ->order('jd.id desc')->select();
  78. $jindu_list = list_domain_image($jindu_list,['avatar','images']);
  79. if(!empty($jindu_list)){
  80. for($i=$jindu_list[0]['weixiu_times'];$i>=1;$i--){
  81. foreach($jindu_list as $key => $val){
  82. if($i == $val['weixiu_times']){
  83. $new_jindulist[$i][] = $val;
  84. }
  85. }
  86. }
  87. foreach($new_jindulist as $key => $val){
  88. $last_jindulist[] = [
  89. 'title' => '第'.$val[0]['weixiu_times'].'次',
  90. 'child' => $val
  91. ];
  92. }
  93. }
  94. $this->success(1, $last_jindulist);
  95. }
  96. //申请材料
  97. public function shenqingcailiao(){
  98. $order_id = input('order_id',0);
  99. $data = input('data','','htmlspecialchars_decode');
  100. $data = json_decode($data,true);
  101. if(empty($data)){
  102. $this->error();
  103. }
  104. Db::startTrans();
  105. //订单
  106. $map = [
  107. 'worker_id' => $this->auth->id,
  108. 'id' => $order_id,
  109. ];
  110. $info = Db::name('maintain')->where($map)->lock(true)->find();
  111. if(empty($info)){
  112. Db::rollback();
  113. $this->error('不存在的订单');
  114. }
  115. if($info['status'] != 50){
  116. Db::rollback();
  117. $this->error('状态不正确,请刷新重试');
  118. }
  119. $nowtime = time();
  120. //准备数据
  121. foreach($data as $key => $val){
  122. $val['order_id'] = $info['id'];
  123. $val['company_id'] = $info['company_id'];
  124. $val['user_id'] = $info['user_id'];
  125. $val['worker_id'] = $info['worker_id'];
  126. $val['createtime'] = $nowtime;
  127. $data[$key] = $val;
  128. }
  129. $id = Db::name('maintain_cailiao')->insertAll($data);
  130. if(!$id){
  131. Db::rollback();
  132. $this->error('申请失败');
  133. }
  134. $update = [
  135. 'status' => 60,
  136. 'updatetime' => $nowtime,
  137. 'cailiao_time' => $nowtime, //材料申请时间
  138. ];
  139. $rs2 = Db::name('maintain')->where('id',$order_id)->update($update);
  140. if($rs2 === false){
  141. Db::rollback();
  142. $this->error('申请失败');
  143. }
  144. Db::commit();
  145. $this->success('申请成功');
  146. }
  147. //材料计量单位
  148. public function cailiao_danwei(){
  149. $data = [
  150. '个',
  151. '把',
  152. '只',
  153. ];
  154. $this->success(1,$data);
  155. }
  156. //领取材料
  157. public function lingqucailiao(){
  158. $order_id = input('order_id',0);
  159. $images = input('images','','trim');
  160. if(empty($images)){
  161. $this->error();
  162. }
  163. //订单
  164. $map = [
  165. 'worker_id' => $this->auth->id,
  166. 'id' => $order_id,
  167. ];
  168. $info = Db::name('maintain')->where($map)->find();
  169. if(empty($info)){
  170. $this->error('不存在的订单');
  171. }
  172. if($info['status'] != 60){
  173. $this->error('状态不正确,请刷新重试');
  174. }
  175. $nowtime = time();
  176. //
  177. $update = [
  178. 'status' => 70,
  179. 'updatetime' => $nowtime,
  180. 'lingqu_time' => $nowtime, //材料申请时间
  181. 'lingqu_images' => $images,
  182. ];
  183. $rs2 = Db::name('maintain')->where('id',$order_id)->update($update);
  184. if($rs2 === false){
  185. $this->error('领取失败');
  186. }
  187. $this->success('领取成功');
  188. }
  189. //////////////////////////////循环开始///////////////////////
  190. //立即上门
  191. public function shangmen(){
  192. $order_id = input('order_id',0);
  193. Db::startTrans();
  194. //订单
  195. $map = [
  196. 'worker_id' => $this->auth->id,
  197. 'id' => $order_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(!in_array($info['status'],[70,92])){
  205. Db::rollback();
  206. $this->error('状态不正确,请刷新重试');
  207. }
  208. $nowtime = time();
  209. //开启一个新轮回
  210. $weixiu = [
  211. 'order_id' => $info['id'],
  212. 'company_id' => $info['company_id'],
  213. 'user_id' => $info['user_id'],
  214. 'worker_id' => $info['worker_id'],
  215. 'weixiu_times' => $info['weixiu_times'] + 1, //次数自增1
  216. 'shangmen_time' => $nowtime,
  217. 'status' => 80,
  218. 'updatetime' => $nowtime,
  219. ];
  220. $weixiu_id = Db::name('maintain_weixiu')->insertGetId($weixiu);
  221. if(!$weixiu_id){
  222. Db::rollback();
  223. $this->error('操作失败,重试一下吧');
  224. }
  225. //新轮回第一个进度
  226. $jindu = [
  227. 'order_id' => $info['id'],
  228. 'company_id' => $info['company_id'],
  229. 'user_id' => $info['user_id'],
  230. 'worker_id' => $info['worker_id'],
  231. 'weixiu_times' => $weixiu['weixiu_times'],
  232. 'weixiu_id' => $weixiu_id,
  233. 'title' => '已上门',
  234. 'createtime' => $nowtime,
  235. ];
  236. $jindu_id = Db::name('maintain_jindu')->insertGetId($jindu);
  237. if(!$jindu_id){
  238. Db::rollback();
  239. $this->error('操作失败,重试一下吧');
  240. }
  241. //修改订单
  242. $update = [
  243. 'status' => 80,
  244. 'updatetime' => $nowtime,
  245. 'shangmen_time' => $nowtime, //上门时间
  246. 'weixiu_times' => $weixiu['weixiu_times'],
  247. 'weixiu_id' => $weixiu_id,
  248. 'wancheng_time' => 0, //清空完成时间
  249. ];
  250. $rs2 = Db::name('maintain')->where('id',$order_id)->update($update);
  251. if($rs2 === false){
  252. Db::rollback();
  253. $this->error('操作失败,重试一下吧');
  254. }
  255. Db::commit();
  256. $this->success('已上门');
  257. }
  258. //新增维修进度
  259. public function jindu_add(){
  260. $order_id = input('order_id',0);
  261. $title = input('title','');
  262. $images = input('images','');
  263. if(empty($title)){
  264. $this->error();
  265. }
  266. //订单
  267. $map = [
  268. 'worker_id' => $this->auth->id,
  269. 'id' => $order_id,
  270. ];
  271. $info = Db::name('maintain')->where($map)->find();
  272. if(empty($info)){
  273. $this->error('不存在的订单');
  274. }
  275. if($info['status'] != 80){
  276. $this->error('状态不正确,请刷新重试');
  277. }
  278. $nowtime = time();
  279. //准备数据
  280. $jindu = [
  281. 'order_id' => $info['id'],
  282. 'company_id' => $info['company_id'],
  283. 'user_id' => $info['user_id'],
  284. 'worker_id' => $info['worker_id'],
  285. 'weixiu_times' => $info['weixiu_times'],
  286. 'weixiu_id' => $info['weixiu_id'],
  287. 'title' => $title,
  288. 'images' => $images,
  289. 'createtime' => $nowtime,
  290. ];
  291. $id = Db::name('maintain_jindu')->insertGetId($jindu);
  292. if(!$id){
  293. $this->error('记录失败');
  294. }
  295. $this->success('记录成功');
  296. }
  297. //确认完成
  298. public function wancheng(){
  299. $order_id = input('order_id',0);
  300. Db::startTrans();
  301. //订单
  302. $map = [
  303. 'worker_id' => $this->auth->id,
  304. 'id' => $order_id,
  305. ];
  306. $info = Db::name('maintain')->where($map)->lock(true)->find();
  307. if(empty($info)){
  308. Db::rollback();
  309. $this->error('不存在的订单');
  310. }
  311. if($info['status'] != 80){
  312. Db::rollback();
  313. $this->error('状态不正确,请刷新重试');
  314. }
  315. $nowtime = time();
  316. //结束最后一个轮回
  317. $weixiu = [
  318. 'status' => 90, //待验收
  319. 'wancheng_time' => $nowtime,
  320. 'updatetime' => $nowtime,
  321. ];
  322. $weixiu_rs = Db::name('maintain_weixiu')->where('id',$info['weixiu_id'])->update($weixiu);
  323. if($weixiu_rs === false){
  324. Db::rollback();
  325. $this->error('操作失败,重试一下吧');
  326. }
  327. //最后一个轮回,追加完成进度
  328. $jindu = [
  329. 'order_id' => $info['id'],
  330. 'company_id' => $info['company_id'],
  331. 'user_id' => $info['user_id'],
  332. 'worker_id' => $info['worker_id'],
  333. 'weixiu_times' => $info['weixiu_times'],
  334. 'weixiu_id' => $info['weixiu_id'],
  335. 'title' => '已完成',
  336. 'createtime' => $nowtime,
  337. ];
  338. $jindu_id = Db::name('maintain_jindu')->insertGetId($jindu);
  339. if(!$jindu_id){
  340. Db::rollback();
  341. $this->error('操作失败,重试一下吧');
  342. }
  343. //修改订单
  344. $update = [
  345. 'status' => 90,
  346. 'wancheng_time' => $nowtime,
  347. 'updatetime' => $nowtime,
  348. ];
  349. $rs2 = Db::name('maintain')->where('id',$order_id)->update($update);
  350. if($rs2 === false){
  351. Db::rollback();
  352. $this->error('操作失败,重试一下吧');
  353. }
  354. Db::commit();
  355. $this->success('操作成功');
  356. }
  357. //////////////////////////////循环结束///////////////////////
  358. }