PaymentBusiness.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace app\common\business;
  3. use app\common\model\BillModel;
  4. use app\common\model\HotelCanteenOrderModel;
  5. use app\common\model\HotelOrderModel;
  6. use app\common\model\OfflineShopOrderModel;
  7. use app\common\model\UniversityEventApplyModel;
  8. use app\utils\Common;
  9. use think\Db;
  10. class PaymentBusiness extends BusinessResult
  11. {
  12. /**
  13. * 注释
  14. */
  15. public const ORDER_TYPE = [
  16. 'hotel_order' => '酒店订单',
  17. 'hotel_canteen_order' => '餐厅订单',
  18. 'university_event_apply' => '活动订单',
  19. 'offline_shop_order' => '线下订单',
  20. ];
  21. /**
  22. * 主表订单对应各自店铺表
  23. */
  24. public const ORDER_SHOP = [
  25. 'hotel_order' => 'hotel',
  26. 'hotel_canteen_order' => 'hotel_canteen',
  27. 'university_event_apply' => 'university_event',
  28. 'offline_shop_order' => 'offline_shop',
  29. ];
  30. protected int $userId = 0;
  31. protected string $orderNo = '';
  32. protected string $tableName = '';
  33. public function __construct() {}
  34. /**
  35. * 获取门店信息(根绝tableName)
  36. * @param string $tableName
  37. * @param int $shopId
  38. * @return array|bool|\PDOStatement|string|\think\Model|null
  39. */
  40. public function getShopInfo(string $tableName,int $shopId)
  41. {
  42. return Db::name($tableName)->where(['shop_id' => $shopId])->find();
  43. }
  44. /**
  45. * 订单成交
  46. * @param int $bill_id
  47. * @param string $third_no
  48. * @return bool
  49. * @throws \think\Exception
  50. * @throws \think\exception\PDOException
  51. */
  52. public function deal(string $bill_order_no, string $third_no = '')
  53. {
  54. $model = new BillModel();
  55. $bill = $model->getDetail(['order_no' => $bill_order_no]);
  56. if (!$bill || $bill['status'] !== 0) {
  57. return $this->error('订单已支付');
  58. }
  59. $update = [
  60. 'status' => 1,
  61. 'third_no' => $third_no,
  62. 'pay_time' => time(),
  63. ];
  64. switch ($bill['table_name']) {
  65. case 'hotel_order':
  66. case 'hotel_canteen_order':
  67. case 'university_event_apply':
  68. break;
  69. case 'offline_shop_order':
  70. $update['back_status'] = 1;
  71. break;
  72. }
  73. if (!$model->where('order_no', $bill_order_no)->update($update)){
  74. return $this->error('订单支付失败');
  75. }
  76. if (!Db::name($bill['table_name'])->where('id', $bill['table_id'])->update(['is_pay'=>1,'pay_time'=>time()])){
  77. return $this->error('订单支付失败');
  78. }
  79. return $this->success('支付成功');
  80. }
  81. /**
  82. * 创建订单
  83. * @param int $userId
  84. * @param string $orderNo
  85. * @param string $tableName
  86. * @return bool
  87. */
  88. public function createOrder(int $userId, string $orderNo, string $tableName)
  89. {
  90. $this->userId = $userId;
  91. $this->orderNo = $orderNo;
  92. $this->tableName = $tableName;
  93. if (!$this->$tableName()) {
  94. return $this->error($this->getMessage(), $this->getData());
  95. }
  96. return $this->success($this->getMessage(), $this->getData());
  97. }
  98. // 酒店订单
  99. private function hotel_order()
  100. {
  101. // 主表校验
  102. $model = new HotelOrderModel();
  103. $order = $model->getDetail(
  104. params: ['order_no', $this->orderNo],
  105. with : [
  106. 'hotel', 'room'
  107. ]
  108. );
  109. if (!$order) {
  110. return $this->error('订单不存在或已取消');
  111. }
  112. if ($order['is_pay'] == 1) {
  113. return $this->error('订单已支付');
  114. }
  115. $billModel = new BillModel();
  116. $bill = $billModel->getDetail(
  117. params: ['table_id' => $order['id'], 'table_name' => $this->tableName],
  118. );
  119. if (!empty($bill) && $bill['status'] !== 0) {
  120. return $this->error('订单已支付');
  121. }
  122. // 如果没有订单 则需要新创建支付订单
  123. if ($bill) {
  124. $billId = $bill['id'];
  125. } else {
  126. $bill = [
  127. 'user_id' => $this->userId,
  128. 'order_no' => Common::createOrderNo('B'),
  129. 'total_amount' => $order['pay_amount'],
  130. 'pay_amount' => $order['pay_amount'],
  131. 'createtime' => time(),
  132. 'num' => $order['num'],
  133. 'table_name' => $this->tableName,
  134. 'table_id' => $order['id'],
  135. 'shop_id' => $order['hotel']['id'],
  136. 'shop_name' => $order['hotel']['name'],
  137. 'shop_logo' => $order['hotel']['image'],
  138. 'back_rate' => $order['hotel']['back_rate'],
  139. 'args' => json_encode([
  140. [
  141. 'image' => $order['room']['image'],// 图片
  142. 'name' => $order['room']['name'],// 规格名
  143. 'num' => $order['num'],// 购买数量
  144. // 酒店独有
  145. 'days' => $order['days'],
  146. 'start_date' => $order['start_date'],
  147. 'end_date' => $order['end_date'],
  148. ]
  149. ], JSON_UNESCAPED_UNICODE),
  150. ];
  151. if (!$billId = $billModel->insertGetId($bill)) {
  152. return $this->error('支付订单创建失败');
  153. }
  154. }
  155. return $this->success('支付订单创建成功', [
  156. 'bill_id' => $billId,
  157. ]);
  158. }
  159. // 餐厅订单
  160. private function hotel_canteen_order()
  161. {
  162. // 主表校验
  163. $model = new HotelCanteenOrderModel();
  164. $order = $model->getDetail(
  165. params: ['order_no', $this->orderNo],
  166. with : [
  167. 'canteen', 'room'
  168. ]
  169. );
  170. if (!$order) {
  171. return $this->error('订单不存在或已取消');
  172. }
  173. if ($order['is_pay'] == 1) {
  174. return $this->error('订单已支付');
  175. }
  176. $billModel = new BillModel();
  177. $bill = $billModel->getDetail(
  178. params: ['table_id' => $order['id'], 'table_name' => $this->tableName],
  179. );
  180. if (!empty($bill) && $bill['status'] !== 0) {
  181. return $this->error('订单已支付');
  182. }
  183. // 如果没有订单 则需要新创建支付订单
  184. if ($bill) {
  185. $billId = $bill['id'];
  186. } else {
  187. $bill = [
  188. 'user_id' => $this->userId,
  189. 'order_no' => Common::createOrderNo('B'),
  190. 'total_amount' => $order['pay_amount'],
  191. 'pay_amount' => $order['pay_amount'],
  192. 'createtime' => time(),
  193. 'num' => 1,
  194. 'table_name' => $this->tableName,
  195. 'table_id' => $order['id'],
  196. 'shop_id' => $order['canteen']['id'],
  197. 'shop_name' => $order['canteen']['name'],
  198. 'shop_logo' => $order['canteen']['image'],
  199. 'back_rate' => $order['canteen']['back_rate'],
  200. 'args' => json_encode([
  201. [
  202. 'image' => $order['room']['image'],// 图片
  203. 'name' => $order['room']['name'],// 规格名
  204. 'num' => 1,// 购买数量
  205. // 餐厅独有
  206. 'get_to_time' => $order['get_to_time'],
  207. ]
  208. ], JSON_UNESCAPED_UNICODE),
  209. ];
  210. if (!$billId = $billModel->insertGetId($bill)) {
  211. return $this->error('支付订单创建失败');
  212. }
  213. }
  214. return $this->success('支付订单创建成功', [
  215. 'bill_id' => $billId,
  216. ]);
  217. }
  218. // 活动订单
  219. private function university_event_apply()
  220. {
  221. // 主表校验
  222. $model = new UniversityEventApplyModel();
  223. $order = $model->getDetail(
  224. params: ['order_no', $this->orderNo],
  225. with : [
  226. 'events'
  227. ]
  228. );
  229. if (!$order) {
  230. return $this->error('订单不存在或已取消');
  231. }
  232. if ($order['is_pay'] == 1) {
  233. return $this->error('订单已支付');
  234. }
  235. $billModel = new BillModel();
  236. $bill = $billModel->getDetail(
  237. params: ['table_id' => $order['id'], 'table_name' => $this->tableName],
  238. );
  239. if (!empty($bill) && $bill['status'] !== 0) {
  240. return $this->error('订单已支付');
  241. }
  242. // 如果没有订单 则需要新创建支付订单
  243. if ($bill) {
  244. $billId = $bill['id'];
  245. } else {
  246. $bill = [
  247. 'user_id' => $this->userId,
  248. 'order_no' => Common::createOrderNo('B'),
  249. 'total_amount' => $order['pay_amount'],
  250. 'pay_amount' => $order['pay_amount'],
  251. 'createtime' => time(),
  252. 'num' => $order['num'],
  253. 'table_name' => $this->tableName,
  254. 'table_id' => $order['id'],
  255. 'shop_id' => $order['events']['id'],
  256. 'shop_name' => $order['events']['name'],
  257. 'shop_logo' => $order['events']['image'],
  258. 'back_rate' => $order['events']['back_rate'],
  259. 'args' => json_encode([
  260. [
  261. 'image' => $order['events']['image'],// 图片
  262. 'name' => $order['events']['name'],// 规格名
  263. 'num' => $order['num'],// 购买数量
  264. // 餐厅独有
  265. 'start_time' => $order['events']['start_time'],
  266. ]
  267. ], JSON_UNESCAPED_UNICODE),
  268. ];
  269. if (!$billId = $billModel->insertGetId($bill)) {
  270. return $this->error('支付订单创建失败');
  271. }
  272. }
  273. return $this->success('支付订单创建成功', [
  274. 'bill_id' => $billId,
  275. ]);
  276. }
  277. // 线下订单
  278. private function offline_shop_order()
  279. {
  280. // 主表校验
  281. $model = new OfflineShopOrderModel();
  282. $order = $model->getDetail(
  283. params: ['order_no', $this->orderNo],
  284. with : [
  285. 'shop'
  286. ]
  287. );
  288. if (!$order) {
  289. return $this->error('订单不存在或已取消');
  290. }
  291. if ($order['is_pay'] == 1) {
  292. return $this->error('订单已支付');
  293. }
  294. $billModel = new BillModel();
  295. $bill = $billModel->getDetail(
  296. params: ['table_id' => $order['id'], 'table_name' => $this->tableName],
  297. );
  298. if (!empty($bill) && $bill['status'] !== 0) {
  299. return $this->error('订单已支付');
  300. }
  301. // 如果没有订单 则需要新创建支付订单
  302. if ($bill) {
  303. $billId = $bill['id'];
  304. } else {
  305. $bill = [
  306. 'user_id' => $this->userId,
  307. 'order_no' => Common::createOrderNo('B'),
  308. 'total_amount' => $order['pay_amount'],
  309. 'pay_amount' => $order['pay_amount'],
  310. 'createtime' => time(),
  311. 'num' => 1,
  312. 'table_name' => $this->tableName,
  313. 'table_id' => $order['id'],
  314. 'shop_id' => $order['shop']['id'],
  315. 'shop_name' => $order['shop']['name'],
  316. 'shop_logo' => $order['shop']['image'],
  317. 'back_rate' => $order['shop']['back_rate'],
  318. 'args' => json_encode([
  319. [
  320. 'image' => $order['shop']['image'],// 图片
  321. 'name' => $order['shop']['name'],// 规格名
  322. 'num' => 1,// 购买数量
  323. ]
  324. ], JSON_UNESCAPED_UNICODE),
  325. ];
  326. if (!$billId = $billModel->insertGetId($bill)) {
  327. return $this->error('支付订单创建失败');
  328. }
  329. }
  330. return $this->success('支付订单创建成功', [
  331. 'bill_id' => $billId,
  332. ]);
  333. }
  334. }