Commit 563457bb by kangxiaoshan

合同上传

parent 1f63568c
...@@ -401,18 +401,15 @@ public class ContractController { ...@@ -401,18 +401,15 @@ public class ContractController {
@ResponseBody @ResponseBody
public ResultModel changesDel(@PathVariable String platform, @CurrentAccount User loginAccount, public ResultModel changesDel(@PathVariable String platform, @CurrentAccount User loginAccount,
String id, String type, HttpServletRequest request) { String id, String type, HttpServletRequest request) {
return ResultModel.OK(service.changesDel(platform, id, type, IPAddrUtil.getIpAddrNew(request), loginAccount)); return ResultModel.OK(service.changesDel(platform, id, type, IPAddrUtil.getIpAddrNew(request), loginAccount));
} }
@RequestMapping("/upload") @RequestMapping(value = "/upload",method = RequestMethod.POST)
@ResponseBody
public ResultModel fileUpload2(@RequestParam("file") MultipartFile file, @PathVariable String platform) { public ResultModel fileUpload2(@RequestParam("file") MultipartFile file, @PathVariable String platform) {
return service.uploadBatchInfo(file,platform); return service.uploadBatchInfo(file,platform);
} }
......
package common.jdbc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class ContractDao {
@Autowired
JdbcTemplate jdbcTemplate;
public void batchSaveContract(String sql, List<Object[]> args) {
jdbcTemplate.batchUpdate(sql, args);
}
}
package common.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
@Component
public class TransactionUtils {
@Autowired
@Qualifier(value = "jdbcTransactionManager")
private DataSourceTransactionManager jdbcTransactionManager;
// 开启事务
public TransactionStatus begin() {
TransactionStatus transaction = jdbcTransactionManager.getTransaction(new DefaultTransactionAttribute());
return transaction;
}
// 提交事务
public void commit(TransactionStatus transaction) {
jdbcTransactionManager.commit(transaction);
}
// 回滚事务
public void rollback(TransactionStatus transaction) {
jdbcTransactionManager.rollback(transaction);
}
}
...@@ -2,10 +2,12 @@ package common.service.impl; ...@@ -2,10 +2,12 @@ package common.service.impl;
import adi.model.ADIUser; import adi.model.ADIUser;
import adi.service.ADIAccountService; import adi.service.ADIAccountService;
import common.jdbc.ContractDao;
import common.model.*; import common.model.*;
import common.repository.*; import common.repository.*;
import common.service.AuthService; import common.service.AuthService;
import common.service.ContractService; import common.service.ContractService;
import common.service.TransactionUtils;
import common.service.UserService; import common.service.UserService;
import dic.ContractStatusEnum; import dic.ContractStatusEnum;
import dic.ContractTypeEnum; import dic.ContractTypeEnum;
...@@ -15,9 +17,14 @@ import org.apache.poi.ss.usermodel.*; ...@@ -15,9 +17,14 @@ import org.apache.poi.ss.usermodel.*;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import tkio.model.Account; import tkio.model.Account;
...@@ -33,6 +40,8 @@ import userio.service.IOAccountService; ...@@ -33,6 +40,8 @@ import userio.service.IOAccountService;
import util.*; import util.*;
import util.DateUtil; import util.DateUtil;
import javax.sql.DataSource;
import javax.transaction.Transaction;
import java.beans.BeanInfo; import java.beans.BeanInfo;
import java.beans.IntrospectionException; import java.beans.IntrospectionException;
import java.beans.Introspector; import java.beans.Introspector;
...@@ -56,7 +65,11 @@ public class ContractServiceImpl implements ContractService { ...@@ -56,7 +65,11 @@ public class ContractServiceImpl implements ContractService {
put("0", "首次签约"); put("0", "首次签约");
put("1", "续约"); put("1", "续约");
put("2", "合同变更"); put("2", "合同变更");
}};
private static final Map<String, String> CONTRACT_TYPE_NAME = new HashMap() {{
put("首次签约", "0");
put("续约", "1");
put("合同变更", "2");
}}; }};
public static final Map<String, String> FUNCTION_TYPE = new HashMap() {{ public static final Map<String, String> FUNCTION_TYPE = new HashMap() {{
...@@ -66,6 +79,21 @@ public class ContractServiceImpl implements ContractService { ...@@ -66,6 +79,21 @@ public class ContractServiceImpl implements ContractService {
put("4", "合同管理"); put("4", "合同管理");
}}; }};
private static final Map<String, String> SHEET_TITLE = new HashMap() {{
put("我方签约主体", "my_body_name");
put("客户签约主体", "customer_body");
put("行业分类", "trade_type");
put("合同金额", "money");
put("客户主账号", "email");
put("合同编号", "contract_code");
put("签约类型", "contract_type");
put("套餐", "price_level");
put("合同类型", "type_id");
put("合同开始日期", "start_date");
put("合同结束日期", "end_date");
put("签约销售", "sale");
}};
public static final String UPLOAD_TITLE = "我方签约主体,客户签约主体,行业分类,合同开始日期,合同结束日期," + public static final String UPLOAD_TITLE = "我方签约主体,客户签约主体,行业分类,合同开始日期,合同结束日期," +
"签约销售,客户主账号,合同编号,签约类型,套餐,合同金额"; "签约销售,客户主账号,合同编号,签约类型,套餐,合同金额";
...@@ -76,6 +104,8 @@ public class ContractServiceImpl implements ContractService { ...@@ -76,6 +104,8 @@ public class ContractServiceImpl implements ContractService {
put("ADI", "adi"); put("ADI", "adi");
}}; }};
Logger logger = LoggerFactory.getLogger(ContractServiceImpl.class);
@Autowired @Autowired
ContractRepository contractRepository; ContractRepository contractRepository;
@Autowired @Autowired
...@@ -145,6 +175,10 @@ public class ContractServiceImpl implements ContractService { ...@@ -145,6 +175,10 @@ public class ContractServiceImpl implements ContractService {
@Autowired @Autowired
JdbcTemplate jdbcTemplate; JdbcTemplate jdbcTemplate;
@Autowired
TransactionUtils transactionUtils;
@Override @Override
public Map<String, Object> checkAccount(String email, String platfrom) { public Map<String, Object> checkAccount(String email, String platfrom) {
...@@ -2049,18 +2083,15 @@ public class ContractServiceImpl implements ContractService { ...@@ -2049,18 +2083,15 @@ public class ContractServiceImpl implements ContractService {
// while (sheetIter.hasNext()) { // while (sheetIter.hasNext()) {
// Sheet sheet = sheetIter.next(); // Sheet sheet = sheetIter.next();
// } // }
return saveDataBySheet(workbook.getSheetAt(0), row_length);
saveDataBySheet(workbook.getSheetAt(0), row_length);
} catch (Exception ex) { } catch (Exception ex) {
logger.error("批量上传合同错误", ex);
}
return ResultModel.ERROR(ResultStatus.NONE_FILE_EXIST); return ResultModel.ERROR(ResultStatus.NONE_FILE_EXIST);
} }
return null;
}
public ResultModel saveDataBySheet(Sheet sheet, int row_length) { public ResultModel saveDataBySheet(Sheet sheet, int row_length) {
String sheetName = sheet.getSheetName(); String sheetName = sheet.getSheetName();
if (!SHEET_NAMES.containsKey(sheetName)) { if (!SHEET_NAMES.containsKey(sheetName)) {
...@@ -2087,32 +2118,33 @@ public class ContractServiceImpl implements ContractService { ...@@ -2087,32 +2118,33 @@ public class ContractServiceImpl implements ContractService {
Row row = sheet.getRow(0); Row row = sheet.getRow(0);
if (!UPLOAD_TITLE.equals(parseRowData(row, row_length, null))) { List<String> titleKey = new ArrayList<>();
if (!parseRowData(row, row_length, null, titleKey)) {
return ResultModel.ERROR(ResultStatus.FORMAT_FILE_ERRO); return ResultModel.ERROR(ResultStatus.FORMAT_FILE_ERRO);
} }
int row_already = titleKey.size();
String extend_row_sql = "platform,product,my_body_code,create_time";
DataFormatter formatter = new DataFormatter(); DataFormatter formatter = new DataFormatter();
int rowNumber = sheet.getLastRowNum(); int rowNumber = sheet.getLastRowNum();
List<Object[]> args_data = new ArrayList<>(); List<Object[]> args_data = new ArrayList<>();
String extend_row_sql = ",platform,product,my_body_code";
int extend_row = 3;
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy/mm/dd");
for (int j = 1; j <= rowNumber; j++) { for (int j = 1; j <= rowNumber; j++) {
Row row_data = sheet.getRow(j); Row row_data = sheet.getRow(j);
Object[] s_data = new Object[row_length + extend_row]; Object[] s_data = new Object[titleKey.size() + 4];
String bodyCode = null; String bodyCode = null;
for (int w = 0; w < row_length; w++) {
for (int w = 0; w < titleKey.size(); w++) {
String sheetTitle = titleKey.get(w);
Cell cell = row_data.getCell(w); Cell cell = row_data.getCell(w);
if (null != cell && !cell.getCellTypeEnum().equals(CellType.BLANK)) { if (null == cell || cell.getCellTypeEnum().equals(CellType.BLANK)) {
return ResultModel.ERROR(ResultStatus.UPLOAD_CONTENT_ERRO); return ResultModel.ERROR(ResultStatus.UPLOAD_CONTENT_ERRO);
} else { } else {
String dataSTR = formatter.formatCellValue(cell); String dataSTR = formatter.formatCellValue(cell);
int line_num = j + 1; int line_num = j + 1;
if (w == 0) { if ("my_body_name".equals(sheetTitle)) {
if (!cBodyMap.containsKey(dataSTR)) { if (!cBodyMap.containsKey(dataSTR)) {
return ResultModel.ERROR("第" + line_num + "行[我方签约主体]错误"); return ResultModel.ERROR("第" + line_num + "行[我方签约主体]错误");
} else { } else {
...@@ -2120,31 +2152,35 @@ public class ContractServiceImpl implements ContractService { ...@@ -2120,31 +2152,35 @@ public class ContractServiceImpl implements ContractService {
bodyCode = cBodyMap.get(dataSTR); bodyCode = cBodyMap.get(dataSTR);
} }
} else if (w == 2) { } else if ("trade_type".equals(sheetTitle)) {
if (!tradTypeMap.containsKey(dataSTR)) { if (!tradTypeMap.containsKey(dataSTR)) {
return ResultModel.ERROR("第" + line_num + "行[行业分类]错误"); return ResultModel.ERROR("第" + line_num + "行[行业分类]错误");
} else { } else {
s_data[w] = tradTypeMap.get(dataSTR); s_data[w] = tradTypeMap.get(dataSTR);
} }
} else if (w == 5) { } else if ("sale".equals(sheetTitle)) {
if (!salseMap.containsKey(dataSTR)) { if (!salseMap.containsKey(dataSTR)) {
return ResultModel.ERROR("第" + line_num + "行[签约销售]错误"); return ResultModel.ERROR("第" + line_num + "行[签约销售]错误");
} else { } else {
s_data[w] = salseMap.get(dataSTR); s_data[w] = salseMap.get(dataSTR);
} }
} else if (w == 8) { } else if ("contract_type".equals(sheetTitle)) {
if (!CONTRACT_TYPE.containsKey(dataSTR)) { if (!CONTRACT_TYPE_NAME.containsKey(dataSTR)) {
return ResultModel.ERROR("第" + line_num + "行[签约类型]错误"); return ResultModel.ERROR("第" + line_num + "行[签约类型]错误");
} else { } else {
s_data[w] = salseMap.get(dataSTR); s_data[w] = salseMap.get(dataSTR);
} }
} else if (w == 9) { } else if ("price_level".equals(sheetTitle)) {
if (packageMap != null && !packageMap.containsKey(dataSTR)) { if (packageMap != null && !packageMap.containsKey(dataSTR)) {
return ResultModel.ERROR("第" + line_num + "行[套餐]错误"); return ResultModel.ERROR("第" + line_num + "行[套餐]错误");
} else { } else {
s_data[w] = packageMap.get(dataSTR); s_data[w] = packageMap.get(dataSTR);
} }
} else if (w == 3 || w == 4) { } else if ("start_date".equals(sheetTitle) || "end_date".equals(sheetTitle)) {
String dfmate = cell.getCellStyle().getDataFormatString();
if ("yyyy/m/d;@".equals(dfmate) && "yyyy\\-mm\\-dd".equals(dfmate)) {
return ResultModel.ERROR("第" + line_num + "行[日期单元格]错误");
}
try { try {
String pattern = "yyyy/MM/dd"; String pattern = "yyyy/MM/dd";
DateTime dateTime = DateTime.parse(dataSTR, DateTimeFormat.forPattern(pattern)); DateTime dateTime = DateTime.parse(dataSTR, DateTimeFormat.forPattern(pattern));
...@@ -2158,35 +2194,44 @@ public class ContractServiceImpl implements ContractService { ...@@ -2158,35 +2194,44 @@ public class ContractServiceImpl implements ContractService {
} }
} }
s_data[extend_row] = platform;
s_data[extend_row + 1] = platform;
s_data[extend_row + 2] = bodyCode;
s_data[row_already] = platform;
s_data[row_already + 1] = platform;
s_data[row_already + 2] = bodyCode;
s_data[row_already + 3] = new Date();
//批量添加数据
args_data.add(s_data); args_data.add(s_data);
} }
String baseSql = "my_body_name,customer_body,trade_type,start_date,end_date,sale,email,contract_code, contract_type,price_level,money"; StringBuffer sql = new StringBuffer("insert into contract (");
baseSql = baseSql + extend_row_sql; titleKey.forEach(v -> sql.append(v + ","));
sql.append(extend_row_sql);
sql.append(") values (");
String endSql = null; for (int ed = 0, s = row_already + 4; ed < s; ed++) {
for (int ed = 0, s = baseSql.split(",").length; ed < s; ed++) { if (ed == 0) {
if (endSql == null) { sql.append("?");
endSql = "?";
} else { } else {
endSql = endSql + ",?"; sql.append(",?");
} }
} }
sql.append(")");
logger.info("sql[{}]", sql.toString());
// TransactionStatus transactionStatus = transactionUtils.begin();
// JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
// jdbcTemplate.batchUpdate(sql.toString(), args_data);
// transactionUtils.commit(transactionStatus);
jdbcTemplate.batchUpdate("insert into contract (" + baseSql + " ) values (" + endSql + ")", args_data); jdbcTemplate.batchUpdate(sql.toString(),args_data);
return ResultModel.OK(); return ResultModel.OK();
} }
private Object parseRowData(Row row, int length, StringBuffer content) { private boolean parseRowData(Row row, int length, StringBuffer content, List<String> titleKey) {
if (row == null) { if (row == null) {
return null; return false;
} }
if (content == null) { if (content == null) {
content = new StringBuffer(); content = new StringBuffer();
...@@ -2195,16 +2240,21 @@ public class ContractServiceImpl implements ContractService { ...@@ -2195,16 +2240,21 @@ public class ContractServiceImpl implements ContractService {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
Cell cell = row.getCell(i); Cell cell = row.getCell(i);
if (null != cell && !cell.getCellTypeEnum().equals(CellType.BLANK)) { if (null != cell && !cell.getCellTypeEnum().equals(CellType.BLANK)) {
String cnValue = formatter.formatCellValue(cell).trim();
if (content.length() == 0) { if (content.length() == 0) {
content.append(formatter.formatCellValue(cell)); content.append(cnValue);
} else { } else {
content.append("," + formatter.formatCellValue(cell)); content.append("," + cnValue);
}
if (!SHEET_TITLE.containsKey(cnValue)) {
return false;
} }
titleKey.add(SHEET_TITLE.get(cnValue));
} }
} }
return content;
return true;
} }
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -261,6 +261,7 @@ ...@@ -261,6 +261,7 @@
<tx:method name="query*" propagation="SUPPORTS" read-only="true"/> <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED"/>
<tx:method name="uploadBatchInfo" propagation="NEVER"/>
</tx:attributes> </tx:attributes>
</tx:advice> </tx:advice>
...@@ -342,4 +343,7 @@ ...@@ -342,4 +343,7 @@
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property> <property name="dataSource" ref="dataSource"></property>
</bean> </bean>
<bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans> </beans>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment