release.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. import {
  2. listByFirstTaskId,
  3. listUserByIds,
  4. listWorkCenter,
  5. listByWorkCenter,
  6. lineByCurrentUser,
  7. taskSave,
  8. taskRevoked,
  9. taskAssignment,
  10. listAssign,
  11. checkAssignConfirm,
  12. resetAssignee,
  13. listByRoutingIds,
  14. listByFactoryId,
  15. checkExists
  16. } from '@/api/mainData/index.js';
  17. import { releaseWorkOrder } from '@/api/workOrder/index.js';
  18. export default {
  19. data() {
  20. return {};
  21. },
  22. methods: {
  23. // 查询班次
  24. getClassesData() {
  25. if (!this.current.factoriesId) return;
  26. listByFactoryId(this.current.factoriesId).then((res) => {
  27. if (!res) return;
  28. this.shiftList = res;
  29. });
  30. },
  31. // 查询是否派单
  32. queryCheckExists() {
  33. let params = {
  34. workOrderId: this.current.id,
  35. workCenterId: 123,
  36. teamId: 345
  37. };
  38. checkExists(params)
  39. .then((res) => {
  40. if (!res) return;
  41. this.isDispatchRow = res;
  42. if (res.hasExists) {
  43. this.form.taskAss = 0;
  44. this.orderDis = true;
  45. this.procTaskDis = false;
  46. // 报工类型 判断
  47. this.form.singleReport = res.dispatchType;
  48. if (res.dispatchType == 1) {
  49. this.batchDis = true;
  50. this.singleDis = false;
  51. } else {
  52. this.singleDis = true;
  53. this.batchDis = false;
  54. }
  55. } else {
  56. this.batchDis = false;
  57. this.singleDis = false;
  58. this.orderDis = false;
  59. this.procTaskDis = false;
  60. }
  61. })
  62. .catch((err) => {
  63. this.$message.error(err.message);
  64. });
  65. },
  66. // 获取当前年月日
  67. getFormattedDate() {
  68. const now = new Date();
  69. const year = now.getFullYear();
  70. const month = String(now.getMonth() + 1).padStart(2, '0');
  71. const date = String(now.getDate()).padStart(2, '0');
  72. return `${year}-${month}-${date}`;
  73. },
  74. // 查询工作中心
  75. async workCenterData() {
  76. // const res = await listWorkCenter(this.current.firstTaskId);
  77. const res = await listWorkCenter(this.current.firstTaskId);
  78. this.workCenterList = res;
  79. if (res.length > 0) {
  80. this.form.factoryName = res[0].factoryName;
  81. this.form.workCenterId = res[0].id;
  82. // 查首工序
  83. await this.changeWork(res[0].id); // 选择工作中心
  84. this.getProductionData(res[0].id); // 查询产线
  85. this.FirstTaskIdFn(); // 查询工位数据
  86. } else {
  87. this.$message.warning(
  88. // `${this.current.firstTaskName}工序没有工作中心,请到主数据工序管理维护工序信息!`
  89. `当前登录人不属于首工序所属工作中心及班组,请切换首工序${this.current.firstTaskName}所在的班组成员派单`
  90. );
  91. }
  92. },
  93. // 查询工序列表数据
  94. changeDispatch(e) {
  95. this.tabsLoading = true;
  96. // listByWorkCenter(this.form.workCenterId)
  97. listByRoutingIds([this.current.produceRoutingId])
  98. .then((res) => {
  99. this.tabsLoading = false;
  100. // 如果没有首工序的数据 就不能选择工序任务派单
  101. if (!res || res.length == 0) {
  102. this.processList = [];
  103. this.form.taskAss = 1;
  104. this.procTaskDis = true;
  105. return;
  106. }
  107. this.procTaskDis = false;
  108. let list = [];
  109. let isExist = false;
  110. let firstTaskindex = '';
  111. res.forEach((item, index) => {
  112. isExist = isExist
  113. ? isExist
  114. : item.sourceTaskId === this.current.firstTaskId;
  115. // 如果有首工序id 存储它的下标
  116. if (isExist) {
  117. if (!firstTaskindex && firstTaskindex !== 0) {
  118. firstTaskindex = index;
  119. }
  120. }
  121. let obj = {
  122. id: item.sourceTaskId, //工序 id
  123. name: item.name, //工序 名称
  124. assignType: 1, // 默认的指派数据
  125. assignName: '工位', // 默认指派数据名称
  126. list: [], // 当前工序下面的指派 绑定的表格
  127. selection: [], // 当前工序下面的指派 选中的数据
  128. code: item.code, // 工序 编码
  129. index: index, // 当前工序数据的下标
  130. radioBun: {
  131. // 指派按钮的操作状态 绑定 默认false(可操作)
  132. stationDis: false, // 工位按钮
  133. staffDis: false, // 人员按钮
  134. lineDis: false // 产线按钮
  135. },
  136. startDate: '', // 开始日期 (当前工序中最早的计划开始时间)
  137. endDate: '' // 结束日期 (当前工序中最晚的计划结束时间)
  138. };
  139. list.push(obj);
  140. });
  141. this.firstTaskindex = firstTaskindex;
  142. // 存在首工序报工
  143. if (isExist) {
  144. this.processList = list;
  145. this.processId = res[0].sourceTaskId;
  146. // this.handleClick({ name: res[0].sourceTaskId });
  147. this.initializeQuery();
  148. } else {
  149. this.processList = [];
  150. this.form.taskAss = 1;
  151. this.procTaskDis = true;
  152. this.$message.warning('当前任务没有首工序报工');
  153. }
  154. })
  155. .catch((err) => {
  156. this.processList = [];
  157. this.tabsLoading = false;
  158. this.$message.error(err.message);
  159. });
  160. },
  161. // 初始化查询 查询全部工序操作过的数据
  162. async initializeQuery() {
  163. try {
  164. // 不存在 班组数据的话 就不调用这个方法
  165. if (!this.form.teamId) {
  166. return;
  167. }
  168. let params = {
  169. workOrderId: this.current.id,
  170. workCenterId: this.form.workCenterId,
  171. teamId: this.form.teamId
  172. };
  173. let processMap = {};
  174. this.processList.map((el, index) => (processMap[el.id] = index));
  175. const res = await listAssign(params);
  176. let isFirstData = false; // 判断默认第一道工序是否操作过
  177. if (res && res.length > 0) {
  178. res.map((el) => {
  179. let index = processMap[el.taskId];
  180. if (index === 0 && el.assignees && el.assignees.length > 0) {
  181. isFirstData = true; // 操作过
  182. }
  183. // 这里第三个参数传空数组就好了 会自动计算的
  184. setTimeout(() => {
  185. this.operationalData(index, [el], []);
  186. });
  187. });
  188. // 没有操作过 默认获取一下表格数据
  189. if (!isFirstData) {
  190. this.handleClick({ name: this.processId });
  191. }
  192. } else {
  193. this.handleClick({ name: this.processId });
  194. }
  195. console.log(this.processList, '工序列表');
  196. } catch (err) {}
  197. },
  198. // 选择工作中心
  199. async changeWork(e) {
  200. this.form.workCenterId = e;
  201. // this.changeDispatch(); // 查询工序列表
  202. let data = this.workCenterList.find((item) => item.id == e);
  203. this.form.workCenterName = data.name;
  204. this.teamList = data.teamList || []; // 重新获取 班组数据
  205. if (this.teamList.length > 0) {
  206. this.form.teamId = this.teamList[0].id;
  207. await this.changeGroups(this.teamList[0].id); // 获取人员数据方法
  208. } else {
  209. // 如果没有 班组数据 将人员数据清空 并且判断
  210. this.crewList = [];
  211. this.form.teamId = '';
  212. let data = this.processList.find((item) => item.id == this.processId);
  213. if (data.assignType == 2) {
  214. this.$set(data, 'list', []);
  215. }
  216. }
  217. },
  218. // 选择班组 查询人员的数据
  219. async changeGroups(e) {
  220. let data = this.teamList.find((item) => item.id == e);
  221. this.form.teamName = data.name;
  222. try {
  223. const res = await listUserByIds([e]);
  224. this.crewList = res;
  225. // 查看当前的工序数据是不是 选择的人员 是的话同步更改
  226. if (!this.processId || this.processId == 0) {
  227. return;
  228. }
  229. this.handleClick({ name: this.processId });
  230. this.btnInit();
  231. } catch {
  232. this.crewList = [];
  233. }
  234. },
  235. // 工序按钮初始化
  236. btnInit() {
  237. let data = this.processList.find((item) => item.id == this.processId);
  238. this.$set(data.radioBun, 'lineDis', false);
  239. this.$set(data.radioBun, 'staffDis', false);
  240. this.$set(data.radioBun, 'stationDis', false);
  241. this.$set(data, 'assignName', '工位');
  242. this.$set(data, 'assignType', 1);
  243. },
  244. // 获取产线数据
  245. async getProductionData() {
  246. const res = await lineByCurrentUser(this.form.workCenterId);
  247. this.productionList =
  248. res &&
  249. res.map((el) => {
  250. return {
  251. name: el.name,
  252. code: el.code,
  253. id: el.id
  254. };
  255. });
  256. },
  257. // 查询工位数据
  258. FirstTaskIdFn() {
  259. listByFirstTaskId(this.current.firstTaskId)
  260. .then((res) => {
  261. this.stationList = res;
  262. this.changeDispatch();
  263. })
  264. .catch((err) => {
  265. this.stationList = [];
  266. this.$message.error(err.message);
  267. });
  268. },
  269. // 重置(单个)
  270. resetData(row, item) {
  271. if (!row.changeId) {
  272. return this.$message.warning('只能对已撤回跟已保存的数据进行重置');
  273. }
  274. this.toolbarLoading = true;
  275. resetAssignee(row.changeId)
  276. .then((res) => {
  277. this.toolbarLoading = false;
  278. if (res) {
  279. this.$message.success('操作成功');
  280. // 更改当前表格数据
  281. this.setCurrentTab(item);
  282. }
  283. })
  284. .catch((err) => {
  285. this.toolbarLoading = false;
  286. this.$message.error(err.message);
  287. });
  288. },
  289. // 撤回的逻辑
  290. getWithdrawT(row, type) {
  291. if (type != 2) {
  292. return true;
  293. }
  294. let isFlag = true;
  295. row.selection.forEach((item) => {
  296. if (!item.status) {
  297. isFlag = false;
  298. return;
  299. }
  300. if (!item.status.code) {
  301. isFlag = false;
  302. }
  303. });
  304. return isFlag;
  305. },
  306. // 派单
  307. dispatch(row, type) {
  308. if (row.selection.length == 0) {
  309. return this.$message.warning('请最少选择一条数据');
  310. }
  311. let isWithdraw = this.getWithdrawT(row, type);
  312. if (!isWithdraw) {
  313. this.$message.warning('只有状态为派单的数据才能进行撤回');
  314. return;
  315. }
  316. let assignees = [];
  317. let changeIds = [];
  318. let flag = true; // 是否填写 校验
  319. row.selection.forEach((item) => {
  320. if (!item.quantity || !item.startTime || !item.endTime) {
  321. flag = false;
  322. return;
  323. }
  324. if (item.quantity == 0) {
  325. this.$message.warning('请检查所选数据的数量不能为0');
  326. digit = false;
  327. return;
  328. }
  329. let Aobj = {
  330. assigneeId: item.id,
  331. quantity: item.quantity,
  332. weight: item.weight,
  333. startTime: item.startTime,
  334. endTime: item.endTime,
  335. assigneeType: row.assignType,
  336. assigneeName: item.name
  337. };
  338. if (item.teamTimeIds) {
  339. Aobj.teamTimeIds = item.teamTimeIds;
  340. }
  341. assignees.push(Aobj);
  342. changeIds.push(item.changeId);
  343. });
  344. // 校验没过 就不往下进行
  345. if (!flag) {
  346. this.$message.warning('请将所选数据的 数量,开始时间,完成时间 填写完毕');
  347. return;
  348. }
  349. let data = null;
  350. this.toolbarLoading = true;
  351. if (type == 2) {
  352. data = changeIds;
  353. } else {
  354. data = {
  355. taskId: this.processId,
  356. taskName: row.name,
  357. taskCode: row.code,
  358. teamName: this.form.teamName,
  359. teamId: this.form.teamId,
  360. workCenterId: this.form.workCenterId,
  361. workCenterName: this.form.workCenterName,
  362. workOrderId: this.current.id,
  363. dispatchMethod: 0,
  364. assignees
  365. };
  366. // 先判断是否派单过 有的话直接用前面派单的 类型数据 否则就是自己选择的数据
  367. if (this.isDispatchRow.hasExists) {
  368. data.dispatchType = this.isDispatchRow.dispatchType;
  369. } else {
  370. data.dispatchType = this.form.singleReport;
  371. }
  372. }
  373. const api =
  374. type == 1 ? taskAssignment : type == 2 ? taskRevoked : taskSave;
  375. api(data)
  376. .then((res) => {
  377. this.toolbarLoading = false;
  378. if (res) {
  379. this.$message.success('操作成功');
  380. // 更改当前表格数据
  381. this.setCurrentTab(row);
  382. // 查询是否派单
  383. this.queryCheckExists();
  384. }
  385. })
  386. .catch((err) => {
  387. this.toolbarLoading = false;
  388. this.$message.error(err.message);
  389. });
  390. },
  391. cancel() {
  392. this.$emit('update:dispatchVisible', false);
  393. },
  394. // 按钮操作成功后 更改当前的表格数据
  395. setCurrentTab(row) {
  396. let arr = [];
  397. if (row.assignType == 1) {
  398. arr = this.stationList;
  399. } else if (row.assignType == 2) {
  400. arr = this.crewList;
  401. } else {
  402. arr = this.productionList;
  403. }
  404. this.getAssignData(row.index, arr);
  405. },
  406. // 最后提交
  407. async confirm() {
  408. if (this.form.singleReport != 1 && this.form.singleReport != 0) {
  409. this.$message.warning('请先选择报工类型');
  410. return;
  411. }
  412. // if (!this.form.singleReport) {
  413. // }
  414. if (!this.form.teamId) {
  415. this.$message.warning('请先选择班组');
  416. return;
  417. }
  418. if (this.form.taskAss == 1) {
  419. this.issue();
  420. // 按原来的逻辑
  421. return;
  422. }
  423. // 获取到首工序id 的 绑定表格数据
  424. let data = this.processList[this.firstTaskindex];
  425. // 是否进行首工序派单
  426. let isFirstDispatch = false;
  427. let dispatchArr = []; // 已派单数据
  428. data.list.map((item) => {
  429. if (!item.status) {
  430. return;
  431. }
  432. if (item.status.code == 1) {
  433. isFirstDispatch = true;
  434. dispatchArr.push(item.id);
  435. }
  436. });
  437. // 判断是否进行首工序派单
  438. if (!isFirstDispatch) {
  439. this.$message.warning(`请您先进行首工序任务(${data.name}工序)派单`);
  440. this.handleClick({ name: data.id });
  441. return;
  442. }
  443. let totalSum = 0;
  444. // 判断派单的数据 数量相加是否为 要求生产数量
  445. data.list.map((item) => {
  446. if (item.status && item.status.code == 1) {
  447. totalSum += item.quantity - 0;
  448. }
  449. });
  450. if (totalSum != this.current.formingNum - 0) {
  451. this.$message.warning(
  452. `首工序已派单的数据数量相加需要等于要求生产数量${this.current.formingNum}`
  453. );
  454. this.handleClick({ name: data.id });
  455. return;
  456. }
  457. let params = {
  458. teamId: this.form.teamId,
  459. workCenterId: this.form.workCenterId,
  460. workOrderId: this.current.id
  461. };
  462. this.toolbarLoading = true;
  463. try {
  464. const res = await checkAssignConfirm(params);
  465. if (!res) {
  466. this.toolbarLoading = false;
  467. return;
  468. }
  469. let paramsD = {
  470. assignType: data.assignType,
  471. singleReport: this.form.singleReport,
  472. teamId: this.form.teamId,
  473. crewIds: data.assignType == 2 ? dispatchArr : [], // 人员 2
  474. workstationIds: data.assignType == 1 ? dispatchArr : [], // 工位 1
  475. id: this.current.id
  476. };
  477. const response = await releaseWorkOrder(paramsD);
  478. this.toolbarLoading = false;
  479. if (response) {
  480. this.$message.success('操作成功');
  481. this.cancel();
  482. this.$emit('createSuccess');
  483. }
  484. } catch (err) {
  485. this.toolbarLoading = false;
  486. this.$message.error(err.message);
  487. }
  488. },
  489. // 下达
  490. issue() {
  491. if (this.form.assignType == 1) {
  492. if (!this.form.workstationIds || this.form.workstationIds.length == 0) {
  493. this.$message.warning('请选择工位数据');
  494. return;
  495. }
  496. }
  497. if (this.form.assignType == 3) {
  498. if (!this.form.factoryLineIds || this.form.factoryLineIds.length == 0) {
  499. this.$message.warning('请选择产线数据');
  500. return;
  501. }
  502. }
  503. let data = {
  504. assignType: this.form.assignType,
  505. singleReport: this.form.singleReport,
  506. teamId: this.form.teamId,
  507. crewIds: [],
  508. workstationIds: [],
  509. id: this.current.id,
  510. factoryLineIds: []
  511. };
  512. if (this.form.assignType == 1) {
  513. data.workstationIds = this.form.workstationIds || [];
  514. } else if (this.form.assignType == 2) {
  515. data.crewIds = this.form.crewIds || [];
  516. } else {
  517. data.factoryLineIds = this.form.factoryLineIds || [];
  518. }
  519. this.toolbarLoading = true;
  520. releaseWorkOrder(data)
  521. .then((res) => {
  522. this.toolbarLoading = false;
  523. if (res) {
  524. this.$message.success('操作成功');
  525. this.cancel();
  526. this.$emit('createSuccess');
  527. }
  528. })
  529. .catch((err) => {
  530. this.toolbarLoading = false;
  531. this.$message.error(err.message);
  532. });
  533. },
  534. // 工序选择
  535. async handleClick(tab) {
  536. let id = tab.name;
  537. this.processId = id;
  538. let data = this.processList.find((item) => item.id == this.processId);
  539. await this.changeRadio(data.assignType, data.index);
  540. },
  541. // 指派选择
  542. async changeRadio(e, index) {
  543. let data = this.processList[index];
  544. if (e == 1) {
  545. this.dynamicName = '工位名称';
  546. data.assignName = '工位';
  547. await this.getAssignData(index, this.stationList);
  548. } else if (e == 2) {
  549. this.dynamicName = '人员名称';
  550. data.assignName = '人员';
  551. await this.getAssignData(index, this.crewList);
  552. } else {
  553. this.dynamicName = '产线名称';
  554. data.assignName = '产线';
  555. await this.getAssignData(index, this.productionList);
  556. }
  557. },
  558. // 指派数据处理
  559. async getAssignData(index, arr) {
  560. let list = JSON.parse(JSON.stringify(arr));
  561. let dataRow = this.processList[index];
  562. // 不存在 班组数据的话 就不调用这个方法
  563. if (!this.form.teamId) {
  564. return;
  565. }
  566. let params = {
  567. workOrderId: this.current.id,
  568. workCenterId: this.form.workCenterId,
  569. teamId: this.form.teamId,
  570. taskId: dataRow.id
  571. };
  572. this.tabLoading = true;
  573. try {
  574. const res = await listAssign(params);
  575. this.tabLoading = false;
  576. // 如果 res 没有数据 说明未对数据进行操作 直接赋值
  577. if (res.length == 0) {
  578. this.$set(dataRow, 'list', list);
  579. return;
  580. }
  581. // 对有操作过的数据进行赋值
  582. this.operationalData(index, res, list);
  583. } catch (err) {
  584. this.tabLoading = false;
  585. this.$message.warning(err.message);
  586. }
  587. },
  588. // 操作过的数据 赋值
  589. operationalData(index, res, list) {
  590. const dataRow = this.processList[index];
  591. // 键值对存储 当前工序 操作的数据 指派的code(键) 数据(list:值)
  592. let arrMap = {};
  593. // 要先判断有没有操作的数据 如果有的话 其它的指派操作置灰
  594. let codeT = null; // 表示有已操作的数据(对应的 code)
  595. res[0].assignees.map((el) => {
  596. let code = el.assigneeType.code;
  597. if (arrMap[code]) {
  598. arrMap[code].arr.push(el);
  599. arrMap[code].bunDis = arrMap[code].bunDis
  600. ? arrMap[code].bunDis
  601. : el.status.desc
  602. ? true
  603. : false;
  604. } else {
  605. arrMap[code] = {
  606. arr: [el],
  607. bunDis: el.status.desc ? true : false
  608. };
  609. }
  610. codeT = codeT ? codeT : el.status.desc ? code : '';
  611. });
  612. let radioBun = {
  613. lineDis: false, // 产线 3
  614. staffDis: false, // 人员 2
  615. stationDis: false // 工位 1
  616. };
  617. // 默认是传递下来的 list 数据 但是如果 codeT有值 说明 工位 人员 产线 有派单数据
  618. // 需要自动切换过去
  619. let listArr = list;
  620. if (codeT) {
  621. if (codeT == 1) {
  622. radioBun.staffDis = true;
  623. radioBun.lineDis = true;
  624. radioBun.stationDis = false;
  625. listArr = JSON.parse(JSON.stringify(this.stationList));
  626. dataRow.assignType = 1;
  627. dataRow.assignName = '工位';
  628. } else if (codeT == 2) {
  629. radioBun.stationDis = true;
  630. radioBun.lineDis = true;
  631. radioBun.staffDis = false;
  632. listArr = JSON.parse(JSON.stringify(this.crewList));
  633. dataRow.assignType = 2;
  634. dataRow.assignName = '人员';
  635. } else {
  636. radioBun.stationDis = true;
  637. radioBun.staffDis = true;
  638. radioBun.lineDis = false;
  639. listArr = JSON.parse(JSON.stringify(this.productionList));
  640. dataRow.assignType = 3;
  641. dataRow.assignName = '产线';
  642. }
  643. }
  644. // 切换完后 对 指派的数组数据进行赋值
  645. let listMap = {};
  646. listArr.map((el, index) => (listMap[el.id] = index));
  647. let arrList = codeT ? arrMap[codeT].arr : res[0].assignees;
  648. dataRow.startDate = ''; // 每次计算的时候清空之前的
  649. dataRow.endDate = '';
  650. arrList.map((item) => {
  651. if (item.assigneeType.code == dataRow.assignType) {
  652. let idx = listMap[item.assigneeId];
  653. if (!idx && idx !== 0) {
  654. return;
  655. }
  656. listArr[idx].status = item.status;
  657. listArr[idx].startTime = item.startTime;
  658. listArr[idx].endTime = item.endTime;
  659. listArr[idx].quantity = item.quantity;
  660. listArr[idx].weight = item.weight;
  661. listArr[idx].changeId = item.id;
  662. listArr[idx].teamTimeIds = item.teamTimeIds;
  663. this.compareAndSetTime(listArr[idx], dataRow);
  664. this.compareEndSetTime(listArr[idx], dataRow);
  665. }
  666. });
  667. // 这里重新调用一个方法 计算出 startDate endDate
  668. this.$set(dataRow, 'list', listArr);
  669. this.$set(dataRow, 'radioBun', radioBun);
  670. },
  671. // 时间比较与赋值方法 开始时间
  672. compareAndSetTime(data, dataRow) {
  673. // 如果startDate为空,直接赋值为startTime
  674. if (!dataRow.startDate) {
  675. dataRow.startDate = data['startTime'];
  676. return;
  677. }
  678. // 转换为Date对象进行比较
  679. const startTimeDate = new Date(data['startTime']);
  680. const startDateDate = new Date(dataRow.startDate);
  681. // 比较时间(getTime()获取时间戳)
  682. if (startTimeDate.getTime() < startDateDate.getTime()) {
  683. dataRow.startDate = data['startTime'];
  684. }
  685. },
  686. // 时间比较与赋值方法 结束时间时间
  687. compareEndSetTime(data, dataRow) {
  688. // 如果startDate为空,直接赋值为endTime
  689. if (!dataRow.endDate) {
  690. dataRow.endDate = data['endTime'];
  691. return;
  692. }
  693. // 转换为Date对象进行比较
  694. const endTimeDate = new Date(data['endTime']);
  695. const endDateDate = new Date(dataRow.endDate);
  696. // 比较时间(getTime()获取时间戳)
  697. if (endTimeDate.getTime() > endDateDate.getTime()) {
  698. dataRow.endDate = data['endTime'];
  699. }
  700. },
  701. assignRadio(e) {},
  702. // 数量正则 quantity
  703. handleQuantityInput(e, row, item) {
  704. // 过滤非数字字符(包括负号)
  705. let value = e.replace(/[^\d]/g, '');
  706. // 限制不能以 0 开头(除非是 0 本身)
  707. if (value.startsWith('0') && value.length > 1) {
  708. value = value.slice(1);
  709. }
  710. // 更新绑定值
  711. row.quantity = value;
  712. this.calculateQuantity(row, item);
  713. this.bringWeight(row.quantity, row);
  714. this.selectedListData(row, item);
  715. },
  716. // 自动算重量
  717. bringWeight(value, row) {
  718. if (!this.current.formingWeight) {
  719. this.$set(row, 'weight', 0);
  720. return;
  721. }
  722. let weight =
  723. (this.current.formingWeight / this.current.formingNum) * value;
  724. if (weight > 0) {
  725. weight = weight.toFixed(4) - 0;
  726. }
  727. this.$set(row, 'weight', weight);
  728. },
  729. calculateQuantity(row, item) {
  730. // 如果没有该字段 就不做判断
  731. if (!this.current.formingNum) {
  732. return;
  733. }
  734. let total = 0;
  735. item.list.forEach((el) => {
  736. if (el.quantity) {
  737. total = total + (el.quantity - 0);
  738. }
  739. });
  740. if (total > this.current.formingNum - 0) {
  741. this.$message.warning('列表数量相加不能大于目标要求生产数量');
  742. row.quantity = 0;
  743. }
  744. },
  745. // 计算重量
  746. calculateWeight(row, item) {
  747. // 如果没有该字段 就不做判断
  748. if (!this.current.formingWeight) {
  749. return;
  750. }
  751. let total = 0;
  752. item.list.forEach((el) => {
  753. if (el.weight) {
  754. total = total + (el.weight - 0);
  755. }
  756. });
  757. if (total > this.current.formingWeight - 0) {
  758. this.$message.warning('列表数量相加不能大于目标要求生产数量');
  759. row.weight = 0;
  760. }
  761. },
  762. // 重量正则 weight
  763. handleWeightInput(e, row, item) {
  764. // 过滤非数字和非小数点字符(包括负号)
  765. let value = e.replace(/[-+^\d.]/g, '');
  766. value = e.replace(/[^\d.]/g, '');
  767. // 限制只能有一个小数点
  768. const dotCount = (value.match(/\./g) || []).length;
  769. if (dotCount > 1) {
  770. value = value.slice(0, value.lastIndexOf('.'));
  771. }
  772. // 限制不能以小数点开头
  773. if (value.startsWith('.')) {
  774. value = '0' + value;
  775. }
  776. // 更新绑定值
  777. row.weight = value;
  778. this.calculateWeight(row, item);
  779. this.selectedListData(row, item);
  780. },
  781. // 选中班次
  782. shiftSelection(e, row, item) {
  783. // this.$refs.tableRef0[0].setSelectedRowKeys(row.id);
  784. // let data = this.shiftList.find((item) => item.id == e);
  785. this.selectedListData(row, item);
  786. let data = this.shifTimeData(e, row);
  787. let startTime = `${this.dateValue} ${data.startTime}`;
  788. let endTime = `${this.dateValue} ${data.endTime}`;
  789. this.$set(row, 'startTime', startTime);
  790. this.$set(row, 'endTime', endTime);
  791. this.handleStartTimeChange(row, item);
  792. this.handleEndTimeChange(row, item);
  793. },
  794. // 默认选中当前更改数据
  795. selectedListData(row, item) {
  796. let data = item.selection.find((el) => el.id == row.id);
  797. if (!data) {
  798. let ids = item.selection.map(el=>el.id);
  799. ids.push(row.id);
  800. let tab = `tableRef${[item.index]}`;
  801. this.$refs[tab][0].setSelectedRowKeys(ids);
  802. }
  803. },
  804. // 多选班次时间数据
  805. shifTimeData(e, row) {
  806. if (!e || e.length == 0) {
  807. return {
  808. startTime: row.startTime,
  809. endTime: row.endTime
  810. };
  811. }
  812. let startTime = '';
  813. let endTime = '';
  814. e.map((el) => {
  815. let obj = this.shiftList.find((item) => item.id == el);
  816. if (!startTime) {
  817. startTime = obj.startTime;
  818. }
  819. if (!endTime) {
  820. endTime = obj.endTime;
  821. }
  822. // 获取更小的
  823. startTime =
  824. this.compareTime(startTime, obj.startTime) !== -1
  825. ? obj.startTime
  826. : startTime;
  827. // 获取更大的
  828. endTime =
  829. this.compareTime(endTime, obj.endTime) !== 1 ? obj.endTime : endTime;
  830. });
  831. return {
  832. startTime,
  833. endTime
  834. };
  835. },
  836. // 将HH:mm:ss格式的时间转换为总秒数
  837. timeToSeconds(timeStr) {
  838. const [hours, minutes, seconds] = timeStr.split(':').map(Number);
  839. return hours * 3600 + minutes * 60 + seconds;
  840. },
  841. // this.$refs.table.setSelectedRowKeys(ids);
  842. // 比较两个时间的大小
  843. compareTime(time1, time2) {
  844. const sec1 = this.timeToSeconds(time1);
  845. const sec2 = this.timeToSeconds(time2);
  846. if (sec1 > sec2) {
  847. return 1; // time1 更大
  848. } else if (sec1 < sec2) {
  849. return -1; // time2 更大
  850. } else {
  851. return 0; // 两个时间相等
  852. }
  853. },
  854. // 【开始时间变化时】触发
  855. handleStartTimeChange(row, item) {
  856. if (!row.startTime) {
  857. return;
  858. }
  859. this.selectedListData(row, item);
  860. // 这一道工序的开始时间 不能小于前一道工序的结束时间
  861. const startTime = new Date(row.startTime); // 开始时间
  862. if (item.index !== 0) {
  863. // let frontIdx = item.index - 1;
  864. let frontIdx = this.calculateIndex(item.index).startIdx;
  865. if (frontIdx !== 'none') {
  866. let frontName = this.processList[frontIdx].name;
  867. let time = this.processList[frontIdx].endDate;
  868. const frontTime = new Date(time); // 前面工序的结束时间
  869. if (time && startTime < frontTime) {
  870. this.$message.closeAll();
  871. this.$message.info(
  872. `开始时间不能小于前面工序${frontName}的结束时间${time}`
  873. );
  874. // 判断是否 配置时间更改规则
  875. if (this.time_calc_code == '1') {
  876. row.startTime = time;
  877. }
  878. return;
  879. }
  880. }
  881. }
  882. // 这一道工序的开始时间更不能大于后一道工序的开始时间
  883. if (item.index !== this.processList.length - 1) {
  884. // let latterIdx = item.index + 1;
  885. let latterIdx = this.calculateIndex(item.index).endIdx;
  886. if (latterIdx !== 'none') {
  887. let time = this.processList[latterIdx].startDate;
  888. let latterName = this.processList[latterIdx].name;
  889. const latterTime = new Date(time); // 下一道工序的结束时间
  890. if (time && startTime > latterTime) {
  891. this.$message.closeAll();
  892. this.$message.info(
  893. `开始时间不能大于后面工序${latterName}的开始时间${time}`
  894. );
  895. // 判断是否 配置时间更改规则
  896. if (this.time_calc_code == '1') {
  897. row.startTime = '';
  898. }
  899. return;
  900. }
  901. }
  902. }
  903. // 校验 是否 大于结束时间 wda
  904. this.checkEndTimeValid(row);
  905. },
  906. // 当更改一个工序开始时间 结束时间的时候
  907. // 开始时间 不能小于之前工序的结束时间 ( 要先去上一道工序找 是否存在结束时间 不存在就再往前找 直到 第一道工序 )
  908. // 结束时间 不能大于后面工序的开始时间 ( 要先去后一道工序找 是否存在开始时间 不存在就再往后找 直到 最后一道工序 )
  909. // 计算出当前下标数据 的前后 有 开始时间 结束时间的数据下标
  910. calculateIndex(index) {
  911. let startIdx = 'none';
  912. let endIdx = 'none';
  913. // 前面工序下标
  914. for (let i = index - 1; i >= 0; i--) {
  915. let row = this.processList[i];
  916. if (row && row.endDate) {
  917. startIdx = i;
  918. break;
  919. }
  920. }
  921. // 后面工序的下标
  922. for (let i = index + 1; i <= this.processList.length; i++) {
  923. let row = this.processList[i];
  924. if (row && row.startDate) {
  925. endIdx = i;
  926. break;
  927. }
  928. }
  929. return {
  930. startIdx,
  931. endIdx
  932. };
  933. },
  934. // 【结束时间变化时】触发
  935. handleEndTimeChange(row, item) {
  936. if (!row.endTime) {
  937. return;
  938. }
  939. this.selectedListData(row, item);
  940. const endTime = new Date(row.endTime); // 结束时间
  941. // 当前工序的结束时间 不能大于后一道工序的开始时间
  942. if (item.index !== this.processList.length - 1) {
  943. let latterIdx = this.calculateIndex(item.index).endIdx;
  944. if (latterIdx !== 'none') {
  945. let latterName = this.processList[latterIdx].name;
  946. let time = this.processList[latterIdx].startDate;
  947. const latterTime = new Date(time); // 后面工序的开始时间
  948. if (time && endTime > latterTime) {
  949. this.$message.closeAll();
  950. this.$message.info(
  951. `结束时间不能大于后面工序${latterName}的开始时间${time}`
  952. );
  953. // 判断是否 配置时间更改规则
  954. if (this.time_calc_code == '1') {
  955. row.endTime = time;
  956. }
  957. return;
  958. }
  959. }
  960. }
  961. // 这一道工序的开始时间更不能小于于前一道工序的结束时间
  962. if (item.index !== 0) {
  963. let frontIdx = this.calculateIndex(item.index).startIdx;
  964. if (frontIdx !== 'none') {
  965. let frontName = this.processList[frontIdx].name;
  966. let time = this.processList[frontIdx].endDate;
  967. const frontTime = new Date(time); // 上一道工序的结束时间
  968. if (time && endTime < frontTime) {
  969. this.$message.closeAll();
  970. this.$message.info(
  971. `结束时间不能小于前面工序${frontName}的结束时间${time}`
  972. );
  973. // 判断是否 配置时间更改规则
  974. if (this.time_calc_code == '1') {
  975. row.endTime = '';
  976. }
  977. return;
  978. }
  979. }
  980. }
  981. this.checkEndTimeValid(row, item);
  982. },
  983. // 时间校验
  984. checkEndTimeValid(row) {
  985. const { startTime: start, endTime: end } = row;
  986. // if (!start || !end) return; // 开始/结束时间未填,跳过
  987. const startTime = new Date(start); // 开始时间
  988. const endTime = new Date(end); // 结束时间
  989. if (endTime < startTime) {
  990. row.endTime = new Date(startTime); // 修正为开始时间
  991. this.$message.info('结束时间不能早于开始时间,已自动设为开始时间');
  992. }
  993. }
  994. }
  995. };