Browse Source

外卖下单

master
weigang 5 years ago
parent
commit
e99bcfdbf0
  1. 4
      app/Amqp/Consumer/DevicOrderConsumer.php
  2. 12
      app/Controller/OrderController.php
  3. 36
      app/Model/OrderMain.php
  4. 14
      app/Service/CouponService.php
  5. 50
      app/Service/OrderService.php

4
app/Amqp/Consumer/DevicOrderConsumer.php

@ -65,7 +65,9 @@ class DevicOrderConsumer extends ConsumerMessage
public function isEnable(): bool public function isEnable(): bool
{ {
// if(env('APP_ENV')!='prod') return false;
if(env('APP_ENV') == 'local') {
return false;
}
return parent::isEnable(); return parent::isEnable();
} }
} }

12
app/Controller/OrderController.php

@ -5,6 +5,7 @@ namespace App\Controller;
use App\Request\OrderOnlineRequest; use App\Request\OrderOnlineRequest;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use App\Service\OrderServiceInterface; use App\Service\OrderServiceInterface;
use Hyperf\HttpMessage\Stream\SwooleStream;
class OrderController extends BaseController class OrderController extends BaseController
{ {
@ -18,6 +19,15 @@ class OrderController extends BaseController
public function addOnlineOrder(OrderOnlineRequest $request) public function addOnlineOrder(OrderOnlineRequest $request)
{ {
$orderMainId = $this->orderService->addOnlineOrder($request->validated()); $orderMainId = $this->orderService->addOnlineOrder($request->validated());
return $this->success(['order_main_id' => $orderMainId]);
if (!is_int($orderMainId)) {
return $this->response
->withHeader('Content-Type', 'application/text')
->withStatus(500)
->withBody(new SwooleStream($orderMainId));
}
return $this->response
->withHeader('Content-Type', 'application/text')
->withStatus(200)
->withBody(new SwooleStream($orderMainId));
} }
} }

36
app/Model/OrderMain.php

