Lists.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\admin\controller\product;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use app\common\model\ProductOrder;
  7. use app\common\model\ProductArea;
  8. use think\exception\DbException;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. /**
  12. * 商品列管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Lists extends Backend
  17. {
  18. protected $multiFields = 'status,is_transfer,is_gift,is_freight,is_teac' ;
  19. protected $model = null;
  20. protected $productArea = null;
  21. protected $areaCode = null;
  22. protected $relationSearch = true;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->productArea = new ProductArea;
  27. $this->model = new \app\common\model\ProductLists;
  28. }
  29. /**
  30. * 查看
  31. *
  32. * @return string|Json
  33. * @throws \think\Exception
  34. * @throws DbException
  35. */
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if (false === $this->request->isAjax()) {
  41. return $this->view->fetch();
  42. }
  43. //如果发送的来源是 Selectpage,则转发到 Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  48. $list = $this->model->with('products')->with('productarea')->with('productareastop')->with('productorder')
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as &$item) {
  53. $item['hold_num'] = $item->productorder? count($item->productorder): 0;//持有数量
  54. $item['total_num']= $item->productarea? count($item->productarea): 0;
  55. $item['sell_num'] = $item->productareastop? count($item->productareastop): 0;
  56. }
  57. $result = ['total' => $list->total(), 'rows' => $list->items()];
  58. return json($result);
  59. }
  60. /**
  61. * 添加
  62. *
  63. * @return string
  64. * @throws \think\Exception
  65. */
  66. public function add()
  67. {
  68. if (false === $this->request->isPost()) {
  69. return $this->view->fetch();
  70. }
  71. $params = $this->request->post('row/a');
  72. if (empty($params)) {
  73. $this->error(__('Parameter %s can not be empty', ''));
  74. }
  75. $params = $this->preExcludeFields($params);
  76. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  77. $params[$this->dataLimitField] = $this->auth->id;
  78. }
  79. $result = false;
  80. Db::startTrans();
  81. try {
  82. //是否采用模型验证
  83. if ($this->modelValidate) {
  84. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  85. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  86. $this->model->validateFailException()->validate($validate);
  87. }
  88. //商品
  89. $result = $this->model->create($params);
  90. //if(!empty($params['is_area'])) $result= self::setEqualArea($result->id, $areaArr, $areaArrTxt, 0);
  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. * @param $ids
  104. * @return string
  105. * @throws DbException
  106. * @throws \think\Exception
  107. */
  108. public function edit($ids = null)
  109. {
  110. $row = $this->model->get($ids);
  111. if (!$row) {
  112. $this->error(__('No Results were found'));
  113. }
  114. $adminIds = $this->getDataLimitAdminIds();
  115. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  116. $this->error(__('You have no permission'));
  117. }
  118. if (false === $this->request->isPost()) {
  119. $this->view->assign('row', $row);
  120. return $this->view->fetch();
  121. }
  122. $params = $this->request->post('row/a');
  123. if (empty($params)) {
  124. $this->error(__('Parameter %s can not be empty', ''));
  125. }
  126. $params = $this->preExcludeFields($params);
  127. $result = false;
  128. Db::startTrans();
  129. try {
  130. //是否采用模型验证
  131. if ($this->modelValidate) {
  132. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  133. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  134. $row->validateFailException()->validate($validate);
  135. }
  136. $result=$row->allowField(true)->save($params);
  137. Db::commit();
  138. } catch (ValidateException|PDOException|Exception $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. }
  142. if (false === $result) {
  143. $this->error(__('No rows were updated'));
  144. }
  145. $this->success();
  146. }
  147. }