xiejun
2023-12-07 05d45bd440ffc92e14e6c8728ac8956a0488c0ab
Source/UBCS-WEB/src/components/BatchImport/index.vue
@@ -1,181 +1,412 @@
<template>
  <div>
    <el-dialog :title="title" :visible="visible" append-to-body>
      <Divider text="导入提示" left="30px"></Divider>
      <ul>
        <li v-for="(item, index) in tipList" :key="index">
          {{ item }}
        </li>
      </ul>
      <div class="radio_box">
        <span>分类的路径使用的属性:</span>
        <el-radio-group v-model="classifyAttr">
          <el-radio label="id">分类编号</el-radio>
          <el-radio label="name">分类名称</el-radio>
        </el-radio-group>
      </div>
      <Divider text="excel文件,选择文件后会自动上传" left="30px"></Divider>
      <el-upload
        class="upload"
        :accept="accept"
        :action="action"
        :before-upload="beforeUpload"
        :on-exceed="handleExceed"
        :headers="uploadHeaders"
        :on-success="onSuccess"
        :show-file-list="false"
        :data="{
          codeClassifyOid: this.codeClassifyOid,
          classifyAttr: this.classifyAttr,
        }"
      >
        <el-button size="small" type="primary"
          ><i class="el-icon-upload"></i> 点击上传</el-button
        >
      </el-upload>
      <template #footer>
        <el-button
          type="primary"
          size="small"
          @click="downloadTemplateFun"
          :loading="downloadLoading"
          >下载导入模板</el-button
        >
        <el-button size="small" @click="visible = false">关闭</el-button>
      </template>
    </el-dialog>
    <ShowImportData :visible="showVisible" v-if="showVisible"></ShowImportData>
  </div>
