|
@@ -0,0 +1,301 @@
|
|
|
+package com.sundata.product.rwa.rwacalcconfigdataparam.service;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.sundata.admin.excelmanage.mapper.RptDefineMapper;
|
|
|
+import com.sundata.admin.excelmanage.model.RptDefineModel;
|
|
|
+import com.sundata.common.base.BaseMapper;
|
|
|
+import com.sundata.common.base.BaseService;
|
|
|
+import com.sundata.common.exception.BusinessException;
|
|
|
+import com.sundata.common.util.DBExecutor;
|
|
|
+import com.sundata.common.util.StringUtil;
|
|
|
+import com.sundata.product.rwa.businessconfig.utils.FilesValueCheckUtils;
|
|
|
+import com.sundata.product.rwa.rwacalcconfigdataparam.mapper.BondMapper;
|
|
|
+import com.sundata.product.rwa.rwacalcconfigdataparam.model.BondModel;
|
|
|
+import com.sundata.product.taskExecute.service.KingBase2GBaseService;
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class BondService extends BaseService<RptDefineModel> {
|
|
|
+
|
|
|
+ // 日志
|
|
|
+ static final Logger log = LoggerFactory.getLogger(BondService.class);
|
|
|
+ // 定义jackson对象
|
|
|
+ private static final ObjectMapper json = new ObjectMapper();
|
|
|
+ @Autowired
|
|
|
+ private RptDefineMapper<RptDefineModel> rptDefineMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ KingBase2GBaseService kingBase2GBaseService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ BondMapper bondMapper;
|
|
|
+
|
|
|
+ public Object updateExistData(BondModel model) {
|
|
|
+ bondMapper.updateExistData(model);
|
|
|
+ bondMapper.delData(model);
|
|
|
+ return bondMapper.copyData(model);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void kingBase2GBase(String typeOrId,String tableName) {
|
|
|
+ kingBase2GBaseService.doExecute(typeOrId,tableName);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<BondModel> selectDataList(BondModel model) {
|
|
|
+
|
|
|
+ return bondMapper.selectDataList(model);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<BondModel> selectDataList1() {
|
|
|
+ return bondMapper.selectDataList1();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object selectRowData(BondModel model) {
|
|
|
+ return bondMapper.selectRowData(model);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void exportExcelModel(HttpServletResponse response, String fileNameCode, List<BondModel> modelList) throws IOException {
|
|
|
+ // 数据检查
|
|
|
+ if (null == modelList) {
|
|
|
+ String err = "导出数据为空!";
|
|
|
+ log.error(err);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ }
|
|
|
+ // 取得文件名
|
|
|
+ String fileName = DBExecutor.doQuery("select RPTNAME from SYS_RPTDEFINE where RPTCODE = '"+ fileNameCode +"'");
|
|
|
+ // 字典里获取附件路径
|
|
|
+ String templatePath = DBExecutor.doQuery("select NOUNVALUE from sys_noun where NOUNITEM = 'ATTACHPATH'");
|
|
|
+
|
|
|
+ // TODO 获取菜单名
|
|
|
+ String menuName = DBExecutor.doQuery("select funcname from sys_funinfo where funccode='M4100000'");
|
|
|
+ StringBuilder pathName = new StringBuilder(templatePath+"/"+menuName);
|
|
|
+ // 获取数据库模板文件
|
|
|
+ RptDefineModel entity = (RptDefineModel)this.queryOne(fileNameCode);
|
|
|
+ // 判断是否获取成功
|
|
|
+ if (entity == null) {
|
|
|
+ String err = "报表代码[" + fileNameCode + "]不存在!";
|
|
|
+ log.error(err);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ } else {
|
|
|
+ // 获取成功执行导出方法
|
|
|
+ FilesValueCheckUtils.exportExcelModel(response,modelList,pathName,fileName+"导出",entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void importExcelModel(InputStream io, MultipartFile file) {
|
|
|
+ String fileName = file.getOriginalFilename().toLowerCase();
|
|
|
+ List<BondModel> modelList = new ArrayList<BondModel>();
|
|
|
+ // 导入文件流转换为poi对象
|
|
|
+ Workbook book = FilesValueCheckUtils.workBookInstance(io,file);
|
|
|
+ if (book == null) {
|
|
|
+ String err = "文件格式不正确!,请用xlsx或xls文件";
|
|
|
+ log.error(err);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ }
|
|
|
+ // FilesValueCheckUtils.castFileValue(book,BondModel.class,modelList);
|
|
|
+ Class clazz=BondModel.class;
|
|
|
+ // 接收反射对象
|
|
|
+ Object object = null;
|
|
|
+ // 获取实体类内方法
|
|
|
+ Method method = null;
|
|
|
+ try {
|
|
|
+ method = clazz.getMethod("getCastFileCellToJson");
|
|
|
+
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
+ String err = "通过反射获得实体类内getCastFileCellToJson方法发生异常,该方法无法正常获取!";
|
|
|
+ log.error(err, e);
|
|
|
+ throw new BusinessException(err, e);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行方法获得返回值
|
|
|
+ HashMap<String,String> map =null;
|
|
|
+ try {
|
|
|
+ map =(HashMap) method.invoke(clazz);
|
|
|
+ } catch (IllegalAccessException | InvocationTargetException e) {
|
|
|
+ String err = "执行getCastFileCellToJson方法发生异常,无法获得该方法的返回值!";
|
|
|
+ log.error(err, e);
|
|
|
+ throw new BusinessException(err, e);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 只读取第1个sheet页
|
|
|
+ Sheet sheet = book.getSheetAt(0);
|
|
|
+ // 当前sheet页的数据总行数
|
|
|
+ int dataRows = sheet.getLastRowNum();
|
|
|
+ // 判断是否有数据
|
|
|
+ if (dataRows == 0) {
|
|
|
+ String err = "文件内容为空!,请检查导入数据是否正确";
|
|
|
+ log.error(err);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ }
|
|
|
+ // 记录空值列
|
|
|
+ StringBuilder cellNullMessage = null;
|
|
|
+ // 遍历当前sheet页每一行
|
|
|
+ for (int rowNum=0;rowNum<=dataRows;rowNum++){
|
|
|
+ // 每一行数据按键值对存进map
|
|
|
+ HashMap<String,String> jsonMap = new HashMap<String,String>();
|
|
|
+ // 记录空值列对象初始化
|
|
|
+ cellNullMessage = new StringBuilder();
|
|
|
+ // 空值个数初始化
|
|
|
+ int nulCellSum = 0;
|
|
|
+
|
|
|
+ // 将反射对象进行实例化
|
|
|
+ try {
|
|
|
+ object = clazz.newInstance();
|
|
|
+ } catch (InstantiationException | IllegalAccessException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断如果当前行是否为空,为空则跳过
|
|
|
+ if (sheet.getRow(rowNum) == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当前行的列数
|
|
|
+ int cellNums = sheet.getRow(0).getLastCellNum();
|
|
|
+
|
|
|
+ // 遍历当前行的每一列
|
|
|
+ for (int cellNum = 0; cellNum<=cellNums-1; cellNum++){
|
|
|
+ Cell cellData = null;
|
|
|
+
|
|
|
+ // 列名检查
|
|
|
+ if (rowNum == 0) {
|
|
|
+ if (!map.containsKey(sheet.getRow(rowNum).getCell(cellNum).toString().trim())) {
|
|
|
+ String err = "请检查模板!,(1)列名是否正确(2)列名不可出现空格或空列(3)列与列之间要连续";
|
|
|
+ log.error(err);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获得当前列数据
|
|
|
+ cellData = sheet.getRow(rowNum).getCell(cellNum);
|
|
|
+ // 获取列名对应的英文
|
|
|
+ String key = map.get(sheet.getRow(0).getCell(cellNum).toString().trim());
|
|
|
+
|
|
|
+ // 记录空数据
|
|
|
+ if (null == cellData || StringUtil.isEmpty(cellData.toString())){
|
|
|
+ // 空数据个数
|
|
|
+ nulCellSum++;
|
|
|
+ // 记录列
|
|
|
+ cellNullMessage.append(String.valueOf(cellNum+1)).append(",");
|
|
|
+ } else {
|
|
|
+ // 将英文名与数据存进map
|
|
|
+ jsonMap.put(key,cellData.toString().trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (nulCellSum !=0 && nulCellSum < cellNums) {
|
|
|
+ String err = "第"+ (rowNum+1)+"行第"+ cellNullMessage.toString() + "列导入数据为空!,请检查";
|
|
|
+ log.error(err);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (nulCellSum == cellNums) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第一行不计入数据
|
|
|
+ if (rowNum != 0) {
|
|
|
+ // 将文件数据内容转化为实体类对象
|
|
|
+ object = json.convertValue(jsonMap,clazz);
|
|
|
+ // 插入list
|
|
|
+ modelList.add((BondModel)object);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (modelList.isEmpty()) {
|
|
|
+ String err = "文件转换对象异常!";
|
|
|
+ log.error(err);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量插入
|
|
|
+ // 先全量删除
|
|
|
+ boolean flag = true;
|
|
|
+// flag = bondMapper.deleteRows(null);
|
|
|
+// if (!flag) {
|
|
|
+// String err = "数据库删除异常!";
|
|
|
+// log.error(err);
|
|
|
+// throw new BusinessException(err);
|
|
|
+// }
|
|
|
+ String datadt=DBExecutor.doQuery("SELECT MAX(DATA_DATE) datadt FROM RWA_BDM_BS_FF_SEC_POSIT");
|
|
|
+
|
|
|
+ for (BondModel BondModel : modelList) {
|
|
|
+ if (null == BondModel || FilesValueCheckUtils.checkStringValue(BondModel.getgeneralSpecialFlagName())) {
|
|
|
+ String err = "一般专项标志不能为空!";
|
|
|
+ log.error(err);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ }
|
|
|
+ BondModel.setDataDate(datadt);
|
|
|
+ flag = bondMapper.updateExistData(BondModel);
|
|
|
+ if (!flag) {
|
|
|
+ String err = "数据库更新异常!";
|
|
|
+ log.error(err);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //更更新完成后复制数据
|
|
|
+ BondModel model =new BondModel();
|
|
|
+
|
|
|
+ model.setDataDate(datadt);
|
|
|
+ bondMapper.delData(model);
|
|
|
+ bondMapper.copyData(model);
|
|
|
+
|
|
|
+ // 工作流关闭
|
|
|
+ try {
|
|
|
+ book.close();
|
|
|
+ io.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ String err = "io流关闭异常";
|
|
|
+ log.error(err,e);
|
|
|
+ throw new BusinessException(err,e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void templateUploadModel(HttpServletResponse response, String fileNameCode,List<BondModel> modelList) throws IOException {
|
|
|
+ // List<BondModel> modelList = new ArrayList<BondModel>();
|
|
|
+ modelList.add(new BondModel());
|
|
|
+ // 取得文件名
|
|
|
+ String fileName = DBExecutor.doQuery("select RPTNAME from SYS_RPTDEFINE where RPTCODE = '"+ fileNameCode +"'");
|
|
|
+ // 字典里获取附件路径
|
|
|
+ String templatePath = DBExecutor.doQuery("select NOUNVALUE from sys_noun where NOUNITEM = 'ATTACHPATH'");
|
|
|
+
|
|
|
+ // TODO 获取菜单名
|
|
|
+ String menuName = DBExecutor.doQuery("select funcname from sys_funinfo where funccode='M412000'");
|
|
|
+ StringBuilder pathName = new StringBuilder(templatePath+"/"+menuName);
|
|
|
+ // 获取数据库模板文件
|
|
|
+ RptDefineModel entity = (RptDefineModel)this.queryOne(fileNameCode);
|
|
|
+ if (entity == null) {
|
|
|
+ String err = "报表代码[" + fileNameCode + "]不存在!";
|
|
|
+ log.error(err);
|
|
|
+ throw new BusinessException(err);
|
|
|
+ } else {
|
|
|
+ FilesValueCheckUtils.exportExcelModel(response,modelList,pathName,fileName+"导入",entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BaseMapper<RptDefineModel> getMapper() {
|
|
|
+ return this.rptDefineMapper;
|
|
|
+ }
|
|
|
+}
|