admin/addon/cashier/storeapi/controller/Goods.php

214 lines
9.5 KiB
PHP

<?php
/**
* ThinkShop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.cdcloudshop.com
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
* =========================================================
*/
namespace addon\cashier\storeapi\controller;
use app\model\goods\Goods as GoodsModel;
use app\model\goods\GoodsCategory;
use app\model\storegoods\StoreGoods as StoreGoodsModel;
use app\storeapi\controller\BaseStoreApi;
/**
* 活动管理控制器
* Class Activity
* @package addon\shop\storeapi\controller
*/
class Goods extends BaseStoreApi
{
/**
* 获取商品分类的组织
* @return false|string
*/
public function category()
{
$level = $this->params[ 'level' ] ?? 1;
$service_category_model = new GoodsCategory();
$condition = [
[ 'is_show', '=', 0 ],
[ 'level', '<=', $level ],
[ 'site_id', '=', $this->site_id ]
];
$list = $service_category_model->getCategoryTree($condition, 'pid,category_id,category_name,image,level', 'sort asc,category_id desc');
return $this->response($list);
}
public function page()
{
$page_index = $this->params[ 'page' ] ?? 1;
$page_size = $this->params[ 'page_size' ] ?? PAGE_LIST_ROWS;
$goods_category = $this->params[ 'category' ] ?? 'all';
$search_text = $this->params[ 'search_text' ] ?? '';
$goods_class = $this->params[ 'goods_class' ] ?? 'all';
$model = new GoodsModel();
$condition = [
[ 'g.site_id', '=', $this->site_id ],
[ 'g.is_delete', '=', 0 ],
[ 'g.goods_state', '=', 1 ],
[ 'g.sale_store', 'like', [ '%all%', '%,' . $this->store_id . ',%' ], 'or' ],
];
if ($goods_class !== 'all') {
$condition[] = [ 'g.goods_class', '=', $goods_class ];
} else {
$condition[] = [ 'g.goods_class', 'in', '4,5' ];
}
if ($goods_category != 'all') $condition[] = [ 'g.category_id', 'like', "%,{$goods_category},%" ];
if (!empty($search_text)) $condition[] = [ 'g.goods_name', 'like', "%{$search_text}%" ];
$status = $this->params[ 'status' ] ?? 1;
if ($status !== 'all') {
$condition[] = [ 'sg1.status', '=', $status ];
}
$field = 'g.goods_id,g.goods_name,g.goods_class_name,g.introduction,g.goods_image,g.goods_state,g.sku_id,g.price,gs.discount_price,g.goods_spec_format,g.is_unify_pirce,
IF(g.is_unify_pirce = 1,g.price,sg1.price) as price, IF(g.is_unify_pirce = 1,gs.discount_price,sg1.price) as discount_price,
sg1.price as store_price,sg1.status as store_status';
$join = [
[ 'goods_sku gs', 'gs.sku_id = g.sku_id', 'left' ],
[ 'store_goods sg1', 'g.goods_id=sg1.goods_id and sg1.store_id=' . $this->store_id, 'left' ],
];
//todo 这部分可以封装
$stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
if ($stock_store_id == $this->store_id) {
$field .= ', sg1.stock';
} else {
$join[] = [ 'store_goods sg2', 'g.goods_id = sg2.goods_id and sg2.store_id=' . $stock_store_id, 'left' ];
$field .= ', sg2.stock';
}
$data = $model->getGoodsPageList($condition, $page_index, $page_size, 'g.sort asc,g.create_time desc', $field, 'g', $join);
return $this->response($data);
}
/**
* 商品详情
* @return false|string
*/
public function detail()
{
$goods_id = $this->params[ 'goods_id' ] ?? 0;
$goods_model = new GoodsModel();
$field = 'g.goods_id, g.goods_name, g.introduction,g.goods_class,g.is_virtual, g.goods_class_name, g.goods_image, g.goods_state, g.sku_id, g.price, g.unit, g.cost_price, g.category_id, g.brand_name,g.is_unify_pirce,
sg1.price as store_price, sg1.cost_price as store_cost_price, sg1.status as store_status';
$join = [
[ 'store_goods sg1', 'g.goods_id=sg1.goods_id and sg1.store_id=' . $this->store_id, 'left' ],
];
//todo 这部分可以封装
$stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
if ($stock_store_id == $this->store_id) {
$field .= ',sg1.stock';
} else {
$join[] = [ 'store_goods sg2', 'g.goods_id = sg2.goods_id and sg2.store_id=' . $stock_store_id, 'left' ];
$field .= ', sg2.stock';
}
$goods_info = $goods_model->getGoodsInfo([ [ 'g.goods_id', '=', $goods_id ], [ 'g.site_id', '=', $this->site_id ] ], $field, 'g', $join)[ 'data' ];
if (empty($goods_info)) return $this->response($goods_model->error(null, '商品信息缺失'));
//查询商品规格
$sku_filed = 'sku.sku_id,sku.sku_name,sku.sku_no,sku.price,sku.discount_price,sku.cost_price,sku.sku_image,sku.sku_images,sku.spec_name,
sgs1.price as store_price, sgs1.cost_price as store_cost_price, sgs1.status as store_status';
$join = [
[ 'store_goods_sku sgs1', 'sku.sku_id=sgs1.sku_id and sgs1.store_id=' . $this->store_id, 'left' ],
];
if ($stock_store_id == $this->store_id) {
$sku_filed .= ', sgs1.stock';
} else {
$join[] = [ 'store_goods_sku sgs2', 'sku.sku_id = sgs2.sku_id and sgs2.store_id=' . $stock_store_id, 'left' ];
$sku_filed .= ', sgs2.stock';
}
$goods_info[ 'sku_list' ] = $goods_model->getGoodsSkuList([ [ 'sku.goods_id', '=', $goods_id ], [ 'sku.site_id', '=', $this->site_id ] ], $sku_filed, 'sku.sku_id asc', 0, 'sku', $join)[ 'data' ];
return $this->response($goods_model->success($goods_info));
}
/**
* 上下架
*/
public function setStatus()
{
$goods_id = $this->params[ 'goods_id' ] ?? 0;
$status = $this->params[ 'status' ] ?? 0;
$model = new StoreGoodsModel();
$res = $model->modifyGoodsState($goods_id, $status, $this->site_id, $this->store_id);
return $this->response($res);
}
/**
* 商品编辑
*/
public function editGoods()
{
$goods_sku_array = isset($this->params[ 'goods_sku_list' ]) ? json_decode($this->params[ 'goods_sku_list' ], true) : [];
$model = new StoreGoodsModel();
$res = $model->editStoreGoods($goods_sku_array, $this->site_id, $this->store_id, $this->uid);
return $this->response($res);
}
/**
* 获取商品规格
*/
public function skuList()
{
$goods_id = $this->params[ 'goods_id' ] ?? 0;
$sku_filed = 'sku.goods_id,sku.sku_id,sku.sku_name,sku.goods_name,sku.sku_no,sku.sku_image,sku.sku_images,sku.spec_name,sku.goods_spec_format,sku.unit,IF(g.is_unify_pirce = 1,sku.price,sgs.price) as price';
$join = [
[ 'goods g', 'sku.goods_id=g.goods_id', 'inner' ],
[ 'store_goods_sku sgs', 'sku.sku_id=sgs.sku_id and sgs.store_id=' . $this->store_id, 'inner' ],
];
$stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
if ($stock_store_id == $this->store_id) {
$sku_filed .= ', sgs.stock';
} else {
$join[] = [ 'store_goods_sku sgs2', 'sku.sku_id = sgs2.sku_id and sgs2.store_id=' . $stock_store_id, 'left' ];
$sku_filed .= ', sgs2.stock';
}
$goods_model = new GoodsModel();
$sku_list = $goods_model->getGoodsSkuList([ [ 'sku.goods_id', '=', $goods_id ], [ 'sku.site_id', '=', $this->site_id ] ], $sku_filed, 'sku.sku_id asc', 0, 'sku', $join)[ 'data' ];
return $this->response($this->success($sku_list));
}
/**
* 查询sku信息
* @return false|string
*/
public function skuInfo()
{
$sku_id = $this->params[ 'sku_id' ] ?? 0;
$sku_no = $this->params[ 'sku_no' ] ?? '';
$condition = [
[ 'sku.site_id', '=', $this->site_id ]
];
if ($sku_id) $condition[] = [ 'sku.sku_id', '=', $sku_id ];
if ($sku_no) $condition[] = [ 'sku.sku_no', '=', $sku_no ];
$sku_filed = 'sku.goods_id,sku.sku_id,sku.sku_name,sku.goods_name,sku.sku_no,sku.sku_image,sku.sku_images,sku.spec_name,sku.goods_spec_format,sku.unit,IF(g.is_unify_pirce = 1,sku.price,sgs.price) as price,sku.cost_price';
$join = [
[ 'goods g', 'sku.goods_id=g.goods_id', 'inner' ],
[ 'store_goods_sku sgs', 'sku.sku_id=sgs.sku_id and sgs.store_id=' . $this->store_id, 'inner' ],
];
$stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
if ($stock_store_id == $this->store_id) {
$sku_filed .= ', sgs.stock, sgs.real_stock';
} else {
$join[] = [ 'store_goods_sku sgs2', 'sku.sku_id = sgs2.sku_id and sgs2.store_id=' . $stock_store_id, 'left' ];
$sku_filed .= ', sgs2.stock,sgs2.real_stock';
}
$goods_model = new GoodsModel();
$sku_info = $goods_model->getGoodsSkuInfo($condition, $sku_filed, 'sku', $join);
return $this->response($sku_info);
}
}