Coupons.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\admin\controller\shopro;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. *
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Coupons extends Backend
  14. {
  15. /**
  16. * Coupons模型对象
  17. * @var \app\admin\model\shopro\Coupons
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\shopro\Coupons;
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = false;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax())
  40. {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField'))
  43. {
  44. return $this->selectpage();
  45. }
  46. $searchWhere = $this->request->request('searchWhere');
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $total = $this->model
  49. ->where($where)
  50. ->whereOr('id', '=', $searchWhere)
  51. ->whereOr('name', 'like', "%$searchWhere%")
  52. ->order($sort, $order)
  53. ->count();
  54. $list = $this->model
  55. ->where($where)
  56. ->whereOr('id', '=', $searchWhere)
  57. ->whereOr('name', 'like', "%$searchWhere%")
  58. ->order($sort, $order)
  59. ->limit($offset, $limit)
  60. ->select();
  61. foreach ($list as $row) {
  62. $row->getnum = \app\admin\model\shopro\user\Coupon::where(['coupons_id' => $row->id])->count();
  63. $row->usenum = \app\admin\model\shopro\user\Coupon::where(['coupons_id' => $row->id, 'usetime' => ['neq', 'null']])->count();
  64. $row->goods = $this->getGoods($row);
  65. }
  66. $list = collection($list)->toArray();
  67. $result = array("total" => $total, "rows" => $list);
  68. return $this->success('优惠券', null, $result);
  69. }
  70. return $this->view->fetch();
  71. }
  72. /**
  73. * 添加
  74. */
  75. public function add()
  76. {
  77. if ($this->request->isPost()) {
  78. $params = $this->request->post();
  79. if ($params) {
  80. $params = json_decode($params['data'], true);
  81. $result = false;
  82. Db::startTrans();
  83. try {
  84. $result = $this->model->allowField(true)->save($params);
  85. Db::commit();
  86. } catch (ValidateException $e) {
  87. Db::rollback();
  88. $this->error($e->getMessage());
  89. } catch (PDOException $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. } catch (Exception $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. }
  96. if ($result !== false) {
  97. $this->success();
  98. } else {
  99. $this->error(__('No rows were inserted'));
  100. }
  101. }
  102. $this->error(__('Parameter %s can not be empty', ''));
  103. }
  104. return $this->view->fetch();
  105. }
  106. /**
  107. * 编辑
  108. */
  109. public function edit($ids = null)
  110. {
  111. $row = $this->model->get($ids);
  112. if (!$row) {
  113. $this->error(__('No Results were found'));
  114. }
  115. $row->goods = $this->getGoods($row);
  116. $adminIds = $this->getDataLimitAdminIds();
  117. if (is_array($adminIds)) {
  118. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  119. $this->error(__('You have no permission'));
  120. }
  121. }
  122. if ($this->request->isPost()) {
  123. $params = $this->request->post();
  124. if ($params) {
  125. $params = json_decode($params['data'], true);
  126. $result = false;
  127. Db::startTrans();
  128. try {
  129. $result = $row->allowField(true)->save($params);
  130. Db::commit();
  131. } catch (ValidateException $e) {
  132. Db::rollback();
  133. $this->error($e->getMessage());
  134. } catch (PDOException $e) {
  135. Db::rollback();
  136. $this->error($e->getMessage());
  137. } catch (Exception $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. }
  141. if ($result !== false) {
  142. $this->success();
  143. } else {
  144. $this->error(__('No rows were updated'));
  145. }
  146. }
  147. $this->error(__('Parameter %s can not be empty', ''));
  148. }
  149. $this->assignconfig("row", $row);
  150. return $this->view->fetch();
  151. }
  152. /**
  153. * 回收站
  154. */
  155. public function recyclebin()
  156. {
  157. //设置过滤方法
  158. $this->request->filter(['strip_tags']);
  159. if ($this->request->isAjax()) {
  160. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  161. $total = $this->model
  162. ->onlyTrashed()
  163. ->where($where)
  164. ->order($sort, $order)
  165. ->count();
  166. $list = $this->model
  167. ->onlyTrashed()
  168. ->where($where)
  169. ->order($sort, $order)
  170. ->limit($offset, $limit)
  171. ->select();
  172. $result = array("total" => $total, "rows" => $list);
  173. return json($result);
  174. }
  175. return $this->view->fetch();
  176. }
  177. /**
  178. * 选择
  179. */
  180. public function select()
  181. {
  182. //设置过滤方法
  183. $this->request->filter(['strip_tags']);
  184. if ($this->request->isAjax()) {
  185. //如果发送的来源是Selectpage,则转发到Selectpage
  186. if ($this->request->request('keyField')) {
  187. return $this->selectpage();
  188. }
  189. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  190. $searchWhere = $this->request->request('searchWhere');
  191. $total = $this->model
  192. ->where($where)
  193. ->whereOr('id', '=', $searchWhere)
  194. ->whereOr('name', 'like', "%$searchWhere%")
  195. ->count();
  196. $list = $this->model
  197. ->where($where)
  198. ->whereOr('id', '=', $searchWhere)
  199. ->whereOr('name', 'like', "%$searchWhere%")
  200. ->limit($offset, $limit)
  201. ->select();
  202. $result = array("total" => $total, "rows" => $list);
  203. return json($result);
  204. }
  205. return $this->view->fetch();
  206. }
  207. private function getGoods($data)
  208. {
  209. if($data['goods_ids'] != 0) {
  210. return \app\admin\model\shopro\goods\Goods::where('id', 'in', $data['goods_ids'])->field('id, title, image')->select();
  211. }
  212. return null;
  213. }
  214. }