Maintain.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\DbException;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use Exception;
  9. use app\common\model\Maintain as Maintainmodel;
  10. /**
  11. * 维修单
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Maintain extends Backend
  16. {
  17. protected $noNeedLogin = ['showinfo'];
  18. protected $noNeedRight = ['showinfo'];
  19. /**
  20. * Maintain模型对象
  21. * @var \app\admin\model\Maintain
  22. */
  23. protected $model = null;
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new \app\admin\model\Maintain;
  28. $this->view->assign("statusList", $this->model->getStatusList());
  29. }
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model
  46. ->with(['company','user','usercompany','worker'])
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. foreach ($list as $row) {
  51. $row->getRelation('company')->visible(['companyname']);
  52. $row->getRelation('user')->visible(['nickname','mobile']);
  53. $row->getRelation('usercompany')->visible(['projectname']);
  54. $row->getRelation('worker')->visible(['truename','mobile']);
  55. }
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 编辑
  63. *
  64. * @param $ids
  65. * @return string
  66. * @throws DbException
  67. * @throws \think\Exception
  68. */
  69. public function edit($ids = null)
  70. {
  71. $row = $this->model->get($ids);
  72. if (!$row) {
  73. $this->error(__('No Results were found'));
  74. }
  75. $adminIds = $this->getDataLimitAdminIds();
  76. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  77. $this->error(__('You have no permission'));
  78. }
  79. if (false === $this->request->isPost()) {
  80. //
  81. $row['filedata_array'] = json_decode($row['filedata'],true);
  82. //
  83. $this->view->assign('row', $row);
  84. return $this->view->fetch();
  85. }
  86. $params = $this->request->post('row/a');
  87. if (empty($params)) {
  88. $this->error(__('Parameter %s can not be empty', ''));
  89. }
  90. $params = $this->preExcludeFields($params);
  91. $result = false;
  92. Db::startTrans();
  93. try {
  94. //是否采用模型验证
  95. if ($this->modelValidate) {
  96. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  97. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  98. $row->validateFailException()->validate($validate);
  99. }
  100. $result = $row->allowField(true)->save($params);
  101. Db::commit();
  102. } catch (ValidateException|PDOException|Exception $e) {
  103. Db::rollback();
  104. $this->error($e->getMessage());
  105. }
  106. if (false === $result) {
  107. $this->error(__('No rows were updated'));
  108. }
  109. $this->success();
  110. }
  111. /**
  112. * 总部指派
  113. * 复制pc端的指派,稍作调整
  114. */
  115. public function zhipai()
  116. {
  117. $id = input('id',0);
  118. $row = Db::name('maintain')->where('id',$id)->find();
  119. if(!$this->request->isPost()){
  120. $this->view->assign('row', $row);
  121. return $this->view->fetch();
  122. }
  123. //检查师傅
  124. $worker_id = input('worker_id',0);
  125. $worker = Db::name('worker')->where('id',$worker_id)->where('status',1)->where('company_id',1)->find();
  126. if(empty($worker)){
  127. $this->error('没找到该师傅');
  128. }
  129. //检查订单
  130. $id = input('id',0);
  131. Db::startTrans();
  132. $info = Db::name('maintain')->where('id',$id)->lock(true)->find();
  133. if(empty($info)){
  134. Db::rollback();
  135. $this->error('没找到该信息,请刷新重试');
  136. }
  137. if(!in_array($info['status'],[0,20,22,30,40])){
  138. Db::rollback();
  139. $this->error('当前订单状态,不能指派师傅');
  140. }
  141. if(!empty($info['worker_id'])){
  142. Db::rollback();
  143. $this->error('已经指派了师傅');
  144. }
  145. //
  146. $update = [
  147. 'worker_id' => $worker_id,
  148. 'status' => 50,
  149. 'updatetime' => time(),
  150. ];
  151. $rs_update = Db::name('maintain')->where('id',$id)->update($update);
  152. if($rs_update === false){
  153. Db::rollback();
  154. $this->error('指派失败,请重试');
  155. }
  156. //
  157. Db::commit();
  158. $this->success();
  159. }
  160. /**
  161. * 查看导出
  162. */
  163. public function showinfo(){
  164. $id = input('id',0);
  165. $info = Db::name('maintain')->alias('mt')
  166. ->join('user_company uc','mt.uc_id = uc.id','LEFT')
  167. ->join('user','mt.user_id = user.id','LEFT')
  168. ->join('worker','mt.worker_id = worker.id','LEFT')
  169. ->join('company','mt.company_id = company.id','LEFT')
  170. ->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,company.companyname')
  171. ->where('mt.id',$id)
  172. ->find();
  173. $maintain_model = new Maintainmodel();
  174. $info['status_text'] = $maintain_model->status_data($info['status']);
  175. //pc用户,已流转订单,直接显示流转中
  176. if($info['company_id'] != 1 && in_array($info['status'],[0,20,22,30,40]) && $info['liuzhuan_status'] == 1){
  177. $info['status_text'] = '流转中';
  178. }
  179. $info['status_colorType'] = $maintain_model->status_colorType($info['status']);
  180. //假状态
  181. $info['fake_status'] = $this->fake_status($info['status']);
  182. $info['fake_status_text'] = $this->fake_status_data($info['status']);
  183. $info['filedata'] = json_decode($info['filedata'],true);
  184. //追加报价历史
  185. $baojia = Db::name('maintain_baojia')->alias('bj')
  186. ->field('bj.*,admin.nickname as baojia_nickname,audit.nickname as baojia_audit_nickname')
  187. ->join('pc_admin admin','bj.baojia_staffid = admin.id','LEFT')
  188. ->join('pc_admin audit','bj.baojia_audit_staffid = audit.id','LEFT')
  189. ->where('order_id',$id)->order('id desc')->select();
  190. if(!empty($baojia)){
  191. foreach($baojia as $key => $val){
  192. $baojia[$key]['status_text'] = $maintain_model->baojia_status_data($val['status']);
  193. }
  194. }
  195. $info['baojia_list'] = $baojia;
  196. //追加材料列表
  197. $info['cailiao_list'] = Db::name('maintain_cailiao')->field('id,name,number,danwei,images')->where('order_id',$id)->order('id desc')->select();
  198. //追加多次维修+对应进度历史
  199. $last_jindulist = [];
  200. $new_jindulist = [];
  201. $jindu_list = Db::name('maintain_jindu')->alias('jd')
  202. ->join('worker','jd.worker_id = worker.id','LEFT')
  203. ->field('jd.id,jd.worker_id,jd.weixiu_times,jd.title,jd.images,jd.createtime,worker.avatar,worker.truename')
  204. ->where('jd.order_id',$id)
  205. ->order('jd.id desc')->select();
  206. if(!empty($jindu_list)){
  207. for($i=$jindu_list[0]['weixiu_times'];$i>=1;$i--){
  208. foreach($jindu_list as $key => $val){
  209. if($i == $val['weixiu_times']){
  210. $new_jindulist[$i][] = $val;
  211. }
  212. }
  213. }
  214. foreach($new_jindulist as $key => $val){
  215. $last_jindulist[] = [
  216. 'title' => '第'.$val[0]['weixiu_times'].'次',
  217. 'child' => $val
  218. ];
  219. }
  220. }
  221. $info['jindu'] = $last_jindulist;
  222. $this->view->assign('info', $info);
  223. // dump($info);exit;
  224. $this->view->engine->layout(false);
  225. return $this->view->fetch();
  226. }
  227. //整合过的状态
  228. private function fake_status_data($status){
  229. $data = [
  230. 0 => '待报价',
  231. 2 => '已取消',
  232. 20 => '评估报价',
  233. 22 => '评估报价',
  234. 30 => '用户待确认',
  235. 40 => '待处理',
  236. 50 => '待处理',
  237. 60 => '待处理',
  238. 70 => '待处理',
  239. 80 => '待处理',
  240. 90 => '待处理',
  241. 92 => '待处理',
  242. 100 => '已完成',
  243. ];
  244. return isset($data[$status]) ? $data[$status] : $status;
  245. }
  246. //整合过的状态
  247. private function fake_status($status){
  248. $data = [
  249. 0 => 0,
  250. 2 => 2,
  251. 20 => 20,
  252. 22 => 20,
  253. 30 => 30,
  254. 40 => 40,
  255. 50 => 40,
  256. 60 => 40,
  257. 70 => 40,
  258. 80 => 40,
  259. 90 => 40,
  260. 92 => 40,
  261. 100 => 100,
  262. ];
  263. return isset($data[$status]) ? $data[$status] : $status;
  264. }
  265. }