model = new Order(); $this->validate = true; $this->validateClass = new OrderValidate(); } /** * @Desc 列表 * @Author Gorden * @Date 2024/3/28 15:01 * * @param Request $request * @return Response * @throws \support\exception\BusinessException */ public function select(Request $request): Response { [$where, $format, $limit, $field, $order] = $this->selectInput($request); if (!empty($where['order_addtimes'])) { $where['order_addtimes'][0] = strtotime($where['order_addtimes'][0]); $where['order_addtimes'][1] = strtotime($where['order_addtimes'][1]); } $order = $request->get('order', 'desc'); $field = $field ?? 'order_addtimes'; // $where['order_id'] = ''; $where['order_category'] = 'VIP'; // $goodsClassify = 'VIP'; $goodsName = $request->get('goods_name', ''); if (!empty($goodsName) && empty($goodsClassify)) { $goodsIds = Goods::where('goods_name', 'like', '%' . $request->get('goods_name') . '%')->pluck('goods_id')->toArray(); } // else if(!empty($goodsClassify)){ // if($goodsClassify == 'SERVICE'){ // $goodsModel = Goods::whereIn('goods_classify',['SERVICE','CHNMED','CHNNCD']); // }else{ // $goodsModel = Goods::where('goods_classify',$goodsClassify); // } // if(!empty($goodsName)){ // $goodsModel->where('goods_name','like','%'.$goodsName.'%'); // } // $goodsIds = $goodsModel->pluck('goods_id')->toArray(); // } if (!empty($goodsIds)) { $orderIds = OrderSheet::whereIn('join_sheet_goods_id', $goodsIds)->pluck('join_sheet_order_id')->toArray(); if ($orderIds) { $where['order_id'] = ['in', implode(',', $orderIds)]; } } $query = $this->doSelect($where, $field, $order); return $this->doFormat($query, $format, $limit); } protected function doSelect(array $where, string $field = null, string $order = 'desc') { $model = $this->model->with([ 'sheets' => function ($query) { $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price'); }, 'member' => function ($query) { $query->select('member_id', 'member_mobile'); }, 'cert' => function ($query) { $query->select('join_cert_member_id', 'member_cert_name'); }, // 'return' => function ($query) use ($where){ // if (isset($where['return'])){ // dump($where['return']); // $query = $query->where('order_return_status',$where['return']); // } // $query->select('orders_return_id', 'join_return_order_id', 'order_return_status'); // }, // 'express' => function ($query) { // $query->select('join_express_order_id', 'order_express_type'); // } ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id') ->leftJoin('order_express', 'order_express.join_express_order_id', '=', 'order.order_id'); // ->leftJoin('order_sheet','join_sheet_order_id','=','order.order_id'); foreach ($where as $column => $value) { if (is_array($value)) { if ($value[0] === 'like' || $value[0] === 'not like') { $model = $model->where($column, $value[0], "%$value[1]%"); } elseif (in_array($value[0], ['>', '=', '<', '<>'])) { $model = $model->where($column, $value[0], $value[1]); } elseif ($value[0] == 'in' && !empty($value[1])) { $valArr = $value[1]; if (is_string($value[1])) { $valArr = explode(",", trim($value[1])); } $model = $model->whereIn($column, $valArr); } elseif ($value[0] == 'not in' && !empty($value[1])) { $valArr = $value[1]; if (is_string($value[1])) { $valArr = explode(",", trim($value[1])); } $model = $model->whereNotIn($column, $valArr); } elseif ($value[0] == 'null') { $model = $model->whereNull($column); } elseif ($value[0] == 'not null') { $model = $model->whereNotNull($column); } elseif ($value[0] !== '' || $value[1] !== '') { $model = $model->whereBetween($column, $value); } } else { $model = $model->where($column, $value); } } if ($field) { $model = $model->orderBy($field, $order); } $model = $model->select('order.*', 'order_express.join_express_order_id', 'order_express.order_express_type', 'order_return.orders_return_id', 'order_return.join_return_order_id', 'order_return.order_return_status', 'order_return.order_return_apply_json', 'order_return.order_return_remark'); return $model; } public function afterQuery($items) { foreach ($items as &$item) { $sheetDeng = ''; $item['sheet'] = $item['sheets'][0] ?? []; if (!empty($item['sheet'])) { $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first(); if (count($item['sheets']) > 1 && $goods->goods_classify == 'MEALS') { $sheetDeng = ' 等餐品'; } $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : ''; $item['sheet']['goods_classify'] = $goods->goods_classify ?? ''; $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']); } unset($item['sheets']); if (isset($item['orders_return_id'])) { $item['return'] = [ 'orders_return_id' => $item['orders_return_id'], 'join_return_order_id' => $item['join_return_order_id'], 'order_return_status' => $item['order_return_status'], 'order_return_apply_json' => $item['order_return_apply_json'], 'order_return_remark' => $item['order_return_remark'] ]; } if (isset($item['join_express_order_id'])) { $item['express'] = [ 'join_express_order_id' => $item['join_express_order_id'], 'order_express_type' => $item['order_express_type'] ]; unset($item['join_express_order_id'], $item['order_express_type']); } // if (!empty($item['order_extend_json'])){ // $orderExtendJson = json_decode($item['order_extend_json'],true); // $item['referee'] = $orderExtendJson['referee'] ?? ''; // } } return $items; } /** * @Desc 下单+支付 * @Author Gorden * @Date 2024/9/5 13:13 * * @param Request $request * @return Response */ public function insert(Request $request): Response { $params = $request->post(); // 判断餐品是否连带着服务或实体 $goodsClassifys = array_unique(array_column($params['goodsContentList'], 'goods_classify')); if (in_array('MEALS', $goodsClassifys) && count($goodsClassifys) > 1) { return json_fail('餐饮餐品不支持和其他类型的产品一起下单'); } if (in_array('MEALS', $goodsClassifys)) { $params['submit_goods_classify'] = 'MEALS'; } if (in_array('PACKAGE', $goodsClassifys)) { $params['submit_goods_classify'] = 'PACKAGE'; } if (!empty($params['dept_premises_id'])) { $premises = SysDept::where('dept_name', $params['dept_premises_id'])->first(); } $params['goods_classify'] = $goodsClassifys[0]; Db::beginTransaction(); try { // 验证线下付款密码 if ($params['settlement_now'] == 'Y' && $params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['OFFLINE', 'MONEY'])) { $password = $params['offline_password']; if ($password != '666888') { throw new BusinessException('密码错误,请重新输入'); } } // 下单账户 if (empty($params['join_order_member_id']) && !empty($params['mobile'])) { if (Member::where('member_mobile', $params['mobile'])->exists()) { throw new BusinessException('会员已存在'); } $params['join_order_member_id'] = $params['member_id'] = 'MR' . date('ymdH') . random_string(6, 'up'); // 创建会员 MemberService::createMember($params); } else if (empty($params['join_order_member_id']) && empty($params['mobile'])) { $params['join_order_member_id'] = Member::where('member_mobile', '0000')->value('member_id'); } if (empty($params['join_order_member_id'])) { throw new BusinessException('检查下单账户'); } $qrcodePayAmount = 0; $params['orderId'] = 'OD' . date('ymdH') . random_string(6, 'up'); $params['orderGroupId'] = 'OD' . date('ymdH') . random_string(6, 'up'); // 查询是否有未完全支付订单 $paidOrder = Order::where('join_order_member_id', $params['join_order_member_id']) ->where('order_category', 'VIP') ->where('order_status_system', 'PAYING') ->first(); if (!empty($paidOrder)) { $paidOrder->order_groupby = $params['orderGroupId']; $params['orderId'] = $paidOrder->order_id; } $systemStatus = 'SENDING'; // 待发货 // 立即结算 if ($params['settlement_now'] == 'Y') { if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'VIP'])) { $params['order_is_complete'] = 'Y'; $systemStatus = 'DONE'; } if ($params['goods_classify'] == 'PACKAGE' && $params['delivery'] == 'ARRIVAL') { $systemStatus = "WAITING"; } } if ($params['settlement_now'] == 'Y' && ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY')) { if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) { $params['pay_category'] = $params['pay_category_sub']; } $params['order_status_system'] = $systemStatus; $params['order_status_payment'] = 'SUCCESS'; } if (($params['pay_constitute'] == 'Y' || $params['pay_category'] == 'QRCODE') && $params['settlement_now'] == 'Y' && !empty($params['qrcode_nbr'])) { // 付款码 // 提交过来的支付分类 $submitPayCategory = $params['pay_category']; // 账户支付的金额 $accountAmount = $params['order_amount_pay']; if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0) { // 组合支付,支付金额改成需要付款码需要支付的金额 $params['order_amount_pay'] = $qrcodePayAmount; } if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount <= 0) { $params['order_status_system'] = $systemStatus; $params['order_status_payment'] = 'SUCCESS'; } // 不组合或者组合后需要付款码的金额>0 if ($params['pay_constitute'] == 'N' || ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0)) { $result = OrderService::qrcodePay($params); $result = json_encode($result); $params['pay_json_response'] = $result; $result = json_decode($result, true); $prefix = substr($params['qrcode_nbr'], 0, 2); if (in_array($prefix, [10, 11, 12, 13, 14, 15])) { $params['pay_category'] = 'WXPAY'; if ((!isset($result['return_code']) || $result['return_code'] != 'SUCCESS') || (!isset($result['result_code']) || $result['result_code'] != 'SUCCESS') || (empty($result['trade_state']) || $result['trade_state'] != 'SUCCESS')) { $params['order_status_system'] = 'PAYING'; $params['order_status_payment'] = 'PENDING'; $params['order_is_complete'] = 'N'; // Db::rollBack(); // return json_fail('支付失败'); } else { $params['order_status_system'] = $systemStatus; $params['order_status_payment'] = 'SUCCESS'; } } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) { $params['pay_category'] = 'ALIPAY'; if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) { $params['order_status_system'] = 'PAYING'; $params['order_status_payment'] = 'PENDING'; $params['order_is_complete'] = 'N'; // Db::rollBack(); // return json_fail('支付失败'); } else { $params['order_status_system'] = $systemStatus; $params['order_status_payment'] = 'SUCCESS'; } } else { throw new BusinessException('付款码无效'); } // 账户支付的金额 $params['order_amount_pay'] = $accountAmount; } } $orderConfigJson = []; // 配送方式 if (isset($params['delivery']) && ($params['delivery'] == 'PICKUP' || $params['delivery'] == 'ARRIVAL')) { // 自提/ 到店 $orderConfigJson['premises'] = $params['dept_premises_id']; } else if (isset($params['delivery']) && $params['delivery'] == 'LOGISTICS') { // $params['order_express_json'] = json_encode([ // 'express' => 'Y', // ]); } $params['order_config_json'] = json_encode($orderConfigJson); if (empty($paidOrder)) { // 写入主订单 $this->insertMain($params); // 订单详情 $this->insertSheet($params); } else { $params['orderGroupId'] = $paidOrder->order_groupby; $params['order_amount_paid'] = $paidOrder->order_amount_paid; $paidOrder->order_amount_paid = $paidOrder->order_amount_paid + $params['order_amount_pay']; $paidOrder->order_amount_pay = $paidOrder->order_amount_paid; if (floatval($paidOrder->order_amount_paid) >= $paidOrder->order_amount_total) { $paidOrder->order_is_complete = 'Y'; $paidOrder->order_status_system = 'DONE'; $paidOrder->order_status_payment = 'SUCCESS'; // 更新sheet OrderSheet::where('join_sheet_order_id', $paidOrder->order_id)->update(['order_sheet_status' => 'DONE']); } $paidOrder->save(); } // 支付记录 $this->insertPayDetail($params); // $params['order_express_goods'] = json_encode(['sheet' => [$sheetId]]); // 2.4W 康养城 if ($params['goods_classify'] == 'VIP' && $params['order_status_payment'] == 'SUCCESS' && (!empty($paidOrder) && floatval($paidOrder->order_amount_paid) >= $paidOrder->order_amount_total || floatval($params['order_amount_pay']) >= $params['order_amount_total'])) { $params['member_id'] = $params['join_order_member_id']; Event::dispatch('order.kangyangCityVIP.grant', $params); } Db::commit(); if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') { _syslog("订单", "支付异常,检查是否有轮询"); return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]); } _syslog("订单", "创建订单成功"); return json_success('创建订单成功'); } catch (BusinessException $e) { Db::rollBack(); dump($e->getMessage()); dump($e->getTrace()); _syslog("订单", $e->getMessage()); return json_fail($e->getMessage()); } catch (\Exception $e) { Db::rollBack(); dump($e->getMessage()); dump($e->getTrace()); _syslog("订单", "创建订单失败"); return json_fail('创建订单失败'); } } /** * @Desc 支付 * @Author Gorden * @Date 2024/9/5 13:12 * * @param Request $request * @return Response */ public function pay(Request $request) { $params = $request->post(); // 验证线下付款密码 if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['OFFLINE', 'MONEY'])) { $password = $params['offline_password']; if ($password != '666888') { return json_fail("密码错误,请重新输入"); } } $order = Order::where('order_id', $params['order_id'])->first(); if (!$order) { return json_fail('订单异常'); } if ($order->order_status_system != 'PAYING') { return json_fail('订单不是可支付状态'); } $params['orderId'] = $params['order_id']; $params['orderGroupId'] = 'OD' . date('ymdH') . random_string(6, 'up'); $order->order_groupby = $params['orderGroupId']; $goods = Goods::where('goods_id', $params['join_sheet_goods_id']) ->select('goods_id', 'goods_name', 'goods_classify') ->first(); if (!$goods) { return json_fail('产品数据异常'); } $goods = $goods->toArray(); $params['goods_classify'] = $goods['goods_classify'] ?? ''; $systemStatus = 'SENDING'; // 待发货 // 立即结算 if (in_array($params['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'VIP'])) { $order->order_is_complete = 'Y'; $systemStatus = 'DONE'; } if ($params['goods_classify'] == 'PACKAGE' && $params['delivery'] == 'ARRIVAL') { $systemStatus = "WAITING"; } $paymentStatus = 'PENDING'; Db::beginTransaction(); try { // 组合支付时,付款码应收金额 $qrcodePayAmount = 0; if ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY') { $order->order_status_system = $systemStatus; $order->order_status_payment = 'SUCCESS'; $paymentStatus = 'SUCCESS'; if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) { $params['pay_category'] = $params['pay_category_sub']; } } if (($params['pay_constitute'] == 'Y' || $params['pay_category'] == 'QRCODE') && !empty($params['qrcode_nbr'])) { // 付款码 // 提交过来的支付分类 $submitPayCategory = $params['pay_category']; // 账户支付的金额 $accountAmount = $params['order_amount_pay']; if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0) { // 组合支付,改成需要付款码需要支付的金额 $params['order_amount_pay'] = $qrcodePayAmount; } // 去支付 $result = OrderService::qrcodePay($params); $result = json_encode($result); $params['pay_json_response'] = $result; $result = json_decode($result, true); $prefix = substr($params['qrcode_nbr'], 0, 2); if (in_array($prefix, [10, 11, 12, 13, 14, 15])) { $params['pay_category'] = 'WXPAY'; if ((!isset($result['return_code']) || $result['return_code'] != 'SUCCESS') || (!isset($result['result_code']) || $result['result_code'] != 'SUCCESS') || (empty($result['trade_state']) || $result['trade_state'] != 'SUCCESS')) { $order->order_status_system = 'PAYING'; $order->order_status_payment = 'PENDING'; $order->order_is_complete = 'N'; // Db::rollBack(); // return json_fail('支付失败'); } else { $order->order_status_system = $systemStatus; $order->order_status_payment = 'SUCCESS'; $paymentStatus = 'SUCCESS'; } } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) { $params['pay_category'] = 'ALIPAY'; if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) { $order->order_status_system = 'PAYING'; $order->order_status_payment = 'PENDING'; $order->order_is_complete = 'N'; // Db::rollBack(); // return json_fail('支付失败'); } else { $order->order_status_system = $systemStatus; $order->order_status_payment = 'SUCCESS'; $paymentStatus = 'SUCCESS'; } } else { throw new BusinessException('付款码无效'); } // 账户支付的金额 $params['order_amount_pay'] = $accountAmount; } $orderConfigJson = []; if (!empty($order->order_config_json)) { $orderConfigJson = json_decode($order->order_config_json, true); } $orderConfigJson['preferential'] = $params['preferential'] ?? ''; if (isset($orderConfigJson['tableid']) && !empty($orderConfigJson['tableid'])) { SysDept::where('dept_id', $orderConfigJson['tableid'])->where('dept_category', '桌台')->update(['dept_status' => 'ACTIVED']); } $order->order_config_json = json_encode($orderConfigJson); $order->order_amount_paid = $order->order_amount_paid + $params['order_amount_pay']; $order->order_amount_pay = $order->order_amount_paid; // 康养城订单,支付完就结束了 if ($order->order_status_payment == 'SUCCESS' && $params['goods_classify'] == 'VIP' && floatval($order->order_amount_paid) >= $order->order_amount_total) { $order->order_is_complete = 'Y'; } if (floatval($order->order_amount_paid) < $order->order_amount_total) { $order->order_status_system = 'PAYING'; $order->order_status_payment = 'PENDING'; $order->order_is_complete = 'N'; } // 主订单 $order->save(); // sheet if ($order->order_status_payment == 'SUCCESS') { $data = [ 'order_sheet_status' => $systemStatus ]; if (floatval($order->order_amount_paid) < $order->order_amount_total) { $data['order_sheet_status'] = 'BEING'; } OrderSheet::where('join_sheet_order_id', $params['order_id'])->update($data); } // payDetail $payData = [ 'pay_amount' => $params['order_amount_pay'] ]; if ($paymentStatus == 'SUCCESS') { $payData['pay_paytimes'] = date('Y-m-d H:i:s'); $payData['pay_status'] = 'SUCCESS'; } if ($params['pay_constitute'] == 'N' && in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) { $payData['pay_prepayid'] = $params['pay_category']; $payData['pay_json_response'] = $params['pay_json_response']; } else if ($params['pay_category'] == 'CASH') { $payData['pay_prepayid'] = $params['join_order_member_id'] . '-CASH'; } else if ($params['pay_category'] == 'WELFARE') { $payData['pay_prepayid'] = $params['join_order_member_id'] . '-WELFARE'; } else if ($params['pay_category'] == 'CARD') { $payData['pay_prepayid'] = $params['card_nbr']; } else if ($params['pay_category'] == 'OFFLINE') { $payData['pay_prepayid'] = 'OFFLINE'; } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') { $payData['pay_prepayid'] = 'OFFLINE_ALIPAY'; } else if ($params['pay_category'] == 'OFFLINE_WXPAY') { $payData['pay_prepayid'] = 'OFFLINE_WXPAY'; } else if ($params['pay_category'] == 'MONEY') { $payData['pay_prepayid'] = 'MONEY'; } // 如果 是APP 过来的菜订单,可能没有paydetail // if (!PayDetail::where('join_pay_order_id', $order->order_groupby)->where('pay_category', '<>', 'WXPAY')->where('pay_category', '<>', 'ALIPAY')->exists()) { $payData['join_pay_member_id'] = $params['join_order_member_id']; $payData['join_pay_order_id'] = $order->order_groupby; $payData['pay_status'] = $payData['pay_status'] == 'SUCCESS' ? $payData['pay_status'] : 'WAITING'; $payData['pay_category'] = $params['goods_classify'] ?? ''; $payData['pay_json_request'] = json_encode($params); // {"pay-result": "支付成功", "result-datetime": "2024-07-29 18:38:21"} $payData['pay_json_response'] = $payData['pay_status'] == 'SUCCESS' ? json_encode([ 'pay-result' => '支付成功', 'result-datetime' => date('Y-m-d H:i:s') ]) : '[]'; $payData['join_pay_object_json'] = !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]'; $payData['pay_addtimes'] = time(); PayDetail::insert($payData); // 2.4W 康养城 if ($params['goods_classify'] == 'VIP' && $order->order_status_payment == 'SUCCESS' && floatval($order->order_amount_paid) >= $order->order_amount_total) { $params['member_id'] = $params['join_order_member_id']; Event::dispatch('order.kangyangCityVIP.grant', $params); } Db::commit(); if ($order->order_is_complete == 'Y' && $order->order_status_payment == 'SUCCESS') { Event::dispatch('order.complete', $params); } if ($order->order_status_payment != 'SUCCESS' && $paymentStatus != 'SUCCESS') { _syslog("订单", "支付异常,检查是否有轮询"); return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]); } _syslog("订单", "订单支付成功"); return json_success('支付成功'); } catch (BusinessException $e) { dump($e->getMessage()); Db::rollBack(); _syslog("订单", "订单支付失败:" . $e->getMessage()); return json_fail("支付失败:" . $e->getMessage()); } catch (\Exception $e) { dump($e->getMessage()); Db::rollBack(); _syslog("订单", "订单支付失败"); return json_fail('支付失败'); } } /** * @Desc * @Author Gorden * @Date 2024/6/7 10:30 * * @param $params * @return void * @throws BusinessException */ public function insertMain($params) { try { $orderCategory = 'NORMAL'; if (!empty($params['order_category'])) { $orderCategory = $params['order_category']; } else if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') { $orderCategory = 'DISHES'; } else if (isset($params['goods_classify'])) { $orderCategory = $params['goods_classify']; } if (empty($params['order_extend_json'])) { $params['order_extend_json'] = []; } else { if (is_json($params['order_extend_json'])) { $params['order_extend_json'] = json_decode($params['order_extend_json'], true); } } // 推荐人 $params['order_extend_json']['referee'] = $params['referee'] ?? ''; $data = [ 'order_id' => $params['orderId'], 'order_groupby' => $params['orderGroupId'], 'join_order_member_id' => $params['join_order_member_id'], 'order_name' => date('Y-m-d H:i:s') . '-订单', 'order_amount_total' => $params['order_amount_total'], 'order_amount_pay' => $params['order_amount_pay'], 'order_amount_paid' => $params['order_amount_pay'], 'order_category' => $orderCategory, 'order_classify' => $orderCategory, 'order_is_complete' => floatval($params['order_amount_pay']) >= floatval($params['order_amount_total']) ? 'Y' : 'N', 'order_status_system' => floatval($params['order_amount_pay']) >= floatval($params['order_amount_total']) ? 'DONE' : 'PAYING', 'order_status_payment' => floatval($params['order_amount_pay']) >= floatval($params['order_amount_total']) ? 'SUCCESS' : 'PENDING', 'order_status_storage' => $params['order_status_storage'], 'order_platform' => $params['order_platform'], 'order_remark' => $params['order_remark'] ?? '', 'order_discount_json' => $params['order_discount_json'] ?? '[]', 'order_config_json' => $params['order_config_json'] ?? '[]', 'order_express_json' => $params['order_express_json'] ?? '[]', 'order_extend_json' => $params['order_extend_json'] ? json_encode($params['order_extend_json']) : '[]', 'order_addtimes' => time() ]; Order::insert($data); } catch (\Exception $e) { dump($e->getMessage()); throw new BusinessException('订单创建信息失败'); } } /** * @Desc * @Author Gorden * @Date 2024/6/7 10:25 * * @param $params * @return void * @throws BusinessException */ public function insertSheet($params) { try { $orderSheetIds = []; foreach ($params['goodsContentList'] as $goods) { //{"unit": "份", "table": null, "premises": "15"} $price = floatval($goods['goods_sales_price']); $extendJson['unit'] = $goods['sku_name']; if (isset($params['submit_premises_id'])) { $extendJson['premises'] = $params['submit_premises_id']; } if (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') { $extendJson['table'] = null; } $orderSheetStatus = 'PAYING'; if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS') { if (floatval($params['order_amount_pay']) >= floatval($params['order_amount_total'])) { $orderSheetStatus = 'DONE'; } else { $orderSheetStatus = 'BEING'; } } $data = [ 'join_sheet_member_id' => $params['join_order_member_id'], 'join_sheet_order_id' => $params['orderId'], 'join_sheet_goods_id' => $goods['goods_id'], 'join_sheet_goods_sku_id' => $goods['sku_id'], 'order_sheet_status' => $orderSheetStatus, 'order_sheet_category' => (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') ? 'DISHES' : 'NORMAL', 'order_sheet_num' => $goods['nbr'], 'order_sheet_price' => $goods['goods_sales_price'], 'order_sheet_amount' => $price * $goods['nbr'], 'order_sheet_pay' => $price * $goods['nbr'], 'order_sheet_task_status' => 'NONE', 'order_sheet_remark' => $params['order_remark'] ?? '', 'order_sheet_addtimes' => time(), 'order_sheet_extend_json' => json_encode($extendJson) ]; $orderSheetId = OrderSheet::insertGetId($data); $orderSheetIds[] = $orderSheetId; // 减库存,规格和总库存 if (!isset($params['submit_goods_classify']) || !in_array($params['submit_goods_classify'], ['MEALS', 'PACKAGE'])) { $goodsSku = GoodsSku::where('goods_sku_id', $goods['sku_id'])->first(); $skuStorageJson = json_decode($goodsSku->goods_sku_storage_json, true); if (isset($skuStorageJson['storage']) && !empty($skuStorageJson['storage'])) { $skuStorageJson['storage'] = $skuStorageJson['storage'] - $goods['nbr']; } if (!isset($skuStorageJson['storage']) || (!empty($skuStorageJson['storage']) && $skuStorageJson['storage'] < 0)) { throw new BusinessException('库存不足'); } $goodsSku->goods_sku_storage_json = json_encode($skuStorageJson); $goodsSku->save(); } $goodsRunning = GoodsRunning::where('join_running_goods_id', $goods['goods_id'])->first(); $goodsRunning->goods_running_storage = $goodsRunning->goods_running_storage - $goods['nbr']; if ($goodsRunning->goods_running_storage < 0) { throw new BusinessException('库存不足'); } $goodsRunning->goods_running_sale = $goodsRunning->goods_running_sale + $goods['nbr']; $goodsRunning->save(); } return $orderSheetIds; } catch (\support\exception\BusinessException $e) { dump($e->getMessage() . '||' . $e->getLine()); throw new BusinessException($e->getMessage()); } catch (\Exception $e) { dump($e->getMessage() . '||' . $e->getLine()); throw new BusinessException('订单创建失败'); } } /** * @Desc * @Author Gorden * @Date 2024/6/7 10:35 * * @param $params * @return void * @throws BusinessException */ public function insertPayDetail($params) { try { if (in_array($params['pay_category'], ['WXPAY', 'ALIPAY'])) { $payPrepayid = $params['pay_category']; } else if ($params['pay_category'] == 'OFFLINE') { $payPrepayid = 'OFFLINE'; } else if ($params['pay_category'] == 'OFFLINE_ALIPAY') { $payPrepayid = 'OFFLINE_ALIPAY'; } else if ($params['pay_category'] == 'OFFLINE_WXPAY') { $payPrepayid = 'OFFLINE_WXPAY'; } else if ($params['pay_category'] == 'MONEY') { $payPrepayid = 'MONEY'; } else { $payPrepayid = $params['join_order_member_id'] . '-' . $params['pay_category']; } $data = [ 'join_pay_member_id' => $params['join_order_member_id'], 'join_pay_order_id' => $params['orderGroupId'], 'pay_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING', 'pay_category' => $params['goods_classify'], 'pay_amount' => $params['order_amount_pay'], 'pay_prepayid' => $payPrepayid, 'pay_paytimes' => date('Y-m-d H:i:s'), 'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]', 'pay_json_request' => json_encode($params), 'pay_json_response' => $params['pay_json_response'] ?? '[]', 'pay_remark' => $params['order_remark'] ?? '', 'pay_addtimes' => time(), ]; PayDetail::insert($data); } catch (\Exception $e) { dump($e->getMessage()); throw new BusinessException('创建支付记录失败'); } } /** * @Desc 订单商品详情 * @Author Gorden * @Date 2024/3/29 8:50 * * @param Request $request * @return Response */ public function sheet(Request $request) { try { $orderId = $request->get('order_id'); $orderSheet = OrderSheet::with([ 'member' => function ($query) { $query->select('member_id', 'member_mobile', 'member_is_owner', 'join_member_role_id'); }, 'goods' => function ($query) { $query->select('goods_id', 'goods_name', 'goods_cover', 'goods_market_price', 'goods_sales_price', 'goods_classify', 'goods_if_express'); }, 'memberInfo', 'cert', 'sku' => function ($query) { $query->where('goods_sku_status', 'ON') ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price'); }, 'skus', 'refund' => function ($query) { $query->select('join_return_order_id', 'order_return_status'); } ])->where('join_sheet_order_id', $orderId) ->orderBy('order_sheet_id', 'DESC') ->get() ->toArray(); $order = Order::where('order_id', $orderId)->first(); $express = OrderExpress::where('join_express_order_id', $orderId)->first(); $premises = ''; if ($express && $express->order_express_type == '自提') { $premises = $express->order_express_company; } $sheetAmount = 0; foreach ($orderSheet as &$item) { $sheetAmount += $item['order_sheet_amount']; $item['goods']['goods_cover'] = getenv('STORAGE_DOMAIN') . $item['goods']['goods_cover']; if (!empty($item['goods']) && $item['goods']['goods_classify'] == 'PACKAGE') { $components = GoodsComponent::with('goods') ->where('join_component_master_goods_id', $item['goods']['goods_id']) ->select('join_component_master_goods_id', 'join_component_goods_id', 'goods_component_price', 'goods_component_price', 'goods_component_json') ->get() ->toArray(); $goodsArr = []; foreach ($components as $component) { $configJson = !empty($component['goods_component_json']) ? json_decode($component['goods_component_json'], true) : []; if (!empty($component['goods'])) { $supplierName = Supplier::where('supplier_id', $component['goods']['join_goods_supplier_id'])->value('supplier_name'); $benefit = MemberBenefit::where('join_benefit_member_id', $item['join_sheet_member_id']) ->where('join_benefit_goods_id', $component['goods']['goods_id']) ->where('join_benefit_order_id', $orderId) ->first(); $goodsArr[] = [ 'goods_name' => $component['goods']['goods_name'], 'goods_cover' => getenv('STORAGE_DOMAIN') . $component['goods']['goods_cover'], 'supplier_name' => $supplierName, 'nbr' => $configJson['nbr'] ?? 0, 'used' => !empty($benefit->member_benefit_used_count) ? intval($benefit->member_benefit_used_count) : '' ]; } } $item['goods']['components'] = $goodsArr; } if (!empty($item['sku'])) { if (!empty($item['sku']['goods_sku_specs_json'])) { $specsJson = json_decode($item['sku']['goods_sku_specs_json'], true); $skuName = ''; foreach ($specsJson as $specsKey => $skuSpecs) { if (is_array($skuSpecs)) { $skuName = $skuName . ' ' . implode(' ', $skuSpecs) . ';'; } else { $skuName = $skuName . ' ' . $skuSpecs . ';'; } } $item['sku']['goods_sku_title'] = rtrim($skuName, ';'); } } if (!empty($item['skus'])) { foreach ($item['skus'] as $key => $skus) { if (!empty($skus['goods_sku_specs_json'])) { $item['skus'][$key]['goods_sku_specs_json'] = json_decode($skus['goods_sku_specs_json']); $skuName = ''; foreach ($item['skus'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) { $keyStr = ($specsKey == '规格') ? '' : ($specsKey . ':'); if (is_array($skuSpecs)) { $skuName = $skuName . $keyStr . ' ' . implode(' ', $skuSpecs) . '; '; } else { $skuName = $skuName . $keyStr . ' ' . $skuSpecs . '; '; } } $item['skus'][$key]['sku_name'] = rtrim($skuName, '; '); } } } else { $item['skus'] = []; } if (in_array($item['goods']['goods_classify'], ['SERVICE', 'CHNMED', 'CHNNCD', 'PACKAGE']) && $order->order_status_system == 'DONE') { $item['appontment'] = []; $appontments = Appointment::where('join_appointment_order_id', $orderId) ->where('appointment_status', 'DONE') ->select('appointment_status', 'appointment_id', 'appointment_done_datetime', 'appointment_done_json', 'appointment_apply_json') ->get() ->toArray(); foreach ($appontments as $appontment) { $doneJson = []; $username = ''; if (!empty($appontment['appointment_done_json'])) { $doneJson = json_decode($appontment['appointment_done_json'], true); if (isset($doneJson['charge'])) { $username = SysUser::where('user_id', $doneJson['charge']['charge_user_id'])->value('user_name'); } } $person = 1; if (!empty($appontment->appointment_apply_json)) { $applyJson = json_decode($appontment->appointment_apply_json, true); if (isset($applyJson['person'])) { $person = $applyJson['person']; } } $item['appontment'][] = [ 'member' => ($item['cert'] ? $item['cert']['member_cert_name'] . '-' : '') . ($item['member'] ? $item['member']['member_mobile'] : ''), 'goods_name' => $item['goods']['goods_name'], 'premisses' => isset($doneJson['charge']) ? $doneJson['charge']['charge_premises'] : '', 'username' => $username, 'nbr' => $person, 'done_time' => $appontment['appointment_done_datetime'], 'appointment_status' => $appontment['appointment_status'], ]; } } if (!empty($item['order_sheet_extend_json']) && !$express) { $extendJson = json_decode($item['order_sheet_extend_json'], true); if (isset($extendJson['address_id'])) { $address = ClientConfig::where('client_config_id', $extendJson['address_id'])->first(); if (!empty($address)) { $valJson = json_decode($address->client_config_val_json, true); $express = [ 'order_express_address' => $valJson['address'] . ($valJson['numbers'] ?? ''), 'order_express_city' => $valJson['city'], 'order_express_mobile' => $valJson['mobile'], 'order_express_person' => $valJson['person'] ]; } } } $item['member']['level'] = '普通用户'; if (!empty($item['member']['join_member_role_id'])) { $item['member']['level'] = MemberRole::where('member_role_id', $item['member']['join_member_role_id'])->value('member_role_name'); } } $order->sheet_amount = number_format($sheetAmount, 2); $payDetails = PayDetail::whereJsonContains('join_pay_object_json->order_id', $order->order_id) ->where('pay_category', 'VIP') ->where('pay_status', 'SUCCESS') ->select('pay_id', 'pay_category', 'pay_prepayid', 'pay_paytimes', 'pay_status', 'pay_amount', 'pay_extend_json') ->orderBy('pay_addtimes', 'DESC') ->get(); $order->pay_amount_total = 0; foreach ($payDetails as &$payDetail) { $categoryArray = explode('-', $payDetail->pay_prepayid); if (isset($categoryArray[1])) { $payDetail->pay_category = $categoryArray[1]; } else if (in_array($categoryArray[0], ['WXPAY', 'ALIPAY', 'OFFLINE', 'OFFLINE_ALIPAY', 'OFFLINE_WXPAY', 'MONEY'])) { $payDetail->pay_category = $categoryArray[0]; } if (!empty($payDetail->pay_extend_json)) { $payExtendJson = json_decode($payDetail->pay_extend_json, true); $order->cancel_times = $payExtendJson['cancel_times'] ?? ''; } $order->pay_amount_total += $payDetail->pay_amount; } $refund = OrderReturn::where('join_return_order_id', $orderId) ->select('order_return_status', 'order_return_apply_datetime', 'order_return_apply_json', 'order_return_accept_datetime', 'order_return_refund_json', 'order_return_extend_json') ->first(); if (!empty($refund->order_return_refund_json)) { $returnRefundJson = json_decode($refund->order_return_refund_json, true); if (isset($returnRefundJson['user_id'])) { $userName = SysUser::where('user_id', $returnRefundJson['user_id'])->value('user_name'); $returnRefundJson['person'] = $userName; unset($returnRefundJson['user_id']); } $refund->order_return_refund_json = json_encode($returnRefundJson); } if (!empty($order->order_config_json)) { $orderConfigJson = json_decode($order->order_config_json, true); if (isset($orderConfigJson['reach'])) { $order->reach = $orderConfigJson['reach']; } if (isset($orderConfigJson['table'])) { $order->table = $orderConfigJson['table']; } if (isset($orderConfigJson['eat'])) { $order->eat = $orderConfigJson['eat']; } if (isset($orderConfigJson['express'])) { $order->express = $orderConfigJson['express']; } if (isset($orderConfigJson['premises'])) { $order->premises = $orderConfigJson['premises']; } if (isset($orderConfigJson['tableid'])) { $order->dept_table_id = $orderConfigJson['tableid']; } } if (!empty($order->order_extend_json)) { $orderExtendJson = json_decode($order->order_extend_json, true); $order->referee = $orderExtendJson['referee'] ?? ''; if (isset($orderExtendJson['cancel_times'])) { $order->cancel_times = $orderExtendJson['cancel_times']; } if (isset($orderExtendJson['free_remark'])) { $order->free_remark = $orderExtendJson['free_remark']; } } $discount = ['coupon_name' => '', 'classify' => '', 'value' => 0]; if (!empty($order->order_discount_json)) { $orderDiscountJson = json_decode($order->order_discount_json, true); foreach ($orderDiscountJson as $discountItem) { if (!empty($discountItem['coupon_id'])) { $coupon = Coupon::where('coupon_id', $discountItem['coupon_id']) ->select('coupon_name', 'coupon_classify', 'coupon_category', 'coupon_value', 'coupon_minimum_limit') ->first(); if (!$coupon) { continue; } $classify = CouponService::couponClassifyInfo($coupon->coupon_classify, $coupon->coupon_category, $coupon->coupon_value, $coupon->coupon_minimum_limit); $discount['coupon_name'] .= $coupon->coupon_classify . ':' . $coupon->coupon_name . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), '; } if ($discountItem['coupon_classify'] == '退款') { continue; } if (empty($discountItem['coupon_id']) && !empty($discountItem['coupon_classify'])) { if (!empty($discountItem['coupon_detail_id'])) { $discount['classify'] .= $discountItem['coupon_detail_id'][0] . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), '; } else { $discount['classify'] .= $discountItem['coupon_classify'] . '(优惠¥' . sprintf("%.2f", $discountItem['coupon_value']) . '), '; } } if (!empty($discountItem['coupon_value'])) { $discount['value'] += $discountItem['coupon_value']; } } if (!empty($discount['coupon_name'])) { $discount['coupon_name'] = rtrim($discount['coupon_name'], ', '); } if (!empty($discount['classify'])) { $discount['classify'] = rtrim($discount['classify'], ', '); } } $discount['value'] = sprintf("%.2f", $discount['value']); $order->discount = $discount; $order->premises = $order->premises ?? $premises; $data = [ 'order' => $order, 'refund' => json_decode(json_encode($refund)), 'sheet' => json_decode(json_encode($orderSheet)), 'express' => json_decode(json_encode($express)), 'payDetails' => json_decode(json_encode($payDetails)) ]; return json_success('', $data); } catch (\Exception $e) { dump($e->getMessage()); } } /** * @Desc 获取分期的订单 * @Author Gorden * @Date 2024/9/10 15:14 * * @param Request $request * @return Response */ public function getPaidOrder(Request $request) { $memberId = $request->get('member_id'); if (!$memberId) { return json_fail('参数异常'); } $order = Order::where('join_order_member_id', $memberId) ->where('order_category', 'VIP') ->where('order_status_system', 'PAYING') ->select('order_id', 'order_amount_total', 'order_amount_paid', 'order_amount_pay') ->first(); return json_success('', $order); } }