Order.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\console\model;
  13. use app\common\enum\order\DeliveryStatus;
  14. use app\common\enum\order\ReceiptStatus;
  15. use app\common\model\Order as OrderModel;
  16. use app\console\model\OrderGoods as OrderGoodsModel;
  17. use app\common\enum\order\PayStatus as PayStatusEnum;
  18. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  19. use app\common\enum\order\HxStatus as HxStatusEnum;
  20. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  21. /**
  22. * 订单模型
  23. * Class Order
  24. * @package app\common\model
  25. */
  26. class Order extends OrderModel
  27. {
  28. /**
  29. * 获取订单列表
  30. * @param int $storeId 商城ID
  31. * @param array $filter
  32. * @param array $with
  33. * @return \think\Collection
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function getList(int $storeId, $filter = [], $with = [])
  39. {
  40. return $this->with($with)
  41. ->where($filter)
  42. ->where('store_id', '=', $storeId)
  43. ->where('is_delete', '=', 0)
  44. ->select();
  45. }
  46. /**
  47. * 获取订单ID集
  48. * @param int $storeId 商城ID
  49. * @param array $filter
  50. * @return array
  51. */
  52. public function getOrderIds(int $storeId, $filter = [])
  53. {
  54. return $this->where($filter)
  55. ->where('store_id', '=', $storeId)
  56. ->where('is_delete', '=', 0)
  57. ->column('order_id');
  58. }
  59. /**
  60. * 查询截止时间未支付的订单列表
  61. * @param int $storeId 商城ID
  62. * @param int $deadlineTime 截止日期的时间戳
  63. * @return \think\Collection
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function getListByClose(int $storeId, int $deadlineTime)
  69. {
  70. // 查询条件
  71. $filter = [
  72. ['pay_status', '=', PayStatusEnum::PENDING],
  73. ['order_status', '=', OrderStatusEnum::NORMAL],
  74. ['create_time', '<=', $deadlineTime],
  75. ];
  76. // 查询列表记录
  77. return $this->getList($storeId, $filter, ['goods', 'user']);
  78. }
  79. /**
  80. * 查询截止时间未自提的订单列表
  81. * @param int $storeId 商城ID
  82. * @param int $deadlineTime 截止日期的时间戳
  83. * @return \think\Collection
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function getListByUnPickup(int $storeId, int $deadlineTime)
  89. {
  90. // 查询条件
  91. $filter = [
  92. ['pay_status', '=', PayStatusEnum::SUCCESS], // 已支付
  93. ['order_status', '=', OrderStatusEnum::NORMAL],
  94. ['delivery_type', '=', DeliveryTypeEnum::SHOPS_DELIVERY], // 门店自提
  95. ['hx_status', '=', HxStatusEnum::PENDING], // 未核销
  96. ['create_time', '<=', $deadlineTime],
  97. ];
  98. // 查询列表记录
  99. return $this->getList($storeId, $filter, ['goods', 'user']);
  100. }
  101. /**
  102. * 查询截止时间已完成的订单列表
  103. * @param int $storeId 商城ID
  104. * @param array $orderIds 订单ID集
  105. * @return \think\Collection
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function getListByOrderIds(int $storeId, array $orderIds)
  111. {
  112. // 查询条件
  113. $filter = [['order_id', 'in', $orderIds]];
  114. // 查询列表记录
  115. return $this->getList($storeId, $filter, ['goods' => ['refund']]);
  116. }
  117. /**
  118. * 查询截止时间未确认收货的订单ID集
  119. * @param int $storeId 商城ID
  120. * @param int $deadlineTime 截止时间
  121. * @return array
  122. */
  123. public function getOrderIdsByReceive(int $storeId, int $deadlineTime)
  124. {
  125. // 查询条件
  126. $filter = [
  127. ['pay_status', '=', PayStatusEnum::SUCCESS],
  128. ['delivery_status', '=', DeliveryStatus::DELIVERED],
  129. ['receipt_status', '=', ReceiptStatus::NOT_RECEIVED],
  130. ['delivery_time', '<=', $deadlineTime]
  131. ];
  132. // 查询列表记录
  133. return $this->getOrderIds($storeId, $filter);
  134. }
  135. /**
  136. * 查询截止时间确认收货的订单列表
  137. * @param int $storeId 商城ID
  138. * @param int $deadlineTime 截止时间
  139. * @return \think\Collection
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\DbException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. */
  144. public function getOrderListBySettled(int $storeId, int $deadlineTime)
  145. {
  146. // 查询条件
  147. $filter = [
  148. ['order_status', '=', OrderStatusEnum::COMPLETED],
  149. ['receipt_time', '<=', $deadlineTime],
  150. ['is_settled', '=', 0]
  151. ];
  152. // 查询列表记录
  153. return $this->getList($storeId, $filter, ['goods.refund']);
  154. }
  155. /**
  156. * 查询截止时间确认收货待评价的订单列表
  157. * @param int $storeId 商城ID
  158. * @param int $deadlineTime 截止时间
  159. * @return \think\Collection
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public function getOrderListByComment(int $storeId, int $deadlineTime)
  165. {
  166. // 查询条件
  167. $filter = [
  168. ['order_status', '=', OrderStatusEnum::COMPLETED],
  169. ['receipt_status', '=', ReceiptStatus::RECEIVED],
  170. ['receipt_time', '<=', $deadlineTime],
  171. ['is_comment', '=', 0]
  172. ];
  173. // 查询列表记录
  174. return $this->getList($storeId, $filter, ['user']);
  175. }
  176. /**
  177. * 批量更新订单状态为已收货
  178. * @param array $orderIds
  179. * @return false|int
  180. */
  181. public function onUpdateReceived(array $orderIds)
  182. {
  183. $newOrderIdsOne = $newOrderIdsTwo = [];
  184. foreach ($orderIds as $orderId){
  185. //判断订单下面的商品是否都已收货
  186. if(!OrderGoodsModel::where('order_id',$orderId)->where('receipt_status',ReceiptStatus::NOT_RECEIVED)->where('has_refund_full',0)->count()){
  187. $order = OrderModel::find($orderId);
  188. if($order['order_status']==OrderStatusEnum::NORMAL){//订单未关闭的情况下
  189. $newOrderIdsOne[] = $orderId;
  190. }else{
  191. $newOrderIdsTwo[] = $orderId;
  192. }
  193. }
  194. }
  195. if(!empty($newOrderIdsOne)){
  196. return $this->onBatchUpdate($newOrderIdsOne, [
  197. 'receipt_status' => ReceiptStatus::RECEIVED,
  198. 'receipt_time' => time(),
  199. 'order_status' => OrderStatusEnum::COMPLETED
  200. ]);
  201. }
  202. if(!empty($newOrderIdsTwo)){
  203. return $this->onBatchUpdate($newOrderIdsTwo, [
  204. 'receipt_status' => ReceiptStatus::RECEIVED,
  205. 'receipt_time' => time()
  206. ]);
  207. }
  208. }
  209. }