You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/store/controller/Goods.php

198 lines
5.9 KiB

11 months ago
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\store\controller;
11 months ago
use think\db\exception\DbException;
11 months ago
use think\response\Json;
use cores\exception\BaseException;
use app\store\model\Goods as GoodsModel;
7 months ago
use app\store\model\goods\Import as ImportModel;
11 months ago
/**
* 商品管理控制器
* Class Goods
* @package app\store\controller
*/
class Goods extends Controller
{
/**
* 商品列表
* @return Json
11 months ago
* @throws DbException
11 months ago
*/
public function list(): Json
{
// 获取列表记录
$model = new GoodsModel;
8 months ago
$params = $this->request->param();
7 months ago
$params['channel'] = 'zy';
8 months ago
$list= $model->getList($params, (int)$this->request->param('pageSize', 15));
11 months ago
return $this->renderSuccess(compact('list'));
}
/**
* 根据商品ID集获取列表记录
* @param array $goodsIds
* @return Json
*/
public function listByIds(array $goodsIds): Json
{
// 获取列表记录
$model = new GoodsModel;
$list = $model->getListByIds($goodsIds);
return $this->renderSuccess(compact('list'));
}
/**
* 商品详情(详细信息)
* @param int $goodsId
* @return Json
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
11 months ago
* @throws DbException
11 months ago
* @throws \think\db\exception\ModelNotFoundException
*/
public function detail(int $goodsId): Json
{
// 获取商品详情
$model = new GoodsModel;
$goodsInfo = $model->getDetail($goodsId);
return $this->renderSuccess(compact('goodsInfo'));
}
/**
* 商品详情(基础信息)
* @param int $goodsId
* @return Json
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
11 months ago
* @throws DbException
11 months ago
* @throws \think\db\exception\ModelNotFoundException
*/
public function basic(int $goodsId): Json
{
// 获取商品详情
$model = new GoodsModel;
$detail = $model->getBasic($goodsId);
return $this->renderSuccess(compact('detail'));
}
/**
* 添加商品
* @return Json
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
11 months ago
* @throws DbException
11 months ago
* @throws \think\db\exception\ModelNotFoundException
*/
public function add(): Json
{
$model = new GoodsModel;
if ($model->add($this->postForm())) {
return $this->renderSuccess('添加成功');
}
return $this->renderError($model->getError() ?: '添加失败');
}
/**
* 商品编辑
* @param int $goodsId
* @return Json
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
11 months ago
* @throws DbException
11 months ago
* @throws \think\db\exception\ModelNotFoundException
*/
public function edit(int $goodsId): Json
{
// 商品详情
$model = GoodsModel::detail($goodsId);
// 更新记录
if ($model->edit($this->postForm())) {
return $this->renderSuccess('更新成功');
}
return $this->renderError($model->getError() ?: '更新失败');
}
/**
* 修改商品状态(上下架)
* @param array $goodsIds 商品id集
* @param bool $state 为true表示上架
* @return Json
*/
public function state(array $goodsIds, bool $state): Json
{
$model = new GoodsModel;
if (!$model->setStatus($goodsIds, $state)) {
return $this->renderError($model->getError() ?: '操作失败');
}
return $this->renderSuccess('操作成功');
}
/**
* 删除商品
* @param array $goodsIds
* @return Json
*/
public function delete(array $goodsIds): Json
{
$model = new GoodsModel;
if (!$model->setDelete($goodsIds)) {
return $this->renderError($model->getError() ?: '删除失败');
}
return $this->renderSuccess('删除成功');
}
7 months ago
public function export(){
$model = new GoodsModel;
$params = $this->request->param();
$params['store_id'] = $this->storeId;
$perSize = 10000;
$params['page'] = 1;
$params['channels'] = ['zy'];
$data = $model->getAdminListExport($params, $perSize)->toArray();
// echo "<pre>";
// print_r($data);
// exit();
$titles = [
['goods_id'=>'系统编码'],
['goods_name'=>'标题'],
['cmmdty_model'=>'型号'],
['link'=>'该商品苏宁的链接'],
['cost_price_min'=>'成本价'],
['stock_total'=>'库存'],
['link_other'=>'京东的价拖链接'],
['goods_price_min'=>'前台价'],
];
downLoadExcel('导出数据-'.date('Y-m-d', time()),$titles,$data['data']);
}
public function import(){
// 新增记录
$model = new ImportModel;
$params = $this->postData();
$params['channel'] = "zy";
if ($model->goodsUpdateBatch($params)) {
return $this->renderSuccess('已添加到导入任务中,请在历史导入记录中查看结果');
}
return $this->renderError($model->getError() ?: '操作失败');
}
11 months ago
}