SysConfigController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package com.cyksj.web.controller.sys;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.date.DateTime;
  5. import cn.hutool.core.date.DateUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.cyksj.common.annotation.Log;
  10. import com.cyksj.common.constant.Constant;
  11. import com.cyksj.common.exception.BusinessRuntimeException;
  12. import com.cyksj.common.util.Jsons;
  13. import com.cyksj.dto.RedisKey;
  14. import com.cyksj.dto.Result;
  15. import com.cyksj.enums.BusinessType;
  16. import com.cyksj.enums.GatewayResponse;
  17. import com.cyksj.mapper.manage.cps.CpsUserAdRecordMapper;
  18. import com.cyksj.mapper.manage.help.HelpQuestionClassificationMapper;
  19. import com.cyksj.mapper.manage.help.HelpQuestionMapper;
  20. import com.cyksj.model.dto.EquipmentConfigDto;
  21. import com.cyksj.model.dto.SysServiceChangeDto;
  22. import com.cyksj.model.entity.HelpQuestion;
  23. import com.cyksj.model.entity.HelpQuestionClassification;
  24. import com.cyksj.model.entity.SysConfig;
  25. import com.cyksj.model.entity.SysConfigChangeRecord;
  26. import com.cyksj.model.views.HelpQuestionClassificationView;
  27. import com.cyksj.redis.RedisService;
  28. import com.cyksj.service.corp.CorpCustomerAcquisitionLinkService;
  29. import com.cyksj.service.sys.SysConfigChangeRecordService;
  30. import com.cyksj.service.sys.SysConfigService;
  31. import com.cyksj.web.util.StpUserUtil;
  32. import com.cyksj.xxljob.XxlJobInfo;
  33. import com.cyksj.xxljob.XxlJobUtil;
  34. import com.ejlchina.searcher.BeanSearcher;
  35. import com.ejlchina.searcher.SearchResult;
  36. import com.ejlchina.searcher.param.Operator;
  37. import com.ejlchina.searcher.util.MapUtils;
  38. import lombok.RequiredArgsConstructor;
  39. import lombok.extern.slf4j.Slf4j;
  40. import org.springframework.web.bind.annotation.*;
  41. import javax.servlet.http.HttpServletRequest;
  42. import java.math.BigDecimal;
  43. import java.util.HashSet;
  44. /** admin/客服运营
  45. * @author chan
  46. * @date 2022/10/20 10:08 AM
  47. */
  48. @Slf4j
  49. @RequestMapping("/sys")
  50. @RestController
  51. @RequiredArgsConstructor
  52. public class SysConfigController {
  53. private final SysConfigService sysConfigService;
  54. private final HelpQuestionMapper helpQuestionMapper;
  55. private final HelpQuestionClassificationMapper helpQuestionClassificationMapper;
  56. private final BeanSearcher beanSearcher;
  57. private final HttpServletRequest request;
  58. private final RedisService redisService;
  59. private final SysConfigChangeRecordService sysConfigChangeRecordService;
  60. private final CpsUserAdRecordMapper cpsUserAdRecordMapper;
  61. private final CorpCustomerAcquisitionLinkService corpCustomerAcquisitionLinkService;
  62. @GetMapping("/get")
  63. public Result<SysConfig> get(String key) {
  64. try {
  65. String cacheKey = RedisService.key.SYS_CONFIG_CACHE_KEY.getName() + key;
  66. Object o = redisService.get(cacheKey);
  67. if (o != null) {
  68. return GatewayResponse.SUCCESS.newBuilder().toResult((SysConfig) o);
  69. }
  70. // 加锁防止缓存击穿
  71. String lockKey = cacheKey + ":lock";
  72. if (redisService.setNx(lockKey, "1", 10L)) {
  73. try {
  74. o = redisService.get(cacheKey);
  75. if (o != null) {
  76. return GatewayResponse.SUCCESS.newBuilder().toResult((SysConfig) o);
  77. }
  78. SysConfig sysConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, key).last(" limit 1"));
  79. if (sysConfig == null) {
  80. throw new BusinessRuntimeException("配置不存在");
  81. }
  82. redisService.set(cacheKey, sysConfig, RedisService.key.SYS_CONFIG_CACHE_KEY.getTimeout());
  83. return GatewayResponse.SUCCESS.newBuilder().toResult(sysConfig);
  84. } finally {
  85. redisService.del(lockKey);
  86. }
  87. } else {
  88. // 未获取锁,返回空
  89. throw new BusinessRuntimeException("配置不存在");
  90. }
  91. } catch (BusinessRuntimeException e) {
  92. throw e;
  93. } catch (Exception e) {
  94. // Redis异常,返回空保护数据库
  95. throw new BusinessRuntimeException("配置不存在");
  96. }
  97. }
  98. @PostMapping("/post")
  99. @Log(module = "系统设置/新增配置", businessType = BusinessType.POST, isSaveRequestData = true)
  100. public Result<SysConfig> post(@RequestBody SysConfig sysConfig) {
  101. sysConfigService.save(sysConfig);
  102. return GatewayResponse.SUCCESS.newBuilder().toResult(sysConfig);
  103. }
  104. @PutMapping("/update")
  105. @Log(module = "系统设置/编辑配置", businessType = BusinessType.PUT, isSaveRequestData = true)
  106. public Result<SysConfig> update(@RequestBody SysConfig sysConfig) throws Exception {
  107. long adminId = StpUtil.getLoginIdAsLong();
  108. Long id = sysConfig.getId();
  109. SysConfig byId = sysConfigService.getById(id);
  110. sysConfig.setSysKey(byId.getSysKey());
  111. if (Constant.EQUIPMENT_DISTRIBUTE_KEY.equals(sysConfig.getSysKey())) {
  112. String sysValue = sysConfig.getSysValue();
  113. if (StrUtil.isNotBlank(sysValue)) {
  114. EquipmentConfigDto equipmentConfigDto = Jsons.parseObject(sysValue, EquipmentConfigDto.class);
  115. if (CollUtil.isNotEmpty(equipmentConfigDto.getSkuList())) {
  116. equipmentConfigDto.setBalance(BigDecimal.ZERO);
  117. }
  118. if (equipmentConfigDto.getBalance() != null && equipmentConfigDto.getBalance().compareTo(BigDecimal.ZERO) > 0) {
  119. equipmentConfigDto.setSkuList(new HashSet<>());
  120. }
  121. sysConfig.setSysValue(Jsons.toJson(equipmentConfigDto));
  122. }
  123. }
  124. SysServiceChangeDto changeRecord = sysConfigChangeRecordService.changeRecord(adminId, sysConfig.getSysKey(), byId.getSysValue(), sysConfig.getSysValue());
  125. sysConfigService.updateById(sysConfig);
  126. //同步获客值班客服
  127. if (Constant.serviceKeys.get(0).equals(sysConfig.getSysKey())) {
  128. corpCustomerAcquisitionLinkService.syncDutyLinkUrl();
  129. }
  130. if (changeRecord.getChangeCsImg()) {
  131. //立即执行客服值班定时任务
  132. XxlJobUtil.executeMarkDynamicRuleTagJob(XxlJobInfo.getExecuteTaskInstance(Constant.CORP_LINK_JOB_ID, StrUtil.EMPTY).toString());
  133. }
  134. if (changeRecord.getChangeCsName()) {
  135. sysConfigChangeRecordService.updateCorpFollowLinkConfig(changeRecord.getUpdateFollowIndxs());
  136. }
  137. String key = sysConfig.getSysKey();
  138. String cacheKey = RedisService.key.SYS_CONFIG_CACHE_KEY.getName() + key;
  139. if (redisService.hasKey(cacheKey)) {
  140. redisService.set(cacheKey, sysConfig, RedisService.key.SYS_CONFIG_CACHE_KEY.getTimeout());
  141. }
  142. return GatewayResponse.SUCCESS.newBuilder().toResult(sysConfig);
  143. }
  144. @DeleteMapping("/delete/{id}")
  145. @Log(module = "系统设置/删除配置", businessType = BusinessType.DELETE)
  146. public Result<String> update(@PathVariable Long id) {
  147. SysConfig byId = sysConfigService.getById(id);
  148. if (byId != null) {
  149. sysConfigService.removeById(id);
  150. String key = byId.getSysKey();
  151. String cacheKey = RedisService.key.SYS_CONFIG_CACHE_KEY.getName() + key;
  152. if (redisService.hasKey(cacheKey)) {
  153. redisService.del(cacheKey);
  154. }
  155. }
  156. return GatewayResponse.SUCCESS.newBuilder().toResult();
  157. }
  158. /**
  159. * 问题分类列表
  160. */
  161. @GetMapping("/help/question/classification/get")
  162. public Result<Page<HelpQuestionClassificationView>> get(Long goodsId, @RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "5") Integer limit) {
  163. Page<HelpQuestionClassificationView> page = helpQuestionClassificationMapper.getQuestionClassificationView(goodsId, new Page<>(start, limit));
  164. page.getRecords().forEach(data -> {
  165. data.setQuestions(helpQuestionMapper.selectList(Wrappers.lambdaQuery(HelpQuestion.class)
  166. .eq(HelpQuestion::getClassificationId, data.getId())
  167. .eq(HelpQuestion::getDeleted, true)
  168. .eq(HelpQuestion::getStatus, true)
  169. .orderByDesc(HelpQuestion::getSort)));
  170. });
  171. return GatewayResponse.SUCCESS.newBuilder().toResult(page);
  172. }
  173. /**
  174. * 问题详情
  175. */
  176. @GetMapping("/get/question/detail")
  177. public Result<HelpQuestion> getQuestionById(Long id) {
  178. if (id == null || id < 1) {
  179. throw BusinessRuntimeException.getInstance("id error");
  180. }
  181. HelpQuestion helpQuestion = helpQuestionMapper.selectById(id);
  182. return GatewayResponse.SUCCESS.newBuilder().toResult(helpQuestion);
  183. }
  184. /**
  185. * 平台是否有关联上架的问题分类
  186. */
  187. @GetMapping("/check/goods/question/classification")
  188. public Result<Boolean> checkGoodsQuestionClassification(Long goodsId) {
  189. if (goodsId == null || goodsId < 0) {
  190. throw BusinessRuntimeException.getInstance("id error");
  191. }
  192. HelpQuestionClassification helpQuestionClassification = helpQuestionClassificationMapper.selectOne(Wrappers.lambdaQuery(HelpQuestionClassification.class)
  193. .eq(HelpQuestionClassification::getGoodsId, goodsId)
  194. .eq(HelpQuestionClassification::getDeleted, true)
  195. .eq(HelpQuestionClassification::getStatus, true)
  196. .last("limit 1"));
  197. return GatewayResponse.SUCCESS.newBuilder().toResult(helpQuestionClassification != null ? true : false);
  198. }
  199. /**
  200. * 值班客服操作日志
  201. */
  202. @GetMapping("/duty/operate/log")
  203. public Result<SearchResult<SysConfigChangeRecord>> sysConfigChangeRecordResult() {
  204. SearchResult<SysConfigChangeRecord> search = beanSearcher.search(SysConfigChangeRecord.class, MapUtils.flatBuilder(request.getParameterMap())
  205. .field(SysConfigChangeRecord::getChangeStr).op(Operator.NotNull)
  206. .orderBy(SysConfigChangeRecord::getId).desc()
  207. .onlySelect(SysConfigChangeRecord::getId, SysConfigChangeRecord::getOperator, SysConfigChangeRecord::getChangeStr, SysConfigChangeRecord::getCreatedTime)
  208. .build());
  209. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  210. }
  211. /**
  212. * 当前用户是否获取了客服
  213. */
  214. @GetMapping("/get/service/indx")
  215. public Result getServiceIndx() {
  216. long userId = StpUserUtil.getLoginIdAsLong();
  217. long time = DateUtil.beginOfDay(DateTime.now()).getTime();
  218. Object re = redisService.get(RedisService.key.EQUIPMENT_USER_SERVICE_RECORD.getName() + time + userId);
  219. return GatewayResponse.SUCCESS.newBuilder().toResult(re);
  220. }
  221. /**
  222. * 记录用户获取的客服
  223. */
  224. @PostMapping("/service/record/{indx}")
  225. public Result<Integer> serviceRecord(@PathVariable Long indx) {
  226. long userId = StpUserUtil.getLoginIdAsLong();
  227. long time = DateUtil.beginOfDay(DateTime.now()).getTime();
  228. redisService.set(RedisService.key.EQUIPMENT_USER_SERVICE_RECORD.getName() + time + userId, indx, RedisService.key.EQUIPMENT_USER_SERVICE_RECORD.getTimeout());
  229. return GatewayResponse.SUCCESS.newBuilder().toResult();
  230. }
  231. /**
  232. * 清零用户获取广告记录
  233. */
  234. @DeleteMapping("/delete/cps/ad")
  235. public Result<String> deleteCpsAdRecord() {
  236. cpsUserAdRecordMapper.delete(null);
  237. return GatewayResponse.SUCCESS.newBuilder().toResult();
  238. }
  239. /**
  240. * 获取客服下标
  241. */
  242. @GetMapping("/get/service/index")
  243. public Result<Long> getCorpFollowIndex(@RequestParam(defaultValue = "1") Integer type) {
  244. //客服下表类型
  245. String oriType = type == 1 ? "ep" : "other";
  246. String key = RedisKey.SERVICE_INDEX_KEY + oriType;
  247. Long index = redisService.incr(key, 1l);
  248. if (index == 10) {
  249. redisService.set(key, 0);
  250. }
  251. return GatewayResponse.SUCCESS.newBuilder().toResult(index - 1);
  252. }
  253. }