summaryrefslogtreecommitdiff
path: root/lib/sedp/sync-form copy.ts
blob: 2cb677b7baa433dadb67ffcd747324b2cbf50a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
// src/lib/cron/syncTagFormMappings.ts
import db from "@/db/db";
import { projects, tagTypes, tagClasses, tagTypeClassFormMappings, formMetas, forms, contractItems, items, contracts } from '@/db/schema';
import { eq, and, inArray, ilike } from 'drizzle-orm';
import { getSEDPToken } from "./sedp-token";

// 환경 변수
const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api';

// 인터페이스 정의
interface TagTypeClassFormMapping {
  projectId: number;
  tagTypeLabel: string;
  classLabel: string;
  formCode: string;
  formName: string;
  remark: string | null;
  ep: string;
  createdAt: Date;
  updatedAt: Date;
}

interface FormMeta {
  projectId: number;
  formCode: string;
  formName: string;
  columns: string; // JSON 문자열
  createdAt: Date;
  updatedAt: Date;
}

interface FormRecord {
  contractItemId: number;
  formCode: string;
  formName: string;
  eng: boolean;
  createdAt: Date;
  updatedAt: Date;
}
interface Register {
  PROJ_NO: string;
  TYPE_ID: string;
  EP_ID: string;
  DESC: string;
  REMARK: string | null;
  NEW_TAG_YN: boolean;
  ALL_TAG_YN: boolean;
  VND_YN: boolean;
  SEQ: number;
  CMPLX_YN: boolean;
  CMPL_SETT: any | null;
  MAP_ATT: any[];
  MAP_CLS_ID: string[];
  MAP_OPER: any | null;
  LNK_ATT: LinkAttribute[];
  JOIN_TABLS: any[];
  DELETED: boolean;
  CRTER_NO: string;
  CRTE_DTM: string;
  CHGER_NO: string | null;
  CHGE_DTM: string | null;
  _id: string;
}

interface LinkAttribute {
  ATT_ID: string;
  CPY_DESC: string;
  JOIN_KEY_ATT_ID: string | null;
  JOIN_VAL_ATT_ID: string | null;
  KEY_YN: boolean;
  EDIT_YN: boolean;
  PUB_YN: boolean;
  VND_YN: boolean;
  DEF_VAL: string | null;
  UOM_ID: string | null;
}

interface Attribute {
  PROJ_NO: string;
  ATT_ID: string;
  DESC: string;
  GROUP: string | null;
  REMARK: string | null;
  VAL_TYPE: string;
  IGN_LIST_VAL: boolean;
  CL_ID: string | null;
  UOM_ID: string | null;
  DEF_VAL: string | null;
  MIN_VAL: number;
  MAX_VAL: number;
  ESS_YN: boolean;
  SEQ: number;
  FORMAT: string | null;
  REG_EXPS: string | null;
  ATTRIBUTES: any[];
  DELETED: boolean;
  CRTER_NO: string;
  CRTE_DTM: string;
  CHGER_NO: string | null;
  CHGE_DTM: string | null;
  _id: string;
}

interface CodeList {
  PROJ_NO: string;
  CL_ID: string;
  DESC: string;
  REMARK: string | null;
  PRNT_CD_ID: string | null;
  REG_TYPE_ID: string | null;
  VAL_ATT_ID: string | null;
  VALUES: CodeValue[];
  LNK_ATT: any[];
  DELETED: boolean;
  CRTER_NO: string;
  CRTE_DTM: string;
  CHGER_NO: string | null;
  CHGE_DTM: string | null;
  _id: string;
}

interface CodeValue {
  PRNT_VALUE: string | null;
  VALUE: string;
  DESC: string;
  REMARK: string;
  USE_YN: boolean;
  SEQ: number;
  ATTRIBUTES: any[];
}

