U8Settle.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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('u8_daily')->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('u8_daily')->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.cDefine23',
  81. 'detail.cBatch',
  82. ],
  83. 'limit' => 30,
  84. ],
  85. SyncTempRecord::type_two => [// 领料申请单 材料出库----
  86. 'main' => 'MaterialAppVouch',
  87. 'detail' => 'MaterialAppVouchs',
  88. 'main_key' => 'ID',
  89. 'key' => 'ID',
  90. 'whereRaw' => "(dDate >= '{$st}')",
  91. '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'],
  92. 'son_field' => [
  93. 'detail.ID as id',
  94. // 'detail.ivouchrowno as lineNum',
  95. // 'detail.cWhCode as warehouseCode',
  96. 'detail.cInvCode as materialCode',
  97. 'detail.iQuantity as planQty',
  98. ],
  99. 'limit' => 30,
  100. ],
  101. SyncTempRecord::type_three => [// 产品报检单 产成品入库流程----- 没有仓库
  102. 'main' => 'QMINSPECTVOUCHER',
  103. 'detail' => 'QMINSPECTVOUCHERS',
  104. 'main_key' => 'ID',
  105. 'key' => 'ID',
  106. 'whereRaw' => "(DDATE >= '{$st}' and CVOUCHTYPE = 'QM02')",
  107. 'main_field' => ['ID as id','CINSPECTCODE as no','DDATE as order_date','DMAKETIME as crt_time', 'DMODIFYTIME as upd_time','CVERIFIER as reviewer'],
  108. 'son_field' => [
  109. 'detail.ID as id',
  110. 'detail.AUTOID as auto_id',
  111. 'detail.CINVCODE as materialCode',
  112. 'detail.FQUANTITY as planQty',
  113. 'detail.cDefine23',
  114. ],
  115. 'limit' => 30,
  116. ],
  117. SyncTempRecord::type_four => [// 销售订单 销售出库流程 ------- 没有仓库
  118. 'main' => 'SO_SOMain',
  119. 'detail' => 'SO_SODetails',
  120. 'main_key' => 'ID',
  121. 'key' => 'ID',
  122. 'whereRaw' => "(dDate >= '{$sa}')",
  123. 'main_field' => ['ID as id','cSOCode as no','dDate as order_date','dcreatesystime as crt_time', 'dmodifysystime as upd_time','cVerifier as reviewer'],
  124. 'son_field' => [
  125. 'detail.ID as id',
  126. 'detail.cInvCode as materialCode',
  127. 'detail.iQuantity as planQty',
  128. ],
  129. 'limit' => 30,
  130. ],
  131. SyncTempRecord::type_five => [// 其他入 其他入库流程
  132. 'main' => 'RdRecord08',
  133. 'detail' => 'RdRecords08',
  134. 'main_key' => 'ID',
  135. 'key' => 'ID',
  136. 'whereRaw' => "(dDate >= '{$st}' and COALESCE(cMemo, '') <> '接口生成')",
  137. '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'],
  138. 'son_field' => [
  139. 'detail.ID as id',
  140. 'detail.cInvCode as materialCode',
  141. 'detail.iQuantity as planQty',
  142. ],
  143. 'limit' => 30,
  144. ],
  145. SyncTempRecord::type_six => [// 销售退货单 其他入库流程
  146. 'main' => 'DispatchList',
  147. 'detail' => 'DispatchLists',
  148. 'main_key' => 'DLID',
  149. 'key' => 'DLID',
  150. 'whereRaw' => "(dDate >= '{$st}' and bReturnFlag = 1)",
  151. 'main_field' => ['DLID as id','cDLCode as no', 'dDate as order_date','dcreatesystime as crt_time', 'dmodifysystime as upd_time','cVerifier as reviewer'],
  152. 'son_field' => [
  153. 'detail.DLID as id',
  154. 'detail.cInvCode as materialCode',
  155. 'detail.cWhCode as warehouseCode',
  156. 'detail.iQuantity as planQty',
  157. 'detail.cDefine23',
  158. 'detail.cBatch',
  159. 'detail.dMDate',
  160. 'detail.iMassDate',
  161. ],
  162. 'limit' => 30,
  163. ],
  164. SyncTempRecord::type_seven => [// 其他出 其他入库流程
  165. 'main' => 'RdRecord09',
  166. 'detail' => 'RdRecords09',
  167. 'main_key' => 'ID',
  168. 'key' => 'ID',
  169. 'whereRaw' => "(dDate >= '{$st}' and COALESCE(cMemo, '') <> '接口生成')",
  170. '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'],
  171. 'son_field' => [
  172. 'detail.ID as id',
  173. 'detail.cInvCode as materialCode',
  174. 'detail.iQuantity as planQty',
  175. ],
  176. 'limit' => 30,
  177. ],
  178. SyncTempRecord::type_eight => [
  179. //检验单
  180. //cvouchtype=QM03=>采购到货单 1 来料报检单
  181. //cvouchtype=QM04=>产品检验单 2
  182. //cvouchtype=QM14=>退货检验单 3
  183. 'main' => 'QMCHECKVOUCHER',
  184. 'detail' => '',
  185. 'main_key' => 'ID',
  186. 'key' => '',
  187. 'whereRaw' => "(DDATE >= '{$st}' and (CVOUCHTYPE = 'QM03' OR CVOUCHTYPE = 'QM04'))",
  188. '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','SOURCECODE as from_order', 'CINSPECTCODE as bj_order'],
  189. 'son_field' => [],
  190. 'limit' => 30,
  191. ],
  192. ];
  193. $time = time();
  194. foreach ($tasks as $name => $config) {
  195. // 用于记录本次在 U8 查到的所有单号,用来比对删除
  196. $currentU8Nos = [];
  197. $lastId = 0;
  198. $orderDateLimit = $dateMap[$name]; // 当前类型的起始日期限制
  199. while (true) {
  200. list($status, $items) = $service->getPendingBills($config, $lastId);
  201. if (!$status || empty($items)) break;
  202. $nos = collect($items)->pluck('no')->toArray();
  203. $currentU8Nos = array_merge($currentU8Nos, $nos); // 记录当前存在的单号
  204. //获取这 30 条的快照
  205. $snapshots = DB::table('sync_snapshot')
  206. ->where('type', $name)
  207. ->whereIn('u8_no', $nos)
  208. ->get()
  209. ->keyBy('u8_no');
  210. foreach ($items as $item) {
  211. if ($name == SyncTempRecord::type_eight){
  212. $type_v = 0;
  213. if($item['type'] == 'QM03'){
  214. $type_v = 1;
  215. }elseif ($item['type'] == 'QM04'){
  216. $type_v = 2;
  217. }elseif ($item['type'] == 'QM14'){
  218. $type_v = 3;
  219. }
  220. $item['type'] = $type_v;
  221. }
  222. $no = $item['no'];
  223. $u8Id = $item['id']; // 抓取主表 ID
  224. $u8Upd = $item['upd_time'] ?: $item['crt_time'];
  225. $snapshot = $snapshots->get($no);
  226. $type_2 = isset($item['type']) ? $item['type'] : 0;
  227. $bool = $item['reviewer'] ? true : false;
  228. if(! $bool) continue;// 没审核跳过
  229. $opType = null;
  230. if (!$snapshot) {
  231. $opType = SyncTempRecord::opt_zero;
  232. } elseif ($u8Upd > $snapshot->last_upd_time) {
  233. $opType = SyncTempRecord::opt_one;
  234. }
  235. if ($opType !== null) {
  236. // 写入任务流水
  237. $this->createSyncTask($name, $no, $u8Id, $item, $opType, $u8Upd, $time, $type_2);
  238. }
  239. }
  240. //分页最大id
  241. $lastId = collect($items)->max('id');
  242. }
  243. $currentU8NosLookup = array_flip($currentU8Nos);
  244. // 使用 chunk 配合日期过滤,只查快照中大于等于月结日期的单据
  245. DB::table('sync_snapshot')
  246. ->where('type', $name)
  247. ->where('order_date', '>=', $orderDateLimit) // 关键:缩小快照查询范围
  248. ->orderBy('u8_id')
  249. ->chunk(100, function ($snapshots) use ($currentU8NosLookup, $name, $time) {
  250. foreach ($snapshots as $oldSnapshot) {
  251. // 如果快照里的单号不在本次 U8 扫描结果里
  252. if (!isset($currentU8NosLookup[$oldSnapshot->u8_no])) {
  253. $this->createSyncTask($name, $oldSnapshot->u8_no, $oldSnapshot->u8_id, $oldSnapshot->payload, SyncTempRecord::opt_two, "", $time);
  254. }
  255. }
  256. });
  257. unset($currentU8Nos); // 释放内存
  258. }
  259. }
  260. private function createSyncTask($type, $no, $u8Id, $payload, $opType, $u8Upd = "", $time, $type_2 = 0) {
  261. $record = SyncTempRecord::create([
  262. 'type' => $type,
  263. 'type_2' => $type_2,
  264. 'u8_no' => $no,
  265. 'u8_id' => $u8Id,
  266. 'payload' => json_encode($payload),
  267. 'u8_upd' => $u8Upd,
  268. 'op_type' => $opType,
  269. 'crt_time'=> $time,
  270. ]);
  271. // 只分发 ID 给队列,Job 内部再根据此 ID 取 u8_id 和 payload
  272. ProcessWMSDataJob::dispatch(['id' => $record->id])->onQueue("sync_wms_order");
  273. }
  274. private function getYjData(){
  275. //结账
  276. //采购到货单 | 退货单 bflag_PU 采购
  277. //领料申请单|产成品入库单| bflag_ST 出和入
  278. //销售发货单 销售退货 bflag_SA 销售
  279. $result = DB::connection('u8_third_sqlserver')
  280. ->table('gl_mend')
  281. ->lock('WITH(NOLOCK)')
  282. ->selectRaw("
  283. MIN(CASE WHEN bflag_PU = 0 AND iyear >= 2023 THEN iYPeriod END) as min_pu,
  284. MIN(CASE WHEN bflag_ST = 0 THEN iYPeriod END) as min_st,
  285. MIN(CASE WHEN bflag_SA = 0 THEN iYPeriod END) as min_sa
  286. ")
  287. ->whereRaw("RIGHT(CAST(iYPeriod AS VARCHAR), 2) <> '00'")
  288. ->first();
  289. // 转换成数组方便取值
  290. $minPeriods = (array)$result;
  291. $formatDate = function($period) {
  292. if (!$period) return null; // 如果没有未结账月份,返回null
  293. $period = (string)$period; // 确保是字符串
  294. $year = substr($period, 0, 4);
  295. $month = substr($period, 4, 2);
  296. return "{$year}-{$month}-01";
  297. };
  298. // 2. 执行转换
  299. $formattedPeriods = [
  300. 'pu_date' => $formatDate($minPeriods['min_pu'] ?? null),
  301. 'st_date' => $formatDate($minPeriods['min_st'] ?? null),
  302. 'sa_date' => $formatDate($minPeriods['min_sa'] ?? null),
  303. ];
  304. return $formattedPeriods;
  305. }
  306. }