U8Settle.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Jobs\ProcessWMSDataJob;
  4. use App\Model\SyncTempRecord;
  5. use App\Service\U8ThirtyPartyDatabaseServerService;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Redis;
  10. class U8Settle extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'command:u8_settle';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Command description';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. public function handle()
  34. {
  35. try {
  36. $this->settle();
  37. }catch (\Exception $exception){
  38. Log::channel('apiLog')->info('异常', ['msg' => $exception->getMessage() . '|' . $exception->getLine()]);
  39. }
  40. }
  41. public function settle(){
  42. $service = new U8ThirtyPartyDatabaseServerService();
  43. $minPeriods = $this->getYjData();
  44. if(empty($minPeriods['pu_date']) || empty($minPeriods['st_date']) || empty($minPeriods['sa_date'])) {
  45. Log::channel('apiLog')->info('月结数据为空', ['msg' => $minPeriods]);
  46. return;
  47. }
  48. //单据-----------------
  49. $this->orderInsert($service, $minPeriods);
  50. }
  51. public function orderInsert($service, $minPeriods){
  52. $pu = $minPeriods['pu_date'] . ' 00:00:00.000';
  53. $st = $minPeriods['st_date'] . ' 00:00:00.000';
  54. $sa = $minPeriods['sa_date'] . ' 00:00:00.000';
  55. $dateMap = [
  56. SyncTempRecord::type_one => $pu,
  57. SyncTempRecord::type_two => $st,
  58. SyncTempRecord::type_three => $st,
  59. SyncTempRecord::type_four => $sa,
  60. SyncTempRecord::type_five => $st,
  61. SyncTempRecord::type_six => $sa,
  62. SyncTempRecord::type_seven => $st,
  63. SyncTempRecord::type_eight => $st,
  64. ];
  65. // 配置不同单据的表名和字段
  66. $tasks = [
  67. SyncTempRecord::type_one => [// 采购到货 0 |退货 1
  68. 'main' => 'PU_ArrivalVouch',
  69. 'detail' => 'PU_ArrivalVouchs',
  70. 'main_key' => 'ID',
  71. 'key' => 'ID',
  72. 'whereRaw' => "(dDate >= '{$pu}')",
  73. 'main_field' => ['ID as id','cCode as no','dDate as order_date', 'cMakeTime as crt_time', 'cModifyTime as upd_time', 'iBillType as type', 'cverifier as reviewer'],
  74. 'son_field' => [
  75. 'detail.ID as id',
  76. // 'detail.ivouchrowno as lineNum',
  77. // 'detail.cWhCode as warehouseCode',
  78. 'detail.cInvCode as materialCode',
  79. 'detail.iQuantity as planQty',
  80. // 'detail.cordercode as from_order',
  81. ],
  82. 'limit' => 30,
  83. ],
  84. SyncTempRecord::type_two => [// 领料申请单 材料出库----
  85. 'main' => 'MaterialAppVouch',
  86. 'detail' => 'MaterialAppVouchs',
  87. 'main_key' => 'ID',
  88. 'key' => 'ID',
  89. 'whereRaw' => "(dDate >= '{$st}')",
  90. 'main_field' => ['ID as id','cCode as no', 'dDate as order_date', 'dnmaketime as crt_time', 'dnmodifytime as upd_time','iverifystate as state', 'cHandler as reviewer'],
  91. 'son_field' => [
  92. 'detail.ID as id',
  93. // 'detail.ivouchrowno as lineNum',
  94. // 'detail.cWhCode as warehouseCode',
  95. 'detail.cInvCode as materialCode',
  96. 'detail.iQuantity as planQty',
  97. ],
  98. 'limit' => 30,
  99. ],
  100. SyncTempRecord::type_three => [// 产品报检单 产成品入库流程----- 没有仓库
  101. 'main' => 'QMINSPECTVOUCHER',
  102. 'detail' => 'QMINSPECTVOUCHERS',
  103. 'main_key' => 'ID',
  104. 'key' => 'ID',
  105. 'whereRaw' => "(DDATE >= '{$st}' and CVOUCHTYPE = 'QM02')",
  106. 'main_field' => ['ID as id','CINSPECTCODE as no','DDATE as order_date','DMAKETIME as crt_time', 'DMODIFYTIME as upd_time','CVERIFIER as reviewer'],
  107. 'son_field' => [
  108. 'detail.ID as id',
  109. 'detail.CINVCODE as materialCode',
  110. 'detail.FQUANTITY as planQty',
  111. 'detail.CDEFINE32 as lottar1',
  112. ],
  113. 'limit' => 30,
  114. ],
  115. SyncTempRecord::type_four => [// 销售订单 销售出库流程 ------- 没有仓库
  116. 'main' => 'SO_SOMain',
  117. 'detail' => 'SO_SODetails',
  118. 'main_key' => 'ID',
  119. 'key' => 'ID',
  120. 'whereRaw' => "(dDate >= '{$sa}')",
  121. 'main_field' => ['ID as id','cSOCode as no','dDate as order_date','dcreatesystime as crt_time', 'dmodifysystime as upd_time','cVerifier as reviewer'],
  122. 'son_field' => [
  123. 'detail.ID as id',
  124. 'detail.cInvCode as materialCode',
  125. 'detail.iQuantity as planQty',
  126. ],
  127. 'limit' => 30,
  128. ],
  129. SyncTempRecord::type_five => [// 其他入 其他入库流程
  130. 'main' => 'RdRecord08',
  131. 'detail' => 'RdRecords08',
  132. 'main_key' => 'ID',
  133. 'key' => 'ID',
  134. 'whereRaw' => "(dDate >= '{$st}')",
  135. 'main_field' => ['ID as id','cCode as no','dDate as order_date','dnmaketime as crt_time', 'dnmodifytime as upd_time','cWhCode as warehouseCode','cHandler as reviewer'],
  136. 'son_field' => [
  137. 'detail.ID as id',
  138. 'detail.cInvCode as materialCode',
  139. 'detail.iQuantity as planQty',
  140. ],
  141. 'limit' => 30,
  142. ],
  143. SyncTempRecord::type_six => [// 销售退货单 其他入库流程
  144. 'main' => 'DispatchList',
  145. 'detail' => 'DispatchLists',
  146. 'main_key' => 'DLID',
  147. 'key' => 'DLID',
  148. 'whereRaw' => "(dDate >= '{$st}' and bReturnFlag = 1)",
  149. 'main_field' => ['DLID as id','cDLCode as no', 'dDate as order_date','dcreatesystime as crt_time', 'dmodifysystime as upd_time','cVerifier as reviewer'],
  150. 'son_field' => [
  151. 'detail.DLID as id',
  152. 'detail.cInvCode as materialCode',
  153. 'detail.cWhCode as warehouseCode',
  154. 'detail.iQuantity as planQty',
  155. ],
  156. 'limit' => 30,
  157. ],
  158. SyncTempRecord::type_seven => [// 其他出 其他入库流程
  159. 'main' => 'RdRecord09',
  160. 'detail' => 'RdRecords09',
  161. 'main_key' => 'ID',
  162. 'key' => 'ID',
  163. 'whereRaw' => "(dDate >= '{$st}')",
  164. 'main_field' => ['ID as id','cCode as no','dDate as order_date','dnmaketime as crt_time', 'dnmodifytime as upd_time','cWhCode as warehouseCode','cHandler as reviewer'],
  165. 'son_field' => [
  166. 'detail.ID as id',
  167. 'detail.cInvCode as materialCode',
  168. 'detail.iQuantity as planQty',
  169. ],
  170. 'limit' => 30,
  171. ],
  172. SyncTempRecord::type_eight => [
  173. //检验单
  174. //cvouchtype=null=>采购到货单 1
  175. //cvouchtype=QM04=>产品检验单 2
  176. //cvouchtype=QM14=>退货检验单 3
  177. 'main' => 'QMCHECKVOUCHER',
  178. 'detail' => 'QMCHECKVOUCHERS',
  179. 'main_key' => 'ID',
  180. 'key' => '',
  181. 'whereRaw' => "(DDATE >= '{$st}' and (CVOUCHTYPE = null OR CVOUCHTYPE = 'QM04' OR CVOUCHTYPE = 'QM14'))",
  182. 'main_field' => ['ID as id','CCHECKCODE as no','DDATE as order_date','DMAKETIME as crt_time', 'DMODIFYTIME as upd_time','CVERIFIER as reviewer', 'CVOUCHTYPE as type', 'CINVCODE as materialCode', 'CBATCH as lot', 'FREGQUANTITY as hg_quantity', 'FDISQUANTITY as hg_not_quantity', 'FCONQUANTIY as rb_quantity'],
  183. 'son_field' => [],
  184. 'limit' => 30,
  185. ],
  186. ];
  187. $time = time();
  188. foreach ($tasks as $name => $config) {
  189. // 用于记录本次在 U8 查到的所有单号,用来比对删除
  190. $currentU8Nos = [];
  191. $lastId = 0;
  192. $orderDateLimit = $dateMap[$name]; // 当前类型的起始日期限制
  193. while (true) {
  194. list($status, $items) = $service->getPendingBills($config, $lastId);
  195. if (!$status || empty($items)) break;
  196. $nos = collect($items)->pluck('no')->toArray();
  197. $currentU8Nos = array_merge($currentU8Nos, $nos); // 记录当前存在的单号
  198. //获取这 30 条的快照
  199. $snapshots = DB::table('sync_snapshot')
  200. ->where('type', $name)
  201. ->whereIn('u8_no', $nos)
  202. ->get()
  203. ->keyBy('u8_no');
  204. foreach ($items as $item) {
  205. if ($name == SyncTempRecord::type_eight){
  206. $type_v = 0;
  207. if($item['type'] == null){
  208. $type_v = 1;
  209. }elseif ($item['type'] == 'QM04'){
  210. $type_v = 2;
  211. }elseif ($item['type'] == 'QM14'){
  212. $type_v = 3;
  213. }
  214. $item['type'] = $type_v;
  215. }
  216. $no = $item['no'];
  217. $u8Id = $item['id']; // 抓取主表 ID
  218. $u8Upd = $item['upd_time'] ?: $item['crt_time'];
  219. $snapshot = $snapshots->get($no);
  220. $type_2 = isset($item['type']) ? $item['type'] : 0;
  221. $bool = $item['reviewer'] ? true : false;
  222. if(! $bool) continue;// 没审核跳过
  223. //退货单没有来源单据的不推送
  224. // if($name == SyncTempRecord::type_one && $item['type_2'] == 1 && empty($item['details'][0]['from_order'])) continue;
  225. $opType = null;
  226. if (!$snapshot) {
  227. $opType = SyncTempRecord::opt_zero;
  228. } elseif ($u8Upd > $snapshot->last_upd_time) {
  229. $opType = SyncTempRecord::opt_one;
  230. }
  231. if ($opType !== null) {
  232. // 写入任务流水
  233. $this->createSyncTask($name, $no, $u8Id, $item, $opType, $u8Upd, $time, $type_2);
  234. }
  235. }
  236. //分页最大id
  237. $lastId = collect($items)->max('id');
  238. }
  239. $currentU8NosLookup = array_flip($currentU8Nos);
  240. // 使用 chunk 配合日期过滤,只查快照中大于等于月结日期的单据
  241. DB::table('sync_snapshot')
  242. ->where('type', $name)
  243. ->where('order_date', '>=', $orderDateLimit) // 关键:缩小快照查询范围
  244. ->orderBy('u8_id')
  245. ->chunk(100, function ($snapshots) use ($currentU8NosLookup, $name, $time) {
  246. foreach ($snapshots as $oldSnapshot) {
  247. // 如果快照里的单号不在本次 U8 扫描结果里
  248. if (!isset($currentU8NosLookup[$oldSnapshot->u8_no])) {
  249. $this->createSyncTask($name, $oldSnapshot->u8_no, $oldSnapshot->u8_id, $oldSnapshot->payload, SyncTempRecord::opt_two, "", $time);
  250. }
  251. }
  252. });
  253. unset($currentU8Nos); // 释放内存
  254. }
  255. }
  256. private function createSyncTask($type, $no, $u8Id, $payload, $opType, $u8Upd = "", $time, $type_2 = 0) {
  257. $record = SyncTempRecord::create([
  258. 'type' => $type,
  259. 'type_2' => $type_2,
  260. 'u8_no' => $no,
  261. 'u8_id' => $u8Id,
  262. 'payload' => json_encode($payload),
  263. 'u8_upd' => $u8Upd,
  264. 'op_type' => $opType,
  265. 'crt_time'=> $time,
  266. ]);
  267. // 只分发 ID 给队列,Job 内部再根据此 ID 取 u8_id 和 payload
  268. ProcessWMSDataJob::dispatch(['id' => $record->id])->onQueue("sync_wms_order");
  269. }
  270. private function getYjData(){
  271. //结账
  272. //采购到货单 | 退货单 bflag_PU 采购
  273. //领料申请单|产成品入库单| bflag_ST 出和入
  274. //销售发货单 销售退货 bflag_SA 销售
  275. $result = DB::connection('u8_third_sqlserver')
  276. ->table('gl_mend')
  277. ->lock('WITH(NOLOCK)')
  278. ->selectRaw("
  279. MIN(CASE WHEN bflag_PU = 0 AND iyear >= 2023 THEN iYPeriod END) as min_pu,
  280. MIN(CASE WHEN bflag_ST = 0 THEN iYPeriod END) as min_st,
  281. MIN(CASE WHEN bflag_SA = 0 THEN iYPeriod END) as min_sa
  282. ")
  283. ->whereRaw("RIGHT(CAST(iYPeriod AS VARCHAR), 2) <> '00'")
  284. ->first();
  285. // 转换成数组方便取值
  286. $minPeriods = (array)$result;
  287. $formatDate = function($period) {
  288. if (!$period) return null; // 如果没有未结账月份,返回null
  289. $period = (string)$period; // 确保是字符串
  290. $year = substr($period, 0, 4);
  291. $month = substr($period, 4, 2);
  292. return "{$year}-{$month}-01";
  293. };
  294. // 2. 执行转换
  295. $formattedPeriods = [
  296. 'pu_date' => $formatDate($minPeriods['min_pu'] ?? null),
  297. 'st_date' => $formatDate($minPeriods['min_st'] ?? null),
  298. 'sa_date' => $formatDate($minPeriods['min_sa'] ?? null),
  299. ];
  300. return $formattedPeriods;
  301. }
  302. }