|
@@ -1,30 +1,45 @@
|
|
|
package com.fjhx.form.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.fjhx.common.constant.SourceConstant;
|
|
|
import com.fjhx.common.utils.Assert;
|
|
|
import com.fjhx.common.utils.excel.util.ExcelUtil;
|
|
|
+import com.fjhx.customer.entity.customer.po.Customer;
|
|
|
+import com.fjhx.customer.entity.customer.vo.CustomerVo;
|
|
|
+import com.fjhx.customer.service.customer.CustomerService;
|
|
|
import com.fjhx.form.entity.*;
|
|
|
+import com.fjhx.form.entity.customer.po.CustomerPayableBalance;
|
|
|
import com.fjhx.form.mapper.ReportMapper;
|
|
|
import com.fjhx.form.service.ReportService;
|
|
|
+import com.fjhx.form.service.customer.CustomerPayableBalanceService;
|
|
|
import com.fjhx.item.service.product.ProductInfoService;
|
|
|
import com.fjhx.mes.entity.report.po.ReportLossesDetails;
|
|
|
import com.fjhx.mes.service.report.ReportLossesDetailsService;
|
|
|
import com.ruoyi.common.core.domain.entity.SysDept;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
import com.ruoyi.common.utils.wrapper.IWrapper;
|
|
|
import com.ruoyi.common.utils.wrapper.SqlField;
|
|
|
import com.ruoyi.system.service.ISysDeptService;
|
|
|
+import com.ruoyi.system.service.ISysUserService;
|
|
|
import com.ruoyi.system.utils.UserUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@@ -35,12 +50,20 @@ public class ReportServiceImpl implements ReportService {
|
|
|
private final ProductInfoService productInfoService;
|
|
|
private final ISysDeptService deptService;
|
|
|
|
|
|
+ private final CustomerPayableBalanceService customerPayableBalanceService;
|
|
|
+
|
|
|
+ private final CustomerService customerService;
|
|
|
+ private final ISysUserService userService;
|
|
|
+
|
|
|
@Autowired
|
|
|
- public ReportServiceImpl(ReportMapper reportMapper, ReportLossesDetailsService reportLossesDetailsService, ProductInfoService productInfoService, ISysDeptService sysDeptService) {
|
|
|
+ public ReportServiceImpl(ReportMapper reportMapper, ReportLossesDetailsService reportLossesDetailsService, ProductInfoService productInfoService, ISysDeptService sysDeptService, CustomerPayableBalanceService customerPayableBalanceService, CustomerService customerService, ISysUserService userService) {
|
|
|
this.reportMapper = reportMapper;
|
|
|
this.reportLossesDetailsService = reportLossesDetailsService;
|
|
|
this.productInfoService = productInfoService;
|
|
|
this.deptService = sysDeptService;
|
|
|
+ this.customerPayableBalanceService = customerPayableBalanceService;
|
|
|
+ this.customerService = customerService;
|
|
|
+ this.userService = userService;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -296,4 +319,139 @@ public class ReportServiceImpl implements ReportService {
|
|
|
list.add(sumBo);
|
|
|
return list;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<JSONObject> getReceivableAccounts(ReceivableAccountsSelectDto dto) {
|
|
|
+ Integer year = dto.getYear();
|
|
|
+ Assert.notEmpty(year, "年份不能为空");
|
|
|
+
|
|
|
+ //获取一年里有交易的客户信息
|
|
|
+ List<CustomerMonthlyAccounts> customerMonthlyAccounts = reportMapper.getCustomerMonthlyAccounts(year);
|
|
|
+ Map<Long, List<CustomerMonthlyAccounts>> monthlyAccountsMap = customerMonthlyAccounts
|
|
|
+ .stream().collect(Collectors.groupingBy(CustomerMonthlyAccounts::getBuyCorporationId));
|
|
|
+ //获取年初额度
|
|
|
+ Map<Long, CustomerPayableBalance> lastPayableBalanceMap = customerPayableBalanceService.mapKEntity(
|
|
|
+ CustomerPayableBalance::getCustomerId,
|
|
|
+ q -> q.eq(CustomerPayableBalance::getAccountDate, StrUtil.format("{}-12", year - 1))
|
|
|
+ );
|
|
|
+
|
|
|
+ //获取今年的调整信息
|
|
|
+ Map<Long, List<CustomerPayableBalance>> payableBalanceListMap = customerPayableBalanceService.mapKGroup(
|
|
|
+ CustomerPayableBalance::getCustomerId,
|
|
|
+ q -> q.apply("YEAR(account_date) = {0}", year)
|
|
|
+ );
|
|
|
+ List<Long> customerIds = customerMonthlyAccounts.stream().map(CustomerMonthlyAccounts::getBuyCorporationId).distinct().collect(Collectors.toList());
|
|
|
+ List<CustomerVo> customerList = new ArrayList<>();
|
|
|
+ if (ObjectUtil.isNotEmpty(customerIds)) {
|
|
|
+ customerList = customerService.getList(IWrapper.<CustomerVo>getWrapper().in("c", Customer::getId, customerIds));
|
|
|
+ //赋值业务员信息
|
|
|
+ UserUtil.assignmentNickName(customerList, CustomerVo::getUserId, CustomerVo::setUserName);
|
|
|
+ }
|
|
|
+ Map<Long, CustomerVo> customerMap = customerList.stream().collect(Collectors.toMap(CustomerVo::getId, Function.identity()));
|
|
|
+
|
|
|
+ List<JSONObject> outList = new ArrayList<>();
|
|
|
+ for (Long customerId : customerIds) {
|
|
|
+ //获取年初信息
|
|
|
+ BigDecimal yearBeginBalance = BigDecimal.ZERO;
|
|
|
+ CustomerPayableBalance customerPayableBalance = lastPayableBalanceMap.get(customerId);
|
|
|
+ if (ObjectUtil.isNotNull(customerPayableBalance)) {
|
|
|
+ yearBeginBalance = customerPayableBalance.getBalance();
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取每月信息
|
|
|
+ List<CustomerMonthlyAccounts> monthlyAccountsList = monthlyAccountsMap.getOrDefault(customerId, new ArrayList<>());
|
|
|
+ Map<Integer, CustomerMonthlyAccounts> itemMonthlyAccountsMap = monthlyAccountsList
|
|
|
+ .stream().collect(Collectors.toMap(CustomerMonthlyAccounts::getDataMonth, Function.identity()));
|
|
|
+
|
|
|
+ //获取调整信息
|
|
|
+ List<CustomerPayableBalance> payableBalanceList = payableBalanceListMap.getOrDefault(customerId, new ArrayList<>());
|
|
|
+ Map<Date, CustomerPayableBalance> payableBalanceMap = payableBalanceList
|
|
|
+ .stream().collect(Collectors.toMap(CustomerPayableBalance::getAccountDate, Function.identity()));
|
|
|
+
|
|
|
+ //上月余额
|
|
|
+ BigDecimal lastBalanceAmount = yearBeginBalance;
|
|
|
+
|
|
|
+ CustomerVo customer = customerMap.getOrDefault(customerId, new CustomerVo());
|
|
|
+
|
|
|
+ JSONObject dataJson = new JSONObject();
|
|
|
+ dataJson.put("customerId", customerId);
|
|
|
+ dataJson.put("customerName", customer.getName());
|
|
|
+ dataJson.put("customerShortName", customer.getShortName());
|
|
|
+ dataJson.put("saleUserName", customer.getUserName());
|
|
|
+ dataJson.put("yearBeginBalance", yearBeginBalance);
|
|
|
+ for (int i = 1; i <= 12; i++) {
|
|
|
+ //获取应收 已收
|
|
|
+ BigDecimal receivableAmount = BigDecimal.ZERO;
|
|
|
+ BigDecimal receivedAmount = BigDecimal.ZERO;
|
|
|
+ CustomerMonthlyAccounts customerMonthlyAccounts1 = itemMonthlyAccountsMap.get(i);
|
|
|
+ if (ObjectUtil.isNotEmpty(customerMonthlyAccounts1)) {
|
|
|
+ receivableAmount = customerMonthlyAccounts1.getTruckAmount();
|
|
|
+ //调整为负数
|
|
|
+ receivedAmount = BigDecimal.ZERO.subtract(customerMonthlyAccounts1.getClaimMonth());
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取调整
|
|
|
+ BigDecimal adjustAmount = BigDecimal.ZERO;
|
|
|
+ CustomerPayableBalance customerPayableBalance1 = payableBalanceMap
|
|
|
+ .get(DateUtil.parse(StrUtil.format("{}-{}", year, i), "yyyy-MM"));
|
|
|
+ if (ObjectUtil.isNotEmpty(customerPayableBalance1)) {
|
|
|
+ adjustAmount = customerPayableBalance1.getAdjust();
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算余额
|
|
|
+ BigDecimal balanceAmount = lastBalanceAmount.add(receivableAmount).add(receivedAmount).add(adjustAmount);
|
|
|
+
|
|
|
+ //赋值上月余额
|
|
|
+ lastBalanceAmount = balanceAmount;
|
|
|
+
|
|
|
+
|
|
|
+ JSONObject itemJson = new JSONObject();
|
|
|
+ itemJson.put("receivableAmount", receivableAmount);
|
|
|
+ itemJson.put("receivedAmount", receivedAmount);
|
|
|
+ itemJson.put("adjustAmount", adjustAmount);
|
|
|
+ itemJson.put("balanceAmount", balanceAmount);
|
|
|
+ dataJson.put("" + i, itemJson);
|
|
|
+ }
|
|
|
+ outList.add(dataJson);
|
|
|
+ }
|
|
|
+
|
|
|
+ return outList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 每月1日0点1分 赋值客户上月应付余额
|
|
|
+ */
|
|
|
+// @PostConstruct
|
|
|
+ @Scheduled(cron = "0 1 0 1 * ?")
|
|
|
+ public void autoSaveReceivableAccountsEnding() {
|
|
|
+ DynamicDataSourceContextHolder.push(SourceConstant.BASE);
|
|
|
+ SecurityUtils.setTenantId("000000");
|
|
|
+
|
|
|
+ ReceivableAccountsSelectDto dto = new ReceivableAccountsSelectDto();
|
|
|
+ DateTime lastMonth = DateUtil.offsetMonth(new Date(), -1);
|
|
|
+ dto.setYear(DateUtil.year(lastMonth));
|
|
|
+ List<JSONObject> receivableAccounts = getReceivableAccounts(dto);
|
|
|
+ for (JSONObject receivableAccount : receivableAccounts) {
|
|
|
+ int month = DateUtil.month(lastMonth) + 1;
|
|
|
+ JSONObject item = receivableAccount.getJSONObject("" + month);
|
|
|
+ Long customerId = receivableAccount.getLong("customerId");
|
|
|
+
|
|
|
+ DateTime parse = DateUtil.parse(StrUtil.format("{}-{}", dto.getYear(), month), "yyyy-MM");
|
|
|
+
|
|
|
+ CustomerPayableBalance customerPayableBalance = customerPayableBalanceService.getOne(q -> q
|
|
|
+ .eq(CustomerPayableBalance::getCustomerId, customerId)
|
|
|
+ .eq(CustomerPayableBalance::getAccountDate, parse)
|
|
|
+ );
|
|
|
+ if (ObjectUtil.isEmpty(customerPayableBalance)) {
|
|
|
+ customerPayableBalance = new CustomerPayableBalance();
|
|
|
+ customerPayableBalance.setCustomerId(customerId);
|
|
|
+ customerPayableBalance.setAccountDate(parse);
|
|
|
+ }
|
|
|
+ customerPayableBalance.setBalance(item.getBigDecimal("balanceAmount"));
|
|
|
+ customerPayableBalanceService.saveOrUpdate(customerPayableBalance);
|
|
|
+ }
|
|
|
+
|
|
|
+ SecurityUtils.clearTenantId();
|
|
|
+ DynamicDataSourceContextHolder.clear();
|
|
|
+ }
|
|
|
}
|