Repair.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\admin\controller\equipment;
  3. use app\admin\model\equipment\Equipment;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\Exception;
  7. /**
  8. * 设备维修管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Repair extends Backend
  13. {
  14. /**
  15. * Repair模型对象
  16. * @var \app\admin\model\equipment\Repair
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\equipment\Repair;
  23. $this->view->assign('statusList', $this->model->getStatusList());
  24. }
  25. public function import()
  26. {
  27. parent::import();
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. $this->relationSearch = true;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. // 获取链接地址参数
  43. $param = $this->request->param();
  44. $archiveId = isset($param['archive_id']) ? $param['archive_id'] : '';
  45. $equipmentId = isset($param['equipment_id']) ? $param['equipment_id'] : '';
  46. if ($this->request->isAjax()) {
  47. //如果发送的来源是Selectpage,则转发到Selectpage
  48. if ($this->request->request('keyField')) {
  49. return $this->selectpage();
  50. }
  51. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  52. $andWhere = [];
  53. if (!empty($archiveId)) {
  54. $equipmentIds = Equipment::where('archive_id', $archiveId)->column('id');
  55. $andWhere['equipment_id'] = ['in', $equipmentIds];
  56. }
  57. if (!empty($equipmentId)) {
  58. $andWhere['equipment_id'] = $equipmentId;
  59. }
  60. $list = $this->model
  61. ->with(['archive', 'equipment', 'registerUser', 'repairUser', 'failureCause'])
  62. ->where($where)
  63. ->where($andWhere)
  64. ->order($sort, $order)
  65. ->paginate($limit);
  66. $result = array("total" => $list->total(), "rows" => $list->items());
  67. return json($result);
  68. }
  69. $this->view->assign('tabsList', $this->model->getTabsList());
  70. return $this->view->fetch();
  71. }
  72. /**
  73. * 维修登记
  74. */
  75. public function register($id = null)
  76. {
  77. $row = $this->model->get($id);
  78. if (!$row) {
  79. $this->error(__('No Results were found'));
  80. }
  81. if ($this->request->isPost()) {
  82. $this->token();
  83. $params = $this->request->post("row/a");
  84. if ($params) {
  85. $repairtime = time();
  86. $params['id'] = $id;
  87. $params['repair_uid'] = $this->auth->id;
  88. $params['repairtime'] = $repairtime;
  89. $params['consuming'] = $repairtime - $row['registertime'];
  90. Db::startTrans();
  91. try {
  92. $result = $this->model->isUpdate(true)->save($params);
  93. if (!$result) {
  94. throw new Exception($this->model->getError());
  95. }
  96. $equipmentId = $this->model->where('id', $id)->value('equipment_id');
  97. $workStatus = $params['status'] == 'scrapped' ? 'scrapped' : 'normal';
  98. $equipmentModel = new Equipment();
  99. $equipmentResult = $equipmentModel->isUpdate(true)->update(['id' => $equipmentId, 'work_status' => $workStatus]);
  100. if (!$equipmentResult) {
  101. throw new Exception($equipmentModel->getError());
  102. }
  103. Db::commit();
  104. $this->success();
  105. } catch (Exception $exception) {
  106. Db::rollback();
  107. $this->error($exception->getMessage());
  108. }
  109. }
  110. $this->error(__('Parameter %s can not be empty', ''));
  111. }
  112. $failureCauseModel = new \app\admin\model\equipment\FailureCause();
  113. $failureCauseList = $failureCauseModel->getSelectpicker();
  114. $statusList = $this->model->getStatusList();
  115. unset($statusList['pending'], $statusList['registered']);
  116. $this->view->assign('statusList', build_select('row[status]', $statusList, '', ['id' => 'c-status', 'class' => 'form-control selectpicker']));
  117. $this->view->assign('failureCauseList', build_select('row[failure_cause_id]', $failureCauseList, '', ['id' => 'c-failure-cause', 'class' => 'form-control selectpicker']));
  118. return $this->view->fetch();
  119. }
  120. /**
  121. * 详情
  122. */
  123. public function detail($id = null)
  124. {
  125. if ($this->request->isPost()) {
  126. $this->token();
  127. }
  128. $row = $this->model->with(['archive' => ['responsibleUser'], 'equipment', 'registerUser', 'repairUser', 'failureCause'])->find($id);
  129. if (!$row) {
  130. $this->error(__('No Results were found'));
  131. }
  132. $this->view->assign('row', $row);
  133. return $this->view->fetch();
  134. }
  135. }