SysConfigController.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. SysConfig sysConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, key).last(" limit 1"));
  65. if (sysConfig == null) {
  66. throw new BusinessRuntimeException("配置不存在");
  67. }
  68. return GatewayResponse.SUCCESS.newBuilder().toResult(sysConfig);
  69. }
  70. @PostMapping("/post")
  71. @Log(module = "系统设置/新增配置", businessType = BusinessType.POST, isSaveRequestData = true)
  72. public Result<SysConfig> post(@RequestBody SysConfig sysConfig) {
  73. sysConfigService.save(sysConfig);
  74. return GatewayResponse.SUCCESS.newBuilder().toResult(sysConfig);
  75. }
  76. @PutMapping("/update")
  77. @Log(module = "系统设置/编辑配置", businessType = BusinessType.PUT, isSaveRequestData = true)
  78. public Result<SysConfig> update(@RequestBody SysConfig sysConfig) throws Exception {
  79. long adminId = StpUtil.getLoginIdAsLong();
  80. Long id = sysConfig.getId();
  81. SysConfig byId = sysConfigService.getById(id);
  82. sysConfig.setSysKey(byId.getSysKey());
  83. if (Constant.EQUIPMENT_DISTRIBUTE_KEY.equals(sysConfig.getSysKey())) {
  84. String sysValue = sysConfig.getSysValue();
  85. if (StrUtil.isNotBlank(sysValue)) {
  86. EquipmentConfigDto equipmentConfigDto = Jsons.parseObject(sysValue, EquipmentConfigDto.class);
  87. if (CollUtil.isNotEmpty(equipmentConfigDto.getSkuList())) {
  88. equipmentConfigDto.setBalance(BigDecimal.ZERO);
  89. }
  90. if (equipmentConfigDto.getBalance() != null && equipmentConfigDto.getBalance().compareTo(BigDecimal.ZERO) > 0) {
  91. equipmentConfigDto.setSkuList(new HashSet<>());
  92. }
  93. sysConfig.setSysValue(Jsons.toJson(equipmentConfigDto));
  94. }
  95. }
  96. SysServiceChangeDto changeRecord = sysConfigChangeRecordService.changeRecord(adminId, sysConfig.getSysKey(), byId.getSysValue(), sysConfig.getSysValue());
  97. sysConfigService.updateById(sysConfig);
  98. //同步获客值班客服
  99. if (Constant.serviceKeys.get(0).equals(sysConfig.getSysKey())) {
  100. corpCustomerAcquisitionLinkService.syncDutyLinkUrl();
  101. }
  102. if (changeRecord.getChangeCsImg()) {
  103. //立即执行客服值班定时任务
  104. XxlJobUtil.executeMarkDynamicRuleTagJob(XxlJobInfo.getExecuteTaskInstance(Constant.CORP_LINK_JOB_ID, StrUtil.EMPTY).toString());
  105. }
  106. if (changeRecord.getChangeCsName()) {
  107. sysConfigChangeRecordService.updateCorpFollowLinkConfig(changeRecord.getUpdateFollowIndxs());
  108. }
  109. return GatewayResponse.SUCCESS.newBuilder().toResult(sysConfig);
  110. }
  111. @DeleteMapping("/delete/{id}")
  112. @Log(module = "系统设置/删除配置", businessType = BusinessType.DELETE)
  113. public Result<String> update(@PathVariable Long id) {
  114. sysConfigService.removeById(id);
  115. return GatewayResponse.SUCCESS.newBuilder().toResult();
  116. }
  117. /**
  118. * 问题分类列表
  119. */
  120. @GetMapping("/help/question/classification/get")
  121. public Result<Page<HelpQuestionClassificationView>> get(Long goodsId, @RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "5") Integer limit) {
  122. Page<HelpQuestionClassificationView> page = helpQuestionClassificationMapper.getQuestionClassificationView(goodsId, new Page<>(start, limit));
  123. page.getRecords().forEach(data -> {
  124. data.setQuestions(helpQuestionMapper.selectList(Wrappers.lambdaQuery(HelpQuestion.class)
  125. .eq(HelpQuestion::getClassificationId, data.getId())
  126. .eq(HelpQuestion::getDeleted, true)
  127. .eq(HelpQuestion::getStatus, true)
  128. .orderByDesc(HelpQuestion::getSort)));
  129. });
  130. return GatewayResponse.SUCCESS.newBuilder().toResult(page);
  131. }
  132. /**
  133. * 问题详情
  134. */
  135. @GetMapping("/get/question/detail")
  136. public Result<HelpQuestion> getQuestionById(Long id) {
  137. if (id == null || id < 1) {
  138. throw BusinessRuntimeException.getInstance("id error");
  139. }
  140. HelpQuestion helpQuestion = helpQuestionMapper.selectById(id);
  141. return GatewayResponse.SUCCESS.newBuilder().toResult(helpQuestion);
  142. }
  143. /**
  144. * 平台是否有关联上架的问题分类
  145. */
  146. @GetMapping("/check/goods/question/classification")
  147. public Result<Boolean> checkGoodsQuestionClassification(Long goodsId) {
  148. if (goodsId == null || goodsId < 0) {
  149. throw BusinessRuntimeException.getInstance("id error");
  150. }
  151. HelpQuestionClassification helpQuestionClassification = helpQuestionClassificationMapper.selectOne(Wrappers.lambdaQuery(HelpQuestionClassification.class)
  152. .eq(HelpQuestionClassification::getGoodsId, goodsId)
  153. .eq(HelpQuestionClassification::getDeleted, true)
  154. .eq(HelpQuestionClassification::getStatus, true)
  155. .last("limit 1"));
  156. return GatewayResponse.SUCCESS.newBuilder().toResult(helpQuestionClassification != null ? true : false);
  157. }
  158. /**
  159. * 值班客服操作日志
  160. */
  161. @GetMapping("/duty/operate/log")
  162. public Result<SearchResult<SysConfigChangeRecord>> sysConfigChangeRecordResult() {
  163. SearchResult<SysConfigChangeRecord> search = beanSearcher.search(SysConfigChangeRecord.class, MapUtils.flatBuilder(request.getParameterMap())
  164. .field(SysConfigChangeRecord::getChangeStr).op(Operator.NotNull)
  165. .orderBy(SysConfigChangeRecord::getId).desc()
  166. .onlySelect(SysConfigChangeRecord::getId, SysConfigChangeRecord::getOperator, SysConfigChangeRecord::getChangeStr, SysConfigChangeRecord::getCreatedTime)
  167. .build());
  168. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  169. }
  170. /**
  171. * 当前用户是否获取了客服
  172. */
  173. @GetMapping("/get/service/indx")
  174. public Result getServiceIndx() {
  175. long userId = StpUserUtil.getLoginIdAsLong();
  176. long time = DateUtil.beginOfDay(DateTime.now()).getTime();
  177. Object re = redisService.get(RedisService.key.EQUIPMENT_USER_SERVICE_RECORD.getEnvName() + time + userId);
  178. return GatewayResponse.SUCCESS.newBuilder().toResult(re);
  179. }
  180. /**
  181. * 记录用户获取的客服
  182. */
  183. @PostMapping("/service/record/{indx}")
  184. public Result<Integer> serviceRecord(@PathVariable Long indx) {
  185. long userId = StpUserUtil.getLoginIdAsLong();
  186. long time = DateUtil.beginOfDay(DateTime.now()).getTime();
  187. redisService.set(RedisService.key.EQUIPMENT_USER_SERVICE_RECORD.getEnvName() + time + userId, indx, RedisService.key.EQUIPMENT_USER_SERVICE_RECORD.getTimeout());
  188. return GatewayResponse.SUCCESS.newBuilder().toResult();
  189. }
  190. /**
  191. * 清零用户获取广告记录
  192. */
  193. @DeleteMapping("/delete/cps/ad")
  194. public Result<String> deleteCpsAdRecord() {
  195. cpsUserAdRecordMapper.delete(null);
  196. return GatewayResponse.SUCCESS.newBuilder().toResult();
  197. }
  198. /**
  199. * 获取客服下标
  200. */
  201. @GetMapping("/get/service/index")
  202. public Result<Long> getCorpFollowIndex() {
  203. String key = RedisKey.SERVICE_INDEX_KEY;
  204. Long index = redisService.incr(key, 1l);
  205. if (index == 10) {
  206. redisService.set(key, 0);
  207. }
  208. return GatewayResponse.SUCCESS.newBuilder().toResult(index - 1);
  209. }
  210. }