SysConfigServiceImpl.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.cyksj.service.sys.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.cyksj.common.constant.Constant;
  8. import com.cyksj.common.service.CropDutyServiceDateService;
  9. import com.cyksj.common.util.Jsons;
  10. import com.cyksj.mapper.corp.CorpFollowMapper;
  11. import com.cyksj.mapper.sys.SysConfigMapper;
  12. import com.cyksj.model.dto.DefaultShopConfig;
  13. import com.cyksj.model.dto.YhServiceDto;
  14. import com.cyksj.model.entity.CorpFollow;
  15. import com.cyksj.model.entity.ShopConfig;
  16. import com.cyksj.model.entity.SysConfig;
  17. import com.cyksj.service.sys.SysConfigService;
  18. import lombok.RequiredArgsConstructor;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springframework.stereotype.Service;
  21. import java.time.DayOfWeek;
  22. import java.time.LocalDateTime;
  23. import java.time.LocalTime;
  24. import java.util.*;
  25. import java.util.stream.Collectors;
  26. /**
  27. * @author chan
  28. * @date 2022/10/20 10:07 AM
  29. */
  30. @Slf4j
  31. @Service
  32. @RequiredArgsConstructor
  33. public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper,SysConfig> implements SysConfigService {
  34. private final CorpFollowMapper corpFollowMapper;
  35. @Override
  36. public String getDefaultShopConfigByType(String type) {
  37. SysConfig shopConfig = this.getOne(Wrappers.lambdaQuery(SysConfig.class)
  38. .eq(SysConfig::getSysKey, Constant.SYS_DEFAULT_SHOP_KEY).last("limit 1"));
  39. if (shopConfig == null) {
  40. return getShopIdByType(type);
  41. }
  42. String sysValue = shopConfig.getSysValue();
  43. try {
  44. DefaultShopConfig defaultShopConfig = Jsons.parseObject(sysValue, DefaultShopConfig.class);
  45. String wxDefaultAppId = defaultShopConfig.getWxDefaultAppId();
  46. String zfbDefaultAppId = defaultShopConfig.getZfbDefaultAppId();
  47. if ("wx".equals(type)) {
  48. return Optional.ofNullable(wxDefaultAppId).orElse(ShopConfig.WX_DEFAULT_APP_ID);
  49. }
  50. return Optional.ofNullable(zfbDefaultAppId).orElse(ShopConfig.ZFB_DEFAULT_APP_ID);
  51. } catch (Exception e) {
  52. }
  53. return getShopIdByType(type);
  54. }
  55. public String getShopIdByType(String type) {
  56. if ("wx".equals(type)) {
  57. return ShopConfig.WX_DEFAULT_APP_ID;
  58. }
  59. return ShopConfig.ZFB_DEFAULT_APP_ID;
  60. }
  61. @Override
  62. public List<Integer> getDutyServices() throws Exception {
  63. SysConfig sysConfig = this.getOne(Wrappers.lambdaQuery(SysConfig.class)
  64. .eq(SysConfig::getSysKey, Constant.serviceKeys.get(0))
  65. .last("limit 1"));
  66. //过滤被禁用的规格
  67. Map<String, List<CorpFollow>> followMap = corpFollowMapper.selectList(Wrappers.lambdaQuery(CorpFollow.class)
  68. .ne(CorpFollow::getStatus, 1)).stream().collect(Collectors.groupingBy(CorpFollow::getUserid));
  69. List<YhServiceDto> yhServiceDtos = null;
  70. if (sysConfig != null && StrUtil.isNotBlank(sysConfig.getSysValue())) {
  71. yhServiceDtos = Jsons.parseList(sysConfig.getSysValue(), YhServiceDto.class);
  72. yhServiceDtos.stream().forEach(s -> {
  73. List<YhServiceDto.CustomerServiceDto> finalSelect = s.getSelect().stream().filter(select -> {
  74. List<CorpFollow> corpFollows = followMap.get(select);
  75. if (CollUtil.isNotEmpty(corpFollows)) {
  76. CorpFollow corpFollow = corpFollows.get(0);
  77. if (corpFollow.getStatus() != 1) {
  78. return false;
  79. }
  80. }
  81. return true;
  82. }).collect(Collectors.toList());
  83. s.setSelect(finalSelect);
  84. });
  85. }
  86. final List<YhServiceDto> dtoCs = yhServiceDtos;
  87. List<YhServiceDto> workdayServices = null;
  88. List<YhServiceDto> saturdayServices = null;
  89. List<YhServiceDto> sundayServices = null;
  90. Map<String, List<YhServiceDto>> holidayDateMap = null;
  91. if (CollUtil.isNotEmpty(dtoCs)) {
  92. Map<String, List<YhServiceDto>> dutyServices = yhServiceDtos.stream().collect(Collectors.groupingBy(YhServiceDto::getSign));
  93. workdayServices = dutyServices.get("workday");
  94. saturdayServices = dutyServices.get("Saturday");
  95. sundayServices = dutyServices.get("Sunday");
  96. List<YhServiceDto> holidayServices = dutyServices.get("holiday");
  97. if (CollUtil.isNotEmpty(holidayServices)) {
  98. holidayDateMap = holidayServices.stream().collect(Collectors.groupingBy(YhServiceDto::getDate));
  99. }
  100. }
  101. LocalDateTime now = LocalDateTime.now();
  102. List<Integer> dutyServices = getDutyServices(workdayServices, saturdayServices, sundayServices, holidayDateMap, now);
  103. return dutyServices;
  104. }
  105. public List<Integer> getDutyServices(List<YhServiceDto> workday, List<YhServiceDto> saturday, List<YhServiceDto> sunday, Map<String, List<YhServiceDto>> holidayDateMap, LocalDateTime now) {
  106. if (CollUtil.isNotEmpty(workday)) {
  107. DayOfWeek currentDayOfWeek = now.getDayOfWeek();
  108. List<YhServiceDto> carryServices = CropDutyServiceDateService.getDutyService(currentDayOfWeek, workday, saturday, sunday, holidayDateMap, now);
  109. //获取值班客服
  110. List<YhServiceDto> dutyServiceCodes = getTimeDutyServices(carryServices, workday, saturday, sunday, holidayDateMap, currentDayOfWeek, now);
  111. if (CollUtil.isNotEmpty(dutyServiceCodes)) {
  112. Set<Integer> collectFollow = new HashSet<>();
  113. for (YhServiceDto dutyServiceCode : dutyServiceCodes) {
  114. dutyServiceCode.getSelect().stream().filter(e -> e.getIndex() != null).forEach(cs -> collectFollow.add(cs.getIndex()));
  115. }
  116. return new ArrayList<>(collectFollow);
  117. }
  118. }
  119. return null;
  120. }
  121. /**
  122. * 获取当前时间段值班的客服
  123. */
  124. public List<YhServiceDto> getTimeDutyServices(List<YhServiceDto> carryServices, List<YhServiceDto> workday, List<YhServiceDto> saturday, List<YhServiceDto> sunday, Map<String, List<YhServiceDto>> holidayDateMap, DayOfWeek currentDayOfWeek, LocalDateTime now) {
  125. // 获取当前时间
  126. LocalTime currentTime = now.toLocalTime();
  127. List<YhServiceDto> collect = carryServices.stream().filter(time -> time.isBetween(currentTime)).collect(Collectors.toList());
  128. if (CollUtil.isEmpty(collect)) {
  129. collect = carryServices.stream().filter(time -> time.isAfter(currentTime)).collect(Collectors.toList());
  130. if (CollUtil.isEmpty(collect)) {
  131. // 如果没有下一个时间段,获取下一个工作时间段的开始时间
  132. DayOfWeek nextWorkDay = CropDutyServiceDateService.getNextWorkDay(currentDayOfWeek);
  133. collect = CropDutyServiceDateService.getDutyService(nextWorkDay, workday, saturday, sunday, holidayDateMap, now.plusDays(1));
  134. }
  135. collect.sort(Comparator.comparing(e -> DateUtil.parse(e.getSortTime() == null ? e.getStartTime() : e.getSortTime(), "HH:mm")));
  136. //获取下一天的早时
  137. return List.of(collect.get(0));
  138. }
  139. return collect;
  140. }
  141. }