CommercialAreaOrderIndependentAccount.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\command;
  3. use app\common\model\store\order\StoreOrder;
  4. use app\common\repositories\store\order\StoreOrderRepository;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\Output;
  8. use think\facade\Db;
  9. class CommercialAreaOrderIndependentAccount extends Command
  10. {
  11. protected function configure()
  12. {
  13. // 指令配置
  14. $this->setName('commercial-area-order-independent-account')
  15. ->setDescription('商贸区订单分账');
  16. }
  17. protected function execute(Input $input, Output $output)
  18. {
  19. // @todo 商贸区订单分账 496:会员专区,439:精品生活区
  20. $this->doCommercialAreaOrderIndependentAccount([439, 491, 496]);
  21. }
  22. /**
  23. * 商贸区订单分账
  24. *
  25. * @param $excludeMerId
  26. * @param $timeLimit
  27. * @return void
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. private function doCommercialAreaOrderIndependentAccount($excludeMerId = [], $timeLimit = 30)
  33. {
  34. $dst_mer_id = systemConfig('mer_365');
  35. // 商户信息
  36. $merchants = [];
  37. /** @var StoreOrderRepository $storeOrderRepository */
  38. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  39. $time = time();
  40. // 订单状态(0:待发货;1:待收货;2:待评价;3:已完成;4已申请退款;-1:已退款)5未支付 6已取消'
  41. $status = [2, 3];
  42. // 已支付、未结算
  43. $where = ['paid' => 1, 'is_independentaccount' => 0, 'is_del' => 0, 'is_system_del' => 0];
  44. // 普通订单 @todo 排除 特殊商户{$excludeMerId}:精彩生活区、会员区、复购区
  45. $cursor = Db::table('rrx_store_order')->where($where)->where('pay_type', 'in', '1,2,4')->whereIn('status', $status)
  46. ->whereNotIn('mer_id', $excludeMerId)->cursor();
  47. foreach ($cursor as $key => $value) {
  48. $settlement_period_time = strtotime($value['create_time']) + $timeLimit * 24 * 3600;
  49. // @todo 下单后第{$timeLimit}天后可分账
  50. if ($settlement_period_time >= $time) {
  51. if (!isset($merchants[$value['mer_id']])) {
  52. $merchants[$value['mer_id']] = Db::name('merchant')->where('mer_id', $value['mer_id'])->find();
  53. }
  54. $group_order = Db::name('store_group_order')->alias('sgo')->where('sgo.group_order_id', $value['group_order_id'])->find();
  55. // @todo 分账账号 496:表示{会员专区}账户;商贸区分账如下:成本分账到商户,利润分账到{会员专区}账户
  56. $independentAccount = $storeOrderRepository->getIndependentAccount($value, $group_order, $dst_mer_id, $merchants[$value['mer_id']]['hf_member_id']);
  57. // @todo 预分账处理
  58. $isSuccessed = $storeOrderRepository->prepareExecuteIndependentAccount(
  59. $value, $group_order, $independentAccount['api_key'], $independentAccount['hf_member_id'], $independentAccount['hf_platform_member_id'], $value['cost'], $value['con_pri']
  60. );
  61. if ($isSuccessed) {
  62. Db::name("log")->insert(array('data' => '商贸区-订单分账:' . json_encode($value, JSON_FORCE_OBJECT)));//日志记录
  63. $order = StoreOrder::getInstance()->where('order_id', $value['order_id'])->find();
  64. $order->is_independentaccount = 1;
  65. $order->save();
  66. }
  67. }
  68. }
  69. }
  70. }