package cn.gson.financial.kernel.service.impl;
import cn.gson.financial.kernel.common.DateUtil;
import cn.gson.financial.kernel.common.DoubleComparer;
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.mapper.*;
import cn.gson.financial.kernel.model.vo.BalanceVo;
import cn.gson.financial.kernel.model.vo.UserVo;
import cn.gson.financial.kernel.model.vo.VoucherDetailVo;
import cn.gson.financial.kernel.service.SubjectService;
import cn.gson.financial.kernel.service.VoucherService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.DateFormat;
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 : ${PACKAGE_NAME}</li>
* <li>Version : 1.0</li>
* <li>Creation : 2019年07月30日</li>
* <li>@author : ____′↘夏悸</li>
* </ul>
* <p>****************************************************************************</p>
*/
@Slf4j
@Service
@AllArgsConstructor
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher> implements VoucherService {
private VoucherDetailsMapper detailsMapper;
private SubjectMapper subjectMapper;
private VoucherDetailsAuxiliaryMapper voucherDetailsAuxiliaryMapper;
private SubjectService subjectService;
private CheckoutMapper checkoutMapper;
private AccountSetsMapper accountSetsMapper;
@Override
public int batchInsert(List<Voucher> list) {
return baseMapper.batchInsert(list);
}
@Override
public List<Voucher> list(Wrapper<Voucher> queryWrapper) {
return baseMapper.selectVoucher(queryWrapper);
}
@Override
public IPage<Voucher> page(IPage<Voucher> page, Wrapper<Voucher> queryWrapper) {
return baseMapper.selectVoucher(page, queryWrapper);
}
@Override
public int loadCode(Integer accountSetsId, String word, Date currentAccountDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentAccountDate);
Integer code = baseMapper.selectMaxCode(accountSetsId, word, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY) + 1);
return code == null ? 1 : code;
}
@Override
public List accountBookDetails(Integer accountSetsId, Integer subjectId, Date accountDate, String subjectCode, Boolean showNumPrice) {
return this.summary(accountSetsId, subjectId, accountDate, subjectCode, showNumPrice, true);
}
@Override
public List accountGeneralLedger(Integer accountSetsId, Date accountDate, Boolean showNumPrice) {
List<Subject> subjectList = subjectService.accountBookList(accountDate, accountSetsId, showNumPrice);
List<Map<String, Object>> data = new ArrayList<>(subjectList.size());
subjectList.forEach(subject -> {
List<VoucherDetailVo> summary = this.summary(accountSetsId, subject.getId(), accountDate, subject.getCode(), showNumPrice, false);
Map<String, Object> item = new HashMap<>(2);
item.put("subject", subject);
item.put("summary", summary);
data.add(item);
});
return data;
}
/**
* 获取期间结转科目总和
*
* @param accountSetsId
* @param years
* @param month
* @param code
* @return
*/
@Override
public Map<String, VoucherDetails> carryForwardMoney(Integer accountSetsId, Integer years, Integer month, String[] code) {
Map<String, VoucherDetails> msv = new HashMap<>(code.length);
for (String s : code) {
VoucherDetails details = this.detailsMapper.selectCarryForwardMoney(accountSetsId, years, month, s + "%");
msv.put(s, details);
}
return msv;
}
/**
* 核算项目明细账
*
* @param accountSetsId
* @param auxiliaryId
* @param accountDate
* @param auxiliaryItemId
* @param showNumPrice
* @return
*/
@Override
public List<VoucherDetailVo> auxiliaryDetails(Integer accountSetsId, Integer auxiliaryId, Date accountDate, Integer auxiliaryItemId, Boolean showNumPrice) {
List<VoucherDetailVo> data = new ArrayList<>();
//期初
VoucherDetailVo startVo = getAuxiliaryStart(accountSetsId, auxiliaryId, accountDate, auxiliaryItemId);
data.add(startVo);
//明细
List<VoucherDetailVo> detailVos = voucherDetailsAuxiliaryMapper.selectAccountBookDetails(accountSetsId, auxiliaryId, accountDate, auxiliaryItemId);
//计算每次余额
for (VoucherDetailVo vo : detailVos) {
double b = 0d;
switch (vo.getBalanceDirection()) {
case "借":
b = DoubleValueUtil.getNotNullVal(vo.getDebitAmount()) - DoubleValueUtil.getNotNullVal(vo.getCreditAmount());
break;
case "贷":
b = DoubleValueUtil.getNotNullVal(vo.getCreditAmount()) - DoubleValueUtil.getNotNullVal(vo.getDebitAmount());
break;
}
vo.setBalance(startVo.getBalance() + b);
startVo = vo;
}
data.addAll(detailVos);
//本期统计
VoucherDetailVo currentVo = getAuxiliaryCurrent(accountSetsId, auxiliaryId, accountDate, auxiliaryItemId, data.get(0));
//年度统计
VoucherDetailVo yearVo = getAuxiliaryYear(accountSetsId, auxiliaryId, accountDate, auxiliaryItemId);
data.add(currentVo);
data.add(yearVo);
return data;
}
/**
* 获取期初统计
*
* @param accountSetsId
* @param auxiliaryId
* @param accountDate
* @param auxiliaryItemId
* @return
*/
private VoucherDetailVo getAuxiliaryStart(Integer accountSetsId, Integer auxiliaryId, Date accountDate, Integer auxiliaryItemId) {
List<VoucherDetailVo> startVos = voucherDetailsAuxiliaryMapper.selectAccountBookStatistical(accountSetsId, auxiliaryId, null, DateUtil.getMonthBegin(accountDate), auxiliaryItemId);
VoucherDetailVo startVo = new VoucherDetailVo();
startVo.setSummary("期初余额");
startVo.setVoucherDate(accountDate);
startVo.setBalance(0d);
startVo.setBalanceDirection("平");
if (startVos != null && !startVos.isEmpty()) {
if (startVos.get(0) != null) {
VoucherDetailVo vo = startVos.get(0);
startVo.setBalanceDirection(vo.getBalanceDirection());
switch (vo.getBalanceDirection()) {
case "借":
startVo.setBalance(DoubleValueUtil.getNotNullVal(vo.getDebitAmount()) - DoubleValueUtil.getNotNullVal(vo.getCreditAmount()));
break;
case "贷":
startVo.setBalance(DoubleValueUtil.getNotNullVal(vo.getCreditAmount()) - DoubleValueUtil.getNotNullVal(vo.getDebitAmount()));
break;
}
startVo.setCreditAmount(vo.getCreditAmount());
startVo.setDebitAmount(vo.getDebitAmount());
}
}
return startVo;
}
/*