zxl
2026-03-25 0b39edb68acc67ed01fbfe5d31bfa776a1b17de1
jyz-base-start/src/main/java/com/tievd/jyz/controller/ClientConfigController.java
@@ -5,21 +5,31 @@
import com.tievd.cube.commons.annotations.DictApi;
import com.tievd.cube.commons.base.CubeController;
import com.tievd.cube.commons.base.Result;
import com.tievd.cube.commons.constant.CacheConst;
import com.tievd.cube.modules.system.entity.SysDictItem;
import com.tievd.cube.modules.system.service.ISysDictItemService;
import com.tievd.jyz.entity.Client;
import com.tievd.jyz.entity.ClientConfig;
import com.tievd.jyz.entity.vo.ClientVo;
import com.tievd.jyz.service.IClientConfigService;
import com.tievd.jyz.service.IClientService;
import cn.dev33.satoken.stp.StpUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
/**
 * ClientConfig
@@ -35,11 +45,16 @@
@Tag(name = "客户规则配置")
public class ClientConfigController extends CubeController<ClientConfig, IClientConfigService> {
  private static final String DICT_ID = "1631109859116990466";
  @Autowired
  private IClientConfigService clientConfigService;
  
  @Autowired
  IClientService clientService;
  @Autowired
  private ISysDictItemService sysDictItemService;
  
  /**
   * 分页列表查询
@@ -54,10 +69,13 @@
  
  @AutoLog("ClientConfig-添加")
  @PostMapping("/add")
  @Transactional(rollbackFor = Exception.class)
  @CacheEvict(value = CacheConst.SYS_DICT_CACHE, allEntries = true)
  @Operation(summary = "添加客户规则")
  public Result<?> add(@RequestBody ClientVo clientVo) {
    clientService.save(clientVo);
    clientConfigService.saveBatch(getConfig(clientVo));
    syncDictItem(clientVo.getClientName(), clientVo.getId());
    return Result.ok();
  }
  
@@ -66,23 +84,73 @@
   */
  @AutoLog("ClientConfig-编辑")
  @PutMapping("/edit")
  @Transactional(rollbackFor = Exception.class)
  @CacheEvict(value = CacheConst.SYS_DICT_CACHE, allEntries = true)
  @Operation(summary = "修改客户规则")
  public Result<?> edit(@RequestBody ClientVo clientVo) {
    clientService.updateById(clientVo);
    clientConfigService.remove(new LambdaQueryWrapper<ClientConfig>().eq(ClientConfig::getClientId, clientVo.getId()));
    clientConfigService.saveBatch(getConfig(clientVo));
    updateDictItem(clientVo.getClientName(), clientVo.getId());
    return Result.ok();
  }
  
  List<ClientConfig> getConfig(ClientVo clientVo) {
    List<ClientConfig> clientConfigs = clientVo.getClientConfigs();
    if (clientConfigs == null || clientConfigs.isEmpty()) {
      return Collections.emptyList();
    }
    clientConfigs.forEach(c -> {
      String[] param = c.getTimeStr().split(",");
      if (StringUtils.hasText(c.getTimeStr())) {
        String[] param = c.getTimeStr().split(",");
        c.setTimeValue(Integer.valueOf(param[0])).setTimeUnit(param[1]);
      }
      if (c.getRuleType() == null) {
        c.setRuleType((byte) 1);
      }
      if (c.getRuleType() == 2) {
        if (c.getCountType() == null) {
          c.setCountType((byte) 2);
        }
        if (c.getCountRef() == null) {
          c.setCountRef((byte) 1);
        }
      }
      c.setClientId(clientVo.getId())
              .setClientName(clientVo.getClientName())
              .setTimeValue(Integer.valueOf(param[0])).setTimeUnit(param[1]);
              .setClientName(clientVo.getClientName());
    });
    return clientConfigs;
  }
  private void syncDictItem(String clientName, Integer clientId) {
    SysDictItem dictItem = new SysDictItem();
    dictItem.setId(UUID.randomUUID().toString().replace("-", ""));
    dictItem.setDictId(DICT_ID);
    dictItem.setItemText(clientName);
    dictItem.setItemValue(String.valueOf(clientId));
    dictItem.setSortOrder(clientId);
    dictItem.setStatus(1);
    dictItem.setCreateBy(StpUtil.getLoginIdAsString());
    dictItem.setCreateTime(new java.util.Date());
    sysDictItemService.save(dictItem);
  }
  private void updateDictItem(String clientName, Integer clientId) {
    LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(SysDictItem::getDictId, DICT_ID).eq(SysDictItem::getItemValue, String.valueOf(clientId));
    SysDictItem dictItem = sysDictItemService.getOne(wrapper);
    if (dictItem != null) {
      dictItem.setItemText(clientName);
      sysDictItemService.updateById(dictItem);
    } else {
      syncDictItem(clientName, clientId);
    }
  }
  private void deleteDictItem(String clientName) {
    LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(SysDictItem::getDictId, DICT_ID).eq(SysDictItem::getItemText, clientName);
    sysDictItemService.remove(wrapper);
  }
  
  /**
@@ -90,10 +158,18 @@
   */
  @AutoLog("ClientConfig-通过id删除")
  @DeleteMapping("/delete")
  @Transactional(rollbackFor = Exception.class)
  @CacheEvict(value = CacheConst.SYS_DICT_CACHE, allEntries = true)
  @Operation(summary = "删除客户规则")
  public Result<?> delete(@RequestParam String id) {
    Client client = clientService.getById(id);
    if (client == null) {
      return Result.error("记录不存在");
    }
    String clientName = client.getClientName();
    clientService.removeById(id);
    clientConfigService.remove(new LambdaQueryWrapper<ClientConfig>().eq(ClientConfig::getClientId, id));
    deleteDictItem(clientName);
    return Result.ok();
  }