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.

912 lines
37 KiB

2 weeks ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // 打样跟踪页 mixin:事业部进度设置、默认进度设置、工序分类配置弹窗及工序拖拽排序
  2. import {
  3. queryProofTrackingBuProcessConfig,
  4. queryProofTrackingProcessCategories,
  5. deleteProofTrackingProcessColumnsManage,
  6. queryProofTrackingProcessColumnsManage,
  7. saveProofTrackingBuProcessConfig,
  8. saveProofTrackingProcessCategoriesManage,
  9. saveProofTrackingProcessColumnsManage
  10. } from '@/api/sampleTracking/sampleTracking'
  11. import Sortable from 'sortablejs'
  12. import { DEFAULT_PROCESS_COLUMNS } from '../constants'
  13. export default {
  14. methods: {
  15. queryBuProcessConfig () {
  16. const inData = {
  17. site: this.$store.state.user.site,
  18. userName: this.$store.state.user.name
  19. }
  20. return queryProofTrackingBuProcessConfig(inData).then(({ data }) => {
  21. if (!(data && data.code === 0)) {
  22. this.$message.error((data && data.msg) || '加载事业部进度展示配置失败')
  23. return
  24. }
  25. const rows = data.rows || []
  26. const nextMap = {}
  27. const processLabelMap = {}
  28. rows.forEach(item => {
  29. if (item && item.processLabelMap && typeof item.processLabelMap === 'object') {
  30. Object.keys(item.processLabelMap).forEach(rawCode => {
  31. const code = rawCode == null ? '' : String(rawCode).trim()
  32. const label = item.processLabelMap[rawCode] == null ? '' : String(item.processLabelMap[rawCode]).trim()
  33. if (!code || !label) {
  34. return
  35. }
  36. processLabelMap[code] = label
  37. })
  38. }
  39. const buNo = this.normalizeBuNo(item && item.buNo ? item.buNo : '', this.$store.state.user.site)
  40. if (!buNo) {
  41. return
  42. }
  43. const processOrderCodes = this.getValidProcessOrderCodes(item.processOrderCodes)
  44. const processCodes = this.getValidProcessCodes(item.processCodes, processOrderCodes)
  45. const showAll = item && item.showAll === true
  46. ? true
  47. : processCodes.length >= processOrderCodes.length
  48. const selectedCodeMap = {}
  49. processCodes.forEach(code => {
  50. selectedCodeMap[code] = true
  51. })
  52. const processCategoryMap = {}
  53. const sourceCategoryMap = item && item.processCategoryMap && typeof item.processCategoryMap === 'object'
  54. ? item.processCategoryMap
  55. : {}
  56. const processCategoryNameMap = {}
  57. const sourceCategoryNameMap = item && item.processCategoryNameMap && typeof item.processCategoryNameMap === 'object'
  58. ? item.processCategoryNameMap
  59. : {}
  60. Object.keys(sourceCategoryNameMap).forEach(rawCategoryCode => {
  61. const categoryCode = rawCategoryCode == null ? '' : String(rawCategoryCode).trim()
  62. const categoryName = sourceCategoryNameMap[rawCategoryCode] == null
  63. ? ''
  64. : String(sourceCategoryNameMap[rawCategoryCode]).trim()
  65. if (!categoryCode || !categoryName) {
  66. return
  67. }
  68. processCategoryNameMap[categoryCode] = categoryName
  69. processCategoryNameMap[categoryCode.toLowerCase()] = categoryName
  70. })
  71. processOrderCodes.forEach(code => {
  72. if (!code) {
  73. return
  74. }
  75. const directVal = sourceCategoryMap[code]
  76. if (directVal != null) {
  77. processCategoryMap[code] = String(directVal).trim()
  78. return
  79. }
  80. const lowerCode = String(code).trim().toLowerCase()
  81. const matchedKey = Object.keys(sourceCategoryMap).find(rawCode => {
  82. return String(rawCode).trim().toLowerCase() === lowerCode
  83. })
  84. processCategoryMap[code] = matchedKey ? String(sourceCategoryMap[matchedKey] || '').trim() : ''
  85. })
  86. nextMap[buNo] = {
  87. showAll: showAll,
  88. processOrderCodes: processOrderCodes,
  89. processCodes: showAll ? processOrderCodes.slice() : processOrderCodes.filter(code => selectedCodeMap[code]),
  90. processCategoryMap: processCategoryMap,
  91. processCategoryNameMap: processCategoryNameMap
  92. }
  93. })
  94. this.mergeProcessCodeLabelMapByObject(processLabelMap)
  95. this.buProcessConfigMap = nextMap
  96. this.ensureBuProcessConfigRows()
  97. this.loadAllBuProcessCategoryOptions(this.buProcessConfigRows, true)
  98. this.refreshTrackingTableLayout()
  99. }).catch(() => {
  100. this.$message.error('加载事业部进度展示配置异常')
  101. })
  102. },
  103. ensureBuProcessConfigRows () {
  104. const allCodes = this.getAllProcessCodes()
  105. const builtinCodeMap = {}
  106. DEFAULT_PROCESS_COLUMNS.forEach(item => {
  107. if (item && item.code) {
  108. builtinCodeMap[item.code] = true
  109. }
  110. })
  111. const defaultSelectedCodes = allCodes.filter(code => !!builtinCodeMap[code])
  112. const rows = []
  113. const usedBuMap = {}
  114. const appendRow = (buNo, buDesc, site) => {
  115. const key = this.normalizeBuNo(buNo, site || this.$store.state.user.site)
  116. if (!key || usedBuMap[key]) {
  117. return
  118. }
  119. usedBuMap[key] = true
  120. const cfg = this.buProcessConfigMap[key]
  121. const row = {
  122. buNo: key,
  123. buDesc: buDesc || key,
  124. processOrderCodes: allCodes.slice(),
  125. // 新增默认工序默认不勾选:仅预勾选系统内置工序,自定义新增项需事业部手工勾选。
  126. processCodes: (defaultSelectedCodes.length > 0 ? defaultSelectedCodes : allCodes).slice(),
  127. processCategoryMap: {}
  128. }
  129. if (cfg) {
  130. const validOrderCodes = this.getValidProcessOrderCodes(cfg.processOrderCodes)
  131. row.processOrderCodes = validOrderCodes
  132. const validCodes = this.getValidProcessCodes(cfg.processCodes, validOrderCodes)
  133. if (validCodes.length > 0) {
  134. const selectedCodeMap = {}
  135. validCodes.forEach(code => {
  136. selectedCodeMap[code] = true
  137. })
  138. row.processCodes = validOrderCodes.filter(code => selectedCodeMap[code])
  139. } else {
  140. const defaultSelectedCodeMap = {}
  141. row.processCodes.forEach(code => {
  142. defaultSelectedCodeMap[code] = true
  143. })
  144. row.processCodes = validOrderCodes.filter(code => defaultSelectedCodeMap[code])
  145. }
  146. row.processCategoryMap = cfg.processCategoryMap && typeof cfg.processCategoryMap === 'object'
  147. ? Object.assign({}, cfg.processCategoryMap)
  148. : {}
  149. }
  150. row.processCategoryMap = this.normalizeProcessCategoryMapForRow(row)
  151. rows.push(row)
  152. }
  153. this.userBuList.forEach(item => {
  154. if (!item || !item.buNo) {
  155. return
  156. }
  157. appendRow(item.buNo, item.buDesc || this.normalizeBuNo(item.buNo, this.$store.state.user.site), this.$store.state.user.site)
  158. })
  159. Object.keys(this.buProcessConfigMap).forEach(buNo => {
  160. appendRow(buNo, buNo, this.$store.state.user.site)
  161. })
  162. this.buProcessConfigRows = rows
  163. },
  164. openBuProcessConfigDialog () {
  165. if (this.buProcessConfigLoading) {
  166. return
  167. }
  168. this.buProcessConfigLoading = true
  169. this.processOrderDragState = {
  170. buNo: '',
  171. fromIndex: -1
  172. }
  173. // 每次打开都强制拉取后端最新配置,避免必须整页刷新才能看到变更。
  174. Promise.all([
  175. this.queryProcessColumns(),
  176. this.queryBuProcessConfig()
  177. ]).finally(() => {
  178. this.ensureBuProcessConfigRows()
  179. this.buProcessConfigDialogVisible = true
  180. this.loadAllBuProcessCategoryOptions(this.buProcessConfigRows, false).finally(() => {
  181. this.buProcessConfigLoading = false
  182. })
  183. })
  184. },
  185. buildDefaultProcessConfigRow (item) {
  186. const parsedSortNo = parseInt(item && item.sortNo, 10)
  187. const activeFlag = item && item.activeFlag ? String(item.activeFlag).trim().toUpperCase() : 'Y'
  188. const code = item && item.code ? String(item.code).trim() : ''
  189. return {
  190. _rowKey: `default_process_${Date.now()}_${this.defaultProcessConfigRowSeed++}`,
  191. // 记录后端原始编码,删除时可避免“修改了编码但尚未保存”导致删不到原记录。
  192. _persistedCode: code,
  193. sortNo: Number.isNaN(parsedSortNo) || parsedSortNo <= 0 ? this.getNextDefaultProcessSortNo() : parsedSortNo,
  194. code: code,
  195. label: item && item.label ? String(item.label).trim() : '',
  196. activeFlag: activeFlag === 'N' ? 'N' : 'Y'
  197. }
  198. },
  199. getNextDefaultProcessSortNo () {
  200. if (!Array.isArray(this.defaultProcessConfigRows) || this.defaultProcessConfigRows.length === 0) {
  201. return 10
  202. }
  203. let maxSortNo = 0
  204. this.defaultProcessConfigRows.forEach(row => {
  205. const parsedSortNo = parseInt(row && row.sortNo, 10)
  206. if (!Number.isNaN(parsedSortNo) && parsedSortNo > maxSortNo) {
  207. maxSortNo = parsedSortNo
  208. }
  209. })
  210. return maxSortNo > 0 ? maxSortNo + 10 : (this.defaultProcessConfigRows.length + 1) * 10
  211. },
  212. resetDefaultProcessConfigSortNoByOrder () {
  213. if (!Array.isArray(this.defaultProcessConfigRows)) {
  214. return
  215. }
  216. this.defaultProcessConfigRows.forEach((row, index) => {
  217. if (!row) {
  218. return
  219. }
  220. this.$set(row, 'sortNo', (index + 1) * 10)
  221. })
  222. },
  223. clearDefaultProcessConfigSelection () {
  224. this.defaultProcessConfigSelectionRows = []
  225. this.$nextTick(() => {
  226. const tableRef = this.$refs.defaultProcessConfigTable
  227. if (tableRef && tableRef.clearSelection) {
  228. tableRef.clearSelection()
  229. }
  230. })
  231. },
  232. destroyDefaultProcessConfigSortable () {
  233. if (this.defaultProcessConfigSortable && typeof this.defaultProcessConfigSortable.destroy === 'function') {
  234. this.defaultProcessConfigSortable.destroy()
  235. }
  236. this.defaultProcessConfigSortable = null
  237. },
  238. initDefaultProcessConfigSortable () {
  239. this.$nextTick(() => {
  240. this.destroyDefaultProcessConfigSortable()
  241. if (!this.defaultProcessConfigDialogVisible || !Array.isArray(this.defaultProcessConfigRows) || this.defaultProcessConfigRows.length === 0) {
  242. return
  243. }
  244. const tableRef = this.$refs.defaultProcessConfigTable
  245. const tableBody = tableRef && tableRef.$el
  246. ? tableRef.$el.querySelector('.el-table__body-wrapper tbody')
  247. : null
  248. if (!tableBody) {
  249. return
  250. }
  251. this.defaultProcessConfigSortable = Sortable.create(tableBody, {
  252. animation: 150,
  253. // 整行可拖拽,避免必须精确悬浮到小图标才能排序。
  254. handle: 'tr',
  255. draggable: 'tr',
  256. ghostClass: 'default-process-sortable-ghost',
  257. chosenClass: 'default-process-sortable-chosen',
  258. dragClass: 'default-process-sortable-drag',
  259. onEnd: (evt) => {
  260. const oldIndex = evt && typeof evt.oldIndex === 'number' ? evt.oldIndex : -1
  261. const newIndex = evt && typeof evt.newIndex === 'number' ? evt.newIndex : -1
  262. if (oldIndex < 0 || newIndex < 0 || oldIndex === newIndex) {
  263. return
  264. }
  265. const movedRow = this.defaultProcessConfigRows.splice(oldIndex, 1)[0]
  266. this.defaultProcessConfigRows.splice(newIndex, 0, movedRow)
  267. this.resetDefaultProcessConfigSortNoByOrder()
  268. this.clearDefaultProcessConfigSelection()
  269. }
  270. })
  271. })
  272. },
  273. handleDefaultProcessConfigDialogClosed () {
  274. this.defaultProcessConfigDeleteLoading = false
  275. this.clearDefaultProcessConfigSelection()
  276. this.destroyDefaultProcessConfigSortable()
  277. },
  278. handleDefaultProcessConfigSelectionChange (rows) {
  279. this.defaultProcessConfigSelectionRows = Array.isArray(rows) ? rows : []
  280. },
  281. normalizeDefaultProcessFieldBaseByCode (code) {
  282. const rawCode = code == null ? '' : String(code).trim()
  283. if (!rawCode) {
  284. return ''
  285. }
  286. if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(rawCode)) {
  287. return rawCode
  288. }
  289. const parts = rawCode.split(/[^A-Za-z0-9]+/).filter(item => !!item)
  290. if (parts.length === 0) {
  291. return ''
  292. }
  293. const firstPart = parts[0].toLowerCase()
  294. const remained = parts.slice(1).map(item => {
  295. const lower = item.toLowerCase()
  296. return `${lower.charAt(0).toUpperCase()}${lower.substring(1)}`
  297. }).join('')
  298. let fieldBase = `${firstPart}${remained}`
  299. if (/^[0-9]/.test(fieldBase)) {
  300. fieldBase = `p${fieldBase}`
  301. }
  302. return fieldBase
  303. },
  304. buildDefaultProcessFieldsByCode (code) {
  305. const fieldBase = this.normalizeDefaultProcessFieldBaseByCode(code)
  306. if (!fieldBase) {
  307. return {
  308. planField: '',
  309. actualField: '',
  310. statusField: ''
  311. }
  312. }
  313. return {
  314. planField: `${fieldBase}PlanDate`,
  315. actualField: `${fieldBase}ActualDate`,
  316. statusField: `${fieldBase}Status`
  317. }
  318. },
  319. queryDefaultProcessConfig () {
  320. const inData = {
  321. site: '*',
  322. buNo: '*',
  323. userName: this.$store.state.user.name
  324. }
  325. return queryProofTrackingProcessColumnsManage(inData).then(({ data }) => {
  326. if (!(data && data.code === 0)) {
  327. this.$message.error((data && data.msg) || '加载默认工序配置失败')
  328. this.defaultProcessConfigRows = []
  329. this.defaultProcessConfigSelectionRows = []
  330. return []
  331. }
  332. const rows = Array.isArray(data.rows) ? data.rows : []
  333. const sortedRows = rows
  334. .map((item, index) => {
  335. const parsedSortNo = parseInt(item && item.sortNo, 10)
  336. return {
  337. sourceIndex: index,
  338. sortNo: Number.isNaN(parsedSortNo) ? Number.MAX_SAFE_INTEGER : parsedSortNo,
  339. item: item
  340. }
  341. })
  342. .sort((a, b) => {
  343. if (a.sortNo !== b.sortNo) {
  344. return a.sortNo - b.sortNo
  345. }
  346. return a.sourceIndex - b.sourceIndex
  347. })
  348. this.defaultProcessConfigRows = sortedRows.map(({ item }) => this.buildDefaultProcessConfigRow(item))
  349. this.resetDefaultProcessConfigSortNoByOrder()
  350. this.clearDefaultProcessConfigSelection()
  351. this.initDefaultProcessConfigSortable()
  352. return this.defaultProcessConfigRows
  353. }).catch(() => {
  354. this.$message.error('加载默认工序配置异常')
  355. this.defaultProcessConfigRows = []
  356. this.defaultProcessConfigSelectionRows = []
  357. this.destroyDefaultProcessConfigSortable()
  358. return []
  359. })
  360. },
  361. openDefaultProcessConfigDialog () {
  362. this.defaultProcessConfigDeleteLoading = false
  363. this.defaultProcessConfigDialogVisible = true
  364. this.queryDefaultProcessConfig()
  365. },
  366. buildProcessCategoryRow (item) {
  367. const parsedSortNo = parseInt(item && item.sortNo, 10)
  368. const categoryCode = item && item.categoryCode ? String(item.categoryCode).trim() : ''
  369. return {
  370. _rowKey: `process_category_${Date.now()}_${this.processCategoryRowSeed++}`,
  371. _persistedCode: categoryCode,
  372. sortNo: Number.isNaN(parsedSortNo) || parsedSortNo <= 0 ? (this.processCategoryRows.length + 1) * 10 : parsedSortNo,
  373. categoryCode: categoryCode,
  374. categoryName: item && item.categoryName ? String(item.categoryName).trim() : ''
  375. }
  376. },
  377. queryProcessCategoryConfig (buNo, silent = false) {
  378. const normalizedBuNo = this.normalizeBuNo(buNo, this.$store.state.user.site)
  379. if (!normalizedBuNo) {
  380. return Promise.resolve([])
  381. }
  382. const inData = {
  383. site: this.$store.state.user.site,
  384. buNo: normalizedBuNo,
  385. userName: this.$store.state.user.name
  386. }
  387. return queryProofTrackingProcessCategories(inData).then(({ data }) => {
  388. if (!(data && data.code === 0)) {
  389. if (!silent) {
  390. this.$message.error((data && data.msg) || '加载工序分类失败')
  391. }
  392. this.processCategoryRows = []
  393. this.processCategorySelectionRows = []
  394. this.$set(this.buProcessCategoryOptionsMap, normalizedBuNo, [])
  395. if (this.buProcessConfigMap && this.buProcessConfigMap[normalizedBuNo]) {
  396. this.$set(this.buProcessConfigMap, normalizedBuNo, Object.assign({}, this.buProcessConfigMap[normalizedBuNo], {
  397. processCategoryNameMap: {}
  398. }))
  399. }
  400. return []
  401. }
  402. const rows = Array.isArray(data.rows) ? data.rows : []
  403. const categoryOptions = this.buildProcessCategoryOptionsByRows(rows)
  404. this.$set(this.buProcessCategoryOptionsMap, normalizedBuNo, categoryOptions)
  405. if (this.buProcessConfigMap && this.buProcessConfigMap[normalizedBuNo]) {
  406. const processCategoryNameMap = {}
  407. categoryOptions.forEach(item => {
  408. const categoryCode = item && item.categoryCode ? String(item.categoryCode).trim() : ''
  409. const categoryName = item && item.categoryName ? String(item.categoryName).trim() : ''
  410. if (!categoryCode || !categoryName) {
  411. return
  412. }
  413. processCategoryNameMap[categoryCode] = categoryName
  414. processCategoryNameMap[categoryCode.toLowerCase()] = categoryName
  415. })
  416. this.$set(this.buProcessConfigMap, normalizedBuNo, Object.assign({}, this.buProcessConfigMap[normalizedBuNo], {
  417. processCategoryNameMap: processCategoryNameMap
  418. }))
  419. }
  420. const sortedRows = rows
  421. .map((item, index) => {
  422. const parsedSortNo = parseInt(item && item.sortNo, 10)
  423. return {
  424. sourceIndex: index,
  425. sortNo: Number.isNaN(parsedSortNo) ? Number.MAX_SAFE_INTEGER : parsedSortNo,
  426. item: item
  427. }
  428. })
  429. .sort((a, b) => {
  430. if (a.sortNo !== b.sortNo) {
  431. return a.sortNo - b.sortNo
  432. }
  433. return a.sourceIndex - b.sourceIndex
  434. })
  435. this.processCategoryRows = sortedRows.map(({ item }) => this.buildProcessCategoryRow(item))
  436. this.processCategorySelectionRows = []
  437. return this.processCategoryRows
  438. }).catch(() => {
  439. if (!silent) {
  440. this.$message.error('加载工序分类异常')
  441. }
  442. this.processCategoryRows = []
  443. this.processCategorySelectionRows = []
  444. this.$set(this.buProcessCategoryOptionsMap, normalizedBuNo, [])
  445. if (this.buProcessConfigMap && this.buProcessConfigMap[normalizedBuNo]) {
  446. this.$set(this.buProcessConfigMap, normalizedBuNo, Object.assign({}, this.buProcessConfigMap[normalizedBuNo], {
  447. processCategoryNameMap: {}
  448. }))
  449. }
  450. return []
  451. })
  452. },
  453. openProcessCategoryDialog (row) {
  454. const buNo = this.normalizeBuNo(row && row.buNo ? row.buNo : '', this.$store.state.user.site)
  455. if (!buNo) {
  456. this.$message.warning('请在事业部进度设置中指定事业部后再维护分类')
  457. return
  458. }
  459. this.processCategoryDialogBuNo = buNo
  460. this.processCategoryDialogBuDesc = row && row.buDesc ? row.buDesc : buNo
  461. this.processCategoryDialogVisible = true
  462. this.processCategorySaveLoading = false
  463. this.queryProcessCategoryConfig(buNo)
  464. },
  465. handleProcessCategoryDialogClosed () {
  466. this.processCategoryRows = []
  467. this.processCategorySelectionRows = []
  468. this.processCategoryDialogBuNo = ''
  469. this.processCategoryDialogBuDesc = ''
  470. this.processCategorySaveLoading = false
  471. },
  472. handleProcessCategorySelectionChange (rows) {
  473. this.processCategorySelectionRows = Array.isArray(rows) ? rows : []
  474. },
  475. addProcessCategoryRow () {
  476. this.processCategoryRows.push(this.buildProcessCategoryRow({
  477. sortNo: (this.processCategoryRows.length + 1) * 10
  478. }))
  479. },
  480. deleteProcessCategoryRows () {
  481. if (!Array.isArray(this.processCategorySelectionRows) || this.processCategorySelectionRows.length === 0) {
  482. this.$message.warning('请先勾选要删除的分类')
  483. return
  484. }
  485. const selectedRows = this.processCategorySelectionRows.slice()
  486. this.$confirm(`确认删除选中的 ${selectedRows.length} 条分类吗?`, '提示', {
  487. confirmButtonText: '确定',
  488. cancelButtonText: '取消',
  489. type: 'warning'
  490. }).then(() => {
  491. const selectedKeyMap = {}
  492. selectedRows.forEach(item => {
  493. if (item && item._rowKey) {
  494. selectedKeyMap[item._rowKey] = true
  495. }
  496. })
  497. const nextRows = this.processCategoryRows.filter(item => !(item && selectedKeyMap[item._rowKey]))
  498. const submitRows = this.normalizeProcessCategoryRowsForSave(nextRows)
  499. if (!submitRows) {
  500. return
  501. }
  502. const inData = {
  503. site: this.$store.state.user.site,
  504. buNo: this.processCategoryDialogBuNo,
  505. updateBy: this.$store.state.user.name,
  506. rows: submitRows
  507. }
  508. this.processCategorySaveLoading = true
  509. saveProofTrackingProcessCategoriesManage(inData).then(({ data }) => {
  510. if (!(data && data.code === 0)) {
  511. this.$message.error((data && data.msg) || '删除工序分类失败')
  512. return
  513. }
  514. this.handleProcessCategoryConfigSaved(data.msg || '删除成功')
  515. }).catch(() => {
  516. this.$message.error('删除工序分类异常')
  517. }).finally(() => {
  518. this.processCategorySaveLoading = false
  519. })
  520. }).catch(() => {
  521. // 用户取消删除时无需额外提示。
  522. })
  523. },
  524. normalizeProcessCategoryRowsForSave (sourceRowsInput) {
  525. const sourceRows = Array.isArray(sourceRowsInput)
  526. ? sourceRowsInput
  527. : (Array.isArray(this.processCategoryRows) ? this.processCategoryRows : [])
  528. const normalizedRows = []
  529. const usedNameMap = {}
  530. for (let i = 0; i < sourceRows.length; i++) {
  531. const row = sourceRows[i] || {}
  532. const rowNo = i + 1
  533. const categoryCode = row.categoryCode == null ? '' : String(row.categoryCode).trim()
  534. const categoryName = row.categoryName == null ? '' : String(row.categoryName).trim()
  535. if (!categoryName) {
  536. this.$message.warning(`第${rowNo}行的分类名称不能为空`)
  537. return null
  538. }
  539. const nameKey = categoryName.toLowerCase()
  540. if (usedNameMap[nameKey]) {
  541. this.$message.warning(`分类名称【${categoryName}】重复,请检查`)
  542. return null
  543. }
  544. usedNameMap[nameKey] = true
  545. normalizedRows.push({
  546. // 旧分类携带隐藏编码回传,便于后端识别“保留/新增/删除”并做使用校验。
  547. categoryCode: categoryCode || null,
  548. categoryName: categoryName,
  549. sortNo: rowNo * 10
  550. })
  551. }
  552. return normalizedRows
  553. },
  554. handleProcessCategoryConfigSaved (successMsg) {
  555. this.$message.success(successMsg || '工序分类已保存')
  556. this.queryProcessCategoryConfig(this.processCategoryDialogBuNo, true).then(() => {
  557. const buNo = this.processCategoryDialogBuNo
  558. const validCategoryCodeMap = {}
  559. this.getProcessCategoryOptionsByBuNo(buNo).forEach(item => {
  560. if (item && item.categoryCode) {
  561. validCategoryCodeMap[String(item.categoryCode).trim()] = true
  562. }
  563. })
  564. this.buProcessConfigRows.forEach(configRow => {
  565. const rowBuNo = this.normalizeBuNo(configRow && configRow.buNo ? configRow.buNo : '', this.$store.state.user.site)
  566. if (!rowBuNo || rowBuNo !== buNo) {
  567. return
  568. }
  569. const nextCategoryMap = this.normalizeProcessCategoryMapForRow(configRow)
  570. Object.keys(nextCategoryMap).forEach(processCode => {
  571. const categoryCode = nextCategoryMap[processCode]
  572. if (!categoryCode || validCategoryCodeMap[categoryCode]) {
  573. return
  574. }
  575. nextCategoryMap[processCode] = ''
  576. })
  577. this.$set(configRow, 'processCategoryMap', nextCategoryMap)
  578. })
  579. this.queryProcessColumns().then(() => {
  580. this.refreshTrackingTableLayout()
  581. })
  582. })
  583. },
  584. saveProcessCategoryConfig () {
  585. const submitRows = this.normalizeProcessCategoryRowsForSave()
  586. if (!submitRows) {
  587. return
  588. }
  589. const inData = {
  590. site: this.$store.state.user.site,
  591. buNo: this.processCategoryDialogBuNo,
  592. updateBy: this.$store.state.user.name,
  593. rows: submitRows
  594. }
  595. this.processCategorySaveLoading = true
  596. saveProofTrackingProcessCategoriesManage(inData).then(({ data }) => {
  597. if (!(data && data.code === 0)) {
  598. this.$message.error((data && data.msg) || '工序分类保存失败')
  599. return
  600. }
  601. this.handleProcessCategoryConfigSaved(data.msg || '工序分类已保存')
  602. }).catch(() => {
  603. this.$message.error('工序分类保存异常')
  604. }).finally(() => {
  605. this.processCategorySaveLoading = false
  606. })
  607. },
  608. addDefaultProcessConfigRow () {
  609. this.defaultProcessConfigRows.push(this.buildDefaultProcessConfigRow({
  610. sortNo: this.getNextDefaultProcessSortNo(),
  611. activeFlag: 'Y'
  612. }))
  613. this.resetDefaultProcessConfigSortNoByOrder()
  614. this.initDefaultProcessConfigSortable()
  615. },
  616. deleteDefaultProcessConfigRow () {
  617. if (!this.defaultProcessConfigSelectionRows || this.defaultProcessConfigSelectionRows.length === 0) {
  618. this.$message.warning('请先勾选要删除的行')
  619. return
  620. }
  621. const selectedRows = this.defaultProcessConfigSelectionRows.slice()
  622. this.$confirm(`确认删除选中的 ${selectedRows.length} 条默认工序吗?`, '提示', {
  623. confirmButtonText: '确定',
  624. cancelButtonText: '取消',
  625. type: 'warning'
  626. }).then(() => {
  627. const selectedKeyMap = {}
  628. const persistedCodeMap = {}
  629. selectedRows.forEach(item => {
  630. if (item && item._rowKey) {
  631. selectedKeyMap[item._rowKey] = true
  632. }
  633. const persistedCode = item && item._persistedCode ? String(item._persistedCode).trim() : ''
  634. const persistedCodeKey = persistedCode.toLowerCase()
  635. if (persistedCode && !persistedCodeMap[persistedCodeKey]) {
  636. persistedCodeMap[persistedCodeKey] = persistedCode
  637. }
  638. })
  639. const nextRows = this.defaultProcessConfigRows.filter(row => {
  640. return !(row && row._rowKey && selectedKeyMap[row._rowKey])
  641. })
  642. const applyDeletedRowsToUi = () => {
  643. this.defaultProcessConfigRows = nextRows
  644. this.resetDefaultProcessConfigSortNoByOrder()
  645. this.clearDefaultProcessConfigSelection()
  646. this.initDefaultProcessConfigSortable()
  647. }
  648. const persistedCodes = Object.keys(persistedCodeMap).map(key => persistedCodeMap[key])
  649. if (persistedCodes.length === 0) {
  650. applyDeletedRowsToUi()
  651. this.$message.success('删除成功')
  652. return
  653. }
  654. const inData = {
  655. site: '*',
  656. buNo: '*',
  657. updateBy: this.$store.state.user.name,
  658. processCodes: persistedCodes
  659. }
  660. this.defaultProcessConfigDeleteLoading = true
  661. deleteProofTrackingProcessColumnsManage(inData).then(({ data }) => {
  662. if (!(data && data.code === 0)) {
  663. this.$message.error((data && data.msg) || '删除默认工序失败')
  664. return
  665. }
  666. applyDeletedRowsToUi()
  667. this.$message.success(data.msg || '删除成功')
  668. // 删除动作实时落库后立即刷新主列表列定义,避免页面继续展示已删工序。
  669. this.queryProcessColumns().then(() => {
  670. this.queryBuProcessConfig()
  671. this.getDataList()
  672. this.refreshTrackingTableLayout()
  673. })
  674. }).catch(() => {
  675. this.$message.error('删除默认工序异常')
  676. }).finally(() => {
  677. this.defaultProcessConfigDeleteLoading = false
  678. })
  679. }).catch(() => {
  680. // 用户取消删除时无需额外提示。
  681. })
  682. },
  683. normalizeDefaultProcessConfigRowsForSave () {
  684. const sourceRows = Array.isArray(this.defaultProcessConfigRows) ? this.defaultProcessConfigRows : []
  685. const normalizedRows = []
  686. const usedCodeMap = {}
  687. for (let i = 0; i < sourceRows.length; i++) {
  688. const row = sourceRows[i] || {}
  689. const rowNo = i + 1
  690. const code = row.code == null ? '' : String(row.code).trim()
  691. const codeKey = code.toLowerCase()
  692. const label = row.label == null ? '' : String(row.label).trim()
  693. if (!code) {
  694. this.$message.warning(`第${rowNo}行的工序编码不能为空`)
  695. return null
  696. }
  697. if (usedCodeMap[codeKey]) {
  698. this.$message.warning(`工序编码【${code}】重复,请检查`)
  699. return null
  700. }
  701. if (!label) {
  702. this.$message.warning(`第${rowNo}行的工序名称不能为空`)
  703. return null
  704. }
  705. const fieldData = this.buildDefaultProcessFieldsByCode(code)
  706. if (!fieldData.planField || !fieldData.actualField || !fieldData.statusField) {
  707. this.$message.warning(`第${rowNo}行的工序编码格式无法生成字段,请调整编码`)
  708. return null
  709. }
  710. usedCodeMap[codeKey] = true
  711. normalizedRows.push({
  712. code: code,
  713. label: label,
  714. planField: fieldData.planField,
  715. actualField: fieldData.actualField,
  716. statusField: fieldData.statusField,
  717. // 用户要求:默认进度设置不维护工序分类。
  718. processCategoryCode: null,
  719. sortNo: rowNo * 10,
  720. activeFlag: String(row.activeFlag || 'Y').toUpperCase() === 'N' ? 'N' : 'Y'
  721. })
  722. }
  723. return normalizedRows
  724. },
  725. saveDefaultProcessConfig () {
  726. const submitRows = this.normalizeDefaultProcessConfigRowsForSave()
  727. if (!submitRows) {
  728. return
  729. }
  730. const executeSave = () => {
  731. const inData = {
  732. site: '*',
  733. buNo: '*',
  734. updateBy: this.$store.state.user.name,
  735. rows: submitRows
  736. }
  737. saveProofTrackingProcessColumnsManage(inData).then(({ data }) => {
  738. if (!(data && data.code === 0)) {
  739. this.$message.error((data && data.msg) || '默认工序配置保存失败')
  740. return
  741. }
  742. this.$message.success(data.msg || '默认工序配置已保存')
  743. this.queryDefaultProcessConfig()
  744. // 保存后刷新页面列定义,确保新增/删除工序立即反映到主表列。
  745. this.queryProcessColumns().then(() => {
  746. this.queryBuProcessConfig()
  747. this.getDataList()
  748. this.refreshTrackingTableLayout()
  749. })
  750. }).catch(() => {
  751. this.$message.error('默认工序配置保存异常')
  752. })
  753. }
  754. if (submitRows.length === 0) {
  755. this.$confirm('当前将清空默认工序配置,保存后页面会回退到系统兜底配置,是否继续?', '提示', {
  756. confirmButtonText: '确定',
  757. cancelButtonText: '取消',
  758. type: 'warning'
  759. }).then(() => {
  760. executeSave()
  761. }).catch(() => {
  762. // 用户取消清空时无需额外提示。
  763. })
  764. return
  765. }
  766. executeSave()
  767. },
  768. handleBuProcessCodesChange (row) {
  769. if (!row) {
  770. return
  771. }
  772. const validOrderCodes = this.getValidProcessOrderCodes(row.processOrderCodes)
  773. const validCodes = this.getValidProcessCodes(row.processCodes, validOrderCodes)
  774. const selectedCodeMap = {}
  775. validCodes.forEach(code => {
  776. selectedCodeMap[code] = true
  777. })
  778. row.processOrderCodes = validOrderCodes
  779. row.processCodes = validOrderCodes.filter(code => selectedCodeMap[code])
  780. row.processCategoryMap = this.normalizeProcessCategoryMapForRow(row)
  781. },
  782. isProcessOrderDraggingItem (row, index) {
  783. if (!row || index == null) {
  784. return false
  785. }
  786. return this.processOrderDragState.buNo === row.buNo && this.processOrderDragState.fromIndex === index
  787. },
  788. handleProcessOrderDragStart (row, index, event) {
  789. if (!row || index == null || index < 0) {
  790. return
  791. }
  792. this.processOrderDragState = {
  793. buNo: row.buNo || '',
  794. fromIndex: index
  795. }
  796. if (event && event.dataTransfer) {
  797. event.dataTransfer.effectAllowed = 'move'
  798. }
  799. },
  800. handleProcessOrderDrop (row, targetIndex, event) {
  801. if (event && event.dataTransfer) {
  802. event.dataTransfer.dropEffect = 'move'
  803. }
  804. if (!row || targetIndex == null || targetIndex < 0) {
  805. this.handleProcessOrderDragEnd()
  806. return
  807. }
  808. const dragBuNo = this.processOrderDragState.buNo
  809. const fromIndex = Number(this.processOrderDragState.fromIndex)
  810. if (!dragBuNo || dragBuNo !== row.buNo || Number.isNaN(fromIndex) || fromIndex < 0) {
  811. this.handleProcessOrderDragEnd()
  812. return
  813. }
  814. const validOrderCodes = this.getValidProcessOrderCodes(row.processOrderCodes)
  815. if (fromIndex >= validOrderCodes.length || targetIndex >= validOrderCodes.length) {
  816. this.handleProcessOrderDragEnd()
  817. return
  818. }
  819. if (fromIndex === targetIndex) {
  820. this.handleProcessOrderDragEnd()
  821. return
  822. }
  823. const movedCode = validOrderCodes.splice(fromIndex, 1)[0]
  824. const insertIndex = targetIndex
  825. validOrderCodes.splice(insertIndex, 0, movedCode)
  826. row.processOrderCodes = validOrderCodes
  827. this.handleBuProcessCodesChange(row)
  828. this.handleProcessOrderDragEnd()
  829. },
  830. handleProcessOrderDragEnd () {
  831. this.processOrderDragState = {
  832. buNo: '',
  833. fromIndex: -1
  834. }
  835. },
  836. saveBuProcessConfig () {
  837. if (!this.buProcessConfigRows || this.buProcessConfigRows.length === 0) {
  838. this.$message.warning('暂无可配置的事业部')
  839. return
  840. }
  841. const nextMap = {}
  842. const submitRows = []
  843. for (let i = 0; i < this.buProcessConfigRows.length; i++) {
  844. const row = this.buProcessConfigRows[i]
  845. const buNo = this.normalizeBuNo(row && row.buNo ? row.buNo : '', this.$store.state.user.site)
  846. if (!buNo) {
  847. continue
  848. }
  849. const validOrderCodes = this.getValidProcessOrderCodes(row.processOrderCodes)
  850. const selectedCodeMap = {}
  851. this.getValidProcessCodes(row.processCodes, validOrderCodes).forEach(code => {
  852. selectedCodeMap[code] = true
  853. })
  854. const validCodes = validOrderCodes.filter(code => selectedCodeMap[code])
  855. if (validCodes.length === 0) {
  856. this.$message.warning(`事业部【${row.buDesc || buNo}】至少需要选择一个可见进度`)
  857. return
  858. }
  859. const processCategoryMap = this.normalizeProcessCategoryMapForRow(Object.assign({}, row, {
  860. processOrderCodes: validOrderCodes
  861. }))
  862. const processCategoryNameMap = {}
  863. this.getProcessCategoryOptionsByBuNo(buNo).forEach(item => {
  864. const categoryCode = item && item.categoryCode ? String(item.categoryCode).trim() : ''
  865. const categoryName = item && item.categoryName ? String(item.categoryName).trim() : ''
  866. if (!categoryCode || !categoryName) {
  867. return
  868. }
  869. processCategoryNameMap[categoryCode] = categoryName
  870. processCategoryNameMap[categoryCode.toLowerCase()] = categoryName
  871. })
  872. const showAll = validCodes.length >= validOrderCodes.length
  873. nextMap[buNo] = {
  874. showAll: showAll,
  875. processOrderCodes: validOrderCodes,
  876. processCodes: showAll ? validOrderCodes.slice() : validCodes,
  877. processCategoryMap: processCategoryMap,
  878. processCategoryNameMap: processCategoryNameMap
  879. }
  880. submitRows.push({
  881. buNo: buNo,
  882. showAll: showAll,
  883. processCodes: validCodes,
  884. processOrderCodes: validOrderCodes,
  885. processCategoryMap: processCategoryMap
  886. })
  887. }
  888. const inData = {
  889. site: this.$store.state.user.site,
  890. updateBy: this.$store.state.user.name,
  891. rows: submitRows
  892. }
  893. saveProofTrackingBuProcessConfig(inData).then(({ data }) => {
  894. if (!(data && data.code === 0)) {
  895. this.$message.error((data && data.msg) || '事业部进度展示设置保存失败')
  896. return
  897. }
  898. this.buProcessConfigMap = nextMap
  899. this.ensureBuProcessConfigRows()
  900. this.$message.success(data.msg || '事业部进度展示设置已保存')
  901. // 保存后重新拉取工序列和列表,确保页面立即按最新可见性与顺序刷新。
  902. this.queryProcessColumns().then(() => {
  903. this.getDataList()
  904. this.refreshTrackingTableLayout()
  905. })
  906. }).catch(() => {
  907. this.$message.error('事业部进度展示设置保存异常')
  908. })
  909. }
  910. }
  911. }