Coupons.php 7.4 KB

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