Commit b17265b6 authored by neo's avatar neo

[dev] add balance import for children balance impl

parent 83f6950b
package pwc.taxtech.atms.dto.vatdto;
import java.math.BigDecimal;
public class BalanceAccountDto {
public String BalanceId ;
public Integer PeriodId ;
public String AcctCode ;
public String CustomerCode ;
public BigDecimal BegDebitBal ;
public BigDecimal BegCreditBal ;
public BigDecimal BegBal ;
public BigDecimal EndBal ;
public BigDecimal EndDebitBal ;
public BigDecimal EndCreditBal ;
public BigDecimal DebitBal ;
public BigDecimal CreditBal ;
public Integer MonthId ;
public String ParentCode ;
public Integer AcctLevel ;
public String StdCode ;
public String AccountName ;
public Integer Direction ;
public Integer IsDummy ;
public String getAcctCode() {
return AcctCode;
}
public Integer getAcctLevel() {
return AcctLevel;
}
}
package pwc.taxtech.atms.service.impl;
import com.github.pagehelper.util.StringUtil;
import com.google.common.collect.Lists;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.constant.enums.EnumServiceType;
......@@ -14,6 +18,7 @@ import pwc.taxtech.atms.dao.EnterpriseAccountMapper;
import pwc.taxtech.atms.dao.ProjectMapper;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.taxadmin.DataImportedStatusDto;
import pwc.taxtech.atms.dto.vatdto.BalanceAccountDto;
import pwc.taxtech.atms.entitiy.EnterpriseAccount;
import pwc.taxtech.atms.entitiy.EnterpriseAccountExample;
import pwc.taxtech.atms.entitiy.Project;
......@@ -24,11 +29,14 @@ import pwc.taxtech.atms.vat.dao.ValidationInfoMapper;
import pwc.taxtech.atms.vat.dao.VatEnterpriseAccountMapper;
import pwc.taxtech.atms.vat.dao.DataImportServiceMapper;
import pwc.taxtech.atms.vat.entity.Balance;
import pwc.taxtech.atms.vat.entity.BalanceExample;
import pwc.taxtech.atms.vat.entity.CompanyBalance;
import pwc.taxtech.atms.vat.entity.CompanyBalanceExample;
import pwc.taxtech.atms.vat.entity.ValidationInfoExample;
import pwc.taxtech.atms.vat.entity.VatEnterpriseAccount;
import pwc.taxtech.atms.vat.entity.VatEnterpriseAccountExample;
import java.io.StringReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
......@@ -43,8 +51,9 @@ import java.util.stream.Collectors;
public class DataImportServiceImpl {
private static final String PROJECT_DB_NAME = "test_db";//TODO:fixed use init db
private static final String COMPANY_BALANCE_TABLE = "CompanyBalance";
private static final String BALANCE_TABLE = "Balance";
private static final String _companyBalancetable = "CompanyBalance";
private static final String _balancetable = "Balance";
private static final String _validationtable = "ValidationInfo";
private static final int FIRST_OR_DEFAULT = 0;
@Autowired
......@@ -66,40 +75,57 @@ public class DataImportServiceImpl {
private DataImportServiceMapper dataImportServiceMapper;
@Autowired
private BalanceMapper balanceMapper;
@Autowired
private SqlSessionTemplate dynamicSqlSessionTemplate;
public OperationResultDto importTrialBalance(List<CompanyBalance> balanceList, Integer importType,
Integer serviceTypeId, String userID) {
OperationResultDto result = new OperationResultDto();
if (serviceTypeId == ServiceType.VAT.getCode().intValue()) {
int period = balanceList.get(0).getPeriodId().intValue();
DataImportedStatusDto dataImportedStatus = new DataImportedStatusDto();
dataImportedStatus.setPeriod(period);
dataImportedStatus.setDbName(PROJECT_DB_NAME);
dataImportedStatus.setDataType(ProjectImportDataType.TrialBalance.getCode());
dataImportedStatus.setStartTime(new Date());
//覆盖导入之前删除数据
if (importType == EnumTbImportType.CoverImport.getCode()) {
clearTableData(period, COMPANY_BALANCE_TABLE);
}
try {
// var sqlhelper = new SqlHelper(_projectDbConnstring);
// var companyBalance = ServiceHelper.ToDataTable(balanceList);
// sqlhelper.SqlBulkCopyInsert(_companyBalancetable, companyBalance, 10000);
// sqlhelper.Close();
//TODO: sql bulk copy insert tobe impl(neo)
OperationResultDto result = new OperationResultDto();
if (serviceTypeId == ServiceType.VAT.getCode().intValue()) {
int period = balanceList.get(0).getPeriodId().intValue();
DataImportedStatusDto dataImportedStatus = new DataImportedStatusDto();
//SynAccountFromTaxAdmin(period, userID);
Boolean isSuccess = refreshTrialBalance(period, serviceTypeId, userID);
result.setResult(isSuccess);
dataImportedStatus.setPeriod(period);
dataImportedStatus.setDbName(PROJECT_DB_NAME);
dataImportedStatus.setDataType(ProjectImportDataType.TrialBalance.getCode());
dataImportedStatus.setStartTime(new Date());
} else if (serviceTypeId == ServiceType.CIT.getCode()) {//TODO import cit (neo)
//覆盖导入之前删除数据
if (importType == EnumTbImportType.CoverImport.getCode()) {
clearTableData(period, _companyBalancetable);
}
}
balanceList.forEach(m -> companyBalanceMapper.insert(m));
//TODO: sql bulk copy insert tobe impl(neo)
//SynAccountFromTaxAdmin(period, userID);
Boolean isSuccess = refreshTrialBalance(period, serviceTypeId, userID);
result.setResult(isSuccess);
ValidationInfoExample validationInfoExample = new ValidationInfoExample();
validationInfoExample.createCriteria().andImportTypeIdEqualTo(0).andPeriodIdEqualTo(period).andErrorLevelIsNotNull();
Long exceptionCount = validationInfoMapper.countByExample(validationInfoExample);
dataImportedStatus.setTotalCount(balanceList.size());
dataImportedStatus.setExceptionCount(exceptionCount.intValue());
dataImportedStatus.setSuccessedCount(dataImportedStatus.getTotalCount() - dataImportedStatus.getExceptionCount());
dataImportedStatus.setEndTime(new Date());
UpdateDataImportedStatu(Lists.newArrayList(dataImportedStatus));
} else if (serviceTypeId == ServiceType.CIT.getCode()) {//TODO import cit (neo)
return result;
}
return result;
} catch (Exception e) {
return new OperationResultDto(false, e.getMessage());
}
}
private String[] UpdateDataImportedStatu(ArrayList<DataImportedStatusDto> dataImportedStatusDtos) {
// todo:[validation] skip update data imported status (neo)
return null;
}
private Boolean refreshTrialBalance(int period, Integer serviceTypeId, String userID) {
......@@ -108,18 +134,18 @@ public class DataImportServiceImpl {
List<CompanyBalance> list = companyBalanceMapper.selectByExample(companyBalanceExample);
if (list != null && !list.isEmpty()) {
//同步账套 TODO: sync admin db and project db(neo)
//同步账套
synAccountFromTaxAdmin(period, userID);
//先删除数据
clearTableData(period, BALANCE_TABLE);
clearTableData(period, _balancetable);
mapper.deleteTbValidation(period, EnumValidationType.TrialBalance.getCode());
CompanyBalanceExample example = new CompanyBalanceExample();
example.createCriteria().andPeriodIdEqualTo(period);
List<CompanyBalance> companyBalanceList = companyBalanceMapper.selectByExample(example);
validationAcctCode(period, userID);
initBalance(companyBalanceList, period, serviceTypeId);
// calculateYearColumn();
// duplicatedValidation(period, userID);
calculateYearColumn();
// duplicatedValidation(period, userID);//todo:[validation] to be open future(neo)
// validationPeriodBal(period, userID);
// validationDebitCreditBalance(period, userID);
// validataionBegDebitAndBegCredit(period, userID);
......@@ -135,6 +161,60 @@ public class DataImportServiceImpl {
return true;
}
private void calculateYearColumn() {
Integer maxMonth= dataImportServiceMapper.maxBalancePeriod();
if(maxMonth!=null&&maxMonth.intValue()>0) {
String sql = "update Balance b, (select sum(DebitBal) as sDebitBal, sum(CreditBal) as sCreditBal ,acctcode, 1 as PeriodId from Balance where PeriodId = 1 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal, sum(CreditBal) as sCreditBal ,acctcode, 2 as PeriodId from Balance where PeriodId <= 2 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 3 as PeriodId from Balance where PeriodId <= 3 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 4 as PeriodId from Balance where PeriodId <= 4 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 5 as PeriodId from Balance where PeriodId <= 5 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 6 as PeriodId from Balance where PeriodId <= 6 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 7 as PeriodId from Balance where PeriodId <= 7 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 8 as PeriodId from Balance where PeriodId <= 8 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 9 as PeriodId from Balance where PeriodId <= 9 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 10 as PeriodId from Balance where PeriodId <= 10 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 11 as PeriodId from Balance where PeriodId <= 11 " +
" group by acctcode " +
" union all " +
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 12 as PeriodId from Balance where PeriodId <=12 " +
" group by acctcode " +
" ) as tmp " +
" set YearDebitBal = sDebitBal, YearCreditBal = sCreditBal where b.acctcode = tmp.acctcode and b.PeriodId = tmp.PeriodId ";
exeCommand(sql);
}
}
private void exeCommand(String command) {
try (SqlSession sqlSession = dynamicSqlSessionTemplate.getSqlSessionFactory().openSession();) {
ScriptRunner scriptRunner = new ScriptRunner(sqlSession.getConnection());
scriptRunner.setAutoCommit(true);
scriptRunner.runScript(new StringReader(command));
}
}
private List<Balance> initBalance(List<CompanyBalance> companyBalanceList, int period, Integer serviceTypeId) {
List<Balance> addlist = new ArrayList<>();
List<VatEnterpriseAccount> vats = vatEnterpriseAccountMapper.selectByExample(null);
......@@ -412,12 +492,162 @@ public class DataImportServiceImpl {
Set<String> accts = new HashSet<>();
reult.forEach(m -> accts.add(m.getAcctCode()));
accts.forEach(m -> addList(addlist, m, period));
DealCodeNotInBalance(addlist,period);
DealCodeNotInBalance(addlist, period);
}
//处理子科目导入,但父科目没导入的情况,需要计算父科目
private void DealCodeNotInBalance(List<Balance> addlist, int period) {//Todo(to be continue neo)
private void DealCodeNotInBalance(List<Balance> addlist, int period) {
Set<String> acctcodes = new HashSet<>();
for (Balance b : addlist) {
if (b.getPeriodId().intValue() == period) {
acctcodes.add(b.getAcctCode());
}
}
VatEnterpriseAccountExample example = new VatEnterpriseAccountExample();
example.createCriteria().andAcctCodeIn(Lists.newArrayList(acctcodes));
List<VatEnterpriseAccount> result = vatEnterpriseAccountMapper.selectByExample(example);
List<BalanceAccountDto> balanceAddList = new ArrayList<>();
if (result != null && !result.isEmpty()) {
addlist.forEach(c -> {
result.forEach(a -> {
if (c.getAcctCode() == a.getAcctCode() && c.getPeriodId().intValue() == period) {
BalanceAccountDto bad = new BalanceAccountDto();
bad.BalanceId = c.getBalanceId();
bad.PeriodId = c.getPeriodId();
bad.AcctCode = c.getAcctCode();
bad.BegDebitBal = c.getBegDebitBal();
bad.BegCreditBal = c.getBegCreditBal();
bad.DebitBal = c.getDebitBal();
bad.CreditBal = c.getCreditBal();
bad.BegBal = c.getBegBal();
bad.EndBal = c.getEndBal();
bad.EndDebitBal = c.getEndDebitBal();
bad.EndCreditBal = c.getEndCreditBal();
bad.AcctLevel = a.getAcctLevel();
bad.ParentCode = a.getParentCode();
bad.Direction = a.getDirection();
bad.StdCode = a.getStdCode();
bad.IsDummy = c.getIsDummy();
balanceAddList.add(bad);
}
});
});
balanceAddList.stream().sorted(Comparator.comparing(BalanceAccountDto::getAcctCode));
}
//补全的科目数据
List<BalanceAccountDto> balanceInsertList = balanceAddList.stream().filter(m -> m.IsDummy == 0).collect(Collectors.toList());
if (balanceInsertList.size() == 0) {
return;
}
List<BalanceAccountDto> list = balanceInsertList.stream().filter(x -> x.AcctLevel != null).collect(Collectors.toList());
if (list != null && list.size() > 0) {
int maxLevel = balanceInsertList.stream().max(Comparator.comparing(BalanceAccountDto::getAcctLevel)).get().getAcctLevel();
while (maxLevel > 0) {
int finalMaxLevel = maxLevel;
List<BalanceAccountDto> toDealList = balanceInsertList.stream().filter(m -> m.getAcctLevel() == finalMaxLevel).distinct().collect(Collectors.toList());
for (BalanceAccountDto dto : toDealList) {
Balance balanceDto = addlist.stream().filter(x -> x.getBalanceId() == dto.BalanceId).findFirst().get();
BalanceAccountDto addDto = balanceAddList.stream().filter(x -> x.BalanceId == dto.BalanceId).findFirst().get();
//判断在原来CompanyBalance表中有子科目或者孙子科目
List<BalanceAccountDto> oldChildList = balanceAddList.stream().filter(a -> a.AcctCode.startsWith(dto.AcctCode)
&& a.AcctCode != dto.AcctCode && a.IsDummy == null).distinct().collect(Collectors.toList());
//获取所有子节点
List<BalanceAccountDto> childList = balanceAddList.stream().filter(a -> a.ParentCode == dto.AcctCode).distinct().collect(Collectors.toList());
//在原来CompanyBalance表中有子科目或者孙子科目
if (oldChildList != null && oldChildList.size() > 0) {
balanceDto.setIsDummy(1);
addDto.IsDummy = 1;
} else {
balanceDto.setIsDummy(0);
addDto.IsDummy = 0;
}
if (childList != null && childList.size() > 0) {
BigDecimal begBal = new BigDecimal(0);
BigDecimal debitBal = new BigDecimal(0);
BigDecimal crediBal = new BigDecimal(0);
for (BalanceAccountDto bad : childList) {
begBal = begBal.add(bad.BegBal.multiply(new BigDecimal(bad.Direction)).multiply(new BigDecimal(dto.Direction)));
debitBal = debitBal.add(bad.DebitBal);
crediBal = crediBal.add(bad.CreditBal);
}
balanceDto.setBegBal(begBal);
balanceDto.setDebitBal(debitBal);
balanceDto.setCreditBal(crediBal);
balanceDto.setEndBal(balanceDto.getEndBal().add(balanceDto.getDebitBal().subtract(balanceDto.getCreditBal())).multiply(new BigDecimal(dto.Direction)));
if (dto.Direction == 1) {
balanceDto.setBegDebitBal(balanceDto.getBegBal());
balanceDto.setBegCreditBal(new BigDecimal(0));
balanceDto.setEndDebitBal(balanceDto.getEndBal());
balanceDto.setEndCreditBal(new BigDecimal(0));
} else {
balanceDto.setBegDebitBal(new BigDecimal(0));
balanceDto.setBegCreditBal(balanceDto.getBegBal());
balanceDto.setEndDebitBal(new BigDecimal(0));
balanceDto.setEndCreditBal(balanceDto.getEndBal());
}
//修改关联列表值
addDto.BegBal = balanceDto.getBegBal();
addDto.DebitBal = balanceDto.getDebitBal();
addDto.CreditBal = balanceDto.getCreditBal();
addDto.EndBal = balanceDto.getEndBal();
addDto.BegDebitBal = balanceDto.getBegDebitBal();
addDto.BegCreditBal = balanceDto.getBegCreditBal();
addDto.EndDebitBal = balanceDto.getEndDebitBal();
addDto.EndCreditBal = balanceDto.getEndCreditBal();
}
}
maxLevel--;
}
}
AddNextPeriodData(addlist, period);
}
private void AddNextPeriodData(List<Balance> addlist, int period) {
if (period < 12) {
for (int i = period + 1; i <= 12; i++) {
//判断是否已经导入过数据
CompanyBalanceExample example = new CompanyBalanceExample();
example.createCriteria().andPeriodIdEqualTo(i);
List<CompanyBalance> companylist = companyBalanceMapper.selectByExample(example);
if (companylist != null && companylist.size() > 0) {
break;
} else {
List<Balance> periodList = addlist.stream().filter(x -> x.getPeriodId() == i - 1).collect(Collectors.toList());
int finalI = i;
periodList.forEach(dto -> {
Balance balance = new Balance();
balance.setBalanceId(UUID.randomUUID().toString());
balance.setPeriodId(finalI);
balance.setAcctCode(dto.getAcctCode());
balance.setCustomerCode(dto.getCustomerCode());
balance.setBegDebitBal(dto.getEndDebitBal());
balance.setBegCreditBal(dto.getEndCreditBal());
balance.setBegBal(dto.getEndBal());
balance.setEndBal(dto.getEndBal());
balance.setEndDebitBal(dto.getEndDebitBal());
balance.setEndCreditBal(dto.getEndCreditBal());
balance.setDebitBal(new BigDecimal(0));
balance.setCreditBal(new BigDecimal(0));
balance.setMonthId(finalI);
balance.setIsDummy(null);
;
addlist.add(balance);
});
//删除该月的balance表数据
BalanceExample example1 = new BalanceExample();
example1.createCriteria().andPeriodIdEqualTo(i);
balanceMapper.deleteByExample(example1);
}
}
}
}
private void addList(List<Balance> addlist, String acctCode, int period, BigDecimal value, ImportBalanceCount v) {
......@@ -455,7 +685,7 @@ public class DataImportServiceImpl {
addlist.add(balance);
}
private void validationAcctCode(int period, String userID) {//(todo to be continue (neo)
private void validationAcctCode(int period, String userID) {//(todo:[validation] to be continue (neo)
}
......@@ -526,7 +756,6 @@ public class DataImportServiceImpl {
}
});
}
});
}
......
......@@ -23,4 +23,8 @@ public interface DataImportServiceMapper extends MyVatMapper {
" AND v.AcctCode = e.AcctCode " +
"GROUP BY AcctCode")
public List<DataImportServiceImpl.ImportBalanceCount> resultBalance(@Param("period") int period, @Param("code") int code);
@Select(" select max(PeriodId) from balance")
Integer maxBalancePeriod();
}
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