interface UOM {
  PROJ_NO: string;
  UOM_ID: string;
  DESC: string;
  SYMBOL: string;
  CONV_RATE: number;
  DELETED: boolean;
  CRTER_NO: string;
  CRTE_DTM: string;
  CHGER_NO: string | null;
  CHGE_DTM: string | null;
  _id: string;
}

interface Project {
  id: number;
  code: string;
  name: string;
  type?: string;
  createdAt?: Date;
  updatedAt?: Date;
}

interface SyncResult {
  project: string;
  success: boolean;
  count?: number;
  error?: string;
}

interface FormColumn {
  key: string;
  label: string;
  type: string;
  options?: string[];
  uom?: string;
  uomId?: string;
  shi?: Boolean;
}

// 아이템 코드 추출 함수
function extractItemCodes(remark: string | null): string[] {
  if (!remark) return [];
  
  // 검색용으로만 소문자로 변환
  const remarkLower = remark.toLowerCase();
  
  // 'vd_' 접두사 확인
  const hasVD_ = remarkLower.includes("vd_");
  
  if (!hasVD_) return [];
  
  let vdPart = "";
  
  // 'vd_'가 있으면 원본 문자열에서 추출 (소문자 버전이 아님)
  if (hasVD_) {
    const vdIndex = remarkLower.indexOf("vd_");
    vdPart = remark.substring(vdIndex + 3); // 원본 문자열에서 추출
  }
  
  if (!vdPart) return [];
  
  // 쉼표로 구분된 여러 itemCode 처리
  return vdPart.split(",").map(code => code.trim());
}

async function getDefaulTAttributes(): Promise<string[]> {
  try {
    const apiKey = await getSEDPToken();

    const response = await fetch(
      `${SEDP_API_BASE_URL}/Dictionary/GetByKey`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
        },
        body: JSON.stringify({
          Key: "DefaultAttributesToCompare",
        })
      }
    );

    if (!response.ok) {
      if (response.status === 404) {
        console.warn(`디폴트 속성 찾을 수 없음`);
        return [];
      }
      throw new Error(`코드 리스트 요청 실패: ${response.status} ${response.statusText}`);
    }

    // 안전하게 JSON 파싱
    try {
      const data = await response.json();
      // 데이터가 배열인지 확인하고 문자열 배열로 변환
      if (Array.isArray(data)) {
        return data as string[];
      } else {
        console.warn('응답이 배열 형식이 아닙니다');
        return [];
      }
    } catch (parseError) {
      console.error(`디폴트 속성 응답 파싱 실패:`, parseError);
      // 응답 내용 로깅
      try {
        const text = await response.clone().text();
        console.log(`응답 내용: ${text.substring(0, 200)}${text.length > 200 ? '...' : ''}`);
      } catch (textError) {
        console.error('응답 내용 로깅 실패:', textError);
      }
      return [];
    }
  } catch (error) {
    console.error(`디폴트 어트리뷰트 가져오기 실패:`, error);
    throw error;
  }
}

