Commit b2f256d2 by 张富康

周期工具类添加

1 parent ed654264
......@@ -95,4 +95,12 @@ public interface BusinessIntelligenceMapper {
* 获取所有失败记录
*/
List<BiInsertFailure> getFailureRecords();
/**
* 根据业务和时间获取对应bi记录
*/
List<BiRecord> getBiRecords(@Param("businessType") Integer businessType,
@Param("merchantIds") List<Integer> merchantIds,
@Param("startDate") String startDate,
@Param("endDate") String endDate);
}
\ No newline at end of file
package cn.com.glsx.dj.peanalysis.service;
import cn.com.glsx.dj.peanalysis.dto.*;
import cn.com.glsx.dj.peanalysis.dto.job.BiRecord;
import cn.com.glsx.dj.peanalysis.dto.req.UserdCarDataStats.*;
import cn.com.glsx.dj.peanalysis.dto.res.UserdCarDataStats.*;
import cn.com.glsx.dj.peanalysis.mapper.BusinessIntelligenceMapper;
import cn.com.glsx.dj.peanalysis.mapper.CarManageInfoMapper;
import cn.com.glsx.dj.peanalysis.mapper.ServiceMerchantMapper;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
......@@ -45,6 +47,8 @@ public class UsedCarDataStatsService {
private MerchantRemote merchantRemote;
@Resource
private CarManageInfoMapper carManageInfoMapper;
@Resource
private BusinessIntelligenceMapper businessIntelligenceMapper;
/**
* 经营情况-累计至今
......@@ -350,6 +354,33 @@ public class UsedCarDataStatsService {
* 存量门店分析
*/
public StockStoreAnalysisRes getStockStoreAnalysis(StockStoreAnalysisReq request) {
Integer brandId = request.getBrandId();
Integer provinceId = request.getProvinceId();
// 是否通过品牌,区域
boolean filterBoolean = ObjectUtil.isNotEmpty(brandId) || ObjectUtil.isNotEmpty(provinceId);
List<MerchantSimpleResponse> filterMerchantResponse;
List<Integer> merchantIdList;
if (filterBoolean) {
// 筛选品牌,区域后的商户id
CarBrandProvinceRequestDTO carBrandProvinceRequestDTO = new CarBrandProvinceRequestDTO();
carBrandProvinceRequestDTO.setFirstBrandIdList(Collections.singletonList(brandId));
carBrandProvinceRequestDTO.setProvinceIds(Collections.singletonList(provinceId));
RpcResponse<List<MerchantSimpleResponse>> filterMerchantIdsResponse = merchantRemote.findMerchantListByBrandIdsProvince(carBrandProvinceRequestDTO);
filterMerchantResponse = filterMerchantIdsResponse.getResult();
// 如果为空则返回空结果
if (CollUtil.isEmpty(filterMerchantResponse)) {
return new StockStoreAnalysisRes();
}
merchantIdList = filterMerchantResponse.stream().map(MerchantSimpleResponse::getMerchantNo).collect(Collectors.toList());
// 查询dj_car_life_service_merchant_bi
List<BiRecord> biRecords = businessIntelligenceMapper.getBiRecords(5, merchantIdList, request.getStartTime(), request.getEndTime());
// 如果为空则返回空结果
if (CollUtil.isEmpty(biRecords)) {
return new StockStoreAnalysisRes();
}
}
return null;
}
......
package cn.com.glsx.dj.peanalysis.utils;
import cn.com.glsx.dj.peanalysis.utils.response.PeriodDescription;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
public class DateUtils {
......@@ -15,7 +24,7 @@ public class DateUtils {
*
* @param startTime 本期开始时间(字符串)
* @param endTime 本期结束时间(字符串)
* @param dateType 日期类型:1=日, 2=周, 3=月, 0=自定义
* @param dateType 日期类型:1-天 2-周 3-月 4-自定义
* @return Pair<上期开始时间, 上期结束时间>
*/
public static Pair<String, String> getLastPeriodDates(String startTime, String endTime, Integer dateType) {
......@@ -74,4 +83,165 @@ public class DateUtils {
return Pair.of("", "");
}
}
/**
* 获取指定周期的前N个周期日期范围(包含当前周期)
*
* @param startTime 本期开始时间(字符串,格式:yyyy-MM-dd)
* @param endTime 本期结束时间(字符串,格式:yyyy-MM-dd)
* @param dateType 日期类型:1-天 2-周 3-月 4-自定义
* @param n 需要获取的周期个数(n=0时返回空列表)
* @return List<Pair<String, String>> 包含n个周期的开始/结束时间,按时间顺序从近到远排列
*/
public static List<Pair<String, String>> getPreviousPeriodsIncludingCurrent(String startTime, String endTime, Integer dateType, int n) {
// 验证参数
if (n <= 0) {
return new ArrayList<>();
}
if (StrUtil.isEmpty(startTime) || StrUtil.isEmpty(endTime)) {
return new ArrayList<>();
}
List<Pair<String, String>> result = new ArrayList<>();
String currentStart = startTime;
String currentEnd = endTime;
try {
// 解析当前时间
Date start = DateUtil.parse(startTime, DatePattern.NORM_DATE_PATTERN);
Date end = DateUtil.parse(endTime, DatePattern.NORM_DATE_PATTERN);
// 添加当前周期
if (dateType == 4) {
result.add(Pair.of(currentEnd, currentEnd));
} else {
result.add(Pair.of(currentStart, currentEnd));
}
// 如果n=1,直接返回
if (n == 1) {
return result;
}
// 计算前n-1个周期
for (int i = 1; i < n; i++) {
Date lastStart = null;
Date lastEnd = null;
// 根据日期类型计算上一周期
if (ObjectUtil.isNull(dateType)) {
dateType = 0; // 默认自定义
}
switch (dateType) {
case 1: // 日
lastStart = DateUtil.offsetDay(start, -1);
lastEnd = DateUtil.offsetDay(end, -1);
break;
case 2: // 周
lastStart = DateUtil.offsetWeek(start, -1);
lastEnd = DateUtil.offsetWeek(end, -1);
break;
case 3: // 月
lastStart = DateUtil.offsetMonth(start, -1);
lastEnd = DateUtil.offsetMonth(end, -1);
break;
case 4: // 自定义
lastStart = DateUtil.offsetDay(end, -1);
lastEnd = DateUtil.offsetDay(end, -1);
break;
}
// 格式化为字符串
String lastStartStr = DateUtil.format(lastStart, DatePattern.NORM_DATE_PATTERN);
String lastEndStr = DateUtil.format(lastEnd, DatePattern.NORM_DATE_PATTERN);
// 检查计算结果是否有效
if (StrUtil.isEmpty(lastStartStr) || StrUtil.isEmpty(lastEndStr)) {
// 计算失败时停止,返回已收集的周期
break;
}
// 添加到结果列表
result.add(Pair.of(lastStartStr, lastEndStr));
// 更新当前周期为上一周期
start = lastStart;
end = lastEnd;
}
} catch (Exception e) {
log.error("计算周期失败,startTime: {}, endTime: {}, dateType: {}, n: {}",
startTime, endTime, dateType, n, e);
// 返回空值或使用其他默认逻辑
return new ArrayList<>();
}
return result;
}
/**
* 格式化周期列表为包含日期描述和日期范围的结构
* @param periods 原始周期列表 (由getPreviousPeriodsIncludingCurrent返回)
* @param dateType 日期类型 (1-天 2-周 3-月 4-自定义)
* @return 包含日期描述和日期范围的列表
*/
public static List<PeriodDescription> formatPeriodDescriptions(List<Pair<String, String>> periods, Integer dateType) {
if (periods == null || periods.isEmpty() || dateType == null) {
return new ArrayList<>();
}
List<PeriodDescription> result = new ArrayList<>();
for (int i = 0; i < periods.size(); i++) {
Pair<String, String> period = periods.get(i);
String dateDescription;
switch (dateType) {
case 1: // 天
String dayStr = period.getKey().substring(8, 10);
int day = NumberUtils.toInt(dayStr, 0);
dateDescription = day + "号";
break;
case 2: // 周
if (i == 0) {
dateDescription = "当前周";
} else {
dateDescription = "前" + i + "周";
}
break;
case 3: // 月
String monthStr = period.getKey().substring(5, 7);
int month = NumberUtils.toInt(monthStr, 0);
dateDescription = month + "月";
break;
case 4: // 自定义
String dayStr4 = period.getKey().substring(8, 10);
int day4 = NumberUtils.toInt(dayStr4, 0);
dateDescription = day4 + "号";
break;
default:
dateDescription = period.getKey() + " - " + period.getValue();
}
result.add(new PeriodDescription(dateDescription, period));
}
return result;
}
public static void main(String[] args) {
List<Pair<String, String>> previousPeriodsIncludingCurrent = getPreviousPeriodsIncludingCurrent("2025-12-01", "2022-12-07", 2, 8);
// System.out.println(previousPeriodsIncludingCurrent);
List<PeriodDescription> periodDescriptions = formatPeriodDescriptions(previousPeriodsIncludingCurrent, 2);
for (PeriodDescription periodDescription : periodDescriptions) {
System.out.println(periodDescription);
}
}
}
package cn.com.glsx.dj.peanalysis.utils.response;
import cn.hutool.core.lang.Pair;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 周期描述类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PeriodDescription {
/**
* 周期描述
*/
private String dateDescription;
/**
* 周期范围
*/
private Pair<String, String> datePair;
}
\ No newline at end of file
......@@ -566,7 +566,48 @@
ORDER BY failure_time DESC
</select>
<select id="getBiRecords" resultType="cn.com.glsx.dj.peanalysis.dto.job.BiRecord">
select merchant_id as merchantId,
stat_date as statDate,
has_benefits as hasBenefits,
has_ext_warranty as hasExtWarranty,
has_light_mod as hasLightMod,
has_used_car as hasUsedCar,
benefits_store_type as benefitsStoreType,
ext_warranty_store_type as extWarrantyStoreType,
light_mod_store_type as lightModStoreType,
used_car_store_type as usedCarStoreType,
created_by as createdBy,
created_date as createdDate,
updated_by as updatedBy,
updated_date as updatedDate,
from dj_car_life_service_merchant_bi
where <![CDATA[
DATE(stat_date) >= #{startDate}
and DATE(stat_date) <= #{endDate}
]]>
<if test="merchantIds != null and merchantIds.size > 0">
and merchant_id in
<foreach item="item" index="index" collection="merchantIds" separator="," open="(" close=")">
#{item}
</foreach>
</if>
<if test="businessType != null">
<if test=" businessType == 2">
and has_ext_warranty = 1
</if>
<if test="businessType == 3">
and has_benefits = 1
</if>
<if test="businessType == 4">
and has_light_mod = 1
</if>
<if test="businessType == 5">
and has_used_car = 1
</if>
</if>
</select>
</mapper>
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!