Explorar el Código

风机类型检查分值表导入功能

wanglt hace 3 meses
padre
commit
d241987525

+ 95 - 4
ygtx-gxt/src/main/java/com/ygtx/gxt/controller/GxtFanInspectionScoreController.java

@@ -3,7 +3,9 @@ package com.ygtx.gxt.controller;
 import java.util.List;
 import javax.servlet.http.HttpServletResponse;
 
+import com.github.pagehelper.util.StringUtil;
 import com.ygtx.common.annotation.Excel;
+import com.ygtx.common.core.domain.entity.SysDictData;
 import com.ygtx.gxt.service.IGxtEquipmentService;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,7 +29,11 @@ import com.ygtx.common.core.page.TableDataInfo;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiImplicitParam;
-import io.swagger.annotations.ApiImplicitParams;
+import com.ygtx.gxt.domain.vo.GxtFanInspectionScoreVo;
+import com.ygtx.system.service.ISysDictDataService;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.ArrayList;
 
 /**
  * 风机类型检查分值Controller
@@ -46,6 +52,9 @@ public class GxtFanInspectionScoreController extends BaseController
     @Autowired
     private IGxtEquipmentService gxtEquipmentService;
 
+    @Autowired
+    private ISysDictDataService sysDictDataService;
+
     /**
      * 查询风机类型检查分值列表
      */
