CompanyBank.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 门店银行卡管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class CompanyBank extends Backend
  11. {
  12. /**
  13. * CompanyBank模型对象
  14. * @var \app\admin\model\CompanyBank
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\CompanyBank;
  21. }
  22. /**
  23. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  24. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  25. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  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. //只能看自己的
  43. $where_op = $this->whereop('company_bank.company_id');
  44. $list = $this->model
  45. ->with(['company'])
  46. ->where($where)
  47. ->where($where_op)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. foreach ($list as $row) {
  51. $row->getRelation('company')->visible(['name']);
  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. * @return string
  62. * @throws \think\Exception
  63. */
  64. public function add()
  65. {
  66. if (false === $this->request->isPost()) {
  67. return $this->view->fetch();
  68. }
  69. $params = $this->request->post('row/a');
  70. if (empty($params)) {
  71. $this->error(__('Parameter %s can not be empty', ''));
  72. }
  73. $params = $this->preExcludeFields($params);
  74. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  75. $params[$this->dataLimitField] = $this->auth->id;
  76. }
  77. $check = Db::name('company_bank')->where('company_id',$params['company_id'])->find();
  78. if($check){
  79. $this->error('该门店已经有一个银行卡');
  80. }
  81. $result = false;
  82. Db::startTrans();
  83. try {
  84. //是否采用模型验证
  85. if ($this->modelValidate) {
  86. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  87. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  88. $this->model->validateFailException()->validate($validate);
  89. }
  90. $result = $this->model->allowField(true)->save($params);
  91. Db::commit();
  92. } catch (ValidateException|PDOException|Exception $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. }
  96. if ($result === false) {
  97. $this->error(__('No rows were inserted'));
  98. }
  99. $this->success();
  100. }
  101. /**
  102. * 编辑
  103. *
  104. * @param $ids
  105. * @return string
  106. * @throws DbException
  107. * @throws \think\Exception
  108. */
  109. public function edit($ids = null)
  110. {
  111. $row = $this->model->get($ids);
  112. if (!$row) {
  113. $this->error(__('No Results were found'));
  114. }
  115. $adminIds = $this->getDataLimitAdminIds();
  116. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  117. $this->error(__('You have no permission'));
  118. }
  119. if (false === $this->request->isPost()) {
  120. $this->view->assign('row', $row);
  121. return $this->view->fetch();
  122. }
  123. $params = $this->request->post('row/a');
  124. if (empty($params)) {
  125. $this->error(__('Parameter %s can not be empty', ''));
  126. }
  127. $params = $this->preExcludeFields($params);
  128. $result = false;
  129. $check = Db::name('company_bank')->where('company_id',$params['company_id'])->where('id','neq',$ids)->find();
  130. if($check){
  131. $this->error('该门店已经有一个银行卡');
  132. }
  133. Db::startTrans();
  134. try {
  135. //是否采用模型验证
  136. if ($this->modelValidate) {
  137. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  138. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  139. $row->validateFailException()->validate($validate);
  140. }
  141. $result = $row->allowField(true)->save($params);
  142. Db::commit();
  143. } catch (ValidateException|PDOException|Exception $e) {
  144. Db::rollback();
  145. $this->error($e->getMessage());
  146. }
  147. if (false === $result) {
  148. $this->error(__('No rows were updated'));
  149. }
  150. $this->success();
  151. }
  152. }