summaryrefslogtreecommitdiff
path: root/lib/tech-vendor-possible-items/repository.ts
blob: 4d876643ab5793296e2d9d1605b58d07a4e853d9 (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
import { eq, desc, count, SQL, sql } from "drizzle-orm";
import { 
  techVendors, 
  techVendorPossibleItems
} from "@/db/schema/techVendors";
import { itemShipbuilding, itemOffshoreTop, itemOffshoreHull } from "@/db/schema/items";
import type { PgTransaction } from "drizzle-orm/pg-core";

/**
 * 새로운 스키마에 맞는 기술영업 벤더 가능 아이템 목록 조회 (조인 포함)
 * techVendorPossibleItems는 shipbuildingItemId, offshoreTopItemId, offshoreHullItemId 중 하나만 가짐
 */
export async function selectTechVendorPossibleItemsWithJoin(
  tx: PgTransaction<any, any, any>,
  where: SQL | undefined,
  orderBy: SQL[],
  offset: number,
  limit: number
) {
  return await tx
    .select({
      id: techVendorPossibleItems.id,
      vendorId: techVendorPossibleItems.vendorId,
      shipbuildingItemId: techVendorPossibleItems.shipbuildingItemId,
      offshoreTopItemId: techVendorPossibleItems.offshoreTopItemId,
      offshoreHullItemId: techVendorPossibleItems.offshoreHullItemId,
      createdAt: techVendorPossibleItems.createdAt,
      updatedAt: techVendorPossibleItems.updatedAt,
      // 벤더 정보
      vendorCode: techVendors.vendorCode,
      vendorName: techVendors.vendorName,
      vendorEmail: techVendors.email,
      vendorStatus: techVendors.status,
      techVendorType: techVendors.techVendorType,
      // 조선 아이템 정보 (shipbuildingItemId가 있을 때만)
      shipItemCode: itemShipbuilding.itemCode,
      shipWorkType: itemShipbuilding.workType,
      shipItemList: itemShipbuilding.itemList,
      shipTypes: itemShipbuilding.shipTypes,
      // 해양 TOP 아이템 정보 (offshoreTopItemId가 있을 때만)
      topItemCode: itemOffshoreTop.itemCode,
      topWorkType: itemOffshoreTop.workType,
      topItemList: itemOffshoreTop.itemList,
      topSubItemList: itemOffshoreTop.subItemList,
      // 해양 HULL 아이템 정보 (offshoreHullItemId가 있을 때만)
      hullItemCode: itemOffshoreHull.itemCode,
      hullWorkType: itemOffshoreHull.workType,
      hullItemList: itemOffshoreHull.itemList,
      hullSubItemList: itemOffshoreHull.subItemList,
    })
    .from(techVendorPossibleItems)
    .innerJoin(techVendors, eq(techVendorPossibleItems.vendorId, techVendors.id))
    .leftJoin(itemShipbuilding, eq(techVendorPossibleItems.shipbuildingItemId, itemShipbuilding.id))
    .leftJoin(itemOffshoreTop, eq(techVendorPossibleItems.offshoreTopItemId, itemOffshoreTop.id))
    .leftJoin(itemOffshoreHull, eq(techVendorPossibleItems.offshoreHullItemId, itemOffshoreHull.id))
    .where(where)
    .orderBy(...(orderBy || [desc(techVendorPossibleItems.createdAt)]))
    .limit(limit)
    .offset(offset);
}

/**
 * 기술영업 벤더 가능 아이템 총 개수 조회 (조인 포함)
 */
export async function countTechVendorPossibleItemsWithJoin(
  tx: PgTransaction<any, any, any>, 
  where?: SQL | undefined
) {
  const [result] = await tx
    .select({ count: count() })
    .from(techVendorPossibleItems)
    .innerJoin(techVendors, eq(techVendorPossibleItems.vendorId, techVendors.id))
    .leftJoin(itemShipbuilding, eq(techVendorPossibleItems.shipbuildingItemId, itemShipbuilding.id))
    .leftJoin(itemOffshoreTop, eq(techVendorPossibleItems.offshoreTopItemId, itemOffshoreTop.id))
    .leftJoin(itemOffshoreHull, eq(techVendorPossibleItems.offshoreHullItemId, itemOffshoreHull.id))
    .where(where);
  
  return result.count;
}

/**
 * 공종별 통계 조회 (새로운 스키마 적용)
 */
export async function getWorkTypeStats(
  tx: PgTransaction<any, any, any>,
  where?: SQL | undefined
) {
  // 각 아이템 타입별로 별도 쿼리 실행 후 통합
  const shipStats = await tx
    .select({
      workType: itemShipbuilding.workType,
      count: count(),
      vendorCount: sql<number>`COUNT(DISTINCT ${techVendorPossibleItems.vendorId})`.as('vendorCount'),
      itemCount: sql<number>`COUNT(DISTINCT ${itemShipbuilding.itemCode})`.as('itemCount'),
    })
    .from(techVendorPossibleItems)
    .innerJoin(techVendors, eq(techVendorPossibleItems.vendorId, techVendors.id))
    .innerJoin(itemShipbuilding, eq(techVendorPossibleItems.shipbuildingItemId, itemShipbuilding.id))
    .where(where)
    .groupBy(itemShipbuilding.workType)
    .orderBy(desc(count()));

  const topStats = await tx
    .select({
      workType: itemOffshoreTop.workType,
      count: count(),
      vendorCount: sql<number>`COUNT(DISTINCT ${techVendorPossibleItems.vendorId})`.as('vendorCount'),
      itemCount: sql<number>`COUNT(DISTINCT ${itemOffshoreTop.itemCode})`.as('itemCount'),
    })
    .from(techVendorPossibleItems)
    .innerJoin(techVendors, eq(techVendorPossibleItems.vendorId, techVendors.id))
    .innerJoin(itemOffshoreTop, eq(techVendorPossibleItems.offshoreTopItemId, itemOffshoreTop.id))
    .where(where)
    .groupBy(itemOffshoreTop.workType)
    .orderBy(desc(count()));

  const hullStats = await tx
    .select({
      workType: itemOffshoreHull.workType,
      count: count(),
      vendorCount: sql<number>`COUNT(DISTINCT ${techVendorPossibleItems.vendorId})`.as('vendorCount'),
      itemCount: sql<number>`COUNT(DISTINCT ${itemOffshoreHull.itemCode})`.as('itemCount'),
    })
    .from(techVendorPossibleItems)
    .innerJoin(techVendors, eq(techVendorPossibleItems.vendorId, techVendors.id))
    .innerJoin(itemOffshoreHull, eq(techVendorPossibleItems.offshoreHullItemId, itemOffshoreHull.id))
    .where(where)
    .groupBy(itemOffshoreHull.workType)
    .orderBy(desc(count()));

  // 결과 통합
  return [...shipStats, ...topStats, ...hullStats];
}

/**
 * 선종별 통계 조회 (조선 아이템만 해당)
 */
export async function getShipTypeStats(
  tx: PgTransaction<any, any, any>,
  where?: SQL | undefined
) {
  return await tx
    .select({
      shipTypes: itemShipbuilding.shipTypes,
      count: count(),
      vendorCount: sql<number>`COUNT(DISTINCT ${techVendorPossibleItems.vendorId})`.as('vendorCount'),
      itemCount: sql<number>`COUNT(DISTINCT ${itemShipbuilding.itemCode})`.as('itemCount'),
    })
    .from(techVendorPossibleItems)
    .innerJoin(techVendors, eq(techVendorPossibleItems.vendorId, techVendors.id))
    .innerJoin(itemShipbuilding, eq(techVendorPossibleItems.shipbuildingItemId, itemShipbuilding.id))
    .where(where)
    .groupBy(itemShipbuilding.shipTypes)
    .orderBy(desc(count()));
}

/**
 * 벤더별 통계 조회
 */
export async function getVendorStats(
  tx: PgTransaction<any, any, any>,
  where?: SQL | undefined
) {
  return await tx
    .select({
      vendorId: techVendorPossibleItems.vendorId,
      vendorCode: techVendors.vendorCode,
      vendorName: techVendors.vendorName,
      vendorEmail: techVendors.email,
      vendorStatus: techVendors.status,
      itemCount: count(),
      latestUpdate: sql<Date>`MAX(${techVendorPossibleItems.updatedAt})`.as('latestUpdate'),
    })
    .from(techVendorPossibleItems)
    .innerJoin(techVendors, eq(techVendorPossibleItems.vendorId, techVendors.id))
    .leftJoin(itemShipbuilding, eq(techVendorPossibleItems.shipbuildingItemId, itemShipbuilding.id))
    .leftJoin(itemOffshoreTop, eq(techVendorPossibleItems.offshoreTopItemId, itemOffshoreTop.id))
    .leftJoin(itemOffshoreHull, eq(techVendorPossibleItems.offshoreHullItemId, itemOffshoreHull.id))
    .where(where)
    .groupBy(
      techVendorPossibleItems.vendorId,
      techVendors.vendorCode,
      techVendors.vendorName,
      techVendors.email,
      techVendors.status
    )
    .orderBy(desc(count()));
}

/**
 * 아이템 타입별 통계 조회 (조선, 해양TOP, 해양HULL)
 */
export async function getItemTypeStats(
  tx: PgTransaction<any, any, any>,
  where?: SQL | undefined
) {
  const [shipCount] = await tx
    .select({ count: count() })
    .from(techVendorPossibleItems)
    .innerJoin(techVendors, eq(techVendorPossibleItems.vendorId, techVendors.id))
    .innerJoin(itemShipbuilding, eq(techVendorPossibleItems.shipbuildingItemId, itemShipbuilding.id))
    .where(where);

  const [topCount] = await tx
    .select({ count: count() })
    .from(techVendorPossibleItems)
    .innerJoin(techVendors, eq(techVendorPossibleItems.vendorId, techVendors.id))
    .innerJoin(itemOffshoreTop, eq(techVendorPossibleItems.offshoreTopItemId, itemOffshoreTop.id))
    .where(where);

  const [hullCount] = await tx
    .select({ count: count() })
    .from(techVendorPossibleItems)
    .innerJoin(techVendors, eq(techVendorPossibleItems.vendorId, techVendors.id))
    .innerJoin(itemOffshoreHull, eq(techVendorPossibleItems.offshoreHullItemId, itemOffshoreHull.id))
    .where(where);

  return [
    { itemType: "조선", count: shipCount.count },
    { itemType: "해양TOP", count: topCount.count },
    { itemType: "해양HULL", count: hullCount.count },
  ];
}