Order.php 6.6 KB

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