U8Settle.php 14 KB

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