1 changed files with 121 additions and 0 deletions
@ -0,0 +1,121 @@ |
|||
package com.iflytop.sgs.app.controller; |
|||
|
|||
import com.iflytop.sgs.app.model.entity.DeviceParamConfig; |
|||
import com.iflytop.sgs.app.model.vo.DeviceParamGroupVO; |
|||
import com.iflytop.sgs.app.model.vo.ModuleIdVO; |
|||
import com.iflytop.sgs.app.model.vo.RegIndexVO; |
|||
import com.iflytop.sgs.app.service.api.DeviceParamConfigService; |
|||
import com.iflytop.sgs.common.result.Result; |
|||
import io.swagger.v3.oas.annotations.Operation; |
|||
import io.swagger.v3.oas.annotations.Parameter; |
|||
import io.swagger.v3.oas.annotations.tags.Tag; |
|||
import jakarta.validation.Valid; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.http.MediaType; |
|||
import org.springframework.web.bind.annotation.*; |
|||
import org.springframework.web.multipart.MultipartFile; |
|||
|
|||
import java.io.BufferedReader; |
|||
import java.io.IOException; |
|||
import java.io.InputStreamReader; |
|||
import java.nio.charset.StandardCharsets; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 设备参数配置 |
|||
*/ |
|||
@Tag(name = "设备参数配置") |
|||
@RestController |
|||
@RequestMapping("/api/device-param") |
|||
@RequiredArgsConstructor |
|||
@Slf4j |
|||
public class DeviceParamController { |
|||
|
|||
private final DeviceParamConfigService deviceParamConfigService; |
|||
|
|||
@Operation(summary = "获取所有设备配置") |
|||
@GetMapping("/list") |
|||
public Result<List<DeviceParamGroupVO>> getList() { |
|||
List<DeviceParamGroupVO> vos = deviceParamConfigService.listGroupedByModule(); |
|||
return Result.success(vos); |
|||
} |
|||
|
|||
@Operation(summary = "获取所有设备模块") |
|||
@GetMapping("/modules") |
|||
public Result<List<ModuleIdVO>> listAllModules() { |
|||
List<ModuleIdVO> vos = deviceParamConfigService.listAllModuleIds(); |
|||
return Result.success(vos); |
|||
} |
|||
|
|||
@Operation(summary = "获取所有设备模块配置项") |
|||
@GetMapping("/reg-indices") |
|||
public Result<List<RegIndexVO>> listAllRegIndices() { |
|||
List<RegIndexVO> vos = deviceParamConfigService.listAllRegIndices(); |
|||
return Result.success(vos); |
|||
} |
|||
|
|||
|
|||
@Operation(summary = "添加新配置") |
|||
@PostMapping("") |
|||
public Result<String> add(@Valid @RequestBody DeviceParamConfig deviceParamConfig) { |
|||
deviceParamConfigService.save(deviceParamConfig); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@Operation(summary = "修改配置") |
|||
@PutMapping("") |
|||
public Result<String> update(@Valid @RequestBody DeviceParamConfig deviceParamConfig) { |
|||
deviceParamConfigService.updateById(deviceParamConfig); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@Operation(summary = "删除配置") |
|||
@DeleteMapping("/{ids}") |
|||
public Result<String> delete(@Parameter(description = "矿石ID,多个以英文逗号(,)分割") @PathVariable String ids) { |
|||
boolean isSuccess = deviceParamConfigService.deleteDeviceParam(ids); |
|||
if (isSuccess) { |
|||
return Result.success(); |
|||
} |
|||
return Result.failed(); |
|||
} |
|||
|
|||
@Operation(summary = "上传csv文件覆盖数据库配置") |
|||
@PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) |
|||
public Result<String> uploadFile(@RequestPart("file") MultipartFile file) { |
|||
//文件后缀检查 |
|||
if (!file.getOriginalFilename().endsWith(".csv")) { |
|||
return Result.failed("<UNK>.csv<UNK>"); |
|||
} |
|||
try (BufferedReader reader = new BufferedReader( |
|||
new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8))) { |
|||
String line; |
|||
//是否含有表头 |
|||
boolean isHeader = true; |
|||
List<DeviceParamConfig> deviceParamConfigList = new ArrayList<>(); |
|||
while ((line = reader.readLine()) != null) { |
|||
if (isHeader) { |
|||
isHeader = false; |
|||
continue; // 跳过CSV文件的标题行 |
|||
} |
|||
String[] data = line.split(","); |
|||
if (data.length >= 4) { // 假设CSV文件至少包含4列 |
|||
DeviceParamConfig deviceParamConfig = new DeviceParamConfig(); |
|||
deviceParamConfig.setId(Long.valueOf(data[0])); |
|||
deviceParamConfig.setMid(String.valueOf(data[1])); |
|||
deviceParamConfig.setRegIndex(String.valueOf(data[2])); |
|||
deviceParamConfig.setRegVal(Integer.valueOf(data[3])); |
|||
deviceParamConfigService.save(deviceParamConfig); |
|||
deviceParamConfigList.add(deviceParamConfig); |
|||
} |
|||
} |
|||
deviceParamConfigService.saveOrUpdateBatch(deviceParamConfigList); |
|||
} catch (IOException e) { |
|||
log.error("csv文件导入数据库失败:{}", e.getMessage()); |
|||
throw new RuntimeException(e); |
|||
} |
|||
return Result.success(); |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue