123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- namespace app\common\business;
- use app\common\model\BillModel;
- use app\common\model\HotelCanteenOrderModel;
- use app\common\model\HotelOrderModel;
- use app\common\model\OfflineShopOrderModel;
- use app\common\model\UniversityEventApplyModel;
- use app\utils\Common;
- use think\Db;
- class PaymentBusiness extends BusinessResult
- {
- /**
- * 注释
- */
- public const ORDER_TYPE = [
- 'hotel_order' => '酒店订单',
- '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,
- ]);
- }
- }
|