You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
9.0 KiB

5 months ago
5 months ago
5 months ago
5 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. package com.example.demo.serviceImpl.coin;
  2. import com.example.demo.Util.ProductRemoteClient;
  3. import com.example.demo.domain.DTO.ProductDTO;
  4. import com.example.demo.domain.entity.Rate;
  5. import com.example.demo.mapper.coin.GeneralMapper;
  6. import com.example.demo.service.coin.GeneralService;
  7. import org.apache.commons.lang3.time.StopWatch;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.scheduling.annotation.Scheduled;
  12. import org.springframework.stereotype.Service;
  13. import java.time.LocalDateTime;
  14. import java.time.LocalTime;
  15. import java.time.ZoneId;
  16. import java.util.*;
  17. import java.util.function.Function;
  18. import java.util.stream.Collectors;
  19. /**
  20. * @program: GOLD
  21. * @ClassName GeneralServiceImpl
  22. * @description:
  23. * @author: huangqizhen
  24. * @create: 202506-22 11:01
  25. * @Version 1.0
  26. **/
  27. @Service
  28. public class GeneralServiceImpl implements GeneralService {
  29. private static final Logger log = LoggerFactory.getLogger(GeneralServiceImpl.class);
  30. private final ProductRemoteClient remoteClient;
  31. @Autowired
  32. private GeneralMapper generalMapper;
  33. public GeneralServiceImpl(ProductRemoteClient remoteClient) {
  34. this.remoteClient = remoteClient;
  35. }
  36. @Override
  37. public List<String> getMarket() {
  38. List<String> list = generalMapper.getMarket();
  39. return list;
  40. }
  41. @Override
  42. public List<String> getALLAdminMarket() {
  43. List<String> list = generalMapper.getALLAdminMarket();
  44. return list;
  45. }
  46. @Override
  47. public List<String> getPlatform() {
  48. List<String> list = generalMapper.getPlatform();
  49. return list;
  50. }
  51. @Override
  52. public List<String> getGoods() {
  53. List<String> list = generalMapper.getGoods();
  54. // 使用 Java 8 的 Stream API 过滤 null 和空字符串
  55. List<String> filteredList = list.stream()
  56. .filter(Objects::nonNull) // 过滤掉 null 值
  57. .filter(str -> !str.trim().isEmpty()) // 过滤掉空字符串和仅包含空白字符的字符串
  58. .collect(Collectors.toList());
  59. return filteredList;
  60. }
  61. @Override
  62. public List<String> getActivity() {
  63. List<String> list = generalMapper.getActivity();
  64. List<String> filteredList = list.stream()
  65. .filter(Objects::nonNull) // 过滤掉 null 值
  66. .filter(str -> !str.trim().isEmpty()) // 过滤掉空字符串和仅包含空白字符的字符串
  67. .collect(Collectors.toList());
  68. return filteredList;
  69. }
  70. /*
  71. 获取昨天的日期
  72. */
  73. @Override
  74. public Date getYesterday() {
  75. Calendar cal = Calendar.getInstance();
  76. cal.add(Calendar.DAY_OF_YEAR, -1); //当前天数-1
  77. return getStartOfDay(cal.getTime()); //昨天的00:00:00
  78. }
  79. /*
  80. 获取某天的开始时间(00:00:00)
  81. */
  82. @Override
  83. public Date getStartOfDay(Date date) {
  84. LocalDateTime localDate = date.toInstant()
  85. .atZone(ZoneId.systemDefault()) // 转换为本地时区
  86. .toLocalDateTime()
  87. .with(LocalTime.MIN); // 设置时间为当天 00:00:00
  88. return Date.from(localDate.atZone(ZoneId.systemDefault()).toInstant());
  89. }
  90. /*
  91. 转换日期格式为yyyy-MM-dd
  92. */
  93. @Override
  94. public String formatDate(Date date) {
  95. return date.toInstant()
  96. .atZone(ZoneId.systemDefault())
  97. .format(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE); // 输出格式如 "2025-10-01"
  98. }
  99. /*
  100. 获取时间段内的所有日期包含起始和结束日
  101. */
  102. @Override
  103. public List<Date> getAllDatesBetween(Date start, Date end) {
  104. List<Date> dates = new ArrayList<>();
  105. //初始化日历对象
  106. Calendar tempStart = Calendar.getInstance();
  107. tempStart.setTime(start);
  108. Calendar tempEnd = Calendar.getInstance();
  109. tempEnd.setTime(end);
  110. while (!tempStart.after(tempEnd)) {
  111. dates.add(tempStart.getTime()); // 将当前日期添加到列表中
  112. tempStart.add(Calendar.DAY_OF_YEAR, 1); // 每次增加一天
  113. }
  114. return dates;
  115. }
  116. /*
  117. 获取角色的地区权限列表
  118. */
  119. @Override
  120. public List<String> getAdminMarket(String account){
  121. try {
  122. String market = generalMapper.getAdminMarket(account);
  123. if (market == null) {
  124. throw new Exception("没有地区权限");
  125. }
  126. List<String> list = Arrays.asList(market.split(","));
  127. //判断是否是总部
  128. if (list != null && list.contains("总部")||market.equals("研发部")) {
  129. List<String> allMarkets = generalMapper.getMarket(); // 获取所有地区
  130. allMarkets.remove("总部");
  131. // 固定顺序
  132. List<String> fixed = Arrays.asList(
  133. "新加坡", "马来西亚", "香港", "加拿大", "泰国", "越南HCM", "韩国", "非网"
  134. );
  135. // 先放固定顺序(存在的才放)
  136. List<String> result = new ArrayList<>();
  137. for (String m : fixed) {
  138. if (allMarkets.contains(m)) result.add(m);
  139. }
  140. // 其余按字母序追加
  141. allMarkets.removeAll(fixed);
  142. Collections.sort(allMarkets);
  143. result.addAll(allMarkets);
  144. return result;
  145. }
  146. return list;
  147. } catch (Exception e) {
  148. // 记录日志
  149. log.error("获取地区权限失败", e);
  150. // 重新抛出异常,或者根据需要返回一个默认值或空列表
  151. throw new RuntimeException("获取地区权限失败", e);
  152. }
  153. }
  154. @Override
  155. public List<String> getAdminMarket1(String account) {
  156. try {
  157. String market = generalMapper.getAdminMarket(account);
  158. if (market == null) {
  159. throw new Exception("没有地区权限");
  160. }
  161. List<String> list = Arrays.asList(market.split(","));
  162. //判断是否是总部
  163. if (list != null && list.contains("1")) {
  164. list=getMarket();//总部有所有地区的权限
  165. }
  166. return list;
  167. } catch (Exception e) {
  168. // 记录日志
  169. // log.error("获取地区权限失败", e);
  170. // 重新抛出异常,或者根据需要返回一个默认值或空列表
  171. throw new RuntimeException("获取地区权限失败", e);
  172. }
  173. }
  174. /*
  175. 获取觉得的地区列表
  176. */
  177. @Override
  178. public List<String> getRoleMarket(Integer roleId) {
  179. try {
  180. String market = generalMapper.getRoleMarket(roleId);
  181. if (market == null) {
  182. throw new Exception("没有地区权限");
  183. }
  184. List<String> list = Arrays.asList(market.split(","));
  185. //判断是否是总部
  186. if (list != null && list.contains("总部")) {
  187. list=getAllRoleMarket();//总部有所有地区的权限
  188. }
  189. return list;
  190. } catch (Exception e) {
  191. // 记录日志
  192. // log.error("获取地区权限失败", e);
  193. // 重新抛出异常,或者根据需要返回一个默认值或空列表
  194. throw new RuntimeException("获取地区权限失败", e);
  195. }
  196. }
  197. @Override
  198. public List<String> getAllRoleMarket() {
  199. List<String> list = generalMapper.getAllRoleMarket();
  200. return list;
  201. }
  202. @Override
  203. public List<Rate> getRate() {
  204. List<Rate> list = generalMapper.getRate();
  205. return list;
  206. }
  207. @Override
  208. @Scheduled(cron = "0 0 2 * * *") // 02:00
  209. public void fullSyncProducts() {
  210. StopWatch sw = StopWatch.createStarted();
  211. log.info("【全量商品同步】开始");
  212. try {
  213. // 1. 拉取远端全量
  214. List<ProductDTO> remoteList = remoteClient.fetchAll();
  215. if (remoteList.isEmpty()) {
  216. log.warn("远端无数据,跳过");
  217. return;
  218. }
  219. Map<Integer, ProductDTO> remoteMap = remoteList.stream()
  220. .collect(Collectors.toMap(ProductDTO::getId, Function.identity()));
  221. // 2. 覆盖本地(含新增 & 修改)
  222. long syncTime = System.currentTimeMillis();
  223. generalMapper.replaceBatch(remoteList, syncTime);
  224. // 3. 删除本地已下架的品
  225. Set<Integer> remoteIds = remoteMap.keySet();
  226. generalMapper.deleteNotIn(remoteIds);
  227. // 4. 统计
  228. int total = remoteList.size();
  229. log.info("【全量商品同步】完成,远端 {} 条,本地覆盖 {} 条,耗时 {} ms",
  230. total, total, sw.getTime());
  231. } catch (Exception e) {
  232. log.error("【全量商品同步】失败", e);
  233. // 可钉钉/邮件告警
  234. }
  235. }
  236. }