Order.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. namespace app\api\model\order;
  3. use app\api\model\product\Product as ProductModel;
  4. use app\api\service\order\paysuccess\type\MasterPaySuccessService;
  5. use app\api\service\order\PaymentService;
  6. use app\api\model\settings\Setting as SettingModel;
  7. use app\common\enum\order\OrderPayTypeEnum;
  8. use app\common\enum\order\OrderSourceEnum;
  9. use app\common\enum\order\OrderTypeEnum;
  10. use app\common\enum\order\OrderPayStatusEnum;
  11. use app\common\enum\order\OrderStatusEnum;
  12. use app\common\exception\BaseException;
  13. use app\common\service\order\OrderCompleteService;
  14. use app\common\enum\settings\DeliveryTypeEnum;
  15. use app\common\library\helper;
  16. use app\common\model\order\Order as OrderModel;
  17. use app\api\service\order\checkpay\CheckPayFactory;
  18. use app\common\service\product\factory\ProductFactory;
  19. use app\common\model\plus\coupon\UserCoupon as UserCouponModel;
  20. /**
  21. * 普通订单模型
  22. */
  23. class Order extends OrderModel
  24. {
  25. /**
  26. * 隐藏字段
  27. * @var array
  28. */
  29. protected $hidden = [
  30. 'app_id',
  31. 'update_time'
  32. ];
  33. /**
  34. * 订单支付事件
  35. */
  36. public function onPay($payType = OrderPayTypeEnum::WECHAT)
  37. {
  38. // 判断订单状态
  39. $checkPay = CheckPayFactory::getFactory($this['order_source']);
  40. if (!$checkPay->checkOrderStatus($this)) {
  41. $this->error = $checkPay->getError();
  42. return false;
  43. }
  44. // 余额支付
  45. if ($payType == OrderPayTypeEnum::BALANCE) {
  46. return $this->onPaymentByBalance($this['order_no']);
  47. }
  48. return true;
  49. }
  50. /**
  51. * 用户中心订单列表
  52. */
  53. public function getList($user_id, $type = 'all', $params)
  54. {
  55. // 筛选条件
  56. $filter = [];
  57. // 订单数据类型
  58. switch ($type) {
  59. case 'all':
  60. break;
  61. case 'payment';
  62. $filter['pay_status'] = OrderPayStatusEnum::PENDING;
  63. $filter['order_status'] = 10;
  64. break;
  65. case 'delivery';
  66. $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
  67. $filter['delivery_status'] = 10;
  68. $filter['order_status'] = 10;
  69. break;
  70. case 'received';
  71. $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
  72. $filter['delivery_status'] = 20;
  73. $filter['receipt_status'] = 10;
  74. $filter['order_status'] = 10;
  75. break;
  76. case 'comment';
  77. $filter['is_comment'] = 0;
  78. $filter['order_status'] = 30;
  79. break;
  80. }
  81. $model = new self;
  82. if(isset($params['shop_supplier_id'])&&$params['shop_supplier_id']){
  83. $model = $model->where('shop_supplier_id','=',$params['shop_supplier_id']);
  84. }else{
  85. // 用户查询
  86. $model = $model->where('user_id', '=', $user_id);
  87. }
  88. return $model->with(['product.image', 'supplier'])
  89. ->where($filter)
  90. ->where('is_delete', '=', 0)
  91. ->order(['create_time' => 'desc'])
  92. ->paginate($params);
  93. }
  94. /**
  95. * 确认收货
  96. */
  97. public function receipt()
  98. {
  99. // 验证订单是否合法
  100. // 条件1: 订单必须已发货
  101. // 条件2: 订单必须未收货
  102. if ($this['delivery_status']['value'] != 20 || $this['receipt_status']['value'] != 10) {
  103. $this->error = '该订单不合法';
  104. return false;
  105. }
  106. return $this->transaction(function () {
  107. // 更新订单状态
  108. $status = $this->save([
  109. 'receipt_status' => 20,
  110. 'receipt_time' => time(),
  111. 'order_status' => 30
  112. ]);
  113. // 执行订单完成后的操作
  114. $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
  115. $OrderCompleteService->complete([$this], static::$app_id);
  116. return $status;
  117. });
  118. }
  119. /**
  120. * 立即购买:获取订单商品列表
  121. */
  122. public static function getOrderProductListByNow($params)
  123. {
  124. // 商品详情
  125. $product = ProductModel::detail($params['product_id']);
  126. // 商品sku信息
  127. $product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
  128. // 商品列表
  129. $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
  130. foreach ($productList as &$item) {
  131. // 商品单价
  132. $item['product_price'] = $item['product_sku']['product_price'];
  133. // 商品购买数量
  134. $item['total_num'] = $params['product_num'];
  135. $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
  136. // 商品购买总金额
  137. $item['total_price'] = helper::bcmul($item['product_price'], $params['product_num']);
  138. }
  139. $supplierData[] = [
  140. 'shop_supplier_id' => $product['shop_supplier_id'],
  141. 'supplier' => $product['supplier'],
  142. 'productList' => $productList
  143. ];
  144. unset($product['supplier']);
  145. return $supplierData;
  146. }
  147. /**
  148. * 获取订单总数
  149. */
  150. public function getCount($user, $type = 'all')
  151. {
  152. if ($user === false) {
  153. return false;
  154. }
  155. // 筛选条件
  156. $filter = [];
  157. // 订单数据类型
  158. switch ($type) {
  159. case 'all':
  160. break;
  161. case 'payment';
  162. $filter['pay_status'] = OrderPayStatusEnum::PENDING;
  163. break;
  164. case 'delivery';
  165. $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
  166. $filter['delivery_status'] = 10;
  167. $filter['order_status'] = 10;
  168. break;
  169. case 'received';
  170. $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
  171. $filter['delivery_status'] = 20;
  172. $filter['receipt_status'] = 10;
  173. break;
  174. case 'comment';
  175. $filter['order_status'] = 30;
  176. $filter['is_comment'] = 0;
  177. break;
  178. }
  179. return $this->where('user_id', '=', $user['user_id'])
  180. ->where('order_status', '<>', 20)
  181. ->where($filter)
  182. ->where('is_delete', '=', 0)
  183. ->count();
  184. }
  185. /**
  186. * 取消订单
  187. */
  188. public function cancel($user)
  189. {
  190. if ($this['delivery_status']['value'] == 20) {
  191. $this->error = '已发货订单不可取消';
  192. return false;
  193. }
  194. //进行中的拼团订单不能取消
  195. if($this['order_source'] == OrderSourceEnum::ASSEMBLE){
  196. if($this['assemble_status'] == 10){
  197. $this->error = '订单正在拼团,到期后如果订单未拼团成功将自动退款';
  198. return false;
  199. }
  200. }
  201. // 订单取消事件
  202. return $this->transaction(function () use ($user) {
  203. // 订单是否已支付
  204. $isPay = $this['pay_status']['value'] == OrderPayStatusEnum::SUCCESS;
  205. // 未付款的订单
  206. if ($isPay == false) {
  207. //主商品退回库存
  208. ProductFactory::getFactory($this['order_source'])->backProductStock($this['product'], $isPay);
  209. // 回退用户优惠券
  210. $this['coupon_id'] > 0 && UserCouponModel::setIsUse($this['coupon_id'], false);
  211. // 回退用户积分
  212. $describe = "订单取消:{$this['order_no']}";
  213. $this['points_num'] > 0 && $user->setIncPoints($this['points_num'], $describe);
  214. }
  215. // 更新订单状态
  216. return $this->save(['order_status' => $isPay ? OrderStatusEnum::APPLY_CANCEL : OrderStatusEnum::CANCELLED]);
  217. });
  218. }
  219. /**
  220. * 订单详情
  221. */
  222. public static function getUserOrderDetail($order_id, $user_id)
  223. {
  224. $model = new static();
  225. $order = $model->where(['order_id' => $order_id, 'user_id' => $user_id,])->with(['product' => ['image', 'refund'], 'address', 'express', 'extractStore', 'supplier'])->find();
  226. if (empty($order)) {
  227. throw new BaseException(['msg' => '订单不存在']);
  228. }
  229. return $order;
  230. }
  231. /**
  232. * 供应商查看订单详情
  233. */
  234. public static function getSupplierOrderDetail($order_id, $shop_supplier_id)
  235. {
  236. $model = new static();
  237. $order = $model->where(['order_id' => $order_id, 'shop_supplier_id' => $shop_supplier_id,])->with(['product' => ['image', 'refund'], 'address', 'express', 'extractStore', 'supplier'])->find();
  238. if (empty($order)) {
  239. throw new BaseException(['msg' => '订单不存在']);
  240. }
  241. return $order;
  242. }
  243. /**
  244. * 余额支付标记订单已支付
  245. */
  246. public function onPaymentByBalance($orderNo)
  247. {
  248. // 获取订单详情
  249. $PaySuccess = new MasterPaySuccessService($orderNo);
  250. // 发起余额支付
  251. $status = $PaySuccess->onPaySuccess(OrderPayTypeEnum::BALANCE);
  252. if (!$status) {
  253. $this->error = $PaySuccess->getError();
  254. }
  255. return $status;
  256. }
  257. /**
  258. * 构建微信支付请求
  259. */
  260. protected static function onPaymentByWechat($user, $order_arr, $pay_source)
  261. {
  262. return PaymentService::wechat(
  263. $user,
  264. $order_arr,
  265. OrderTypeEnum::MASTER,
  266. $pay_source
  267. );
  268. }
  269. /**
  270. * 构建支付宝请求
  271. */
  272. protected static function onPaymentByAlipay($user, $order_arr, $pay_source)
  273. {
  274. return PaymentService::alipay(
  275. $user,
  276. $order_arr,
  277. OrderTypeEnum::MASTER,
  278. $pay_source
  279. );
  280. }
  281. /**
  282. * 待支付订单详情
  283. */
  284. public static function getPayDetail($orderNo)
  285. {
  286. $model = new static();
  287. return $model->where(['order_no' => $orderNo, 'pay_status' => 10, 'is_delete' => 0])->with(['product', 'user'])->find();
  288. }
  289. /**
  290. * 构建支付请求的参数
  291. */
  292. public static function onOrderPayment($user, $order_arr, $payType, $pay_source)
  293. {
  294. //如果来源是h5,首次不处理,payH5再处理
  295. if($pay_source == 'h5'){
  296. return [];
  297. }
  298. if ($payType == OrderPayTypeEnum::WECHAT) {
  299. return self::onPaymentByWechat($user, $order_arr, $pay_source);
  300. }
  301. if ($payType == OrderPayTypeEnum::ALIPAY) {
  302. return self::onPaymentByAlipay($user, $order_arr, $pay_source);
  303. }
  304. return [];
  305. }
  306. /**
  307. * 判断当前订单是否允许核销
  308. */
  309. public function checkExtractOrder(&$order)
  310. {
  311. if (
  312. $order['pay_status']['value'] == OrderPayStatusEnum::SUCCESS
  313. && $order['delivery_type']['value'] == DeliveryTypeEnum::EXTRACT
  314. && $order['delivery_status']['value'] == 10
  315. ) {
  316. return true;
  317. }
  318. $this->setError('该订单不能被核销');
  319. return false;
  320. }
  321. /**
  322. * 当前订单是否允许申请售后
  323. */
  324. public function isAllowRefund()
  325. {
  326. // 必须是已发货的订单
  327. if ($this['delivery_status']['value'] != 20) {
  328. return false;
  329. }
  330. // 允许申请售后期限(天)
  331. $refundDays = SettingModel::getItem('trade')['order']['refund_days'];
  332. // 不允许售后
  333. if ($refundDays == 0) {
  334. return false;
  335. }
  336. // 当前时间超出允许申请售后期限
  337. if (
  338. $this['receipt_status'] == 20
  339. && time() > ($this['receipt_time'] + ((int)$refundDays * 86400))
  340. ) {
  341. return false;
  342. }
  343. return true;
  344. }
  345. /**
  346. * 获取活动订单
  347. * 已付款,未取消
  348. */
  349. public static function getPlusOrderNum($user_id, $product_id, $order_source)
  350. {
  351. $model = new static();
  352. return $model->alias('order')->where('order.user_id', '=', $user_id)
  353. ->join('order_product', 'order_product.order_id = order.order_id', 'left')
  354. ->where('order_product.product_source_id', '=', $product_id)
  355. ->where('order.pay_status', '=', 20)
  356. ->where('order.order_source', '=', $order_source)
  357. ->where('order.order_status', '<>', 20)
  358. ->sum('total_num');
  359. }
  360. /**
  361. * 累计成交笔数
  362. */
  363. public static function getTotalPayOrder($shop_supplier_id){
  364. //累积成交笔数
  365. return (new static())->where('shop_supplier_id', '=', $shop_supplier_id)
  366. ->where('pay_status', '=', 20)
  367. ->where('order_status', 'in', [10,30])
  368. ->count();
  369. }
  370. public static function getTodayPayOrder($shop_supplier_id){
  371. //开始
  372. $beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
  373. //结束
  374. $endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  375. //今日成交笔数
  376. return (new static())->where('shop_supplier_id', '=', $shop_supplier_id)
  377. ->where('pay_status', '=', 20)
  378. ->where('order_status', 'in', [10,30])
  379. ->whereBetweenTime('create_time', $beginToday, $endToday)
  380. ->count();
  381. }
  382. /**
  383. * 设置错误信息
  384. */
  385. protected function setError($error)
  386. {
  387. empty($this->error) && $this->error = $error;
  388. }
  389. /**
  390. * 是否存在错误
  391. */
  392. public function hasError()
  393. {
  394. return !empty($this->error);
  395. }
  396. /**
  397. * 获取直播订单
  398. */
  399. public function getLiveOrder($params){
  400. $model = $this;
  401. if(isset($params['room_id'])&&$params['room_id']){
  402. $model = $model->where('room_id','=',$params['room_id']);
  403. }
  404. if(isset($params['pay_status'])&&$params['pay_status']){
  405. $model = $model->where('pay_status','=',$params['pay_status']);
  406. }
  407. return $model->with(['product.image'])
  408. ->where('shop_supplier_id', '=', $params['shop_supplier_id'])
  409. ->where('room_id', '>', 0)
  410. ->where('is_delete', '=', 0)
  411. ->order(['create_time' => 'desc'])
  412. ->paginate($params);
  413. }
  414. /**
  415. * 主订单购买的数量
  416. * 未取消的订单
  417. */
  418. public static function getHasBuyOrderNum($user_id, $product_id)
  419. {
  420. $model = new static();
  421. return $model->alias('order')->where('order.user_id', '=', $user_id)
  422. ->join('order_product', 'order_product.order_id = order.order_id', 'left')
  423. ->where('order_product.product_id', '=', $product_id)
  424. ->where('order.order_source', '=', OrderSourceEnum::MASTER)
  425. ->where('order.order_status', '<>', 21)
  426. ->sum('total_num');
  427. }
  428. }