request->param(); //搜索条件 $where = []; if (!empty($param['keyword'])) { $where[] = ['product_name', 'like', "%{$param['keyword']}%"]; } if (!empty($param['on_sale'])) { $where[] = ['on_sale', '=', $param['on_sale']]; } $exp = '(CASE WHEN start_time <= NOW() AND NOW() <= end_time THEN 1 ELSE 0 END) DESC,'; $exp .= '(CASE WHEN start_time > NOW() THEN 1 ELSE 0 END) DESC,'; $exp .= '(CASE WHEN end_time < NOW() THEN 1 ELSE 0 END) DESC'; $order =new \think\db\Expression($exp); $list = ProductModel::where($where) ->order($order) ->paginate(10, false, ['query' => $param]); $this->assign('on_sale', isset($param['on_sale']) ? $param['on_sale'] : 0); $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : ''); $this->assign('list', $list->items()); $this->assign('page', $list->render()); return $this->fetch(); } /** * 添加商品 */ public function add() { return $this->fetch(); } /** * 添加商品提交 */ public function addPost() { if ($this->request->isPost()) { $data = $this->request->post(); //错误提示 if ($data['type'] == 2) { if (empty($data['code_list'])) { $this->error('请添加虚拟券号'); } if (count(array_unique($data['code_list'])) != count($data['code_list'])) { $this->error('虚拟券号不能重复!'); } unset($data['product_address'],$data['product_mobile']); } if (strtotime($data['end_time']) < strtotime($data['start_time'])) { $this->error('开始时间必须小于结束时间!'); } //添加商品 if (!empty($data['photo_urls'])) { foreach ($data['photo_urls'] as $k => $v) { $data['image_list'][] = ['url' => $v, 'name' => $data['photo_names'][$k]]; } } $product = ProductModel::create($data); //虚拟券 if ($data['type'] == 2) { $code_list = []; foreach ($data['code_list'] as $code) { $code_list[] = [ 'product_id' => $product['id'], 'code' => $code, ]; } ProductCodeModel::insertAll($code_list); } $this->success('添加成功!', url('index')); } } /** * 编辑商品 */ public function edit() { $id = $this->request->param('id', 0, 'intval'); $info = ProductModel::get($id, ['productCode']); $this->assign('info', $info); return $this->fetch(); } /** * 编辑商品提交 */ public function editPost() { if ($this->request->isPost()) { $data = $this->request->post(); //验证 if (strtotime($data['end_time']) < strtotime($data['start_time'])) { $this->error('开始时间必须小于结束时间!'); } //编辑商品 $data['options'] = []; if (!empty($data['file_urls'])) { foreach ($data['file_urls'] as $k => $v) { $data['options'][] = ['url' => $v, 'name' => $data['file_names'][$k]]; } } ProductModel::update($data, ['id' => $data['id']]); $this->success('编辑成功!', url('index')); } } /** * 删除 */ public function delete() { $id = $this->request->param('id', 0, 'intval'); //是否已被购买 $check = OrderProductModel::where('order_id', $id)->find(); if (!empty($check)) { $this->error('该商品已被购买,无法删除!'); } ProductModel::destroy($id); $this->success('删除成功'); } /** * 更改状态 */ public function setStatus() { $ids = $this->request->param('ids'); $status = $this->request->param('status'); ProductModel::update(['status' => $status], ['id' => $ids]); $this->success('操作成功'); } }