Popular.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\ProductArea;
  7. use think\exception\DbException;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. /**
  11. * 热门抢购
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Popular extends Backend
  16. {
  17. /**
  18. * Popular模型对象
  19. * @var \app\admin\model\product\Popular
  20. */
  21. protected $model = null;
  22. protected $productArea = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\common\model\ProductPopular();
  27. $this->productArea = new ProductArea;
  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('productsList')
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. $result = ['total' => $list->total(), 'rows' => $list->items()];
  53. return json($result);
  54. }
  55. /**
  56. * 添加
  57. * @return string
  58. * @throws \think\Exception
  59. */
  60. public function add()
  61. {
  62. if (false === $this->request->isPost()) {
  63. return $this->view->fetch();
  64. }
  65. $params = $this->request->post('row/a');
  66. if (empty($params)) {
  67. $this->error(__('Parameter %s can not be empty', ''));
  68. }
  69. $params = $this->preExcludeFields($params);
  70. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  71. $params[$this->dataLimitField] = $this->auth->id;
  72. }
  73. $result = false;
  74. Db::startTrans();
  75. try {
  76. //是否采用模型验证
  77. if ($this->modelValidate) {
  78. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  79. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  80. $this->model->validateFailException()->validate($validate);
  81. }
  82. if(time() >= strtotime($params['start_time'])) $params['status'] = $this->model::NORMAL;
  83. if(time() >= strtotime($params['end_time'])) $params['status'] = $this->model::STOP;
  84. $params['num'] = 0;
  85. $params['stock'] = ProductArea::getProductStock($params['product_id']);
  86. $result = $this->model->allowField(true)->save($params);
  87. Db::commit();
  88. } catch (ValidateException|PDOException|Exception $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. }
  92. if ($result === false) {
  93. $this->error(__('No rows were inserted'));
  94. }
  95. $this->success();
  96. }
  97. /**
  98. * 编辑
  99. * @param $ids
  100. * @return string
  101. * @throws DbException
  102. * @throws \think\Exception
  103. */
  104. public function edit($ids = null)
  105. {
  106. $row = $this->model->get($ids);
  107. if (!$row) {
  108. $this->error(__('No Results were found'));
  109. }
  110. $adminIds = $this->getDataLimitAdminIds();
  111. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  112. $this->error(__('You have no permission'));
  113. }
  114. if (false === $this->request->isPost()) {
  115. $this->view->assign('row', $row);
  116. return $this->view->fetch();
  117. }
  118. $params = $this->request->post('row/a');
  119. if (empty($params)) {
  120. $this->error(__('Parameter %s can not be empty', ''));
  121. }
  122. $params = $this->preExcludeFields($params);
  123. $result = false;
  124. Db::startTrans();
  125. try {
  126. //是否采用模型验证
  127. if ($this->modelValidate) {
  128. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  129. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  130. $row->validateFailException()->validate($validate);
  131. }
  132. if(time() < strtotime($params['start_time'])) $params['status'] = $this->model::NOTSTOP;
  133. if(time() >= strtotime($params['start_time'])) $params['status'] = $this->model::NORMAL;
  134. if(time() >= strtotime($params['end_time'])) $params['status'] = $this->model::STOP;
  135. $params['stock'] = ProductArea::getProductStock($params['product_id']);
  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. }