package cn.gson.financial.common;
import cn.gson.financial.kernel.common.DoubleValueUtil;
import cn.gson.financial.kernel.exception.ServiceException;
import cn.gson.financial.kernel.model.entity.*;
import cn.gson.financial.kernel.model.vo.UserVo;
import cn.gson.financial.kernel.service.AccountingCategoryDetailsService;
import cn.gson.financial.kernel.service.AccountingCategoryService;
import cn.gson.financial.kernel.service.SubjectService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* <p>****************************************************************************</p>
* <p><b>Copyright © 2010-2019 soho team All Rights Reserved<b></p>
* <ul style="margin:15px;">
* <li>Description : cn.gson.financial.common</li>
* <li>Version : 1.0</li>
* <li>Creation : 2019年10月15日</li>
* <li>@author : ____′↘夏悸</li>
* </ul>
* <p>****************************************************************************</p>
*/
@Slf4j
@Component
public final class VoucherExcelUtils {
private String[] COLS = {};
//辅助项目
private String[] AUXILIARY = {};
private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
@Autowired
private SubjectService subjectService;
@Autowired
private AccountingCategoryDetailsService acds;
@Autowired
private AccountingCategoryService accountingCategoryService;
/**
* 读取excel数据并转化
*
* @param fileName
* @param is
* @param userVo
* @return
* @throws IOException
*/
public List<Voucher> readExcel(String fileName, InputStream is, UserVo userVo) throws IOException {
this.loadCategory(userVo);
List<Voucher> list = new ArrayList<>();
Workbook workbook;
if ("xls".equals(FilenameUtils.getExtension(fileName))) {
workbook = new HSSFWorkbook(is);
} else {
workbook = new XSSFWorkbook(is);
}
Sheet sheet = workbook.getSheetAt(0);
if (sheet != null) {
//检查excel格式
this.checkHeader(sheet);
List<Map<String, Object>> data = new ArrayList<>(sheet.getLastRowNum());
//所有编码类项目
Set<String> subjectCodeSet = new HashSet<>();
Map<String, Set<String>> auxiliaryMap = new HashMap<>(AUXILIARY.length);
for (int i = 1, len = sheet.getLastRowNum(); i <= len; i++) {
Row row = sheet.getRow(i);
Map<String, Object> rowData = new HashMap<>(row.getLastCellNum());
//根据表头遍历数据
for (int j = 0; j < COLS.length; j++) {
Cell cell = row.getCell(j);
String key = COLS[j];
Object val;
try {
switch (cell.getCellType()) {
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
val = DateFormatUtils.format(cell.getDateCellValue(), "yyyy-MM-dd");
} else {
val = cell.getNumericCellValue();
}
break;
default:
val = cell.getStringCellValue();
break;
}
if (key.equals("科目编码") && val instanceof Double) {
val = ((Double) val).intValue();
}
rowData.put(key, val);
} catch (Exception e) {
throw new ServiceException("[" + key + "]列读取错误!" + e.getMessage(), e);
}
}
subjectCodeSet.add(rowData.get("科目编码").toString());
//汇总辅助项
for (String a : AUXILIARY) {
if (!auxiliaryMap.containsKey(a)) {
auxiliaryMap.put(a, new HashSet<>());
}
Object val = rowData.get(a + "编码");
if (val != null && StringUtils.trimToNull(val.toString()) != null) {
auxiliaryMap.get(a).add(StringUtils.trim(val.toString()));
}
}
rowData.put("unix", StringUtils.join(rowData.get("日期"), "~", rowData.get("凭证号"), "~", rowData.get("备注")));
data.add(rowData);
}
//科目数据
Map<String, Subject> codeList = this.getByCodeList(userVo.getAccountSetsId(), subjectCodeSet);
//辅助项目数据
Map<String, Map<String, AccountingCategoryDetails>> auxiliaryDataMap = this.getAuxiliaryDataMap(userVo.getAccountSetsId(), auxiliaryMap);
Map<String, List<Map<String, Object>>> unix = data.stream().collect(Collectors.groupingBy(map -> map.get("unix").toString()));
unix.forEach((key, maps) -> {
String[] split = StringUtils.split(key, "~");
Calendar calendar;
try {
calendar = DateUtils.toCalendar(df.parse(split[0]));
} catch (ParseException e) {
throw new ServiceException("日期[" + split[0] + "]格式错误,请设置日期列单元格格式为‘文本’类型!", e);
}
String[] wordCode = StringUtils.split(split[1], "-");
Voucher voucher = new Voucher();
voucher.setWord(wordCode[0]);
voucher.setCode(Integer.parseInt(wordCode[1]));
if (split.length > 2) {
voucher.setRemark(split[2]);
}
voucher.setCreateDate(calendar.getTime());
voucher.setVoucherYear(calendar.get(Calendar.YEAR));
voucher.setVoucherMonth(calendar.get(Calendar.MONTH) + 1);
voucher.setVoucherDate(calendar.getTime());
voucher.setDetails(new ArrayList<>(maps.size()));
voucher.setCreateMember(userVo.getId());
voucher.setAccountSetsId(userVo.getAccountSetsId());
voucher.setCarryForward(false);
maps.forEach(map -> {
VoucherDetails details = new VoucherDetails();
JSONObject jo = new JSONObject(map);
details.setSummary(jo.getString("摘要"));
details.setSubjectName(jo.getString("科目名称"));
details.setSubjectCode(jo.getString("科目编码"));
details.setDebitAmount(jo.getDouble("借方本币"));
details.setCreditAmount(jo.getDouble("贷方本币"));
details.setAccountSetsId(userVo.getAccountSetsId());
details.setAuxiliary(new ArrayList<>());
details.setCarryForward(false);
details.setNum(DoubleValueUtil.getNotNullVal(jo.getDouble("借方数量"), jo.getDouble("贷方数量")));
if (StringUtil
没有合适的资源?快使用搜索试试~ 我知道了~
财务软件源码 财务软件开源代码
共39个文件
java:35个
yml:2个
ttc:1个
需积分: 0 24 下载量 140 浏览量
2023-07-06
10:54:08
上传
评论 1
收藏 5.27MB RAR 举报
温馨提示
可以二开
资源推荐
资源详情
资源评论
收起资源包目录
bs-server.rar (39个子文件)
bs-server
src
main
resources
application-prod.yml 723B
simsun.ttc 10.01MB
application.yml 2KB
java
cn
gson
financial
annotation
IgnoresLogin.java 780B
Permissions.java 848B
converter
DateConverter.java 1KB
controller
CheckOutController.java 3KB
AppController.java 10KB
VoucherTemplateController.java 968B
AccountingCategoryController.java 10KB
AccountBookController.java 5KB
AccountingCategoryDetailsController.java 3KB
VoucherWordController.java 944B
PrintPdfController.java 8KB
InitialBalanceController.java 5KB
UserController.java 1KB
AccountSetsController.java 8KB
CurrencyController.java 924B
SubjectController.java 7KB
ReportTemplateController.java 1KB
VoucherController.java 6KB
HomeController.java 5KB
ReportTemplateItemsController.java 2KB
base
BaseCrudController.java 5KB
ErrorControllerAdvice.java 2KB
BaseController.java 1KB
common
PinYinUtil.java 7KB
PdfFontUtils.java 4KB
ConvertMoney.java 6KB
CusMetaObjectHandler.java 1KB
VoucherExcelUtils.java 13KB
SubjectExcelUtils.java 9KB
FinancialApplication.java 2KB
WebMvcSupport.java 3KB
config
JsonFormatConfig.java 2KB
GracefulShutdownTomcat.java 2KB
interceptor
PermissionsHandlerInterceptorAdapter.java 2KB
LoginHandlerInterceptorAdapter.java 2KB
build.gradle 1KB
共 39 条
- 1
资源评论
财务软件源码企万慧
- 粉丝: 3
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功