Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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, $pay_source)
  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. $data['attach'] = '{"pay_source":"' . $pay_source . '"}';
  47. return $this->onPaymentByBalance($this['order_no'], $data);
  48. }
  49. return true;
  50. }
  51. /**
  52. * 用户中心订单列表
  53. */
  54. public function getList($user_id, $type, $params)
  55. {
  56. // 筛选条件
  57. $filter = [];
  58. // 订单数据类型
  59. switch ($type) {
  60. case 'all':
  61. break;
  62. case 'payment';//待付款
  63. $filter['pay_status'] = OrderPayStatusEnum::PENDING;//付款状态(10未付款)
  64. $filter['order_status'] = 10;//订单状态10=>进行中
  65. break;
  66. case 'delivery';//代发货
  67. $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;//已付款
  68. $filter['delivery_status'] = 10;//未发货
  69. $filter['order_status'] = 10;//订单状态10=>进行中
  70. break;
  71. case 'received';//待收货
  72. $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
  73. $filter['delivery_status'] = 20;//已发货
  74. $filter['receipt_status'] = 10;//未收货
  75. $filter['order_status'] = 10;//订单状态10=>进行中
  76. break;
  77. case 'comment';//待评价
  78. $filter['is_comment'] = 0;//未评价
  79. $filter['order_status'] = 30;//订单状态30=>已完成
  80. break;
  81. }
  82. return $this->with(['product.image'])
  83. ->where('user_id', '=', $user_id)
  84. ->where($filter)
  85. ->where('is_delete', '=', 0)
  86. ->order(['create_time' => 'desc'])
  87. ->paginate($params);
  88. }
  89. /**
  90. * 确认收货
  91. */
  92. public function receipt()
  93. {
  94. // 验证订单是否合法
  95. // 条件1: 订单必须已发货
  96. // 条件2: 订单必须未收货
  97. if ($this['delivery_status']['value'] != 20 || $this['receipt_status']['value'] != 10) {
  98. $this->error = '该订单不合法';
  99. return false;
  100. }
  101. return $this->transaction(function () {
  102. // 更新订单状态
  103. $status = $this->save([
  104. 'receipt_status' => 20,
  105. 'receipt_time' => time(),
  106. 'order_status' => 30
  107. ]);
  108. // 执行订单完成后的操作
  109. $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
  110. $OrderCompleteService->complete([$this], static::$app_id);
  111. return $status;
  112. });
  113. }
  114. /**
  115. * 立即购买:获取订单商品列表
  116. */
  117. public static function getOrderProductListByNow($params)
  118. {
  119. // 商品详情
  120. $product = ProductModel::detail($params['product_id']);
  121. // 商品sku信息
  122. $product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
  123. $product['total_pv'] = helper::bcmul($product['pv'], $params['product_num']);
  124. // 商品列表
  125. $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
  126. foreach ($productList as &$item) {
  127. // 商品单价
  128. $item['product_price'] = $item['product_sku']['product_price'];
  129. // 商品购买数量
  130. $item['total_num'] = $params['product_num'];
  131. $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
  132. $item['pv'] = $item['product_sku']['pv'];
  133. $item['total_pv'] = helper::bcmul($item['pv'], $params['product_num']);
  134. // 商品购买总金额
  135. $item['total_price'] = helper::bcmul($item['product_price'], $params['product_num']);
  136. }
  137. return $productList;
  138. }
  139. /**
  140. * 获取订单总数
  141. */
  142. public function getCount($user, $type = 'all')
  143. {
  144. if ($user === false) {
  145. return false;
  146. }
  147. // 筛选条件
  148. $filter = [];
  149. // 订单数据类型
  150. switch ($type) {
  151. case 'all':
  152. break;
  153. case 'payment';
  154. $filter['pay_status'] = OrderPayStatusEnum::PENDING;
  155. break;
  156. case 'delivery';
  157. $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
  158. $filter['delivery_status'] = 10;
  159. $filter['order_status'] = 10;
  160. break;
  161. case 'received';
  162. $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
  163. $filter['delivery_status'] = 20;
  164. $filter['receipt_status'] = 10;
  165. break;
  166. case 'comment';
  167. $filter['order_status'] = 30;
  168. $filter['is_comment'] = 0;
  169. break;
  170. }
  171. return $this->where('user_id', '=', $user['user_id'])
  172. ->where('order_status', '<>', 20)
  173. ->where($filter)
  174. ->where('is_delete', '=', 0)
  175. ->count();
  176. }
  177. /**
  178. * 取消订单
  179. */
  180. public function cancel($user)
  181. {
  182. if ($this['delivery_status']['value'] == 20) {
  183. $this->error = '已发货订单不可取消';
  184. return false;
  185. }
  186. //进行中的拼团订单不能取消
  187. if($this['order_source'] == OrderSourceEnum::ASSEMBLE){
  188. if($this['assemble_status'] == 10){
  189. $this->error = '订单正在拼团,到期后如果订单未拼团成功将自动退款';
  190. return false;
  191. }
  192. }
  193. // 订单取消事件
  194. return $this->transaction(function () use ($user) {
  195. // 订单是否已支付
  196. $isPay = $this['pay_status']['value'] == OrderPayStatusEnum::SUCCESS;
  197. // 未付款的订单
  198. if ($isPay == false) {
  199. //主商品退回库存
  200. ProductFactory::getFactory($this['order_source'])->backProductStock($this['product'], $isPay);
  201. // 回退用户优惠券
  202. $this['coupon_id'] > 0 && UserCouponModel::setIsUse($this['coupon_id'], false);
  203. // 回退用户积分
  204. $describe = "订单取消:{$this['order_no']}";
  205. $this['points_num'] > 0 && $user->setIncPoints($this['points_num'], $describe);
  206. }
  207. // 更新订单状态
  208. return $this->save(['order_status' => $isPay ? OrderStatusEnum::APPLY_CANCEL : OrderStatusEnum::CANCELLED]);
  209. });
  210. }
  211. /**
  212. * 订单详情
  213. */
  214. public static function getUserOrderDetail($order_id, $user_id)
  215. {
  216. $model = new static();
  217. $order = $model->where(['order_id' => $order_id, 'user_id' => $user_id])->with(['product' => ['image', 'refund','last_refund'], 'address', 'express', 'extractStore'])->find();
  218. if (empty($order)) {
  219. throw new BaseException(['msg' => '订单不存在']);
  220. }
  221. return $order;
  222. }
  223. /**
  224. * 余额支付标记订单已支付
  225. */
  226. public function onPaymentByBalance($orderNo, $data)
  227. {
  228. // 获取订单详情
  229. $PaySuccess = new MasterPaySuccessService($orderNo);
  230. // 发起余额支付
  231. $status = $PaySuccess->onPaySuccess(OrderPayTypeEnum::BALANCE, $data);
  232. if (!$status) {
  233. $this->error = $PaySuccess->getError();
  234. }
  235. return $status;
  236. }
  237. /**
  238. * 构建微信支付请求
  239. */
  240. protected static function onPaymentByWechat($user, $order, $pay_source)
  241. {
  242. return PaymentService::wechat(
  243. $user,
  244. $order['order_id'],
  245. $order['order_no'],
  246. $order['pay_price'],
  247. OrderTypeEnum::MASTER,
  248. $pay_source
  249. );
  250. }
  251. /**
  252. * 待支付订单详情
  253. */
  254. public static function getPayDetail($orderNo, $pay_status)
  255. {
  256. $model = new static();
  257. $model = $model->where('order_no', '=', $orderNo)->where('is_delete', '=', 0);
  258. if($pay_status > 0){
  259. $model = $model->where('pay_status', '=', 10);
  260. }
  261. return $model->with(['product', 'user'])->find();
  262. }
  263. /**
  264. * 构建支付请求的参数
  265. */
  266. public static function onOrderPayment($user, $order, $payType, $pay_source)
  267. {
  268. //如果来源是h5,首次不处理,payH5再处理
  269. if($pay_source == 'h5'){
  270. return [];
  271. }
  272. if ($payType == OrderPayTypeEnum::WECHAT) {
  273. return self::onPaymentByWechat($user, $order, $pay_source);
  274. }
  275. if ($payType == OrderPayTypeEnum::ALIPAY) {
  276. return self::onPaymentByAlipay($user, $order, $pay_source);
  277. }
  278. return [];
  279. }
  280. /**
  281. * 判断当前订单是否允许核销
  282. */
  283. public function checkExtractOrder($order)
  284. {
  285. if (
  286. $order['pay_status']['value'] == OrderPayStatusEnum::SUCCESS
  287. && $order['delivery_type']['value'] == DeliveryTypeEnum::EXTRACT
  288. && $order['delivery_status']['value'] == 10
  289. ) {
  290. return true;
  291. }
  292. $this->setError('该订单不能被核销');
  293. return false;
  294. }
  295. /**
  296. * 当前订单是否允许申请售后
  297. */
  298. public function isAllowRefund()
  299. {
  300. // 必须是已发货的订单
  301. if ($this['delivery_status']['value'] != 20) {
  302. return false;
  303. }
  304. // 允许申请售后期限(天)
  305. $refundDays = SettingModel::getItem('trade')['order']['refund_days'];
  306. // 不允许售后
  307. if ($refundDays == 0) {
  308. return false;
  309. }
  310. // 当前时间超出允许申请售后期限
  311. if (
  312. $this['receipt_status']['value'] == 20
  313. && time() > ($this['receipt_time'] + ((int)$refundDays * 86400))
  314. ) {
  315. return false;
  316. }
  317. return true;
  318. }
  319. /**
  320. * 获取活动订单
  321. * 已付款,未取消
  322. */
  323. public static function getPlusOrderNum($user_id, $product_id, $order_source)
  324. {
  325. $model = new static();
  326. return $model->alias('order')->where('order.user_id', '=', $user_id)
  327. ->join('order_product', 'order_product.order_id = order.order_id', 'left')
  328. ->where('order_product.product_source_id', '=', $product_id)
  329. ->where('order.pay_status', '=', 20)
  330. ->where('order.order_source', '=', $order_source)
  331. ->where('order.order_status', '<>', 20)
  332. ->count();
  333. }
  334. /**
  335. * 构建支付宝请求
  336. */
  337. protected static function onPaymentByAlipay($user, $order, $pay_source)
  338. {
  339. return PaymentService::alipay(
  340. $user,
  341. $order['order_id'],
  342. $order['order_no'],
  343. $order['pay_price'],
  344. OrderTypeEnum::MASTER,
  345. $pay_source
  346. );
  347. }
  348. /**
  349. * 主订单购买的数量
  350. * 未取消的订单
  351. */
  352. public static function getHasBuyOrderNum($user_id, $product_id)
  353. {
  354. $model = new static();
  355. return $model->alias('order')->where('order.user_id', '=', $user_id)
  356. ->join('order_product', 'order_product.order_id = order.order_id', 'left')
  357. ->where('order_product.product_id', '=', $product_id)
  358. ->where('order.order_source', '=', OrderSourceEnum::MASTER)
  359. ->where('order.pay_status', '<>', 10)
  360. ->sum('total_num');
  361. }
  362. /**
  363. * 设置错误信息
  364. */
  365. protected function setError($error)
  366. {
  367. empty($this->error) && $this->error = $error;
  368. }
  369. /**
  370. * 是否存在错误
  371. */
  372. public function hasError()
  373. {
  374. return !empty($this->error);
  375. }
  376. }