"use server"; import { convertObjToURLSearchParams } from "@/app/utils/commonUtil"; import { serverFetchJson } from "@/app/utils/fetchUtil"; import { BASE_API_URL } from "@/config/api"; import { cache } from "react"; import { DetailedProdScheduleLineBomMaterialResult, DetailedProdScheduleLineResult, ScheduleType } from "."; import { revalidateTag } from "next/cache"; export interface SearchProdSchedule { scheduleAt?: string; schedulePeriod?: string; schedulePeriodTo?: string; produceAt?: string; totalEstProdCount?: number; types?: ScheduleType[]; pageSize?: number; pageNum?: number; } export interface ProdScheduleResult { id: number; scheduleAt: number[]; schedulePeriod?: number[]; schedulePeriodTo?: number[]; produceAt: string; totalEstProdCount: number; totalFGType: number; type: string; } export interface ProdScheduleResultByPage { total: number; records: ProdScheduleResult[]; } export interface ReleaseProdScheduleInputs { id: number; demandQty: number; } export interface ReleaseProdScheduleResponse { id: number; code: string; entity: { prodScheduleLines: DetailedProdScheduleLineResult[]; }; message: string; } export interface SaveProdScheduleResponse { id: number; code: string; entity: { bomMaterials: DetailedProdScheduleLineBomMaterialResult[] }; message: string; } export const fetchProdSchedules = cache( async (data: SearchProdSchedule | null) => { const params = convertObjToURLSearchParams(data); // console.log(params) return serverFetchJson( `${BASE_API_URL}/productionSchedule/getRecordByPage?${params}`, { method: "GET", headers: { "Content-Type": "application/json" }, next: { tags: ["prodSchedules"], }, }, ); }, ); export const fetchRoughProdSchedules = cache( async (data: SearchProdSchedule | null) => { const params = convertObjToURLSearchParams(data); // console.log(params) return serverFetchJson( `${BASE_API_URL}/productionSchedule/getRecordByPage/rough?${params}`, { method: "GET", headers: { "Content-Type": "application/json" }, next: { tags: ["roughProdSchedules"], }, }, ); }, ); export const fetchDetailedProdSchedules = cache( async (data: SearchProdSchedule | null) => { const params = convertObjToURLSearchParams(data); // console.log(params) return serverFetchJson( `${BASE_API_URL}/productionSchedule/getRecordByPage/detailed?${params}`, { method: "GET", headers: { "Content-Type": "application/json" }, next: { tags: ["detailedProdSchedules"], }, }, ); }, ); export const testRoughSchedule = cache(async () => { return serverFetchJson( `${BASE_API_URL}/productionSchedule/testRoughSchedule`, { method: "GET", headers: { "Content-Type": "application/json" }, next: { tags: ["roughProdSchedules"], }, }, ); }); export const testDetailedSchedule = cache(async (date?: string) => { const queryStr = convertObjToURLSearchParams({genDate: date}) return serverFetchJson( `${BASE_API_URL}/productionSchedule/testDetailedSchedule?${queryStr}`, { method: "GET", headers: { "Content-Type": "application/json" }, next: { tags: ["detailedProdSchedules"], }, }, ); }); export const releaseProdScheduleLine = cache(async (data: ReleaseProdScheduleInputs) => { const response = serverFetchJson( `${BASE_API_URL}/productionSchedule/detail/detailed/releaseLine`, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, } ); revalidateTag("detailedProdSchedules"); revalidateTag("prodSchedule"); return response; }) export const saveProdScheduleLine = cache(async (data: ReleaseProdScheduleInputs) => { const response = serverFetchJson( `${BASE_API_URL}/productionSchedule/detail/detailed/save`, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, } ); revalidateTag("detailedProdSchedules"); revalidateTag("prodSchedule"); return response; })