package com.ydtech.modules.inv.service.impl; import com.alibaba.excel.util.ListUtils; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ydtech.core.page.HttpResult; import com.ydtech.core.page.PageRequest; import com.ydtech.exception.SystemException; import com.ydtech.modules.admin.constants.BusinessTypeEnum; import com.ydtech.modules.inv.dao.InvAccountMapper; import com.ydtech.modules.inv.model.*; import com.ydtech.modules.inv.model.dto.InvAccountCardQueryDto; import com.ydtech.modules.inv.model.dto.InvAccountOpDto; import com.ydtech.modules.inv.model.excel.AccountExcel; import com.ydtech.modules.inv.model.vo.InvAccountQueryVo; import com.ydtech.modules.inv.service.InvAccountOpService; import com.ydtech.modules.inv.service.InvAccountProfitService; import com.ydtech.modules.inv.service.InvAccountService; import com.ydtech.modules.inv.utils.EasyExcelUtils; import com.ydtech.modules.inv.utils.LocalDateTimeUtils; import com.ydtech.modules.order.entity.BasePageResult; import org.joda.time.LocalDateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.File; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.*; import java.util.stream.Collectors; /** * @author Administrator * @description 针对表【ins_account(账户信息表)】的数据库操作Service实现 * @createDate 2024-01-04 18:29:20 */ @Service public class InvAccountServiceImpl extends ServiceImpl implements InvAccountService { @Autowired private InvAccountMapper insAccountMapper; @Autowired private InvAccountProfitService invAccountProfitService; @Autowired private InvAccountOpService invAccountOpService; @Value("${upload.file.path}") String uploadFilePath; /** * 分页查询 * * @return */ @Override public IPage selectAccountsListPage(InvAccountQueryVo invAccountQueryVo) { PageRequest pageRequest = new PageRequest(); pageRequest.setPageNum(invAccountQueryVo.getPageNo()); pageRequest.setPageSize(invAccountQueryVo.getPageSize()); invAccountQueryVo.setInvoiceTimeStart(LocalDateTimeUtils.get12MonthsBefore()); // 获取12月前月的第一天 invAccountQueryVo.setInvoiceTimeEnd(LocalDateTimeUtils.getTodayLastDay()); //获取当前月最后一天 List invAccountIPage = insAccountMapper.selectAccountsListPage(invAccountQueryVo); for (InvAccount invAccount : invAccountIPage) { if (invAccount.getInvoicingAmountExcess() != null) { String invoicingAmountExcess = invAccount.getInvoicingAmountExcess(); BigDecimal amount = new BigDecimal(invoicingAmountExcess); BigDecimal roundedAmount = amount.setScale(2, RoundingMode.HALF_UP); invAccount.setInvoicingAmountExcess(roundedAmount.toString()); } else { invAccount.setInvoicingAmountExcess(null); } } //计算当前季度剩余300000减去 invAccountQueryVo.setInvoiceTimeStart(LocalDateTimeUtils.quarterStartTime()); invAccountQueryVo.setInvoiceTimeEnd(LocalDateTimeUtils.quarterEndTime()); List invAccountIPage1 = insAccountMapper.selectAccountsListPage(invAccountQueryVo); //将当前季度额度赋值 List collect = invAccountIPage.stream() .map(rel -> invAccountIPage1.stream() .filter(ord -> Objects.equals(rel.getAccountId(), ord.getAccountId())) .findFirst() .map(ord -> { if (ord.getInvoicingAmountExcess() != null) { rel.setQuarterAmountExcess(new BigDecimal("300000").subtract(new BigDecimal("5000000").subtract(new BigDecimal(ord.getInvoicingAmountExcess()))).toString()); rel.setSumInvoicingAmount("0"); } else { if (!rel.getBusinessQuality().equals("0")&&rel.getOuterFlag().equals("0")) { rel.setQuarterAmountExcess("300000"); } rel.setSumInvoicingAmount("0"); if (rel.getInvoicingAmountExcess() == null){ rel.setInvoicingAmountExcess(rel.getInvoicingAmount()); } } return rel; }).orElse(null) ).collect(Collectors.toList()); //将总开票额度赋值 List sumInvoicingAmount = insAccountMapper.getSumInvoicingAmount(invAccountQueryVo); List collect1 = collect.stream() .map(rel -> sumInvoicingAmount.stream() .filter(ord -> Objects.equals(rel.getAccountId(), ord.getAccountId())) .findFirst() .map(ord -> { if (ord.getSumInvoicingAmount() != null) { BigDecimal sumInvoicingAmountDecimal = new BigDecimal(rel.getSumInvoicingAmount()).add(new BigDecimal(ord.getSumInvoicingAmount())); DecimalFormat df = new DecimalFormat("0.00"); String sumInvoicingAmountString = df.format(sumInvoicingAmountDecimal); rel.setSumInvoicingAmount(sumInvoicingAmountString); } return rel; }).orElse(null) ).collect(Collectors.toList()); //本季度开票总额度和本年开票总额度 List sumYearAmount = insAccountMapper.getYearAndQuarterAmount(); collect1.forEach(invAccount -> { if (invAccount != null) { if (invAccount.getInvAccountCardList().size() < 2) { if (invAccount.getInvAccountCardList().get(0).getBankCardNum() == null) { invAccount.setInvAccountCardList(null); } } if (invAccount.getBusinessQuality().equals("3")) { invAccount.setQuarterAmountExcess(null); invAccount.setInvoicingAmountExcess(null); } } boolean found = false; for (InvAccountSumAmount yearAmount : sumYearAmount) { if (yearAmount.getAccountId().equals(invAccount.getAccountId())) { DecimalFormat df = new DecimalFormat("0.00"); String formattedYearAmount = df.format(Double.parseDouble(yearAmount.getYearAmount())); String formattedQuarterAmount = df.format(Double.parseDouble(yearAmount.getQuarterAmount())); invAccount.setYearAmount(formattedYearAmount); invAccount.setQuarterAmount(formattedQuarterAmount); found = true; } } if (!found) { invAccount.setYearAmount("0.00"); invAccount.setQuarterAmount("0.00"); } }); IPage ipage = PageRequest.buildPageRequest(pageRequest); ipage.setRecords(collect1.stream().skip((invAccountQueryVo.getPageNo() - 1) * invAccountQueryVo.getPageSize()).limit(invAccountQueryVo.getPageSize()). collect(Collectors.toList())); ipage.setTotal(collect1.size()); return ipage; } /** * 根据户名分类 * * @return */ @Override public HttpResult selectByAccountName() { InvAccountQueryVo invAccountQueryVo = new InvAccountQueryVo(); List insAccounts = insAccountMapper.selectAccountsList(invAccountQueryVo); if (!insAccounts.isEmpty()) { insAccounts.forEach(invAccountN -> { if (invAccountN.getInvAccountCardList().size() < 2) { if (invAccountN.getInvAccountCardList().get(0).getBankCardNum() == null) { invAccountN.setInvAccountCardList(null); } } invAccountN.setBusinessQuality(BusinessTypeEnum.getBusinessType(invAccountN.getBusinessQuality())); }); return HttpResult.ok("", insAccounts); } return HttpResult.error("查询错误"); } @Override public HttpResult selectAccountExcel(InvAccountQueryVo invAccountQueryVo) { List insAccounts = insAccountMapper.selectAccountsList(invAccountQueryVo); List invAccountProfitList = invAccountProfitService.list(); LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(InvAccountOp::getDelFlag,"0"); List list = invAccountOpService.list(lambdaQueryWrapper); File folder = new File(uploadFilePath); if (folder.exists()) { return HttpResult.ok("", EasyExcelUtils.noModelWrite(uploadFilePath, invAccountProfitList, insAccounts, list)); } else { if (folder.mkdirs()) { return HttpResult.ok("", EasyExcelUtils.noModelWrite(uploadFilePath, invAccountProfitList, insAccounts, list)); } else { throw new SystemException("文件导出失败"); } } } @Override public HttpResult selectAccountList(InvAccountQueryVo invAccountQueryVo) { invAccountQueryVo.setInvoiceTimeStart(LocalDateTimeUtils.get12MonthsBefore()); // 获取12月前月的第一天 invAccountQueryVo.setInvoiceTimeEnd(LocalDateTimeUtils.getTodayLastDay()); //获取当前月最后一天 List invAccountIPage = insAccountMapper.selectAccountsListPage(invAccountQueryVo); //计算当前季度剩余300000减去 invAccountQueryVo.setInvoiceTimeStart(LocalDateTimeUtils.quarterStartTime()); invAccountQueryVo.setInvoiceTimeEnd(LocalDateTimeUtils.quarterEndTime()); List invAccountIPage1 = insAccountMapper.selectAccountsListPage(invAccountQueryVo); //将当前季度额度赋值 List collect = invAccountIPage.stream() .map(rel -> invAccountIPage1.stream() .filter(ord -> Objects.equals(rel.getAccountId(), ord.getAccountId())) .findFirst() .map(ord -> { rel.setSumInvoicingAmount("0"); if (ord.getInvoicingAmountExcess() != null) { rel.setQuarterAmountExcess(new BigDecimal("300000").subtract(new BigDecimal("5000000").subtract(new BigDecimal(ord.getInvoicingAmountExcess()))).toString()); } else { if (!rel.getBusinessQuality().equals("0")&&rel.getOuterFlag().equals("0")) { rel.setQuarterAmountExcess("300000"); } rel.setSumInvoicingAmount("0"); if (rel.getInvoicingAmountExcess() == null){ rel.setInvoicingAmountExcess(rel.getInvoicingAmount()); } } return rel; }).orElse(null) ).collect(Collectors.toList()); //将总开票额度赋值 List sumInvoicingAmount = insAccountMapper.getSumInvoicingAmount(invAccountQueryVo); List collect1 = collect.stream() .map(rel -> sumInvoicingAmount.stream() .filter(ord -> Objects.equals(rel.getAccountId(), ord.getAccountId())) .findFirst() .map(ord -> { if (ord.getSumInvoicingAmount() != null) { BigDecimal sumSumInvoicingAmountDecimal = new BigDecimal(rel.getSumInvoicingAmount()).add(new BigDecimal(ord.getSumInvoicingAmount())); DecimalFormat df = new DecimalFormat("0.00"); String sumQuarterAmountExcessString = df.format(sumSumInvoicingAmountDecimal); rel.setSumInvoicingAmount(sumQuarterAmountExcessString); } return rel; }).orElse(null) ).collect(Collectors.toList()); collect1.forEach(invAccount -> { if (invAccount != null) { if (invAccount.getInvAccountCardList().size() < 2) { if (invAccount.getInvAccountCardList().get(0).getBankCardNum() == null) { invAccount.setInvAccountCardList(null); } } if (invAccount.getBusinessQuality().equals("3")) { invAccount.setQuarterAmountExcess(null); invAccount.setInvoicingAmountExcess(null); } } }); List accountExcels = new ArrayList<>(); collect1.forEach(invAccount -> { if (invAccount.getInvAccountCardList() != null) { AccountExcel accountExcel = new AccountExcel(); accountExcel.setAccountId(invAccount.getAccountId()); accountExcel.setAccountName(invAccount.getAccountName()); if (invAccount.getBusinessQuality() != null) { if (invAccount.getBusinessQuality().equals("3")) { accountExcel.setBusinessQuality("一般纳税"); } else if (invAccount.getBusinessQuality().equals("1")) { accountExcel.setBusinessQuality("个体户"); } else if (invAccount.getBusinessQuality().equals("2")) { accountExcel.setBusinessQuality("小规模"); } } accountExcel.setOwned(invAccount.getOwned()); if (invAccount.getEstablishTime() != null) { LocalDateTime establishTime = LocalDateTime.fromDateFields(invAccount.getEstablishTime()); accountExcel.setEstablishTime(establishTime.toString().substring(0, establishTime.toString().indexOf("T"))); } List sumYearAmount = insAccountMapper.getYearAndQuarterAmount(); boolean found = false; for (InvAccountSumAmount yearAmount : sumYearAmount) { if (yearAmount.getAccountId().equals(invAccount.getAccountId())) { DecimalFormat df = new DecimalFormat("0.00"); String formattedYearAmount = df.format(Double.parseDouble(yearAmount.getYearAmount())); String formattedQuarterAmount = df.format(Double.parseDouble(yearAmount.getQuarterAmount())); accountExcel.setYearAmount(formattedYearAmount); accountExcel.setQuarterAmount(formattedQuarterAmount); found = true; } } if (!found) { accountExcel.setYearAmount("0.00"); accountExcel.setQuarterAmount("0.00"); } accountExcel.setSumInvoicingAmount(invAccount.getSumInvoicingAmount()); accountExcel.setQuarterAmountExcess(invAccount.getQuarterAmountExcess()); accountExcel.setInvoicingAmountExcess(invAccount.getInvoicingAmountExcess()); accountExcel.setEstimate(invAccount.getTentativeEstimates()); accountExcels.add(accountExcel); } }); return HttpResult.ok("", EasyExcelUtils.downExcel(uploadFilePath, AccountExcel.class, accountExcels)); } @Override public HttpResult look(InvAccountQueryVo invAccountQueryVo) { List insAccounts = insAccountMapper.selectAccountsList(invAccountQueryVo); List invAccountProfitList = invAccountProfitService.list(); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); queryWrapper.eq(InvAccountOp::getDelFlag,"0"); List list = invAccountOpService.list(queryWrapper); List> head = head(invAccountProfitList); List> maps = dataList(insAccounts, list, invAccountProfitList); List> subList = maps.stream().skip((invAccountQueryVo.getPageNo()-1)*invAccountQueryVo.getPageSize()).limit(invAccountQueryVo.getPageSize()). collect(Collectors.toList()); InvAccountOpDto invAccountOpDto = new InvAccountOpDto(); invAccountOpDto.setHead(head); invAccountOpDto.setMaps(subList); invAccountOpDto.setTotal(maps.size()); invAccountOpDto.setPageSize(invAccountQueryVo.getPageSize()); invAccountOpDto.setPageNo(invAccountQueryVo.getPageNo()); return HttpResult.ok("",invAccountOpDto); } private static List> head(List invAccountProfitList) { List> list = ListUtils.newArrayList(); Map mm = new HashMap<>(); mm.put("accountXing", "账户性质"); mm.put("accountName", "户名"); mm.put("bank", "开户银行"); mm.put("banlce", "银行卡余额"); for (Map.Entry m : mm.entrySet()) { Map map = new HashMap<>(); map.put(m.getKey(), m.getValue()); map.put("eng",m.getKey()); list.add(map); } invAccountProfitList.forEach(invAccountProfit -> { Map map = new HashMap<>(); map.put(invAccountProfit.getProfitEng(), invAccountProfit.getProfitName()); map.put("eng",invAccountProfit.getProfitEng()); list.add(map); }); return list; } private static List> dataList(List invAccounts, List invAccountOpList, List invAccountProfitList) { List> map = new ArrayList<>(); invAccounts.forEach(invAccount -> { invAccount.getInvAccountCardList().forEach(invAccountCard -> { Map data = new HashMap<>(); if (invAccountCard.getBankCardNum() != null) { data.put("accountXing",invAccount.getOuterFlag().equals("0") ? "内部账户" : "外部账户"); data.put("accountName",invAccount.getAccountName()); data.put("bank",invAccountCard.getBankName()); data.put("banlce",invAccountCard.getBalance()); invAccountProfitList.forEach(invAccountProfitList1 -> { String a = "0"; for (InvAccountOp invAccountProfit : invAccountOpList) { if (invAccountProfitList1.getProfitEng().equals(invAccountProfit.getProfit()) && invAccountCard.getBankCardNum().equals(invAccountProfit.getCardNum())) { a = new BigDecimal(a).add(new BigDecimal(invAccountProfit.getMoney())).toString(); } } data.put(invAccountProfitList1.getProfitEng(),a); }); map.add(data); } }); }); return map; } /** * @version * @author: hxl * @Date: 2024/7/8 10:43 * @Description: 通过银行卡号和户名分页查询对象 */ @Override public BasePageResult queryPage(InvAccountCardQueryDto invAccountCardQueryDto) { Page page = new Page<>(invAccountCardQueryDto.getPageNum(), invAccountCardQueryDto.getPageSize()); Page result = insAccountMapper.queryPage(page, invAccountCardQueryDto); return new BasePageResult<>(invAccountCardQueryDto.getPageNum(), page.getSize(), result.getTotal(), page.getPages(), result.getRecords()); } @Override public List getInvAccountList(InvAccountQueryVo invAccountQueryVo) { return baseMapper.selectAccountsListPage(invAccountQueryVo); } }