package com.example.demo.serviceImpl.coin; import com.example.demo.Util.ProductRemoteClient; import com.example.demo.domain.DTO.ProductDTO; import com.example.demo.domain.entity.Rate; import com.example.demo.mapper.coin.GeneralMapper; import com.example.demo.service.coin.GeneralService; import org.apache.commons.lang3.time.StopWatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; /** * @program: GOLD * @ClassName GeneralServiceImpl * @description: * @author: huangqizhen * @create: 2025−06-22 11:01 * @Version 1.0 **/ @Service public class GeneralServiceImpl implements GeneralService { private static final Logger log = LoggerFactory.getLogger(GeneralServiceImpl.class); private final ProductRemoteClient remoteClient; @Autowired private GeneralMapper generalMapper; public GeneralServiceImpl(ProductRemoteClient remoteClient) { this.remoteClient = remoteClient; } @Override public List getMarket() { List list = generalMapper.getMarket(); return list; } @Override public List getALLAdminMarket() { List list = generalMapper.getALLAdminMarket(); return list; } @Override public List getPlatform() { List list = generalMapper.getPlatform(); return list; } @Override public List getGoods() { List list = generalMapper.getGoods(); // 使用 Java 8 的 Stream API 过滤 null 和空字符串 List filteredList = list.stream() .filter(Objects::nonNull) // 过滤掉 null 值 .filter(str -> !str.trim().isEmpty()) // 过滤掉空字符串和仅包含空白字符的字符串 .collect(Collectors.toList()); return filteredList; } @Override public List getActivity() { List list = generalMapper.getActivity(); List filteredList = list.stream() .filter(Objects::nonNull) // 过滤掉 null 值 .filter(str -> !str.trim().isEmpty()) // 过滤掉空字符串和仅包含空白字符的字符串 .collect(Collectors.toList()); return filteredList; } /* 获取昨天的日期 */ @Override public Date getYesterday() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_YEAR, -1); //当前天数-1 return getStartOfDay(cal.getTime()); //昨天的00:00:00 } /* 获取某天的开始时间(00:00:00) */ @Override public Date getStartOfDay(Date date) { LocalDateTime localDate = date.toInstant() .atZone(ZoneId.systemDefault()) // 转换为本地时区 .toLocalDateTime() .with(LocalTime.MIN); // 设置时间为当天 00:00:00 return Date.from(localDate.atZone(ZoneId.systemDefault()).toInstant()); } /* 转换日期格式为yyyy-MM-dd */ @Override public String formatDate(Date date) { return date.toInstant() .atZone(ZoneId.systemDefault()) .format(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE); // 输出格式如 "2025-10-01" } /* 获取时间段内的所有日期(包含起始和结束日) */ @Override public List getAllDatesBetween(Date start, Date end) { List dates = new ArrayList<>(); //初始化日历对象 Calendar tempStart = Calendar.getInstance(); tempStart.setTime(start); Calendar tempEnd = Calendar.getInstance(); tempEnd.setTime(end); while (!tempStart.after(tempEnd)) { dates.add(tempStart.getTime()); // 将当前日期添加到列表中 tempStart.add(Calendar.DAY_OF_YEAR, 1); // 每次增加一天 } return dates; } /* 获取角色的地区权限列表 */ @Override public List getAdminMarket(String account){ try { String market = generalMapper.getAdminMarket(account); if (market == null) { throw new Exception("没有地区权限"); } List list = Arrays.asList(market.split(",")); //判断是否是总部 if (list != null && list.contains("总部")||market.equals("研发部")) { List allMarkets = generalMapper.getMarket(); // 获取所有地区 allMarkets.remove("总部"); // 固定顺序 List fixed = Arrays.asList( "新加坡", "马来西亚", "香港", "加拿大", "泰国", "越南HCM", "韩国", "非网" ); // 先放固定顺序(存在的才放) List result = new ArrayList<>(); for (String m : fixed) { if (allMarkets.contains(m)) result.add(m); } // 其余按字母序追加 allMarkets.removeAll(fixed); Collections.sort(allMarkets); result.addAll(allMarkets); return result; } return list; } catch (Exception e) { // 记录日志 log.error("获取地区权限失败", e); // 重新抛出异常,或者根据需要返回一个默认值或空列表 throw new RuntimeException("获取地区权限失败", e); } } @Override public List getAdminMarket1(String account) { try { String market = generalMapper.getAdminMarket(account); if (market == null) { throw new Exception("没有地区权限"); } List list = Arrays.asList(market.split(",")); //判断是否是总部 if (list != null && list.contains("1")) { list=getMarket();//总部有所有地区的权限 } return list; } catch (Exception e) { // 记录日志 // log.error("获取地区权限失败", e); // 重新抛出异常,或者根据需要返回一个默认值或空列表 throw new RuntimeException("获取地区权限失败", e); } } /* 获取觉得的地区列表 */ @Override public List getRoleMarket(Integer roleId) { try { String market = generalMapper.getRoleMarket(roleId); if (market == null) { throw new Exception("没有地区权限"); } List list = Arrays.asList(market.split(",")); //判断是否是总部 if (list != null && list.contains("总部")) { list=getAllRoleMarket();//总部有所有地区的权限 } return list; } catch (Exception e) { // 记录日志 // log.error("获取地区权限失败", e); // 重新抛出异常,或者根据需要返回一个默认值或空列表 throw new RuntimeException("获取地区权限失败", e); } } @Override public List getAllRoleMarket() { List list = generalMapper.getAllRoleMarket(); return list; } @Override public List getRate() { List list = generalMapper.getRate(); return list; } @Override @Scheduled(cron = "0 0 2 * * *") // 02:00 public void fullSyncProducts() { StopWatch sw = StopWatch.createStarted(); log.info("【全量商品同步】开始"); try { // 1. 拉取远端全量 List remoteList = remoteClient.fetchAll(); if (remoteList.isEmpty()) { log.warn("远端无数据,跳过"); return; } Map remoteMap = remoteList.stream() .collect(Collectors.toMap(ProductDTO::getId, Function.identity())); // 2. 覆盖本地(含新增 & 修改) long syncTime = System.currentTimeMillis(); generalMapper.replaceBatch(remoteList, syncTime); // 3. 删除本地已下架的品 Set remoteIds = remoteMap.keySet(); generalMapper.deleteNotIn(remoteIds); // 4. 统计 int total = remoteList.size(); log.info("【全量商品同步】完成,远端 {} 条,本地覆盖 {} 条,耗时 {} ms", total, total, sw.getTime()); } catch (Exception e) { log.error("【全量商品同步】失败", e); // 可钉钉/邮件告警 } } }