| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace app\shop\controller\product;
- use app\common\model\product\ProductTimer;
- use app\common\model\shop\Task;
- use app\shop\model\product\Product as ProductModel;
- use app\shop\model\product\Category as CategoryModel;
- use app\shop\service\ProductService;
- use app\shop\controller\Controller;
- use think\facade\Cache;
- use think\response\Json;
- /**
- * 商品管理控制器
- */
- class Product extends Controller
- {
- /**
- * 商品列表(全部)
- */
- public function index()
- {
- // 获取全部商品列表
- $model = new ProductModel;
- $list = $model->getList(array_merge(['status' => -1], $this->postData()));
- // 商品分类
- $category = CategoryModel::getCacheTree();
- // 数量
- $product_count = [
- 'sell' => $model->getCount('sell'),
- 'stock' => $model->getCount('stock'),
- 'recovery' => $model->getCount('recovery'),
- 'over' => $model->getCount('over'),
- 'lower' => $model->getCount('lower')
- ];
- return $this->renderSuccess('', compact('list', 'category', 'product_count'));
- }
- /**
- * 商品列表(在售)
- */
- public function lists()
- {
- // 获取全部商品列表
- $model = new ProductModel;
- $list = $model->getLists($this->postData());
- // 商品分类
- $catgory = CategoryModel::getCacheTree();
- return $this->renderSuccess('', compact('list', 'catgory'));
- }
- /**
- * 添加商品
- */
- public function add($scene = 'add')
- {
- // get请求
- if($this->request->isGet()){
- return $this->getBaseData();
- }
- //post请求
- $data = json_decode($this->postData()['params'], true);
- if($scene == 'copy'){
- unset($data['create_time']);
- unset($data['sku']['product_sku_id']);
- unset($data['sku']['product_id']);
- unset($data['product_sku']['product_sku_id']);
- unset($data['product_sku']['product_id']);
- if($data['spec_type'] == 20){
- foreach ($data['spec_many']['spec_list'] as &$spec){
- $spec['product_sku_id'] = 0;
- }
- }
- //初始化销量等数据
- $data['sales_initial'] = 0;
- }
- $model = new ProductModel;
- if (isset($data['product_id'])) {
- $data['product_id'] = 0;
- }
- if ($model->add($data)) {
- return $this->renderSuccess('添加成功');
- }
- return $this->renderError($model->getError() ?: '添加失败');
- }
- /**
- * 获取基础数据
- */
- public function getBaseData()
- {
- return $this->renderSuccess('', array_merge(ProductService::getEditData(null, 'add'), []));
- }
- /**
- * 获取编辑数据
- */
- public function getEditData($product_id, $scene = 'edit')
- {
- $model = ProductModel::detail($product_id);
- return $this->renderSuccess('', array_merge(ProductService::getEditData($model, $scene), compact('model')));
- }
- /**
- * 商品编辑
- */
- public function edit($product_id, $scene = 'edit')
- {
- if($this->request->isGet()){
- $model = ProductModel::detail($product_id);
- // p($model['is_enable_grade']);
- return $this->renderSuccess('', array_merge(ProductService::getEditData($model, $scene), compact('model')));
- }
- if ($scene == 'copy') {
- return $this->add($scene);
- }
- // 商品详情
- $model = ProductModel::detail($product_id);
- // 更新记录
- if ($model->edit(json_decode($this->postData()['params'], true))) {
- return $this->renderSuccess('更新成功');
- }
- return $this->renderError($model->getError() ?: '更新失败');
- }
- /**
- * 修改商品状态
- */
- public function state($product_id, $state)
- {
- // 商品详情
- $model = ProductModel::detail($product_id);
- if (!$model->setStatus($state)) {
- return $this->renderError('操作失败');
- }
- // 删除定时器
- $timer = ProductTimer::detail($product_id);
- if ($timer) {
- // 删除定时数据
- $timer->deleteTimer($product_id);
- // 从缓存中删除队列
- Cache::store('redis')->delete($timer['task_id']);
- // 写入task表,留存执行记录
- $record = [
- 'task_id' => $timer['task_id'],
- 'category' => 'product',
- 'tid' => $timer['product_id'],
- 'state' => $timer['state_id'],
- 'publish_time' => $timer['publish_time'],
- 'payload' => '',
- 'result' => 2, // 1成功,0失败,2取消
- ];
- Task::add($record);
- }
- return $this->renderSuccess('操作成功');
- }
- /**
- * 商品定时上下架
- * @param string $product_id 商品ID
- * @param integer $state 状态
- * @param string $solt 时间
- * @return Json
- */
- public function addTimer(string $product_id, int $state, string $solt): Json
- {
- // 商品详情
- $model = ProductModel::detail($product_id);
- if (!$model) {
- return $this->renderError('商品错误');
- }
- if (!$model->setTimer($model, $state, $solt)) {
- return $this->renderError('操作失败');
- }
- return $this->renderSuccess('操作成功');
- }
- /**
- * 删除商品定时上下架
- * @param $product_id
- * @return Json
- */
- public function deleteTimer($product_id): Json
- {
- $timer = ProductTimer::detail($product_id);
- // 删除定时数据
- if (!$timer->deleteTimer($product_id)) {
- return $this->renderError('操作失败');
- }
- // 写入task表,留存执行记录
- $record = [
- 'task_id' => $timer['task_id'],
- 'category' => 'product',
- 'tid' => $timer['product_id'],
- 'state' => $timer['state_id'],
- 'publish_time' => $timer['publish_time'],
- 'payload' => '',
- 'result' => 2, // 1成功,0失败,2取消
- ];
- Task::add($record);
- return $this->renderSuccess('操作成功');
- }
- /**
- * 删除商品
- */
- public function delete($product_id): Json
- {
- // 商品详情
- $model = ProductModel::detail($product_id);
- if (!$model->setDelete()) {
- return $this->renderError($model->getError() ?: '删除失败');
- }
- // 删除定时器
- $timer = ProductTimer::detail($product_id);
- if ($timer) {
- // 删除定时数据
- $timer->deleteTimer($product_id);
- // 写入task表,留存执行记录
- $record = [
- 'task_id' => $timer['task_id'],
- 'category' => 'product',
- 'tid' => $timer['product_id'],
- 'state' => $timer['state_id'],
- 'publish_time' => $timer['publish_time'],
- 'payload' => '',
- 'result' => 2, // 1成功,0失败,2取消
- ];
- Task::add($record);
- }
- return $this->renderSuccess('删除成功');
- }
- }
|