Maintain.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /**
  10. * 维修单
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Maintain extends Backend
  15. {
  16. /**
  17. * Maintain模型对象
  18. * @var \app\admin\model\Maintain
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Maintain;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. //当前是否为关联查询
  33. $this->relationSearch = true;
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags', 'trim']);
  36. if ($this->request->isAjax()) {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $list = $this->model
  43. ->with(['company','user','usercompany','worker'])
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->paginate($limit);
  47. foreach ($list as $row) {
  48. $row->getRelation('company')->visible(['companyname']);
  49. $row->getRelation('user')->visible(['nickname','mobile']);
  50. $row->getRelation('usercompany')->visible(['projectname']);
  51. $row->getRelation('worker')->visible(['truename','mobile']);
  52. }
  53. $result = array("total" => $list->total(), "rows" => $list->items());
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 编辑
  60. *
  61. * @param $ids
  62. * @return string
  63. * @throws DbException
  64. * @throws \think\Exception
  65. */
  66. public function edit($ids = null)
  67. {
  68. $row = $this->model->get($ids);
  69. if (!$row) {
  70. $this->error(__('No Results were found'));
  71. }
  72. $adminIds = $this->getDataLimitAdminIds();
  73. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  74. $this->error(__('You have no permission'));
  75. }
  76. if (false === $this->request->isPost()) {
  77. //
  78. $row['filedata_array'] = json_decode($row['filedata'],true);
  79. //
  80. $this->view->assign('row', $row);
  81. return $this->view->fetch();
  82. }
  83. $params = $this->request->post('row/a');
  84. if (empty($params)) {
  85. $this->error(__('Parameter %s can not be empty', ''));
  86. }
  87. $params = $this->preExcludeFields($params);
  88. $result = false;
  89. Db::startTrans();
  90. try {
  91. //是否采用模型验证
  92. if ($this->modelValidate) {
  93. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  94. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  95. $row->validateFailException()->validate($validate);
  96. }
  97. $result = $row->allowField(true)->save($params);
  98. Db::commit();
  99. } catch (ValidateException|PDOException|Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. if (false === $result) {
  104. $this->error(__('No rows were updated'));
  105. }
  106. $this->success();
  107. }
  108. /**
  109. * 总部指派
  110. * 复制pc端的指派,稍作调整
  111. */
  112. public function zhipai()
  113. {
  114. $id = input('id',0);
  115. $row = Db::name('maintain')->where('id',$id)->find();
  116. if(!$this->request->isPost()){
  117. $this->view->assign('row', $row);
  118. return $this->view->fetch();
  119. }
  120. //检查师傅
  121. $worker_id = input('worker_id',0);
  122. $worker = Db::name('worker')->where('id',$worker_id)->where('status',1)->where('company_id',1)->find();
  123. if(empty($worker)){
  124. $this->error('没找到该师傅');
  125. }
  126. //检查订单
  127. $id = input('id',0);
  128. Db::startTrans();
  129. $info = Db::name('maintain')->where('id',$id)->lock(true)->find();
  130. if(empty($info)){
  131. Db::rollback();
  132. $this->error('没找到该信息,请刷新重试');
  133. }
  134. if(!in_array($info['status'],[0,20,22,30,40])){
  135. Db::rollback();
  136. $this->error('当前订单状态,不能指派师傅');
  137. }
  138. if(!empty($info['worker_id'])){
  139. Db::rollback();
  140. $this->error('已经指派了师傅');
  141. }
  142. //
  143. $update = [
  144. 'worker_id' => $worker_id,
  145. 'status' => 50,
  146. 'updatetime' => time(),
  147. ];
  148. $rs_update = Db::name('maintain')->where('id',$id)->update($update);
  149. if($rs_update === false){
  150. Db::rollback();
  151. $this->error('指派失败,请重试');
  152. }
  153. //
  154. Db::commit();
  155. $this->success();
  156. }
  157. }