{
boolean isSingleFail = false; //单条信息录入错误
Map<String, Pair<Object, Object>> baseValueMap = new LinkedHashMap<String, Pair<Object, Object>>();
Map<UUID, Pair<Object, Object>> extValueMap = new LinkedHashMap<UUID, Pair<Object, Object>>();
Data data = das.addData(templateId);
String title = ""; //标题
ErrorInfo errorInfo = null;
if(data == null){
return "";
}
for (Field field : allFields) {
if (excelValueNum.keySet().contains(field.getName())) {
String fieldName = field.getName();
//excel中存在该字段,从excel中获取内容
String cellContentString = "";
java.util.Date cellContentDate = null;
if(row.getCell(excelValueNum.get(fieldName))!=null)
{
if(row.getCell(excelValueNum.get(fieldName)).getCellType()==XSSFCell.CELL_TYPE_NUMERIC) //日期类型
{
cellContentDate = row.getCell(excelValueNum.get(fieldName)).getDateCellValue();
cellContentString = CynthiaUtil.getValue(row, excelValueNum.get(fieldName));
}else{
//其它类型
cellContentString = CynthiaUtil.getValue(row, excelValueNum.get(fieldName));
}
}else {
//判断该字段是否为必填
if (allNeedFields.contains(field.getName())) {
//必填为空,错误
isSingleFail = true;
errorInfo = new ErrorInfo() ;
errorInfo.setErrorDescription("必填字段为空");
errorInfo.setErrorRowNum(j);
errorInfo.setErrorColumnName(field.getName());
break;
}
}
if (field.getDataType()==DataType.dt_timestamp) { //处理日期类型
Date timeDate = null;
try {
timeDate = Date.valueOf(sdf.format(cellContentDate));
} catch (Exception e) {
}
if (timeDate == null) {
try {
timeDate = Date.valueOf(cellContentString);
} catch (Exception e) {
}
}
if (timeDate == null && allNeedFields.contains(fieldName)) {
// 日期类型错误 返回错误
isSingleFail = true;
errorInfo = new ErrorInfo() ;
errorInfo.setErrorDescription("日期类型错误");
errorInfo.setErrorRowNum(j);
errorInfo.setErrorColumnName(field.getName());
break;
}else {
if (timeDate != null) {
data.setDate(field.getId(), timeDate);
extValueMap.put(field.getId(), new Pair<Object, Object>(null, timeDate));
}
}
}else if (field.getType()==Type.t_selection) { //处理单选类型
Option option = field.getOption(cellContentString);
if (option == null) {
if (allNeedFields.contains(field)) { //为空必填
//错误,单选选项错误
isSingleFail = true;
errorInfo = new ErrorInfo() ;
errorInfo.setErrorDescription("单选选项错误");
errorInfo.setErrorRowNum(j);
errorInfo.setErrorColumnName(field.getName());
break;
}
}else {
data.setSingleSelection(field.getId(), option.getId());
extValueMap.put(field.getId(), new Pair<Object, Object>(null,option.getId()));
}
}else { //普通字段类型
data.setString(field.getId(), cellContentString);
extValueMap.put(field.getId(), new Pair<Object, Object>(null, cellContentString));
}
}
}//end foreach
if (isSingleFail) {
//记录错误 开始下一条数据录入
failCount ++;
errorInfoList.add(errorInfo);
continue;
}
//问题概述
if (excelValueNum.get("标题")!=null) {
title = CynthiaUtil.getValue(row, excelValueNum.get("标题"));
}
//标题
data.setTitle(title);
baseValueMap.put("title", new Pair<Object, Object>(null,title));
data.setObject("logCreateUser", addUser); //添加人
//正文
if (excelValueNum.get("正文")!=null) {
String content = CynthiaUtil.getValue(row, excelValueNum.get("正文"));
data.setDescription(content);
baseValueMap.put("description", new Pair<Object, Object>(null, content));
}
//状态
UUID statId = null;
if (excelValueNum.get("状态")!=null) {
String statIdStr = CynthiaUtil.getValue(row, excelValueNum.get("状态"));
if (statIdStr != null && statIdStr.length() >0) {
Stat stat = flow.getStat(statIdStr);
if (stat == null) {
failCount ++;
errorInfo = new ErrorInfo() ;
errorInfo.setErrorDescription("状态不存在");
errorInfo.setErrorRowNum(j);
errorInfo.setErrorColumnName("状态");
errorInfoList.add(errorInfo);
continue;
}else {
statId = stat.getId();
}
}
}
boolean isEndStat = isEndStat(flow, statId);
//指派人
if (excelValueNum.get("指派人")!=null && !isEndStat) {
String assignUser = CynthiaUtil.getValue(row, excelValueNum.get("指派人"));
if (userMap.get(assignUser) == null) {
UserInfo relatedUsers = das.queryUserInfoByUserName(assignUser);
if (relatedUsers == null) {
failCount ++;
errorInfo = new ErrorInfo() ;
errorInfo.setErrorDescription("指派人不存在");
errorInfo.setErrorRowNum(j);
errorInfo.setErrorColumnName("指派人");
errorInfoList.add(errorInfo);
continue;
}else {
userMap.put(assignUser, relatedUsers.getNickName());
}
}
if (statId != null) {
if(!isEndStat){
data.setAssignUsername(assignUser);
baseValueMap.put("assignUser", new Pair<Object, Object>(null, assignUser));
}
}
}
data.setObject("logActionId", null);
data.setStatusId(statId);
baseValueMap.put("statusId", new Pair<Object, Object>(null, statId));
data.setObject("logBaseValueMap", baseValueMap);
data.setObject("logExtValueMap", extValueMap);
//验证控制字段是否正确
String controlErrorString = checkDataControlValid(data, template);
if(controlErrorString != null && !controlErrorString.equals("")){
failCount ++;
errorInfo = new ErrorInfo() ;
errorInfo.setErrorDescription(controlErrorString);
errorInfo.setErrorRowNum(j);
errorInfoList.add(errorInfo);
continue;
}
if (!isSingleFail) {
Pair<ErrorCode, String> pair = das.modifyData(data);
if (pair.getFirst().equals(ErrorCode.success)) {
das.commitTranscation();
das.updateCache(DataAccessAction.delete, data.getId().getValue(), data);
}else {
isSingleFail = true;
errorInfo = new ErrorInfo();
errorInfo.setErrorDescription("数据库操作失败!");
errorInfo.setErrorRowNum(j);