GroupBuyOrderController.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace console\controllers;
  3. use addons\Bargain\common\enums\BargainEnum;
  4. use addons\GroupBuy\common\enums\GroupBuyEnum;
  5. use addons\GroupBuy\common\listeners\order\OrderPaidListener;
  6. use addons\GroupBuy\common\logic\order\ActiveFinish;
  7. use addons\GroupBuy\common\models\GroupBuyActive;
  8. use addons\GroupBuy\common\models\GroupBuyOrder;
  9. use common\enums\RedisKeyEnum;
  10. use common\events\order\OrderPaidEvent;
  11. use common\helpers\FormatHelper;
  12. use common\helpers\LockHelper;
  13. use common\models\goods\Goods;
  14. use common\models\goods\GoodsAttr;
  15. use common\models\goods\GoodsMarketing;
  16. use common\models\mall\MallAddons;
  17. use common\models\user\User;
  18. use yii\console\Controller;
  19. use common\models\order\Order;
  20. use common\models\mall\Mall;
  21. use common\enums\OrderStatusEnum;
  22. use common\enums\StatusEnum;
  23. use yii\console\ExitCode;
  24. /**
  25. * 订单相关定时处理任务
  26. */
  27. class GroupBuyOrderController extends Controller
  28. {
  29. /**
  30. * 自动关闭过期订单
  31. */
  32. public function actionAutoClose()
  33. {
  34. $where = [
  35. ['order_status' => OrderStatusEnum::NOT_PAY],
  36. ['status' => StatusEnum::ENABLED],
  37. ];
  38. return self::actionOrderProcess('close', 'order.payment_expire_time', $where);
  39. }
  40. /**
  41. * 自动结束拼团订单
  42. */
  43. public function actionAutoFinish()
  44. {
  45. try {
  46. $mall_list = Mall::getEnableMallList(true);
  47. $mall_ids = array_column($mall_list, 'id');
  48. if (empty($mall_ids)) {
  49. echo '没有可用商城,不做处理 ... ' . PHP_EOL;
  50. return ExitCode::UNSPECIFIED_ERROR;
  51. }
  52. foreach ($mall_ids as $mall_id) {
  53. if (MallAddons::isInstall($mall_id, GroupBuyEnum::ADDONS_NAME) == 0) continue;
  54. $where = [
  55. 'mall_id' => $mall_id,
  56. 'order_status' => GroupBuyEnum::JOINING,
  57. 'is_commander' => 1,
  58. ];
  59. foreach (GroupBuyOrder::find()->where($where)->each(500) as $group_buy_order) {
  60. $active = GroupBuyActive::findOne(['mall_id' => $group_buy_order->mall_id, 'id' => $group_buy_order->group_buy_active_id]);
  61. if (empty($active)) continue;
  62. $effective_time = $active->effective_time * 60;
  63. if ((time() - $group_buy_order->created_at) > $effective_time) {
  64. if ($active->virtual_group_type == 3) { // 开启了虚拟成团
  65. ActiveFinish::handle($mall_id, $group_buy_order['open_group_id'], $active);
  66. // 这个状态下的拼团默认成功
  67. continue;
  68. }
  69. GroupBuyOrder::autoFinish(['order_id'=>$group_buy_order['id'],'open_group_id'=>$group_buy_order['open_group_id']]);
  70. }
  71. }
  72. }
  73. return ExitCode::OK;
  74. } catch (\Exception $e) {
  75. echo '拼团订单,执行拼团结束失败任务处理出错:' . $e->getMessage() . PHP_EOL;
  76. return ExitCode::UNSPECIFIED_ERROR;
  77. }
  78. }
  79. /**
  80. * @desc:自动结束过期拼团活动
  81. * @Author: hua
  82. * @Date: 2021/12/16
  83. * @Time: 8:57
  84. * @Copyright: copyright (c) 2021 广东七件事集团
  85. */
  86. public function actionAutoFinishActive()
  87. {
  88. try {
  89. $mall_list = Mall::getEnableMallList(true);
  90. $mall_ids = array_column($mall_list, 'id');
  91. if (empty($mall_ids)) {
  92. echo '没有可用商城,不做处理 ... ' . PHP_EOL;
  93. return ExitCode::UNSPECIFIED_ERROR;
  94. }
  95. foreach ($mall_ids as $mall_id) {
  96. if (MallAddons::isInstall($mall_id, GroupBuyEnum::ADDONS_NAME) == 0) continue;
  97. //可能活动状态已经变成 结束,但是订单没有修改状态
  98. $active_list = GroupBuyActive::findAll(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'group_status' => [GroupBuyEnum::OPEN_GROUP,GroupBuyEnum::GROUP_FINISH]]);
  99. if ($active_list) {
  100. foreach ($active_list as $active) {
  101. if ($active->end_at > time()) continue;
  102. $where = [
  103. 'mall_id' => $mall_id,
  104. 'group_buy_active_id' => $active->id,
  105. 'order_status' => GroupBuyEnum::JOINING,
  106. 'is_commander' => 1,
  107. ];
  108. foreach (GroupBuyOrder::find()->where($where)->each(500) as $group_buy_order) {
  109. $effective_time = $active->effective_time * 60;
  110. if ((time() - $group_buy_order->created_at) > $effective_time) {
  111. if ($group_buy_order->is_pay) {
  112. if ($active->virtual_group_type == 3) { // 开启了虚拟成团
  113. ActiveFinish::handle($group_buy_order->mall_id, $group_buy_order->open_group_id, $active);
  114. // 这个状态下的拼团默认成功
  115. continue;
  116. }
  117. //已过期,拼团要结束
  118. $res = GroupBuyOrder::finishGroupBuyOrder($group_buy_order->mall_id, $group_buy_order->open_group_id);
  119. if ($res == false) {
  120. echo ' 拼团订单[ID:' . $group_buy_order->id . ']过期,执行拼团结束失败, errmsg: ' . GroupBuyOrder::getStaticError() . PHP_EOL;
  121. continue;
  122. }
  123. }
  124. }
  125. }
  126. $active->group_status = GroupBuyEnum::GROUP_FINISH;
  127. $res = $active->save();
  128. $params['mall_id'] = $mall_id;
  129. $params['marketing_id'] = $active['id'];
  130. $params['marketing_type'] = GroupBuyEnum::ADDONS_NAME;
  131. $params['status'] = StatusEnum::DELETE;
  132. GoodsMarketing::setData($params);
  133. }
  134. }
  135. }
  136. return ExitCode::OK;
  137. } catch (\Exception $e) {
  138. echo '拼团订单,执行拼团结束失败任务处理出错:' . $e->getMessage() . PHP_EOL;
  139. return ExitCode::UNSPECIFIED_ERROR;
  140. }
  141. }
  142. /**
  143. * @name:
  144. * @msg:
  145. * @param {string} $action 操作类型,close:自动关闭订单,confirm:自动确认收货,complete:订单自动过售后
  146. * @param {string} $config_key 相应操作时间间隔配置名称
  147. * @param {int} $order_status 需要进行处理的订单的状态
  148. * @return {int}
  149. */
  150. private static function actionOrderProcess(string $action, string $config_key, array $order_wheres = null): int
  151. {
  152. $action_info_map = [
  153. 'close' => [
  154. 'task_name' => '自动关闭订单',
  155. 'time_radix' => 60, // 时间节点计算的基数,自动关闭订单时间单位是分钟,转换为秒的基数是60
  156. 'method' => 'close',
  157. 'time_field' => 'created_at',
  158. 'default' => 30, // 自动关闭订单默认时间 30 分钟
  159. ],
  160. ];
  161. try {
  162. $mall_list = Mall::getEnableMallList(true);
  163. $mall_ids = array_column($mall_list, 'id');
  164. if (empty($mall_ids)) {
  165. echo '没有可用商城,不做处理 ... ' . PHP_EOL;
  166. return ExitCode::UNSPECIFIED_ERROR;
  167. }
  168. foreach ($mall_ids as $mall_id) {
  169. // 判断用户插件是否安装
  170. if (MallAddons::isInstall($mall_id, GroupBuyEnum::ADDONS_NAME) == 0) continue;
  171. // 订单自动处理时间
  172. $order_auto_handle_time = \Yii::$app->services->setting->mall->get($mall_id, $config_key);
  173. // 如果自动处理时间没有配置,则取默认值
  174. if (empty($order_auto_handle_time)) $order_auto_handle_time = $action_info_map[$action]['default'];
  175. // 订单自动处理时间节点,延后两分钟处理,避免支付回调尚未回来订单已经被取消s
  176. $time_node = time() - $order_auto_handle_time * $action_info_map[$action]['time_radix'] - 120;
  177. $time_field = $action_info_map[$action]['time_field'];
  178. $where = [
  179. 'and',
  180. ['mall_id' => $mall_id],
  181. ['<', $time_field, $time_node]
  182. ];
  183. if (!empty($order_wheres)) {
  184. foreach ($order_wheres as $order_where) {
  185. array_push($where, $order_where);
  186. }
  187. }
  188. $select = 'id,user_id';
  189. foreach (GroupBuyOrder::find()->select($select)->where($where)->each(500) as $order) {
  190. $data = [
  191. 'id' => $order->id,
  192. 'user_id' => $order->user_id,
  193. 'mall_id' => $mall_id,
  194. 'operator_id' => 0,
  195. 'operator_name' => '系统',
  196. ];
  197. $method = $action_info_map[$action]['method'];
  198. if (!GroupBuyOrder::$method($data)) {
  199. echo ' 拼团订单[ID:' . $order->id . ']' . $action_info_map[$action]['task_name'] . '失败, errmsg: ' . Order::getStaticError() . PHP_EOL;
  200. continue;
  201. }
  202. }
  203. }
  204. return ExitCode::OK;
  205. } catch (\Exception $e) {
  206. echo ' 拼团订单' . $action_info_map[$action]['task_name'] . ' 任务处理出错:' . $e->getMessage() . PHP_EOL;
  207. return ExitCode::UNSPECIFIED_ERROR;
  208. }
  209. }
  210. }