SysConfigController.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.*;
  23. import com.cyksj.model.views.HelpQuestionClassificationView;
  24. import com.cyksj.redis.RedisService;
  25. import com.cyksj.service.sys.SysConfigChangeRecordService;
  26. import com.cyksj.service.sys.SysConfigService;
  27. import com.cyksj.web.util.StpUserUtil;
  28. import com.cyksj.xxljob.XxlJobInfo;
  29. import com.cyksj.xxljob.XxlJobUtil;
  30. import com.ejlchina.searcher.BeanSearcher;
  31. import com.ejlchina.searcher.SearchResult;
  32. import com.ejlchina.searcher.param.Operator;
  33. import com.ejlchina.searcher.util.MapUtils;
  34. import lombok.RequiredArgsConstructor;
  35. import lombok.extern.slf4j.Slf4j;
  36. import org.springframework.transaction.annotation.Transactional;
  37. import org.springframework.web.bind.annotation.*;
  38. import javax.servlet.http.HttpServletRequest;
  39. import java.math.BigDecimal;
  40. import java.util.HashSet;
  41. /** admin/客服运营
  42. * @author chan
  43. * @date 2022/10/20 10:08 AM
  44. */
  45. @Slf4j
  46. @RequestMapping("/sys")
  47. @RestController
  48. @RequiredArgsConstructor
  49. public class SysConfigController {
  50. private final SysConfigService sysConfigService;
  51. private final HelpQuestionMapper helpQuestionMapper;
  52. private final HelpQuestionClassificationMapper helpQuestionClassificationMapper;
  53. private final BeanSearcher beanSearcher;
  54. private final HttpServletRequest request;
  55. private final RedisService redisService;
  56. private final SysConfigChangeRecordService sysConfigChangeRecordService;
  57. private final CpsUserAdRecordMapper cpsUserAdRecordMapper;
  58. @GetMapping("/get")
  59. public Result<SysConfig> get(String key) {
  60. SysConfig sysConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, key).last(" limit 1"));
  61. if (sysConfig == null) {
  62. throw new BusinessRuntimeException("配置不存在");
  63. }
  64. return GatewayResponse.SUCCESS.newBuilder().toResult(sysConfig);
  65. }
  66. @PostMapping("/post")
  67. @Log(module = "系统设置/新增配置", businessType = BusinessType.POST, isSaveRequestData = true)
  68. public Result<SysConfig> post(@RequestBody SysConfig sysConfig) {
  69. sysConfigService.save(sysConfig);
  70. return GatewayResponse.SUCCESS.newBuilder().toResult(sysConfig);
  71. }
  72. @PutMapping("/update")
  73. @Log(module = "系统设置/编辑配置", businessType = BusinessType.PUT, isSaveRequestData = true)
  74. @Transactional(rollbackFor = Throwable.class)
  75. public Result<SysConfig> update(@RequestBody SysConfig sysConfig) throws Exception {
  76. long adminId = StpUtil.getLoginIdAsLong();
  77. Long id = sysConfig.getId();
  78. SysConfig byId = sysConfigService.getById(id);
  79. sysConfig.setSysKey(byId.getSysKey());
  80. if (Constant.EQUIPMENT_DISTRIBUTE_KEY.equals(sysConfig.getSysKey())) {
  81. String sysValue = sysConfig.getSysValue();
  82. if (StrUtil.isNotBlank(sysValue)) {
  83. EquipmentConfigDto equipmentConfigDto = Jsons.parseObject(sysValue, EquipmentConfigDto.class);
  84. if (CollUtil.isNotEmpty(equipmentConfigDto.getSkuList())) {
  85. equipmentConfigDto.setBalance(BigDecimal.ZERO);
  86. }
  87. if (equipmentConfigDto.getBalance() != null && equipmentConfigDto.getBalance().compareTo(BigDecimal.ZERO) > 0) {
  88. equipmentConfigDto.setSkuList(new HashSet<>());
  89. }
  90. sysConfig.setSysValue(Jsons.toJson(equipmentConfigDto));
  91. }
  92. }
  93. SysServiceChangeDto changeRecord = sysConfigChangeRecordService.changeRecord(adminId, sysConfig.getSysKey(), byId.getSysValue(), sysConfig.getSysValue());
  94. sysConfigService.updateById(sysConfig);
  95. if (changeRecord.getChangeCsImg()) {
  96. //立即执行客服值班定时任务
  97. XxlJobUtil.executeMarkDynamicRuleTagJob(XxlJobInfo.getExecuteTaskInstance(Constant.CORP_LINK_JOB_ID, StrUtil.EMPTY).toString());
  98. }
  99. if (changeRecord.getChangeCsName()) {
  100. sysConfigChangeRecordService.updateCorpFollowLinkConfig(changeRecord.getUpdateFollowIndxs());
  101. }
  102. return GatewayResponse.SUCCESS.newBuilder().toResult(sysConfig);
  103. }
  104. @DeleteMapping("/delete/{id}")
  105. @Log(module = "系统设置/删除配置", businessType = BusinessType.DELETE)
  106. public Result<String> update(@PathVariable Long id) {
  107. sysConfigService.removeById(id);
  108. return GatewayResponse.SUCCESS.newBuilder().toResult();
  109. }
  110. /**
  111. * 问题分类列表
  112. */
  113. @GetMapping("/help/question/classification/get")
  114. public Result<Page<HelpQuestionClassificationView>> get(Long goodsId, @RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "5") Integer limit) {
  115. Page<HelpQuestionClassificationView> page = helpQuestionClassificationMapper.getQuestionClassificationView(goodsId, new Page<>(start, limit));
  116. page.getRecords().forEach(data -> {
  117. data.setQuestions(helpQuestionMapper.selectList(Wrappers.lambdaQuery(HelpQuestion.class)
  118. .eq(HelpQuestion::getClassificationId, data.getId())
  119. .eq(HelpQuestion::getDeleted, true)
  120. .eq(HelpQuestion::getStatus, true)
  121. .orderByDesc(HelpQuestion::getSort)));
  122. });
  123. return GatewayResponse.SUCCESS.newBuilder().toResult(page);
  124. }
  125. /**
  126. * 问题详情
  127. */
  128. @GetMapping("/get/question/detail")
  129. public Result<HelpQuestion> getQuestionById(Long id) {
  130. if (id == null || id < 1) {
  131. throw BusinessRuntimeException.getInstance("id error");
  132. }
  133. HelpQuestion helpQuestion = helpQuestionMapper.selectById(id);
  134. return GatewayResponse.SUCCESS.newBuilder().toResult(helpQuestion);
  135. }
  136. /**
  137. * 平台是否有关联上架的问题分类
  138. */
  139. @GetMapping("/check/goods/question/classification")
  140. public Result<Boolean> checkGoodsQuestionClassification(Long goodsId) {
  141. if (goodsId == null || goodsId < 0) {
  142. throw BusinessRuntimeException.getInstance("id error");
  143. }
  144. HelpQuestionClassification helpQuestionClassification = helpQuestionClassificationMapper.selectOne(Wrappers.lambdaQuery(HelpQuestionClassification.class)
  145. .eq(HelpQuestionClassification::getGoodsId, goodsId)
  146. .eq(HelpQuestionClassification::getDeleted, true)
  147. .eq(HelpQuestionClassification::getStatus, true)
  148. .last("limit 1"));
  149. return GatewayResponse.SUCCESS.newBuilder().toResult(helpQuestionClassification != null ? true : false);
  150. }
  151. /**
  152. * 值班客服操作日志
  153. */
  154. @GetMapping("/duty/operate/log")
  155. public Result<SearchResult<SysConfigChangeRecord>> sysConfigChangeRecordResult() {
  156. SearchResult<SysConfigChangeRecord> search = beanSearcher.search(SysConfigChangeRecord.class, MapUtils.flatBuilder(request.getParameterMap())
  157. .field(SysConfigChangeRecord::getChangeStr).op(Operator.NotNull)
  158. .orderBy(SysConfigChangeRecord::getId).desc()
  159. .onlySelect(SysConfigChangeRecord::getId, SysConfigChangeRecord::getOperator, SysConfigChangeRecord::getChangeStr, SysConfigChangeRecord::getCreatedTime)
  160. .build());
  161. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  162. }
  163. /**
  164. * 当前用户是否获取了客服
  165. */
  166. @GetMapping("/get/service/indx")
  167. public Result getServiceIndx() {
  168. long userId = StpUserUtil.getLoginIdAsLong();
  169. long time = DateUtil.beginOfDay(DateTime.now()).getTime();
  170. Object re = redisService.get(RedisService.key.EQUIPMENT_USER_SERVICE_RECORD.getEnvName() + time + userId);
  171. return GatewayResponse.SUCCESS.newBuilder().toResult(re);
  172. }
  173. /**
  174. * 记录用户获取的客服
  175. */
  176. @PostMapping("/service/record/{indx}")
  177. public Result<Integer> serviceRecord(@PathVariable Long indx) {
  178. long userId = StpUserUtil.getLoginIdAsLong();
  179. long time = DateUtil.beginOfDay(DateTime.now()).getTime();
  180. redisService.set(RedisService.key.EQUIPMENT_USER_SERVICE_RECORD.getEnvName() + time + userId, indx, RedisService.key.EQUIPMENT_USER_SERVICE_RECORD.getTimeout());
  181. return GatewayResponse.SUCCESS.newBuilder().toResult();
  182. }
  183. /**
  184. * 清零用户获取广告记录
  185. */
  186. @DeleteMapping("/delete/cps/ad")
  187. public Result<String> deleteCpsAdRecord() {
  188. cpsUserAdRecordMapper.delete(null);
  189. return GatewayResponse.SUCCESS.newBuilder().toResult();
  190. }
  191. /**
  192. * 获取客服下标
  193. */
  194. @GetMapping("/get/service/index")
  195. public Result<Long> getCorpFollowIndex() {
  196. String key = RedisKey.SERVICE_INDEX_KEY;
  197. Long index = redisService.incr(key, 1l);
  198. if (index == 10) {
  199. redisService.set(key, 0);
  200. }
  201. return GatewayResponse.SUCCESS.newBuilder().toResult(index - 1);
  202. }
  203. }