刀具导入功能

zpl 4 years ago
parent b39ace89f3
commit 3778d070ff

@ -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<Tool> {
*/
void deleteInspectionPlanByHandle(String tool);
/**
*
* sqlType
* 1update
* 2insert
* 3both
* @param jsonObject
* @param sqlType
*/
String saveOrUpdateToolByImport(JSONObject jsonObject, String sqlType);
}

@ -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<ToolMapper, Tool> implements To
@Autowired
private ToolMapper toolMapper;
@Autowired
private ToolService toolService;
@Override
public IPage<Tool> selectPage(FrontPage<Tool> frontPage, Tool tool) {
@ -91,5 +94,85 @@ public class ToolServiceImpl extends ServiceImpl<ToolMapper, Tool> 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 "导入成功";
}
}

@ -28,6 +28,8 @@ public class HandlerFactory {
return new InspectionItemHandler();
case "messageType" :
//return new MessageTypeHandler();
case "tool" :
return new ToolHandler();
}
return null;

@ -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<JSONObject> 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<String, String> getHeaders() {
return MasterObjectDefine.getHeadsMapping("TOOL");
}
public RowVisitor<JSONObject> getRowVisitor(String site, String mode, StringBuffer buffer, Integer[] failedNumber) {
return new RowVisitor<JSONObject>() {
@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;
}
};
}
}

@ -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;
}

Loading…
Cancel
Save