Order.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. namespace app\api\model\sharing;
  3. use app\api\model\User as UserModel;
  4. use app\api\model\Setting as SettingModel;
  5. use app\api\model\UserCoupon as UserCouponModel;
  6. use app\api\model\sharing\Goods as GoodsModel;
  7. use app\api\model\sharing\GoodsSku as GoodsSkuModel;
  8. use app\common\model\sharing\Order as OrderModel;
  9. use app\api\model\sharing\OrderGoods as OrderGoodsModel;
  10. use app\api\service\User as UserService;
  11. use app\api\service\sharing\order\PaySuccess;
  12. use app\api\service\Payment as PaymentService;
  13. use app\common\enum\OrderType as OrderTypeEnum;
  14. use app\common\enum\order\Status as OrderStatusEnum;
  15. use app\common\enum\DeliveryType as DeliveryTypeEnum;
  16. use app\common\enum\order\PayType as PayTypeEnum;
  17. use app\common\enum\order\PayStatus as PayStatusEnum;
  18. use app\common\service\order\Complete as OrderCompleteService;
  19. use app\common\library\helper;
  20. use app\common\exception\BaseException;
  21. /**
  22. * 拼团订单模型
  23. * Class Order
  24. * @package app\api\model
  25. */
  26. class Order extends OrderModel
  27. {
  28. /**
  29. * 隐藏字段
  30. * @var array
  31. */
  32. protected $hidden = [
  33. 'wxapp_id',
  34. 'update_time'
  35. ];
  36. /**
  37. * 订单结算api参数
  38. * @var array
  39. */
  40. private $checkoutParam = [
  41. 'active_id' => 0, // 参与的拼单id
  42. 'delivery' => null, // 配送方式
  43. 'shop_id' => 0, // 自提门店id
  44. 'linkman' => '', // 自提联系人
  45. 'phone' => '', // 自提联系电话
  46. 'coupon_id' => 0, // 优惠券id
  47. 'remark' => '', // 买家留言
  48. 'pay_type' => PayTypeEnum::WECHAT, // 支付方式
  49. ];
  50. /**
  51. * 待支付订单详情
  52. * @param $orderNo
  53. * @return null|static
  54. * @throws \think\exception\DbException
  55. */
  56. public static function getPayDetail($orderNo)
  57. {
  58. return self::get(['order_no' => $orderNo, 'pay_status' => 10, 'is_delete' => 0], ['goods', 'user']);
  59. }
  60. /**
  61. * 获取订单商品列表
  62. * @param $params
  63. * @return array
  64. */
  65. public function getOrderGoodsListByNow($params)
  66. {
  67. // 商品详情
  68. /* @var GoodsModel $goods */
  69. $goods = GoodsModel::detail($params['goods_id']);
  70. // 商品sku信息
  71. $goods['goods_sku'] = GoodsModel::getGoodsSku($goods, $params['goods_sku_id']);
  72. // 商品列表
  73. $goodsList = [$goods->hidden(['category', 'content', 'image', 'sku'])];
  74. foreach ($goodsList as &$item) {
  75. // 商品单价(根据order_type判断单买还是拼单)
  76. // order_type:下单类型 10 => 单独购买,20 => 拼团
  77. $item['goods_price'] = $params['order_type'] == 10 ? $item['goods_sku']['goods_price']
  78. : $item['goods_sku']['sharing_price'];
  79. // 商品购买数量
  80. $item['total_num'] = $params['goods_num'];
  81. $item['spec_sku_id'] = $item['goods_sku']['spec_sku_id'];
  82. // 商品购买总金额
  83. $item['total_price'] = helper::bcmul($item['goods_price'], $params['goods_num']);
  84. }
  85. return $goodsList;
  86. }
  87. /**
  88. * 订单支付事件
  89. * @param int $payType
  90. * @return bool
  91. * @throws \think\exception\DbException
  92. */
  93. public function onPay($payType = PayTypeEnum::WECHAT)
  94. {
  95. // 判断商品状态、库存
  96. if (!$this->checkGoodsStatusFromOrder($this['goods'])) {
  97. return false;
  98. }
  99. // 余额支付
  100. if ($payType == PayTypeEnum::BALANCE) {
  101. return $this->onPaymentByBalance($this['order_no']);
  102. }
  103. return true;
  104. }
  105. /**
  106. * 构建支付请求的参数
  107. * @param $user
  108. * @param $order
  109. * @param $payType
  110. * @return array
  111. * @throws BaseException
  112. * @throws \think\exception\DbException
  113. */
  114. public function onOrderPayment($user, $order, $payType)
  115. {
  116. if ($payType == PayTypeEnum::WECHAT) {
  117. return $this->onPaymentByWechat($user, $order);
  118. }
  119. return [];
  120. }
  121. /**
  122. * 构建微信支付请求
  123. * @param $user
  124. * @param $order
  125. * @return array
  126. * @throws BaseException
  127. * @throws \think\exception\DbException
  128. */
  129. private function onPaymentByWechat($user, $order)
  130. {
  131. return PaymentService::wechat(
  132. $user,
  133. $order['order_id'],
  134. $order['order_no'],
  135. $order['pay_price'],
  136. OrderTypeEnum::SHARING
  137. );
  138. }
  139. /**
  140. * 余额支付标记订单已支付
  141. * @param $orderNo
  142. * @return bool
  143. * @throws \think\exception\DbException
  144. */
  145. public function onPaymentByBalance($orderNo)
  146. {
  147. // 获取订单详情
  148. $PaySuccess = new PaySuccess($orderNo);
  149. // 发起余额支付
  150. $status = $PaySuccess->onPaySuccess(PayTypeEnum::BALANCE);
  151. if (!$status) {
  152. $this->error = $PaySuccess->getError();
  153. }
  154. return $status;
  155. }
  156. /**
  157. * 验证拼单是否允许加入
  158. * @param $active_id
  159. * @return bool
  160. * @throws BaseException
  161. * @throws \think\exception\DbException
  162. */
  163. public function checkActiveIsAllowJoin($active_id)
  164. {
  165. // 拼单详情
  166. $detail = Active::detail($active_id);
  167. if (!$detail) {
  168. throw new BaseException('很抱歉,拼单不存在');
  169. }
  170. // 验证当前拼单是否允许加入新成员
  171. return $detail->checkAllowJoin();
  172. }
  173. /**
  174. * 保存上门自提联系人
  175. * @param $linkman
  176. * @param $phone
  177. * @return false|\think\Model
  178. */
  179. public function saveOrderExtract($linkman, $phone)
  180. {
  181. // 记忆上门自提联系人(缓存),用于下次自动填写
  182. UserService::setLastExtract($this['user_id'], trim($linkman), trim($phone));
  183. // 保存上门自提联系人(数据库)
  184. return $this->extract()->save([
  185. 'linkman' => trim($linkman),
  186. 'phone' => trim($phone),
  187. 'user_id' => $this['user_id'],
  188. 'wxapp_id' => self::$wxapp_id,
  189. ]);
  190. }
  191. /**
  192. * 用户拼团订单列表
  193. * @param $user_id
  194. * @param string $type
  195. * @return \think\Paginator
  196. * @throws \think\exception\DbException
  197. */
  198. public function getList($user_id, $type = 'all')
  199. {
  200. // 筛选条件
  201. $filter = [];
  202. // 订单数据类型
  203. switch ($type) {
  204. case 'all':
  205. // 全部
  206. break;
  207. case 'payment';
  208. // 待支付
  209. $filter['pay_status'] = PayStatusEnum::PENDING;
  210. break;
  211. case 'sharing';
  212. // 拼团中
  213. $filter['active.status'] = 10;
  214. break;
  215. case 'delivery';
  216. // 待发货
  217. $this->where('IF ( (`order`.`order_type` = 20), (`active`.`status` = 20), TRUE)');
  218. $filter['pay_status'] = 20;
  219. $filter['delivery_status'] = 10;
  220. break;
  221. case 'received';
  222. // 待收货
  223. $filter['pay_status'] = 20;
  224. $filter['delivery_status'] = 20;
  225. $filter['receipt_status'] = 10;
  226. break;
  227. case 'comment';
  228. $filter['order_status'] = 30;
  229. $filter['is_comment'] = 0;
  230. break;
  231. }
  232. return $this->with(['goods.image', 'active'])
  233. ->alias('order')
  234. ->field('order.*, active.status as active_status')
  235. ->join('sharing_active active', 'order.active_id = active.active_id', 'LEFT')
  236. ->where('user_id', '=', $user_id)
  237. ->where($filter)
  238. ->where('order.is_delete', '=', 0)
  239. ->order(['create_time' => 'desc'])
  240. ->paginate(15, false, [
  241. 'query' => \request()->request()
  242. ]);
  243. }
  244. /**
  245. * 取消订单
  246. * @param UserModel $user
  247. * @return bool|false|int
  248. */
  249. public function cancel($user)
  250. {
  251. // 订单是否已支付
  252. $isPay = $this['pay_status']['value'] == PayStatusEnum::SUCCESS;
  253. // 已发货订单不可取消
  254. if ($this['delivery_status']['value'] == 20) {
  255. $this->error = '已发货订单不可取消';
  256. return false;
  257. }
  258. // 已付款的拼团订单不允许取消
  259. if ($isPay && $this['order_type']['value'] == 20) {
  260. $this->error = '已付款的拼团订单不允许取消';
  261. return false;
  262. }
  263. // 订单取消事件
  264. $this->transaction(function () use ($user, $isPay) {
  265. // 未付款的订单
  266. if ($isPay == false) {
  267. // 回退商品库存
  268. (new OrderGoodsModel)->backGoodsStock($this['goods'], $isPay);
  269. // 回退用户优惠券
  270. $this['coupon_id'] > 0 && UserCouponModel::setIsUse($this['coupon_id'], false);
  271. // 回退用户积分
  272. $describe = "订单取消:{$this['order_no']}";
  273. $this['points_num'] > 0 && $user->setIncPoints($this['points_num'], $describe);
  274. }
  275. // 更新订单状态
  276. return $this->save(['order_status' => $isPay ? OrderStatusEnum::APPLY_CANCEL : OrderStatusEnum::CANCELLED]);
  277. });
  278. return true;
  279. }
  280. /**
  281. * 确认收货
  282. * @return bool|mixed
  283. */
  284. public function receipt()
  285. {
  286. // 验证订单是否合法
  287. // 条件1: 订单必须已发货
  288. // 条件2: 订单必须未收货
  289. if ($this['delivery_status']['value'] != 20 || $this['receipt_status']['value'] != 10) {
  290. $this->error = '该订单不合法';
  291. return false;
  292. }
  293. return $this->transaction(function () {
  294. // 更新订单状态
  295. $status = $this->save([
  296. 'receipt_status' => 20,
  297. 'receipt_time' => time(),
  298. 'order_status' => 30
  299. ]);
  300. // 执行订单完成后的操作
  301. $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::SHARING);
  302. $OrderCompleteService->complete([$this], static::$wxapp_id);
  303. return $status;
  304. });
  305. }
  306. /**
  307. * 获取订单总数
  308. * @param $user_id
  309. * @param string $type
  310. * @return int|string
  311. * @throws \think\Exception
  312. */
  313. public function getCount($user_id, $type = 'all')
  314. {
  315. // 筛选条件
  316. $filter = [];
  317. // 订单数据类型
  318. switch ($type) {
  319. case 'all':
  320. break;
  321. case 'payment';
  322. $filter['pay_status'] = PayStatusEnum::PENDING;
  323. break;
  324. case 'received';
  325. $filter['pay_status'] = PayStatusEnum::SUCCESS;
  326. $filter['delivery_status'] = 20;
  327. $filter['receipt_status'] = 10;
  328. break;
  329. case 'comment';
  330. $filter['order_status'] = 30;
  331. $filter['is_comment'] = 0;
  332. break;
  333. }
  334. return $this->where('user_id', '=', $user_id)
  335. ->where('order_status', '<>', 20)
  336. ->where($filter)
  337. ->where('is_delete', '=', 0)
  338. ->count();
  339. }
  340. /**
  341. * 订单详情
  342. * @param $order_id
  343. * @param $user_id
  344. * @return array|false|\PDOStatement|string|\think\Model|static
  345. * @throws BaseException
  346. */
  347. public static function getUserOrderDetail($order_id, $user_id)
  348. {
  349. $order = (new static)->with(['goods' => ['image', 'refund'], 'address', 'express', 'extract_shop'])
  350. ->alias('order')
  351. ->field('order.*, active.status as active_status')
  352. ->join('sharing_active active', 'order.active_id = active.active_id', 'LEFT')
  353. ->where([
  354. 'order_id' => $order_id,
  355. 'user_id' => $user_id,
  356. ])->find();
  357. if (!$order) {
  358. throw new BaseException(['msg' => '订单不存在']);
  359. }
  360. return $order;
  361. }
  362. /**
  363. * 判断商品库存不足 (未付款订单)
  364. * @param $goodsList
  365. * @return bool
  366. * @throws \think\exception\DbException
  367. */
  368. private function checkGoodsStatusFromOrder($goodsList)
  369. {
  370. foreach ($goodsList as $goods) {
  371. // 判断商品是否下架
  372. if (
  373. empty($goods['goods'])
  374. || $goods['goods']['goods_status']['value'] != 10
  375. ) {
  376. $this->error = "很抱歉,商品 [{$goods['goods_name']}] 已下架";
  377. return false;
  378. }
  379. // 获取商品的sku信息
  380. $goodsSku = GoodsSkuModel::detail($goods['goods_id'], $goods['spec_sku_id']);
  381. // sku已不存在
  382. if (empty($goodsSku)) {
  383. $this->error = "很抱歉,商品 [{$goods['goods_name']}] sku已不存在,请重新下单";
  384. return false;
  385. }
  386. // 付款减库存
  387. if ($goods['deduct_stock_type'] == 20 && $goods['total_num'] > $goodsSku['stock_num']) {
  388. $this->error = "很抱歉,商品 [{$goods['goods_name']}] 库存不足";
  389. return false;
  390. }
  391. }
  392. return true;
  393. }
  394. /**
  395. * 当前订单是否允许申请售后
  396. * @return bool
  397. */
  398. public function isAllowRefund()
  399. {
  400. // 必须是已发货的订单
  401. if ($this['delivery_status']['value'] != 20) {
  402. return false;
  403. }
  404. // 允许申请售后期限(天)
  405. $refundDays = SettingModel::getItem('trade')['order']['refund_days'];
  406. // 不允许售后
  407. if ($refundDays == 0) {
  408. return false;
  409. }
  410. // 当前时间超出允许申请售后期限
  411. if (
  412. $this['receipt_status'] == 20
  413. && time() > ($this['receipt_time'] + ((int)$refundDays * 86400))
  414. ) {
  415. return false;
  416. }
  417. return true;
  418. }
  419. /**
  420. * 判断当前订单是否允许核销
  421. * @param static $order
  422. * @return bool
  423. */
  424. public function checkExtractOrder(&$order)
  425. {
  426. if (
  427. $order['pay_status']['value'] == PayStatusEnum::SUCCESS
  428. && $order['delivery_type']['value'] == DeliveryTypeEnum::EXTRACT
  429. && $order['delivery_status']['value'] == 10
  430. // 拼团订单验证拼单状态
  431. && ($order['order_type']['value'] == 20 ? $order['active']['status']['value'] == 20 : true)
  432. ) {
  433. return true;
  434. }
  435. $this->setError('该订单不能被核销');
  436. return false;
  437. }
  438. /**
  439. * 设置错误信息
  440. * @param $error
  441. */
  442. private function setError($error)
  443. {
  444. empty($this->error) && $this->error = $error;
  445. }
  446. /**
  447. * 是否存在错误
  448. * @return bool
  449. */
  450. public function hasError()
  451. {
  452. return !empty($this->error);
  453. }
  454. }