GreatLifeOrderIndependentAccount.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace app\command;
  3. use app\common\model\store\order\StoreOrder;
  4. use app\common\model\user\UserCertification;
  5. use app\common\repositories\merchant\order\OrderGzcRepository;
  6. use app\common\repositories\merchant\order\OrderMerchantRepository;
  7. use app\common\repositories\store\order\StoreOrderRepository;
  8. use app\common\repositories\store\order\StoreOrderStatusRepository;
  9. use app\common\repositories\user\UserBillRepository;
  10. use app\common\repositories\user\UserRepository;
  11. use Carbon\Carbon;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\Output;
  15. use think\facade\Db;
  16. use think\facade\Log;
  17. class GreatLifeOrderIndependentAccount extends Command
  18. {
  19. protected function configure()
  20. {
  21. // 指令配置
  22. $this->setName('great-life-order-independent-account')
  23. ->setDescription('精彩生活区订单分账');
  24. }
  25. protected function execute(Input $input, Output $output)
  26. {
  27. // @todo 精彩生活区订单分账
  28. $this->doGreatLifeOrderIndependentAccount([439]);
  29. }
  30. /**
  31. * 会员专区、精彩生活区订单分账
  32. *
  33. * @param $merId
  34. * @param $timeLimit
  35. * @return void
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. private function doGreatLifeOrderIndependentAccount($merId = [], $timeLimit = 30)
  41. {
  42. // 商户信息
  43. $merchants = [];
  44. $time = time();
  45. // 已支付、未结算
  46. $where = ['paid' => 1, 'gzc' => 0, 'is_del' => 0, 'is_system_del' => 0];
  47. // 订单状态(0:待发货;1:待收货;2:待评价;3:已完成;4已申请退款;-1:已退款)5未支付 6已取消'
  48. $status = [2, 3];
  49. // 普通订单
  50. $cursor = Db::table('rrx_store_order')->where($where)->where('pay_type', 'in', '1,2,4')->whereIn('status', $status)
  51. ->whereIn('mer_id', $merId)->cursor();
  52. // $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
  53. // $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  54. // $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  55. /** @var StoreOrderRepository $storeOrderRepository */
  56. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  57. foreach ($cursor as $key => $value) {
  58. $settlement_period_time = strtotime($value['create_time']) + $timeLimit * 24 * 3600;
  59. // @todo 下单后第{$timeLimit}天后可分账
  60. if ($settlement_period_time >= $time) {
  61. if (!isset($merchants[$value['mer_id']])) {
  62. $merchants[$value['mer_id']] = Db::name('merchant')->where('mer_id', $value['mer_id'])->find();
  63. }
  64. $group_order = Db::name('store_group_order')->alias('sgo')->where('sgo.group_order_id', $value['group_order_id'])->find();
  65. // 分账账号 496:表示{会员专区}账户,精彩生活区分账到{会员专区}账户
  66. $independentAccount = $storeOrderRepository->getIndependentAccount($value, $group_order, 496, $merchants[$value['mer_id']]['hf_member_id']);
  67. // @todo 预分账处理 496:表示{会员专区}账户,精彩生活区分账到{会员专区}账户,故:hf_platform_member_id 为入账账户
  68. $isSuccessed = $storeOrderRepository->prepareExecuteIndependentAccount(
  69. $value, $group_order, $independentAccount['api_key'], $independentAccount['hf_platform_member_id'], '', $value['con_pri'], 0
  70. );
  71. Db::name("log")->insert(array('data' => '精彩生活区-订单分账:' . json_encode($value, JSON_FORCE_OBJECT)));//日志记录
  72. // Db::transaction(function () use ($certification, $online, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
  73. // //新增入账明细记录
  74. // $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
  75. // $create = [
  76. // 'uid' => $online['uid'],
  77. // 'platform_no' => $orderId,
  78. // 'account_type' => $online['pay_type'] == 1 ? 3 : 2,
  79. // 'user_name' => $certification['user_name'],
  80. // 'mobile' => $certification['mobile'],
  81. // 'user_card' => $certification['user_card'],
  82. // 'pension' => $pension,
  83. // 'order_type' => 2,
  84. // 'out_trade_no' => $online['order_sn']
  85. // ];
  86. // $test_user = Db::name('user')->where('uid', $online['uid'])->value('test_user');
  87. // $check = Db::name("gzc_logs")->where("out_trade_no", $online['order_sn'])->where("order_type", 2)->count();
  88. // if ($test_user == 0 && $check <= 0) {
  89. // $OrderGzcRepository->create($create);
  90. // }
  91. $order = StoreOrder::getInstance()->find($value['order_id']);
  92. $order->gzc = 1;
  93. $order->save();
  94. // });
  95. }
  96. }
  97. // 普通订单(红包,积分,京东,佣金,复购金,养老金,贡献值)
  98. // $cursor = Db::table('rrx_store_order')->where('paid', 1)->where('is_settlement', 0)->where('pay_type', 'in', '0,1,2,4')->cursor();
  99. // $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
  100. // $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  101. // $OrderGzcRepository = app()->make(OrderGzcRepository::class);
  102. // foreach ($cursor as $online) {
  103. // $status = $StoreOrderStatusRepository->getWhere(['order_id' => $online['order_id'], 'change_type' => 'take']);
  104. // if ($online['mer_id'] == 496) {
  105. // if (!in_array($online['status'], [0, 1, 2, 3])) {
  106. // continue;
  107. // }
  108. // if (time() < strtotime($online['pay_time']) + 3600 * 24) {
  109. // Log::info('还没有到收货后' . $timeLimit . '天');
  110. // continue;
  111. // }
  112. // } else {
  113. // if (!in_array($online['status'], [2, 3])) {
  114. // continue;
  115. // }
  116. // if (Carbon::now()->modify("-{$timeLimit} days")->lt($status->change_time ?? $status['change_time'])) {
  117. // Log::info('还没有到收货后' . $timeLimit . '天');
  118. // continue;
  119. // }
  120. // }
  121. //
  122. // $StoreOrderRepository = app()->make(StoreOrderRepository::class);
  123. // Db::transaction(function () use ($online, $StoreOrderRepository) {
  124. // $order = StoreOrder::getInstance()->find($online['order_id']);
  125. // $order->is_settlement = 1;
  126. // $order->settlement_time = date('Y-m-d H:i:s');
  127. // $order->save();
  128. //
  129. // // 结算积分
  130. // $user_sign_score_list = Db::name('user_sign_score')
  131. // ->where('status', -1)
  132. // ->where('order_id', $online['order_id'])
  133. // ->whereIn('type', [1, 2])
  134. // ->select()
  135. // ->toArray();
  136. // foreach ($user_sign_score_list as $user_sign_score) {
  137. // // 修改积分
  138. // Db::name('user')
  139. // ->where('uid', $user_sign_score['uid'])
  140. // ->inc('score', (float)$user_sign_score['reward_socre'])
  141. // ->update();
  142. // Db::name('user_sign_score')
  143. // ->where('status', -1)
  144. // ->where('id', $user_sign_score['id'])
  145. // ->update([
  146. // 'status' => 1,
  147. // 'settlement_time' => date('Y-m-d H:i:s')
  148. // ]);
  149. // }
  150. //
  151. // // 结算红包
  152. // $user_sign_hongbao_list = Db::name('user_sign_hongbao')
  153. // ->where('status', -1)
  154. // ->where('order_id', $online['order_id'])
  155. // ->whereIn('type', [1, 2])
  156. // ->select()
  157. // ->toArray();
  158. // foreach ($user_sign_hongbao_list as $user_sign_hongbao) {
  159. // // 修改红包
  160. // Db::name('user')
  161. // ->where('uid', $user_sign_hongbao['uid'])
  162. // ->inc('hongbao', (float)$user_sign_hongbao['number'])
  163. // ->update();
  164. // Db::name('user_sign_hongbao')
  165. // ->where('status', -1)
  166. // ->where('id', $user_sign_hongbao['id'])
  167. // ->update([
  168. // 'status' => 1,
  169. // 'settlement_time' => date('Y-m-d H:i:s')
  170. // ]);
  171. // }
  172. //
  173. //
  174. // // 结算京豆
  175. // $user_sign_jingdou_list = Db::name('user_sign_jingdou')
  176. // ->where('status', -1)
  177. // ->where('order_id', $online['order_id'])
  178. // ->whereIn('type', [1, 2])
  179. // ->select()
  180. // ->toArray();
  181. // foreach ($user_sign_jingdou_list as $user_sign_jingdou) {
  182. // // 修改京豆
  183. // Db::name('user')
  184. // ->where('uid', $user_sign_jingdou['uid'])
  185. // ->inc('jingdou', (float)$user_sign_jingdou['number'])
  186. // ->update();
  187. // Db::name('user_sign_jingdou')
  188. // ->where('status', -1)
  189. // ->where('id', $user_sign_jingdou['id'])
  190. // ->update([
  191. // 'status' => 1,
  192. // 'settlement_time' => date('Y-m-d H:i:s')
  193. // ]);
  194. // }
  195. //
  196. //
  197. //
  198. // // 结算复购金
  199. // $user_sign_fugou_list = Db::name('user_sign_fugou')
  200. // ->where('status', -1)
  201. // ->where('order_id', $online['order_id'])
  202. // ->whereIn('type', [1, 2])
  203. // ->select()
  204. // ->toArray();
  205. // foreach ($user_sign_fugou_list as $user_sign_fugou) {
  206. // // 修改复购金
  207. // Db::name('user')
  208. // ->where('uid', $user_sign_fugou['uid'])
  209. // ->inc('fugou', (float)$user_sign_fugou['number'])
  210. // ->update();
  211. // Db::name('user_sign_fugou')
  212. // ->where('status', -1)
  213. // ->where('id', $user_sign_fugou['id'])
  214. // ->update([
  215. // 'status' => 1,
  216. // 'settlement_time' => date('Y-m-d H:i:s')
  217. // ]);
  218. // }
  219. //
  220. //
  221. // // 结算贡献值
  222. // $user_sign_gongxian_list = Db::name('user_sign_gongxian')
  223. // ->where('status', -1)
  224. // ->where('order_id', $online['order_id'])
  225. // ->whereIn('type', [1, 2])
  226. // ->select()
  227. // ->toArray();
  228. // foreach ($user_sign_gongxian_list as $user_sign_gongxian) {
  229. // // 修改贡献值
  230. // Db::name('user')
  231. // ->where('uid', $user_sign_gongxian['uid'])
  232. // ->inc('contribute', (float)$user_sign_gongxian['number'])
  233. // ->update();
  234. // Db::name('user_sign_gongxian')
  235. // ->where('status', -1)
  236. // ->where('id', $user_sign_gongxian['id'])
  237. // ->update([
  238. // 'status' => 1,
  239. // 'settlement_time' => date('Y-m-d H:i:s')
  240. // ]);
  241. // }
  242. //
  243. //
  244. // // 结算佣金
  245. // $user_bill_list = Db::name('user_bill')
  246. // ->where('status', 0)
  247. // ->where('commission_type','<>', 5)
  248. // ->where([ 'link_id' => $online['order_id'], 'order_sn' => $online['order_sn']])
  249. // ->select()
  250. // ->toArray();
  251. // foreach ($user_bill_list as $user_bill) {
  252. // if ($user_bill['commission_type'] != 5) {
  253. // if (in_array($user_bill['commission_type'], [7,12,19])) {
  254. // Db::name("merchant")
  255. // ->where("mer_id", $user_bill['mer_id'])
  256. // ->inc('brokerage_price', (float)$user_bill['number'])
  257. // ->update();
  258. // } else {
  259. // Db::name('user')
  260. // ->where('uid', $user_bill['uid'])
  261. // ->inc('brokerage_price', (float)$user_bill['number'])
  262. // ->update();
  263. // }
  264. // }
  265. // if ($online['pay_type'] == 0 && $user_bill['commission_type'] == 5) {
  266. // } else {
  267. // // 虚拟支付的养老金不修改不结算
  268. // Db::name('user_bill')
  269. // ->where('status', 0)
  270. // ->where('bill_id', $user_bill['bill_id'])
  271. // ->update([
  272. // 'status' => 1,
  273. // 'take_time' => time()
  274. // ]);
  275. // }
  276. // if ($online['pay_type'] == 0 && $user_bill['commission_type'] == 5) {
  277. // } else {
  278. // // 虚拟支付的养老金不修改不结算
  279. // Db::name('user_bill')
  280. // ->where('status', 0)
  281. // ->where('bill_id', $user_bill['bill_id'])
  282. // ->update([
  283. // 'status' => 1,
  284. // 'take_time' => time()
  285. // ]);
  286. // }
  287. // }
  288. // //按角色结算
  289. // $StoreOrderRepository->close_order($online['order_id']);
  290. // });
  291. // }
  292. }
  293. }