useTable.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import { Query } from "@/api/webApi";
  2. import { Ref } from "vue";
  3. import { CommonData, CommonTableColumn, CommonValue } from "~/common";
  4. const idGetter = (name: string) => DATACONTENT_ID[name + "DataQuery"];
  5. const tableColumnsMap: {
  6. [tableName: string]: {
  7. columnLabel: string;
  8. columnName: string;
  9. [x: string]: any;
  10. }[];
  11. } = {
  12. flight: [
  13. {
  14. columnLabel: "航司二字码",
  15. columnName: "IATACode",
  16. width: 80,
  17. },
  18. {
  19. columnLabel: "航班号",
  20. columnName: "flightNO",
  21. width: 80,
  22. },
  23. {
  24. columnLabel: "机型",
  25. columnName: "planeType",
  26. },
  27. {
  28. columnLabel: "起飞机场",
  29. columnName: "departureAirport",
  30. },
  31. {
  32. columnLabel: "经停机场",
  33. columnName: "transferAirport",
  34. },
  35. {
  36. columnLabel: "降落机场",
  37. columnName: "landingAirport",
  38. },
  39. {
  40. columnLabel: "计划起飞时间",
  41. columnName: "planDepartureTime",
  42. },
  43. {
  44. columnLabel: "实际起飞时间",
  45. columnName: "acDepartureTime",
  46. },
  47. {
  48. columnLabel: "计划降落时间",
  49. columnName: "planLandingTime",
  50. },
  51. {
  52. columnLabel: "实际降落时间",
  53. columnName: "acLandingTime",
  54. },
  55. {
  56. columnLabel: "航班类型",
  57. columnName: "flightType",
  58. },
  59. {
  60. columnLabel: "航班状态",
  61. columnName: "flightState",
  62. },
  63. {
  64. columnLabel: "国内国际",
  65. columnName: "DIType",
  66. },
  67. {
  68. columnLabel: "国内国际标识",
  69. columnName: "DIType_OLD",
  70. },
  71. ],
  72. waybill: [
  73. {
  74. columnLabel: "运单号",
  75. columnName: "stockCode",
  76. },
  77. {
  78. columnLabel: "品名",
  79. columnName: "typeCode",
  80. },
  81. {
  82. columnLabel: "离港航班号",
  83. columnName: "flightNO",
  84. },
  85. {
  86. columnLabel: "离港航班日期",
  87. columnName: "flightDate",
  88. },
  89. {
  90. columnLabel: "离港出发机场",
  91. columnName: "departureAirport",
  92. },
  93. {
  94. columnLabel: "离港到达机场",
  95. columnName: "arriveAirport",
  96. },
  97. {
  98. columnLabel: "总重量",
  99. columnName: "weight",
  100. },
  101. {
  102. columnLabel: "总件数",
  103. columnName: "luggageCount",
  104. },
  105. {
  106. columnLabel: "计费重量",
  107. columnName: "feeWeight",
  108. },
  109. ],
  110. };
  111. export function useTable(
  112. tableName: string,
  113. formData: CommonData,
  114. page: Ref<number>,
  115. noMore: Ref<boolean>,
  116. loading?: Ref<boolean>
  117. ) {
  118. const tableColumns = ref<CommonTableColumn[]>([]);
  119. const tableData = ref<CommonData[]>([]);
  120. const getTableData = async (defaultDataContent?: CommonValue[]) => {
  121. if (!idGetter(tableName)) {
  122. return;
  123. }
  124. if (loading) {
  125. loading.value = true;
  126. }
  127. try {
  128. let {
  129. flightDate,
  130. inOrOut,
  131. planeType,
  132. sAirport,
  133. eAirport,
  134. flightType,
  135. sFlightDate,
  136. company,
  137. startDate,
  138. endDate,
  139. keyWords,
  140. flightNO,
  141. } = formData;
  142. if (
  143. tableName === "waybill" &&
  144. typeof keyWords === "string" &&
  145. keyWords.length === 11
  146. ) {
  147. keyWords = `${keyWords.slice(0, 3)}-${keyWords.slice(3)}`;
  148. }
  149. let dataContent =
  150. tableName === "flight"
  151. ? [
  152. keyWords,
  153. company,
  154. flightDate,
  155. planeType,
  156. sAirport,
  157. eAirport,
  158. flightType,
  159. sFlightDate,
  160. ]
  161. : tableName === "waybill"
  162. ? [startDate, endDate, flightNO, keyWords]
  163. : [startDate, endDate, keyWords];
  164. dataContent = dataContent.map((v) => (v === "" ? null : v));
  165. const {
  166. code,
  167. returnData: { columnSet, listValues },
  168. message,
  169. } = await Query<CommonData>({
  170. id: idGetter(tableName),
  171. dataContent: defaultDataContent ?? dataContent,
  172. pageSize: 9999,
  173. // needPage: page.value,
  174. });
  175. if (Number(code) !== 0) {
  176. throw new Error(message || "失败");
  177. }
  178. if (!tableColumnsMap[tableName]) {
  179. tableColumns.value = columnSet;
  180. }
  181. if (listValues.length) {
  182. tableData.value.push(
  183. ...listValues.filter(
  184. (row) =>
  185. !Object.values(row).some(
  186. (cellValue) =>
  187. typeof cellValue === "string" &&
  188. cellValue.includes("undefined")
  189. )
  190. )
  191. );
  192. tableData.value.forEach((row, rowIndex) => {
  193. row.index = rowIndex + 1;
  194. });
  195. } else {
  196. page.value--;
  197. noMore.value = true;
  198. }
  199. } catch (error) {
  200. console.error(error);
  201. page.value--;
  202. noMore.value = true;
  203. }
  204. if (loading) {
  205. loading.value = false;
  206. }
  207. };
  208. const route = useRoute();
  209. const { flightDate, stockCode } = route.query;
  210. onMounted(() => {
  211. if (tableColumnsMap[tableName]) {
  212. tableColumns.value = tableColumnsMap[tableName].map((column) => ({
  213. columnDescribe: "",
  214. dataType: "",
  215. listqueryTemplateID: null,
  216. needCount: null,
  217. needFilters: null,
  218. needGroup: null,
  219. needSearch: null,
  220. needShow: 1,
  221. needSort: null,
  222. orderNumber: null,
  223. queryTemplateColumnSetID: null,
  224. queryTemplateID: null,
  225. ...column,
  226. }));
  227. }
  228. // const defaultDataContent =
  229. // tableName === 'flight'
  230. // ? [null, null, '2000-01-01', null, null, null, null, null]
  231. // : stockCode
  232. // ? [flightDate, flightDate, stockCode]
  233. // : [null, null, null]
  234. // getTableData(defaultDataContent as CommonValue[])
  235. if (tableName === "waybill") {
  236. formData.startDate = formData.endDate = null;
  237. if (stockCode) {
  238. formData.keyWords = stockCode as string;
  239. getTableData();
  240. }
  241. } else if (tableName === "freight") {
  242. getTableData([null, null, null]);
  243. }
  244. });
  245. return {
  246. tableColumns,
  247. tableData,
  248. getTableData,
  249. };
  250. }