'酒店订单', 'hotel_canteen_order' => '餐厅订单', 'university_event_apply' => '活动订单', 'offline_shop_order' => '线下订单', ]; /** * 主表订单对应各自店铺表 */ public const ORDER_SHOP = [ 'hotel_order' => 'hotel', 'hotel_canteen_order' => 'hotel_canteen', 'university_event_apply' => 'university_event', 'offline_shop_order' => 'offline_shop', ]; protected int $userId = 0; protected string $orderNo = ''; protected string $tableName = ''; public function __construct() {} /** * 获取门店信息(根绝tableName) * @param string $tableName * @param int $shopId * @return array|bool|\PDOStatement|string|\think\Model|null */ public function getShopInfo(string $tableName,int $shopId) { $tableName = self::ORDER_SHOP[$tableName]; return Db::name($tableName)->where(['id' => $shopId])->find(); } /** * 订单成交 * @param int $bill_id * @param string $third_no * @return bool * @throws \think\Exception * @throws \think\exception\PDOException */ public function deal(string $bill_order_no, string $third_no = '') { $model = new BillModel(); $bill = $model->getDetail(['order_no' => $bill_order_no]); if (!$bill || $bill['status'] !== 0) { return $this->error('订单已支付'); } $update = [ 'status' => 1, 'third_no' => $third_no, 'pay_time' => time(), ]; switch ($bill['table_name']) { case 'hotel_order': case 'hotel_canteen_order': case 'university_event_apply': break; case 'offline_shop_order': $update['back_status'] = 1; break; } if (!$model->where('order_no', $bill_order_no)->update($update)){ return $this->error('订单支付失败'); } if (!Db::name($bill['table_name'])->where('id', $bill['table_id'])->update(['is_pay'=>1,'pay_time'=>time()])){ return $this->error('订单支付失败'); } return $this->success('支付成功'); } /** * 创建订单 * @param int $userId * @param string $orderNo * @param string $tableName * @return bool */ public function createOrder(int $userId, string $orderNo, string $tableName) { $this->userId = $userId; $this->orderNo = $orderNo; $this->tableName = $tableName; if (!$this->$tableName()) { return $this->error($this->getMessage(), $this->getData()); } return $this->success($this->getMessage(), $this->getData()); } // 酒店订单 private function hotel_order() { // 主表校验 $model = new HotelOrderModel(); $order = $model->getDetail( params: ['order_no', $this->orderNo], with : [ 'hotel', 'room' ] ); if (!$order) { return $this->error('订单不存在或已取消'); } if ($order['is_pay'] == 1) { return $this->error('订单已支付'); } $billModel = new BillModel(); $bill = $billModel->getDetail( params: ['table_id' => $order['id'], 'table_name' => $this->tableName], ); if (!empty($bill) && $bill['status'] !== 0) { return $this->error('订单已支付'); } // 如果没有订单 则需要新创建支付订单 if ($bill) { $billId = $bill['id']; } else { $bill = [ 'user_id' => $this->userId, 'order_no' => Common::createOrderNo('B'), 'total_amount' => $order['pay_amount'], 'pay_amount' => $order['pay_amount'], 'createtime' => time(), 'num' => $order['num'], 'table_name' => $this->tableName, 'table_id' => $order['id'], 'shop_id' => $order['hotel']['id'], 'shop_name' => $order['hotel']['name'], 'shop_logo' => $order['hotel']['image'], 'back_rate' => $order['hotel']['back_rate'], 'args' => json_encode([ [ 'image' => $order['room']['image'],// 图片 'name' => $order['room']['name'],// 规格名 'num' => $order['num'],// 购买数量 // 酒店独有 'days' => $order['days'], 'start_date' => $order['start_date'], 'end_date' => $order['end_date'], ] ], JSON_UNESCAPED_UNICODE), ]; if (!$billId = $billModel->insertGetId($bill)) { return $this->error('支付订单创建失败'); } } return $this->success('支付订单创建成功', [ 'bill_id' => $billId, ]); } // 餐厅订单 private function hotel_canteen_order() { // 主表校验 $model = new HotelCanteenOrderModel(); $order = $model->getDetail( params: ['order_no', $this->orderNo], with : [ 'canteen', 'room' ] ); if (!$order) { return $this->error('订单不存在或已取消'); } if ($order['is_pay'] == 1) { return $this->error('订单已支付'); } $billModel = new BillModel(); $bill = $billModel->getDetail( params: ['table_id' => $order['id'], 'table_name' => $this->tableName], ); if (!empty($bill) && $bill['status'] !== 0) { return $this->error('订单已支付'); } // 如果没有订单 则需要新创建支付订单 if ($bill) { $billId = $bill['id']; } else { $bill = [ 'user_id' => $this->userId, 'order_no' => Common::createOrderNo('B'), 'total_amount' => $order['pay_amount'], 'pay_amount' => $order['pay_amount'], 'createtime' => time(), 'num' => 1, 'table_name' => $this->tableName, 'table_id' => $order['id'], 'shop_id' => $order['canteen']['id'], 'shop_name' => $order['canteen']['name'], 'shop_logo' => $order['canteen']['image'], 'back_rate' => $order['canteen']['back_rate'], 'args' => json_encode([ [ 'image' => $order['room']['image'],// 图片 'name' => $order['room']['name'],// 规格名 'num' => 1,// 购买数量 // 餐厅独有 'get_to_time' => $order['get_to_time'], ] ], JSON_UNESCAPED_UNICODE), ]; if (!$billId = $billModel->insertGetId($bill)) { return $this->error('支付订单创建失败'); } } return $this->success('支付订单创建成功', [ 'bill_id' => $billId, ]); } // 活动订单 private function university_event_apply() { // 主表校验 $model = new UniversityEventApplyModel(); $order = $model->getDetail( params: ['order_no', $this->orderNo], with : [ 'events' ] ); if (!$order) { return $this->error('订单不存在或已取消'); } if ($order['is_pay'] == 1) { return $this->error('订单已支付'); } $billModel = new BillModel(); $bill = $billModel->getDetail( params: ['table_id' => $order['id'], 'table_name' => $this->tableName], ); if (!empty($bill) && $bill['status'] !== 0) { return $this->error('订单已支付'); } // 如果没有订单 则需要新创建支付订单 if ($bill) { $billId = $bill['id']; } else { $bill = [ 'user_id' => $this->userId, 'order_no' => Common::createOrderNo('B'), 'total_amount' => $order['pay_amount'], 'pay_amount' => $order['pay_amount'], 'createtime' => time(), 'num' => $order['num'], 'table_name' => $this->tableName, 'table_id' => $order['id'], 'shop_id' => $order['events']['id'], 'shop_name' => $order['events']['name'], 'shop_logo' => $order['events']['image'], 'back_rate' => $order['events']['back_rate'], 'args' => json_encode([ [ 'image' => $order['events']['image'],// 图片 'name' => $order['events']['name'],// 规格名 'num' => $order['num'],// 购买数量 // 餐厅独有 'start_time' => $order['events']['start_time'], ] ], JSON_UNESCAPED_UNICODE), ]; if (!$billId = $billModel->insertGetId($bill)) { return $this->error('支付订单创建失败'); } } return $this->success('支付订单创建成功', [ 'bill_id' => $billId, ]); } // 线下订单 private function offline_shop_order() { // 主表校验 $model = new OfflineShopOrderModel(); $order = $model->getDetail( params: ['order_no', $this->orderNo], with : [ 'shop' ] ); if (!$order) { return $this->error('订单不存在或已取消'); } if ($order['is_pay'] == 1) { return $this->error('订单已支付'); } $billModel = new BillModel(); $bill = $billModel->getDetail( params: ['table_id' => $order['id'], 'table_name' => $this->tableName], ); if (!empty($bill) && $bill['status'] !== 0) { return $this->error('订单已支付'); } // 如果没有订单 则需要新创建支付订单 if ($bill) { $billId = $bill['id']; } else { $bill = [ 'user_id' => $this->userId, 'order_no' => Common::createOrderNo('B'), 'total_amount' => $order['pay_amount'], 'pay_amount' => $order['pay_amount'], 'createtime' => time(), 'num' => 1, 'table_name' => $this->tableName, 'table_id' => $order['id'], 'shop_id' => $order['shop']['id'], 'shop_name' => $order['shop']['name'], 'shop_logo' => $order['shop']['image'], 'back_rate' => $order['shop']['back_rate'], 'args' => json_encode([ [ 'image' => $order['shop']['image'],// 图片 'name' => $order['shop']['name'],// 规格名 'num' => 1,// 购买数量 ] ], JSON_UNESCAPED_UNICODE), ]; if (!$billId = $billModel->insertGetId($bill)) { return $this->error('支付订单创建失败'); } } return $this->success('支付订单创建成功', [ 'bill_id' => $billId, ]); } }