diff --git a/app/controller/api/store/order/StoreProcessing.php b/app/controller/api/store/order/StoreProcessing.php index 731f3c3e..9a70c102 100755 --- a/app/controller/api/store/order/StoreProcessing.php +++ b/app/controller/api/store/order/StoreProcessing.php @@ -4,66 +4,96 @@ namespace app\controller\api\store\order; use think\facade\Db; use crmeb\basic\BaseController; -use think\facade\Log; /** * 订单处理 */ class StoreProcessing extends BaseController { + /** * 自动向市级供应链创建订单 + * @param $order + * @return void + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ public function AutomaticallyCreateOrders($order) { - - - $merchant_two = Db::name('merchant')->where('mer_id', $order['mer_id'])->find(); + if (!in_array($order['source'], [103, 105])) { + return; + } + $orderUser = Db::name('merchant')->where('mer_id', $order['mer_id'])->find(); $store_group_order = Db::name('store_group_order')->where('group_order_id', $order['group_order_id'])->find(); $store_group_order_other = Db::name('store_group_order_other')->where('group_order_sn', $store_group_order['group_order_sn'])->find(); if (!$store_group_order_other) { unset($store_group_order['group_order_id']); $group_order_id = Db::name('store_group_order_other')->strict(false)->insertGetId($store_group_order); - }else{ - $group_order_id=$store_group_order_other['group_order_id']; + } else { + $group_order_id = $store_group_order_other['group_order_id']; } - $select = Db::name('store_order_product')->where('order_id', $order['order_id'])->select(); - if ($order['source'] == 103 ||$order['source'] == 105 && $select) { - // $financialRecordRepository = app()->make(FinancialRecordRepository::class); - // $financeSn = $financialRecordRepository->getSn(); - $arr = $select->toArray(); + $orderProducts = Db::name('store_order_product')->where('order_id', $order['order_id'])->select()->toArray(); + $merchants = []; + foreach ($orderProducts as $item) { + $cartInfo = json_decode($item['cart_info'], true); + $payPrice = bcmul($cartInfo['productAttr']['procure_price'], $item['product_num'], 2); + if (isset($merchants[$item['product_mer_id']])) { + $merchants[$item['product_mer_id']]['total_price'] = bcadd($merchants[$item['product_mer_id']]['procure_price'], $payPrice, 2); + $merchants[$item['product_mer_id']]['total_num'] = $merchants[$item['product_mer_id']]['product_num'] + $item['product_num']; + } else { + $merchants[$item['product_mer_id']]['total_price'] = $payPrice; + $merchants[$item['product_mer_id']]['total_num'] = $item['product_num']; + } + $merchants[$item['product_mer_id']]['product'][] = $item; + } + $productOther = []; + foreach ($merchants as $merId => $merchant) { $order['group_order_id'] = $group_order_id; $order['source'] = 104; - - $order['mer_id'] = $select[0]['product_mer_id']; - $order['uid'] = $merchant_two['uid']; - $order['real_name'] = $merchant_two['mer_name'] . '-' . $merchant_two['real_name']; - $order['user_phone'] = $merchant_two['mer_phone']; - $order['user_address'] = $merchant_two['mer_address']; + $order['mer_id'] = $merId; + $order['uid'] = $orderUser['uid']; + $order['real_name'] = $orderUser['mer_name'] . '-' . $orderUser['real_name']; + $order['user_phone'] = $orderUser['mer_phone']; + $order['user_address'] = $orderUser['mer_address']; $order['order_type'] = 0; - $order['pay_price'] = $order['procure_price']; - $order['total_price'] = $order['procure_price']; - + $order['pay_price'] = $merchant['total_price']; + $order['total_price'] = $merchant['total_price']; + $order['procure_price'] = $merchant['total_price']; + $order['total_num'] = $merchant['total_num']; unset($order['order_id'], $order['orderProduct'], $order['user'], $order['supply_chain_rate'], $order['logistics_code'], $order['logistics_phone']); - $order_id = Db::name('store_order_other')->strict(false)->insertGetId($order); - - foreach ($arr as $key => $value) { - $arr[$key]['order_id'] = $order_id; - $arr[$key]['source'] = 104; - $cartInfo = json_decode($value['cart_info'], true); - $arr[$key]['product_price'] = bcmul($cartInfo['productAttr']['procure_price'], $value['product_num'], 2); - $arr[$key]['total_price'] = $arr[$key]['product_price']; - $arr[$key]['pay_price'] = $arr[$key]['product_price']; - $arr[$key]['product_id'] = $value['product_source_id']; - $arr[$key]['uid'] = $merchant_two['uid']; - $arr[$key]['product_source_id'] = 0; - $arr[$key]['product_mer_id'] = 0; - unset($arr[$key]['order_product_id']); - Db::name('store_product')->where('product_id',$value['product_source_id'])->dec('stock'); - } - // $financialRecordRepository->insertAll($finance); - Db::name('store_order_product_other')->strict(false)->insertAll($arr); - return $order_id; + $orderId = Db::name('store_order_other')->strict(false)->insertGetId($order); + $productOther = array_merge($productOther, $this->setOrderOtherProduct($merchant, $orderUser, $orderId)); } + Db::name('store_order_product_other')->strict(false)->insertAll($productOther); } + + /** + * 设置订单商品 + * @param $merchant + * @param $orderUser + * @param $orderId + * @return array + */ + public function setOrderOtherProduct($merchant, $orderUser, $orderId) + { + $productOther = []; + foreach ($merchant['product'] as $product) { + $cartInfo = json_decode($product['cart_info'], true); + $product['order_id'] = $orderId; + $product['source'] = 104; + $product['product_price'] = bcmul($cartInfo['productAttr']['procure_price'], $product['product_num'], 2); + $product['total_price'] = $product['product_price']; + $product['pay_price'] = $product['product_price']; + $product['product_id'] = $product['product_source_id']; + $product['uid'] = $orderUser['uid']; + $product['product_source_id'] = 0; + $product['product_mer_id'] = 0; + unset($product['order_product_id']); + $productOther[] = $product; + Db::name('store_product')->where('product_id', $product['product_source_id'])->dec('stock'); + } + return $productOther; + } + }