Pricerange.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\admin\controller\goods;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 价格区间管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Pricerange extends Backend
  13. {
  14. /**
  15. * Pricerange模型对象
  16. * @var \app\admin\model\goods\Pricerange
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\goods\Pricerange;
  23. }
  24. public function import()
  25. {
  26. parent::import();
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. public function add()
  34. {
  35. if ($this->request->isPost()) {
  36. $params = $this->request->post("row/a");
  37. if ($params) {
  38. $params = $this->preExcludeFields($params);
  39. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  40. $params[$this->dataLimitField] = $this->auth->id;
  41. }
  42. // 检查区间是否已存在
  43. $params['range'] = trim($params['range']);
  44. if (\app\admin\model\goods\Pricerange::where('range', $params['range'])->value('id')) {
  45. $this->error('相同区间已存在');
  46. }
  47. $result = false;
  48. Db::startTrans();
  49. try {
  50. //是否采用模型验证
  51. if ($this->modelValidate) {
  52. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  53. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  54. $this->model->validateFailException(true)->validate($validate);
  55. }
  56. $result = $this->model->allowField(true)->save($params);
  57. Db::commit();
  58. } catch (ValidateException $e) {
  59. Db::rollback();
  60. $this->error($e->getMessage());
  61. } catch (PDOException $e) {
  62. Db::rollback();
  63. $this->error($e->getMessage());
  64. } catch (Exception $e) {
  65. Db::rollback();
  66. $this->error($e->getMessage());
  67. }
  68. if ($result !== false) {
  69. $this->success();
  70. } else {
  71. $this->error(__('No rows were inserted'));
  72. }
  73. }
  74. $this->error(__('Parameter %s can not be empty', ''));
  75. }
  76. return $this->view->fetch();
  77. }
  78. public function edit($ids = null)
  79. {
  80. $row = $this->model->get($ids);
  81. if (!$row) {
  82. $this->error(__('No Results were found'));
  83. }
  84. $adminIds = $this->getDataLimitAdminIds();
  85. if (is_array($adminIds)) {
  86. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  87. $this->error(__('You have no permission'));
  88. }
  89. }
  90. if ($this->request->isPost()) {
  91. $params = $this->request->post("row/a");
  92. if ($params) {
  93. $params = $this->preExcludeFields($params);
  94. // 检查区间是否已存在
  95. $params['range'] = trim($params['range']);
  96. $exist = \app\admin\model\goods\Pricerange::where('range', $params['range'])->value('id');
  97. if ($exist && $exist != $ids) {
  98. $this->error('相同区间已存在');
  99. }
  100. $result = false;
  101. Db::startTrans();
  102. try {
  103. //是否采用模型验证
  104. if ($this->modelValidate) {
  105. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  106. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  107. $row->validateFailException(true)->validate($validate);
  108. }
  109. $result = $row->allowField(true)->save($params);
  110. Db::commit();
  111. } catch (ValidateException $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. } catch (PDOException $e) {
  115. Db::rollback();
  116. $this->error($e->getMessage());
  117. } catch (Exception $e) {
  118. Db::rollback();
  119. $this->error($e->getMessage());
  120. }
  121. if ($result !== false) {
  122. $this->success();
  123. } else {
  124. $this->error(__('No rows were updated'));
  125. }
  126. }
  127. $this->error(__('Parameter %s can not be empty', ''));
  128. }
  129. $this->view->assign("row", $row);
  130. return $this->view->fetch();
  131. }
  132. }