<?php
|
|
namespace app\supplier\controller\product;
|
|
use app\common\model\settings\Setting;
|
use app\supplier\model\product\Product as ProductModel;
|
use app\supplier\model\product\Category as CategoryModel;
|
use app\supplier\service\ProductService;
|
use app\supplier\model\supplier\Supplier as SupplierModel;
|
use app\supplier\controller\Controller;
|
use think\facade\Cache;
|
|
/**
|
* 商品管理控制器
|
*/
|
class Product extends Controller
|
{
|
/**
|
* 商品列表(全部)
|
*/
|
public function index()
|
{
|
$supplier = SupplierModel::detail($this->getSupplierId());
|
// 获取全部商品列表
|
$model = new ProductModel;
|
$list = $model->getList(array_merge(['status' => -1, 'shop_supplier_id' => $this->getSupplierId()], $this->postData()));
|
// 商品分类
|
$category = CategoryModel::getSupplierCacheTree($this->getSupplierId());
|
|
// 数量
|
$product_count = [
|
'sell' => $model->getCount('sell', $this->getSupplierId()),
|
'stock' => $model->getCount('stock', $this->getSupplierId()),
|
'recovery' => $model->getCount('recovery', $this->getSupplierId()),
|
'draft' => $model->getCount('draft', $this->getSupplierId()),
|
'lower' => $model->getCount('lower', $this->getSupplierId()),
|
'audit' => $model->getCount('audit', $this->getSupplierId()),
|
'no_audit' => $model->getCount('no_audit', $this->getSupplierId())
|
];
|
return $this->renderSuccess('', compact('list', 'category', 'supplier', 'product_count'));
|
}
|
|
/**
|
* 添加商品
|
*/
|
public function add($scene = 'add')
|
{
|
$supplier = SupplierModel::detail($this->getSupplierId());
|
// get请求
|
if($this->request->isGet()){
|
return $this->getBaseData($supplier);
|
}
|
//post请求
|
$data = json_decode($this->postData()['params'], true);
|
$data['is_newcomer'] = $supplier['is_newcomer'];
|
$data['is_repurchase'] = $supplier['is_repurchase'];
|
$data['is_vip'] = $supplier['is_vip'];
|
// 添加商品
|
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 &$sku){
|
$sku['product_sku_id'] = 0;
|
}
|
}
|
//初始化销量等数据
|
$data['sales_initial'] = 0;
|
}
|
//是否需要审核
|
$add_audit = Setting::getItem('store')['add_audit'];
|
if($add_audit == 0){
|
// 如果不需要审核,则审核状态是已审核
|
$data['audit_status'] = 10;
|
}
|
$model = new ProductModel;
|
if (isset($data['product_id'])) {
|
$data['product_id'] = 0;
|
}
|
|
$data['shop_supplier_id'] = $this->getSupplierId();
|
if ($model->add($data)) {
|
return $this->renderSuccess('添加成功');
|
}
|
return $this->renderError($model->getError() ?: '添加失败');
|
}
|
|
/**
|
* 获取基础数据
|
*/
|
public function getBaseData($supplier)
|
{
|
return $this->renderSuccess('', array_merge(ProductService::getEditData(null, 'add', $this->getSupplierId()),['is_newcomer'=>$supplier['is_newcomer'],'is_repurchase'=>$supplier['is_repurchase'],'is_vip'=>$supplier['is_vip']]));
|
}
|
|
/**
|
* 获取编辑数据
|
*/
|
public function getEditData($product_id, $scene = 'edit')
|
{
|
$model = ProductModel::detail($product_id);
|
return $this->renderSuccess('', array_merge(ProductService::getEditData($model, $scene, $this->getSupplierId()), compact('model')));
|
}
|
|
/**
|
* 商品编辑
|
*/
|
public function edit($product_id, $scene = 'edit')
|
{
|
$supplier = SupplierModel::detail($this->getSupplierId());
|
if($this->request->isGet()){
|
$model = ProductModel::detail($product_id);
|
$is_newcomer = $supplier['is_newcomer'];
|
$is_repurchase = $supplier['is_repurchase'];
|
$is_vip = $supplier['is_vip'];
|
$supplierName='';
|
if($model['belonging_shop_supplier_id']){
|
$belonging_shop_supplier= SupplierModel::detail($model['belonging_shop_supplier_id']);
|
$supplierName=$belonging_shop_supplier['name'];
|
}
|
return $this->renderSuccess('', array_merge(ProductService::getEditData($model, $scene, $this->getSupplierId()), compact('model','is_newcomer','is_repurchase','is_vip','supplierName')));
|
}
|
if ($scene == 'copy') {
|
return $this->add($scene);
|
}
|
// 商品详情
|
$model = ProductModel::detail($product_id);
|
$data = json_decode($this->postData()['params'], true);
|
$data['is_newcomer'] = $supplier['is_newcomer'];
|
$data['is_repurchase'] = $supplier['is_repurchase'];
|
$data['is_vip'] = $supplier['is_vip'];
|
// 更新记录
|
if ($model->edit($data,$this->getSupplierId())) {
|
return $this->renderSuccess('更新成功');
|
}
|
return $this->renderError($model->getError() ?: '更新失败');
|
}
|
|
/**
|
* 修改商品状态
|
*/
|
public function state($product_id, $state)
|
{
|
if(is_numeric($product_id)){
|
// 商品详情
|
$model = ProductModel::detail($product_id);
|
if($model['audit_status'] != 10){
|
return $this->renderError('商品状态不正确');
|
}
|
if (!$model->setStatus($state)) {
|
return $this->renderError('操作失败');
|
}
|
return $this->renderSuccess('操作成功');
|
}else{
|
$product_id_arr=explode(',',$product_id);
|
$s=0;
|
$e=0;
|
$errorMsg='';
|
foreach($product_id_arr as $key => $product_id){
|
// 商品详情
|
$model = ProductModel::detail($product_id);
|
if($model['audit_status'] != 10){
|
$e++;
|
}else if (!$model->setStatus($state)) {
|
$e++;
|
}else{
|
$s++;
|
}
|
}
|
if($e>0){
|
$errorMsg=$e.'条商品记录商品状态不正确';
|
}
|
return $this->renderSuccess('操作成功'.$s.'条记录!'.$errorMsg);
|
}
|
}
|
|
/**
|
* 删除商品
|
*/
|
public function delete($product_id)
|
{
|
if(is_numeric($product_id)){
|
// 商品详情
|
$model = ProductModel::detail($product_id);
|
if (!$model->setDelete()) {
|
return $this->renderError($model->getError() ?: '删除失败');
|
}
|
return $this->renderSuccess('删除成功');
|
}else{
|
$product_id_arr=explode(',',$product_id);
|
$s=0;
|
$e=0;
|
$errorMsg='';
|
foreach($product_id_arr as $key => $product_id){
|
// 商品详情
|
$model = ProductModel::detail($product_id);
|
if (!$model->setDelete()) {
|
$e++;
|
}else{
|
$s++;
|
}
|
}
|
if($e>0){
|
$errorMsg=$e.'条商品记录正在参与其他活动,不允许删除';
|
}
|
return $this->renderSuccess('删除成功'.$s.'条记录!'.$errorMsg);
|
}
|
|
}
|
}
|