zxl
2026-03-25 74e332504d98caaf8fab951d7d24be762b169f49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.tievd.jyz.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tievd.cube.commons.annotations.AutoLog;
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
 *
 * @author      cube
 * @since       2023-08-15
 * @version     V2.0.0
 */
@Slf4j
@DictApi
@RestController
@RequestMapping("/jyz/clientConfig")
@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;
  
  /**
   * 分页列表查询
   */
  @GetMapping("/list")
  @Operation(summary = "客户规则列表")
  public Result<List<ClientVo>> queryPageList() {
    List<ClientVo> list = clientConfigService.groupList();
    return Result.ok(list);
  }
  
  
  @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();
  }
  
  /**
   * 编辑
   */
  @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 -> {
      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());
    });
    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);
  }
  
  /**
   * 通过id删除
   */
  @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();
  }
  
  /**
   * 批量删除
   */
  @AutoLog("ClientConfig-批量删除")
  @DeleteMapping("/deleteBatch")
  public Result<?> deleteBatch(@RequestParam String ids) {
    this.clientConfigService.removeByIds(Arrays.asList(ids.split(",")));
    return Result.ok();
  }
  
  /**
   * 通过id查询
   */
  @GetMapping("/queryById")
  public Result<?> queryById(@RequestParam String id) {
    ClientConfig clientConfig = clientConfigService.getById(id);
    return Result.ok(clientConfig);
  }
 
  /**
   * 导出excel
   */
  @RequestMapping("/exportXls")
  public void exportXls(HttpServletRequest request, HttpServletResponse response, ClientConfig clientConfig)  throws IOException {
      super.exportXls(request, response, clientConfig, "ClientConfig");
  }
 
  /**
   * 通过excel导入数据
   */
  @PostMapping("/importExcel")
  public Result<?> importExcel(HttpServletRequest request) throws Exception {
      return super.importExcel(request, ClientConfig.class);
  }
}