</template>
<script>
import ShowImportData from "./ShowImportData.vue";
import {
  downloadHistoryImportTemplate,
  downloadErrorFile,
  importDataShow
} from "../../api/batchImport/index";
import { getToken } from "@/util/auth";
export default {
  name: "BatchImport",
  components: { ShowImportData },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    accept: {
      type: String,
      default: ".xlsx, .xls",
    },
    codeClassifyOid: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      classifyAttr: "id",
      uploadParams: {},
      downloadLoading: false,
      showVisible: false,
    };
  },
  computed: {
    uploadHeaders() {
      return {
        "Blade-Auth": "bearer " + getToken(),
      };
    },
  },
  methods: {
    getDataByType() {
      if (this.type === "historyImport") {
        this.title = "历史数据导入";
        this.tipList = [
          "1.标题带五角星的表示关键属性,带星号表示必输项",
          "2.企业编码,集团码和状态都需要导入",
          "3.每次仅能最多导入10000条数据,如果出错会返回错误的数据和原因,但是正确的数据会保存",
          "4.分类的路径需要用#分隔。仅填写当前选的分类树上的下级分类的路径,如果当前分类已经是叶子节点,则不填写",
        ];
        this.action =
          "/api/ubcs-code/mdmEngineController/batchImportHistoryData";
      } else if (this.type === "codeApply") {
        this.title = "批量编码申请";
        this.tipList = [
          "1.标题带五角星的表示关键属性,带星号表示必输项",
          "2.分类的路径需要用#分隔。从当前选择分类节点的下级开始填写,直到最末尾节点,如 产品#主机产品#主机产品",
        ];
      }
    },
    beforeUpload(file) {
      const fileType = file.name.split(".").pop();
      if (fileType !== "xlsx" && fileType !== "xls") {
        // 上传格式不符合要求,提示错误信息并取消上传
        this.$message.error("只允许上传xlsx、xls格式的文件");
        return false;
      }
      return true;
    },
    // 下载导入模板
    downloadTemplateFun() {
      this.downloadLoading = true;
      downloadHistoryImportTemplate({
        codeClassifyOid: this.codeClassifyOid,
      })
        .then((res) => {
          this.downloadLoading = false;
          this.$utilFunc.downloadFileByBlob(res.data, "历史数据导入模板.xls");
        })
        .catch(() => {
          this.downloadLoading = false;
        });
    },
    onSuccess(res) {
      if (res.code === 400) {
        this.$message.error(`${res.msg},请下载错误信息进行查看!`);
      }
      downloadErrorFile({ uuid: res.data.fileOid });
    },
  },
  watch: {
    visible: {
      immediate: true,
      handler() {
        this.getDataByType();
      },
    },
  },
};
</script>
<style lang="scss" scoped>
ul {
  color: rgb(188, 188, 188);
  margin: 20px 0 0 0;
  padding: 0;
  padding-left: 30px;
  list-style: none;
  li {
    margin-bottom: 5px;
    font-size: 12px;
  }
}
.radio_box {
  padding-left: 30px;
  margin: 20px 0 25px 0;
  display: flex;
  align-items: center;
  span {
    margin-right: 20px;
  }
}
.upload {
  padding-left: 30px;
  margin-top: 30px;
}
</style>
<template>
  <el-dialog
    v-if="dialogVisible"
    v-dialogDrag
    :title="title"
    :visible.sync="dialogVisible"
    append-to-body
  >
    <Divider left="30px" text="导入提示"></Divider>
    <ul>
      <li v-for="(item, index) in tipList" :key="index">
        {{ item }}
      </li>
    </ul>
    <div
      v-show="currentTypeObj[type]['upParams'] === 'classifyAttr' && this.type !== 'bulkEdit'&& this.type !== 'groupCode'"
      class="radio_box"
    >
      <span>分类的路径使用的属性:</span>
      <el-radio-group v-model="classifyAttr">
        <el-radio label="id">分类编号</el-radio>
        <el-radio label="name">分类名称</el-radio>
      </el-radio-group>
    </div>
    <div
      v-if="showCodeApply && currentTypeObj[type]['upParams'] === 'codeApply'"
      style="margin-top: 20px"
    >
      <Divider
        left="30px"
        text="编码规则的码段信息,请先选择后再导入"
      ></Divider>
      <FormTemplate
        ref="CodeApply"
        v-loading="!showCodeApply"
        :selfColumnConfig="selfColumnConfig"
        :selfColumnType="selfColumnType"
        style="margin-top: 30px"
        type="add"
        @getFormData="getCodeApplyFormData"
        @referConfigDataUpdate="referConfigDataUpdate"
      ></FormTemplate>
    </div>
    <Divider left="30px" text="excel文件,选择文件后会自动上传"></Divider>
    <el-upload
      :accept="accept"
      :action="action"
      :before-upload="beforeUpload"
      :data="upParams"
      :headers="uploadHeaders"
      :on-change="uploadChange"
      :on-error="onError"
      :on-exceed="handleExceed"
      :on-success="onSuccess"
      :show-file-list="false"
      class="upload"
    >
      <el-button size="small" type="primary"
      ><i class="el-icon-upload"></i> 点击上传
      </el-button
      >
    </el-upload>
    <template #footer>
      <div style="display: inline-block" v-if="type != 'groupCode'">
          <el-button
            v-if="type !== 'bulkEdit' "
            :loading="downloadLoading"
            size="small"
            type="primary"
            @click="downloadTemplateFun"
          >下载导入模板
          </el-button>
          <el-button v-if="type === 'bulkEdit'"
                     :loading="downloadLoading"
                     size="small"
                     type="primary"
                     @click="downloadTemplateFun"
          >下载编辑模板
          </el-button>
        </div>
        <div style="display: inline-block">
          <el-button size="small" @click="dialogVisible = false">关闭</el-button>
        </div>
    </template>
    <ShowImportData
      v-if="showVisible"
      :classifyAttr="classifyAttr"
      :codeClassifyOid="codeClassifyOid"
      :leftTree="leftTree"
      :redisOid="redisOid"
      :resetTable="resetTable"
      :secDTOList="secDTOList"
      :title="title"
      :type="type"
      :visible.sync="showVisible"
    ></ShowImportData>
  </el-dialog>