@@ -70,13 +79,17 @@ public class GxtFanInspectionScoreController extends BaseController
                        @RequestParam(value = "exportFields", required = false) String exportFieldsStr)
     {
         List<GxtFanInspectionScore> list = gxtFanInspectionScoreService.selectGxtFanInspectionScoreList(gxtFanInspectionScore);
-        ExcelUtil<GxtFanInspectionScore> util = new ExcelUtil<GxtFanInspectionScore>(GxtFanInspectionScore.class);
+        
+        // 转换为VO类并填充维保类型名称
+        List<GxtFanInspectionScoreVo> voList = convertToVoList(list);
+        
+        ExcelUtil<GxtFanInspectionScoreVo> util = new ExcelUtil<GxtFanInspectionScoreVo>(GxtFanInspectionScoreVo.class);
         if (exportFieldsStr != null && !exportFieldsStr.isEmpty()) {
             // 将字符串分割成数组
             String[] exportFields = exportFieldsStr.split(",");
             util.showColumn(exportFields);
         }
-        util.exportExcel(response, list, "风机类型检查分值数据");
+        util.exportExcel(response, voList, "风机类型检查分值数据");
     }
 
     /**
@@ -85,7 +98,7 @@ public class GxtFanInspectionScoreController extends BaseController
     @GetMapping("/exportFields")
     @ApiOperation("获取风机类型检查分值导出字段列表")
     public AjaxResult getExportFields() {
-        ExcelUtil<GxtFanInspectionScore> util = new ExcelUtil<GxtFanInspectionScore>(GxtFanInspectionScore.class);
+        ExcelUtil<GxtFanInspectionScoreVo> util = new ExcelUtil<GxtFanInspectionScoreVo>(GxtFanInspectionScoreVo.class);
         util.init(null, "风机类型检查分值", "", Excel.Type.EXPORT);
         return success(util.getExportFields());
     }
@@ -161,4 +174,82 @@ public class GxtFanInspectionScoreController extends BaseController
         List<GxtFanInspectionScore> list = gxtFanInspectionScoreService.selectGxtFanInspectionScoreByFanType(fanType);
         return AjaxResult.success(list);
     }
+    
+    /**
+     * 将实体类列表转换为VO类列表,并填充维保类型名称
+     */
+    private List<GxtFanInspectionScoreVo> convertToVoList(List<GxtFanInspectionScore> list) {
+        List<GxtFanInspectionScoreVo> voList = new ArrayList<>();
+        
+        for (GxtFanInspectionScore entity : list) {
+            GxtFanInspectionScoreVo vo = new GxtFanInspectionScoreVo();
+            
+            // 手动复制属性
+            vo.setId(entity.getId());
+            vo.setFanType(entity.getFanType());
+            vo.setScore(entity.getScore());
+            vo.setRemark(entity.getRemark());
+            vo.setCreateBy(entity.getCreateBy());
+            vo.setCreateTime(entity.getCreateTime());
+            vo.setUpdateBy(entity.getUpdateBy());
+            vo.setUpdateTime(entity.getUpdateTime());
+            
+            // 根据inspectionTypeId获取维保类型名称
+            if (entity.getInspectionTypeId() != null) {
+                String inspectionTypeName = sysDictDataService.selectDictLabel("gxt_inspection_type", entity.getInspectionTypeId().toString());
+                vo.setInspectionTypeName(inspectionTypeName != null ? inspectionTypeName : "");
+            }
+            
+            voList.add(vo);
+        }
+        
+        return voList;
+    }
+    
+    /**
+     * 导入风机类型检查分值数据
+     */
+    @PreAuthorize("@ss.hasPermi('gxt:scorefaninspection:import')")
+    @Log(title = "风机类型检查分值", businessType = BusinessType.IMPORT)
+    @PostMapping("/importData")
+    public AjaxResult importData(MultipartFile file, @RequestParam(defaultValue = "false") boolean updateSupport) throws Exception
+    {
+        ExcelUtil<GxtFanInspectionScore> util = new ExcelUtil<GxtFanInspectionScore>(GxtFanInspectionScore.class);
+        List<GxtFanInspectionScore> fanInspectionScoreList = util.importExcel(file.getInputStream());
+        if (!fanInspectionScoreList.isEmpty()) {
+            SysDictData dictData = new SysDictData();
+            dictData.setDictType("gxt_inspection_type");
+            dictData.setStatus("0");
+            List<SysDictData> dataList = sysDictDataService.selectDictDataList(dictData);
+            if (dataList.isEmpty()) {
+                return error("数据库风机类型为空");
+            }
+            for (GxtFanInspectionScore score:fanInspectionScoreList) {
+                score.setStatus(0);
+                String fanName = score.getInspectionTypeName();
+                if (StringUtil.isNotEmpty(fanName)) {
+                    for (SysDictData data:dataList) {
+                        if (data.getDictLabel().equals(fanName)) {
+                            score.setInspectionTypeId(Integer.valueOf(data.getDictValue()));
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+        String operName = getUsername();
+        String message = gxtFanInspectionScoreService.importFanInspectionScoreList(fanInspectionScoreList, updateSupport, operName);
+        return success(message);
+    }
+    
+    /**
+     * 下载风机类型检查分值导入模板
+     */
+    @PreAuthorize("@ss.hasPermi('gxt:scorefaninspection:import')")
+    @PostMapping("/importTemplate")
+    public void importTemplate(HttpServletResponse response)
+    {
+        ExcelUtil<GxtFanInspectionScoreVo> util = new ExcelUtil<GxtFanInspectionScoreVo>(GxtFanInspectionScoreVo.class);
+        util.importTemplateExcel(response, "风机类型检查分值数据");
+    }
 }

+ 14 - 0
ygtx-gxt/src/main/java/com/ygtx/gxt/domain/GxtFanInspectionScore.java

@@ -51,6 +51,10 @@ public class GxtFanInspectionScore extends BaseEntity
     /** 备注 */
     @Excel(name = "备注")
     private String remark;
+    
+    /** 维保类型名称(导入时使用) */
+    @Excel(name = "维保类型")
+    private String inspectionTypeName;
 
     public void setId(Long id) 
     {
@@ -142,6 +146,16 @@ public class GxtFanInspectionScore extends BaseEntity
     {
         return remark;
     }
+    
+    public void setInspectionTypeName(String inspectionTypeName)
+    {
+        this.inspectionTypeName = inspectionTypeName;
+    }
+    
+    public String getInspectionTypeName()
+    {
+        return inspectionTypeName;
+    }
 
     @Override
     public String toString() {

+ 109 - 0
ygtx-gxt/src/main/java/com/ygtx/gxt/domain/vo/GxtFanInspectionScoreVo.java

@@ -0,0 +1,109 @@
+package com.ygtx.gxt.domain.vo;
+
+import com.ygtx.common.annotation.Excel;
+import com.ygtx.common.core.domain.BaseEntity;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+/**
+ * 风机类型检查分值视图对象,用于导出时显示维保类型名称
+ * 
+ * @author ruoyi
+ * @date 2025-01-20
+ */
+public class GxtFanInspectionScoreVo extends BaseEntity
+{
+    private static final long serialVersionUID = 1L;
+
+    /** 主键ID */
+    private Long id;
+
+    /** 风机类型 */
+    @Excel(name = "机型")
+    private String fanType;
+
+    /** 维保类型名称 */
+    @Excel(name = "维保类型")
+    private String inspectionTypeName;
+
+    /** 对应分值 */
+    @Excel(name = "分值")
+    private BigDecimal score;
+
+    /** 创建者 */
+    private String createBy;
+
+    /** 创建时间 */
+    private Date createTime;
+
+    /** 更新者 */
+    private String updateBy;
+
+    /** 更新时间 */
+    private Date updateTime;
+    
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getFanType() {
+        return fanType;
+    }
+
+    public void setFanType(String fanType) {
+        this.fanType = fanType;
+    }
+
+    public String getInspectionTypeName() {
+        return inspectionTypeName;
+    }
+
+    public void setInspectionTypeName(String inspectionTypeName) {
+        this.inspectionTypeName = inspectionTypeName;
+    }
+
+    public BigDecimal getScore() {
+        return score;
+    }
+
+    public void setScore(BigDecimal score) {
+        this.score = score;
+    }
+
+    public String getCreateBy() {
+        return createBy;
+    }
+
+    public void setCreateBy(String createBy) {
+        this.createBy = createBy;
+    }
+
+    public Date getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Date createTime) {
+        this.createTime = createTime;
+    }
+
+    public String getUpdateBy() {
+        return updateBy;
+    }
+
+    public void setUpdateBy(String updateBy) {
+        this.updateBy = updateBy;
+    }
+
+    public Date getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(Date updateTime) {
+        this.updateTime = updateTime;
+    }
+}

+ 10 - 0
ygtx-gxt/src/main/java/com/ygtx/gxt/service/IGxtFanInspectionScoreService.java

@@ -66,4 +66,14 @@ public interface IGxtFanInspectionScoreService
      * @return 结果
      */
     public int deleteGxtFanInspectionScoreById(Long id);
+    
+    /**
+     * 批量导入风机类型检查分值
+     * 
+     * @param fanInspectionScoreList 风机类型检查分值列表
+     * @param updateSupport 是否更新支持,如果已存在,则进行更新数据
+     * @param operName 操作用户名
+     * @return 结果
+     */
+    public String importFanInspectionScoreList(List<GxtFanInspectionScore> fanInspectionScoreList, Boolean updateSupport, String operName);
 }

+ 97 - 0
ygtx-gxt/src/main/java/com/ygtx/gxt/service/impl/GxtFanInspectionScoreServiceImpl.java

@@ -6,6 +6,8 @@ import org.springframework.stereotype.Service;
 import com.ygtx.gxt.mapper.GxtFanInspectionScoreMapper;
 import com.ygtx.gxt.domain.GxtFanInspectionScore;
 import com.ygtx.gxt.service.IGxtFanInspectionScoreService;
+import com.ygtx.common.exception.ServiceException;
+import com.ygtx.common.utils.StringUtils;
 
 /**
  * 风机类型检查分值Service业务层处理
@@ -102,4 +104,99 @@ public class GxtFanInspectionScoreServiceImpl implements IGxtFanInspectionScoreS
     {
         return gxtFanInspectionScoreMapper.deleteGxtFanInspectionScoreById(id);
     }
+    
+    /**
+     * 批量导入风机类型检查分值
+     * 
+     * @param fanInspectionScoreList 风机类型检查分值列表
+     * @param updateSupport 是否更新支持,如果已存在,则进行更新数据
+     * @param operName 操作用户名
+     * @return 结果
+     */
+    @Override
+    public String importFanInspectionScoreList(List<GxtFanInspectionScore> fanInspectionScoreList, Boolean updateSupport, String operName)
+    {
+        if (StringUtils.isNull(fanInspectionScoreList) || fanInspectionScoreList.size() == 0)
+        {
+            throw new ServiceException("导入风机类型检查分值数据不能为空!");
+        }
+        int successNum = 0;
+        int failureNum = 0;
+        StringBuilder successMsg = new StringBuilder();
+        StringBuilder failureMsg = new StringBuilder();
+        for (GxtFanInspectionScore fanInspectionScore : fanInspectionScoreList)
+        {
+            try
+            {
+                // 校验必填字段
+                if (StringUtils.isEmpty(fanInspectionScore.getFanType()))
+                {
+                    throw new ServiceException("机型不能为空");
+                }
+                if (fanInspectionScore.getScore()==null)
+                {
+                    throw new ServiceException("分值不能为空");
+                }
+                if (fanInspectionScore.getInspectionTypeId()==null) {
+                    throw new ServiceException("检查类型不存在");
+                }
+                
+                // 验证是否存在这个数据(根据fanType和inspectionTypeId查重)
+                GxtFanInspectionScore existsData = new GxtFanInspectionScore();
+                existsData.setFanType(fanInspectionScore.getFanType());
+                existsData.setInspectionTypeId(fanInspectionScore.getInspectionTypeId());
+                List<GxtFanInspectionScore> list = gxtFanInspectionScoreMapper.selectGxtFanInspectionScoreList(existsData);
+                
+                if (list.isEmpty())
+                {
+                    // 没有重复数据,直接新增
+                    fanInspectionScore.setCreateBy(operName);
+                    insertGxtFanInspectionScore(fanInspectionScore);
+                    successMsg.append("<br/>" + successNum + "、机型 " + fanInspectionScore.getFanType() + " 维保类型ID " + 
+                        fanInspectionScore.getInspectionTypeId() + " 导入成功");
+                    successNum++;
+                } else if (list.size()>1) {
+                    failureMsg.append("<br/>" + failureNum + "、机型 " + fanInspectionScore.getFanType() + " 维保类型ID " +
+                            fanInspectionScore.getInspectionTypeId() + " 存在多条数据,请处理后再导入");
+                    failureNum++;
+                } else
+                {
+                    if (Boolean.TRUE.equals(updateSupport))
+                    {
+                        // 存在相同数据,且支持更新,则更新
+                        fanInspectionScore.setId(list.get(0).getId());
+                        fanInspectionScore.setUpdateBy(operName);
+                        updateGxtFanInspectionScore(fanInspectionScore);
+                        successMsg.append("<br/>" + successNum + "、机型 " + fanInspectionScore.getFanType() + " 维保类型ID " + 
+                            fanInspectionScore.getInspectionTypeId() + " 更新成功");
+                        successNum++;
+                    }
+                    else
+                    {
+                        failureMsg.append("<br/>" + failureNum + "、机型 " + fanInspectionScore.getFanType() + " 维保类型ID " + 
+                            fanInspectionScore.getInspectionTypeId() + " 已存在");
+                        failureNum++;
+                    }
+                }
+            }
+            catch (Exception e)
+            {
+                failureMsg.append("<br/>" + failureNum + "、导入失败:" + e.getMessage());
+                failureNum++;
+            }
+        }
+        StringBuilder resultMsg = new StringBuilder();
+        resultMsg.append("已成功导入 " + successNum + " 条数据");
+        if (successNum > 0)
+        {
+            resultMsg.append(",成功导入数据如下:");
+            resultMsg.append(successMsg);
+        }
+        if (failureNum > 0)
+        {
+            resultMsg.append("<br/>导入失败 " + failureNum + " 条数据,错误如下:");
+            resultMsg.append(failureMsg);
+        }
+        return resultMsg.toString();
+    }
 }

+ 92 - 2
ygtx-ui/src/views/gxt/fanInspectionScore/index.vue

@@ -78,6 +78,9 @@
           v-hasPermi="['gxt:scorefaninspection:remove']"
         >删除</el-button>
       </el-col>
+      <el-col :span="1.5">
+        <el-button type="info" icon="Upload" @click="handleImport" v-hasPermi="['gxt:scorefaninspection:import']">导入</el-button>
+      </el-col>
       <el-col :span="1.5">
         <el-button
           type="warning"
@@ -188,12 +191,35 @@
         </div>
       </template>
     </el-dialog>
+
+    <!-- 导入对话框 -->
+    <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
+      <el-upload ref="uploadRef" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + (upload.updateSupport ? 'true' : 'false')" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :on-change="handleFileChange" :on-remove="handleFileRemove" :auto-upload="false" drag>
+        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
+        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
+        <template #tip>
+          <div class="el-upload__tip text-center">
+            <div class="el-upload__tip">
+              <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据
+            </div>
+            <span>仅允许导入xls、xlsx格式文件。</span>
+            <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
+          </div>
+        </template>
+      </el-upload>
+      <template #footer>
+        <div class="dialog-footer">
+          <el-button type="primary" @click="submitFileForm">确 定</el-button>
+          <el-button @click="upload.open = false">取 消</el-button>
+        </div>
+      </template>
+    </el-dialog>
     
     <!-- 字段选择对话框 -->
     <el-dialog title="选择导出字段" v-model="showExportFieldsDialog" width="600px" append-to-body>
       <div style="position: absolute;bottom: 65px;right: 38%;">
-        <el-button type="primary" icon="upload" size="mini" style="width:5px;" @click="upDown('up')" /><br/>
-        <el-button type="primary" icon="download" size="mini" style="margin-top:3px;width:5px;" @click="upDown('down')" />
+        <el-button type="primary" icon="upload" size="small" style="width:5px;" @click="upDown('up')" /><br/>
+        <el-button type="primary" icon="download" size="small" style="margin-top:3px;width:5px;" @click="upDown('down')" />
       </div>
       <el-transfer 
         v-model="exportFieldsSelected" 
@@ -216,6 +242,7 @@
 </template>
 
 <script setup name="FanInspectionScore">
+import { getToken } from "@/utils/auth"
 import { listFanInspectionScore, getFanInspectionScore, delFanInspectionScore, addFanInspectionScore, updateFanInspectionScore, listFanTypes, getExportFields } from "@/api/gxt/fanInspectionScore";
 import { getCurrentInstance, reactive, ref, toRefs, onMounted, watch } from "vue";
 
@@ -233,6 +260,22 @@ const total = ref(0);
 const title = ref("");
 const fanTypeOptions = ref([]);
 
+/*** 导入参数 */
+const upload = reactive({
+  // 是否显示弹出层(导入)
+  open: false,
+  // 弹出层标题(导入)
+  title: "",
+  // 是否禁用上传
+  isUploading: false,
+  // 是否更新已经存在的数据
+  updateSupport: true,
+  // 设置上传的请求头部
+  headers: { Authorization: "Bearer " + getToken() },
+  // 上传的地址
+  url: import.meta.env.VITE_APP_BASE_API + "/gxt/fanInspectionScore/importData"
+})
+
 // 导出字段选择相关
 const showExportFieldsDialog = ref(false);
 const exportFieldsSelected = ref([]);
@@ -393,6 +436,53 @@ function handleDelete(row) {
   }).catch(() => {});
 }
 
+/** 导入按钮操作 */
+function handleImport() {
+  upload.title = "风机类型检查分值导入"
+  upload.open = true
+  upload.selectedFile = null
+}
+
+/** 下载模板操作 */
+function importTemplate() {
+  proxy.download("gxt/fanInspectionScore/importTemplate", {
+  }, `fanInspection_score_template_${new Date().getTime()}.xlsx`)
+}
+
+/**文件上传中处理 */
+const handleFileUploadProgress = (event, file, fileList) => {
+  upload.isUploading = true
+}
+
+/** 文件选择处理 */
+const handleFileChange = (file, fileList) => {
+  upload.selectedFile = file
+}
+
+/** 文件删除处理 */
+const handleFileRemove = (file, fileList) => {
+  upload.selectedFile = null
+}
+
+/** 文件上传成功处理 */
+const handleFileSuccess = (response, file, fileList) => {
+  upload.open = false
+  upload.isUploading = false
+  proxy.$refs["uploadRef"].handleRemove(file)
+  proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true })
+  getList()
+}
+
+/** 提交上传文件 */
+function submitFileForm() {
+  const file = upload.selectedFile
+  if (!file || file.length === 0 || !file.name.toLowerCase().endsWith('.xls') && !file.name.toLowerCase().endsWith('.xlsx')) {
+    proxy.$modal.msgError("请选择后缀为 “xls”或“xlsx”的文件。")
+    return
+  }
+  proxy.$refs["uploadRef"].submit()
+}
+
 /** 导出按钮操作 */
 function handleExport() {
   // 获取导出字段数据并处理默认选中