仓库订单拆分逻辑开发

This commit is contained in:
devil 2020-07-18 23:24:31 +08:00
parent 3af384fffe
commit 70b5a4baf2
6 changed files with 194 additions and 96 deletions

View File

@ -50,9 +50,9 @@ class Buy extends Common
*/
public function Index()
{
if(input('post.'))
if($this->data_post)
{
session('buy_post_data', $_POST);
session('buy_post_data', $this->data_post);
return redirect(MyUrl('index/buy/index'));
} else {
// 站点类型,是否开启了展示型
@ -82,7 +82,7 @@ class Buy extends Common
$buy_base = $buy_ret['data']['base'];
$buy_goods = $buy_ret['data']['goods'];
$buy_extension_data = $buy_ret['data']['extension_data'];
print_r($buy_goods);die;
// 用户地址
$address = UserService::UserAddressList(['user'=>$this->user]);
$this->assign('user_address_list', $address['data']);

View File

@ -17,6 +17,7 @@ use app\service\UserService;
use app\service\ResourcesService;
use app\service\PaymentService;
use app\service\ConfigService;
use app\service\OrderSplitService;
/**
* 购买服务层
@ -710,6 +711,13 @@ class BuyService
}
}
// 订单拆分
$order_split = OrderSplitService::Run(['goods'=>$goods]);
if($order_split['code'] != 0)
{
return $order_split;
}
// 商品/基础信息
$total_price = empty($goods) ? 0 : array_sum(array_column($goods, 'total_price'));
$base = [
@ -767,7 +775,7 @@ class BuyService
// 返回数据
$result = [
'goods' => $goods,
'goods' => $order_split['data'],
'base' => $base,
'extension_data' => $extension_data,
];

View File

@ -0,0 +1,157 @@
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
use think\Db;
use think\facade\Hook;
use app\service\WarehouseService;
/**
* 订单拆分服务层
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-18
* @desc description
*/
class OrderSplitService
{
/**
* 订单拆分入口
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-18
* @desc description
* @param [array] $params [输入参数]
*/
public static function Run($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'goods',
'error_msg' => '商品为空',
],
[
'checked_type' => 'is_array',
'key_name' => 'goods',
'error_msg' => '商品有误',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 商品仓库集合
$warehouse_goods = self::GoodsWarehouseAggregate($params['goods']);
return DataReturn('操作成功', 0, $warehouse_goods);
}
/**
* 商品仓库集合
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-18
* @desc description
* @param [array] $data [商品数据]
*/
public static function GoodsWarehouseAggregate($data)
{
$result = [];
foreach($data as $v)
{
// 不存在规格则使用默认
if(empty($v['spec']))
{
$spec = [
[
'type' => '默认规格',
'value' => 'default',
]
];
} else {
$spec = $v['spec'];
}
// 获取商品库存
$where = [
'wgs.goods_id' => $v['goods_id'],
'wgs.md5_key' => md5(implode('', array_column($spec, 'value'))),
'wg.is_enable' => 1,
'w.is_enable' => 1,
'w.is_delete_time' => 0,
];
$field = 'distinct w.id,w.name,w.alias,w.lng,w.lat,w.province,w.city,w.county,w.address,wgs.inventory,w.is_default,w.level';
$warehouse = Db::name('WarehouseGoodsSpec')->alias('wgs')->join(['__WAREHOUSE_GOODS__'=>'wg'], 'wgs.warehouse_id=wg.warehouse_id')->join(['__WAREHOUSE__'=>'w'], 'wg.warehouse_id=w.id')->where($where)->field($field)->order('w.level desc,w.is_default desc,wgs.inventory desc')->select();
// 默认仓库
$warehouse_default = [];
// 商品仓库分组
if(!empty($warehouse))
{
$goods = [];
foreach($warehouse as $w)
{
// 是否还存在未分配的数量
if($v['stock'] > 0)
{
// 追加商品并减除数量
$goods[] = $v;
$v['stock'] -= $w['inventory'];
// 是否第一次赋值
if(!array_key_exists($w['id'], $result))
{
$warehouse_handle = WarehouseService::DataHandle([$w]);
$result[$w['id']] = $warehouse_handle[0];
$result[$w['id']]['goods_items'] = [];
unset($result[$w['id']]['is_default'], $result[$w['id']]['level'], $result[$w['id']]['inventory']);
}
// 商品归属到仓库下
if(!empty($goods))
{
$result[$w['id']]['goods_items'] = array_merge($result[$w['id']]['goods_items'], $goods);
}
} else {
break;
}
}
} else {
// 未获取到仓库则使用默认仓库
if(empty($warehouse_default))
{
$warehouse_default = Db::name('Warehouse')->where(['is_default'=>1, 'is_enable'=>1, 'is_delete_time'=>0])->field('id,name,alias,lng,lat,province,city,county,address')->find();
}
if(!empty($warehouse_default))
{
if(!array_key_exists($warehouse_default['id'], $result))
{
$warehouse_handle = WarehouseService::DataHandle([$warehouse_default]);
$result[$warehouse_default['id']] = $warehouse_handle[0];
$result[$warehouse_default['id']]['goods_items'] = [];
}
// 商品归属到仓库下
$result[$warehouse_default['id']]['goods_items'][] = $v;
}
}
}
return array_values($result);
}
}
?>

View File

@ -42,7 +42,6 @@ class WarehouseGoodsService
$n = isset($params['n']) ? intval($params['n']) : 10;
$order_by = 'id desc';
$data = Db::name('WarehouseGoods')->field($field)->where($where)->order($order_by)->limit($m, $n)->select();
return DataReturn('处理成功', 0, self::DataHandle($data));
}

View File

@ -39,11 +39,32 @@ class WarehouseService
$field = empty($params['field']) ? '*' : $params['field'];
$order_by = empty($params['order_by']) ? 'level desc, id desc' : trim($params['order_by']);
$data = Db::name('Warehouse')->field($field)->where($where)->order($order_by)->select();
return DataReturn('处理成功', 0, self::DataHandle($data));
}
/**
* 数据处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-18
* @desc description
* @param [array] $data [仓库数据]
*/
public static function DataHandle($data)
{
if(!empty($data))
{
// 地区数据
$ids = array_unique(array_merge(array_column($data, 'province'), array_column($data, 'city'), array_column($data, 'county')));
$region = Db::name('Region')->where(['id'=>$ids])->column('name', 'id');
// 字段列表
$keys = ArrayKeys($data);
// 获取商品信息
if(in_array('province', $keys) && in_array('city', $keys) && in_array('county', $keys))
{
// 地区数据
$ids = array_unique(array_merge(array_column($data, 'province'), array_column($data, 'city'), array_column($data, 'county')));
$region = Db::name('Region')->where(['id'=>$ids])->column('name', 'id');
}
// 循环处理数据
foreach($data as &$v)
@ -73,7 +94,7 @@ class WarehouseService
}
}
}
return DataReturn('success', 0, $data);
return $data;
}
/**

View File

@ -32,92 +32,5 @@ return array (
'log_write' =>
array (
),
'plugins_css' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
1 => 'app\\plugins\\freightfee\\Hook',
2 => 'app\\plugins\\limitedtimediscount\\Hook',
),
'plugins_service_users_center_left_menu_handle' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_service_header_navigation_top_right_handle' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_service_order_status_change_history_success_handle' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_service_order_aftersale_audit_handle_end' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_service_site_extraction_address_list' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_service_buy_order_insert_end' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_service_goods_spec_extends_handle' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_view_admin_user_save' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_service_user_save_handle' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_view_goods_detail_base_buy_nav_min_inside' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_view_goods_detail_photo_within' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_view_goods_detail_base_bottom' =>
array (
0 => 'app\\plugins\\distribution\\Hook',
),
'plugins_service_buy_handle' =>
array (
0 => 'app\\plugins\\freightfee\\Hook',
),
'plugins_view_goods_detail_title' =>
array (
0 => 'app\\plugins\\freightfee\\Hook',
),
'plugins_js' =>
array (
0 => 'app\\plugins\\limitedtimediscount\\Hook',
),
'plugins_service_navigation_header_handle' =>
array (
0 => 'app\\plugins\\limitedtimediscount\\Hook',
1 => 'app\\plugins\\speedplaceorder\\Hook',
),
'plugins_service_goods_handle_end' =>
array (
0 => 'app\\plugins\\limitedtimediscount\\Hook',
),
'plugins_service_goods_spec_base' =>
array (
0 => 'app\\plugins\\limitedtimediscount\\Hook',
),
'plugins_view_goods_detail_base_top' =>
array (
0 => 'app\\plugins\\limitedtimediscount\\Hook',
),
'plugins_view_home_floor_top' =>
array (
0 => 'app\\plugins\\limitedtimediscount\\Hook',
),
);
?>