verify_sign(); // 验证参数 $request->scene('create')->validate(); // 检查登录 $uid = $this->checkLogin(); // 接收参数 $isCart = request('is_cart',0); $productList = request('product_list','[]'); $customCouponId = request('custom_coupon_id',1); $addr = $CustomAddr->getOne(request('addr_id',0)); // 如果不存在数据 if( !$addr ) return json_send(['code'=>'error','msg'=>'地址有误,请核对','data'=>['error'=>'没有找到对应的地址']]); // 重组数据 $addr = ['contact_name'=>$addr['contact_name'],'contact_phone'=>$addr['contact_phone'],'contact_province'=>$addr['contact_province'],'contact_city'=>$addr['contact_city'],'contact_area'=>$addr['contact_area'],'contact_addr'=>$addr['contact_addr']]; // 解码购买信息 $buyList = json_decode($productList,true); // 如果不存在数据 if( empty($buyList) ) return json_send(['code'=>'error','msg'=>'没有需下单的产品','data'=>['error'=>'产品列表为空']]); // 查询产品信息 $productList = $Product->getListByIds(array_column($buyList,'product_id')); $skusList = $ProductSkus->getListByIds(array_column($buyList,'product_skuid')); // 产品以商业公司分组,方便写入订单 $orderProduct = []; // 产品价格同级,用于优惠券计算 $productPrice = []; // 循环处理购买信息 foreach ($buyList as $buyInfo) { // 如果产品不存在 if( empty($productList[$buyInfo['product_id']]) ) return json_send(['code'=>'error','msg'=>'产品不存在或已下架','data'=>['error'=>'产品不存在或已下架=>'.$buyInfo['product_id']]]); // 获取产信息 $productInfo = $productList[$buyInfo['product_id']]; // 如果存在SKU if( $buyInfo['product_skuid'] ) { // 判断SKU信息存不存在 if( empty($skusList[$buyInfo['product_skuid']]) ) return json_send(['code'=>'error','msg'=>'该产品规格不存在或已下架','data'=>['error'=>'SKU不存在或已下架=>'.$buyInfo['product_skuid']]]); // 如果SKU存在,合并产品信息 $productInfo = array_merge($productInfo,$skusList[$buyInfo['product_skuid']]); // 扣除库存 $skusList[$buyInfo['product_skuid']]['stock'] = $skusList[$buyInfo['product_skuid']]['stock'] - $buyInfo['buy_num']; } // 判断库存 if( $productInfo['stock'] < $buyInfo['buy_num'] ) return json_send(['code'=>'error','msg'=>'产品库存不足','data'=>['error'=>'产品库存不足=>'.$buyInfo['product_id']]]); // 扣除库存 $productList[$buyInfo['product_id']]['stock'] = $productList[$buyInfo['product_id']]['stock'] - $buyInfo['buy_num']; // 计算价值 $priceTotal = $buyInfo['buy_num'] * $productInfo['price']; $courseTotal = $buyInfo['buy_num'] * $productInfo['course_number']; // 购买信息 $buyInfo = [ 'is_rebate'=>0, 'custom_uid'=>$uid, 'business_id'=>$productInfo['business_id'], 'product_id'=>$buyInfo['product_id'], 'buy_num'=>$buyInfo['buy_num'], 'price_total'=>$priceTotal, 'pay_total'=>$priceTotal, 'coupon_total'=>0, 'product_name'=>$productInfo['product_name'], 'course_id'=>$productInfo['course_id'], 'course_number'=>$courseTotal, 'sku_attr_names'=>$productInfo['sku_attr_names'], 'sku_id'=>$productInfo['sku_id'], 'product_thumb'=>$productInfo['product_thumb'] ]; // 获取信息 if( !isset($orderProduct[$buyInfo['business_id']]) ) $orderProduct[$buyInfo['business_id']] = ['business_id'=>$buyInfo['business_id'],'custom_uid'=>$buyInfo['custom_uid'],'price_total'=>0,'pay_total'=>0,'coupon_total'=>0,'product_list'=>[]]; // 订单产品 $orderProduct[$buyInfo['business_id']]['price_total'] += $buyInfo['price_total']; $orderProduct[$buyInfo['business_id']]['pay_total'] += $buyInfo['price_total']; $orderProduct[$buyInfo['business_id']]['product_list'][] = $buyInfo; // 商品优惠信息不存在,创建 if( !isset($productPrice[$buyInfo['product_id']]) ) $productPrice[$buyInfo['product_id']] = ['price_total'=>0,'rebate_price'=>0]; // 计算总价 $productPrice[$buyInfo['product_id']]['price_total'] = $productPrice[$buyInfo['product_id']]['price_total'] + $priceTotal; } // 优惠券数据 $couponRebate = $CustomCoupon->getRebatePrice($customCouponId,$uid,$productPrice); // 判断是否使用了优惠券 $usedCoupon = $couponRebate['is_used']; // 获取优惠券扣减金额 $productPrice = $couponRebate['product_price']; // 获取优惠券赠品信息 $rebateProduct = $couponRebate['rebate_product']; // 库存处理 foreach ($productList as $key => $value) { $productList[$key] = ['id'=>$value['id'],'stock'=>$value['stock']]; } foreach ($skusList as $key => $value) { $skusList[$key] = ['id'=>$value['sku_id'],'stock'=>$value['stock']]; } // 组合订单数据 foreach ($orderProduct as $key => $order) { // 判断哪一家的赠品 if( isset($rebateProduct[$order['business_id']]) ){ // 循环赠品 foreach ( $rebateProduct[$order['business_id']] as $value) { // 没有对应的产品 if( !isset($productList[$value['product_id']]) ) $productList[$value['product_id']] = ['id'=>$value['id'],'stock'=>$value['stock']]; // 判断库存,如果库存不足,只赠送最后的库存 if( $productList[$value['product_id']]['stock'] <= $value['buy_num'] ) $value['buy_num'] = $productList[$value['product_id']]['stock']; // 库存扣减 $productList[$value['product_id']]['stock'] = $productList[$value['product_id']]['stock'] - $value['buy_num']; // 追加到订单表 $order['product_list'][] = ['is_rebate'=>1,'custom_uid'=>$uid,'business_id'=>$value['business_id'],'product_id'=>$value['product_id'],'buy_num'=>$value['buy_num'],'price_total'=>$value['price_total'],'pay_total'=>$value['pay_total'],'coupon_total'=>$value['coupon_total'],'product_name'=>$value['product_name'],'sku_attr_names'=>$value['sku_attr_names'],'product_thumb'=>$value['product_thumb']]; } } // 计算总价格 foreach ($order['product_list'] as $k=>$product) { // 商品不存在,不进行扣减 if( empty($productPrice[$product['product_id']]['rebate_price']) ) { // 重组 $order['product_list'][$k] = $product; continue; } // 总优惠增加 $order['coupon_total'] = $order['coupon_total'] + $productPrice[$product['product_id']]['rebate_price']; // 当前商品的优惠折扣计算 $product['coupon_total'] = number_format( $productPrice[$product['product_id']]['rebate_price'] * ($product['price_total'] / $productPrice[$product['product_id']]['price_total']) , 2 , '.' ,''); // 成交小计 $product['pay_total'] = $product['pay_total'] - $product['coupon_total']; // 重组 $order['product_list'][$k] = $product; } // 成交总价 $order['pay_total'] = $order['pay_total'] - $order['coupon_total']; // 成交总价 $order['custom_uid'] = $uid; // 重组 $orderProduct[$key] = $order; } // 组合数据,写入订单表,子表 DB::beginTransaction(); // 写入数据 try { // 当前时间 $time = time(); // 更新库存 $Product->updateBatch(array_values($productList)); // 更新库存 $ProductSkus->updateBatch(array_values($skusList)); // 循环订单数据 foreach ($orderProduct as $order) { // 先获取产品列表,并去除key $productList = array_values($order['product_list']); // 删除非必要数据 unset($order['product_list']); // 创建总订单 $orderId = $Model->add($order); // 如果订单写入失败 if( !$orderId ) { // 回退数据 DB::rollBack(); // 错误提示 return json_send(['code'=>'error','msg'=>'下单失败','data'=>['error'=>'订单创建失败']]); } // 创建子订单 foreach ($productList as $key=>$value) { // 增加订单ID $value['order_id'] = $orderId; // 增加订单ID $value['insert_time']= $time; $value['update_time']= $time; // 结果 $productList[$key] = $value; } // 写入子表 $result = $OrdersProduct->query()->insert($productList); // 如果扣减失败 if( !$result ) { // 回退数据 DB::rollBack(); // 提示信息 return json_send(['code'=>'error','msg'=>'下单失败','data'=>['error'=>'子订单创建失败']]); } // 写入订单地址表 $addr['order_id'] = $orderId; // 写入订单地址表 $result = $OrdersAddr->add($addr); // 地址写入失败 if( !$result ) { // 回退数据 DB::rollBack(); // 提示信息 return json_send(['code'=>'error','msg'=>'下单失败','data'=>['error'=>'地址写入失败']]); } // 如果使用了优惠券 if( $usedCoupon ) $CustomCoupon->edit($usedCoupon,['status'=>1,'order_id'=>$orderId]); // 购物车 if( $isCart ) $ShopCart->query()->where([['custom_uid','=',$uid]])->whereIn('skuid',array_column($buyList,'product_skuid'))->delete(); } // 提交数据 DB::commit(); // 返回结果 return json_send(['code'=>'success','msg'=>'下单成功','data'=>['id'=>null]]); // 返回结果 } catch (\Throwable $th) { // 回退数据 DB::rollBack(); // 判断结果,如果库存扣减失败的话 if( stripos($th->getMessage(),'UNSIGNED') ) return json_send(['code'=>'error','msg'=>'下单失败','data'=>['error'=>'产品库存扣减失败']]); // 下单失败提示 return json_send(['code'=>'error','msg'=>'下单失败','data'=>['error'=>$th->getMessage().$th->getLine()]]); } } /** * 获取产品列表 /api/orders/get_list * * @param string $name 产品名称 * @param int $page 页码,默认1 * @param int $limit 每页条数,默认10条 * * */ public function get_list(Request $request,Model $Model,OrdersProduct $OrdersProduct,Business $Business){ // 接口验签 // $this->verify_sign(); // 验证参数 $request->scene('get_list')->validate(); // 检查登录 $uid = $this->checkLogin(); // 接收参数 $status = request('status',0); $limit = request('limit',10); // 显示 $map = [['custom_uid','=',$uid]]; // 查询状态 if( $status ) $map[] = ['status','=',$status]; // 查询 $Paginator = $Model->query()->where($map)->orderByDesc('id')->paginate($limit,['id','pay_total','status','price_total','coupon_total','business_id','pay_total','insert_time']); // 订单产品 $productList = $OrdersProduct->query()->whereIn('order_id', array_column($Paginator->items(),'id'))->select(['id as item_id','order_id','product_id','buy_num','is_rebate','sku_attr_names as product_spec','product_name','product_thumb',])->get()->toArray(); // 循环处理 foreach ($Paginator as $key => $order) { // 商品列表 $itemList = []; // 返回结果 foreach ($productList as $item) { // 产品图路径 $item['product_thumb'] = path_compat($item['product_thumb']); // 如果是订单的 if( $item['order_id'] == $order['id'] ) $itemList[] = $item; } // 获取子列表 $order['state'] = (string) $Model->getState($order['status'],'state'); // 获取子列表 $order['business_name'] = (string) $Business->getOne($order['business_id'],'name'); // 获取子列表 $order['contents_class']= 0; // 获取子列表 $order['product_list'] = $itemList; // 重组 $Paginator[$key] = $order; } // 获取数据 $data['total'] = $Paginator->total(); $data['current_page'] = $Paginator->currentPage(); $data['per_page'] = $Paginator->perPage(); $data['last_page'] = $Paginator->lastPage(); $data['data'] = $Paginator->items(); // 返回结果 return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]); } }