GzcAddByBillCommand.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace app\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\Output;
  6. use think\facade\{Db, Log};
  7. class GzcAddByBillCommand extends Command
  8. {
  9. protected function configure()
  10. {
  11. $this->setName('gzcAddByBill')
  12. ->setDescription('从bill表中查询未提交到公证处的养老金记录,并提交公证处');
  13. }
  14. protected function execute(Input $input, Output $output)
  15. {
  16. $output->writeln('定时任务执行中...');
  17. $this->billGzcAdd();
  18. /** 修复 历史数据脚本 */
  19. // $gzcLogList = Db::name("gzc_logs")
  20. // ->where('link_id', '=', 0)
  21. // ->select()
  22. // ->toArray();
  23. // $orderSnList = array_column($gzcLogList, 'out_trade_no');
  24. // $userBillList = Db::name('user_bill')
  25. // ->where('status', 1)
  26. // ->where('commission_type', 5)
  27. // ->whereIn('order_sn', $orderSnList)
  28. // ->select()
  29. // ->toArray();
  30. // $userBillMap = [];
  31. // foreach ($userBillList as $key => $value) {
  32. // $userBillMap[$value['order_sn'] . '_' . $value['uid'] . '_' . $value['number']] = [
  33. // 'bill_id' => $value['bill_id'],
  34. // 'type_shop' => $value['type_shop']
  35. // ];
  36. // }
  37. // unset($userBillList);
  38. // $str = '';
  39. // foreach ($gzcLogList as $value) {
  40. // if (isset($userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']])) {
  41. // $update = [
  42. // 'link_id' => $userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']]['bill_id'],
  43. // 'order_type' => $userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']]['type_shop'],
  44. // ];
  45. // if (empty($value['platform_no'])) {
  46. // $update['platform_no'] = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999);
  47. // }
  48. // Db::name("gzc_logs")
  49. // ->where('id', '=', $value['id'])
  50. // ->update($update);
  51. // $str .= $value['id'] . '、';
  52. // }
  53. // }
  54. // echo $str;
  55. // // 定时任务执行中...
  56. // // 181、274、275、277、279、280、281、282、283、284、285、286、287、288、289、290、291、292、293、294、295、296、297、298、299、300、301、302、303、304、305、306、307、308、309、310、311、312、313、314、315、316、317、318、319、320、321、322、323、324、325、326、327、328、329、330、331、332、333、334、335、336352、353、354、355、356、357、358、359、360、361、362、363、364、365、366、367、368、369、370、371、372、373、374、375、376、377、378、379、380、381、382、383、384、385、386、387、388、389、390、391、392、393、394、395、396、398、400、401、402、403、404、405、406、407、408、409、410、411、412、413、414、415431、432、433、434、435、436、437、438、439、440、441、442、443、444、445、446、447、448、449、450、451、452、453、454、455、456、457、458、459、460、461、462、463、464、465、466、467、468、469、470、471、472、473、474、475、476、477、478、479、480、481、482、483、484、485、486、487、488、489、490、491、492508、509、510、511、512、513、514、515、516、517、518、519、520、521、522、523、524、525、526、527、528、529、530、1591、1607、1608、1632、1633、1634、1649、1650、1651、1652、2108、2109、2110、2111、3687、3688、3689、3690、3691、3692、3693、3694、3695、3696、3697、3698、3699、3700、3701、3702、3703、3704、3705、3706、3707、3708、
  57. // // 定时任务执行完成
  58. /** 删除重复数据脚本 */
  59. // $gzcLogList = Db::query('SELECT link_id FROM rrx_gzc_logs WHERE link_id != 0 GROUP BY link_id HAVING COUNT(*) > 1');
  60. // $linkIdList = array_column($gzcLogList, 'link_id');
  61. // $gzcLogList = Db::name("gzc_logs")
  62. // ->whereIn('link_id', $linkIdList)
  63. // ->field('id, gzc, success, link_id')
  64. // ->order('id ASC')
  65. // ->select()
  66. // ->toArray();
  67. // $gzcLogMap = [];
  68. // foreach ($gzcLogList as $item) {
  69. // $gzcLogMap[$item['link_id']][] = $item;
  70. // }
  71. //
  72. // $delAllIdList = [];
  73. // foreach ($gzcLogMap as $gzcLogArr) {
  74. // $gzcIdList = [];
  75. // $allIds = [];
  76. // // 单次遍历同时收集所有ID和需要保留的ID
  77. // foreach ($gzcLogArr as $gzcLog) {
  78. // $allIds[] = $gzcLog['id'];
  79. // if ($gzcLog['gzc'] != 0) {
  80. // $gzcIdList[] = $gzcLog['id'];
  81. // }
  82. // }
  83. // // 确定需要删除的ID列表
  84. // if (!empty($gzcIdList)) {
  85. // // 保留有gzc标志的记录,删除其他
  86. // $delIdList = array_diff($allIds, $gzcIdList);
  87. // } else {
  88. // // 保留第一条记录,删除其他
  89. // $delIdList = array_slice($allIds, 1);
  90. // }
  91. // $delAllIdList = array_merge($delAllIdList, $delIdList);
  92. // }
  93. //
  94. // // 如果后续需要索引连续
  95. // $delAllIdList = array_values($delAllIdList);
  96. // $res = Db::name("gzc_logs")
  97. // ->whereIn('id', $delAllIdList)
  98. // ->delete();
  99. // echo implode('、', $delAllIdList);
  100. // echo 'deleteNum' . $res;
  101. // // 定时任务执行中...
  102. // // 2327、2362、2375、2413、2414、2487、2536、2550、2564、320、2576、2578、2579、2580、2581、2588、2589、2599、2601、2602、2603、2604、2605、2606、2607、2608、2614、2638、2666、2680、2706、2707、2721、2729、2736、2739、2744、2748、2811、2859、2860、2861、2897、2928、2933、3264、3277、3279、3571、3572、3573、357、1590、3673、3675、1631、3678、3679、3680、3681、2106、3683、2107、3684、2112、3685、2113、3686
  103. // // deleteNum78
  104. // // 定时任务执行完成
  105. /** 将之前多跑出来的佣金和 */
  106. // $userBillLinkIdMap = Db::query('SELECT link_id FROM `rrx_user_bill` WHERE link_id != 0 GROUP BY link_id, type_shop, uid, number, commission_type HAVING COUNT(*) > 1');
  107. // $linkIdList = array_column($userBillLinkIdMap, 'link_id');
  108. //
  109. // $userBillList = Db::name("user_bill")
  110. // ->whereIn('link_id', $linkIdList)
  111. // ->field('bill_id, gzc, link_id, type_shop, uid, number, commission_type, order_sn, status')
  112. // ->order('bill_id ASC')
  113. // ->select()
  114. // ->toArray();
  115. //
  116. // $userBillMap = [];
  117. // $userBillIdList = [];
  118. // foreach ($userBillList as $item) {
  119. // $userBillIdList[] = $item['bill_id'];
  120. // $userBillMap[$item['link_id'] . '_' . $item['type_shop'] . '_' . $item['uid'] . '_' . $item['commission_type'] . '_' . $item['number']][] = $item;
  121. // }
  122. //
  123. // $gzcLogList = Db::name("gzc_logs")
  124. // ->whereIn('link_id', $userBillIdList)
  125. // ->field('id, gzc, success, link_id')
  126. // ->order('id ASC')
  127. // ->select()
  128. // ->toArray();
  129. // // 提前查询过了,按正常来说 就是一对一,上边数据修复过后,没有重复数据
  130. // $gzcLogMap = array_column($gzcLogList, null, 'link_id');
  131. //
  132. // $delAllIdList = [];
  133. // $delYongJin = [];
  134. // $yjNum = 0.00;
  135. // $yongjinKouchu = [];
  136. // $yongjinKouchuDelBillIdList = [];
  137. // foreach ($userBillMap as $userBillArr) {
  138. // $allIds = [];
  139. // $baoLiuIds = [];
  140. // $yjDataList = [];
  141. // // 单次遍历同时收集所有ID和需要保留的ID
  142. // foreach ($userBillArr as $userBill) {
  143. // $allIds[] = $userBill['bill_id'];
  144. // // 如果该条记录是养老金记录 则根据 养老金公证处记录表 内的条件 来决定是否删除该条记录
  145. // if ($userBill['commission_type'] == 5) {
  146. // if (!empty($gzcLogMap[$userBill['bill_id']] ?? []) && $gzcLogMap[$userBill['bill_id']]['success'] == 1) {
  147. // // 如果该条 养老金记录 推送到了公证处明细记录表
  148. // $baoLiuIds[] = $userBill['bill_id'];
  149. // }
  150. // }
  151. //
  152. // if ($userBill['commission_type'] == 3) {
  153. // if ($userBill['status'] == 1) {
  154. // $yjDataList[] = $userBill;
  155. // }
  156. // }
  157. // }
  158. // // 确定需要删除的ID列表
  159. // if (!empty($billIdList)) {
  160. // // 保留有gzc标志的记录,删除其他
  161. // $delIdList = array_diff($allIds, $billIdList);
  162. // } else if (!empty($yjDataList)) {
  163. // // 保留第一条 已生效 的佣金,其余的,需要从用户身上扣减 并删除
  164. // $yjDataOne = array_shift($yjDataList);
  165. // if (!empty($yjDataList)) {
  166. // // 如果移除掉第一个 还有已经发放的 佣金 需要从用户身上扣除
  167. // foreach ($yjDataList as $billData) {
  168. // $delYongJin[] = $billData['bill_id'];
  169. // $yjNum += $billData['number'];
  170. // if (!isset($yongjinKouchu[$billData['uid']])) {
  171. // $yongjinKouchu[$billData['uid']] = 0.00;
  172. // }
  173. // $yongjinKouchu[$billData['uid']] += $billData['number'];
  174. // $yongjinKouchuDelBillIdList[$billData['uid']][] = $billData['bill_id'];
  175. // }
  176. // }
  177. // $delIdList = array_values(array_diff($allIds, [$yjDataOne['bill_id']]));
  178. // } else {
  179. // // 保留第一条记录,删除其他
  180. // $delIdList = array_slice($allIds, 1);
  181. // }
  182. // $delAllIdList = array_merge($delAllIdList, $delIdList);
  183. // }
  184. //
  185. // echo '应删除的Bill记录' . implode(',', $delAllIdList) . "\n";
  186. // echo '应删除的佣金记录' . implode(',', $delYongJin) . "\n";
  187. // echo '应删除的佣金金额' . $yjNum . "\n";
  188. //
  189. // $successDecYongjin = 0.00;
  190. // $fallDelBillIdList = [];
  191. // if (!empty($yongjinKouchu)) {
  192. // foreach ($yongjinKouchu as $uid => $number) {
  193. // $user = Db::name('user')->where('uid', '=', $uid)->find();
  194. // if ($user) {
  195. // if ($user['brokerage_price'] >= $number) {
  196. // $updateNum = Db::name('user')->where('uid', '=', $uid)->dec('brokerage_price', $number)->update();
  197. // if ($updateNum) {
  198. // $successDecYongjin += $number;
  199. // echo "用户佣金扣除成功(uid:$uid ,账号:{$user['account']} ,用户佣金余额:{$user['brokerage_price']},应扣除佣金:$number )\n";
  200. // continue;
  201. // } else {
  202. // echo "用户佣金余额不足(uid:$uid ,账号:{$user['account']} ,用户佣金余额:{$user['brokerage_price']},应扣除佣金:$number )\n";
  203. // }
  204. // } else {
  205. // echo "用户佣金余额不足(uid:$uid ,账号:{$user['account']} ,用户佣金余额:{$user['brokerage_price']},应扣除佣金:$number )\n";
  206. // }
  207. // } else {
  208. // echo "未查询到用户信息(uid:$uid ,应扣除佣金:$number )\n";
  209. // }
  210. // $fallDelBillIdList = array_values(array_merge($fallDelBillIdList, $yongjinKouchuDelBillIdList[$uid]));
  211. // }
  212. // }
  213. // echo '因佣金扣减失败而不能删除的佣金记录:' . implode(',', $fallDelBillIdList) . "\n";
  214. // echo '成功扣减佣金总额:' . $successDecYongjin . "\n";
  215. // $delAllIdList = array_values(array_diff($delAllIdList, $fallDelBillIdList));
  216. // echo '实际删除的Bill记录' . implode(',', $delAllIdList) . "\n";
  217. // Db::name("user_bill")
  218. // ->whereIn('bill_id', $delAllIdList)
  219. // ->delete();
  220. // Db::name("gzc_logs")
  221. // ->whereIn('link_id', $delAllIdList)
  222. // ->delete();
  223. // $delYongJin = array_values(array_diff($delYongJin, $fallDelBillIdList));
  224. // echo '实际应删除的佣金记录' . implode(',', $delYongJin) . "\n";
  225. $output->writeln('定时任务执行完成');
  226. }
  227. public function billGzcAdd()
  228. {
  229. $testUserIdList = Db::name('user')
  230. ->where('test_user', 1)
  231. ->column('uid');
  232. // 1. 使用chunk分批处理
  233. Db::name('user_bill')
  234. ->where('status', 1)
  235. ->where('gzc', 0)
  236. ->where('commission_type', 5)
  237. ->whereNotIn('uid', $testUserIdList)
  238. ->chunk(100, function ($bills) {
  239. $this->processBills($bills);
  240. });
  241. }
  242. protected function processBills($bills)
  243. {
  244. // 2. 预加载用户实名认证信息
  245. $userIds = array_unique(array_column($bills->toArray(), 'uid'));
  246. $certifications = Db::name('user_certification')
  247. ->whereIn('uid', $userIds)
  248. ->where('status', '=', 1)
  249. ->column('user_name,mobile,user_card', 'uid');
  250. // 3. 订单类型映射配置
  251. $orderMap = [
  252. 0 => ['table' => 'rrx_store_order', 'condition' => [['paid', '=', 1]]],
  253. 1 => ['table' => 'rrx_order_pdd'],
  254. 2 => ['table' => 'rrx_order_vip'],
  255. 3 => ['table' => 'rrx_order_su'],
  256. 5 => ['table' => 'rrx_order_tb'],
  257. 6 => ['table' => 'rrx_order_jd'],
  258. 9 => ['table' => 'rrx_order_huafei'],
  259. 10 => [
  260. 'table' => 'rrx_order_merchant',
  261. 'condition' => [['paid', '=', 1], ['status', '=', 1]],
  262. 'field_map' => ['order_sn' => 'out_trade_no']
  263. ],
  264. ];
  265. foreach ($bills as $bill) {
  266. $typeShop = $bill['type_shop'] ?? 0;
  267. // 4. 跳过无效类型
  268. if (in_array($typeShop, [4, 7, 8])) continue;
  269. // 5. 使用映射配置查询订单
  270. $online = $this->getOrderData($orderMap, $typeShop, $bill);
  271. if (!$online) {
  272. Log::info("用户{$bill['uid']}订单{$bill['order_sn']}不存在");
  273. continue;
  274. }
  275. // 6. 检查实名认证
  276. if (!isset($certifications[$bill['uid']])) {
  277. Log::info("用户{$bill['uid']}未实名认证");
  278. continue;
  279. }
  280. // 7. 验证养老金金额
  281. $pension = $bill['number'] ?? 0;
  282. if ($pension <= 0) {
  283. Log::info("订单{$online['order_sn']}养老金金额无效: {$pension}");
  284. continue;
  285. }
  286. // 8. 事务处理
  287. Db::transaction(function () use ($bill, $online, $certifications, $pension, $typeShop) {
  288. // 检查是否已存在记录
  289. $exists = Db::name("gzc_logs")
  290. ->where("link_id", $bill['bill_id'])
  291. ->where("order_type", $typeShop)
  292. ->count();
  293. if ($exists) return;
  294. // 创建唯一订单ID
  295. $orderId = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999);
  296. Db::name("gzc_logs")->insert([
  297. 'uid' => $bill['uid'],
  298. 'platform_no' => $orderId,
  299. 'account_type' => 1,
  300. 'user_name' => $certifications[$bill['uid']]['user_name'],
  301. 'mobile' => $certifications[$bill['uid']]['mobile'],
  302. 'user_card' => $certifications[$bill['uid']]['user_card'],
  303. 'pension' => $pension,
  304. 'order_type' => $typeShop,
  305. 'out_trade_no' => $online['order_sn'],
  306. 'link_id' => $bill['bill_id'],
  307. 'create_time' => date('Y-m-d H:i:s'),
  308. 'update_time' => date('Y-m-d H:i:s')
  309. ]);
  310. Db::name('user_bill')->where('bill_id', $bill['bill_id'])->update([
  311. 'is_gzc' => 1,
  312. 'gzc' => 1
  313. ]);
  314. });
  315. }
  316. }
  317. protected function getOrderData($orderMap, $typeShop, $bill)
  318. {
  319. $config = $orderMap[$typeShop] ?? $orderMap[0];
  320. $query = Db::table($config['table'])
  321. ->where($config['field_map']['order_sn'] ?? 'order_sn', $bill['order_sn']);
  322. if (!empty($config['condition'])) {
  323. $query->where($config['condition']);
  324. }
  325. $result = $query->find();
  326. // 字段映射处理
  327. if ($result && isset($config['field_map'])) {
  328. foreach ($config['field_map'] as $src => $dest) {
  329. $result[$src] = $result[$dest] ?? null;
  330. }
  331. }
  332. return $result;
  333. }
  334. }