Maintain.php 11 KB

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