diff --git a/common/src/main/java/com/foreverwin/mesnac/common/service/ToolService.java b/common/src/main/java/com/foreverwin/mesnac/common/service/ToolService.java index a49fe251..45cdd70e 100644 --- a/common/src/main/java/com/foreverwin/mesnac/common/service/ToolService.java +++ b/common/src/main/java/com/foreverwin/mesnac/common/service/ToolService.java @@ -1,5 +1,6 @@ package com.foreverwin.mesnac.common.service; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.foreverwin.mesnac.common.model.Tool; @@ -45,4 +46,16 @@ public interface ToolService extends IService { */ void deleteInspectionPlanByHandle(String tool); + /** + * 插入或更新导入的刀具数据 + * sqlType: + * 1、update + * 2、insert + * 3、both + * @param jsonObject + * @param sqlType + */ + String saveOrUpdateToolByImport(JSONObject jsonObject, String sqlType); + + } \ No newline at end of file diff --git a/common/src/main/java/com/foreverwin/mesnac/common/service/impl/ToolServiceImpl.java b/common/src/main/java/com/foreverwin/mesnac/common/service/impl/ToolServiceImpl.java index bf8ff9cc..e1e77be6 100644 --- a/common/src/main/java/com/foreverwin/mesnac/common/service/impl/ToolServiceImpl.java +++ b/common/src/main/java/com/foreverwin/mesnac/common/service/impl/ToolServiceImpl.java @@ -1,5 +1,6 @@ package com.foreverwin.mesnac.common.service.impl; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -32,6 +33,8 @@ public class ToolServiceImpl extends ServiceImpl implements To @Autowired private ToolMapper toolMapper; + @Autowired + private ToolService toolService; @Override public IPage selectPage(FrontPage frontPage, Tool tool) { @@ -91,5 +94,85 @@ public class ToolServiceImpl extends ServiceImpl implements To toolMapper.updateById(byId); } + @Override + public String saveOrUpdateToolByImport(JSONObject json, String sqlType) { + String user = CommonMethods.getUser(); + Tool target = new Tool(); + String site = json.getString("site").trim(); + String tool = json.getString("tool").trim(); + String handle = HandleEnum.TOOL.getHandle(site, tool).trim(); + String description = json.getString("description").trim(); + String specification = json.getString("specification").trim(); + String brand = json.getString("brand").trim(); + String remark = json.getString("remark").trim(); + if (tool==null || tool != ""){ + return "刀具编号不能为空"; + } + switch (sqlType){ + case "insert": + target.setTool(tool); + target.setHandle(handle); + target.setDescription(description); + target.setSpecification(specification); + target.setBrand(brand); + target.setCreateUser(user); + target.setCreatedDateTime(LocalDateTime.now()); + toolMapper.insert(target); + break; + case "update": + Tool byId = toolMapper.selectById(handle); + if (byId == null){ + return "更新失败,刀具编号不存在!"; + } + if (description != ""){ + byId.setDescription(description); + } + if (specification != ""){ + byId.setSpecification(specification); + } + if (brand != ""){ + byId.setBrand(brand); + } + if (remark != ""){ + byId.setRemark(remark); + } + toolMapper.updateById(byId); + break; + case "both": + Tool selId = toolMapper.selectById(handle); + if (selId != null){ + if (description != ""){ + selId.setDescription(description); + } + if (specification != ""){ + selId.setSpecification(specification); + } + if (brand != ""){ + selId.setBrand(brand); + } + if (remark != ""){ + selId.setRemark(remark); + } + selId.setModifyUser(user); + selId.setModifiedDateTime(LocalDateTime.now()); + toolMapper.updateById(selId); + break; + } + target.setTool(tool); + target.setHandle(handle); + target.setDescription(description); + target.setSpecification(specification); + target.setBrand(brand); + target.setCreateUser(user); + target.setCreatedDateTime(LocalDateTime.now()); + toolMapper.insert(target); + break; + default: + return "更新/导入类型未找到!"; + } + return "导入成功"; + } + + } \ No newline at end of file diff --git a/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/handler/HandlerFactory.java b/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/handler/HandlerFactory.java index 244cd285..c0110de6 100644 --- a/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/handler/HandlerFactory.java +++ b/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/handler/HandlerFactory.java @@ -28,6 +28,8 @@ public class HandlerFactory { return new InspectionItemHandler(); case "messageType" : //return new MessageTypeHandler(); + case "tool" : + return new ToolHandler(); } return null; diff --git a/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/handler/ToolHandler.java b/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/handler/ToolHandler.java new file mode 100644 index 00000000..61174ce6 --- /dev/null +++ b/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/handler/ToolHandler.java @@ -0,0 +1,80 @@ +package com.foreverwin.mesnac.dataimport.handler; + +import com.alibaba.fastjson.JSONObject; +import com.foreverwin.mesnac.common.service.ToolService; +import com.foreverwin.mesnac.dataimport.handler.base.BaseHandler; +import com.foreverwin.mesnac.dataimport.reader.FileReader; +import com.foreverwin.mesnac.dataimport.reader.FileReaderBuilder; +import com.foreverwin.mesnac.dataimport.reader.RowVisitor; +import com.foreverwin.mesnac.dataimport.service.MasterObjectDefine; +import com.foreverwin.modular.core.exception.BusinessException; +import com.foreverwin.modular.core.util.I18nUtil; +import org.springframework.beans.factory.annotation.Autowired; + +import java.io.InputStream; +import java.util.Map; + +public class ToolHandler extends BaseHandler { + + @Autowired + private ToolService toolService; + + + @Override + public String importFile(String site, InputStream inputStream, String fileType, String mode) throws Exception { + int row = 0; + StringBuffer buffer = new StringBuffer(); + Integer[] failedNumber = new Integer[]{0}; + + try { + FileReader fileReader = new FileReaderBuilder().build(fileType); + RowVisitor visitor = getRowVisitor(site, mode, buffer, failedNumber); + row = fileReader.visitor(visitor).read(inputStream, getHeaders()); + } catch (Exception e) { + buffer.append(e.getMessage() + "\n"); + } + + if (buffer.length() > 0) { + buffer.insert(0, I18nUtil.getI18nText("MaterData.import.Summary", new Object[]{row + failedNumber[0], row, failedNumber[0]}) + "\n"); + throw BusinessException.build(buffer.toString()); + } + buffer.insert(0, I18nUtil.getI18nText("MaterData.import.Summary", new Object[]{row + failedNumber[0], row, failedNumber[0]}) + "\n"); + + return buffer.toString(); + } + + + @Override + public Map getHeaders() { + + return MasterObjectDefine.getHeadsMapping("TOOL"); + } + + public RowVisitor getRowVisitor(String site, String mode, StringBuffer buffer, Integer[] failedNumber) { + + return new RowVisitor() { + @Override + public int visit(long index, JSONObject jsonObject) { + Object[] params = new Object[10]; + params[0] = index; + params[1] = jsonObject.getString("tool"); + jsonObject.put("site", site); + try { + String msg = toolService.saveOrUpdateToolByImport(jsonObject, mode); + buffer.append("第" + index + "行:" + msg + "\n"); + return 1; + } catch (Exception e) { + params[2] = e.getMessage(); + failedNumber[0]++; + buffer.append("第" + index + "行:" + I18nUtil.getI18nText("MaterData.import.update.fail", params) + "\n"); + } + return 0; + } + + @Override + public String[] getHeader() { + return null; + } + }; + } +} diff --git a/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/service/MasterObjectDefine.java b/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/service/MasterObjectDefine.java index 8f768afa..bd9fc385 100644 --- a/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/service/MasterObjectDefine.java +++ b/dataimport/src/main/java/com/foreverwin/mesnac/dataimport/service/MasterObjectDefine.java @@ -164,6 +164,18 @@ public class MasterObjectDefine { cvsHeadsMapping.put("isRequired","是否重要项目"); cvsHeadsMapping.put("checkNum","收集次数"); break; + + //刀具 + case "TOOL": + cvsHeadsMapping = new HashMap<>(); + + cvsHeadsMapping.put("tool","刀具"); + cvsHeadsMapping.put("description","描述"); + cvsHeadsMapping.put("specification","规格"); + cvsHeadsMapping.put("brand","品牌"); + cvsHeadsMapping.put("remark","备注"); + break; + default: break; }