Maintain.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. }