AdminProductController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\rob\controller;
  3. use app\rob\model\OrderProductModel;
  4. use app\rob\model\ProductCodeModel;
  5. use app\rob\model\ProductModel;
  6. use cmf\controller\AdminBaseController;
  7. class AdminProductController extends AdminBaseController
  8. {
  9. /**
  10. * 商品列表
  11. */
  12. public function index()
  13. {
  14. $param = $this->request->param();
  15. //搜索条件
  16. $where = [];
  17. if (!empty($param['keyword'])) {
  18. $where[] = ['product_name', 'like', "%{$param['keyword']}%"];
  19. }
  20. if (!empty($param['on_sale'])) {
  21. $where[] = ['on_sale', '=', $param['on_sale']];
  22. }
  23. $exp = '(CASE WHEN start_time <= NOW() AND NOW() <= end_time THEN 1 ELSE 0 END) DESC,';
  24. $exp .= '(CASE WHEN start_time > NOW() THEN 1 ELSE 0 END) DESC,';
  25. $exp .= '(CASE WHEN end_time < NOW() THEN 1 ELSE 0 END) DESC';
  26. $order =new \think\db\Expression($exp);
  27. $list = ProductModel::where($where)
  28. ->order($order)
  29. ->paginate(10, false, ['query' => $param]);
  30. $this->assign('on_sale', isset($param['on_sale']) ? $param['on_sale'] : 0);
  31. $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
  32. $this->assign('list', $list->items());
  33. $this->assign('page', $list->render());
  34. return $this->fetch();
  35. }
  36. /**
  37. * 添加商品
  38. */
  39. public function add()
  40. {
  41. return $this->fetch();
  42. }
  43. /**
  44. * 添加商品提交
  45. */
  46. public function addPost()
  47. {
  48. if ($this->request->isPost()) {
  49. $data = $this->request->post();
  50. //错误提示
  51. if ($data['type'] == 2) {
  52. if (empty($data['code_list'])) {
  53. $this->error('请添加虚拟券号');
  54. }
  55. if (count(array_unique($data['code_list'])) != count($data['code_list'])) {
  56. $this->error('虚拟券号不能重复!');
  57. }
  58. unset($data['product_address'],$data['product_mobile']);
  59. }
  60. if (strtotime($data['end_time']) < strtotime($data['start_time'])) {
  61. $this->error('开始时间必须小于结束时间!');
  62. }
  63. //添加商品
  64. if (!empty($data['photo_urls'])) {
  65. foreach ($data['photo_urls'] as $k => $v) {
  66. $data['image_list'][] = ['url' => $v, 'name' => $data['photo_names'][$k]];
  67. }
  68. }
  69. $product = ProductModel::create($data);
  70. //虚拟券
  71. if ($data['type'] == 2) {
  72. $code_list = [];
  73. foreach ($data['code_list'] as $code) {
  74. $code_list[] = [
  75. 'product_id' => $product['id'],
  76. 'code' => $code,
  77. ];
  78. }
  79. ProductCodeModel::insertAll($code_list);
  80. }
  81. $this->success('添加成功!', url('index'));
  82. }
  83. }
  84. /**
  85. * 编辑商品
  86. */
  87. public function edit()
  88. {
  89. $id = $this->request->param('id', 0, 'intval');
  90. $info = ProductModel::get($id, ['productCode']);
  91. $this->assign('info', $info);
  92. return $this->fetch();
  93. }
  94. /**
  95. * 编辑商品提交
  96. */
  97. public function editPost()
  98. {
  99. if ($this->request->isPost()) {
  100. $data = $this->request->post();
  101. //验证
  102. if (strtotime($data['end_time']) < strtotime($data['start_time'])) {
  103. $this->error('开始时间必须小于结束时间!');
  104. }
  105. //编辑商品
  106. $data['options'] = [];
  107. if (!empty($data['file_urls'])) {
  108. foreach ($data['file_urls'] as $k => $v) {
  109. $data['options'][] = ['url' => $v, 'name' => $data['file_names'][$k]];
  110. }
  111. }
  112. ProductModel::update($data, ['id' => $data['id']]);
  113. $this->success('编辑成功!', url('index'));
  114. }
  115. }
  116. /**
  117. * 删除
  118. */
  119. public function delete()
  120. {
  121. $id = $this->request->param('id', 0, 'intval');
  122. //是否已被购买
  123. $check = OrderProductModel::where('order_id', $id)->find();
  124. if (!empty($check)) {
  125. $this->error('该商品已被购买,无法删除!');
  126. }
  127. ProductModel::destroy($id);
  128. $this->success('删除成功');
  129. }
  130. /**
  131. * 更改状态
  132. */
  133. public function setStatus()
  134. {
  135. $ids = $this->request->param('ids');
  136. $status = $this->request->param('status');
  137. ProductModel::update(['status' => $status], ['id' => $ids]);
  138. $this->success('操作成功');
  139. }
  140. }