// 레지스터 데이터 가져오기
async function getRegisters(projectCode: string): Promise<Register[]> {
  try {
    // 토큰(API 키) 가져오기
    const apiKey = await getSEDPToken();

    const response = await fetch(
      `${SEDP_API_BASE_URL}/Register/Get`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify({
          ProjectNo: projectCode,
          ContainDeleted: false
        })
      }
    );

    if (!response.ok) {
      throw new Error(`레지스터 요청 실패: ${response.status} ${response.statusText}`);
    }

    // 안전하게 JSON 파싱
    let data;
    try {
      data = await response.json();
    } catch (parseError) {
      console.error(`프로젝트 ${projectCode}의 레지스터 응답 파싱 실패:`, parseError);
      // 응답 내용 로깅
      const text = await response.clone().text();
      console.log(`응답 내용: ${text.substring(0, 200)}${text.length > 200 ? '...' : ''}`);
      throw new Error(`레지스터 응답 파싱 실패: ${parseError instanceof Error ? parseError.message : String(parseError)}`);
    }

    // 결과를 배열로 변환 (단일 객체인 경우 배열로 래핑)
    let registers: Register[] = Array.isArray(data) ? data : [data];

    // MAP_CLS_ID가 비어있지 않고 REMARK가 vd, VD, vD, Vd 중 하나인 레지스터만 필터링
    registers = registers.filter(register => {
      // 삭제된 레지스터 제외
      if (register.DELETED) return false;

      // MAP_CLS_ID 배열이 존재하고 요소가 하나 이상 있는지 확인
      const hasValidMapClsId = Array.isArray(register.MAP_CLS_ID) && register.MAP_CLS_ID.length > 0;

      // REMARK가 'vd_' 또는 'vd' 포함 확인 (대소문자 구분 없이)
      const remarkLower = register.REMARK && register.REMARK.toLowerCase();
      const hasValidRemark = remarkLower && (remarkLower.includes('vd'));

      // 두 조건 모두 충족해야 함
      return hasValidMapClsId && hasValidRemark;
    });

    console.log(`프로젝트 ${projectCode}에서 ${registers.length}개의 유효한 레지스터를 가져왔습니다.`);
    return registers;
  } catch (error) {
    console.error(`프로젝트 ${projectCode}의 레지스터 가져오기 실패:`, error);
    throw error;
  }
}

// 프로젝트의 모든 속성을 가져와 맵으로 반환
async function getAttributes(projectCode: string): Promise<Map<string, Attribute>> {
  try {
    // 토큰(API 키) 가져오기
    const apiKey = await getSEDPToken();

    const response = await fetch(
      `${SEDP_API_BASE_URL}/Attributes/Get`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify({
          ProjectNo: projectCode,
          ContainDeleted: false
        })
      }
    );

    if (!response.ok) {
      throw new Error(`속성 요청 실패: ${response.status} ${response.statusText}`);
    }

    // 안전하게 JSON 파싱
    try {
      const data = await response.json();
      
      // 데이터가 배열인지 확인
      const attributes: Attribute[] = Array.isArray(data) ? data : [data];
      
      // ATT_ID로 효율적인 조회를 위한 맵 생성
      const attributeMap = new Map<string, Attribute>();
      for (const attribute of attributes) {
        if (!attribute.DELETED) {
          attributeMap.set(attribute.ATT_ID, attribute);
        }
      }
      
      console.log(`프로젝트 ${projectCode}에서 ${attributeMap.size}개의 속성을 가져왔습니다`);
      return attributeMap;
      
    } catch (parseError) {
      console.error(`프로젝트 ${projectCode}의 속성 응답 파싱 실패:`, parseError);
      // 응답 내용 로깅
      try {
        const text = await response.clone().text();
        console.log(`응답 내용: ${text.substring(0, 200)}${text.length > 200 ? '...' : ''}`);
      } catch (textError) {
        console.error('응답 내용 로깅 실패:', textError);
      }
      return new Map();
    }
  } catch (error) {
    console.error(`프로젝트 ${projectCode}의 속성 가져오기 실패:`, error);
    return new Map();
  }
}

// 특정 속성 가져오기 (하위 호환성을 위해 유지)
async function getAttributeById(projectCode: string, attributeId: string, register: string): Promise<Attribute | null> {
  try {
    // 토큰(API 키) 가져오기
    const apiKey = await getSEDPToken();

    const response = await fetch(
      `${SEDP_API_BASE_URL}/Attributes/GetByID`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify({
          ProjectNo: projectCode,
          ATT_ID: attributeId,
          ContainDeleted: false
        })
      }
    );

    if (!response.ok) {
      if (response.status === 404) {
        console.warn(`속성 ID ${attributeId}를 찾을 수 없음`);
        return null;
      }
      throw new Error(`속성 요청 실패: ${response.status} ${response.statusText}`);
    }

    // 안전하게 JSON 파싱
    try {
      const data = await response.json();
      return data;
    } catch (parseError) {
      console.error(`속성 ID ${attributeId} ${register} ${projectCode} 응답 파싱 실패:`, parseError);
      // 응답 내용 로깅
      try {
        const text = await response.clone().text();
        console.log(`응답 내용: ${text.substring(0, 200)}${text.length > 200 ? '...' : ''}`);
      } catch (textError) {
        console.error('응답 내용 로깅 실패:', textError);
      }
      return null;
    }
  } catch (error) {
    console.error(`속성 ID ${attributeId} 가져오기 실패:`, error);
    return null;
  }
}

