diff --git a/application/index/controller/Buy.php b/application/index/controller/Buy.php index e3014c0818999b9dd150f8a05ab02b8a25065ebf..31c73af8127234a99d553aab7b6ecb65d1571f32 100755 --- a/application/index/controller/Buy.php +++ b/application/index/controller/Buy.php @@ -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']); diff --git a/application/service/BuyService.php b/application/service/BuyService.php index 44774faae2f13a7c4e0eb1bada7a104e0302cfdf..b7cafc9311e85940c940915f9548973af874f53a 100755 --- a/application/service/BuyService.php +++ b/application/service/BuyService.php @@ -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, ]; diff --git a/application/service/OrderSplitService.php b/application/service/OrderSplitService.php new file mode 100644 index 0000000000000000000000000000000000000000..0354592298b97370215f75bf0e61f26a2c99801c --- /dev/null +++ b/application/service/OrderSplitService.php @@ -0,0 +1,157 @@ + '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); + } +} +?> \ No newline at end of file diff --git a/application/service/WarehouseGoodsService.php b/application/service/WarehouseGoodsService.php index 66925a2a2f357b5582692a08a878a2bc90724865..9098ad5c63ce1e50dd661561951916a6214e843e 100644 --- a/application/service/WarehouseGoodsService.php +++ b/application/service/WarehouseGoodsService.php @@ -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)); } diff --git a/application/service/WarehouseService.php b/application/service/WarehouseService.php index 97c0a9a92487cefd16017045d1bbae7f6d23bb73..63d02852b3521cdf71184b1071b27a2deda51b08 100644 --- a/application/service/WarehouseService.php +++ b/application/service/WarehouseService.php @@ -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; } /** diff --git a/application/tags.php b/application/tags.php index c0e0ea4a677081f01176581209574c807271b287..7fb4b4ce8cf21ddb6f5dd9c94f67aad0e78b5245 100755 --- a/application/tags.php +++ b/application/tags.php @@ -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', - ), ); ?> \ No newline at end of file