You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

899 lines
30 KiB

3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
3 months ago
​
3 months ago
3 months ago
​
​
​
​
2 months ago
3 months ago
3 months ago
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
​
​
3 months ago
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
3 months ago
3 months ago
3 months ago
​
3 months ago
3 months ago
3 months ago
​
​
​
3 months ago
3 months ago
​
​
​
​
3 months ago
2 months ago
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
3 months ago
3 months ago
3 months ago
​
​
​
​
​
​
3 months ago
3 months ago
​
​
​
​
​
3 months ago
3 months ago
3 months ago
3 months ago
​
​
​
​
​
​
​
​
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
​
3 months ago
3 months ago
3 months ago
​
​
3 months ago
​
3 months ago
​
3 months ago
​
​
3 months ago
​
​
​
​
​
2 months ago
3 months ago
2 months ago
​
​
​
​
3 months ago
​
​
​
3 months ago
​
3 months ago
3 months ago
​
​
​
3 months ago
3 months ago
3 months ago
​
3 months ago
3 months ago
​
​
2 months ago
​
3 months ago
​
3 months ago
3 months ago
2 months ago
3 months ago
2 months ago
​
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
​
​
​
3 months ago
​
​
3 months ago
3 months ago
​
​
​
​
​
3 months ago
3 months ago
3 months ago
3 months ago
​
​
​
​
​
2 months ago
​
3 months ago
​
​
​
​
​
3 months ago
3 months ago
​
​
3 months ago
3 months ago
​
​
3 months ago
​
​
3 months ago
​
​
3 months ago
3 months ago
​
​
​
​
3 months ago
​
​
​
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
​
3 months ago
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
3 months ago
​
​
3 months ago
3 months ago
​
3 months ago
​
3 months ago
​
​
3 months ago
​
​
​
3 months ago
​
​
​
​
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
3 months ago
3 months ago
3 months ago
​
​
3 months ago
​
3 months ago
​
​
​
3 months ago
​
​
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
​
​
2 months ago
3 months ago
​
​
​
​
​
​
​
3 months ago
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
​
3 months ago
​
​
3 months ago
​
​
​
​
​
​
​
​
3 months ago
3 months ago
​
3 months ago
3 months ago
​
​
​
​
​
3 months ago
3 months ago
​
​
​
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
3 months ago
​
​
3 months ago
​
3 months ago
​
3 months ago
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
​
​
​
​
3 months ago
​
3 months ago
​
​
​
2 months ago
3 months ago
2 months ago
3 months ago
​
2 months ago
3 months ago
​
​
​
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
3 months ago
2 months ago
​
3 months ago
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
2 months ago
2 months ago
​
2 months ago
3 months ago
​
​
2 months ago
3 months ago
​
2 months ago
2 months ago
3 months ago
3 months ago
​
​
​
​
​
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
3 months ago
​
2 months ago
​
​
​
​
​
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
3 months ago
3 months ago
​
3 months ago
​
​
3 months ago
3 months ago
​
​
3 months ago
3 months ago
​
​
3 months ago
​
​
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
3 months ago
​
​
​
​
3 months ago
​
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
​
​
3 months ago
​
​
​
​
3 months ago
​
​
​
​
​
3 months ago
3 months ago
​
​
3 months ago
3 months ago
​
​
3 months ago
3 months ago
​
​
3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 months ago
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
  1. <template>
  2. <div class="mod-initial-import">
  3. <div class="page-toolbar">
  4. <div>
  5. <div class="page-title">期初导入</div>
  6. <div class="page-subtitle">按业务类型导入系统启用前的基础数据</div>
  7. </div>
  8. </div>
  9. <div class="rq">
  10. <el-table :data="importItems" border style="width: 100%">
  11. <el-table-column prop="name" label="期初导入功能" min-width="220" />
  12. <el-table-column prop="description" label="说明" min-width="520" show-overflow-tooltip />
  13. <el-table-column label="操作" width="220" align="center">
  14. <template slot-scope="scope">
  15. <el-button
  16. type="primary"
  17. icon="el-icon-upload2"
  18. @click="openImportDialog(scope.row.type)">
  19. {{ scope.row.name }}
  20. </el-button>
  21. </template>
  22. </el-table-column>
  23. </el-table>
  24. </div>
  25. <el-dialog
  26. :title="dialogTitle"
  27. v-drag
  28. :visible.sync="dialogVisible"
  29. :width="dialogWidth"
  30. top="5vh"
  31. :show-close="!saving"
  32. :close-on-click-modal="false"
  33. :close-on-press-escape="!saving"
  34. :before-close="handleDialogClose"
  35. @closed="handleDialogClosed">
  36. <div class="dialog-toolbar">
  37. <com-po-order-customer-picker
  38. v-if="isPoOrderImport"
  39. v-model="poCustomer"
  40. :site="currentSite"
  41. :disabled="previewLoading || saving"
  42. @input="handlePoCustomerChange"
  43. class="po-import-customer-picker" />
  44. <el-button
  45. type="primary"
  46. icon="el-icon-download"
  47. :loading="templateDownloading"
  48. :disabled="previewLoading || saving"
  49. @click="handleDownloadTemplate">
  50. 下载导入模板
  51. </el-button>
  52. <el-upload
  53. ref="initialImportUpload"
  54. action="#"
  55. accept=".xls,.xlsx"
  56. :auto-upload="false"
  57. :show-file-list="false"
  58. :limit="1"
  59. :disabled="previewLoading || saving"
  60. :on-change="handleFileChange"
  61. :on-exceed="handleFileExceed">
  62. <el-button
  63. icon="el-icon-upload2"
  64. :loading="previewLoading"
  65. :disabled="previewLoading || saving">
  66. 选择导入文件
  67. </el-button>
  68. </el-upload>
  69. <el-button
  70. icon="el-icon-download"
  71. :loading="exporting"
  72. :disabled="previewLoading || saving || exporting || !previewRows.length"
  73. @click="handleExportPreview">
  74. 导出校验结果
  75. </el-button>
  76. <span class="file-name" :title="fileName">{{ fileName || '未选择文件' }}</span>
  77. <span v-if="hasInvalidPreviewRows" class="invalid-hint">存在校验失败行,请先导出并修正后再保存</span>
  78. <span class="preview-count">
  79. 已读取 {{ previewRows.length }} 行<span v-if="previewRows.length > previewDisplayLimit">,表格显示前{{ previewDisplayLimit }}行</span>
  80. <span v-if="invalidPreviewCount">,其中 {{ invalidPreviewCount }} 行校验失败</span>
  81. </span>
  82. </div>
  83. <el-table
  84. v-loading="previewLoading"
  85. :data="previewDisplayRows"
  86. border
  87. height="520"
  88. size="mini"
  89. empty-text="请选择Excel文件进行预览"
  90. :row-class-name="previewRowClassName"
  91. class="initial-import-preview">
  92. <el-table-column
  93. prop="validationMessage"
  94. label="错误信息"
  95. min-width="280"
  96. fixed="left"
  97. align="left"
  98. show-overflow-tooltip />
  99. <el-table-column
  100. v-if="isPoOrderImport"
  101. prop="excelRowNo"
  102. label="Excel行号"
  103. width="90"
  104. fixed="left"
  105. align="center" />
  106. <el-table-column
  107. v-for="column in activeColumns"
  108. :key="column.prop"
  109. :prop="column.prop"
  110. :label="column.label"
  111. :min-width="column.width"
  112. :align="column.align"
  113. show-overflow-tooltip />
  114. </el-table>
  115. <div v-if="isPoOrderImport && saveProgressText" class="save-progress">{{ saveProgressText }}</div>
  116. <div slot="footer" class="dialog-footer">
  117. <el-button
  118. type="primary"
  119. icon="el-icon-check"
  120. :loading="saving"
  121. :disabled="saveDisabled"
  122. @click="handleSave">
  123. {{ saveButtonText }}
  124. </el-button>
  125. <el-button :disabled="previewLoading || saving" @click="dialogVisible = false">关闭</el-button>
  126. </div>
  127. </el-dialog>
  128. </div>
  129. </template>
  130. <script>
  131. import {
  132. downloadPoOrderTemplate,
  133. downloadPartSkuTemplate,
  134. downloadSupplierPriceTemplate,
  135. previewPartSku,
  136. previewPoOrder,
  137. previewSupplierPrice,
  138. savePartSku,
  139. savePoOrderBatch,
  140. saveSupplierPrice
  141. } from '@/api/initialImport'
  142. import ComPoOrderCustomerPicker from '@/views/modules/order/com_poOrder_CustomerPicker.vue'
  143. /**
  144. * 期初导入可用截止日(含当天)。本地日历日晚于该日期后,三个导入入口均禁止使用。
  145. */
  146. const INITIAL_IMPORT_DEADLINE = '2099-10-10'
  147. const INITIAL_IMPORT_EXPIRED_MSG = '期初导入功能已超过可用日期'
  148. export default {
  149. name: 'SysInitialImport',
  150. components: {
  151. ComPoOrderCustomerPicker
  152. },
  153. data () {
  154. return {
  155. importItems: [
  156. {
  157. type: 'supplierPrice',
  158. name: '供应商价格期初导入',
  159. description: '导入供应商物料的QTY/PO、MOQ/COLOR、QTY/CARTON、阶梯数量和阶梯价格。'
  160. },
  161. // {
  162. // type: 'partSku',
  163. // name: '物料SKU导入',
  164. // description: '按Item更新物料SKU,不存在的SKU会自动新增。'
  165. // },
  166. {
  167. type: 'poOrder',
  168. name: 'PO订单导入',
  169. description: '按Customer下载动态模板并批量创建PO订单。'
  170. }
  171. ],
  172. activeImportType: '',
  173. dialogVisible: false,
  174. previewLoading: false,
  175. saving: false,
  176. templateDownloading: false,
  177. exporting: false,
  178. fileName: '',
  179. importFile: null,
  180. poCustomer: '',
  181. previewRows: [],
  182. previewDisplayLimit: 500,
  183. poOrderColumns: [],
  184. saveProgressText: '',
  185. // 保存成功后先关弹框,等关闭动画结束再弹出绿色提示,避免提示被弹框盖住或一起消失。
  186. pendingSuccessMessage: '',
  187. missingItems: [],
  188. missingSuppliers: [],
  189. supplierPriceColumns: [
  190. { prop: 'rawCode', label: 'RAW CODE', width: 120, align: 'center' },
  191. { prop: 'supplier', label: '供应商名称', width: 180, align: 'center' },
  192. { prop: 'moqColor', label: 'MOQ/COLOR', width: 80, align: 'center' },
  193. { prop: 'qtyPo', label: 'QTY/PO', width: 80, align: 'center' },
  194. { prop: 'qtyCarton', label: 'QTY/CARTON', width: 90, align: 'center' },
  195. { prop: 'startDate', label: 'START DATE', width: 100, align: 'center' },
  196. { prop: 'tierQty1', label: 'Tier_QTY_1', width: 80, align: 'center' },
  197. { prop: 'tierQty2', label: 'Tier_QTY_2', width: 80, align: 'center' },
  198. { prop: 'tierQty3', label: 'Tier_QTY_3', width: 80, align: 'center' },
  199. { prop: 'tierQty4', label: 'Tier_QTY_4', width: 80, align: 'center' },
  200. { prop: 'tierQty5', label: 'Tier_QTY_5', width: 80, align: 'center' },
  201. { prop: 'tierQty6', label: 'Tier_QTY_6', width: 80, align: 'center' },
  202. { prop: 'tierQty7', label: 'Tier_QTY_7', width: 80, align: 'center' },
  203. { prop: 'tierQty8', label: 'Tier_QTY_8', width: 80, align: 'center' },
  204. { prop: 'priceTier1', label: 'Price_Tier_1', width: 90, align: 'center' },
  205. { prop: 'priceTier2', label: 'Price_Tier_2', width: 90, align: 'center' },
  206. { prop: 'priceTier3', label: 'Price_Tier_3', width: 90, align: 'center' },
  207. { prop: 'priceTier4', label: 'Price_Tier_4', width: 90, align: 'center' },
  208. { prop: 'priceTier5', label: 'Price_Tier_5', width: 90, align: 'center' },
  209. { prop: 'priceTier6', label: 'Price_Tier_6', width: 90, align: 'center' },
  210. { prop: 'priceTier7', label: 'Price_Tier_7', width: 90, align: 'center' },
  211. { prop: 'priceTier8', label: 'Price_Tier_8', width: 90, align: 'center' }
  212. ],
  213. partSkuColumns: [
  214. { prop: 'item', label: 'ITEM', width: 220, align: 'left' },
  215. { prop: 'sku', label: 'SKU', width: 220, align: 'left' }
  216. ]
  217. }
  218. },
  219. computed: {
  220. currentSite () {
  221. return this.$store.state.user.site || ''
  222. },
  223. isPoOrderImport () {
  224. return this.activeImportType === 'poOrder'
  225. },
  226. activeColumns () {
  227. let columns
  228. if (this.isPoOrderImport) {
  229. columns = this.poOrderColumns
  230. } else if (this.activeImportType === 'partSku') {
  231. columns = this.partSkuColumns
  232. } else {
  233. columns = this.supplierPriceColumns
  234. }
  235. // 预览业务列不含错误信息;弹框里错误信息单独固定在最左侧。
  236. return (columns || []).filter(column => column.prop !== 'validationMessage')
  237. },
  238. invalidPreviewCount () {
  239. return this.previewRows.filter(row => this.isPreviewRowInvalid(row)).length
  240. },
  241. hasInvalidPreviewRows () {
  242. return this.invalidPreviewCount > 0
  243. },
  244. previewDisplayRows () {
  245. return this.previewRows.slice(0, this.previewDisplayLimit)
  246. },
  247. dialogTitle () {
  248. if (this.activeImportType === 'poOrder') {
  249. return 'PO订单导入'
  250. }
  251. return this.activeImportType === 'partSku' ? '物料SKU导入' : '供应商价格期初导入'
  252. },
  253. dialogWidth () {
  254. if (this.activeImportType === 'poOrder') {
  255. return '94%'
  256. }
  257. return this.activeImportType === 'partSku' ? '860px' : '94%'
  258. },
  259. saveButtonText () {
  260. return '保存'
  261. },
  262. saveDisabled () {
  263. // 任意导入类型只要存在黄标行,就不允许继续保存。
  264. if (this.previewLoading || this.saving || !this.previewRows.length || this.hasInvalidPreviewRows) {
  265. return true
  266. }
  267. if (this.isPoOrderImport) {
  268. return !this.poCustomer
  269. }
  270. return false
  271. }
  272. },
  273. methods: {
  274. /**
  275. * 取浏览器本地日历日 YYYY-MM-DD。
  276. * 不用 toISOString,避免 UTC 把日期提前或推后一天。
  277. */
  278. getLocalDateText () {
  279. const now = new Date()
  280. const month = String(now.getMonth() + 1).padStart(2, '0')
  281. const day = String(now.getDate()).padStart(2, '0')
  282. return `${now.getFullYear()}-${month}-${day}`
  283. },
  284. /**
  285. * 导入按钮点击拦截:超过 2026-10-08 则报错并阻止后续导入。
  286. * @returns {boolean} true 表示仍在可用期内
  287. */
  288. ensureInitialImportAvailable () {
  289. if (this.getLocalDateText() > INITIAL_IMPORT_DEADLINE) {
  290. this.$message.error(INITIAL_IMPORT_EXPIRED_MSG)
  291. return false
  292. }
  293. return true
  294. },
  295. openImportDialog (type) {
  296. if (!this.ensureInitialImportAvailable()) {
  297. return
  298. }
  299. if (type !== 'supplierPrice' && type !== 'partSku' && type !== 'poOrder') {
  300. return
  301. }
  302. this.resetDialog()
  303. this.activeImportType = type
  304. this.dialogVisible = true
  305. },
  306. handleFileChange (file) {
  307. if (!this.ensureInitialImportAvailable()) {
  308. this.clearSelectedFile()
  309. return
  310. }
  311. const rawFile = file && file.raw
  312. if (!rawFile) {
  313. return
  314. }
  315. const site = this.$store.state.user.site
  316. if (!site) {
  317. this.$message.error('当前登录用户没有Site')
  318. this.clearSelectedFile()
  319. return
  320. }
  321. this.fileName = file.name || ''
  322. this.importFile = rawFile
  323. this.saveProgressText = ''
  324. if (this.isPoOrderImport && !this.poCustomer) {
  325. this.$message.warning('请先选择 Customer')
  326. this.clearSelectedFile()
  327. return
  328. }
  329. this.previewRows = []
  330. this.poOrderColumns = []
  331. this.missingItems = []
  332. this.missingSuppliers = []
  333. this.previewLoading = true
  334. const previewRequest = this.resolvePreviewRequest(rawFile, site)
  335. previewRequest.then(({ data }) => {
  336. if (data && data.code === 0) {
  337. this.previewRows = data.data || []
  338. this.missingItems = data.missingItems || []
  339. this.missingSuppliers = data.missingSuppliers || []
  340. if (this.isPoOrderImport) {
  341. this.poOrderColumns = this.buildPoOrderColumns(data.columnMapping || {})
  342. }
  343. this.$message.success(`Excel读取成功,共${this.previewRows.length}行`)
  344. this.showMissingDataAlert()
  345. } else {
  346. this.$message.error((data && data.msg) || 'Excel读取失败')
  347. this.clearSelectedFile()
  348. }
  349. }).catch(error => {
  350. const message = error && error.response && error.response.data && error.response.data.msg
  351. ? error.response.data.msg
  352. : 'Excel读取失败,请检查文件格式'
  353. this.$message.error(message)
  354. this.clearSelectedFile()
  355. }).finally(() => {
  356. this.previewLoading = false
  357. })
  358. },
  359. resolvePreviewRequest (rawFile, site) {
  360. if (this.isPoOrderImport) {
  361. return previewPoOrder(rawFile, site, this.poCustomer)
  362. }
  363. return this.activeImportType === 'partSku'
  364. ? previewPartSku(rawFile, site)
  365. : previewSupplierPrice(rawFile, site)
  366. },
  367. buildPoOrderColumns (columnMapping) {
  368. // 预览表按模板字段展示;物料编码等于 Item,供应商编码仍由 Vendor Name 解析。
  369. const columns = Object.keys(columnMapping).map(prop => ({
  370. prop,
  371. label: columnMapping[prop],
  372. width: 130,
  373. align: 'left'
  374. }))
  375. columns.push(
  376. { prop: 'part_no', label: '物料编码', width: 140, align: 'left' },
  377. { prop: 'supplier_no', label: '供应商编码', width: 140, align: 'left' }
  378. )
  379. return columns
  380. },
  381. handlePoCustomerChange () {
  382. if (!this.importFile && !this.previewRows.length) {
  383. return
  384. }
  385. this.previewRows = []
  386. this.poOrderColumns = []
  387. this.missingItems = []
  388. this.missingSuppliers = []
  389. this.saveProgressText = ''
  390. this.clearSelectedFile()
  391. },
  392. handleFileExceed (files) {
  393. if (this.previewLoading || !files || !files.length) {
  394. return
  395. }
  396. this.clearSelectedFile()
  397. this.handleFileChange({ raw: files[0], name: files[0].name })
  398. },
  399. handleDownloadTemplate () {
  400. if (!this.ensureInitialImportAvailable()) {
  401. return
  402. }
  403. const site = this.currentSite
  404. if (!site) {
  405. this.$message.error('当前登录用户没有Site')
  406. return
  407. }
  408. if (this.isPoOrderImport && !this.poCustomer) {
  409. this.$message.warning('请先选择 Customer')
  410. return
  411. }
  412. this.templateDownloading = true
  413. const downloadRequest = this.resolveDownloadTemplateRequest()
  414. const downloadFileName = this.resolveDownloadTemplateFileName()
  415. downloadRequest().then(response => {
  416. const blob = new Blob([response.data], {
  417. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  418. })
  419. const url = window.URL.createObjectURL(blob)
  420. const link = document.createElement('a')
  421. link.href = url
  422. link.download = downloadFileName
  423. document.body.appendChild(link)
  424. link.click()
  425. document.body.removeChild(link)
  426. window.URL.revokeObjectURL(url)
  427. }).catch(() => {
  428. this.$message.error('模板下载失败,请联系管理员')
  429. }).finally(() => {
  430. this.templateDownloading = false
  431. })
  432. },
  433. resolveDownloadTemplateRequest () {
  434. if (this.isPoOrderImport) {
  435. return () => downloadPoOrderTemplate(this.currentSite, this.poCustomer)
  436. }
  437. return this.activeImportType === 'partSku'
  438. ? downloadPartSkuTemplate
  439. : downloadSupplierPriceTemplate
  440. },
  441. resolveDownloadTemplateFileName () {
  442. if (this.isPoOrderImport) {
  443. return `PO订单导入模板_${this.poCustomer}.xlsx`
  444. }
  445. return this.activeImportType === 'partSku'
  446. ? '物料SKU期初导入模板.xlsx'
  447. : '供应商价格期初导入模板.xlsx'
  448. },
  449. handleSave () {
  450. if (!this.ensureInitialImportAvailable()) {
  451. return
  452. }
  453. if (!this.previewRows.length) {
  454. this.$message.warning('请先选择导入文件并完成预览')
  455. return
  456. }
  457. // 三个导入统一:有黄标行一律禁止保存,避免把失败数据写入系统。
  458. if (this.hasInvalidPreviewRows) {
  459. this.$message.warning(`有${this.invalidPreviewCount}行校验未通过,请修正Excel后重新预览。可先导出校验结果`)
  460. return
  461. }
  462. if (this.isPoOrderImport) {
  463. this.performPoOrderImport()
  464. return
  465. }
  466. const site = this.$store.state.user.site
  467. if (!site) {
  468. this.$message.error('当前登录用户没有Site')
  469. return
  470. }
  471. this.performSave(site)
  472. },
  473. performSave (site) {
  474. this.saving = true
  475. const saveRequest = this.activeImportType === 'partSku' ? savePartSku : saveSupplierPrice
  476. saveRequest({
  477. site,
  478. rows: this.previewRows
  479. }).then(({ data }) => {
  480. if (data && data.code === 0) {
  481. this.closeImportDialogAfterSuccess()
  482. } else {
  483. this.showImportSaveError((data && data.msg) || '保存失败')
  484. }
  485. }).catch(error => {
  486. this.showImportSaveError(this.resolveImportSaveErrorMessage(error, '保存失败,请稍后重试'))
  487. }).finally(() => {
  488. this.saving = false
  489. })
  490. },
  491. async performPoOrderImport () {
  492. if (!this.previewRows.length) {
  493. this.$message.warning('请先选择导入文件并完成预览')
  494. return
  495. }
  496. if (!this.currentSite) {
  497. this.$message.error('当前登录用户没有Site')
  498. return
  499. }
  500. if (!this.poCustomer) {
  501. this.$message.warning('请先选择 Customer')
  502. return
  503. }
  504. this.saving = true
  505. this.saveProgressText = ''
  506. const batchSize = 500
  507. const totalCount = this.previewRows.length
  508. let savedCount = 0
  509. try {
  510. while (this.previewRows.length) {
  511. const rows = this.previewRows.slice(0, batchSize)
  512. const { data } = await savePoOrderBatch({
  513. site: this.currentSite,
  514. customer: this.poCustomer,
  515. rows
  516. })
  517. if (!(data && data.code === 0)) {
  518. throw new Error((data && data.msg) || `第${Math.floor(savedCount / batchSize) + 1}批保存失败`)
  519. }
  520. savedCount += Number(data.savedCount == null ? rows.length : data.savedCount)
  521. // 已成功提交的行立即从待保存列表移除。若后续批次失败,用户再次点击保存时
  522. // 只会续传剩余行,避免前面已提交的数据被重复创建。
  523. this.previewRows.splice(0, rows.length)
  524. this.saveProgressText = `正在分批保存:${savedCount}/${totalCount}`
  525. }
  526. this.closeImportDialogAfterSuccess()
  527. } catch (error) {
  528. const message = this.resolveImportSaveErrorMessage(error, '保存失败,请稍后重试')
  529. this.showImportSaveError(`已保存${savedCount}行,后续批次停止:${message}`)
  530. } finally {
  531. this.saving = false
  532. }
  533. },
  534. /**
  535. * 从保存接口失败结果中取出用户可读错误。
  536. * HTTP 200 + code=500 时错误在 data.msg;网络/超时时走 axios error。
  537. */
  538. resolveImportSaveErrorMessage (error, fallback) {
  539. if (!error) {
  540. return fallback
  541. }
  542. const responseData = error.response && error.response.data
  543. if (responseData && responseData.msg) {
  544. return responseData.msg
  545. }
  546. if (error.message && /timeout/i.test(error.message)) {
  547. return '保存超时,请缩小导入行数后重试'
  548. }
  549. return error.message || fallback
  550. },
  551. /**
  552. * 导入保存失败必须弹确认框,点确定后才关闭,避免 $message 几秒后消失看不清。
  553. */
  554. showImportSaveError (message) {
  555. const text = message || '保存失败'
  556. const content = this.$createElement(
  557. 'div',
  558. { style: { maxHeight: '360px', overflowY: 'auto', whiteSpace: 'pre-wrap', wordBreak: 'break-all', lineHeight: '1.6', textAlign: 'left' } },
  559. text
  560. )
  561. this.$alert(content, '导入保存失败', {
  562. confirmButtonText: '确定',
  563. type: 'error',
  564. customClass: 'initial-import-error-alert'
  565. }).catch(() => {})
  566. },
  567. /**
  568. * 预览后弹出缺失主数据提示。
  569. * PO 订单导入不再校验 Item 是否存在,因此不展示 Item 缺失清单。
  570. */
  571. showMissingDataAlert () {
  572. const showMissingItems = this.missingItems.length && !this.isPoOrderImport
  573. const showMissingSuppliers = (this.activeImportType === 'supplierPrice' || this.isPoOrderImport) &&
  574. this.missingSuppliers.length
  575. if (!showMissingItems && !showMissingSuppliers) {
  576. return
  577. }
  578. const messages = []
  579. if (showMissingItems) {
  580. messages.push(`以下Item不存在:${this.missingItems.join('、')}`)
  581. }
  582. if (showMissingSuppliers) {
  583. const supplierLabel = this.isPoOrderImport ? 'Vendor Name' : '供应商名称'
  584. messages.push(`以下${supplierLabel}不存在:${this.missingSuppliers.join('、')}`)
  585. }
  586. const content = this.$createElement(
  587. 'div',
  588. { style: { maxHeight: '360px', overflowY: 'auto', wordBreak: 'break-all', lineHeight: '1.6' } },
  589. messages.map(message => this.$createElement('p', { style: { margin: '0 0 8px' } }, message))
  590. )
  591. this.$alert(content, '数据校验提示', {
  592. confirmButtonText: '确定',
  593. type: 'warning'
  594. }).catch(() => {})
  595. },
  596. previewRowClassName ({ row }) {
  597. return this.isPreviewRowInvalid(row) ? 'initial-import-warning-row' : ''
  598. },
  599. /**
  600. * 预览行是否校验失败。只认 valid === false,与页面黄标规则一致。
  601. */
  602. isPreviewRowInvalid (row) {
  603. return !!(row && row.valid === false)
  604. },
  605. /**
  606. * 导出当前预览数据及校验结果。
  607. * 触发条件:已完成预览且存在数据行。
  608. * 结果:下载 Excel,失败行整行标黄,最后一列为错误信息。
  609. */
  610. handleExportPreview () {
  611. if (!this.previewRows.length) {
  612. this.$message.warning('请先选择导入文件并完成预览')
  613. return
  614. }
  615. this.exporting = true
  616. try {
  617. const columns = this.buildPreviewExportColumns()
  618. const xml = this.buildPreviewExportXml(columns, this.previewRows)
  619. const blob = new Blob(['\ufeff' + xml], { type: 'application/vnd.ms-excel;charset=utf-8' })
  620. const url = window.URL.createObjectURL(blob)
  621. const link = document.createElement('a')
  622. link.href = url
  623. link.download = this.resolvePreviewExportFileName()
  624. document.body.appendChild(link)
  625. link.click()
  626. document.body.removeChild(link)
  627. window.URL.revokeObjectURL(url)
  628. this.$message.success('校验结果已导出')
  629. } catch (error) {
  630. this.$message.error((error && error.message) || '导出失败,请稍后重试')
  631. } finally {
  632. this.exporting = false
  633. }
  634. },
  635. /**
  636. * 导出列与弹框预览一致:错误信息放在最左侧,PO 额外带 Excel 行号。
  637. */
  638. buildPreviewExportColumns () {
  639. const columns = [{
  640. prop: 'validationMessage',
  641. label: '错误信息',
  642. width: 280,
  643. align: 'left'
  644. }]
  645. if (this.isPoOrderImport) {
  646. columns.push({ prop: 'excelRowNo', label: 'Excel行号' })
  647. }
  648. return columns.concat(this.activeColumns || [])
  649. },
  650. resolvePreviewExportFileName () {
  651. const now = new Date()
  652. const month = String(now.getMonth() + 1).padStart(2, '0')
  653. const day = String(now.getDate()).padStart(2, '0')
  654. const hour = String(now.getHours()).padStart(2, '0')
  655. const minute = String(now.getMinutes()).padStart(2, '0')
  656. const second = String(now.getSeconds()).padStart(2, '0')
  657. return `${this.dialogTitle}校验结果_${now.getFullYear()}${month}${day}${hour}${minute}${second}.xls`
  658. },
  659. /**
  660. * 生成带行底色的 SpreadsheetML,Excel 打开后失败行仍为黄色。
  661. * 入参 columns 为导出列;rows 为当前全部预览行(不只表格显示的前 500 行)。
  662. */
  663. buildPreviewExportXml (columns, rows) {
  664. const headerCells = columns.map(column => {
  665. const width = column.prop === 'validationMessage' ? 280 : 80
  666. return `<Column ss:AutoFitWidth="0" ss:Width="${width}"/>`
  667. }).join('')
  668. const headerRow = '<Row ss:StyleID="Header">' + columns.map(column => {
  669. return `<Cell><Data ss:Type="String">${this.escapeExcelXml(column.label)}</Data></Cell>`
  670. }).join('') + '</Row>'
  671. const dataRows = (rows || []).map(row => {
  672. const styleAttr = this.isPreviewRowInvalid(row) ? ' ss:StyleID="Warn"' : ''
  673. const cells = columns.map(column => {
  674. const value = this.formatExportCellValue(row, column.prop)
  675. return `<Cell><Data ss:Type="String">${this.escapeExcelXml(value)}</Data></Cell>`
  676. }).join('')
  677. return `<Row${styleAttr}>${cells}</Row>`
  678. }).join('')
  679. return `<?xml version="1.0" encoding="UTF-8"?>
  680. <?mso-application progid="Excel.Sheet"?>
  681. <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  682. xmlns:o="urn:schemas-microsoft-com:office:office"
  683. xmlns:x="urn:schemas-microsoft-com:office:excel"
  684. xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
  685. <Styles>
  686. <Style ss:ID="Default" ss:Name="Normal">
  687. <Alignment ss:Vertical="Center" ss:WrapText="1"/>
  688. </Style>
  689. <Style ss:ID="Header">
  690. <Font ss:Bold="1"/>
  691. <Interior ss:Color="#D9D9D9" ss:Pattern="Solid"/>
  692. </Style>
  693. <Style ss:ID="Warn">
  694. <Interior ss:Color="#FFF7D6" ss:Pattern="Solid"/>
  695. </Style>
  696. </Styles>
  697. <Worksheet ss:Name="校验结果">
  698. <Table>${headerCells}${headerRow}${dataRows}</Table>
  699. </Worksheet>
  700. </Workbook>`
  701. },
  702. /**
  703. * 导出单元格文本。空值输出空串,避免 Excel 出现 undefined。
  704. */
  705. formatExportCellValue (row, prop) {
  706. if (!row || !prop) {
  707. return ''
  708. }
  709. const value = row[prop]
  710. if (value == null || value === '') {
  711. return ''
  712. }
  713. return String(value)
  714. },
  715. /**
  716. * SpreadsheetML 特殊字符转义,防止错误信息里的符号破坏 XML。
  717. */
  718. escapeExcelXml (value) {
  719. return String(value == null ? '' : value)
  720. .replace(/&/g, '&amp;')
  721. .replace(/</g, '&lt;')
  722. .replace(/>/g, '&gt;')
  723. .replace(/"/g, '&quot;')
  724. },
  725. /**
  726. * 导入保存成功:先关弹框,提示文案等 closed 后再弹出。
  727. */
  728. closeImportDialogAfterSuccess () {
  729. this.saving = false
  730. this.pendingSuccessMessage = '导入成功'
  731. this.dialogVisible = false
  732. },
  733. /**
  734. * 弹框关闭动画结束后再清理状态。
  735. * 若本次是保存成功关闭,再弹出绿色“导入成功”,避免和弹框一起消失。
  736. */
  737. handleDialogClosed () {
  738. const successMessage = this.pendingSuccessMessage
  739. this.pendingSuccessMessage = ''
  740. this.resetDialog()
  741. if (successMessage) {
  742. this.$message.success(successMessage)
  743. }
  744. },
  745. handleDialogClose (done) {
  746. if (!this.saving) {
  747. done()
  748. }
  749. },
  750. clearSelectedFile () {
  751. this.fileName = ''
  752. this.importFile = null
  753. if (this.$refs.initialImportUpload) {
  754. this.$refs.initialImportUpload.clearFiles()
  755. }
  756. },
  757. resetDialog () {
  758. this.activeImportType = ''
  759. this.previewLoading = false
  760. this.saving = false
  761. this.templateDownloading = false
  762. this.exporting = false
  763. this.previewRows = []
  764. this.missingItems = []
  765. this.missingSuppliers = []
  766. this.poOrderColumns = []
  767. this.saveProgressText = ''
  768. this.poCustomer = ''
  769. this.clearSelectedFile()
  770. }
  771. }
  772. }
  773. </script>
  774. <style scoped>
  775. .mod-initial-import {
  776. padding: 16px;
  777. }
  778. .page-toolbar {
  779. display: flex;
  780. align-items: flex-end;
  781. justify-content: space-between;
  782. margin-bottom: 14px;
  783. }
  784. .page-title {
  785. color: #303133;
  786. font-size: 18px;
  787. font-weight: 600;
  788. }
  789. .page-subtitle {
  790. margin-top: 4px;
  791. color: #909399;
  792. font-size: 13px;
  793. }
  794. .dialog-toolbar {
  795. display: flex;
  796. align-items: center;
  797. flex-wrap: wrap;
  798. min-height: 40px;
  799. margin-bottom: 12px;
  800. gap: 10px;
  801. }
  802. .po-import-customer-picker {
  803. width: 100%;
  804. }
  805. .po-import-customer-picker /deep/ .el-form-item {
  806. margin-bottom: 0;
  807. }
  808. .file-name {
  809. display: inline-block;
  810. max-width: 360px;
  811. overflow: hidden;
  812. color: #606266;
  813. text-overflow: ellipsis;
  814. white-space: nowrap;
  815. }
  816. .invalid-hint {
  817. color: #e6a23c;
  818. font-size: 13px;
  819. }
  820. .preview-count {
  821. margin-left: auto;
  822. color: #606266;
  823. font-variant-numeric: tabular-nums;
  824. }
  825. .initial-import-preview {
  826. width: 100%;
  827. }
  828. .initial-import-preview /deep/ .initial-import-warning-row > td {
  829. background-color: #fff7d6 !important;
  830. }
  831. .initial-import-preview /deep/ .initial-import-warning-row:hover > td {
  832. background-color: #fff0b3 !important;
  833. }
  834. .save-progress {
  835. margin-top: 10px;
  836. color: #606266;
  837. font-size: 13px;
  838. text-align: right;
  839. }
  840. .po-import-result {
  841. min-height: 180px;
  842. padding: 12px;
  843. color: #606266;
  844. font-size: 13px;
  845. border: 1px solid #ebeef5;
  846. border-radius: 4px;
  847. background-color: #fafafa;
  848. }
  849. .po-import-errors {
  850. max-height: 160px;
  851. margin-top: 8px;
  852. overflow: auto;
  853. }
  854. .po-import-error-row {
  855. line-height: 1.6;
  856. }
  857. .po-import-error-more {
  858. color: #909399;
  859. }
  860. .dialog-footer {
  861. text-align: right;
  862. }
  863. </style>
  864. <style>
  865. /* MessageBox 挂到 body,不能写 scoped,否则超长 SQL 错误会撑破弹框。 */
  866. .initial-import-error-alert .el-message-box__message {
  867. max-height: 360px;
  868. overflow-y: auto;
  869. white-space: pre-wrap;
  870. word-break: break-all;
  871. line-height: 1.6;
  872. text-align: left;
  873. }
  874. </style>