CompanyBank.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 = [];
  44. if($this->auth->company_id){
  45. $where_op['company_bank.company_id'] = $this->auth->company_id;
  46. }
  47. $list = $this->model
  48. ->with(['company'])
  49. ->where($where)
  50. ->where($where_op)
  51. ->order($sort, $order)
  52. ->paginate($limit);
  53. foreach ($list as $row) {
  54. $row->getRelation('company')->visible(['name']);
  55. }
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 添加
  63. *
  64. * @return string
  65. * @throws \think\Exception
  66. */
  67. public function add()
  68. {
  69. if (false === $this->request->isPost()) {
  70. return $this->view->fetch();
  71. }
  72. $params = $this->request->post('row/a');
  73. if (empty($params)) {
  74. $this->error(__('Parameter %s can not be empty', ''));
  75. }
  76. $params = $this->preExcludeFields($params);
  77. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  78. $params[$this->dataLimitField] = $this->auth->id;
  79. }
  80. $check = Db::name('company_bank')->where('company_id',$params['company_id'])->find();
  81. if($check){
  82. $this->error('该门店已经有一个银行卡');
  83. }
  84. $result = false;
  85. Db::startTrans();
  86. try {
  87. //是否采用模型验证
  88. if ($this->modelValidate) {
  89. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  90. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  91. $this->model->validateFailException()->validate($validate);
  92. }
  93. $result = $this->model->allowField(true)->save($params);
  94. Db::commit();
  95. } catch (ValidateException|PDOException|Exception $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. }
  99. if ($result === false) {
  100. $this->error(__('No rows were inserted'));
  101. }
  102. $this->success();
  103. }
  104. /**
  105. * 编辑
  106. *
  107. * @param $ids
  108. * @return string
  109. * @throws DbException
  110. * @throws \think\Exception
  111. */
  112. public function edit($ids = null)
  113. {
  114. $row = $this->model->get($ids);
  115. if (!$row) {
  116. $this->error(__('No Results were found'));
  117. }
  118. $adminIds = $this->getDataLimitAdminIds();
  119. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  120. $this->error(__('You have no permission'));
  121. }
  122. if (false === $this->request->isPost()) {
  123. $this->view->assign('row', $row);
  124. return $this->view->fetch();
  125. }
  126. $params = $this->request->post('row/a');
  127. if (empty($params)) {
  128. $this->error(__('Parameter %s can not be empty', ''));
  129. }
  130. $params = $this->preExcludeFields($params);
  131. $result = false;
  132. $check = Db::name('company_bank')->where('company_id',$params['company_id'])->where('id','neq',$ids)->find();
  133. if($check){
  134. $this->error('该门店已经有一个银行卡');
  135. }
  136. Db::startTrans();
  137. try {
  138. //是否采用模型验证
  139. if ($this->modelValidate) {
  140. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  141. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  142. $row->validateFailException()->validate($validate);
  143. }
  144. $result = $row->allowField(true)->save($params);
  145. Db::commit();
  146. } catch (ValidateException|PDOException|Exception $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. }
  150. if (false === $result) {
  151. $this->error(__('No rows were updated'));
  152. }
  153. $this->success();
  154. }
  155. }