提交 33f1ff9b 编写于 作者: D devil

商品库存转换脚本

上级 e4f75fed
......@@ -13,6 +13,8 @@ namespace app\api\controller;
use think\Db;
use app\service\ResourcesService;
use app\service\RegionService;
use app\service\GoodsService;
use app\service\WarehouseGoodsService;
/**
* 开发测试
......@@ -141,17 +143,136 @@ class Devtest extends Common
*/
public function GoodsInventoryHandle()
{
if(input('pwd') != 'shopxo520')
{
die('非法访问');
}
$warehouse_id = 1;
$warehouse = Db::name('Warehouse')->where(['id'=>$warehouse_id])->find();
if(empty($warehouse))
{
$data = [
$w_data = [
'id' => $warehouse_id,
'name' => '默认仓库',
'is_default' => 1,
'add_time' => time(),
];
$warehouse_id = Db::name('Warehouse')->insertGetId($data);
$warehouse_id = Db::name('Warehouse')->insertGetId($w_data);
}
// 状态
$success = 0;
$fail = 0;
// 获取数据
// 一次处理100条
$prefix = config('database.prefix');
$field = 'id as goods_id';
$sql = 'SELECT '.$field.' FROM `'.$prefix.'goods` WHERE `id` NOT IN (SELECT `goods_id` FROM `'.$prefix.'warehouse_goods` WHERE `warehouse_id`='.$warehouse_id.') ORDER BY `id` ASC LIMIT 500';
$result = Db::query($sql);
if(!empty($result))
{
foreach($result as $rv)
{
$goods_id = $rv['goods_id'];
// 获取商品规格
$res = GoodsService::GoodsSpecificationsActual($goods_id);
$inventory_spec = [];
if(!empty($res['value']) && is_array($res['value']))
{
$inventory_arr = [];
$inventory_temp = Db::name('GoodsSpecBase')->where(['id'=>array_column($res['value'], 'base_id')])->field('id,inventory')->select();
if(!empty($inventory_temp))
{
foreach($inventory_temp as $rv)
{
$inventory_arr[$rv['id']] = $rv['inventory'];
}
foreach($res['value'] as &$vv)
{
$vv['inventory'] = isset($inventory_arr[$vv['base_id']]) ? $inventory_arr[$vv['base_id']] : 0;
}
}
// 获取当前配置的库存
$spec = array_column($res['value'], 'value');
foreach($spec as $k=>$v)
{
$arr = explode(',', $v);
$inventory_spec[] = [
'name' => implode(' / ', $arr),
'spec' => json_encode(WarehouseGoodsService::GoodsSpecMuster($v, $res['title']), JSON_UNESCAPED_UNICODE),
'md5_key' => md5(implode('', $arr)),
'inventory' => isset($res['value'][$k]['inventory']) ? $res['value'][$k]['inventory'] : 0,
];
}
} else {
// 没有规格则处理默认规格 default
$str = 'default';
$inventory_spec[] = [
'name' => '默认规格',
'spec' => $str,
'md5_key' => md5($str),
'inventory' => Db::name('GoodsSpecBase')->where(['goods_id'=>$goods_id])->value('inventory'),
];
}
// 添加关联商品
$warehouse_goods_id = Db::name('WarehouseGoods')->where(['warehouse_id'=>$warehouse_id, 'goods_id'=>$goods_id])->value('id');
$inventory_total = array_sum(array_column($inventory_spec, 'inventory'));
if(empty($warehouse_goods_id))
{
$w_goods = [
'warehouse_id' => $warehouse_id,
'goods_id' => $goods_id,
'is_enable' => 1,
'inventory' => $inventory_total,
'add_time' => time(),
];
$warehouse_goods_id = Db::name('WarehouseGoods')->insertGetId($w_goods);
} else {
Db::name('WarehouseGoods')->where(['warehouse_id'=>$warehouse_id, 'goods_id'=>$goods_id])->update(['inventory'=>$inventory_total, 'upd_time'=>time()]);
}
// 库存
$w_values = [];
if($warehouse_goods_id > 0)
{
foreach($inventory_spec as $iv)
{
$w_values[] = [
'warehouse_id' => $warehouse_id,
'warehouse_goods_id' => $warehouse_goods_id,
'goods_id' => $goods_id,
'md5_key' => $iv['md5_key'],
'spec' => $iv['spec'],
'inventory' => $iv['inventory'],
'add_time' => time(),
];
}
} else {
$fail++;
}
// 添加数据
if(!empty($w_values))
{
// 删除库存关联
Db::name('WarehouseGoodsSpec')->where(['warehouse_id'=>$warehouse_id, 'warehouse_goods_id'=>$warehouse_goods_id, 'goods_id'=>$goods_id])->delete();
// 写入
if(Db::name('WarehouseGoodsSpec')->insertAll($w_values) >= count($w_values))
{
$success++;
}
} else {
$fail++;
}
}
}
echo 'success:'.$success.', fail:'.$fail;
}
/**
......
......@@ -1410,7 +1410,7 @@ class GoodsService
}
// 获取仓库规格库存
$temp_data['inventory'] = WarehouseGoodsService::GoodsSpecInventory($goods_id, implode('', array_column($temp_value, 'value')));
$temp_data['inventory'] = WarehouseGoodsService::WarehouseGoodsSpecInventory($goods_id, implode('', array_column($temp_value, 'value')));
// 规格基础添加
$base_id = Db::name('GoodsSpecBase')->insertGetId($temp_data);
......@@ -1688,7 +1688,7 @@ class GoodsService
if(!empty($ids))
{
// 根据基础值id获取规格值列表
$temp_data = Db::name('GoodsSpecValue')->where(['goods_spec_base_id'=>$ids])->field('goods_spec_base_id,value')->select();
$temp_data = Db::name('GoodsSpecValue')->where(['goods_spec_base_id'=>$ids])->field('goods_spec_base_id,value')->order('id asc')->select();
if(!empty($temp_data))
{
// 根据基础值id分组
......@@ -1810,7 +1810,7 @@ class GoodsService
if(!empty($ids))
{
// 根据基础值id获取规格值列表
$temp_data = Db::name('GoodsSpecValue')->where(['goods_spec_base_id'=>$ids])->field('goods_spec_base_id,value')->select();
$temp_data = Db::name('GoodsSpecValue')->where(['goods_spec_base_id'=>$ids])->field('goods_spec_base_id,value')->order('id asc')->select();
if(!empty($temp_data))
{
// 根据基础值id分组
......@@ -1821,7 +1821,6 @@ class GoodsService
}
// 获取当前操作元素索引
$last = end($params['spec']);
$index = count($params['spec'])-1;
$spec_str = implode('', array_column($params['spec'], 'value'));
$spec_type = [];
......
......@@ -556,7 +556,7 @@ class WarehouseGoodsService
'data' => $warehouse_goods,
'spec' => $inventory_spec,
];
return DataReturn('success', 0, $result);
return DataReturn('success', 0, $result);
}
/**
......@@ -680,15 +680,11 @@ class WarehouseGoodsService
// 启动事务
Db::startTrans();
// 获取原始数据
// 删除原始数据
$where = [
'warehouse_goods_id' => $warehouse_goods['id'],
'warehouse_id' => $warehouse_goods['warehouse_id'],
'goods_id' => $warehouse_goods['goods_id'],
];
$data_old = Db::name('WarehouseGoodsSpec')->where($where)->select();
// 删除原有数据
Db::name('WarehouseGoodsSpec')->where($where)->delete();
// 仓库商品更新
......
......@@ -32,17 +32,5 @@ return array (
'log_write' =>
array (
),
'plugins_css' =>
array (
0 => 'app\\plugins\\freightfee\\Hook',
),
'plugins_service_buy_group_goods_handle' =>
array (
0 => 'app\\plugins\\freightfee\\Hook',
),
'plugins_view_goods_detail_title' =>
array (
0 => 'app\\plugins\\freightfee\\Hook',
),
);
?>
\ No newline at end of file
......@@ -19,7 +19,7 @@ define('APPLICATION_VERSION', 'v1.9.0');
define('DS', '/');
// HTTP类型
define('__MY_HTTP__', ((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') || (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') || (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) || (!empty($_SERVER['HTTP_FROM_HTTPS']) && $_SERVER['HTTP_FROM_HTTPS'] !== 'off') || (!empty($_SERVER['HTTP_X_CLIENT_SCHEME']) && $_SERVER['HTTP_X_CLIENT_SCHEME'] == 'https')) ? 'https' : 'http');
define('__MY_HTTP__', ((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') || (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') || (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) || (!empty($_SERVER['HTTP_FROM_HTTPS']) && $_SERVER['HTTP_FROM_HTTPS'] !== 'off') || (!empty($_SERVER['HTTP_X_CLIENT_SCHEME']) && $_SERVER['HTTP_X_CLIENT_SCHEME'] == 'https') || (isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https')) ? 'https' : 'http');
// 根目录
$my_root = empty($_SERVER['SCRIPT_NAME']) ? '' : substr($_SERVER['SCRIPT_NAME'], 1, strrpos($_SERVER['SCRIPT_NAME'], '/'));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册