package com.nanometer.smartlab.service;
|
|
import com.nanometer.smartlab.dao.SysSupplierDao;
|
import com.nanometer.smartlab.entity.SysSupplier;
|
import com.nanometer.smartlab.exception.AlarmCode;
|
import com.nanometer.smartlab.exception.AlarmException;
|
import com.nanometer.smartlab.exception.BusinessException;
|
import com.nanometer.smartlab.exception.ExceptionEnumCode;
|
import com.nanometer.smartlab.util.ExcelUtils;
|
import com.nanometer.smartlab.util.IDUtils;
|
import com.nanometer.smartlab.util.MessageUtil;
|
import org.apache.commons.lang.StringUtils;
|
import org.apache.log4j.Logger;
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.primefaces.event.FileUploadEvent;
|
import org.primefaces.model.UploadedFile;
|
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DuplicateKeyException;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.io.InputStream;
|
import java.math.BigDecimal;
|
import java.util.*;
|
|
/**
|
* Created by johnny on 17/11/29.
|
*/
|
@Service("sysSupplierService")
|
public class SysSupplierServiceImpl implements SysSupplierService {
|
|
private static Logger logger = Logger.getLogger(SysSupplierService.class);
|
|
@Resource(name = "sysSupplierDao")
|
SysSupplierDao sysSupplierDao;
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public List<SysSupplier> getSysSupplierList(String name, Integer first, Integer pageSize) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
if (StringUtils.isNotBlank(name)) {
|
params.put("name", "%" + name + "%");
|
}
|
params.put("first", first);
|
params.put("pageSize", pageSize);
|
return this.sysSupplierDao.getSysSupplierList(params);
|
} catch (DataAccessException e) {
|
logger.error(e.getMessage(), e);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
|
}
|
}
|
public List<SysSupplier> getSysSupplierList() {
|
return this.getSysSupplierList(null, null, null);
|
}
|
@Transactional(propagation = Propagation.REQUIRED)
|
public int getSysSupplierTotalCount(String name) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
if (StringUtils.isNotBlank(name)) {
|
params.put("name", "%" + name + "%");
|
}
|
return this.sysSupplierDao.getSysSupplierTotalCount(params);
|
} catch (DataAccessException e) {
|
logger.error(e.getMessage(), e);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public SysSupplier getSysSupplier(Long id) {
|
try {
|
return this.sysSupplierDao.getSysSupplier(id);
|
} catch (DataAccessException e) {
|
logger.error(e.getMessage(), e);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
|
}
|
}
|
|
@Override
|
public Long getSysSupplierId(String name, Long groupId) {
|
try {
|
return this.sysSupplierDao.getSysSupplierId(name, groupId);
|
} catch (DataAccessException e) {
|
logger.error(e.getMessage(), e);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
|
}
|
}
|
|
@Override
|
public Long getSysSupplierIdByname(String name) {
|
try {
|
return this.sysSupplierDao.getSysSupplierIdByname(name);
|
} catch (DataAccessException e) {
|
logger.error(e.getMessage(), e);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public SysSupplier insertSysSupplier(SysSupplier sysSupplier) {
|
try {
|
// if (sysSupplier.getId() == null) {
|
// sysSupplier.setId(IDUtils.uuid());
|
// }
|
this.sysSupplierDao.insertSysSupplier(sysSupplier);
|
return sysSupplier;
|
} catch (DuplicateKeyException ex) {
|
logger.warn(ex.getMessage(), ex);
|
throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
|
} catch (DataIntegrityViolationException ex) {
|
logger.warn(ex.getMessage(), ex);
|
throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
|
} catch (DataAccessException ex) {
|
logger.error(ex.getMessage(), ex);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public boolean updateSysSupplier(SysSupplier sysSupplier) {
|
try {
|
int row = this.sysSupplierDao.updateSysSupplier(sysSupplier);
|
|
if (row == 0) {
|
return false;
|
}
|
return true;
|
} catch (DuplicateKeyException ex) {
|
logger.warn(ex.getMessage(), ex);
|
throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
|
} catch (DataIntegrityViolationException ex) {
|
logger.warn(ex.getMessage(), ex);
|
throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
|
} catch (DataAccessException ex) {
|
logger.error(ex.getMessage(), ex);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public boolean deleteSysSupplier(List<SysSupplier> sysSupplierList) {
|
try {
|
if (sysSupplierList == null || sysSupplierList.size() == 0) {
|
return false;
|
}
|
|
List<Long> ids = new ArrayList<Long>();
|
for (SysSupplier sysSupplier : sysSupplierList) {
|
ids.add(sysSupplier.getId());
|
}
|
|
int row = this.sysSupplierDao.deleteSysSupplier(ids);
|
if (row == 0) {
|
return false;
|
}
|
|
return true;
|
} catch (DataIntegrityViolationException ex) {
|
logger.warn(ex.getMessage(), ex);
|
throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
|
} catch (DataAccessException ex) {
|
logger.error(ex.getMessage(), ex);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
|
}
|
}
|
|
@Override
|
@Transactional
|
public void importSupplier(FileUploadEvent event) throws Exception {
|
|
List<SysSupplier> list = sysSupplierDao.getSysSupplierList(new HashMap());
|
Map<String, Long> supplierMap = new HashMap();
|
list.forEach(supplier -> {
|
supplierMap.put(supplier.getName(), supplier.getId());
|
});
|
|
UploadedFile file = event.getFile();
|
InputStream is = file.getInputstream();
|
boolean isExcel2003 = true;
|
if (file.getFileName().matches("^.+\\.(?i)(xlsx)$")) {
|
isExcel2003 = false;
|
}
|
Workbook wb = null;
|
if (isExcel2003) {
|
wb = new HSSFWorkbook(is);
|
} else {
|
wb = new XSSFWorkbook(is);
|
}
|
Sheet sheet = wb.getSheetAt(0);
|
List<SysSupplier> sysSuppliers = new ArrayList<>();
|
int totalRows = sheet.getPhysicalNumberOfRows();
|
Row row = null;
|
int totalCells = 0;
|
for (int i = 1; i < totalRows; i++) {
|
List<String> valuesList = new ArrayList<String>();
|
row = sheet.getRow(i);
|
|
totalCells = row.getPhysicalNumberOfCells();
|
//System.out.println("====="+totalCells);
|
|
// 目前导入文件11列,不满足条件的情况跳过
|
/*if (totalCells != 12) {
|
throw new Exception("导入文件格式不正确");
|
}*/
|
|
|
for (int t = 0; t < totalCells; t++) {
|
String cellInfo = "";
|
if (row.getCell(t) != null) {
|
cellInfo = row.getCell(t).getStringCellValue();
|
}
|
valuesList.add(cellInfo);
|
}
|
if (valuesList.size() != 4){
|
break;
|
}
|
|
SysSupplier sysSupplier = new SysSupplier();
|
//这一行有供应商是数据库里的就跳过
|
if (supplierMap.get(valuesList.get(0)) != null) {
|
continue;
|
}
|
|
// sysSupplier.setId(IDUtils.uuid());
|
sysSupplier.setName(valuesList.get(0));
|
sysSupplier.setPersonName(valuesList.get(1));
|
sysSupplier.setPhone(valuesList.get(2));
|
sysSupplier.setMemo(valuesList.get(3));
|
sysSuppliers.add(sysSupplier);
|
|
}
|
|
if (sysSuppliers.size() > 0) {
|
sysSupplierDao.insertBatch(sysSuppliers);
|
}
|
|
}
|
|
@Override
|
public List<Map> exportExcelList(String name) {
|
Map<String, String> params = new HashMap<>();
|
params.put("name", name);
|
return sysSupplierDao.exportExcelList(params);
|
}
|
|
@Override
|
public void export2Excel(List<Map> list) throws Exception {
|
Map<String,String> map = new LinkedHashMap<>();
|
map.put("name", "供应商名");
|
map.put("personName", "联系人");
|
map.put("phone", "电话");
|
map.put("memo", "备注");
|
ExcelUtils.export2Excel(list,"供应商信息",map);
|
}
|
|
@Override
|
public SysSupplier getSysSupplierByName(String name) {
|
return sysSupplierDao.getSysSupplierByName(name);
|
}
|
|
}
|