</template>
<script>
import ShowImportData from "./ShowImportData.vue";
import {
  downloadHistoryImportTemplate,
  downloadErrorFile,
  importDataShow,
  downloadBatchImportApplyTemplate,
  downloadBatchApplyCodeTemplate,
  downloadExcelBatchEdit
} from "../../api/batchImport/index";
import {exportCode} from '@/api/GetItem'
import func from "@/util/func";
import FormTemplate from "../FormTemplate/FormTemplate.vue";
import {getToken} from "@/util/auth";
import codeApply from "@/mixins/codeApply.js";
export default {
  name: "BatchImport",
  mixins: [codeApply],
  components: {ShowImportData, FormTemplate},
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    accept: {
      type: String,
      default: ".xlsx, .xls",
    },
    codeClassifyOid: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "",
    },
    tableHeadFindData: {
      type: Array,
      default: []
    },
    selectRow: {
      type: Array,
      default: []
    },
    resetTable: Function
  },
  computed: {
    uploadHeaders() {
      return {
        "Blade-Auth": "bearer " + getToken(),
      };
    },
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
    title() {
      return this.currentTypeObj[this.type]["title"];
    },
    tipList() {
      return this.currentTypeObj[this.type]["tipList"];
    },
    downloadTemplateApi() {
      return this.currentTypeObj[this.type]["downloadTemplateFun"];
    },
    action() {
      return this.currentTypeObj[this.type]["action"];
    },
    upParams() {
      if (this.type === "historyImport" || this.type === "batchApplyCode") {
        return {
          codeClassifyOid: this.codeClassifyOid,
          classifyAttr: this.classifyAttr,
        };
      } else if (this.type === "batchImportApply") {
        // eslint-disable-next-line vue/no-side-effects-in-computed-properties
        this.secDTOList = this.localSecVOList.map((item) => {
          return {
            secOid: item.oid,
            secValue: this.codeApplyForm[item.oid],
          };
        });
        return {
          codeClassifyOid: this.codeClassifyOid,
          secDTOList: JSON.stringify(this.secDTOList),
          ...this.codeApplyForm,
        };
      } else if (this.type === "bulkEdit") {
        return {
          codeClassifyOid: this.codeClassifyOid,
        };
      } else if (this.type === 'groupCode') {
        return {
          codeClassifyOid: this.codeClassifyOid,
        };
      }
    },
  },
  data() {
    return {
      exportArrTwo: {},
      classifyAttr: "id",
      downloadLoading: false,
      showVisible: false,
      leftTree: [],
      redisOid: "",
      pageLoading: null,
      showCodeApply: true,
      currentTypeObj: {
        historyImport: {
          title: "历史数据导入",
          tipList: [
            "1.标题带五角星的表示关键属性,带星号表示必输项",
            "2.企业编码,集团码和状态都需要导入",
            "3.每次仅能最多导入5000条数据(可通过nacos进行配置,推荐5000/次导入),如果出错会返回错误的数据和原因,但是正确数据可以继续保存",
            "4.分类的路径需要用#分隔。仅填写当前选的分类树上的下级分类的路径,如果当前分类已经是叶子节点,则不填写",
          ],
          action: "/api/ubcs-code/mdmEngineController/batchImportHistoryData",
          downloadTemplateFun: downloadHistoryImportTemplate,
          upParams: "classifyAttr",
        },
        bulkEdit: {
          title: "批量编辑导入",
          tipList: [
            "1.标题带五角星的表示关键属性,带星号表示必输项",
            "2.每次仅能最多导入5000条数据(可通过nacos进行配置,推荐5000/次导入),如果出错会返回错误的数据和原因,但是正确数据可以继续保存",
            "3.请按照下载的编辑模板所使用的配置进行具体配置"
          ],
          action: "/api/ubcs-code/mdmEngineController/batchImportEdit",
          downloadTemplateFun: downloadExcelBatchEdit,
          upParams: "classifyAttr",
        },
        batchImportApply: {
          title: "批量导入申请",
          tipList: [
            "1.标题带五角星的表示关键属性,带星号表示必输项",
            "2.请一定先选择/输入编码规则的码段值后再选择excel文件",
            "3.每次仅能最多导入10000条数据,如果出错会返回错误的数据和原因,但是正确的数据会保存",
            "4.如果属性为参照,在参照配置中设置多个属性时,优先使用name属性的值,否则填写第一个属性的值。",
          ],
          action: "/api/ubcs-code/mdmEngineController/batchImportCode",
          downloadTemplateFun: downloadBatchImportApplyTemplate,
          upParams: "codeApply",
        },
        batchApplyCode: {
          title: "批量申请编码",
          tipList: [
            "1.标题带五角星的表示关键属性,带星号表示必输项",
            "2.分类的路径需要用#分隔。从当前选择分类节点的下级开始填写,直到最末尾节点,如 产品#主机产品#主机产品",
          ],
          action: "/api/ubcs-code/mdmEngineController/batchTopImportCode",
          downloadTemplateFun: downloadBatchApplyCodeTemplate,
          upParams: "classifyAttr",
        },
        groupCode: {
          title: "集团码导入",
          tipList: [
            "1.标题带五角星的表示关键属性,带星号表示必输项",
            "2.每次仅能最多导入5000条数据(可通过nacos进行配置,推荐5000/次导入),如果出错会返回错误的数据和原因,但是正确数据可以继续保存",
            "3.Excel第一行和第二行依次为企业编码和集团编码(必填)"
          ],
          action: "/api/ubcs-code/mdmEngineController/importGroupCode",
          downloadTemplateFun: null,
          upParams: "classifyAttr",
        }
      },
      secDTOList: [],
    };
  },
  methods: {
    async beforeUpload(file) {
      const fileType = file.name.split(".").pop();
      if (fileType !== "xlsx" && fileType !== "xls") {
        // 上传格式不符合要求,提示错误信息并取消上传
        this.$message.error("只允许上传xlsx、xls格式的文件");
        return Promise.reject(false);
      }
      if (this.type === "batchImportApply" && this.showCodeApply) {
        const flag = await this.$refs.CodeApply.validate();
        if (!flag) {
          return Promise.reject(false);
        }
      }
      this.pageLoading = this.$loading({
        lock: true,
        text: "文件上传中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      return true;
    },
    // 下载导入模板
    downloadTemplateFun() {
      if (this.type !== 'bulkEdit') {
        this.downloadLoading = true;
        this.downloadTemplateApi({
          codeClassifyOid: this.codeClassifyOid,
        })
          .then((res) => {
            this.$utilFunc.downloadFileByBlob(res.data, this.title + "模板.xls");
            this.downloadLoading = false;
          })
          .catch(() => {
            this.downloadLoading = false;
          });
        return;
      }
      if (this.type === 'bulkEdit') {
        downloadExcelBatchEdit({
          codeClassifyOid: this.codeClassifyOid,
        }).then(res => {
          if (res) {
            func.downloadFileByBlobHandler(res);
            this.$message.success('下载成功,请查看!')
          }
        })
        return;
      }
    },
    onSuccess(res) {
      console.log(res)
      if (Object.keys(res.data).length === 0) {
        this.$message.success(this.title + "导入成功!");
        this.resetTable()
        this.dialogVisible = false;
        return;
      }
      if (res.data.fileOid) {
        const fileName = res.data.filePath.split("/").pop();
        this.$message.error("请下载错误信息文件进行查看!");
        downloadErrorFile({uuid: res.data.fileOid}).then((res2) => {
          this.$utilFunc.downloadFileByBlob(res2.data, fileName);
        });
      }
      if (res.data.redisUuid) {
        this.redisOid = res.data.redisUuid;
        importDataShow(res.data.redisUuid).then((res2) => {
          this.leftTree = res2.data.data.map((item) => {
            if (this.type === 'batchApplyCode') {
              return {
                cloNamesList: item.cloNamesList,
                oid: item.codeTemplateOid,
                codeClassifyOid: item.codeClassifyOid,
                codeRuleOid: item.codeRuleOid,
                name: item.codeRuleVO.name
              }
            } else {
              return {
                ...item.codeClassifyTemplateVO,
                cloNamesList: item.cloNamesList,
                codeClassifyOid: item.codeClassifyTemplateVO.codeclassifyoid
              };
            }
          });
          this.showVisible = true;
        });
      }
    },
    onError(err) {
      this.pageLoading.close();
      //console.log('onError')
    },
    uploadChange(file) {
      if (file.status === "success" || file.status === "error") {
        this.pageLoading.close();
        //console.log('uploadChange')
      }
    },
  },
  watch: {},
};
</script>
<style lang="scss" scoped>
ul {
  color: rgb(188, 188, 188);
  margin: 20px 0 20px 0;
  padding: 0;
  padding-left: 30px;
  list-style: none;
  li {
    margin-bottom: 5px;
    font-size: 12px;
  }
}
.radio_box {
  padding-left: 30px;
  margin: 0px 0 25px 0;
  display: flex;
  align-items: center;
  span {
    margin-right: 20px;
  }
}
.upload {
  padding-left: 30px;
  margin-top: 30px;
}
/deep/ .no-print {
  display: none !important;
}
</style>