Przeglądaj źródła

feat: 优化货位功能

liujt 1 miesiąc temu
rodzic
commit
293c30f523
1 zmienionych plików z 193 dodań i 65 usunięć
  1. 193 65
      src/views/warehouseManagement/warehouseDefinition/add.vue

+ 193 - 65
src/views/warehouseManagement/warehouseDefinition/add.vue

@@ -303,9 +303,9 @@
                     >添加货架</el-button
                   >
 
-                  <!-- <el-button type="text" v-if="!row.isEdit" @click="editArea(row, $index)"
+                  <el-button type="text" v-if="!row.isEdit" @click="editArea(row, $index)"
                     >编辑</el-button
-                  > -->
+                  >
 
                   <el-button type="text" @click="removeArea($index, row)"
                     >删除库区</el-button
@@ -538,7 +538,7 @@
                   <span v-else>{{ scope.row.remark }}</span>
                 </template>
               </el-table-column>
-              <el-table-column label="操作" fixed="right" min-width="120" align="center">
+              <el-table-column label="操作" fixed="right" min-width="140" align="center">
                 <template slot-scope="{ $index, row }">
                   <el-button
                     type="text"
@@ -546,6 +546,12 @@
                     @click="saveGoods($index, row)"
                     >保存</el-button
                   >
+                  <el-button
+                    type="text"
+                    v-if="!row.isEdit"
+                    @click="editGoods($index, row)"
+                    >编辑</el-button
+                  >
                   <el-button type="text" @click="removeGoods($index, row)"
                     >删除货架</el-button
                   >
@@ -1065,9 +1071,7 @@
         const res = await warehouseDefinition.warehouseDetail(id);
         this.warehouseVO = res.warehouseVO; //仓库基本信息
         const { kq, hj, hw } = this.handleHouse(res);