// 프로젝트의 모든 코드 리스트를 가져와 맵으로 반환
async function getCodeLists(projectCode: string): Promise<Map<string, CodeList>> {
  try {
    // 토큰(API 키) 가져오기
    const apiKey = await getSEDPToken();

    const response = await fetch(
      `${SEDP_API_BASE_URL}/CodeList/Get`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify({
          ProjectNo: projectCode,
          ContainDeleted: false
        })
      }
    );

    if (!response.ok) {
      throw new Error(`코드 리스트 요청 실패: ${response.status} ${response.statusText}`);
    }

    // 안전하게 JSON 파싱
    try {
      const data = await response.json();
      
      // 데이터가 배열인지 확인
      const codeLists: CodeList[] = Array.isArray(data) ? data : [data];
      
      // CL_ID로 효율적인 조회를 위한 맵 생성
      const codeListMap = new Map<string, CodeList>();
      for (const codeList of codeLists) {
        if (!codeList.DELETED) {
          codeListMap.set(codeList.CL_ID, codeList);
        }
      }
      
      console.log(`프로젝트 ${projectCode}에서 ${codeListMap.size}개의 코드 리스트를 가져왔습니다`);
      return codeListMap;
      
    } catch (parseError) {
      console.error(`프로젝트 ${projectCode}의 코드 리스트 응답 파싱 실패:`, parseError);
      // 응답 내용 로깅
      try {
        const text = await response.clone().text();
        console.log(`응답 내용: ${text.substring(0, 200)}${text.length > 200 ? '...' : ''}`);
      } catch (textError) {
        console.error('응답 내용 로깅 실패:', textError);
      }
      return new Map();
    }
  } catch (error) {
    console.error(`프로젝트 ${projectCode}의 코드 리스트 가져오기 실패:`, error);
    return new Map();
  }
}

// 특정 코드 리스트 가져오기 (하위 호환성을 위해 유지)
async function getCodeListById(projectCode: string, codeListId: string): Promise<CodeList | null> {
  try {
    // 토큰(API 키) 가져오기
    const apiKey = await getSEDPToken();

    const response = await fetch(
      `${SEDP_API_BASE_URL}/CodeList/GetByID`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify({
          ProjectNo: projectCode,
          CL_ID: codeListId,
          ContainDeleted: false
        })
      }
    );

    if (!response.ok) {
      if (response.status === 404) {
        console.warn(`코드 리스트 ID ${codeListId}를 찾을 수 없음`);
        return null;
      }
      throw new Error(`코드 리스트 요청 실패: ${response.status} ${response.statusText}`);
    }

    // 안전하게 JSON 파싱
    try {
      const data = await response.json();
      return data;
    } catch (parseError) {
      console.error(`코드 리스트 ID ${codeListId} 응답 파싱 실패:`, parseError);
      // 응답 내용 로깅
      try {
        const text = await response.clone().text();
        console.log(`응답 내용: ${text.substring(0, 200)}${text.length > 200 ? '...' : ''}`);
      } catch (textError) {
        console.error('응답 내용 로깅 실패:', textError);
      }
      return null;
    }
  } catch (error) {
    console.error(`코드 리스트 ID ${codeListId} 가져오기 실패:`, error);
    return null;
  }
}

