jh-admin/addon/store/shopapi/controller/Goods.php

150 lines
6.1 KiB
PHP

<?php
/**
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.gobuysaas.com
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
* =========================================================
*/
namespace addon\store\shopapi\controller;
use app\model\goods\GoodsCategory;
use app\model\goods\Goods as GoodsModel;
use addon\store\model\StoreGoods;
use addon\store\model\StoreGoodsSku;
/**
* 活动管理控制器
* Class Activity
* @package addon\shop\siteapi\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, '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_state = $this->params[ 'goods_state' ] ?? 'all';
$is_virtual = $this->params[ 'is_virtual' ] ?? 0;
$model = new GoodsModel();
$condition = [
[ 'g.site_id', '=', $this->site_id ],
[ 'g.is_delete', '=', 0 ],
[ 'g.is_virtual', '=', $is_virtual ]
];
if ($goods_state !== 'all') {
$condition[] = [ 'g.goods_state', '=', $goods_state ];
}
if ($goods_category != 'all') $condition[] = [ 'g.category_id', 'like', "%,{$goods_category},%" ];
if ($search_text != '') $condition[] = [ 'g.goods_name', 'like', "%{$search_text}%" ];
$field = 'g.goods_id,g.goods_name,g.introduction,g.goods_image,g.goods_state,g.sku_id,g.price,gs.discount_price,g.goods_spec_format';
$data = $model->getGoodsPageList($condition, $page_index, $page_size, 'g.sort asc,g.create_time desc', $field, 'g', [ ['goods_sku gs', 'gs.sku_id = g.sku_id', 'left'] ]);
if (!empty($data[ 'data' ][ 'list' ])) {
$store_goods_model = new StoreGoods();
foreach ($data[ 'data' ][ 'list' ] as $k => $item) {
$store_goods = $store_goods_model->getStoreGoodsInfo([ [ 'store_id', '=', $this->store_id ], [ 'goods_id', '=', $item[ 'goods_id' ] ] ], 'store_goods_stock')[ 'data' ];
$data[ 'data' ][ 'list' ][ $k ][ 'stock' ] = $store_goods[ 'store_goods_stock' ] ?? 0;
}
}
return $this->response($data);
}
/**
* 商品详情
* @return false|string
*/
public function detail()
{
$goods_id = $this->params[ 'goods_id' ] ?? 0;
$goods_model = new GoodsModel();
$field = 'goods_id, goods_name, introduction, goods_class_name, goods_image, goods_state, sku_id, price, unit, cost_price, category_id, brand_name';
$goods_info = $goods_model->getGoodsInfo([ [ 'goods_id', '=', $goods_id ], [ 'site_id', '=', $this->site_id ] ], $field)[ 'data' ];
if (empty($goods_info)) return $this->response($goods_model->error(null, '商品信息缺失'));
//查询商品规格
$sku_filed = 'sku_id,sku_name,sku_no,price,discount_price,cost_price,sku_image,sku_images,spec_name';
$goods_info[ 'sku_list' ] = $goods_model->getGoodsSkuList([ [ 'goods_id', '=', $goods_id ], [ 'site_id', '=', $this->site_id ] ], $sku_filed)[ 'data' ];
if (!empty($goods_info[ 'sku_list' ])) {
$store_goods_model = new StoreGoodsSku();
foreach ($goods_info[ 'sku_list' ] as $k => $item) {
$store_goods = $store_goods_model->getStoreGoodsSkuInfo([ [ 'store_id', '=', $this->store_id ], [ 'sku_id', '=', $item[ 'sku_id' ] ] ], 'store_stock')[ 'data' ];
$goods_info[ 'sku_list' ][ $k ][ 'stock' ] = $store_goods[ 'store_stock' ] ?? 0;
}
}
return $this->response($goods_model->success($goods_info));
}
/**
* 规格列表
* @return false|string
*/
public function skuList()
{
$goods_id = $this->params[ 'goods_id' ];
$condition = [
[ 'site_id', '=', $this->site_id ],
[ 'goods_id', '=', $goods_id ]
];
$model = new GoodsModel();
$data = $model->getGoodsSkuList($condition, 'goods_id,sku_id,site_id,sku_name as goods_name,sku_image as goods_image,discount_price as price,unit,goods_spec_format', '');
if (!empty($data)) {
$store_goods_model = new StoreGoodsSku();
foreach ($data[ 'data' ] as $k => $item) {
$store_goods = $store_goods_model->getStoreGoodsSkuInfo([ [ 'store_id', '=', $this->store_id ], [ 'sku_id', '=', $item[ 'sku_id' ] ] ], 'store_stock')[ 'data' ];
$data[ 'data' ][ $k ][ 'stock' ] = $store_goods[ 'store_stock' ] ?? 0;
$data[ 'data' ][ $k ][ 'store_id' ] = $this->store_id;
}
}
return $this->response($data);
}
/**
* 规格详情
* @return false|string
*/
public function skuInfo()
{
$goods_id = $this->params[ 'goods_id' ] ?? 0;
$sku_id = $this->params[ 'sku_id' ] ?? 0;
$goods_model = new StoreGoods();
$sku_info = $goods_model->getStoreGoodsSkuInfo([
[ 'gsd.goods_id', '=', $goods_id ],
[ 'gsd.sku_id', '=', $sku_id ],
], 'gs.*,osgs.stock,osgs.sale_stock', $this->store_id);
return $this->response($this->success($sku_info));
}
}