<?php
namespace app\store\service;
use think\Db;
class GoodsService
{
public static function buildGoodsList(&$goodsList)
{
$cateField = 'id,pid,cate_title,cate_desc';
$cateWhere = ['status' => '1', 'is_deleted' => '0'];
$cateList = Db::name('StoreGoodsCate')->where($cateWhere)->order('sort asc,id desc')->column($cateField);
$brandWhere = ['status' => '1', 'is_deleted' => '0'];
$brandField = 'id,brand_logo,brand_cover,brand_title,brand_desc,brand_detail';
$brandList = Db::name('StoreGoodsBrand')->where($brandWhere)->order('sort asc,id desc')->column($brandField);
if (empty($goodsList)) {
return ['list' => $goodsList, 'cate' => $cateList, 'brand' => $brandList];
}
$specWhere = [['status', 'eq', '1'], ['is_deleted', 'eq', '0'], ['goods_id', 'in', array_column($goodsList, 'id')]];
$specField = 'id,goods_id,goods_spec,goods_number,market_price,selling_price,goods_stock,goods_sale';
$specList = Db::name('StoreGoodsList')->where($specWhere)->column($specField);
foreach ($specList as $key => $spec) {
foreach ($goodsList as $goods) {
if ($goods['id'] === $spec['goods_id']) {
$specList[$key]['goods_title'] = $goods['goods_title'];
}
}
if ($spec['goods_spec'] === 'default:default') {
$specList[$key]['goods_spec_alias'] = '<span class="color-desc">默认规格</span>';
} else {
$specList[$key]['goods_spec_alias'] = str_replace([':', ','], [': ', ', '], $spec['goods_spec']);
}
}
foreach ($goodsList as $key => $vo) {
$goodsList[$key]['goods_content'] = $vo['goods_content'];
$goodsList[$key]['brand'] = isset($brandList[$vo['brand_id']]) ? $brandList[$vo['brand_id']] : [];
$goodsList[$key]['cate'] = [];
if (isset($cateList[$vo['cate_id']])) {
$goodsList[$key]['cate'][] = ($tcate = $cateList[$vo['cate_id']]);
while (isset($tcate['pid']) && $tcate['pid'] > 0 && isset($cateList[$tcate['pid']])) {
$goodsList[$key]['cate'][] = ($tcate = $cateList[$tcate['pid']]);
}
$goodsList[$key]['cate'] = array_reverse($goodsList[$key]['cate']);
}
$goodsList[$key]['spec'] = [];
foreach ($specList as $spec) {
if ($vo['id'] === $spec['goods_id']) {
$goodsList[$key]['spec'][] = $spec;
}
}
}
return ['list' => $goodsList, 'cate' => $cateList, 'brand' => $brandList];
}
public static function syncGoodsStock($goods_id)
{
$map = ['id' => $goods_id, 'is_deleted' => '0'];
if (!($goods = Db::name('StoreGoods')->where($map)->find())) {
return ['code' => 0, 'msg' => '指定商品信息无法同步库存!'];
}
$stockField = 'goods_id,goods_spec,ifnull(sum(goods_stock), 0) goods_stock';
$stockWhere = ['status' => '1', 'is_deleted' => '0', 'goods_id' => $goods_id];
$stockList = (array)Db::name('StoreGoodsStock')->field($stockField)->where($stockWhere)->group('goods_id,goods_spec')->select();
$saleField = 'goods_id,goods_spec,ifnull(sum(number), 0) goods_sale';
$saleWhere = ['status' => '1', 'is_deleted' => '0', 'goods_id' => $goods_id];
$saleList = (array)Db::name('StoreOrderGoods')->field($saleField)->where($saleWhere)->group('goods_id,goods_spec')->select();
list($where, $total_sale) = [['goods_id' => $goods_id], 0];
Db::name('StoreGoodsList')->where($where)->update(['goods_stock' => 0, 'goods_sale' => 0]);
foreach ($stockList as $stock) {
$where = ['goods_id' => $goods_id, 'goods_spec' => $stock['goods_spec']];
Db::name('StoreGoodsList')->where($where)->update(['goods_stock' => $stock['goods_stock']]);
}
foreach ($saleList as $sale) {
$total_sale += intval($sale['goods_sale']);
$where = ['goods_id' => $goods_id, 'goods_spec' => $sale['goods_spec']];
Db::name('StoreGoodsList')->where($where)->update(['goods_sale' => $sale['goods_sale']]);
}
return ['code' => 1, 'msg' => '同步商品库存成功!'];
}
}