@ -6,41 +6,43 @@ namespace App\Model;
class OrderMain extends Model class OrderMain extends Model
{ {
// ÏßÉ϶©µ¥£¬ÍâÂô
// 线上订�,外�
const ORDER_TYPE_ONLINE = 1; const ORDER_TYPE_ONLINE = 1;
// Ïß϶©µ¥£¬µ±Ã渶
// 线下订�,当�付
const ORDER_TYPE_OFFLINE = 4; const ORDER_TYPE_OFFLINE = 4;
// ¶©µ¥×´Ì¬
// ´ý¸¶¿î
// 订�状�
// 待付款
const ORDER_STATE_UNPAY = 1; const ORDER_STATE_UNPAY = 1;
// ´ý½Óµ¥
// 待接�
const ORDER_STATE_UNTAKE = 2; const ORDER_STATE_UNTAKE = 2;
// ´ýËÍ´ï
// 待�达
const ORDER_STATE_DELIVERY = 3; const ORDER_STATE_DELIVERY = 3;
// ÒÑÍê³É
// 已完�
const ORDER_STATE_COMPLETE = 4; const ORDER_STATE_COMPLETE = 4;
// ÒÑÆÀ¼Û
// 已评价
const ORDER_STATE_EVALUATED = 5; const ORDER_STATE_EVALUATED = 5;
// ÒÑÈ¡Ïû
// 已�消
const ORDER_STATE_CANCEL = 6; const ORDER_STATE_CANCEL = 6;
// ÒѾܵ¥
// 已拒�
const ORDER_STATE_REFUSE = 7; const ORDER_STATE_REFUSE = 7;
// ÍË¿îÖÐ
// 退款中
const ORDER_STATE_REFUNDING = 8; const ORDER_STATE_REFUNDING = 8;
// ÒÑÍË¿î
// 已退款
const ORDER_STATE_REFUNDED = 9; const ORDER_STATE_REFUNDED = 9;
// ¾Ü¾øÍË¿î
// 拒�退款
const ORDER_STATE_UNREFUND = 10; const ORDER_STATE_UNREFUND = 10;
// ¶©µ¥Ö§¸¶·½Ê½
// ΢ÐÅÖ§¸¶
// 订�支付方�
// 微信支付
const ORDER_PAY_WX = 1; const ORDER_PAY_WX = 1;
// Óà¶îÖ§¸¶
// 余�支付
const ORDER_PAY_BALANCE = 2; const ORDER_PAY_BALANCE = 2;
protected $table = 'ims_cjdc_order_main'; protected $table = 'ims_cjdc_order_main';
public $timestamps = false;
protected $fillable = [ protected $fillable = [
'order_num', 'order_num',
'delivery_no', 'delivery_no',
@ -75,6 +77,8 @@ class OrderMain extends Model
'state', 'state',
'time', 'time',
'time_add', 'time_add',
'pay_time',
'jj_note',
'global_order_id', 'global_order_id',
]; ];

14
app/Service/CouponService.php

@ -18,25 +18,13 @@ class CouponService implements CoupnoServiceInterface
// 用户今日使用过的优惠券 // 用户今日使用过的优惠券
$redis = ApplicationContext::getContainer()->get(Redis::class); $redis = ApplicationContext::getContainer()->get(Redis::class);
$couponTodayUsedIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId); $couponTodayUsedIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId);
$currentTime = time(); $currentTime = time();
$builder = Db::table('ims_system_coupon_user_receive as receive') $builder = Db::table('ims_system_coupon_user_receive as receive')
->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id', 'inner'); ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id', 'inner');
if (is_array($fields)&&!empty($fields)) { if (is_array($fields)&&!empty($fields)) {
$builder->select([
'receive.id as receive_id',
'receive.user_id',
'receive.number_remain',
'coupon.id',
'coupon.title',
'coupon.full_amount',
'coupon.discounts',
'coupon.usable_start_time',
'coupon.usable_end_time',
'coupon.discount_type'
]);
$builder->select($fields);
} }
if (is_array($couponTodayUsedIds)&&!empty($couponTodayUsedIds)) { if (is_array($couponTodayUsedIds)&&!empty($couponTodayUsedIds)) {

50
app/Service/OrderService.php

@ -57,7 +57,7 @@ class OrderService implements OrderServiceInterface
// 店铺IDs // 店铺IDs
$dataMain['store_ids'] = ''; $dataMain['store_ids'] = '';
$storeList = json_decode(json_encode($data['store_list']), true);
$storeList = json_decode(html_entity_decode($data['store_list']), true);
if (!is_array($storeList)||empty($storeList)) { if (!is_array($storeList)||empty($storeList)) {
Db::rollBack(); Db::rollBack();
return '订单中商品不存在或已失效'; return '订单中商品不存在或已失效';
@ -72,15 +72,19 @@ class OrderService implements OrderServiceInterface
$currentTime = time(); $currentTime = time();
$dataMain['time'] = date('Y-m-d H:i:s', $currentTime); $dataMain['time'] = date('Y-m-d H:i:s', $currentTime);
$dataMain['time_add'] = $currentTime; $dataMain['time_add'] = $currentTime;
$dataMain['pay_time'] = date('Y-m-d H:i:s', $currentTime);
$dataMain['state'] = OrderMain::ORDER_STATE_UNPAY; $dataMain['state'] = OrderMain::ORDER_STATE_UNPAY;
$dataMain['code'] = $dataMain['global_order_id']; $dataMain['code'] = $dataMain['global_order_id'];
$dataMain['jj_note'] = '';
// 主订单模型保存 // 主订单模型保存
$orderMain = OrderMain::create($dataMain); $orderMain = OrderMain::create($dataMain);
$orderMainId = $orderMain->id; $orderMainId = $orderMain->id;
// 统计订单中所有店铺当日订单数,做店铺订单序号 // 统计订单中所有店铺当日订单数,做店铺订单序号
$countsArr = Order::query()->select('COUNT(*) AS count, id')
$countsArr = Order::query()
->selectRaw('COUNT(*) AS count')
->select('id')
->whereIn('store_id', explode(',', $dataMain['store_ids'])) ->whereIn('store_id', explode(',', $dataMain['store_ids']))
->where(['type' => OrderMain::ORDER_TYPE_ONLINE]) ->where(['type' => OrderMain::ORDER_TYPE_ONLINE])
->whereBetween('time', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')]) ->whereBetween('time', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')])
@ -102,14 +106,14 @@ class OrderService implements OrderServiceInterface
'order_num' => 's'.date('YmdHis', time()) . rand(1111, 9999), 'order_num' => 's'.date('YmdHis', time()) . rand(1111, 9999),
'user_id' => $orderMain->user_id, 'user_id' => $orderMain->user_id,
'store_id' => $item['store_id'], 'store_id' => $item['store_id'],
'order_main_id' => $orderMain,
'order_main_id' => $orderMainId,
'state' => OrderMain::ORDER_STATE_UNPAY, 'state' => OrderMain::ORDER_STATE_UNPAY,
'tel' => $orderMain->tel, 'tel' => $orderMain->tel,
'name' => $orderMain->name, 'name' => $orderMain->name,
'address' => $orderMain->address, 'address' => $orderMain->address,
'area' => $orderMain->area, 'area' => $orderMain->area,
'time' => date("Y-m-d H:i:s"), 'time' => date("Y-m-d H:i:s"),
'note' => $item['note'],
'note' => $item['note'] ?? '',
'delivery_time' => $orderMain->delivery_time, 'delivery_time' => $orderMain->delivery_time,
'type' => $orderMain->type, 'type' => $orderMain->type,
'lat' => $orderMain->lat, 'lat' => $orderMain->lat,
@ -127,10 +131,13 @@ class OrderService implements OrderServiceInterface
'xyh_money' => floatval($item['xyh_money']), 'xyh_money' => floatval($item['xyh_money']),
'oid' => (isset($storeOrderCounts[$item['store_id']]) ? $item['store_id'] : 0) + 1, 'oid' => (isset($storeOrderCounts[$item['store_id']]) ? $item['store_id'] : 0) + 1,
'time_add' => date("Y-m-d H:i:s"), 'time_add' => date("Y-m-d H:i:s"),
'jj_note' => '',
'form_id' => '',
'form_id2' => '',
'code' => '',
]; ];
$order = Order::create($dataChild);
$orderChildId = $order->id;
$orderChildId = Order::query()->insertGetId($dataChild);
// 子订单内商品处理 // 子订单内商品处理
$goodsAmountTotal = 0; $goodsAmountTotal = 0;
@ -140,7 +147,6 @@ class OrderService implements OrderServiceInterface
return '订单商品异常'; return '订单商品异常';
} }
foreach ($item['good_list'] as &$goods) { foreach ($item['good_list'] as &$goods) {
$goodsAmount = bcadd(floatval($goods['money']), floatval($goods['box_money'])); $goodsAmount = bcadd(floatval($goods['money']), floatval($goods['box_money']));
$goodsAmount = bcmul($goodsAmount, $goods['num']); $goodsAmount = bcmul($goodsAmount, $goods['num']);
$goodsAmountTotal = bcadd($goodsAmountTotal, $goodsAmount); $goodsAmountTotal = bcadd($goodsAmountTotal, $goodsAmount);
@ -170,7 +176,7 @@ class OrderService implements OrderServiceInterface
} }
// 校验库存 // 校验库存
foreach ($orderGoods as $Key=>&$goodsItem) {
foreach ($orderGoods as $Key => &$goodsItem) {
$goodsItem['combination_id'] = intval($goodsItem['combination_id']); $goodsItem['combination_id'] = intval($goodsItem['combination_id']);
@ -179,21 +185,27 @@ class OrderService implements OrderServiceInterface
if ($goodsItem['combination_id'] > 0) { if ($goodsItem['combination_id'] > 0) {
$goods = SpecCombination::query() $goods = SpecCombination::query()
->select('id, number AS inventory')
->with(['goods' => function($query){
$query->select(['name', 'is_max']);
}])
->select(['id', 'number AS inventory'])
->where(['id' => $goodsItem['combination_id']]) ->where(['id' => $goodsItem['combination_id']])
->first();
->first()
->toArray();
$goods->name = $goods->goods->name; $goods->name = $goods->goods->name;
$goods->is_max = $goods->goods->is_max; $goods->is_max = $goods->goods->is_max;
} else { } else {
$goods = Goods::query() $goods = Goods::query()
->select('id, name, is_max, inventory')
->select(['id', 'name', 'is_max', 'inventory'])
->where(['id' => $goodsItem['good_id']]) ->where(['id' => $goodsItem['good_id']])
->first();
->first()
->toArray();
} }
var_dump('$goods', $goods);
if (!$goods) { if (!$goods) {
Db::rollBack(); Db::rollBack();
return '缺少商品'; return '缺少商品';
@ -243,7 +255,7 @@ class OrderService implements OrderServiceInterface
$goods['combination_id'] = $value['combination_id']; $goods['combination_id'] = $value['combination_id'];
$orderGoods[] = $goods; $orderGoods[] = $goods;
} }
var_dump('$orderGoods', $orderGoods);
$addOrderGoods = OrderGoods::query()->insert($orderGoods); $addOrderGoods = OrderGoods::query()->insert($orderGoods);
if (!$addOrderGoods) { if (!$addOrderGoods) {
Db::rollBack(); Db::rollBack();
@ -324,7 +336,7 @@ class OrderService implements OrderServiceInterface
} }
Db::commit(); Db::commit();
return $orderMainId;
} catch (Exception $e) { } catch (Exception $e) {
@ -332,16 +344,6 @@ class OrderService implements OrderServiceInterface
return $e->getMessage(); return $e->getMessage();
} }
// 订单成功后处理
if ($orderMainId) {
// 处理喇叭播报
}
return $orderMainId;
} }
/** /**

Loading…
Cancel
Save