Files
orico-official-website-old/app/admin/controller/ProductSpecial.php
2024-10-29 14:04:59 +08:00

200 lines
7.0 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use think\Lang;
use think\Loader;
use think\Config;
use think\TransDb;
class ProductSpecial extends BaseController {
public function index() {
$this->redirect('/admin/product_special/lists');
}
public function lists() {
$list = model('product_special')->where(['stat' => 0, 'country_code' => $this->country_code])->select();
$value = ['list' => $list];
$this->assign($value);
return $this->fetch();
}
public function add_product()
{
$id = $this->request->param('id');
if ($id <= 0)
return $this->error('id错误');
$product_special = model('product_special')->where(['stat' => 0, 'id' => $id, 'country_code' => $this->country_code])->find();
if (empty($product_special))
return $this->error('专题不存在');
$list = db('special_product_relation')->where(['special_id' => $id, 'stat' => 0])->select();
$this->assign('special_id', $id);
$this->assign('list', $list);
return $this->fetch();
}
public function update_product()
{
$data = $this->request->param();
// tiaoshi($data);die;
if (!is_array($data))
return $this->error('数据错误');
$id = $data['id'];
$arr_product_id = $data['product_id'];
$type = $data['type'];
$image = $data['picture'];
$product_special = db('special_product_relation')->where(['stat' => 0, 'country_code' => $this->country_code, 'special_id' => $id])->find();
if (!empty($product_special))
{
db('special_product_relation')->where(['stat' => 0, 'country_code' => $this->country_code, 'special_id' => $id])->update(['stat' => 1]);
}
foreach ($arr_product_id as $key => $value)
{
$insert_data = [
'special_id' => $id,
'product_id' => $value,
'type' => $type[$key],
'img' => $image[$key],
'country_code' => $this->country_code,
];
$result = db('special_product_relation')->insert($insert_data);
if (!$result)
return $this->error('添加失败');
}
return $this->success('添加成功', url('/admin/product_special/lists'));
}
public function add()
{
return $this->fetch();
}
public function create() {
if ($this->request->isPost()) {
$data = $this->request->post();
// tiaoshi($data);die;
if (empty($data) || !is_array($data)) {
return $this->error(Lang::get('incorrect operation'));
}
$validaterule = ['name' => 'require'];
$validatemsg = ['name.require' => '名称不能为空'];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result)
return $this->error($valid_result);
$data['sort'] = intval($data['sort']);
$data['country_code'] = $this->country_code;
$data['create_time'] = time();
$model = model('product_special')->insertGetId($data);
if ($model)
return $this->redirect(url('/admin/product_special/lists'));
else
return $this->error(Lang::get('operation failed'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
public function edit($id = 0) {
$id = intval($id);
if ($id > 0) {
$product_special = model('product_special')->where(['id' => $id, 'country_code' => $this->country_code])->find();
if (empty($product_special))
return $this->error(Lang::get('incorrect operation'));
$value['product_special'] = $product_special;
} else
return $this->error(Lang::get('incorrect operation'));
$this->assign($value);
return $this->fetch();
}
public function update() {
if ($this->request->isPost()) {
$data = $this->request->post();
if (empty($data) || !is_array($data))
return $this->error(Lang::get('incorrect operation'));
$validaterule = ['name' => 'require'];
$validatemsg = ['name.require' => '名称不能为空'];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result)
return $this->error($valid_result);
$product_special = model('product_special')->where(['id' => $data['id'], 'country_code' => $this->country_code])->find();
if (empty($product_special))
return $this->error('专题不存在');
$data['sort'] = intval($data['sort']);
$model = model('product_special')->updateRow($data);
if ($model)
return $this->redirect(url('/admin/product_special/lists'));
else
return $this->error(Lang::get('operation failed'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
public function updatesort() {
$id = $this->request->param('id', 0);
$sort = $this->request->param('sort', 0);
$sort = intval($sort);
$id = intval($id);
if ($id && $sort < 2147483647) {
$model = model('product_special')->updateRow(['id' => $id, 'sort' => $sort]);
if ($model && $model->getData('id')) {
$this->cacheClear('ProductCategoryTag');
return $this->success(Lang::get('operation successed'), url('/admin/product_special/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function togglestat() {
$id = $this->request->get('id', 0);
$flag = $this->request->get('flag', 0);
$id = intval($id);
if ($id > 0) {
$model = model('product_special')->updateRow(['id' => $id, 'stat' => !$flag]);
if ($model && $model->getData('id')) {
$this->cacheClear('ProductCategoryTag');
return $this->success(Lang::get('operation successed'), url('/admin/product_special/lists'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
public function delete($id = 0) {
$id = intval($id);
if ($id > 0) {
$result = model('product_special')->where(['id' => $id])->update(['stat' => 1]);
if ($result) {
return $this->success(Lang::get('operation successed'));
} else {
return $this->error(Lang::get('operation failed'));
}
}
return $this->error(Lang::get('incorrect operation'));
}
}