|
@@ -16,6 +16,7 @@ class Buy
|
|
|
}
|
|
|
|
|
|
$order_data['shop_id'] = $shop['id'];
|
|
|
+ $order_data['uid'] = -1;
|
|
|
$order_data['mobile'] = $shop['mobile'];
|
|
|
$order_data['name'] = $name;
|
|
|
$order_data['num'] = $num;
|
|
@@ -148,10 +149,7 @@ class Buy
|
|
|
if ($order && $order['pay_status'] == 1) {
|
|
|
|
|
|
if ($status == 2) {
|
|
|
-
|
|
|
- if (Dever::project('message')) {
|
|
|
- Dever::load('message/lib/data')->push(-1, $order['shop_id'], '订购成功通知', '购买成功', 1);
|
|
|
- }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
$update['pay_status'] = $status;
|
|
@@ -280,25 +278,127 @@ class Buy
|
|
|
$shop = Dever::db('shop/info')->find($info['shop_id']);
|
|
|
|
|
|
|
|
|
+ $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
|
|
|
+ $store = array();
|
|
|
+ $distance = array();
|
|
|
+
|
|
|
+
|
|
|
+ foreach ($shop_store as $k => $v) {
|
|
|
+ $info = Dever::db('store/info')->find($v['store_id']);
|
|
|
+ if ($info && $info['status'] == 1) {
|
|
|
+ $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
|
|
|
+ $store[] = $info;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- $store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
|
|
|
- $store_id = array();
|
|
|
- foreach ($store as $k => $v) {
|
|
|
- $store_id[] = $v['store_id'];
|
|
|
+ array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
|
|
|
+
|
|
|
+
|
|
|
+ $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
|
|
|
+ $factory = array();
|
|
|
+ $distance = array();
|
|
|
+
|
|
|
+
|
|
|
+ foreach ($shop_factory as $k => $v) {
|
|
|
+ $info = Dever::db('factory/info')->find($v['factory_id']);
|
|
|
+ if ($info && $info['status'] == 1) {
|
|
|
+ $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
|
|
|
+ $factory[] = $info;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- $store_id = implode(',', $store_id);
|
|
|
+ array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
|
|
|
+
|
|
|
+ print_r($store);
|
|
|
+ print_r($factory);die;
|
|
|
+
|
|
|
+ if ($store) {
|
|
|
+ foreach ($goods as $k => $v) {
|
|
|
+
|
|
|
+ $find = $this->store($order_id, $shop, $store, $v);
|
|
|
+ if (!$find[1]) {
|
|
|
+
|
|
|
+ if ($factory) {
|
|
|
+ $find = $this->factory($order_id, $shop, $factory, $v);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$find[1]) {
|
|
|
+
|
|
|
+ Dever::db('shop/buy_order_goods')->update(array('where_id' => $v['id'], 'status' => 4));
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($find[1]) {
|
|
|
+ Dever::db('shop/buy_order_goods')->update(array('where_id' => $v['id'], 'status' => 2, 'type' => $find[0], 'type_id' => implode(',', $find[1])));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- $where = ' id in('.$store_id.') and status = 1 ';
|
|
|
+ Dever::db('shop/buy_order')->update(array('where_id' => $info['id'], 'status' => 3));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- $sql = 'select *,round((st_distance(point(lng, lat), point('.$shop['lng'].', '.$shop['lat'].'))*111195)/1000, 2) as distance from {table} where '.$where.' order by distance asc';
|
|
|
+ private function store($order_id, $shop, $store, $goods, $state = 2)
|
|
|
+ {
|
|
|
+ $find = array();
|
|
|
+ $id = array();
|
|
|
+ foreach ($store as $k => $v) {
|
|
|
+ $where['store_id'] = $v['id'];
|
|
|
+ $where['goods_id'] = $goods['goods_id'];
|
|
|
+ $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
|
|
|
+ $sku = Dever::db('store/goods_sku')->getOne($where);
|
|
|
+
|
|
|
+ if ($sku) {
|
|
|
+ if ($goods['num'] <= $sku['total']) {
|
|
|
+
|
|
|
+ $state = Dever::load('store/lib/order')->pay($order_id, $shop['id'], $where['store_id'], $goods['price'], $goods['num'], $where['goods_id'], $where['sku_id']);
|
|
|
+ if ($state) {
|
|
|
+ $id[] = $where['store_id'];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } elseif ($state == 1 && $sku['total'] > 0) {
|
|
|
+
|
|
|
+ $goods['num'] = $goods['num'] - $sku['total'];
|
|
|
+
|
|
|
+ $state = Dever::load('store/lib/order')->pay($order_id, $shop['id'], $where['store_id'], $goods['price'], $sku['total'], $where['goods_id'], $where['sku_id']);
|
|
|
+ if (!$state) {
|
|
|
+ $goods['num'] = $goods['num'] + $sku['total'];
|
|
|
+ } else {
|
|
|
+ $id[] = $where['store_id'];
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- $store = Dever::db('factory/info')->$method($sql, array(), $page);
|
|
|
+ return array(1, $id);
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- foreach ($goods as $k => $v) {
|
|
|
-
|
|
|
+ private function factory($order_id, $shop, $factory, $goods)
|
|
|
+ {
|
|
|
+ $find = array();
|
|
|
+ $id = array();
|
|
|
+ foreach ($factory as $k => $v) {
|
|
|
+ $where['factory_id'] = $v['id'];
|
|
|
+ $where['goods_id'] = $goods['goods_id'];
|
|
|
+ $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
|
|
|
+ $sku = Dever::db('factory/goods_sku')->getOne($where);
|
|
|
+
|
|
|
+ if ($sku) {
|
|
|
+ $state = Dever::load('factory/lib/order')->pay($order_id, $shop['id'], $where['factory_id'], $goods['price'], $goods['num'], $where['goods_id'], $where['sku_id']);
|
|
|
+ if ($state) {
|
|
|
+ $id[] = $where['factory_id'];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ return array(2, $id);
|
|
|
}
|
|
|
}
|