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);
|
}
|
}
|