SysConfigController.java 8.4 KB

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