// 프로젝트의 모든 UOM을 가져와 맵으로 반환
async function getUOMs(projectCode: string): Promise<Map<string, UOM>> {
  try {
    // 토큰(API 키) 가져오기
    const apiKey = await getSEDPToken();

    const response = await fetch(
      `${SEDP_API_BASE_URL}/UOM/Get`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify({
          ProjectNo: projectCode,
          ContainDeleted: false
        })
      }
    );

    if (!response.ok) {
      throw new Error(`UOM 요청 실패: ${response.status} ${response.statusText}`);
    }

    // 안전하게 JSON 파싱
    try {
      const data = await response.json();
      
      // 데이터가 배열인지 확인
      const uoms: UOM[] = Array.isArray(data) ? data : [data];
      
      // UOM_ID로 효율적인 조회를 위한 맵 생성
      const uomMap = new Map<string, UOM>();
      for (const uom of uoms) {
        if (!uom.DELETED) {
          uomMap.set(uom.UOM_ID, uom);
        }
      }
      
      console.log(`프로젝트 ${projectCode}에서 ${uomMap.size}개의 UOM을 가져왔습니다`);
      return uomMap;
      
    } catch (parseError) {
      console.error(`프로젝트 ${projectCode}의 UOM 응답 파싱 실패:`, parseError);
      // 응답 내용 로깅
      try {
        const text = await response.clone().text();
        console.log(`응답 내용: ${text.substring(0, 200)}${text.length > 200 ? '...' : ''}`);
      } catch (textError) {
        console.error('응답 내용 로깅 실패:', textError);
      }
      return new Map();
    }
  } catch (error) {
    console.error(`프로젝트 ${projectCode}의 UOM 가져오기 실패:`, error);
    return new Map();
  }
}

// UOM 가져오기 (하위 호환성을 위해 유지)
async function getUomById(projectCode: string, uomId: string): Promise<UOM | null> {
  try {
    // 토큰(API 키) 가져오기
    const apiKey = await getSEDPToken();

    const response = await fetch(
      `${SEDP_API_BASE_URL}/UOM/GetByID`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'accept': '*/*',
          'ApiKey': apiKey,
          'ProjectNo': projectCode
        },
        body: JSON.stringify({
          UOMID: uomId, // API 명세서에 따라 UOMID 사용
          ProjectNo: projectCode,
          ContainDeleted: false
        })
      }
    );

    if (!response.ok) {
      if (response.status === 404) {
        console.warn(`UOM ID ${uomId}를 찾을 수 없음`);
        return null;
      }
      throw new Error(`UOM 요청 실패: ${response.status} ${response.statusText}`);
    }

    // 안전하게 JSON 파싱
    try {
      const data = await response.json();
      return data;
    } catch (parseError) {
      console.error(`UOM ID ${uomId} 응답 파싱 실패:`, parseError);
      // 응답 내용 로깅
      try {
        const text = await response.clone().text();
        console.log(`응답 내용: ${text.substring(0, 200)}${text.length > 200 ? '...' : ''}`);
      } catch (textError) {
        console.error('응답 내용 로깅 실패:', textError);
      }
      return null;
    }
  } catch (error) {
    console.error(`UOM ID ${uomId} 가져오기 실패:`, error);
    return null;
  }
}

