123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace app\rob\controller;
- use app\rob\model\OrderProductModel;
- use app\rob\model\ProductCodeModel;
- use app\rob\model\ProductModel;
- use cmf\controller\AdminBaseController;
- class AdminProductController extends AdminBaseController
- {
- /**
- * 商品列表
- */
- public function index()
- {
- $param = $this->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('操作成功');
- }
- }
|