Commit 4353c979 authored by 刘文胜's avatar 刘文胜

增量保存sales数据和products数据

parent e24c7782
......@@ -54,7 +54,9 @@ public class KolonMemberController extends JWTSecurityController{
public @ResponseBody String syncData(HttpServletRequest req) {
DTO dto = DTO.newDTO();
try {
kolonMemberService.syncData();
kolonMemberService.syncMemberData();
kolonMemberService.syncProductData();
kolonMemberService.syncSaleData();
return dto.toJson();
} catch (Exception e) {
e.printStackTrace();
......@@ -107,7 +109,9 @@ public class KolonMemberController extends JWTSecurityController{
@RequestParam(value = "time",required = false) String time) {
DTO dto = DTO.newDTO();
try {
kolonMemberService.syncData();
kolonMemberService.syncMemberData();
kolonMemberService.syncProductData();
kolonMemberService.syncSaleData();
String lastSendTime = null;
if(time != null){
......
......@@ -32,9 +32,13 @@ public interface KolonMemberService {
void matchData(String[] s);
/**
* 同步数据
* 同步会员数据
*/
void syncData();
void syncMemberData();
void syncProductData();
void syncSaleData();
/**
* 生成文件并发送到FTP服务器,返回最后更新的时间
......
......@@ -10,15 +10,20 @@ import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import au.com.bytecode.opencsv.CSVWriter;
import com.hdp.pi.domain.kolon.KolonMember;
import com.hdp.pi.domain.kolon.Product;
import com.hdp.pi.domain.kolon.Sale;
import com.hdp.pi.dto.kolon.KolonMemberDTO;
import com.hdp.pi.repository.kolon.KolonMemberPreassignSnRepository;
import com.hdp.pi.repository.kolon.KolonMemberRepository;
import com.hdp.pi.repository.kolon.ProductRepository;
import com.hdp.pi.repository.kolon.SaleRepository;
import com.hdp.pi.utils.kolon.FtpUtil;
import com.hdp.pi.utils.kolon.Util;
......@@ -31,12 +36,27 @@ public class KolonMemberServiceImpl implements KolonMemberService {
@Autowired
private FtpUtil ftpUtil;
@Value("${kolon.property.ftp.getFileName}")
private String memberFileName;
@Value("${kolon.property.ftp.saleFileName}")
private String saleFileName;
@Value("${kolon.property.ftp.productFileName}")
private String productFileName;
@Autowired
private KolonMemberRepository kolonMemberRepository;
@Autowired
private KolonMemberPreassignSnRepository kolonMemberPreassignSnRepository;
@Autowired
private ProductRepository productRepository;
@Autowired
private SaleRepository saleRepository;
@Override
public KolonMember findOneByCsNo(Long csNo) {
return kolonMemberRepository.findFirstByCsNo(csNo);
......@@ -130,8 +150,8 @@ public class KolonMemberServiceImpl implements KolonMemberService {
}
@Override
public void syncData() {
List<String[]> list = ftpUtil.readCSVFile();
public void syncMemberData() {
List<String[]> list = ftpUtil.readCSVFile(memberFileName);
if (list != null) {
boolean firstRow = true;
for (String[] row : list) {
......@@ -142,9 +162,125 @@ public class KolonMemberServiceImpl implements KolonMemberService {
}
}
}
ftpUtil.moveFile();
ftpUtil.moveFile(memberFileName);
}
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
private void saveProductAbsentIf(Product product){
if(product == null || product.id == null){
return;
}
Product p = productRepository.findOne(product.id);
if(p == null){
productRepository.save(product);
}
}
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
private void saveSale(Sale sale){
if(sale == null){
return;
}
if(sale.oId == null){
return;
}
if(sale.oDate == null){
return;
}
if(sale.customer == null){
return;
}
if(sale.item == null){
return;
}
if(sale.quantity == null){
return;
}
if(sale.amount == null){
return;
}
saleRepository.save(sale);
}
private String getByIndex(String[] row,int index){
if(row == null){
return "";
}
if(index < 0 || index >= row.length){
return "";
}
return row[index];
}
private Product newProduct(String[] row){
Product p = new Product();
try{
p.id = getByIndex(row, 0);
p.title = getByIndex(row, 1);
p.category = getByIndex(row, 2);
p.image = getByIndex(row, 3);
p.link = getByIndex(row, 4);
p.avaliable = getByIndex(row, 5);
p.dept = getByIndex(row, 6);
p.seaons = getByIndex(row, 7);
p.planyy = getByIndex(row, 8);
p.color = getByIndex(row, 9);
p.price = getByIndex(row, 10);
}catch(Throwable e){
e.printStackTrace();
}
return p;
}
private Sale newSale(String[] row){
Sale s = new Sale();
try{
s.oId = getByIndex(row, 0);
s.oDate = Integer.valueOf(getByIndex(row, 1));
s.customer = getByIndex(row, 2);
s.item = getByIndex(row, 3);
s.quantity = Integer.valueOf(getByIndex(row, 4));
s.amount = Double.valueOf(getByIndex(row, 5));
}catch(Throwable e){
e.printStackTrace();
}
return s;
}
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void syncProductData() {
List<String[]> list = ftpUtil.readCSVFile(productFileName);
if (list != null) {
boolean firstRow = true;
for (String[] row : list) {
if (firstRow) {
firstRow = false;
} else {
saveProductAbsentIf(newProduct(row));
}
}
}
ftpUtil.moveFile(productFileName);
}
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void syncSaleData() {
List<String[]> list = ftpUtil.readCSVFile(saleFileName);
if (list != null) {
boolean firstRow = true;
for (String[] row : list) {
if (firstRow) {
firstRow = false;
} else {
saveSale(newSale(row));
}
}
}
ftpUtil.moveFile(saleFileName);
}
@Override
public Date sendData(Date time) {
......
......@@ -51,12 +51,6 @@ public class FtpUtil {
@Value("${kolon.property.ftp.getFileHistoryDir}")
private String getFileHistoryDir;
/**
* FTP文件名称
*/
@Value("${kolon.property.ftp.getFileName}")
private String getFileName;
/**
* 临时路径
*/
......@@ -125,14 +119,14 @@ public class FtpUtil {
/**
* 读取文件数据
*/
public List<String[]> readCSVFile() {
public List<String[]> readCSVFile(String fileName) {
List<String[]> list = new ArrayList<String[]>();
try {
FTPClient ftpClient = this.getFTPClient();
InputStream csv = ftpClient.retrieveFileStream(getFileDir
+ getFileName);
+ fileName);
if (csv == null) {
logger.warn("kolon数据同步失败,没有找到【" + getFileDir + getFileName
logger.warn("kolon数据同步失败,没有找到【" + getFileDir + fileName
+ "】数据文件");
return null;
}
......@@ -153,17 +147,17 @@ public class FtpUtil {
/**
* 移动文件
*/
public void moveFile() {
public void moveFile(String fileName) {
try {
FTPClient ftpClient = this.getFTPClient();
// 新的文件名
String suffix = getFileName.substring(getFileName.lastIndexOf("."));
String name = getFileName
.substring(0, getFileName.lastIndexOf("."));
String suffix = fileName.substring(fileName.lastIndexOf("."));
String name = fileName
.substring(0, fileName.lastIndexOf("."));
String newFileName = name + "_" + getTime() + suffix;
ftpClient.rename(getFileDir + getFileName, getFileHistoryDir
ftpClient.rename(getFileDir + fileName, getFileHistoryDir
+ newFileName);
ftpClient.disconnect();
} catch (IOException e) {
......
......@@ -31,6 +31,9 @@ kolon.property.ftp.password = yang19821123
kolon.property.ftp.getFileDir = /Emarsys/
kolon.property.ftp.getFileHistoryDir = /Emarsys/history/
kolon.property.ftp.getFileName = contact.csv
kolon.property.ftp.saleFileName = sales.csv
kolon.property.ftp.productFileName = products.csv
kolon.property.ftp.tempFileDir = /temp
kolon.property.ftp.tempAddFileName = /temp/tempAddContact.csv
......
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