// contractItemId 조회 함수
async function getContractItemsByItemCodes(itemCodes: string[], projectId: number): Promise<Map<string, number>> {
  try {
    if (!itemCodes.length) return new Map();

    // 먼저 itemCodes에 해당하는 item 레코드를 조회
    const itemRecords = await db.select({
      id: items.id,
      itemCode: items.itemCode
    })
    .from(items)
    .where(inArray(items.itemCode, itemCodes));

    if (!itemRecords.length) {
      console.log(`No items found for itemCodes: ${itemCodes.join(', ')}`);
      return new Map();
    }

    // item ID 목록 추출
    const itemIds = itemRecords.map(item => item.id);

    // contracts와 join하여 projectId로 필터링하면서 contractItems 조회
    const contractItemRecords = await db.select({
      id: contractItems.id,
      itemId: contractItems.itemId
    })
    .from(contractItems)
    .innerJoin(contracts, eq(contractItems.contractId, contracts.id))
    .where(
      and(
        inArray(contractItems.itemId, itemIds),
        eq(contracts.projectId, projectId)
      )
    );

    // itemCode와 contractItemId의 매핑 생성
    const itemCodeToContractItemId = new Map<string, number>();
    
    for (const item of itemRecords) {
      // itemCode가 null이 아닌 경우에만 처리
      if (item.itemCode) {
        const matchedContractItems = contractItemRecords.filter(ci => ci.itemId === item.id);
        if (matchedContractItems.length > 0) {
          // 일치하는 첫 번째 contractItem 사용
          itemCodeToContractItemId.set(item.itemCode, matchedContractItems[0].id);
        }
      }
    }

    return itemCodeToContractItemId;
  } catch (error) {
    console.error('ContractItems 조회 중 오류 발생:', error);
    return new Map();
  }
}

// 데이터베이스에 태그 타입 클래스 폼 매핑 및 폼 메타 저장
async function saveFormMappingsAndMetas(
  projectId: number,
  projectCode: string,
  registers: Register[]
): Promise<number> {
  try {
    // 프로젝트의 태그 타입과 클래스 가져오기
    const tagTypeRecords = await db.select()
      .from(tagTypes)
      .where(eq(tagTypes.projectId, projectId));

    const tagClassRecords = await db.select()
      .from(tagClasses)
      .where(eq(tagClasses.projectId, projectId));

    // 태그 타입과 클래스 매핑
    const tagTypeMap = new Map(tagTypeRecords.map(type => [type.code, type]));
    const tagClassMap = new Map(tagClassRecords.map(cls => [cls.code, cls]));

    // 모든 속성, 코드 리스트, UOM을 한 번에 가져와 반복 API 호출 방지
    const attributeMap = await getAttributes(projectCode);
    const codeListMap = await getCodeLists(projectCode);
    const uomMap = await getUOMs(projectCode);
    
    // 기본 속성 가져오기
    const defaultAttributes = await getDefaulTAttributes();

    // 모든 register에서 itemCode를 추출하여 한 번에 조회
    const allItemCodes: string[] = [];
    registers.forEach(register => {
      if (register.REMARK) {
        const itemCodes = extractItemCodes(register.REMARK);
        allItemCodes.push(...itemCodes);
      }
    });

    // 중복 제거
    const uniqueItemCodes = [...new Set(allItemCodes)];
    
    // 모든 itemCode에 대한 contractItemId 조회
    const itemCodeToContractItemId = await getContractItemsByItemCodes(uniqueItemCodes , projectId);

    console.log(`${uniqueItemCodes.length}개의 고유 itemCode 중 ${itemCodeToContractItemId.size}개의 contractItem을 찾았습니다`);

    // 저장할 데이터 준비
    const mappingsToSave: TagTypeClassFormMapping[] = [];
    const formMetasToSave: FormMeta[] = [];
    const formsToSave: FormRecord[] = [];

    // 폼이 있는 contractItemId 트래킹
    const contractItemIdsWithForms = new Set<number>();

    // 각 register 처리
    for (const register of registers) {
      // 삭제된 register 건너뛰기
      if (register.DELETED) continue;

      // REMARK에서 itemCodes 추출


      // 폼 메타용 columns 구성
      const columns: FormColumn[] = [];

      for (const linkAtt of register.LNK_ATT) {
        let attribute = null;

        // 기본 속성인지 확인
        if (defaultAttributes && defaultAttributes.includes(linkAtt.ATT_ID)) {
          // 기본 속성에 대한 기본 attribute 객체 생성
          attribute = {
            DESC: linkAtt.ATT_ID,
            VAL_TYPE: 'STRING'
          };
        } else {
          // 맵에서 속성 조회
          attribute = attributeMap.get(linkAtt.ATT_ID);

          // 속성을 찾지 못한 경우 다음으로 넘어감
          if (!attribute) continue;
        }

        // 컬럼 정보 생성
        const column: FormColumn = {
          key: linkAtt.ATT_ID,
          label: attribute.DESC,
          type: (attribute.VAL_TYPE === 'LIST' || attribute.VAL_TYPE === 'DYNAMICLIST')
            ? 'LIST'
            : (attribute.VAL_TYPE || 'STRING'),
          shi: attribute.REMARK?.toLocaleLowerCase() === "shi"
        };

        // 리스트 타입에 대한 옵션 추가 (기본 속성이 아닌 경우)
        if (!defaultAttributes.includes(linkAtt.ATT_ID) &&
            (attribute.VAL_TYPE === 'LIST' || attribute.VAL_TYPE === 'DYNAMICLIST') &&
            attribute.CL_ID) {
          
          // 맵에서 코드 리스트 조회
          const codeList = codeListMap.get(attribute.CL_ID);

          if (codeList && codeList.VALUES) {
            const options = [...new Set(
              codeList.VALUES
                .filter(value => value.USE_YN)
                .map(value => value.VALUE)
            )];

            if (options.length > 0) {
              column.options = options;
            }
          }
        }

        // UOM 정보 추가
        if (linkAtt.UOM_ID) {
          const uom = uomMap.get(linkAtt.UOM_ID);

          if (uom) {
            column.uom = uom.SYMBOL;
            column.uomId = uom.UOM_ID;
          }
        }

        columns.push(column);
      }

      // 컬럼이 없으면 건너뛰기
      if (columns.length === 0) {
        console.log(`폼 ${register.TYPE_ID} (${register.DESC})에 컬럼이 없어 건너뜁니다`);
        continue;
      }

      // 폼 메타 데이터 준비
      formMetasToSave.push({
        projectId,
        formCode: register.TYPE_ID,
        formName: register.DESC,
        columns: JSON.stringify(columns),
        createdAt: new Date(),
        updatedAt: new Date()
      });

      // 클래스 매핑 처리
      for (const classId of register.MAP_CLS_ID) {
        const tagClass = tagClassMap.get(classId);

        if (!tagClass) {
          console.warn(`프로젝트 ID ${projectId}에서 클래스 ID ${classId}를 찾을 수 없습니다`);
          continue;
        }

        const tagTypeCode = tagClass.tagTypeCode;
        const tagType = tagTypeMap.get(tagTypeCode);

        if (!tagType) {
          console.warn(`프로젝트 ID ${projectId}에서 태그 타입 ${tagTypeCode}를 찾을 수 없습니다`);
          continue;
        }

        // 매핑 정보 저장
        mappingsToSave.push({
          projectId,
          tagTypeLabel: tagType.description,
          classLabel: tagClass.label,
          formCode: register.TYPE_ID,
          formName: register.DESC,
          remark: register.REMARK,
          ep: register.EP_ID,
          createdAt: new Date(),
          updatedAt: new Date()
        });
      }

      const itemCodes = extractItemCodes(register.REMARK || '');
      if (!itemCodes.length) {
        console.log(`Register ${register.TYPE_ID} (${register.DESC})의 REMARK에 유효한 itemCode가 없습니다`);
        continue;
      }
      // 폼 레코드 준비
      for (const itemCode of itemCodes) {
        const contractItemId = itemCodeToContractItemId.get(itemCode);
        
        if (!contractItemId) {
          console.warn(`itemCode: ${itemCode}에 대한 contractItemId를 찾을 수 없습니다`);
          continue;
        }

        // 폼이 있는 contractItemId 추적
        contractItemIdsWithForms.add(contractItemId);

        formsToSave.push({
          contractItemId,
          formCode: register.TYPE_ID,
          formName: register.DESC,
          eng: true,
          createdAt: new Date(),
          updatedAt: new Date()
        });
      }
    }

    // 트랜잭션으로 모든 작업 처리
    let totalSaved = 0;

    await db.transaction(async (tx) => {
      // 기존 데이터 삭제
      await tx.delete(tagTypeClassFormMappings).where(eq(tagTypeClassFormMappings.projectId, projectId));
      await tx.delete(formMetas).where(eq(formMetas.projectId, projectId));

      // 해당 contractItemId에 대한 기존 폼 삭제
      if (contractItemIdsWithForms.size > 0) {
        await tx.delete(forms).where(inArray(forms.contractItemId, [...contractItemIdsWithForms]));
      }

      // 매핑 저장
      if (mappingsToSave.length > 0) {
        await tx.insert(tagTypeClassFormMappings).values(mappingsToSave);
        totalSaved += mappingsToSave.length;
        console.log(`프로젝트 ID ${projectId}에 대해 ${mappingsToSave.length}개의 태그 타입-클래스-폼 매핑을 저장했습니다`);
      }

      // 폼 메타 저장
      if (formMetasToSave.length > 0) {
        await tx.insert(formMetas).values(formMetasToSave);
        totalSaved += formMetasToSave.length;
        console.log(`프로젝트 ID ${projectId}에 대해 ${formMetasToSave.length}개의 폼 메타 레코드를 저장했습니다`);
      }

      // 폼 레코드 저장
      if (formsToSave.length > 0) {
        await tx.insert(forms).values(formsToSave);
        totalSaved += formsToSave.length;
        console.log(`프로젝트 ID ${projectId}에 대해 ${formsToSave.length}개의 폼 레코드를 저장했습니다`);
      }
    });

    return totalSaved;
  } catch (error) {
    console.error(`폼 매핑 및 메타 저장 실패 (프로젝트 ID: ${projectId}):`, error);
    throw error;
  }
}

