|
@@ -4,60 +4,68 @@ use Dever\Helper\Str;
|
|
|
class Order
|
|
|
{
|
|
|
|
|
|
- public function add($info, $goods, $sku, $account, $order)
|
|
|
+ public function add($info, $goods, $sku, $account, $order, $num = 1)
|
|
|
{
|
|
|
+ $data = Dever::store()->transaction(array($this, 'addAct'), array($info, $goods, $sku, $account, $order, $num), '下单失败');
|
|
|
+ if ($data) {
|
|
|
+
|
|
|
+ \Dever\Helper\Redis::push('submit_' . DEVER_PROJECT, $data['id']);
|
|
|
+ Dever::load('info', 'seller')->log($data['seller_id'], $data['id'], $data['price'], 3);
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ public function addAct($info, $goods, $sku, $account, $order, $num = 1)
|
|
|
+ {
|
|
|
+
|
|
|
$cate = Dever::db('cate', 'goods')->find($goods['cate_id']);
|
|
|
if ($cate && $cate['rule'] && !preg_match($cate['rule'], $account)) {
|
|
|
Dever::error($account . '不符合规则');
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- $seller_goods = Dever::db('goods', 'seller')->find(array('seller_id' => $info['id'], 'goods_id' => $sku['info_id'], 'sku_id' => $sku['id']));
|
|
|
- if ($seller_goods && $seller_goods['discount'] > 0) {
|
|
|
- $info['discount'] = $seller_goods['discount'];
|
|
|
- }
|
|
|
- if (!$info['discount']) {
|
|
|
- $info['discount'] = 1;
|
|
|
- }
|
|
|
- $cash = round($sku['value'] * $info['discount'], 2);
|
|
|
-
|
|
|
-
|
|
|
- $info['yue'] = $info['credit'] + $info['cash'];
|
|
|
- if ($info['yue'] < $cash) {
|
|
|
- Dever::error('余额不足');
|
|
|
- }
|
|
|
- $state = Dever::load('info', 'seller')->dec($info['id'], $cash);
|
|
|
-
|
|
|
|
|
|
$check = Dever::db('order', 'seller')->find(array('seller_id' => $info['id'], 'seller_order_num' => $order));
|
|
|
if ($check) {
|
|
|
Dever::error('订单重复');
|
|
|
}
|
|
|
|
|
|
+ $cash = $this->cash($info['id'], $sku['info_id'], $sku['id'], $sku['value'], $num);
|
|
|
+
|
|
|
|
|
|
$data = array();
|
|
|
$data['account'] = $account;
|
|
|
$data['cate_id'] = $goods['cate_id'];
|
|
|
$data['goods_id'] = $sku['info_id'];
|
|
|
$data['sku_id'] = $sku['id'];
|
|
|
- $data['num'] = 1;
|
|
|
+ $data['num'] = $num;
|
|
|
$data['cash'] = $sku['value'];
|
|
|
$data['price'] = $cash;
|
|
|
$data['seller_id'] = $info['id'];
|
|
|
$data['seller_order_num'] = $order;
|
|
|
$data['seller_request'] = Dever::json_encode(Dever::input());
|
|
|
|
|
|
- $order_id = Dever::db('order', 'seller')->insert($data);
|
|
|
- if (!$order_id) {
|
|
|
+ $data['id'] = Dever::db('order', 'seller')->insert($data);
|
|
|
+ if (!$data['id']) {
|
|
|
Dever::error('下单失败');
|
|
|
}
|
|
|
-
|
|
|
- $state = \Dever\Helper\Redis::push('submit_' . DEVER_PROJECT, $order_id);
|
|
|
- Dever::load('info', 'seller')->log($info['id'], $order_id, $data['price'], 3);
|
|
|
- return $order_id;
|
|
|
+ return $data;
|
|
|
}
|
|
|
|
|
|
public function handle($info, $selected = array())
|
|
|
+ {
|
|
|
+ $data = Dever::store()->transaction(array($this, 'handleAct'), array($info, $selected), '下单失败');
|
|
|
+ $order = Dever::db('order', 'seller')->find($info['id']);
|
|
|
+ if ($info['status'] < 10 && $order['status'] >= 10) {
|
|
|
+ if ($order['status'] == 11) {
|
|
|
+
|
|
|
+ Dever::load('info', 'seller')->inc($order['seller_id'], $order['price']);
|
|
|
+ Dever::load('info', 'seller')->log($order['seller_id'], $order['id'], $order['price'], 4);
|
|
|
+ }
|
|
|
+
|
|
|
+ Dever::db('order_log', 'seller')->insert($order);
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function handleAct($info, $selected = array())
|
|
|
{
|
|
|
if ($info['status'] >= 10) {
|
|
|
return;
|
|
@@ -67,75 +75,74 @@ class Order
|
|
|
$info['order_num'] = $update['order_num'] = $this->createOrder();
|
|
|
}
|
|
|
Dever::db('order', 'seller')->update($info['id'], $update);
|
|
|
-
|
|
|
- $channel_list = Dever::db('channel', 'seller')->select(array('seller_id' => $info['seller_id'], 'goods_id' => $info['goods_id']));
|
|
|
- if (!$channel_list) {
|
|
|
- return $this->notify($info, '通道未开启', $update);
|
|
|
- }
|
|
|
- $channel = $channel_goods = array();
|
|
|
- foreach ($channel_list as $k => $v) {
|
|
|
- if ($selected && isset($selected[$v['channel_id']])) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (!$v['sku_id']) {
|
|
|
- $channel_goods = Dever::db('goods', 'channel')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $info['goods_id'], 'sku_id' => $info['sku_id']));
|
|
|
- if ($channel_goods) {
|
|
|
- $channel = $v['channel_id'];
|
|
|
- break;
|
|
|
- }
|
|
|
- } else {
|
|
|
- $sku_id = explode(',', $v['sku_id']);
|
|
|
- if (in_array($info['sku_id'], $sku_id)) {
|
|
|
- $channel = $v['channel_id'];
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (!$channel) {
|
|
|
- return $this->notify($info, '通道未开启', $update);
|
|
|
- }
|
|
|
|
|
|
- $channel = Dever::db('info', 'channel')->find($channel);
|
|
|
+ $channel = $this->channel($info['seller_id'], $info['goods_id'], $info['sku_id']);
|
|
|
if (!$channel) {
|
|
|
return $this->notify($info, '通道未开启', $update);
|
|
|
}
|
|
|
- if ($channel['status'] == 2) {
|
|
|
- return $this->notify($info, '通道未开启', $update);
|
|
|
+ $result = array();
|
|
|
+ if ($channel['type'] == 1) {
|
|
|
+
|
|
|
+
|
|
|
+ $param['order_id'] = $info['id'];
|
|
|
+ $param['order_num'] = $info['order_num'];
|
|
|
+ $param['account'] = $info['account'];
|
|
|
+ $param['cash'] = $info['cash'];
|
|
|
+ $param['num'] = $info['num'];
|
|
|
+ if ($info['other']) {
|
|
|
+ $info['other'] = Dever::json_decode($info['other']);
|
|
|
+ $param = array_merge($info['other'], $param);
|
|
|
+ }
|
|
|
+ if (isset($channel['goods']['code'])) {
|
|
|
+ $param['code'] = $channel['goods']['code'];
|
|
|
+ }
|
|
|
+ $result = Dever::load('func/api', 'connect')->run(1, $info['cate_id'], $channel, $param);
|
|
|
+ } elseif ($channel['type'] == 2) {
|
|
|
+
|
|
|
+ $param['channel_id'] = $channel['id'];
|
|
|
+ $param['goods_id'] = $info['goods_id'];
|
|
|
+ $param['sku_id'] = $info['sku_id'];
|
|
|
+ $param['status'] = 1;
|
|
|
+ $param['use_status'] = 1;
|
|
|
+ $card = Dever::db('card', 'channel')->select($param, array('limit' => '0, ' . $info['num']), true);
|
|
|
+ $total = count($card);
|
|
|
+ if ($total < $info['num']) {
|
|
|
+ return $this->notify($info, '卡密剩余数量不足', $update);
|
|
|
+ }
|
|
|
+ foreach ($card as $k => $v) {
|
|
|
+ Dever::db('card', 'channel')->update($v['id'], array('use_status' => 2, 'order_id' => $info['id']));
|
|
|
+ }
|
|
|
+ $result['status'] = 1;
|
|
|
+ } elseif ($channel['type'] == 11) {
|
|
|
+ $result['status'] = 1;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- $param['order_id'] = $info['id'];
|
|
|
- $param['order_num'] = $info['order_num'];
|
|
|
- $param['account'] = $info['account'];
|
|
|
- $param['cash'] = $info['cash'];
|
|
|
- if ($info['other']) {
|
|
|
- $info['other'] = Dever::json_decode($info['other']);
|
|
|
- $param = array_merge($info['other'], $param);
|
|
|
- }
|
|
|
- if (isset($channel_goods['code'])) {
|
|
|
- $param['code'] = $channel_goods['code'];
|
|
|
- }
|
|
|
- $result = Dever::load('func/api', 'connect')->run(1, $info['cate_id'], $channel, $param);
|
|
|
+
|
|
|
$update = array();
|
|
|
-
|
|
|
$update['channel_id'] = $channel['id'];
|
|
|
$update['channel_order_date'] = time();
|
|
|
- $update['channel_request'] = Dever::json_encode($result['request']);
|
|
|
- $update['channel_response'] = Dever::json_encode($result['response']);
|
|
|
- if (isset($channel_goods['id'])) {
|
|
|
- $update['channel_goods_id'] = $channel_goods['id'];
|
|
|
+ if (isset($result['request'])) {
|
|
|
+ $update['channel_request'] = Dever::json_encode($result['request']);
|
|
|
+ }
|
|
|
+ if (isset($result['response'])) {
|
|
|
+ $update['channel_response'] = Dever::json_encode($result['response']);
|
|
|
+ }
|
|
|
+ if (isset($channel['goods']['id'])) {
|
|
|
+ $update['channel_goods_id'] = $channel['goods']['id'];
|
|
|
}
|
|
|
$update['channel_goods_discount'] = $channel['discount'];
|
|
|
- if (isset($channel_goods['discount']) && $channel_goods['discount']) {
|
|
|
- $update['channel_goods_discount'] = $channel_goods['discount'];
|
|
|
+ if (isset($channel['goods']['discount']) && $channel['goods']['discount']) {
|
|
|
+ $update['channel_goods_discount'] = $channel['goods']['discount'];
|
|
|
}
|
|
|
if (!$update['channel_goods_discount']) {
|
|
|
$update['channel_goods_discount'] = 1;
|
|
|
}
|
|
|
- $update['buy_price'] = round($info['cash'] * $update['channel_goods_discount'], 2);
|
|
|
+ $update['buy_price'] = round($info['cash'] * $update['channel_goods_discount'], 2) * $info['num'];
|
|
|
if ($result['status'] == 1) {
|
|
|
|
|
|
Dever::db('order', 'seller')->update($info['id'], $update);
|
|
|
+ if ($channel['type'] == 2) {
|
|
|
+ return $this->notify($info, 'ok', $update);
|
|
|
+ }
|
|
|
return 'ok';
|
|
|
} else {
|
|
|
|
|
@@ -158,24 +165,30 @@ class Order
|
|
|
} else {
|
|
|
$notify = $seller['notify'];
|
|
|
}
|
|
|
- if ($notify && $info['seller_callback'] != 'ok' && $info['seller_callback_num'] <= $total) {
|
|
|
- $param = array();
|
|
|
- $param['appkey'] = $seller['appkey'];
|
|
|
- $param['order_num'] = $info['seller_order_num'];
|
|
|
- $param['system_order_num'] = $info['order_num'];
|
|
|
- $param['order_status'] = $status;
|
|
|
- $param['order_cash'] = $info['cash'];
|
|
|
- $param['official_order_num'] = $param['official_msg'] = '';
|
|
|
- if (isset($update['official_order_num'])) {
|
|
|
- $param['official_order_num'] = $update['official_order_num'];
|
|
|
- }
|
|
|
- if (isset($update['official_msg'])) {
|
|
|
- $param['official_msg'] = $update['official_msg'];
|
|
|
+ if ($notify) {
|
|
|
+ if ($info['seller_callback'] != 'ok' && $info['seller_callback_num'] <= $total) {
|
|
|
+ $param = array();
|
|
|
+ $param['appkey'] = $seller['appkey'];
|
|
|
+ $param['order_num'] = $info['seller_order_num'];
|
|
|
+ $param['system_order_num'] = $info['order_num'];
|
|
|
+ $param['order_status'] = $status;
|
|
|
+ $param['order_cash'] = $info['cash'];
|
|
|
+ $param['official_order_num'] = $param['official_msg'] = '';
|
|
|
+ if (isset($update['official_order_num'])) {
|
|
|
+ $param['official_order_num'] = $update['official_order_num'];
|
|
|
+ }
|
|
|
+ if (isset($update['official_msg'])) {
|
|
|
+ $param['official_msg'] = $update['official_msg'];
|
|
|
+ }
|
|
|
+ $param = \Dever\Helper\Secure::get($param, $seller['appsecret']);
|
|
|
+ $response = Dever::curl($notify, $param, 'post')->result();
|
|
|
+
|
|
|
+ $update['seller_callback'] = $response;
|
|
|
+ $update['seller_callback_date'] = time();
|
|
|
+ $update['seller_callback_num'] = $info['seller_callback_num'] + 1;
|
|
|
}
|
|
|
- $param = \Dever\Helper\Secure::get($param, $seller['appsecret']);
|
|
|
- $response = Dever::curl($notify, $param, 'post')->result();
|
|
|
-
|
|
|
- $update['seller_callback'] = $response;
|
|
|
+ } else {
|
|
|
+ $update['seller_callback'] = 'ok';
|
|
|
$update['seller_callback_date'] = time();
|
|
|
$update['seller_callback_num'] = $info['seller_callback_num'] + 1;
|
|
|
}
|
|
@@ -185,16 +198,6 @@ class Order
|
|
|
$update['status'] = $status;
|
|
|
$update['finish'] = 1;
|
|
|
$update['finish_date'] = time();
|
|
|
- if ($status == 11) {
|
|
|
-
|
|
|
- Dever::load('info', 'seller')->inc($info['seller_id'], $info['price']);
|
|
|
- Dever::load('info', 'seller')->log($info['seller_id'], $info['id'], $info['price'], 4);
|
|
|
- } elseif ($status == 10) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- $info = array_merge($info, $update);
|
|
|
- Dever::db('order_log', 'seller')->insert($info);
|
|
|
}
|
|
|
if (!$info['order_num']) {
|
|
|
$update['order_num'] = $this->createOrder();
|
|
@@ -203,6 +206,81 @@ class Order
|
|
|
return $msg;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public function cash($seller_id, $goods_id, $sku_id, $value, $num = 1)
|
|
|
+ {
|
|
|
+
|
|
|
+ $seller_goods = Dever::db('goods', 'seller')->find(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'sku_id' => $sku_id));
|
|
|
+
|
|
|
+ $info = Dever::db('info', 'seller')->find($seller_id);
|
|
|
+ if ($seller_goods && $seller_goods['discount'] > 0) {
|
|
|
+ $info['discount'] = $seller_goods['discount'];
|
|
|
+ }
|
|
|
+ if (!$info['discount']) {
|
|
|
+ $info['discount'] = 1;
|
|
|
+ }
|
|
|
+ $cash = round($value * $info['discount'], 2) * $num;
|
|
|
+
|
|
|
+
|
|
|
+ $info['yue'] = $info['credit'] + $info['cash'];
|
|
|
+ if ($info['yue'] < $cash) {
|
|
|
+ Dever::error('余额不足');
|
|
|
+ }
|
|
|
+ $state = Dever::load('info', 'seller')->dec($info['id'], $cash);
|
|
|
+ if (!$state) {
|
|
|
+ Dever::error('余额不足');
|
|
|
+ }
|
|
|
+ return $cash;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function channel($seller_id, $goods_id, $sku_id, $selected = array())
|
|
|
+ {
|
|
|
+
|
|
|
+ $channel_list = Dever::db('channel', 'seller')->select(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'status' => 1));
|
|
|
+ if (!$channel_list) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $channel = $goods = array();
|
|
|
+ foreach ($channel_list as $k => $v) {
|
|
|
+ if ($selected && isset($selected[$v['channel_id']])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!$v['sku_id']) {
|
|
|
+ $goods = Dever::db('goods', 'channel')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $goods_id, 'sku_id' => $sku_id));
|
|
|
+ if ($goods) {
|
|
|
+ $channel = $v['channel_id'];
|
|
|
+ break;
|
|
|
+ } else {
|
|
|
+ $goods = Dever::db('goods', 'channel')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $goods_id, 'sku_id' => -1));
|
|
|
+ if ($goods) {
|
|
|
+ $channel = $v['channel_id'];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $all_sku_id = explode(',', $v['sku_id']);
|
|
|
+ if (in_array($sku_id, $all_sku_id)) {
|
|
|
+ $channel = $v['channel_id'];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!$channel) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $channel = Dever::db('info', 'channel')->find($channel);
|
|
|
+ if (!$channel) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if ($channel['status'] == 2) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $channel['goods'] = $goods;
|
|
|
+ return $channel;
|
|
|
+ }
|
|
|
+
|
|
|
public function createOrder()
|
|
|
{
|
|
|
$where['order_num'] = Str::order('C');
|