Maintain.php 9.5 KB

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