StockConfig.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller\goods;
  4. use app\common\controller\Backend;
  5. use think\annotation\route\Group;
  6. use think\annotation\route\Route;
  7. use app\admin\traits\Actions;
  8. use think\facade\Db;
  9. use app\common\model\StockConfig as StockConfigModel;
  10. #[Group("goods/stock_config")]
  11. class StockConfig extends Backend
  12. {
  13. use Actions;
  14. protected function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new StockConfigModel();
  18. }
  19. /**
  20. * 添加
  21. */
  22. #[Route('GET,POST','add')]
  23. public function add()
  24. {
  25. if (false === $this->request->isPost()) {
  26. return $this->fetch();
  27. }
  28. $params = array_merge($this->request->post("row/a"),$this->postParams);
  29. if (empty($params)) {
  30. $this->error(__('提交的参数不能为空'));
  31. }
  32. if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
  33. $this->error(__('token错误,请刷新页面重试'));
  34. }
  35. if($this->model::where('field_name', $params['field_name'])->count() >0) $this->error(__('库存唯一属性已存在'));
  36. foreach ($params as &$value){
  37. if(is_array($value)){
  38. $value=implode(',',$value);
  39. }
  40. if($value===''){
  41. $value=null;
  42. }
  43. }
  44. $result = false;
  45. Db::startTrans();
  46. try {
  47. $result = $this->model->save($params);
  48. if($this->callback){
  49. $callback=$this->callback;
  50. $callback($this->model);
  51. }
  52. Db::commit();
  53. } catch (\Exception $e) {
  54. Db::rollback();
  55. $this->error($e->getMessage());
  56. }
  57. if ($result === false) {
  58. $this->error(__('没有新增任何数据'));
  59. }
  60. $this->success();
  61. }
  62. /**
  63. * 编辑
  64. */
  65. #[Route('GET,POST','edit')]
  66. public function edit(mixed $row=null)
  67. {
  68. $ids = $this->request->get('ids');
  69. if(!$row || is_array($row)){
  70. $row = $this->model->find($ids);
  71. }
  72. if (!$row) {
  73. $this->error(__('没有找到记录'));
  74. }
  75. if(count($this->volidateFields)>0){
  76. foreach ($this->volidateFields as $field=>$value){
  77. if($row[$field]!=$value){
  78. $this->error(__('没有操作权限'));
  79. }
  80. }
  81. }
  82. if (false === $this->request->isPost()) {
  83. $this->assign('row', $row);
  84. return $this->fetch();
  85. }
  86. $params = array_merge($this->request->post("row/a"),$this->postParams);
  87. if (empty($params)) {
  88. $this->error(__('提交的参数不能为空'));
  89. }
  90. if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
  91. $this->error(__('token错误,请刷新页面重试'));
  92. }
  93. if($this->model::where('field_name', $params['field_name'])->count() >1) $this->error(__('库存唯一属性已存在'));
  94. foreach ($params as &$value){
  95. if(is_array($value)){
  96. $value=implode(',',$value);
  97. }
  98. if($value===''){
  99. $value=null;
  100. }
  101. }
  102. $result = false;
  103. Db::startTrans();
  104. try {
  105. $result = $row->save($params);
  106. if($this->callback){
  107. $callback=$this->callback;
  108. $callback($row);
  109. }
  110. Db::commit();
  111. } catch (\Exception $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. }
  115. if (false === $result) {
  116. $this->error(__('没有数据被更新'));
  117. }
  118. $this->success();
  119. }
  120. }