// 메인 동기화 함수
export async function syncTagFormMappings() {
  try {
    console.log('태그 폼 매핑 동기화 시작:', new Date().toISOString());

    // 모든 프로젝트 가져오기
    const allProjects = await db.select().from(projects);

    // 각 프로젝트에 대해 폼 매핑 동기화
    const results = await Promise.allSettled(
      allProjects.map(async (project: Project) => {
        try {
          // 레지스터 데이터 가져오기
          const registers = await getRegisters(project.code);

          // 데이터베이스에 저장
          const count = await saveFormMappingsAndMetas(project.id, project.code, registers);
          return {
            project: project.code,
            success: true,
            count
          } as SyncResult;
        } catch (error) {
          console.error(`프로젝트 ${project.code} 폼 매핑 동기화 실패:`, error);
          return {
            project: project.code,
            success: false,
            error: error instanceof Error ? error.message : String(error)
          } as SyncResult;
        }
      })
    );

    // 결과 처리를 위한 배열 준비
    const successfulResults: SyncResult[] = [];
    const failedResults: SyncResult[] = [];

    // 결과 분류
    results.forEach((result) => {
      if (result.status === 'fulfilled') {
        if (result.value.success) {
          successfulResults.push(result.value);
        } else {
          failedResults.push(result.value);
        }
      } else {
        // 거부된 프로미스는 실패로 간주
        failedResults.push({
          project: 'unknown',
          success: false,
          error: result.reason?.toString() || 'Unknown error'
        });
      }
    });

    const successCount = successfulResults.length;
    const failCount = failedResults.length;

    // 이제 안전하게 count 속성에 접근 가능
    const totalItems = successfulResults.reduce((sum, result) =>
      sum + (result.count || 0), 0
    );

    console.log(`태그 폼 매핑 동기화 완료: ${successCount}개 프로젝트 성공 (총 ${totalItems}개 항목), ${failCount}개 프로젝트 실패`);

    return {
      success: successCount,
      failed: failCount,
      items: totalItems,
      timestamp: new Date().toISOString()
    };
  } catch (error) {
    console.error('태그 폼 매핑 동기화 중 오류 발생:', error);
    throw error;
  }
}