2020-07-13 21:33:55 +08:00
|
|
|
|
<?php
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | ShopXO 国内领先企业级B2C免费开源电商系统
|
|
|
|
|
// +----------------------------------------------------------------------
|
2021-03-16 10:34:52 +08:00
|
|
|
|
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
|
2020-07-13 21:33:55 +08:00
|
|
|
|
// +----------------------------------------------------------------------
|
2021-03-16 10:34:52 +08:00
|
|
|
|
// | Licensed ( https://opensource.org/licenses/mit-license.php )
|
2020-07-13 21:33:55 +08:00
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | Author: Devil
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
namespace app\service;
|
|
|
|
|
|
2021-07-18 23:42:10 +08:00
|
|
|
|
use think\facade\Db;
|
2020-07-13 21:33:55 +08:00
|
|
|
|
use app\service\ResourcesService;
|
|
|
|
|
use app\service\GoodsService;
|
2023-02-08 21:20:04 +08:00
|
|
|
|
use app\service\GoodsCategoryService;
|
2020-08-18 19:39:06 +08:00
|
|
|
|
use app\service\WarehouseService;
|
2020-07-13 21:33:55 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 仓库商品服务层
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
2020-07-16 22:34:56 +08:00
|
|
|
|
* @date 2020-07-11
|
2020-07-13 21:33:55 +08:00
|
|
|
|
* @desc description
|
|
|
|
|
*/
|
|
|
|
|
class WarehouseGoodsService
|
|
|
|
|
{
|
2020-07-16 22:34:56 +08:00
|
|
|
|
/**
|
|
|
|
|
* 数据处理
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-07-16
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $data [需要处理的数据]
|
|
|
|
|
*/
|
2022-08-02 17:23:10 +08:00
|
|
|
|
public static function WarehouseGoodsListHandle($data)
|
2020-07-16 22:34:56 +08:00
|
|
|
|
{
|
2020-07-13 21:33:55 +08:00
|
|
|
|
if(!empty($data))
|
|
|
|
|
{
|
|
|
|
|
// 字段列表
|
|
|
|
|
$keys = ArrayKeys($data);
|
|
|
|
|
|
|
|
|
|
// 获取商品信息
|
|
|
|
|
if(in_array('goods_id', $keys))
|
|
|
|
|
{
|
|
|
|
|
$goods_params = [
|
|
|
|
|
'where' => [
|
2022-04-22 11:31:12 +08:00
|
|
|
|
['id', 'in', array_unique(array_column($data, 'goods_id'))],
|
2022-04-18 00:54:11 +08:00
|
|
|
|
['is_delete_time', '=', 0],
|
2020-07-13 21:33:55 +08:00
|
|
|
|
],
|
|
|
|
|
'field' => 'id,title,images,price,min_price',
|
2020-08-16 20:17:58 +08:00
|
|
|
|
'm' => 0,
|
|
|
|
|
'n' => 0,
|
2020-07-13 21:33:55 +08:00
|
|
|
|
];
|
|
|
|
|
$ret = GoodsService::GoodsList($goods_params);
|
|
|
|
|
$goods = [];
|
|
|
|
|
if(!empty($ret['data']))
|
|
|
|
|
{
|
|
|
|
|
foreach($ret['data'] as $g)
|
|
|
|
|
{
|
|
|
|
|
$goods[$g['id']] = $g;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 仓库名称
|
|
|
|
|
if(in_array('warehouse_id', $keys))
|
|
|
|
|
{
|
2020-08-18 19:39:06 +08:00
|
|
|
|
$warehouse = [];
|
|
|
|
|
$w_ret = WarehouseService::WarehouseIdsAllList(array_column($data, 'warehouse_id'));
|
|
|
|
|
if(!empty($w_ret['data']))
|
|
|
|
|
{
|
|
|
|
|
foreach($w_ret['data'] as $wv)
|
|
|
|
|
{
|
|
|
|
|
$warehouse[$wv['id']] = $wv['name'];
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-13 21:33:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-28 23:19:51 +08:00
|
|
|
|
// 商品规格库存
|
|
|
|
|
$spec_inventory = [];
|
|
|
|
|
$temp_inventory = Db::name('WarehouseGoodsSpec')->where(['warehouse_goods_id'=>array_column($data, 'id')])->field('warehouse_goods_id,inventory,spec')->select()->toArray();
|
|
|
|
|
if(!empty($temp_inventory))
|
|
|
|
|
{
|
|
|
|
|
foreach($temp_inventory as $iv)
|
|
|
|
|
{
|
|
|
|
|
$temp = empty($iv['spec']) ? [] : json_decode($iv['spec'], true);
|
|
|
|
|
$iv['spec'] = (!empty($temp) && is_array($temp)) ? implode(' / ', array_column($temp, 'value')) : '';
|
|
|
|
|
if(!array_key_exists($iv['warehouse_goods_id'], $spec_inventory))
|
|
|
|
|
{
|
|
|
|
|
$spec_inventory[$iv['warehouse_goods_id']] = [];
|
|
|
|
|
}
|
|
|
|
|
$spec_inventory[$iv['warehouse_goods_id']][] = $iv;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 21:33:55 +08:00
|
|
|
|
// 数据处理
|
|
|
|
|
foreach($data as &$v)
|
|
|
|
|
{
|
|
|
|
|
// 商品信息
|
2022-06-28 23:19:51 +08:00
|
|
|
|
if(array_key_exists('goods_id', $v))
|
2020-07-13 21:33:55 +08:00
|
|
|
|
{
|
|
|
|
|
$v['goods'] = isset($goods[$v['goods_id']]) ? $goods[$v['goods_id']] : [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 仓库
|
2022-06-28 23:19:51 +08:00
|
|
|
|
if(array_key_exists('warehouse_id', $v))
|
2020-07-13 21:33:55 +08:00
|
|
|
|
{
|
|
|
|
|
$v['warehouse_name'] = isset($warehouse[$v['warehouse_id']]) ? $warehouse[$v['warehouse_id']] : '';
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-28 23:19:51 +08:00
|
|
|
|
// 规格
|
|
|
|
|
$v['spec'] = (empty($spec_inventory) || !array_key_exists($v['id'], $spec_inventory)) ? [] : $spec_inventory[$v['id']];
|
|
|
|
|
|
2020-07-13 21:33:55 +08:00
|
|
|
|
// 时间
|
2022-06-28 23:19:51 +08:00
|
|
|
|
if(array_key_exists('add_time', $v))
|
2020-07-13 21:33:55 +08:00
|
|
|
|
{
|
|
|
|
|
$v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
|
|
|
|
|
}
|
2022-06-28 23:19:51 +08:00
|
|
|
|
if(array_key_exists('upd_time', $v))
|
2020-07-13 21:33:55 +08:00
|
|
|
|
{
|
|
|
|
|
$v['upd_time'] = empty($v['upd_time']) ? '' : date('Y-m-d H:i:s', $v['upd_time']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-16 22:34:56 +08:00
|
|
|
|
return $data;
|
2020-07-13 21:33:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
2020-07-16 22:34:56 +08:00
|
|
|
|
* @date 2018-07-11
|
2020-07-13 21:33:55 +08:00
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
|
*/
|
|
|
|
|
public static function WarehouseGoodsDelete($params = [])
|
|
|
|
|
{
|
|
|
|
|
// 参数是否有误
|
|
|
|
|
if(empty($params['ids']))
|
|
|
|
|
{
|
2023-02-04 16:47:48 +08:00
|
|
|
|
return DataReturn(MyLang('data_id_error_tips'), -1);
|
2020-07-13 21:33:55 +08:00
|
|
|
|
}
|
|
|
|
|
// 是否数组
|
|
|
|
|
if(!is_array($params['ids']))
|
|
|
|
|
{
|
|
|
|
|
$params['ids'] = explode(',', $params['ids']);
|
|
|
|
|
}
|
2023-02-11 17:01:04 +08:00
|
|
|
|
// 获取数据
|
|
|
|
|
$warehouse_goods = Db::name('WarehouseGoods')->where(['id'=>$params['ids']])->column('*', 'id');
|
|
|
|
|
if(empty($warehouse_goods))
|
|
|
|
|
{
|
|
|
|
|
return DataReturn(MyLang('data_no_exist_error_tips'), -1);
|
|
|
|
|
}
|
2020-07-13 21:33:55 +08:00
|
|
|
|
|
2020-07-16 23:56:16 +08:00
|
|
|
|
// 启动事务
|
|
|
|
|
Db::startTrans();
|
2023-02-11 17:01:04 +08:00
|
|
|
|
// 捕获异常
|
|
|
|
|
try {
|
|
|
|
|
// 循环处理删除
|
|
|
|
|
foreach($params['ids'] as $k=>$id)
|
2020-07-16 23:56:16 +08:00
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
if(array_key_exists($id, $warehouse_goods))
|
2020-07-16 23:56:16 +08:00
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
// 位置
|
|
|
|
|
$index = $k+1;
|
|
|
|
|
|
|
|
|
|
// 删除仓库商品和仓库商品规格数据
|
|
|
|
|
$temp = $warehouse_goods[$id];
|
|
|
|
|
$where = [
|
|
|
|
|
'goods_id' => $temp['goods_id'],
|
|
|
|
|
'warehouse_id' => $temp['warehouse_id'],
|
|
|
|
|
];
|
|
|
|
|
if(!Db::name('WarehouseGoods')->where($where)->delete() && Db::name('WarehouseGoodsSpec')->where($where)->delete())
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception(MyLang('common_service.warehousegoods.row_delete_fail_tips', ['index'=>$index]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 同步商品库存
|
|
|
|
|
$ret = self::GoodsSpecInventorySync($temp['goods_id']);
|
|
|
|
|
if($ret['code'] != 0)
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception($ret['msg']);
|
|
|
|
|
}
|
2020-07-16 23:56:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-11 17:01:04 +08:00
|
|
|
|
// 提交事务
|
|
|
|
|
Db::commit();
|
|
|
|
|
return DataReturn(MyLang('delete_success'), 0);
|
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
|
Db::rollback();
|
|
|
|
|
return DataReturn($e->getMessage(), -1);
|
2020-07-13 21:33:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 状态更新
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
2020-07-16 22:34:56 +08:00
|
|
|
|
* @date 2020-07-11
|
2020-07-13 21:33:55 +08:00
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
|
*/
|
|
|
|
|
public static function WarehouseGoodsStatusUpdate($params = [])
|
|
|
|
|
{
|
|
|
|
|
// 请求参数
|
|
|
|
|
$p = [
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'id',
|
2023-02-04 16:47:48 +08:00
|
|
|
|
'error_msg' => MyLang('data_id_error_tips'),
|
2020-07-13 21:33:55 +08:00
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'field',
|
2023-02-06 18:30:08 +08:00
|
|
|
|
'error_msg' => MyLang('operate_field_error_tips'),
|
2020-07-13 21:33:55 +08:00
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'in',
|
|
|
|
|
'key_name' => 'state',
|
|
|
|
|
'checked_data' => [0,1],
|
2023-02-04 16:47:48 +08:00
|
|
|
|
'error_msg' => MyLang('form_status_range_message'),
|
2020-07-13 21:33:55 +08:00
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$ret = ParamsChecked($params, $p);
|
|
|
|
|
if($ret !== true)
|
|
|
|
|
{
|
|
|
|
|
return DataReturn($ret, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 23:56:16 +08:00
|
|
|
|
// 获取数据
|
|
|
|
|
$where = ['id'=>intval($params['id'])];
|
|
|
|
|
$warehouse_goods = Db::name('WarehouseGoods')->where($where)->find();
|
|
|
|
|
if(empty($warehouse_goods))
|
|
|
|
|
{
|
2023-02-04 16:47:48 +08:00
|
|
|
|
return DataReturn(MyLang('data_no_exist_error_tips'), -1);
|
2020-07-16 23:56:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 启动事务
|
|
|
|
|
Db::startTrans();
|
2023-02-11 17:01:04 +08:00
|
|
|
|
// 捕获异常
|
|
|
|
|
try {
|
|
|
|
|
// 数据更新
|
|
|
|
|
if(!Db::name('WarehouseGoods')->where($where)->update([$params['field']=>intval($params['state']), 'upd_time'=>time()]))
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception(MyLang('edit_fail'));
|
|
|
|
|
}
|
2020-07-16 23:56:16 +08:00
|
|
|
|
// 状态更新
|
|
|
|
|
if($params['field'] == 'is_enable')
|
|
|
|
|
{
|
|
|
|
|
// 同步商品库存
|
|
|
|
|
$ret = self::GoodsSpecInventorySync($warehouse_goods['goods_id']);
|
|
|
|
|
if($ret['code'] != 0)
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
throw new \Exception($ret['msg']);
|
2020-07-16 23:56:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 提交事务
|
|
|
|
|
Db::commit();
|
2023-01-19 17:44:03 +08:00
|
|
|
|
return DataReturn(MyLang('edit_success'), 0);
|
2023-02-11 17:01:04 +08:00
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
|
Db::rollback();
|
|
|
|
|
return DataReturn($e->getMessage(), -1);
|
2020-07-13 21:33:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-16 22:34:56 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 商品搜索
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-07-13
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
|
*/
|
|
|
|
|
public static function GoodsSearchList($params = [])
|
|
|
|
|
{
|
|
|
|
|
// 请求参数
|
|
|
|
|
$p = [
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'warehouse_id',
|
2023-02-11 17:01:04 +08:00
|
|
|
|
'error_msg' => MyLang('common_service.warehousegoods.warehouse_id_empty_tips'),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$ret = ParamsChecked($params, $p);
|
|
|
|
|
if($ret !== true)
|
|
|
|
|
{
|
|
|
|
|
return DataReturn($ret, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回数据
|
|
|
|
|
$result = [
|
|
|
|
|
'page_total' => 0,
|
|
|
|
|
'page_size' => 20,
|
|
|
|
|
'page' => max(1, isset($params['page']) ? intval($params['page']) : 1),
|
|
|
|
|
'total' => 0,
|
|
|
|
|
'data' => [],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 条件
|
|
|
|
|
$where = [
|
|
|
|
|
['g.is_delete_time', '=', 0],
|
|
|
|
|
['g.is_shelves', '=', 1]
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 关键字
|
|
|
|
|
if(!empty($params['keywords']))
|
|
|
|
|
{
|
|
|
|
|
$where[] = ['g.title', 'like', '%'.$params['keywords'].'%'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 分类id
|
|
|
|
|
if(!empty($params['category_id']))
|
|
|
|
|
{
|
2023-02-08 21:20:04 +08:00
|
|
|
|
$category_ids = GoodsCategoryService::GoodsCategoryItemsIds([$params['category_id']], 1);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
$category_ids[] = $params['category_id'];
|
|
|
|
|
$where[] = ['gci.category_id', 'in', $category_ids];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取商品总数
|
|
|
|
|
$result['total'] = GoodsService::CategoryGoodsTotal($where);
|
|
|
|
|
|
|
|
|
|
// 获取商品列表
|
|
|
|
|
if($result['total'] > 0)
|
|
|
|
|
{
|
|
|
|
|
// 基础参数
|
|
|
|
|
$field = 'g.id,g.title,g.images';
|
|
|
|
|
$order_by = 'g.id desc';
|
|
|
|
|
|
|
|
|
|
// 分页计算
|
|
|
|
|
$m = intval(($result['page']-1)*$result['page_size']);
|
|
|
|
|
$goods = GoodsService::CategoryGoodsList(['where'=>$where, 'm'=>$m, 'n'=>$result['page_size'], 'field'=>$field, 'order_by'=>$order_by]);
|
|
|
|
|
$result['data'] = $goods['data'];
|
|
|
|
|
$result['page_total'] = ceil($result['total']/$result['page_size']);
|
|
|
|
|
// 数据处理
|
|
|
|
|
if(!empty($result['data']) && is_array($result['data']))
|
|
|
|
|
{
|
|
|
|
|
// 获取仓库商品
|
2020-07-17 15:43:13 +08:00
|
|
|
|
$warehouse_goods_ids = Db::name('WarehouseGoods')->where(['warehouse_id'=>intval($params['warehouse_id']), 'goods_id'=>array_column($result['data'], 'id')])->column('goods_id');
|
2020-07-16 22:34:56 +08:00
|
|
|
|
if(!empty($warehouse_goods_ids))
|
|
|
|
|
{
|
|
|
|
|
foreach($result['data'] as &$v)
|
|
|
|
|
{
|
|
|
|
|
// 是否已添加
|
|
|
|
|
$v['is_exist'] = in_array($v['id'], $warehouse_goods_ids) ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-19 17:44:03 +08:00
|
|
|
|
return DataReturn(MyLang('handle_success'), 0, $result);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 仓库商品添加
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-07-14
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
|
*/
|
|
|
|
|
public static function WarehouseGoodsAdd($params = [])
|
|
|
|
|
{
|
|
|
|
|
// 请求参数
|
|
|
|
|
$p = [
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'warehouse_id',
|
2023-02-11 17:01:04 +08:00
|
|
|
|
'error_msg' => MyLang('common_service.warehousegoods.warehouse_id_empty_tips'),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'goods_id',
|
2023-02-06 18:30:08 +08:00
|
|
|
|
'error_msg' => MyLang('goods_id_error_tips'),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$ret = ParamsChecked($params, $p);
|
|
|
|
|
if($ret !== true)
|
|
|
|
|
{
|
|
|
|
|
return DataReturn($ret, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 不存在添加
|
|
|
|
|
$where = [
|
|
|
|
|
'goods_id' => intval($params['goods_id']),
|
|
|
|
|
'warehouse_id' => intval($params['warehouse_id']),
|
|
|
|
|
];
|
|
|
|
|
$warehouse_goods = Db::name('WarehouseGoods')->where($where)->find();
|
|
|
|
|
if(empty($warehouse_goods))
|
|
|
|
|
{
|
|
|
|
|
$data = [
|
|
|
|
|
'warehouse_id' => intval($params['warehouse_id']),
|
|
|
|
|
'goods_id' => intval($params['goods_id']),
|
|
|
|
|
'is_enable' => 1,
|
|
|
|
|
'add_time' => time(),
|
|
|
|
|
];
|
|
|
|
|
if(Db::name('WarehouseGoods')->insertGetId($data) <= 0)
|
|
|
|
|
{
|
2023-01-19 17:44:03 +08:00
|
|
|
|
return DataReturn(MyLang('insert_fail'), -100);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-19 17:44:03 +08:00
|
|
|
|
return DataReturn(MyLang('insert_success'), 0);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 仓库商品删除
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-07-14
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
|
*/
|
|
|
|
|
public static function WarehouseGoodsDel($params = [])
|
|
|
|
|
{
|
|
|
|
|
// 请求参数
|
|
|
|
|
$p = [
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'warehouse_id',
|
2023-02-11 17:01:04 +08:00
|
|
|
|
'error_msg' => MyLang('common_service.warehousegoods.warehouse_id_empty_tips'),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'goods_id',
|
2023-02-06 18:30:08 +08:00
|
|
|
|
'error_msg' => MyLang('goods_id_error_tips'),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$ret = ParamsChecked($params, $p);
|
|
|
|
|
if($ret !== true)
|
|
|
|
|
{
|
|
|
|
|
return DataReturn($ret, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 23:56:16 +08:00
|
|
|
|
// 启动事务
|
|
|
|
|
Db::startTrans();
|
2023-02-11 17:01:04 +08:00
|
|
|
|
// 捕获异常
|
|
|
|
|
try {
|
|
|
|
|
// 删除仓库商品和仓库商品规格数据
|
|
|
|
|
$where = [
|
|
|
|
|
'goods_id' => intval($params['goods_id']),
|
|
|
|
|
'warehouse_id' => intval($params['warehouse_id']),
|
|
|
|
|
];
|
|
|
|
|
if(!Db::name('WarehouseGoods')->where($where)->delete() !== false && Db::name('WarehouseGoodsSpec')->where($where)->delete())
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception(MyLang('delete_fail'));
|
|
|
|
|
}
|
2020-07-16 23:56:16 +08:00
|
|
|
|
// 同步商品库存
|
|
|
|
|
$ret = self::GoodsSpecInventorySync($params['goods_id']);
|
|
|
|
|
if($ret['code'] != 0)
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
throw new \Exception($ret['msg']);
|
2020-07-16 23:56:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 提交事务
|
|
|
|
|
Db::commit();
|
2023-01-19 17:44:03 +08:00
|
|
|
|
return DataReturn(MyLang('delete_success'), 0);
|
2023-02-11 17:01:04 +08:00
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
|
Db::rollback();
|
|
|
|
|
return DataReturn($e->getMessage(), -1);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 仓库商品库存数据
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-07-15
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
|
*/
|
|
|
|
|
public static function WarehouseGoodsInventoryData($params = [])
|
|
|
|
|
{
|
|
|
|
|
// 请求参数
|
|
|
|
|
$p = [
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'id',
|
2023-02-06 18:30:08 +08:00
|
|
|
|
'error_msg' => MyLang('data_id_error_tips'),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$ret = ParamsChecked($params, $p);
|
|
|
|
|
if($ret !== true)
|
|
|
|
|
{
|
|
|
|
|
return DataReturn($ret, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取仓库商品
|
|
|
|
|
$where = [
|
|
|
|
|
'id' => intval($params['id']),
|
|
|
|
|
];
|
|
|
|
|
$warehouse_goods = Db::name('WarehouseGoods')->where($where)->find();
|
|
|
|
|
if(empty($warehouse_goods))
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('data_no_exist_error_tips'), -1);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取商品规格
|
|
|
|
|
$res = GoodsService::GoodsSpecificationsActual($warehouse_goods['goods_id']);
|
|
|
|
|
$inventory_spec = [];
|
|
|
|
|
if(!empty($res['value']) && is_array($res['value']))
|
|
|
|
|
{
|
|
|
|
|
// 获取当前配置的库存
|
2020-07-16 23:56:16 +08:00
|
|
|
|
$spec = array_column($res['value'], 'value');
|
|
|
|
|
foreach($spec as $v)
|
2020-07-16 22:34:56 +08:00
|
|
|
|
{
|
2020-10-12 10:36:57 +08:00
|
|
|
|
$arr = explode(GoodsService::$goods_spec_to_string_separator, $v);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
$inventory_spec[] = [
|
|
|
|
|
'name' => implode(' / ', $arr),
|
2020-07-25 00:13:18 +08:00
|
|
|
|
'spec' => json_encode(self::GoodsSpecMuster($v, $res['title']), JSON_UNESCAPED_UNICODE),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
'md5_key' => md5(implode('', $arr)),
|
|
|
|
|
'inventory' => 0,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-07-17 00:07:12 +08:00
|
|
|
|
// 没有规格则处理默认规格 default
|
2020-07-16 22:34:56 +08:00
|
|
|
|
$str = 'default';
|
|
|
|
|
$inventory_spec[] = [
|
2023-02-09 19:04:35 +08:00
|
|
|
|
'name' => GoodsService::GoodsSpecDefaultName(),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
'spec' => $str,
|
|
|
|
|
'md5_key' => md5($str),
|
|
|
|
|
'inventory' => 0,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取库存
|
|
|
|
|
$keys = array_column($inventory_spec, 'md5_key');
|
|
|
|
|
$where = [
|
|
|
|
|
'md5_key' => $keys,
|
|
|
|
|
'warehouse_goods_id' => $warehouse_goods['id'],
|
|
|
|
|
'warehouse_id' => $warehouse_goods['warehouse_id'],
|
|
|
|
|
'goods_id' => $warehouse_goods['goods_id'],
|
|
|
|
|
];
|
|
|
|
|
$inventory_data = Db::name('WarehouseGoodsSpec')->where($where)->column('inventory', 'md5_key');
|
|
|
|
|
if(!empty($inventory_data))
|
|
|
|
|
{
|
|
|
|
|
foreach($inventory_spec as &$v)
|
|
|
|
|
{
|
|
|
|
|
if(array_key_exists($v['md5_key'], $inventory_data))
|
|
|
|
|
{
|
|
|
|
|
$v['inventory'] = $inventory_data[$v['md5_key']];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回数据
|
|
|
|
|
$result = [
|
|
|
|
|
'data' => $warehouse_goods,
|
|
|
|
|
'spec' => $inventory_spec,
|
|
|
|
|
];
|
2020-07-30 19:31:04 +08:00
|
|
|
|
return DataReturn('success', 0, $result);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 规格值组合
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-07-16
|
|
|
|
|
* @desc description
|
2020-09-09 15:44:46 +08:00
|
|
|
|
* @param [string] $spec_str [规格字符串,英文逗号分割]
|
2020-07-16 22:34:56 +08:00
|
|
|
|
* @param [array] $spec_title [规格类型名称]
|
|
|
|
|
*/
|
|
|
|
|
public static function GoodsSpecMuster($spec_str, $spec_title)
|
|
|
|
|
{
|
|
|
|
|
$result = [];
|
2020-10-12 10:36:57 +08:00
|
|
|
|
$arr = explode(GoodsService::$goods_spec_to_string_separator, $spec_str);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
if(count($arr) == count($spec_title))
|
|
|
|
|
{
|
|
|
|
|
foreach($arr as $k=>$v)
|
|
|
|
|
{
|
|
|
|
|
$result[] = [
|
|
|
|
|
'type' => $spec_title[$k],
|
|
|
|
|
'value' => $v,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 仓库商品库存保存
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-07-15
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [array] $params [输入参数]
|
|
|
|
|
*/
|
|
|
|
|
public static function WarehouseGoodsInventorySave($params = [])
|
|
|
|
|
{
|
|
|
|
|
// 请求参数
|
|
|
|
|
$p = [
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'empty',
|
|
|
|
|
'key_name' => 'id',
|
2023-02-06 18:30:08 +08:00
|
|
|
|
'error_msg' => MyLang('data_id_error_tips'),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'is_array',
|
|
|
|
|
'key_name' => 'specifications_inventory',
|
2023-02-11 17:01:04 +08:00
|
|
|
|
'error_msg' => MyLang('common_service.warehousegoods.save_inventory_data_error_tips'),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'is_array',
|
|
|
|
|
'key_name' => 'specifications_md5_key',
|
2023-02-11 17:01:04 +08:00
|
|
|
|
'error_msg' => MyLang('common_service.warehousegoods.save_md5_key_data_error_tips'),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'checked_type' => 'is_array',
|
|
|
|
|
'key_name' => 'specifications_spec',
|
2023-02-11 17:01:04 +08:00
|
|
|
|
'error_msg' => MyLang('common_service.warehousegoods.save_spec_data_error_tips'),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$ret = ParamsChecked($params, $p);
|
|
|
|
|
if($ret !== true)
|
|
|
|
|
{
|
|
|
|
|
return DataReturn($ret, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取仓库商品
|
2023-02-11 17:01:04 +08:00
|
|
|
|
$warehouse_goods = Db::name('WarehouseGoods')->where(['id'=>intval($params['id'])])->find();
|
2020-07-16 22:34:56 +08:00
|
|
|
|
if(empty($warehouse_goods))
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('data_no_exist_error_tips'), -1);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 数据组装
|
|
|
|
|
$inventory = [];
|
|
|
|
|
$spec_value = [];
|
|
|
|
|
$md5_key = [];
|
|
|
|
|
$data = [];
|
|
|
|
|
foreach($params['specifications_spec'] as $k=>$v)
|
|
|
|
|
{
|
|
|
|
|
// 规格值,md5key,库存 必须存在
|
|
|
|
|
if(!empty($v) && array_key_exists($k, $params['specifications_md5_key']) && array_key_exists($k, $params['specifications_inventory']))
|
|
|
|
|
{
|
|
|
|
|
$inventory = intval($params['specifications_inventory'][$k]);
|
|
|
|
|
if($inventory > 0)
|
|
|
|
|
{
|
|
|
|
|
$data[] = [
|
|
|
|
|
'warehouse_goods_id' => $warehouse_goods['id'],
|
|
|
|
|
'warehouse_id' => $warehouse_goods['warehouse_id'],
|
|
|
|
|
'goods_id' => $warehouse_goods['goods_id'],
|
|
|
|
|
'md5_key' => $params['specifications_md5_key'][$k],
|
|
|
|
|
'spec' => htmlspecialchars_decode($v),
|
|
|
|
|
'inventory' => $inventory,
|
|
|
|
|
'add_time' => time(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 启动事务
|
|
|
|
|
Db::startTrans();
|
2020-12-01 15:01:46 +08:00
|
|
|
|
// 捕获异常
|
|
|
|
|
try {
|
2020-07-16 22:34:56 +08:00
|
|
|
|
|
2020-12-01 15:01:46 +08:00
|
|
|
|
// 删除原始数据
|
|
|
|
|
$where = [
|
|
|
|
|
'warehouse_id' => $warehouse_goods['warehouse_id'],
|
|
|
|
|
'goods_id' => $warehouse_goods['goods_id'],
|
|
|
|
|
];
|
|
|
|
|
Db::name('WarehouseGoodsSpec')->where($where)->delete();
|
2020-07-16 22:34:56 +08:00
|
|
|
|
|
2020-12-01 15:01:46 +08:00
|
|
|
|
// 添加数据
|
|
|
|
|
if(!empty($data))
|
|
|
|
|
{
|
|
|
|
|
if(Db::name('WarehouseGoodsSpec')->insertAll($data) < count($data))
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
throw new \Exception(MyLang('common_service.warehousegoods.save_inventory_spec_insert_fail_tips'));
|
2020-12-01 15:01:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-16 22:34:56 +08:00
|
|
|
|
|
2020-12-01 15:01:46 +08:00
|
|
|
|
// 仓库商品更新
|
|
|
|
|
if(!Db::name('WarehouseGoods')->where(['id'=>$warehouse_goods['id']])->update([
|
|
|
|
|
'inventory' => array_sum(array_column($data, 'inventory')),
|
|
|
|
|
'upd_time' => time(),
|
|
|
|
|
]))
|
2020-07-16 22:34:56 +08:00
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
throw new \Exception(MyLang('common_service.warehousegoods.save_warehouse_goods_update_fail_tips'));
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 15:01:46 +08:00
|
|
|
|
// 同步商品库存
|
|
|
|
|
$ret = self::GoodsSpecInventorySync($warehouse_goods['goods_id']);
|
|
|
|
|
if($ret['code'] != 0)
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception($ret['msg']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 完成
|
|
|
|
|
Db::commit();
|
2023-01-19 17:44:03 +08:00
|
|
|
|
return DataReturn(MyLang('operate_success'), 0);
|
2020-12-01 15:01:46 +08:00
|
|
|
|
} catch(\Exception $e) {
|
2020-07-16 23:56:16 +08:00
|
|
|
|
Db::rollback();
|
2020-12-01 15:01:46 +08:00
|
|
|
|
return DataReturn($e->getMessage(), -1);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
2020-07-16 23:56:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-11 23:26:06 +08:00
|
|
|
|
/**
|
|
|
|
|
* 商品改变规格仓库商品库存同步
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-07-16
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [int] $goods_id [商品id]
|
|
|
|
|
*/
|
|
|
|
|
public static function GoodsSpecChangeInventorySync($goods_id)
|
|
|
|
|
{
|
|
|
|
|
// 获取商品实际规格
|
|
|
|
|
$res = GoodsService::GoodsSpecificationsActual($goods_id);
|
|
|
|
|
if(empty($res['value']))
|
|
|
|
|
{
|
|
|
|
|
// 没有规格则读取默认规格数据
|
|
|
|
|
$res['value'][] = [
|
|
|
|
|
'base_id' => Db::name('GoodsSpecBase')->where(['goods_id'=>$goods_id])->value('id'),
|
|
|
|
|
'value' => 'default',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取商品仓库
|
2021-07-18 23:42:10 +08:00
|
|
|
|
$warehouse_goods = Db::name('WarehouseGoods')->where(['goods_id'=>$goods_id])->select()->toArray();
|
2020-10-11 23:26:06 +08:00
|
|
|
|
if(!empty($warehouse_goods))
|
|
|
|
|
{
|
|
|
|
|
// 循环根据仓库处理库存
|
|
|
|
|
foreach($warehouse_goods as $wg)
|
|
|
|
|
{
|
|
|
|
|
$data = [];
|
|
|
|
|
foreach($res['value'] as $v)
|
|
|
|
|
{
|
2020-10-12 10:36:57 +08:00
|
|
|
|
$md5_key = md5(empty($v['value']) ? 'default' : str_replace(GoodsService::$goods_spec_to_string_separator, '', $v['value']));
|
2020-12-01 15:01:46 +08:00
|
|
|
|
$inventory = (int) Db::name('WarehouseGoodsSpec')->where([
|
|
|
|
|
'warehouse_id' => $wg['warehouse_id'],
|
|
|
|
|
'goods_id' => $wg['goods_id'],
|
|
|
|
|
'md5_key' => $md5_key,
|
|
|
|
|
])->value('inventory');
|
2020-10-11 23:26:06 +08:00
|
|
|
|
$data[] = [
|
|
|
|
|
'warehouse_goods_id' => $wg['id'],
|
|
|
|
|
'warehouse_id' => $wg['warehouse_id'],
|
|
|
|
|
'goods_id' => $wg['goods_id'],
|
|
|
|
|
'md5_key' => $md5_key,
|
|
|
|
|
'spec' => json_encode(self::GoodsSpecMuster($v['value'], $res['title']), JSON_UNESCAPED_UNICODE),
|
|
|
|
|
'inventory' => $inventory,
|
|
|
|
|
'add_time' => time(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除原始数据
|
|
|
|
|
$where = [
|
2020-12-01 15:01:46 +08:00
|
|
|
|
'warehouse_id' => $wg['warehouse_id'],
|
|
|
|
|
'goods_id' => $wg['goods_id'],
|
2020-10-11 23:26:06 +08:00
|
|
|
|
];
|
|
|
|
|
Db::name('WarehouseGoodsSpec')->where($where)->delete();
|
|
|
|
|
|
|
|
|
|
// 添加数据
|
|
|
|
|
if(Db::name('WarehouseGoodsSpec')->insertAll($data) < count($data))
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.save_inventory_spec_insert_fail_tips'), -1);
|
2020-10-11 23:26:06 +08:00
|
|
|
|
}
|
2020-12-01 15:01:46 +08:00
|
|
|
|
|
|
|
|
|
// 仓库商品更新
|
|
|
|
|
if(!Db::name('WarehouseGoods')->where(['id'=>$wg['id']])->update([
|
|
|
|
|
'inventory' => array_sum(array_column($data, 'inventory')),
|
|
|
|
|
'upd_time' => time(),
|
|
|
|
|
]))
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.save_warehouse_goods_update_fail_tips'), -1);
|
2020-12-01 15:01:46 +08:00
|
|
|
|
}
|
2020-10-11 23:26:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 15:01:46 +08:00
|
|
|
|
// 商品库存同步
|
|
|
|
|
return self::GoodsSpecInventorySync($goods_id);
|
2020-10-11 23:26:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 23:56:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 商品规格库存同步
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-07-16
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [int] $goods_id [商品id]
|
|
|
|
|
*/
|
|
|
|
|
public static function GoodsSpecInventorySync($goods_id)
|
|
|
|
|
{
|
|
|
|
|
// 获取商品实际规格
|
|
|
|
|
$res = GoodsService::GoodsSpecificationsActual($goods_id);
|
|
|
|
|
if(empty($res['value']))
|
2020-07-16 22:34:56 +08:00
|
|
|
|
{
|
2020-07-17 00:07:12 +08:00
|
|
|
|
// 没有规格则读取默认规格数据
|
|
|
|
|
$res['value'][] = [
|
|
|
|
|
'base_id' => Db::name('GoodsSpecBase')->where(['goods_id'=>$goods_id])->value('id'),
|
|
|
|
|
'value' => 'default',
|
|
|
|
|
];
|
2020-07-16 23:56:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 商品规格库存
|
2023-02-11 17:01:04 +08:00
|
|
|
|
$inventory_total = 0;
|
2020-07-16 23:56:16 +08:00
|
|
|
|
foreach($res['value'] as $v)
|
|
|
|
|
{
|
2020-10-12 10:36:57 +08:00
|
|
|
|
$inventory = self::WarehouseGoodsSpecInventory($goods_id, str_replace(GoodsService::$goods_spec_to_string_separator, '', $v['value']));
|
2020-07-16 23:56:16 +08:00
|
|
|
|
if(Db::name('GoodsSpecBase')->where(['id'=>$v['base_id'], 'goods_id'=>$goods_id])->update(['inventory'=>$inventory]) === false)
|
2020-07-16 22:34:56 +08:00
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.goods_spec_sync_inventory_fail_tips'), -20);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
2020-07-16 23:56:16 +08:00
|
|
|
|
$inventory_total += $inventory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 商品库存
|
|
|
|
|
$data = [
|
|
|
|
|
'inventory' => $inventory_total,
|
|
|
|
|
'upd_time' => time(),
|
|
|
|
|
];
|
2020-07-30 22:18:57 +08:00
|
|
|
|
if(Db::name('Goods')->where(['id'=>$goods_id])->update($data) === false)
|
2020-07-16 23:56:16 +08:00
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.goods_sync_inventory_fail_tips'), -21);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 22:21:04 +08:00
|
|
|
|
// 商品仓库库存修改钩子
|
|
|
|
|
$hook_name = 'plugins_service_warehouse_goods_inventory_sync';
|
2021-07-18 23:42:10 +08:00
|
|
|
|
$ret = EventReturnHandle(MyEventTrigger($hook_name, [
|
2021-01-27 22:21:04 +08:00
|
|
|
|
'hook_name' => $hook_name,
|
|
|
|
|
'is_backend' => true,
|
|
|
|
|
'goods_id' => $goods_id
|
|
|
|
|
]));
|
|
|
|
|
if(isset($ret['code']) && $ret['code'] != 0)
|
|
|
|
|
{
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-19 17:44:03 +08:00
|
|
|
|
return DataReturn(MyLang('update_success'), 0);
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据商品id和规格获取库存
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-07-16
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [int] $goods_id [商品id]
|
2020-07-16 23:56:16 +08:00
|
|
|
|
* @param [string] $spec_str [规格值,如 套餐一白色16G(无则 default)]
|
2020-07-16 22:34:56 +08:00
|
|
|
|
*/
|
2020-07-17 15:43:13 +08:00
|
|
|
|
public static function WarehouseGoodsSpecInventory($goods_id, $spec_str = 'default')
|
2020-07-16 22:34:56 +08:00
|
|
|
|
{
|
2020-07-16 23:56:16 +08:00
|
|
|
|
// 获取商品仓库
|
|
|
|
|
// 仓库商品是否有效
|
|
|
|
|
$warehouse_ids = Db::name('WarehouseGoods')->where(['goods_id'=>$goods_id, 'is_enable'=>1])->column('warehouse_id');
|
|
|
|
|
if(empty($warehouse_ids))
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 15:43:13 +08:00
|
|
|
|
// 检查仓库是否启用
|
|
|
|
|
$warehouse_ids = Db::name('Warehouse')->where(['id'=>$warehouse_ids, 'is_enable'=>1, 'is_delete_time'=>0])->column('id');
|
|
|
|
|
if(empty($warehouse_ids))
|
2020-07-16 22:34:56 +08:00
|
|
|
|
{
|
2020-07-17 15:43:13 +08:00
|
|
|
|
return 0;
|
2020-07-16 22:34:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取商品规格库存
|
|
|
|
|
$where = [
|
2020-07-16 23:56:16 +08:00
|
|
|
|
'warehouse_id' => $warehouse_ids,
|
|
|
|
|
'goods_id' => $goods_id,
|
2020-07-17 15:43:13 +08:00
|
|
|
|
'md5_key' => md5(empty($spec_str) ? 'default' : $spec_str),
|
2020-07-16 22:34:56 +08:00
|
|
|
|
];
|
|
|
|
|
return (int) Db::name('WarehouseGoodsSpec')->where($where)->sum('inventory');
|
|
|
|
|
}
|
2020-08-02 20:07:05 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 仓库库存扣减
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-08-02
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [int] $order_id [订单id]
|
|
|
|
|
* @param [int] $goods_id [商品id]
|
|
|
|
|
* @param [string] $spec [商品规格]
|
|
|
|
|
* @param [int] $buy_number [扣除库存数量]
|
|
|
|
|
*/
|
|
|
|
|
public static function WarehouseGoodsInventoryDeduct($order_id, $goods_id, $spec, $buy_number)
|
|
|
|
|
{
|
|
|
|
|
// 获取仓库id
|
|
|
|
|
$warehouse_id = Db::name('Order')->where(['id'=>$order_id])->value('warehouse_id');
|
|
|
|
|
if(empty($warehouse_id))
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.order_warehouse_id_empty_tips'), -1);
|
2020-08-02 20:07:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 规格key,空则默认default
|
|
|
|
|
$md5_key = 'default';
|
|
|
|
|
if(!empty($spec))
|
|
|
|
|
{
|
|
|
|
|
if(!is_array($spec))
|
|
|
|
|
{
|
|
|
|
|
$spec = json_decode($spec, true);
|
|
|
|
|
}
|
|
|
|
|
$md5_key = implode('', array_column($spec, 'value'));
|
|
|
|
|
}
|
|
|
|
|
$md5_key = md5($md5_key);
|
|
|
|
|
|
|
|
|
|
// 扣除仓库商品规格库存
|
2020-09-03 22:12:30 +08:00
|
|
|
|
$where = ['warehouse_id'=>$warehouse_id, 'goods_id'=>$goods_id, 'md5_key'=>$md5_key];
|
2020-11-23 11:27:15 +08:00
|
|
|
|
$inventory = (int) Db::name('WarehouseGoodsSpec')->where($where)->value('inventory');
|
2020-09-03 22:12:30 +08:00
|
|
|
|
if($inventory < $buy_number)
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.goods_spec_inventory_not_enough_tips').'['.$warehouse_id.'-'.$goods_id.'('.$inventory.'<'.$buy_number.')]', -11);
|
2020-09-03 22:12:30 +08:00
|
|
|
|
}
|
2021-07-21 16:35:38 +08:00
|
|
|
|
if(!Db::name('WarehouseGoodsSpec')->where($where)->dec('inventory', $buy_number)->update())
|
2020-08-02 20:07:05 +08:00
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.goods_spec_inventory_dec_fail_tips').'['.$warehouse_id.'-'.$goods_id.'('.$buy_number.')]', -11);
|
2020-08-02 20:07:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 扣除仓库商品库存
|
2020-09-03 22:12:30 +08:00
|
|
|
|
$where = ['warehouse_id'=>$warehouse_id, 'goods_id'=>$goods_id];
|
|
|
|
|
$inventory = Db::name('WarehouseGoods')->where($where)->value('inventory');
|
|
|
|
|
if($inventory < $buy_number)
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.goods_inventory_not_enough_tips').'['.$warehouse_id.'-'.$goods_id.'('.$inventory.'<'.$buy_number.')]', -11);
|
2020-09-03 22:12:30 +08:00
|
|
|
|
}
|
2021-07-21 16:35:38 +08:00
|
|
|
|
if(!Db::name('WarehouseGoods')->where($where)->dec('inventory', $buy_number)->update())
|
2020-08-02 20:07:05 +08:00
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.goods_inventory_dec_fail_tips').'['.$warehouse_id.'-'.$goods_id.'('.$buy_number.')]', -12);
|
2020-08-02 20:07:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 22:21:04 +08:00
|
|
|
|
// 商品库存扣除钩子
|
|
|
|
|
$hook_name = 'plugins_service_warehouse_goods_inventory_deduct';
|
2021-07-18 23:42:10 +08:00
|
|
|
|
$ret = EventReturnHandle(MyEventTrigger($hook_name, [
|
2021-01-27 22:21:04 +08:00
|
|
|
|
'hook_name' => $hook_name,
|
|
|
|
|
'is_backend' => true,
|
|
|
|
|
'order_id' => $order_id,
|
|
|
|
|
'goods_id' => $goods_id,
|
|
|
|
|
'spec' => $spec,
|
|
|
|
|
'buy_number' => $buy_number,
|
|
|
|
|
]));
|
|
|
|
|
if(isset($ret['code']) && $ret['code'] != 0)
|
|
|
|
|
{
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('operate_success'), 0);
|
2020-08-02 20:07:05 +08:00
|
|
|
|
}
|
2020-09-04 15:55:10 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 仓库库存回滚
|
|
|
|
|
* @author Devil
|
|
|
|
|
* @blog http://gong.gg/
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
* @date 2020-08-02
|
|
|
|
|
* @desc description
|
|
|
|
|
* @param [int] $order_id [订单id]
|
|
|
|
|
* @param [int] $goods_id [商品id]
|
|
|
|
|
* @param [string] $spec [商品规格]
|
|
|
|
|
* @param [int] $buy_number [扣除库存数量]
|
|
|
|
|
*/
|
|
|
|
|
public static function WarehouseGoodsInventoryRollback($order_id, $goods_id, $spec, $buy_number)
|
|
|
|
|
{
|
|
|
|
|
// 获取仓库id
|
|
|
|
|
$warehouse_id = Db::name('Order')->where(['id'=>$order_id])->value('warehouse_id');
|
|
|
|
|
if(empty($warehouse_id))
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.order_warehouse_id_empty_tips'), -1);
|
2020-09-04 15:55:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 规格key,空则默认default
|
|
|
|
|
$md5_key = 'default';
|
|
|
|
|
if(!empty($spec))
|
|
|
|
|
{
|
|
|
|
|
if(!is_array($spec))
|
|
|
|
|
{
|
|
|
|
|
$spec = json_decode($spec, true);
|
|
|
|
|
}
|
|
|
|
|
$md5_key = implode('', array_column($spec, 'value'));
|
|
|
|
|
}
|
|
|
|
|
$md5_key = md5($md5_key);
|
|
|
|
|
|
2021-09-24 15:45:24 +08:00
|
|
|
|
// 扣除仓库商品规格库存、存在对应规格才进行库存回滚操作
|
2020-09-04 15:55:10 +08:00
|
|
|
|
$where = ['warehouse_id'=>$warehouse_id, 'goods_id'=>$goods_id, 'md5_key'=>$md5_key];
|
2021-09-24 15:45:24 +08:00
|
|
|
|
$temp = Db::name('WarehouseGoodsSpec')->where($where)->find();
|
|
|
|
|
if(!empty($temp))
|
2020-09-04 15:55:10 +08:00
|
|
|
|
{
|
2021-09-24 15:45:24 +08:00
|
|
|
|
if(!Db::name('WarehouseGoodsSpec')->where($where)->inc('inventory', $buy_number)->update())
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.goods_spec_inventory_inc_fail_tips').'['.$warehouse_id.'-'.$goods_id.'('.$buy_number.')]', -11);
|
2021-09-24 15:45:24 +08:00
|
|
|
|
}
|
2020-09-04 15:55:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 扣除仓库商品库存
|
|
|
|
|
$where = ['warehouse_id'=>$warehouse_id, 'goods_id'=>$goods_id];
|
2021-09-24 15:45:24 +08:00
|
|
|
|
$temp = Db::name('WarehouseGoods')->where($where)->find();
|
|
|
|
|
if(!empty($temp))
|
2020-09-04 15:55:10 +08:00
|
|
|
|
{
|
2021-09-24 15:45:24 +08:00
|
|
|
|
if(!Db::name('WarehouseGoods')->where($where)->inc('inventory', $buy_number)->update())
|
|
|
|
|
{
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('common_service.warehousegoods.goods_inventory_inc_fail_tips').'['.$warehouse_id.'-'.$goods_id.'('.$buy_number.')]', -12);
|
2021-09-24 15:45:24 +08:00
|
|
|
|
}
|
2020-09-04 15:55:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 22:21:04 +08:00
|
|
|
|
// 商品库存回滚钩子
|
|
|
|
|
$hook_name = 'plugins_service_warehouse_goods_inventory_rollback';
|
2021-07-18 23:42:10 +08:00
|
|
|
|
$ret = EventReturnHandle(MyEventTrigger($hook_name, [
|
2021-01-27 22:21:04 +08:00
|
|
|
|
'hook_name' => $hook_name,
|
|
|
|
|
'is_backend' => true,
|
|
|
|
|
'order_id' => $order_id,
|
|
|
|
|
'goods_id' => $goods_id,
|
|
|
|
|
'spec' => $spec,
|
|
|
|
|
'buy_number' => $buy_number,
|
|
|
|
|
]));
|
|
|
|
|
if(isset($ret['code']) && $ret['code'] != 0)
|
|
|
|
|
{
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 17:01:04 +08:00
|
|
|
|
return DataReturn(MyLang('operate_success'), 0);
|
2020-09-04 15:55:10 +08:00
|
|
|
|
}
|
2020-07-13 21:33:55 +08:00
|
|
|
|
}
|
|
|
|
|
?>
|