jh-admin/addon/store/model/StoreGoods.php

196 lines
7.0 KiB
PHP

<?php
/**
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.gobuysaas.com
* =========================================================
*/
namespace addon\store\model;
use app\model\BaseModel;
use app\model\storegoods\StoreGoods as StoreGoodsModel;
class StoreGoods extends BaseModel
{
/****
* 添加门店商品
* @param $data
* @return array
*/
public function StoreGoodsAdd($data)
{
$sale_store = $data['sale_store'];
$site_id = $data['site_id'];
$goods_id = $data['goods_id'];
$goods_stock_model = new \app\model\stock\GoodsStock();
if ($sale_store != 'all') {
$store_type = $data['store_type'] ?? 'directsale';
if ($store_type != 'directsale') { //连锁门店自营添加产品
$goods_stock_model->addStoreGoods([$data['sale_store']], $goods_id, $data);
} else { //仅同步自营店铺并且库存为统一库存的
$store_ids = explode(',', $data['sale_store']);
$store_ids = model('store')->getColumn([
['site_id', '=', $site_id],
['store_id', 'in', $store_ids],
['store_type', '=', 'directsale'],
['stock_type', '=', 'all']
], 'store_id');
$goods_stock_model->addStoreGoods($store_ids, $goods_id, $data);
}
} else if ($sale_store == 'all') { //刷新所有自营门店、并且库存为统一库存的
$store_ids = model('store')->getColumn([
['site_id', '=', $site_id],
['store_type', '=', 'directsale'],
['stock_type', '=', 'all']
], 'store_id');
$goods_stock_model->addStoreGoods($store_ids, $goods_id, $data);
}
return $this->success();
}
/***
* 商品添加与编辑实际
* @param $data
* @return array
*/
public function StoreGoodsEditAfter($data)
{
$sale_store = $data['sale_store'];
$site_id = $data['site_id'];
$goods_id = $data['goods_id'];
if ($sale_store != 'all') {
$store_type = $data['store_type'] ?? 'directsale';
if ($store_type != 'directsale') { //连锁门店自营添加产品
$this->storeGoodsStockAndPrice($site_id, [$data['sale_store']], $goods_id, $data);
} else {
$store_ids = explode(',', $data['sale_store']);
$store_ids = model('store')->getColumn([
['site_id', '=', $site_id],
['store_id', 'in', array_values($store_ids)],
['store_type', '=', 'directsale'],
['stock_type', '=', 'all']
], 'store_id');
$this->storeGoodsStockAndPrice($site_id, $store_ids, $goods_id, $data);
}
} else if ($sale_store == 'all') { //刷新所有自营门店、并且库存为统一库存的
$store_ids = model('store')->getColumn([
['site_id', '=', $site_id],
['store_type', '=', 'directsale'],
['stock_type', '=', 'all']
], 'store_id');
$this->storeGoodsStockAndPrice($site_id, $store_ids, $goods_id, $data);
}
return $this->success();
}
/***
* 门店商品库与价格
* @param $site_id
* @param $store_ids
* @param $goods_id
* @param $data
* @return array
*/
private function storeGoodsStockAndPrice($site_id, $store_ids, $goods_id, $data)
{
$goods_stock_model = new \app\model\stock\GoodsStock();
$StoreGoodsModel = new StoreGoodsModel();
foreach ($store_ids as $store_id) {
$goods_stock_model->checkExistGoodsSku(['goods_id' => $goods_id]);//检查门店
$goods_stock_model->changeGoodsStock([ //刷新门店库存
'site_id' => $site_id,
'store_id' => $store_id,
'goods_class' => $data['goods_class'],
'goods_sku_list' => $data['goods_sku_list']
]);
$StoreGoodsModel->setSkuPrice([//刷新产品价格
'goods_id' => $goods_id,
'site_id' => $site_id,
'store_id' => $store_id,
'goods_sku_list' => $data['goods_sku_list']
]);
}
return $this->success();
}
/**
* 门店详情
* @param $condition
* @param string $fields
* @return array
*/
public function getStoreGoodsInfo($condition, $fields = '*')
{
$info = model('store_goods')->getInfo($condition, $fields);
if (!empty($info)) {
if (isset($info['stock'])) {
$info['stock'] = numberFormat($info['stock']);
}
if (isset($info['sale_num'])) {
$info['sale_num'] = numberFormat($info['sale_num']);
}
if (isset($info['real_stock'])) {
$info['real_stock'] = numberFormat($info['real_stock']);
}
}
return $this->success($info);
}
/**
* 获取门店商品列表
* @param array $condition
* @param string $field
* @param string $order
* @param string $limit
*/
public function getStoreGoodsList($condition = [], $field = '*', $order = '', $limit = null)
{
$list = model('store_goods')->getList($condition, $field, $order, '', '', '', $limit);
foreach ($list as &$v) {
$v['store_goods_skus'] = model('store_goods_sku')->getList([
['store_id', '=', $v['store_id']],
['goods_id', '=', $v['goods_id']]
], 'sku_id,goods_id,stock,sale_num');
if (isset($v['stock'])) {
$v['stock'] = numberFormat($v['stock']);
}
if (isset($v['sale_num'])) {
$v['sale_num'] = numberFormat($v['sale_num']);
}
if (isset($v['real_stock'])) {
$v['real_stock'] = numberFormat($v['real_stock']);
}
}
return $this->success($list);
}
/**
* 获取商品分页列表
* @param array $condition
* @param number $page
* @param string $page_size
* @param string $order
* @param string $field
*/
public function getGoodsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $store_id, $order = 'g.create_time desc', $field = '*')
{
$alias = 'g';
$join = [
[
'store_goods sg',
'sg.goods_id = g.goods_id and (sg.store_id is null or sg.store_id = ' . $store_id . ')',
'left'
]
];
$field = 'g.*,sg.stock,sg.sale_num';
$list = model('goods')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
return $this->success($list);
}
}