-        console.log('kq~~', kq);
-        console.log('hj~~', hj);
-        console.log('hw~~', hw);
+
         this.areaForm = {
           warehouseAreasSaveList: kq || [], //库区
           areaGoodsshelvesList: hj || [], //货架
@@ -1135,8 +1139,9 @@
           inputErrorMessage: '请输入正整数'
         })
           .then(({ value }) => {
-            row.subCount = +row.subCount + +value;
             let obj = this.areaForm.warehouseAreasSaveList[index];
+            // 同步更新库区的货架数量
+            obj.subCount = +obj.subCount + +value;
 
             const pre =
               this.areaForm.areaGoodsshelvesList[
@@ -1184,6 +1189,17 @@
             item.extInfo[0].goodsAllocationWide;
         }
       },
+      async editGoods(index, row) {
+        // 校验该货架是否可编辑
+        if (row.id) {
+          const canEdit = await warehouseDefinition.isGoodsshelvesCanDelete(row.id);
+          if (!canEdit) {
+            return this.$message.warning('该货架已上架产品,无法编辑修改。');
+          }
+        }
+        console.log(index, row);
+        this.$set(this.areaForm.areaGoodsshelvesList[index], 'isEdit', true);
+      },
       saveGoods(index, row) {
         // 需要校验的字段 areaGoodsshelvesList
         let list = Object.keys(this.areaCoodsRules),
@@ -1245,53 +1261,80 @@
         if (flag) {
           this.$set(obj, 'isEdit', false);
           this.shelfObj = obj; //储存货架对象
-          let num = obj.extInfo[0].line * obj.extInfo[0].column;
 
-          this.areaIndex = index;
-          this.generateAllocation(num, obj);
-        }
-      },
-      generateAllocation(num, obj) {
-        console.log(obj);
-        const rows = Number(obj.extInfo[0].line); // 行数
-        const cols = Number(obj.extInfo[0].column); // 列数
-        
-        // 按行优先遍历:行从1到rows,列从1到cols
-        for (let row = 1; row <= rows; row++) {
-          for (let col = 1; col <= cols; col++) {
-            // 货位编号规则:货架编号-行号-列号
-            const allocationCode = `${obj.code}-${row}-${col}`;
-            
-            this.areaForm.allocationReqList.push({
-              warehouseAreaCode: obj.reservoirAreaCode,
-              warehouseAreaGoodsCode: obj.code,
-              goodsAllocationCode: allocationCode,
-              // allocationBarcode: '',
-              code: allocationCode,
+          const newRows = Number(obj.extInfo[0].line);
+          const newCols = Number(obj.extInfo[0].column);
+          const newAllocationCount = newRows * newCols;
+
+          // 获取该货架原来的货位数量
+          const existingAllocationList = this.areaForm.allocationReqList.filter(
+            (item) => item.goodsShelvesCode === obj.code
+          );
+          const oldAllocationCount = existingAllocationList.length;
+          const diff = newAllocationCount - oldAllocationCount;
 
-              extInfo: [
-                {
-                  goodsAllocationWide: Number(obj.extInfo[0].areaWidth),
-                  goodsAllocationHigh: Number(
-                    obj.extInfo[0].areaHigh / rows
-                  ).toFixed(0),
-                  goodsAllocationLength: Number(
-                    obj.extInfo[0].areaLength / cols
-                  ).toFixed(0),
-                  weightLimit: '',
-                  volumeUnit:
-                    Number(obj.extInfo[0].areaLength / cols).toFixed(0) *
-                    Number(obj.extInfo[0].areaHigh / rows).toFixed(0) *
-                    Number(obj.extInfo[0].areaWidth)
+          if (diff > 0) {
+            // 货位数量增加,从末尾添加新的货位
+            const lastAllocation = existingAllocationList[existingAllocationList.length - 1];
+            let startRow = 1, startCol = 1;
+            if (lastAllocation) {
+              // 解析最后一个货位的行号和列号
+              const lastCode = lastAllocation.goodsAllocationCode;
+              const parts = lastCode.split('-');
+              if (parts.length >= 2) {
+                startRow = parseInt(parts[parts.length - 2]) || 1;
+                startCol = parseInt(parts[parts.length - 1]) || 0;
+                startCol++;
+                if (startCol > newCols) {
+                  startCol = 1;
+                  startRow++;
                 }
-              ],
-              goodsShelvesCode: obj.code,
-              reservoirAreaCode: obj.reservoirAreaCode,
-              warehouseId: this.formData.id,
+              }
+            }
 
-              type: 3,
-              isEdit: true
-            });
+            for (let i = 0; i < diff; i++) {
+              const rowNum = startRow + Math.floor((startCol + i - 1) / newCols);
+              const colNum = ((startCol + i - 1) % newCols) + 1;
+              const allocationCode = `${obj.code}-${rowNum}-${colNum}`;
+
+              this.areaForm.allocationReqList.push({
+                warehouseAreaCode: obj.reservoirAreaCode,
+                warehouseAreaGoodsCode: obj.code,
+                goodsAllocationCode: allocationCode,
+                code: allocationCode,
+                extInfo: [
+                  {
+                    goodsAllocationWide: Number(obj.extInfo[0].areaWidth),
+                    goodsAllocationHigh: Number(
+                      obj.extInfo[0].areaHigh / newRows
+                    ).toFixed(0),
+                    goodsAllocationLength: Number(
+                      obj.extInfo[0].areaLength / newCols
+                    ).toFixed(0),
+                    weightLimit: '',
+                    volumeUnit:
+                      Number(obj.extInfo[0].areaLength / newCols).toFixed(0) *
+                      Number(obj.extInfo[0].areaHigh / newRows).toFixed(0) *
+                      Number(obj.extInfo[0].areaWidth)
+                  }
+                ],
+                goodsShelvesCode: obj.code,
+                reservoirAreaCode: obj.reservoirAreaCode,
+                warehouseId: this.formData.id,
+                type: 3,
+                isEdit: true
+              });
+            }
+          } else if (diff < 0) {
+            // 货位数量减少,从末尾删除货位
+            let deleteCount = Math.abs(diff);
+            for (let i = this.areaForm.allocationReqList.length - 1; i >= 0 && deleteCount > 0; i--) {
+              const allocation = this.areaForm.allocationReqList[i];
+              if (allocation.goodsShelvesCode === obj.code) {
+                this.areaForm.allocationReqList.splice(i, 1);
+                deleteCount--;
+              }
+            }
           }
         }
       },
@@ -1349,13 +1392,84 @@
         ) {
           return this.$message.warning('库区编号不能重复!');
         }
-        this.hjCodeIndex = index;
-        this.hjCodeFalg = true;
+
+        this.$refs.areaFormName.validate((val) => {
+          if (val) {
+            this.$set(
+              this.areaForm.warehouseAreasSaveList[index],
+              'isEdit',
+              false
+            );
+
+            let obj = this.areaForm.warehouseAreasSaveList[index];
+            const newSubCount = obj.subCount;
+
+            // 获取该库区原有的货架列表
+            const existingShelfList = this.areaForm.areaGoodsshelvesList.filter(
+              (item) => item.reservoirAreaCode === obj.code
+            );
+            const oldSubCount = existingShelfList.length;
+
+            // 计算差值
+            const diff = newSubCount - oldSubCount;
+
+            if (diff > 0) {
+              if (this.editType === 'add') {
+                // 添加库区时,弹出规则货架编号弹窗
+                this.hjCodeIndex = index;
+                this.hjCodeFalg = true;
+              } else {
+                // 编辑库区时,直接添加空白货架行
+                for (let i = 0; i < diff; i++) {
+                  this.areaForm.areaGoodsshelvesList.push({
+                    reservoirAreaCode: obj.code,
+                    warehouseId: this.formData.id,
+                    code: '',
+                    type: 2,
+                    extInfo: [
+                      {
+                        line: '',
+                        column: '',
+                        areaLength: '',
+                        areaWidth: '',
+                        areaHigh: '',
+                        isRegular: 1
+                      }
+                    ],
+                    subCount: '',
+                    remark: '',
+                    isEdit: true
+                  });
+                }
+              }
+            } else if (diff < 0) {
+              // 货架数量减少,从末尾开始删除货架及其对应的货位
+              let deleteCount = Math.abs(diff);
+              for (let i = this.areaForm.areaGoodsshelvesList.length - 1; i >= 0 && deleteCount > 0; i--) {
+                const shelf = this.areaForm.areaGoodsshelvesList[i];
+                if (shelf.reservoirAreaCode === obj.code) {
+                  const shelfCode = shelf.code;
+                  // 删除该货架对应的货位
+                  this.areaForm.allocationReqList = this.areaForm.allocationReqList.filter(
+                    (item) => item.goodsShelvesCode !== shelfCode
+                  );
+                  // 删除货架
+                  this.areaForm.areaGoodsshelvesList.splice(i, 1);
+                  deleteCount--;
+                }
+              }
+              // 更新库区的货架数量
+              const remainingCount = this.areaForm.areaGoodsshelvesList.filter(
+                (item) => item.reservoirAreaCode === obj.code
+              ).length;
+              this.areaForm.warehouseAreasSaveList[index].subCount = remainingCount;
+            }
+          }
+        });
       },
       handleMine() {
         const index = this.hjCodeIndex;
         const ruleName = this.hjForm.code;
-        // console.log('名称', ruleName + index + 1);
         this.$refs.areaFormName.validate((val) => {
           if (val) {
             this.$set(
@@ -1365,8 +1479,8 @@
             );
 
             let obj = this.areaForm.warehouseAreasSaveList[index];
-            let num = this.areaForm.warehouseAreasSaveList[index].subCount;
 
+            // 获取最后一个货架的编号,计算下一个编号的起始值
             const pre =
               this.areaForm.areaGoodsshelvesList[
                 this.areaForm.areaGoodsshelvesList.length - 1
@@ -1375,8 +1489,9 @@
             if (pre) {
               preCode = +(pre.code?.substr(1) || 0);
             }
+
             let codeEng = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
-            let currentLetterIndex = 0;
+            let currentLetterIndex = preCode + 1;
 
             if (ruleName.length > 0) {
               // 计算初始索引
@@ -1388,19 +1503,25 @@
               }
             }
 
-            for (let i = 0; i < num; i++) {
-              let currentLetter = '';
+            // 获取该库区原有的货架数量
+            const existingShelfList = this.areaForm.areaGoodsshelvesList.filter(
+              (item) => item.reservoirAreaCode === obj.code
+            );
+            const oldSubCount = existingShelfList.length;
+            const newSubCount = obj.subCount;
+            const diff = newSubCount - oldSubCount;
 
-              // 获取当前字母
-              let index = currentLetterIndex;
+            // 货架数量增加,添加新的货架
+            for (let i = 0; i < diff; i++) {
+              let idx = currentLetterIndex;
               let dividend = codeEng.length;
               let remainder;
               let quotient;
               let code = '';
 
-              while (index > 0) {
-                remainder = index % dividend;
-                quotient = Math.floor(index / dividend);
+              while (idx > 0) {
+                remainder = idx % dividend;
+                quotient = Math.floor(idx / dividend);
 
                 if (remainder === 0) {
                   remainder = codeEng.length;
@@ -1408,7 +1529,7 @@
                 }
 
                 code = codeEng[remainder - 1] + code;
-                index = quotient;
+                idx = quotient;
               }
 
               let obj1111 = {
@@ -1433,7 +1554,7 @@
 
               this.areaForm.areaGoodsshelvesList.push(obj1111);
 
-              currentLetterIndex++; // 自增索引
+              currentLetterIndex++;
             }
 
             this.hjCodeFalg = false;
@@ -1507,7 +1628,14 @@
           this.areaForm.warehouseAreasSaveList[areaIndex].subCount = afterCount;
         }
       },
-      editArea(row, index) {
+      async editArea(row, index) {
+        // 校验该库区是否可编辑
+        if (row.id) {
+          const canEdit = await warehouseDefinition.isAreaCanDelete(row.id);
+          if (!canEdit) {
+            return this.$message.warning('该库区已上架产品,无法编辑修改。');
+          }
+        }
         this.editType = 'edit';
         row.isEdit = true;
         this.$set(this.areaForm.warehouseAreasSaveList[index], 'isEdit', true);