SysConfigController.java 7.0 KB

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