123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- namespace addons\unishop\behavior;
- use addons\unishop\extend\Ali;
- use addons\unishop\extend\Hashids;
- use addons\unishop\extend\Wechat;
- use addons\unishop\model\Address;
- use addons\unishop\model\Config;
- use addons\unishop\model\DeliveryRule as DeliveryRuleModel;
- use addons\unishop\model\OrderProduct;
- use addons\unishop\model\Product;
- use app\admin\model\unishop\Coupon;
- use think\Db;
- use think\Exception;
- class Order
- {
-
- public function createOrderAfter(&$params, $extra)
- {
-
- $key = 0;
- $productExtend = new \addons\unishop\extend\Product;
- $prefix = \think\Config::get('database.prefix');
- if (Config::isPessimism()) {
-
- foreach ($extra['specNumber'] as $spec => $number) {
- $result = 0;
- if (is_numeric($spec) && $params[$key]['use_spec'] == Product::SPEC_OFF) {
- $result = Db::execute('UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number} WHERE id = {$params[$key]['id']}");
- } else if ($params[$key]['use_spec'] == Product::SPEC_ON) {
- $info = $productExtend->getBaseData($params[$key], $spec);
-
-
- $spec = str_replace(',', '","', $spec);
- $search = '"stock":"' . $info['stock'] . '","value":["' . $spec . '"]';
- $stock = $info['stock'] - $number;
- $replace = '"stock":\"' . $stock . '\","value":["' . $spec . '"]';
- $sql = 'UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, stock = stock-{$number}, real_sales = real_sales+{$number} ,`specTableList` = REPLACE(specTableList,'$search','$replace') WHERE id = {$params[$key]['id']}";
- $result = Db::execute($sql);
-
-
-
-
-
-
- }
- if ($result == 0) {
- throw new Exception('下单失败,请重试');
- }
- $key++;
- }
- } else {
-
- foreach ($extra['specNumber'] as $spec => $number) {
- $result = 0;
- if (is_numeric($spec) && $params[$key]['use_spec'] == Product::SPEC_OFF) {
- $result = Db::execute('UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number} WHERE id = {$params[$key]['id']} AND stock = {$params[$key]['stock']}");
- } else if ($params[$key]['use_spec'] == Product::SPEC_ON) {
- $info = $productExtend->getBaseData($params[$key], $spec);
-
-
- $spec = str_replace(',', '","', $spec);
- $search = '"stock":"' . $info['stock'] . '","value":["' . $spec . '"]';
- $stock = $info['stock'] - $number;
- $replace = '"stock":\"' . $stock . '\","value":["' . $spec . '"]';
- $sql = 'UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number},`specTableList` = REPLACE(specTableList,'$search','$replace') WHERE id = {$params[$key]['id']} AND stock = {$params[$key]['stock']}";
- $result = Db::execute($sql);
-
-
-
-
-
-
- }
- if ($result == 0) {
- throw new Exception('下单失败,请重试');
- }
- $key++;
- }
- }
-
- if (!empty($extra['cart'])) {
- $cart = $extra['cart'];
- Db::execute('DELETE FROM ' . $prefix . "unishop_cart WHERE id IN ($cart) AND user_id = $extra[userId]");
- }
-
- }
-
- public function createOrderBefore(&$params, $extra)
- {
- if(empty($extra['cart'])){
- $specs = explode(',', $extra['spec']);
- foreach ($specs as &$spec) {
- $spec = str_replace('|', ',', $spec);
- }
- $numbers = explode(',', $extra['number']);
- $productIds = explode(',', $extra['product_id']);
- if (count($specs) !== count($numbers) || count($specs) !== count($productIds)) {
- throw new Exception(__('Parameter error'));
- }
- }else{
- $carts = Db::name('unishop_cart')->field('id,product_id,spec,number')
- ->whereIn('id', $extra['cart'])
- ->order(['id' => 'desc'])
- ->select();
- $numbers = array_column($carts,'number');
- $productIds = array_column($carts,'product_id');
- $specs = array_column($carts,'spec');
- foreach ($productIds as $key => $productId) {
- $productIds[$key] = Hashids::encodeHex($productId);
- }
- foreach ($specs as $key => $spec) {
- $specs[$key] = is_null($spec) ? '' : $spec;
- }
- }
-
-
- $orderPrice = 0;
-
- $products = [];
- foreach ($productIds as $key => &$productId) {
- $productId = Hashids::decodeHex($productId);
- $products[$key] = Db::name('unishop_product')
- ->where(['id' => $productId, 'switch' => Product::SWITCH_ON])
- ->lock(Config::isPessimism())
- ->find();
- if (!$products[$key]) {
- throw new Exception(__('There are not exist or Offline'));
- }
- }
- if (count($products) == 0 || count($productIds) != count($products)) {
- throw new Exception(__('There are offline product'));
- }
-
- if (count($productIds) > 0) {
- $reduceStock = [];
- foreach ($products as $key => $value) {
- if (!isset($reduceStock[$value['id']])) {
- $reduceStock[$value['id']] = $numbers[$key];
- } else {
- $products[$key]['stock'] -= $reduceStock[$value['id']];
- $reduceStock[$value['id']] += $numbers[$key];
- }
- }
- }
-
- foreach ($products as $key => $product) {
- $productInfo = (new \addons\unishop\extend\Product())->getBaseData($product, $specs[$key] ? $specs[$key] : '');
- if ($productInfo['stock'] < $numbers[$key]) {
- throw new Exception(__('Insufficient inventory,%s pieces left', $productInfo['stock']));
- }
- $orderPrice = bcadd($orderPrice, bcmul($productInfo['sales_price'], $numbers[$key], 2), 2);
- $baseProductInfo[] = $productInfo;
- }
-
-
- $delivery = [];
-
- $address = [];
-
-
- $coupon = [];
- $params = [$products, $delivery, $coupon, $baseProductInfo, $address, $orderPrice, $specs, $numbers];
- }
-
- public function paidSuccess(&$params, $extra)
- {
- $order = &$params;
- $order->have_paid = time();
- $order->pay_type = $extra['pay_type'];
- $order->save();
- $orderProductModel = new OrderProduct();
- $orderProducts = $orderProductModel
- ->with('product')
- ->where(['order_id' => $order->id])
- ->select();
- foreach ($orderProducts as $product) {
- if (isset($product->product)) {
- $product->product->no_buy_yet -= $product->number;
- $product->product->save();
- }
- }
-
- }
-
- public function paidFail(&$params)
- {
- $order = &$params;
- $order->have_paid = \addons\unishop\model\Order::PAID_NO;
- $order->save();
-
- }
-
- public function orderRefund(&$params)
- {
- $order = &$params;
-
- switch ($order['pay_type']) {
- case \addons\unishop\model\Order::PAY_WXPAY:
- $app = Wechat::initEasyWechat('payment');
- $result = $app->refund->byOutTradeNumber($params['out_trade_no'], $params['out_trade_no'], bcmul($params['total_price'], 100), bcmul($params['refund']['amount'], 100), [
-
- 'refund_desc' => $params['refund']['reason_type'],
- ]);
- break;
- case \addons\unishop\model\Order::PAY_ALIPAY:
- $alipay = Ali::initAliPay();
- $order = [
- 'out_trade_no' => $params['out_trade_no'],
- 'refund_amount' => $params['total_price'],
- ];
- $result = $alipay->refund($order);
- break;
- }
-
- }
- }
|