FPSMS-frontend
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 

247 строки
6.3 KiB

  1. "use server";
  2. import { convertObjToURLSearchParams } from "@/app/utils/commonUtil";
  3. import { serverFetchJson } from "@/app/utils/fetchUtil";
  4. import { BASE_API_URL } from "@/config/api";
  5. import { cache } from "react";
  6. import { DetailedProdScheduleLineBomMaterialResult, DetailedProdScheduleLineResult, ScheduleType } from ".";
  7. import { revalidateTag } from "next/cache";
  8. export interface SearchProdSchedule {
  9. scheduleAt?: string;
  10. schedulePeriod?: string;
  11. schedulePeriodTo?: string;
  12. produceAt?: string;
  13. totalEstProdCount?: number;
  14. types?: ScheduleType[];
  15. pageSize?: number;
  16. pageNum?: number;
  17. }
  18. export interface ProdScheduleResult {
  19. id: number;
  20. scheduleAt: number[];
  21. schedulePeriod?: number[];
  22. schedulePeriodTo?: number[];
  23. produceAt: number[];
  24. totalEstProdCount: number;
  25. totalFGType: number;
  26. type: ScheduleType;
  27. }
  28. export interface ProdScheduleResultByPage {
  29. total: number;
  30. records: ProdScheduleResult[];
  31. }
  32. export interface ReleaseProdScheduleInputs {
  33. id: number;
  34. demandQty: number;
  35. }
  36. export interface ReleaseProdScheduleReq {
  37. id: number;
  38. }
  39. export interface print6FilesReq {
  40. itemCode: 'string',
  41. lotNo: 'string',
  42. expiryDate: 'string',
  43. productName: 'string'
  44. }
  45. export interface ReleaseProdScheduleResponse {
  46. id: number;
  47. code: string;
  48. entity: {
  49. prodScheduleLines: DetailedProdScheduleLineResult[];
  50. };
  51. message: string;
  52. }
  53. export interface ReleaseProdScheduleRsp {
  54. id: number;
  55. code: string;
  56. message: string;
  57. }
  58. export interface SaveProdScheduleResponse {
  59. id: number;
  60. code: string;
  61. entity: {
  62. bomMaterials: DetailedProdScheduleLineBomMaterialResult[]
  63. };
  64. message: string;
  65. }
  66. export const fetchProdSchedules = cache(
  67. async (data: SearchProdSchedule | null) => {
  68. const params = convertObjToURLSearchParams<SearchProdSchedule>(data);
  69. // console.log(params)
  70. return serverFetchJson<ProdScheduleResultByPage>(
  71. `${BASE_API_URL}/productionSchedule/getRecordByPage?${params}`,
  72. {
  73. method: "GET",
  74. headers: { "Content-Type": "application/json" },
  75. next: {
  76. tags: ["prodSchedules"],
  77. },
  78. },
  79. );
  80. },
  81. );
  82. export const fetchRoughProdSchedules = cache(
  83. async (data: SearchProdSchedule | null) => {
  84. const params = convertObjToURLSearchParams<SearchProdSchedule>(data);
  85. // console.log(params)
  86. return serverFetchJson<ProdScheduleResultByPage>(
  87. `${BASE_API_URL}/productionSchedule/getRecordByPage/rough?${params}`,
  88. {
  89. method: "GET",
  90. headers: { "Content-Type": "application/json" },
  91. next: {
  92. tags: ["roughProdSchedules"],
  93. },
  94. },
  95. );
  96. },
  97. );
  98. export const fetchDetailedProdSchedules = cache(
  99. async (data: SearchProdSchedule | null) => {
  100. const params = convertObjToURLSearchParams<SearchProdSchedule>(data);
  101. // console.log(params)
  102. return serverFetchJson<ProdScheduleResultByPage>(
  103. `${BASE_API_URL}/productionSchedule/getRecordByPage/detailed?${params}`,
  104. {
  105. method: "GET",
  106. headers: { "Content-Type": "application/json" },
  107. next: {
  108. tags: ["detailedProdSchedules"],
  109. },
  110. },
  111. );
  112. },
  113. );
  114. export const testRoughSchedule = cache(async () => {
  115. return serverFetchJson(
  116. `${BASE_API_URL}/productionSchedule/testRoughSchedule`,
  117. {
  118. method: "GET",
  119. headers: { "Content-Type": "application/json" },
  120. next: {
  121. tags: ["roughProdSchedules"],
  122. },
  123. },
  124. );
  125. });
  126. export const testDetailedSchedule = cache(async (date?: string) => {
  127. const queryStr = convertObjToURLSearchParams({genDate: date})
  128. return serverFetchJson(
  129. `${BASE_API_URL}/productionSchedule/testDetailedSchedule?${queryStr}`,
  130. {
  131. method: "GET",
  132. headers: { "Content-Type": "application/json" },
  133. next: {
  134. tags: ["detailedProdSchedules"],
  135. },
  136. },
  137. );
  138. });
  139. export const getFile6 = cache(async (
  140. token: string | "",
  141. data: print6FilesReq
  142. ) => {
  143. const queryStr = convertObjToURLSearchParams(data)
  144. return serverFetchJson(
  145. `${BASE_API_URL}/plastic/get-printer6?${queryStr}`,
  146. {
  147. method: "GET",
  148. headers: {
  149. "Content-Type": "application/json",
  150. "Authorization": `Bearer ${token}`
  151. },
  152. },
  153. );
  154. });
  155. export const releaseProdScheduleLine = cache(async (data: ReleaseProdScheduleInputs) => {
  156. const response = serverFetchJson<ReleaseProdScheduleResponse>(
  157. `${BASE_API_URL}/productionSchedule/detail/detailed/releaseLine`,
  158. {
  159. method: "POST",
  160. body: JSON.stringify(data),
  161. headers: { "Content-Type": "application/json" },
  162. }
  163. );
  164. revalidateTag("detailedProdSchedules");
  165. revalidateTag("prodSchedule");
  166. return response;
  167. })
  168. export const releaseProdSchedule = cache(async (data: ReleaseProdScheduleReq) => {
  169. const response = serverFetchJson<ReleaseProdScheduleRsp>(
  170. `${BASE_API_URL}/productionSchedule/detail/detailed/release`,
  171. {
  172. method: "POST",
  173. body: JSON.stringify(data),
  174. headers: { "Content-Type": "application/json" },
  175. }
  176. );
  177. //revalidateTag("detailedProdSchedules");
  178. //revalidateTag("prodSchedule");
  179. return response;
  180. })
  181. export const exportProdSchedule = async (
  182. token: string | null,
  183. inputs: any,
  184. prodHeaders: string[],
  185. matHeaders: string[]
  186. ) => {
  187. if (!token) throw new Error("No access token found");
  188. const response = await fetch(`${BASE_API_URL}/productionSchedule/export-prod-schedule`, {
  189. method: "POST",
  190. headers: {
  191. "Content-Type": "application/json", // Critical for @RequestBody
  192. "Accept": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  193. "Authorization": `Bearer ${token}`
  194. },
  195. // Send everything in one object
  196. body: JSON.stringify({
  197. ...inputs,
  198. prodHeaders,
  199. matHeaders
  200. })
  201. });
  202. if (!response.ok) throw new Error(`Backend error: ${response.status}`);
  203. const arrayBuffer = await response.arrayBuffer();
  204. return Buffer.from(arrayBuffer).toString('base64');
  205. };
  206. export const saveProdScheduleLine = cache(async (data: ReleaseProdScheduleInputs) => {
  207. const response = serverFetchJson<SaveProdScheduleResponse>(
  208. `${BASE_API_URL}/productionSchedule/detail/detailed/save`,
  209. {
  210. method: "POST",
  211. body: JSON.stringify(data),
  212. headers: { "Content-Type": "application/json" },
  213. }
  214. );
  215. revalidateTag("detailedProdSchedules");
  216. revalidateTag("prodSchedule");
  217. return response;
  218. })