|
@@ -0,0 +1,151 @@
|
|
|
+package com.fjhx.common.service.logistics.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.fastjson.JSONObject;
|
|
|
+import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.common.constant.SourceConstant;
|
|
|
+import com.fjhx.common.entity.logistics.dto.LogisticsEndingAmountSelectDto;
|
|
|
+import com.fjhx.common.entity.logistics.po.LogisticsCompanyInfo;
|
|
|
+import com.fjhx.common.entity.logistics.po.LogisticsEndingAmount;
|
|
|
+import com.fjhx.common.entity.logistics.vo.LogisticsEndingAmountVo;
|
|
|
+import com.fjhx.common.mapper.logistics.LogisticsEndingAmountMapper;
|
|
|
+import com.fjhx.common.service.logistics.LogisticsCompanyInfoService;
|
|
|
+import com.fjhx.common.service.logistics.LogisticsEndingAmountService;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.math.BigDecimal;
|
|
|
+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;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 物流期末余额 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author
|
|
|
+ * @since 2024-05-06
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class LogisticsEndingAmountServiceImpl extends ServiceImpl<LogisticsEndingAmountMapper, LogisticsEndingAmount> implements LogisticsEndingAmountService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private LogisticsCompanyInfoService logisticsCompanyInfoService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 每月1日0点1分 赋值上月期末余额
|
|
|
+ */
|
|
|
+ @PostConstruct
|
|
|
+ @Scheduled(cron = "0 1 0 1 * ?")
|
|
|
+ public void autoSaveEndingAmount() {
|
|
|
+ DynamicDataSourceContextHolder.push(SourceConstant.BASE);
|
|
|
+ SecurityUtils.setTenantId("000000");
|
|
|
+ DateTime lastMonth = DateUtil.offsetMonth(new Date(), -1);
|
|
|
+ int year = DateUtil.year(lastMonth);
|
|
|
+ int month = DateUtil.month(lastMonth) + 1;
|
|
|
+
|
|
|
+ LogisticsEndingAmountSelectDto dto = new LogisticsEndingAmountSelectDto();
|
|
|
+ dto.setYear(year);
|
|
|
+
|
|
|
+ List<JSONObject> jsonObjects = logisticsRechargeSummary(dto);
|
|
|
+ for (JSONObject json : jsonObjects) {
|
|
|
+ Long logisticsCompanyId = json.getLong("id");
|
|
|
+ LogisticsEndingAmountVo logisticsEndingAmountVo = json.getObject("" + month, LogisticsEndingAmountVo.class);
|
|
|
+
|
|
|
+ //更新期末余额
|
|
|
+ String format = StrUtil.format("{}-{}", year, month);
|
|
|
+ LogisticsEndingAmount one = this.getOne(q -> q
|
|
|
+ .eq(LogisticsEndingAmount::getLogisticsCompanyId, logisticsCompanyId)
|
|
|
+ .apply("DATE_FORMAT(account_date,'%Y-%m') = {0}", format)
|
|
|
+ );
|
|
|
+ if (ObjectUtil.isEmpty(one)) {
|
|
|
+ one = new LogisticsEndingAmount();
|
|
|
+ one.setLogisticsCompanyId(logisticsCompanyId);
|
|
|
+ one.setAccountDate(DateUtil.parse(format, "yyyy-MM"));
|
|
|
+ one.setEndingBalance(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+
|
|
|
+ //赋值期末余额
|
|
|
+ BigDecimal endingBalance = logisticsEndingAmountVo.getEndingBalance();
|
|
|
+ if (ObjectUtil.isEmpty(endingBalance)) {
|
|
|
+ endingBalance = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ one.setEndingBalance(endingBalance);
|
|
|
+
|
|
|
+ this.saveOrUpdate(one);
|
|
|
+ }
|
|
|
+ SecurityUtils.clearTenantId();
|
|
|
+ DynamicDataSourceContextHolder.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<JSONObject> logisticsRechargeSummary(LogisticsEndingAmountSelectDto dto) {
|
|
|
+ List<JSONObject> outList = new ArrayList<>();
|
|
|
+
|
|
|
+ //默认赋值年
|
|
|
+ if (ObjectUtil.isEmpty(dto.getYear())) {
|
|
|
+ dto.setYear(DateUtil.year(new Date()));
|
|
|
+ }
|
|
|
+
|
|
|
+ List<LogisticsEndingAmountVo> logisticsRechargeSummaryBos = baseMapper.logisticsRechargeSummary(dto);
|
|
|
+ Map<Long, List<LogisticsEndingAmountVo>> logisticsRechargeMap = logisticsRechargeSummaryBos.stream()
|
|
|
+ .collect(Collectors.groupingBy(LogisticsEndingAmountVo::getLogisticsCompanyId));
|
|
|
+
|
|
|
+ List<LogisticsCompanyInfo> list = logisticsCompanyInfoService.list();
|
|
|
+
|
|
|
+
|
|
|
+ for (LogisticsCompanyInfo logisticsCompanyInfo : list) {
|
|
|
+ JSONObject logisticsCompanyInfoJson = JSONObject.parseObject(JSONObject.toJSONString(logisticsCompanyInfo));
|
|
|
+ //赋值年初余额
|
|
|
+ BigDecimal yearBeginBalance = BigDecimal.ZERO;
|
|
|
+ String lastYearEndMonth = StrUtil.format("{}-12", dto.getYear() - 1);
|
|
|
+ LogisticsEndingAmount lastYearEndAmount = this.getOne(q -> q
|
|
|
+ .eq(LogisticsEndingAmount::getLogisticsCompanyId, logisticsCompanyInfo.getId())
|
|
|
+ .apply("DATE_FORMAT(account_date,'%Y-%m') = {0}", lastYearEndMonth)
|
|
|
+ );
|
|
|
+ if (ObjectUtil.isNotEmpty(lastYearEndAmount)) {
|
|
|
+ yearBeginBalance = lastYearEndAmount.getEndingBalance();
|
|
|
+ }
|
|
|
+ logisticsCompanyInfoJson.put("yearBeginBalance", yearBeginBalance);
|
|
|
+
|
|
|
+ //上一期末余额
|
|
|
+ BigDecimal lastEndingBalance = yearBeginBalance;
|
|
|
+
|
|
|
+ //赋值每一个月信息
|
|
|
+ List<LogisticsEndingAmountVo> list1 = logisticsRechargeMap.getOrDefault(logisticsCompanyInfo.getId(), new ArrayList<>());
|
|
|
+ Map<Integer, LogisticsEndingAmountVo> collect = list1.stream().collect(Collectors.toMap(LogisticsEndingAmountVo::getAccountPeriodMonth, Function.identity()));
|
|
|
+ for (int i = 1; i <= 12; i++) {
|
|
|
+ LogisticsEndingAmountVo logisticsRecharge = collect.getOrDefault(i, new LogisticsEndingAmountVo());
|
|
|
+ BigDecimal advanceAmount = logisticsRecharge.getAdvanceAmount();
|
|
|
+ if (advanceAmount == null) {
|
|
|
+ advanceAmount = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ BigDecimal deductionAmount = logisticsRecharge.getDeductionAmount();
|
|
|
+ if (deductionAmount == null) {
|
|
|
+ deductionAmount = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+
|
|
|
+ //期末余额 = 期初余额 + 预付 - 抵扣
|
|
|
+ logisticsRecharge.setEndingBalance(lastEndingBalance.add(advanceAmount).subtract(deductionAmount));
|
|
|
+
|
|
|
+ logisticsCompanyInfoJson.put("" + i, logisticsRecharge);
|
|
|
+ }
|
|
|
+
|
|
|
+ outList.add(logisticsCompanyInfoJson);
|
|
|
+ }
|
|
|
+
|
|
|
+ return outList;
|
|
|
+ }
|
|
|
+}
|