Order.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\task\behavior;
  3. use think\Cache;
  4. use app\task\model\Setting;
  5. use app\task\model\Order as OrderModel;
  6. use app\task\service\Order as OrderService;
  7. use app\common\enum\OrderType as OrderTypeEnum;
  8. use app\common\service\order\Complete as OrderCompleteService;
  9. use app\common\library\helper;
  10. /**
  11. * 订单行为管理
  12. * Class Order
  13. * @package app\task\behavior
  14. */
  15. class Order
  16. {
  17. /* @var \app\task\model\Order $model */
  18. private $model;
  19. /* @var \app\task\service\Order $service */
  20. private $service;
  21. // 小程序id
  22. private $wxappId;
  23. /**
  24. * 执行函数
  25. * @param $model
  26. * @return bool
  27. */
  28. public function run($model)
  29. {
  30. if (!$model instanceof OrderModel)
  31. return new OrderModel and false;
  32. if (!$model::$wxapp_id) return false;
  33. // 绑定订单模型
  34. $this->model = $model;
  35. $this->wxappId = $model::$wxapp_id;
  36. // 普通订单行为管理
  37. $this->master();
  38. return true;
  39. }
  40. /**
  41. * 普通订单行为管理
  42. * @return bool
  43. */
  44. private function master()
  45. {
  46. $key = "__task_space__order__{$this->wxappId}";
  47. if (Cache::has($key)) return true;
  48. // 获取商城交易设置
  49. $this->service = new OrderService;
  50. $config = Setting::getItem('trade');
  51. $this->model->transaction(function () use ($config) {
  52. // 未支付订单自动关闭
  53. $this->close($config['order']['close_days']);
  54. // 已发货订单自动确认收货
  55. $this->receive($config['order']['receive_days']);
  56. // 已完成订单结算
  57. $this->settled($config['order']['refund_days']);
  58. });
  59. Cache::set($key, time(), 3600);
  60. return true;
  61. }
  62. /**
  63. * 未支付订单自动关闭
  64. * @param $closeDays
  65. * @return bool
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. * @throws \Exception
  70. */
  71. private function close($closeDays)
  72. {
  73. // 取消n天以前的的未付款订单
  74. if ($closeDays < 1) return false;
  75. // 截止时间
  76. $deadlineTime = time() - ((int)$closeDays * 86400);
  77. // 执行自动关闭
  78. $this->service->close($deadlineTime);
  79. // 记录日志
  80. $this->dologs('close', [
  81. 'close_days' => (int)$closeDays,
  82. 'deadline_time' => $deadlineTime,
  83. 'orderIds' => json_encode($this->service->getCloseOrderIds()),
  84. ]);
  85. return true;
  86. }
  87. /**
  88. * 已发货订单自动确认收货
  89. * @param $receiveDays
  90. * @return bool|false|int
  91. * @throws \think\Exception
  92. * @throws \think\exception\DbException
  93. */
  94. private function receive($receiveDays)
  95. {
  96. // 截止时间
  97. if ($receiveDays < 1) return false;
  98. $deadlineTime = time() - ((int)$receiveDays * 86400);
  99. // 条件
  100. $filter = [
  101. 'pay_status' => 20,
  102. 'delivery_status' => 20,
  103. 'receipt_status' => 10,
  104. 'delivery_time' => ['<=', $deadlineTime]
  105. ];
  106. // 订单id集
  107. $orderIds = $this->model->where($filter)->column('order_id');
  108. if (!empty($orderIds)) {
  109. // 更新订单收货状态
  110. $this->model->onBatchUpdate($orderIds, [
  111. 'receipt_status' => 20,
  112. 'receipt_time' => time(),
  113. 'order_status' => 30
  114. ]);
  115. // 批量处理已完成的订单
  116. $this->onReceiveCompleted($orderIds);
  117. }
  118. // 记录日志
  119. $this->dologs('receive', [
  120. 'receive_days' => (int)$receiveDays,
  121. 'deadline_time' => $deadlineTime,
  122. 'orderIds' => json_encode($orderIds),
  123. ]);
  124. return true;
  125. }
  126. /**
  127. * 已完成订单结算
  128. * @param $refundDays
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. * @throws \think\exception\DbException
  132. * @throws \Exception
  133. */
  134. private function settled($refundDays)
  135. {
  136. // 获取已完成的订单(未累积用户实际消费金额)
  137. // 条件1:订单状态:已完成
  138. // 条件2:超出售后期限
  139. // 条件3:is_settled 为 0
  140. // 截止时间
  141. $deadlineTime = time() - ((int)$refundDays * 86400);
  142. // 查询条件
  143. $filter = [
  144. 'order_status' => 30,
  145. 'receipt_time' => ['<=', $deadlineTime], // 此处使用<=,用于兼容自动确认收货后
  146. 'is_settled' => 0
  147. ];
  148. // 查询订单列表
  149. $orderList = $this->model->getList($filter, [
  150. 'goods' => ['refund'], // 用于计算售后退款金额
  151. ]);
  152. // 订单id集
  153. $orderIds = helper::getArrayColumn($orderList, 'order_id');
  154. // 订单结算服务
  155. $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
  156. !empty($orderIds) && $OrderCompleteService->settled($orderList);
  157. // 记录日志
  158. $this->dologs('settled', [
  159. 'refund_days' => (int)$refundDays,
  160. 'deadline_time' => $deadlineTime,
  161. 'orderIds' => json_encode($orderIds),
  162. ]);
  163. }
  164. /**
  165. * 批量处理已完成的订单
  166. * @param $orderIds
  167. * @return bool
  168. * @throws \think\Exception
  169. * @throws \think\db\exception\DataNotFoundException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. * @throws \think\exception\DbException
  172. */
  173. private function onReceiveCompleted($orderIds)
  174. {
  175. // 获取已完成的订单列表
  176. $list = $this->model->getList(['order_id' => ['in', $orderIds]], [
  177. 'goods' => ['refund'], // 用于发放分销佣金
  178. 'user', 'address', 'goods', 'express', // 用于同步微信好物圈
  179. ]);
  180. if ($list->isEmpty()) return false;
  181. // 执行订单完成后的操作
  182. $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
  183. $OrderCompleteService->complete($list, $this->wxappId);
  184. return true;
  185. }
  186. /**
  187. * 记录日志
  188. * @param $method
  189. * @param array $params
  190. * @return bool|int
  191. */
  192. private function dologs($method, $params = [])
  193. {
  194. $value = 'behavior Order --' . $method;
  195. foreach ($params as $key => $val)
  196. $value .= ' --' . $key . ' ' . $val;
  197. return log_write($value);
  198. }
  199. }