订单添加

parent 37ce953d
...@@ -21,7 +21,7 @@ use Illuminate\Support\Facades\Validator; ...@@ -21,7 +21,7 @@ use Illuminate\Support\Facades\Validator;
class OrderApiController extends Controller class OrderApiController extends Controller
{ {
//添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2} //生成订单
public function addOrder(Request $request) public function addOrder(Request $request)
{ {
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
...@@ -43,12 +43,17 @@ class OrderApiController extends Controller ...@@ -43,12 +43,17 @@ class OrderApiController extends Controller
return !$result ? $this->setError('Add cart failed , please contact administrator'):$this->setSuccess('Add inquiry success'); return !$result ? $this->setError('Add cart failed , please contact administrator'):$this->setSuccess('Add inquiry success');
} }
//购物车列表 //订单列表
public function orderLists(Request $request){ public function orderLists(Request $request){
$result = CartService::cartLists($request->user->id); $result = OrderService::orderLists($request->user->id,$request->input());
return $this->setSuccessData($result); return $this->setSuccessData($result);
} }
//取消订单
public function cancelOrder(Request $request){
$result = OrderService::cancelOrder($request->user->id,$request->input("order_id"));
return $result[0] > 0 ? $this->setError($result[1],$result[0]):$this->setSuccessData($result[1]);
}
} }
...@@ -102,7 +102,7 @@ class CartService ...@@ -102,7 +102,7 @@ class CartService
/* /*
* 刷新购物车并且返回列表 * 刷新购物车并且返回列表
*/ */
public static function cartLists($user_id,$gid="",$page_size=100,$page=1){ public static function cartLists($user_id,$gid="",$page_size=400,$page=1){
//当前用户所有可用的购物车数据 //当前用户所有可用的购物车数据
$where["status"] = 1; $where["status"] = 1;
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Http\Services; namespace App\Http\Services;
use App\Exceptions\InvalidRequestException; use App\Exceptions\InvalidRequestException;
use App\Http\Models\User\TaxInfoModel;
use App\Models\OrderAddressModel; use App\Models\OrderAddressModel;
use App\Models\OrderItemsModel; use App\Models\OrderItemsModel;
use App\Models\OrderModel; use App\Models\OrderModel;
...@@ -185,100 +186,98 @@ class OrderService ...@@ -185,100 +186,98 @@ class OrderService
return $order_sn; return $order_sn;
} }
//订单列表 /*
public static function orderLists($user_id){ * 个人订单列表
*
//当前用户所有可用的购物车数据 */
$query = CartModel::where(['user_id'=>$user_id,"status"=>1]) public static function orderLists($user_id,$param){
->orderBy('cart_id', 'desc');
$result = $query->get()->toArray(); $query = OrderModel::with('order_items')
if (!$result){ ->select([
return false; 'order_id',
'order_sn',
'order_amount',
'status',
])
->orderBy('order_id', 'desc');
$param["user_id"] = $user_id;
#拼接搜索
foreach ($param as $a => $b) {
$b = trim($b);
$a = trim($a);
if (empty($b)) {
continue;
}
switch ($a) {
case "order_sn": //
case "order_id": //
case "status": //
$query->where($a,$b);
break;
case "goods_name": //
$query->whereHas('order_items', function ($q) use ($b) {
$q->where('goods_name', $b);
});
break;
}
} }
try{ $res = $query->paginate(\Arr::get($param,"page_size",10), ['*'], 'page',\Arr::get($param,"page",1) )->toArray();
$con = DB::connection();
$con->beginTransaction();
$redis = Redis::connection(); if (!$res){
$goodsInfoArr = ThirdService::getGoodsInfo(array_column($result,"goods_id")); return false;
}
$cartList = []; $result = $res["data"];
foreach ($result as $v) {
$goods_id = $v['goods_id'];
$temp = [
'user_id' => $user_id,
'goods_id' => $v['goods_id'],
];
$skuInfo = $goodsInfoArr[$goods_id]; //sku库存
$digikeyInfo = $redis->hget("sku_raw_map",$goods_id); //digikey 编码
if ($digikeyInfo){
$digikeyArr = json_decode($digikeyInfo,true);
$temp["raw_goods_sn"]= $digikeyArr["raw_goods_id"];
$temp["raw_goods_packing"]= $digikeyArr["pack"];
$temp["raw_brand_name"]= $digikeyArr["raw_brand_name"];
}
$temp["buy_number"] = $skuInfo["stock"] > $v['buy_number'] ? $v['buy_number'] : $skuInfo["stock"]; //购买数量 $orderAddressArr = OrderAddressModel::whereIn("order_id", array_column($result, "order_id"))->get()->keyBy("order_id")->toArray(); //发票信息
$temp = [];
$buy_price = 0; foreach ($result as $k=>$v){
$ladder_price = \Arr::get($skuInfo,"ladder_price"); $order_id = $v["order_id"];
if ($ladder_price){ //购买价格
foreach ($ladder_price as $a=>$b){
if ($temp["buy_number"] >= $b["purchases"]){
$buy_price = $b["price_us"];
}else{
break;
}
}
}
$temp["status"] = $temp["buy_number"] > 0 ? CartModel::STATUS_YES:CartModel::STATUS_NO; $items = OrderItemsModel::where("order_id",$order_id)->get()->toArray();
$temp["buy_price"] = $buy_price; $orderAddress = \Arr::get($orderAddressArr,$order_id,[]);
$temp["update_time"] = time(); $temp[]=[
$temp["update_time"] = time(); "order_id"=>$order_id,
$flag = CartModel::where(["cart_id"=>$v["cart_id"]])->update($temp); "order_sn"=>$v["order_sn"],
if (!$flag){ "order_amount"=>$v["order_amount"],
continue; "goods_name_arr"=>array_column($items,"goods_name"),
} "status"=>$v["status"],
"status_en"=>\Arr::get(OrderModel::$status,$v["status"],""),
//格式化数据 "receiver"=>\Arr::get($orderAddress,"consignee"),
$skuInfo = self::dullDataInfo($skuInfo); "email"=>\Arr::get($orderAddress,"email"),
$cartList[] = [ "phone"=>\Arr::get($orderAddress,"phone"),
"goods_id"=>$skuInfo["goods_id"], //sku_id "shipping_address"=>\Arr::get($orderAddress,"detail_address"),
"goods_name"=>$skuInfo["goods_name"], //型号名称 ];
"supplier_id"=>$skuInfo["supplier_id"], //供应商id }
"supplier_name"=>$skuInfo["supplier_name"], //供应商id
"buy_number"=>$temp["buy_number"], //购买数量
"encap"=>$skuInfo["encap"], //标准包装数
"pdf"=>$skuInfo["pdf"], //型号pdf
"brand_id"=>$skuInfo["brand_id"], //品牌id
"brand_name"=>$skuInfo["brand_name"], //品牌名称
"stock"=>$skuInfo["stock"], //库存
"stock_format"=>numberToHtml($skuInfo["stock"]), //库存加密
"max_buy_stock"=>$skuInfo["max_buy_stock"], //最大可购买数量
"moq"=>numberToHtml($skuInfo["moq"]), //最少起订量
"mpq"=>numberToHtml($skuInfo["mpq"]), //标准包装量
"mult"=>numberToHtml($skuInfo["multiple"]), //递增量
"hk_delivery_time"=>$skuInfo["hk_delivery_time"], //香港交期
"ladder_price"=>$skuInfo["ladder_price"], //价格阶梯
"is_buy"=>$skuInfo["is_buy"], //是否能购买
];
}
$con->commit(); return [
"total"=> $res["total"], //返回总条数
"page"=>$res["current_page"], //第几页
"page_size"=> \Arr::get($param,"page_size",10), //每页多少条
"lists"=>$temp //数据列表
];
}
return $cartList; /*
}catch (\Exception $e){ * 更新订单状态
$con->rollback(); */
throw new InvalidRequestException($e->getMessage().$e->getLine()); public static function cancelOrder($user_id,$order_id){
return false; if (!$order_id){
return [1001,"error:order_id no empty"];
}
$where["order_id"] = $order_id;
if ($user_id >0){
$where["user_id"] = $user_id;
} }
$orderInfo = OrderModel::getOrderInfo($where);
if (!$orderInfo || $orderInfo["status"] > OrderModel::status_waiting_delivery){
return [1003,"error:order status"];
}
$flag = OrderModel::where("order_id",$order_id)->update(["status"=>OrderModel::status_cancel,"update_time"=>time()]);
return $flag ? [0,"cancel order success"]:[1002,"error: cancel order"];
} }
} }
...@@ -37,6 +37,11 @@ class OrderModel extends Model ...@@ -37,6 +37,11 @@ class OrderModel extends Model
self::status_complete => "Transaction Complete", self::status_complete => "Transaction Complete",
]; ];
public function order_items()
{
return $this->hasMany(OrderItemsModel::class, 'order_id', 'order_id');
}
//获取订单详情 //获取订单详情
public static function getOrderInfo($where) public static function getOrderInfo($where)
{ {
......
...@@ -50,8 +50,9 @@ Route::middleware(['api', 'api.check'])->namespace('Api')->group(function () { ...@@ -50,8 +50,9 @@ Route::middleware(['api', 'api.check'])->namespace('Api')->group(function () {
Route::POST('cart/cartMyCount', 'CartApiController@cartMyCount'); //购物车总数 Route::POST('cart/cartMyCount', 'CartApiController@cartMyCount'); //购物车总数
Route::POST('order/addOrder', 'OrderApiController@addOrder'); //添加订单 Route::POST('order/addOrder', 'OrderApiController@addOrder'); //添加订单
Route::POST('order/orderList', 'OrderApiController@orderList'); //订单列表 Route::POST('order/orderLists', 'OrderApiController@orderLists'); //订单列表
Route::POST('order/updateOrder', 'OrderApiController@updateOrder'); //更新订单状态 Route::POST('order/updateOrder', 'OrderApiController@updateOrder'); //更新订单状态
Route::POST('order/cancelOrder', 'OrderApiController@cancelOrder'); //取消订单
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment