feat(device): 新增设备导入导出功能

- 新增设备导入模板下载功能
- 实现设备新增导入功能,包括数据校验和错误反馈
- 添加设备更新导入功能,支持修改现有设备信息
- 优化设备导出功能,支持导出选中的设备- 新增设备状态和质保状态的字典项
This commit is contained in:
曹鹏飞 2025-04-06 13:43:44 +08:00
parent 63a587f39e
commit cd1775a297
7 changed files with 627 additions and 69 deletions

View File

@ -1,7 +1,10 @@
package com.nflg.mobilebroken.admin.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Sets;
import com.nflg.mobilebroken.admin.annotation.ApiMark;
@ -9,31 +12,38 @@ import com.nflg.mobilebroken.admin.pojo.dto.DeviceDTO;
import com.nflg.mobilebroken.admin.pojo.dto.SyncFromCrmDTO;
import com.nflg.mobilebroken.admin.pojo.query.DeviceQuery;
import com.nflg.mobilebroken.admin.pojo.vo.DeviceDetailResultVO;
import com.nflg.mobilebroken.admin.pojo.vo.DeviceExcelVO;
import com.nflg.mobilebroken.admin.service.AdminDeviceService;
import com.nflg.mobilebroken.common.constant.Constant;
import com.nflg.mobilebroken.common.constant.STATE;
import com.nflg.mobilebroken.common.exception.NflgException;
import com.nflg.mobilebroken.common.pojo.ApiResult;
import com.nflg.mobilebroken.common.pojo.PageData;
import com.nflg.mobilebroken.common.util.EecExcelUtil;
import com.nflg.mobilebroken.common.util.VUtils;
import com.nflg.mobilebroken.repository.entity.Device;
import com.nflg.mobilebroken.repository.entity.DictionaryItem;
import com.nflg.mobilebroken.repository.service.IDeviceService;
import com.nflg.mobilebroken.repository.service.IDictionaryItemService;
import com.nflg.mobilebroken.common.pojo.dto.DeviceAddImportDTO;
import com.nflg.mobilebroken.common.pojo.dto.DeviceUpdateImportDTO;
import com.nflg.mobilebroken.common.util.*;
import com.nflg.mobilebroken.repository.entity.*;
import com.nflg.mobilebroken.repository.service.*;
import com.nflg.mobilebroken.starter.annotation.MethodInfoMark;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.ttzero.excel.entity.ListSheet;
import org.ttzero.excel.entity.TemplateSheet;
import org.ttzero.excel.entity.Workbook;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.net.URLEncoder;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -54,6 +64,18 @@ public class DeviceController extends ControllerBase {
@Resource
IDictionaryItemService dictionaryItemService;
@Resource
private ITBaseCustomerService customerService;
@Resource
private ITBaseAreaService areaService;
@Resource
private ITBaseDeviceTypeService deviceTypeService;
@Resource
private IDeviceComponentService deviceComponentService;
/**
* 获取设备列表
@ -156,64 +178,64 @@ public class DeviceController extends ControllerBase {
return ApiResult.success(true);
}
@GetMapping("downTemplate")
@ApiMark(moduleName = "设备管理", apiName = "导入模板下载")
// @MethodInfoMark(value = "导入模板下载",menuName = "设备管理")
public void downTemplate(HttpServletResponse response) throws IOException {
EecExcelUtil.setResponseExcelHeader(response,"设备导入模板");
// @GetMapping("downTemplate")
// @ApiMark(moduleName = "设备管理", apiName = "导入模板下载")
//// @MethodInfoMark(value = "导入模板下载",menuName = "设备管理")
// public void downTemplate(HttpServletResponse response) throws IOException {
// EecExcelUtil.setResponseExcelHeader(response,"设备导入模板");
//
// final ListSheet<DeviceExcelVO> listSheet = new ListSheet<DeviceExcelVO>() {
// @Override
// protected List<DeviceExcelVO> more() {
// return null;
// }
// };
// EecExcelUtil.eecExcel("设备导入模板", listSheet, response);
// }
final ListSheet<DeviceExcelVO> listSheet = new ListSheet<DeviceExcelVO>() {
@Override
protected List<DeviceExcelVO> more() {
return null;
}
};
EecExcelUtil.eecExcel("设备导入模板", listSheet, response);
}
// /**
// * 导出设备
// * @param response
// * @throws IOException
// */
// @GetMapping("exportData")
//// @MethodInfoMark(value = "导出设备",menuName = "设备管理")
// @ApiMark(moduleName = "设备管理", apiName = "导出设备")
// public void exportData(HttpServletResponse response) throws IOException {
//
// EecExcelUtil.setResponseExcelHeader(response,"设备列表");
//
// final ListSheet<DeviceExcelVO> listSheet = new ListSheet<DeviceExcelVO>() {
// int i=0;
// @Override
// protected List<DeviceExcelVO> more() {
//
// List<Device> list = deviceService.list();
// i++;
// return i>1?null : Convert.toList(DeviceExcelVO.class, list);
// }
// };
// EecExcelUtil.eecExcel("设备列表", listSheet, response);
// }
/**
* 导出设备
* @param response
* @throws IOException
*/
@GetMapping("exportData")
// @MethodInfoMark(value = "导出设备",menuName = "设备管理")
@ApiMark(moduleName = "设备管理", apiName = "导出设备")
public void exportData(HttpServletResponse response) throws IOException {
EecExcelUtil.setResponseExcelHeader(response,"设备列表");
final ListSheet<DeviceExcelVO> listSheet = new ListSheet<DeviceExcelVO>() {
int i=0;
@Override
protected List<DeviceExcelVO> more() {
List<Device> list = deviceService.list();
i++;
return i>1?null : Convert.toList(DeviceExcelVO.class, list);
}
};
EecExcelUtil.eecExcel("设备列表", listSheet, response);
}
/**
* 导入设备
* @param file
* @return
*/
@PostMapping("importData")
@ApiMark(moduleName = "设备管理", apiName = "导入设备")
public ApiResult<Boolean> importData( @RequestParam(value = "file") MultipartFile file){
try {
List<DeviceExcelVO> data = EecExcelUtil.getExcelContext(file.getInputStream(), DeviceExcelVO.class);
VUtils.trueThrowBusinessError(CollUtil.isEmpty(data)).throwMessage("导入文件内容为空");
adminDeviceService.importData(data);
} catch (IOException e) {
throw new NflgException(STATE.BusinessError, "导出失败:"+e.getMessage());
}
return ApiResult.success(true);
}
// /**
// * 导入设备
// * @param file
// * @return
// */
// @PostMapping("importData")
// @ApiMark(moduleName = "设备管理", apiName = "导入设备")
// public ApiResult<Boolean> importData( @RequestParam(value = "file") MultipartFile file){
//
// try {
// List<DeviceExcelVO> data = EecExcelUtil.getExcelContext(file.getInputStream(), DeviceExcelVO.class);
// VUtils.trueThrowBusinessError(CollUtil.isEmpty(data)).throwMessage("导入文件内容为空");
// adminDeviceService.importData(data);
// } catch (IOException e) {
// throw new NflgException(STATE.BusinessError, "导出失败:"+e.getMessage());
// }
// return ApiResult.success(true);
// }
/**
@ -228,4 +250,423 @@ public class DeviceController extends ControllerBase {
return ApiResult.success(true);
}
/**
* 下载导入模板(新增)
*/
@GetMapping("downTemplateForAdd")
@ApiMark(moduleName = "设备管理", apiName = "下载导入模板(新增)")
public void downTemplateForAdd(HttpServletResponse response) throws Exception {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("设备新增导入模板.xlsx", "UTF-8"));
ClassPathResource resource = new ClassPathResource("templates/设备新增导入模板.xlsx");
List<DeviceAddImportDTO> datas = new ArrayList<>();
new Workbook()
.addSheet(new TemplateSheet(Paths.get(resource.getURI()))
.setData(datas)
.setData("@list:deviceStateDesc", dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE)
.stream().map(DictionaryItem::getName).collect(Collectors.toList())))
.writeTo(response.getOutputStream());
}
/**
* 新增导入
* @param file 文件
*/
@Transactional
@PostMapping("importForAdd")
@ApiMark(moduleName = "设备管理", apiName = "新增导入")
public void importForAdd(HttpServletResponse response, @RequestParam(value = "file") MultipartFile file) throws IOException {
List<DeviceAddImportDTO> data = EecExcelUtil.getExcelContext(file.getInputStream(), DeviceAddImportDTO.class);
VUtils.trueThrowBusinessError(CollectionUtil.isEmpty(data)).throwMessage("导入文件内容为空");
if (addCheckAndImport(data)) {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
byte[] datas = StrUtil.bytes(JSONUtil.toJsonStr(ApiResult.success()));
response.getOutputStream().write(datas, 0, datas.length);
} else {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("设备新增.xlsx", "UTF-8"));
ClassPathResource resource = new ClassPathResource("templates/设备新增导入模板.xlsx");
new Workbook()
.addSheet(new TemplateSheet(Paths.get(resource.getURI()))
.setData(data)
.setData("@list:deviceStateDesc", dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE)
.stream().map(DictionaryItem::getName).collect(Collectors.toList())))
.writeTo(response.getOutputStream());
}
}
@Transactional
public boolean addCheckAndImport(List<DeviceAddImportDTO> data) {
List<DictionaryItem> states = dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE);
List<DictionaryItem> warrantyStates = dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_WARRANTY_STATE);
List<Device> devices = new ArrayList<>();
List<TBaseDeviceType> deviceTypes = new ArrayList<>();
List<DeviceComponent> deviceComponents = new ArrayList<>();
for (DeviceAddImportDTO dto : data) {
Device device = new Device();
StringBuilder sb = new StringBuilder();
device.setDeviceNo(dto.getDeviceNo());
if (StrUtil.isBlank(dto.getDeviceNo())) {
sb.append("设备编号不能为空;");
}
device.setDeviceName(dto.getDeviceName());
device.setDeviceType(dto.getDeviceType());
if (StrUtil.isBlank(dto.getDeviceType())) {
sb.append("设备类型不能为空;");
}
device.setDeviceTypeSub(dto.getDeviceTypeSub());
if (StrUtil.isBlank(dto.getDeviceTypeSub())) {
sb.append("设备类型细分不能为空;");
}
device.setModelNo(dto.getModelNo());
if (StrUtil.isBlank(dto.getModelNo())) {
sb.append("设备机型不能为空;");
}
if (StrUtil.isBlank(dto.getDeviceStateDesc())) {
sb.append("设备状态不能为空;");
} else {
DictionaryItem state = states.stream().filter(s -> StrUtil.equals(s.getName(), dto.getDeviceStateDesc())).findFirst().orElse(null);
if (Objects.isNull(state)) {
sb.append("设备状态无效;");
} else {
device.setDeviceState(state.getId());
if (StrUtil.isNotBlank(dto.getDeviceNo()) && StrUtil.equals(state.getCode(), "Normal")
&& deviceService.lambdaQuery().eq(Device::getDeviceNo, dto.getDeviceNo()).eq(Device::getDeviceState, state.getId()).exists()) {
sb.append("设备已存在;");
}
}
}
device.setCustomerId(0);
device.setCustomerName(dto.getCustomerName());
if (StrUtil.isNotBlank(dto.getAgentName())) {
TBaseCustomer customer = customerService.lambdaQuery()
.eq(TBaseCustomer::getDelIs, 0)
.eq(TBaseCustomer::getAgencyCompanyName, dto.getAgentName())
.one();
if (Objects.isNull(customer)) {
sb.append("代理商不存在;");
} else if (customer.getEnableState() == 0) {
sb.append("代理商状态为禁用;");
} else {
device.setAgentName(customer.getAgencyCompanyName());
device.setAgentCode(customer.getAgencyCompanyCode());
}
}
if (StrUtil.isNotBlank(dto.getAreaName())) {
TBaseArea area = areaService.lambdaQuery().eq(TBaseArea::getDelIs, 0).eq(TBaseArea::getAreaName, dto.getAreaName()).one();
if (Objects.isNull(area)) {
sb.append("区域不存在;");
} else if (area.getAreaState().intValue() == 0) {
sb.append("区域为禁用;");
} else {
device.setAreaName(area.getAreaName());
device.setAreaCode(area.getAreaCode());
}
}
if (StrUtil.isNotBlank(dto.getShipmentDate())) {
try {
LocalDate date = LocalDate.parse(dto.getShipmentDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
device.setShipmentDate(DateTimeUtil.asSystemDate(date));
} catch (DateTimeParseException e) {
sb.append("发货日期无效;");
}
}
if (StrUtil.isNotBlank(dto.getStartWarrantyDate())) {
try {
LocalDate date = LocalDate.parse(dto.getStartWarrantyDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
device.setStartWarrantyDate(DateTimeUtil.asSystemDate(date));
if (StrUtil.isBlank(dto.getWarrantyMonth())) {
sb.append("质保期不能为空;");
} else {
int month = Integer.parseInt(dto.getWarrantyMonth());
device.setWarrantyMonth(month);
if (date.isAfter(LocalDate.now())) {
device.setWarrantyState(warrantyStates.stream().filter(s -> StrUtil.equals(s.getCode(), "NotStarted")).findFirst().get().getId());
} else if (date.plusMonths(month).isAfter(LocalDate.now())) {
device.setWarrantyState(warrantyStates.stream().filter(s -> StrUtil.equals(s.getCode(), "WithinWarranty")).findFirst().get().getId());
} else {
device.setWarrantyState(warrantyStates.stream().filter(s -> StrUtil.equals(s.getCode(), "OutsideWarranty")).findFirst().get().getId());
}
}
} catch (DateTimeParseException e) {
sb.append("开始质保日期无效;");
} catch (NumberFormatException e) {
sb.append("质保期无效;");
}
}
device.setDataValidState(true);
device.setSourceFrom(3);
device.setCreateBy(AdminUserUtil.getUserName());
device.setCreateTime(LocalDateTime.now());
device.setRemark("从excel新增");
dto.setError(sb.toString());
if (StrUtil.isBlank(dto.getError())){
devices.add(device);
if (!deviceTypeService.lambdaQuery().eq(TBaseDeviceType::getDeviceType, dto.getDeviceType()).exists()){
TBaseDeviceType ent = new TBaseDeviceType();
ent.setDeviceTypeCode(UniqueSequenceGenerator.generateCode(com.nflg.mobilebroken.admin.constant.Constant.DeviceTypeCodePrefix));
ent.setDeviceType(dto.getDeviceType());
ent.setDeviceState(Boolean.TRUE);
ent.setDataCreateUserNo(AdminUserUtil.getUserNo());
ent.setDataCreateUserName(AdminUserUtil.getUserName());
ent.setDataCreateTime(LocalDateTime.now());
ent.setDataModifyUserNo(AdminUserUtil.getUserNo());
ent.setDataModifyUserName(AdminUserUtil.getUserName());
ent.setDataModifyTime(LocalDateTime.now());
deviceTypes.add(ent);
}
if (!deviceComponentService.lambdaQuery().eq(DeviceComponent::getModelNo, dto.getModelNo()).exists()){
DeviceComponent ent = new DeviceComponent();
ent.setModelNo(dto.getModelNo());
ent.setComponent("");
ent.setEnable(true);
ent.setCreateBy(AdminUserUtil.getUserName());
ent.setCreateTime(LocalDateTime.now());
deviceComponents.add(ent);
}
}
}
if (data.stream().noneMatch(dto -> StrUtil.isNotBlank(dto.getError()))){
deviceService.saveBatch(devices);
if (CollUtil.isNotEmpty(deviceTypes)){
deviceTypeService.saveBatch(deviceTypes);
}
if (CollUtil.isNotEmpty(deviceComponents)){
deviceComponentService.saveBatch(deviceComponents);
}
return true;
}
return false;
}
/**
* 导出选中的设备
* @param ids 选中的id集合
*/
@PostMapping("exportSelect")
@ApiMark(moduleName = "设备管理", apiName = "导出选中的设备")
public void exportSelect(HttpServletResponse response,@Valid @RequestBody @NotEmpty List<Integer> ids) throws IOException {
List<Device> devices=deviceService.listByIds(ids);
List<DictionaryItem> states = dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE);
List<DeviceUpdateImportDTO> datas=devices.stream().map(d->{
DeviceUpdateImportDTO dto= new DeviceUpdateImportDTO();
dto.setId(d.getId());
dto.setDeviceNo(d.getDeviceNo());
dto.setDeviceName(d.getDeviceName());
dto.setDeviceType(d.getDeviceType());
dto.setDeviceTypeSub(d.getDeviceTypeSub());
dto.setModelNo(d.getModelNo());
dto.setDeviceStateDesc(states.stream().filter(s->Objects.equals(s.getId(),d.getDeviceState())).findFirst().get().getName());
dto.setCustomerName(d.getCustomerName());
dto.setAgentName(d.getAgentName());
dto.setAreaName(d.getAreaName());
dto.setShipmentDate(DateTimeUtil.format(d.getShipmentDate(), "yyyy-MM-dd"));
dto.setStartWarrantyDate(DateTimeUtil.format(d.getStartWarrantyDate(), "yyyy-MM-dd"));
if (Objects.nonNull(d.getWarrantyMonth())) {
dto.setWarrantyMonth(String.valueOf(d.getWarrantyMonth()));
}
return dto;
}).collect(Collectors.toList());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("设备更新.xlsx", "UTF-8"));
ClassPathResource resource = new ClassPathResource("templates/设备更新导入模板.xlsx");
new Workbook()
.addSheet(new TemplateSheet(Paths.get(resource.getURI()))
.setData(datas)
.setData("@list:deviceStateDesc", dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE)
.stream().map(DictionaryItem::getName).collect(Collectors.toList())))
.writeTo(response.getOutputStream());
}
/**
* 更新导入
* @param file 文件
*/
@Transactional
@PostMapping("importForUpdate")
@ApiMark(moduleName = "设备管理", apiName = "更新导入")
public void importForUpdate(HttpServletResponse response, @RequestParam(value = "file") MultipartFile file) throws IOException {
List<DeviceUpdateImportDTO> data = EecExcelUtil.getExcelContext(file.getInputStream(), DeviceUpdateImportDTO.class);
VUtils.trueThrowBusinessError(CollectionUtil.isEmpty(data)).throwMessage("导入文件内容为空");
if (updateCheckAndImport(data)) {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
byte[] datas = StrUtil.bytes(JSONUtil.toJsonStr(ApiResult.success()));
response.getOutputStream().write(datas, 0, datas.length);
} else {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("设备更新.xlsx", "UTF-8"));
ClassPathResource resource = new ClassPathResource("templates/设备更新导入模板.xlsx");
new Workbook()
.addSheet(new TemplateSheet(Paths.get(resource.getURI()))
.setData(data)
.setData("@list:deviceStateDesc", dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE)
.stream().map(DictionaryItem::getName).collect(Collectors.toList())))
.writeTo(response.getOutputStream());
}
}
@Transactional
public boolean updateCheckAndImport(List<DeviceUpdateImportDTO> data) {
List<DictionaryItem> states = dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_STATE);
List<DictionaryItem> warrantyStates = dictionaryItemService.getListByDictionaryCode(Constant.DICTIONARY_DEVICE_WARRANTY_STATE);
List<Device> devices = new ArrayList<>();
List<TBaseDeviceType> deviceTypes = new ArrayList<>();
List<DeviceComponent> deviceComponents = new ArrayList<>();
for (DeviceUpdateImportDTO dto : data) {
Device device = new Device();
StringBuilder sb = new StringBuilder();
if (Objects.isNull(dto.getId())) {
sb.append("id不能为空;");
}else {
device=deviceService.getById(dto.getId());
if (Objects.isNull(device)){
sb.append("设备不存在;");
device = new Device();
}
}
device.setDeviceNo(dto.getDeviceNo());
if (StrUtil.isBlank(dto.getDeviceNo())) {
sb.append("设备编号不能为空;");
}
device.setDeviceName(dto.getDeviceName());
device.setDeviceType(dto.getDeviceType());
if (StrUtil.isBlank(dto.getDeviceType())) {
sb.append("设备类型不能为空;");
}
device.setDeviceTypeSub(dto.getDeviceTypeSub());
if (StrUtil.isBlank(dto.getDeviceTypeSub())) {
sb.append("设备类型细分不能为空;");
}
device.setModelNo(dto.getModelNo());
if (StrUtil.isBlank(dto.getModelNo())) {
sb.append("设备机型不能为空;");
}
if (StrUtil.isBlank(dto.getDeviceStateDesc())) {
sb.append("设备状态不能为空;");
} else {
DictionaryItem state = states.stream().filter(s -> StrUtil.equals(s.getName(), dto.getDeviceStateDesc())).findFirst().orElse(null);
if (Objects.isNull(state)) {
sb.append("设备状态无效;");
} else {
device.setDeviceState(state.getId());
if (StrUtil.isNotBlank(dto.getDeviceNo()) && StrUtil.equals(state.getCode(), "Normal")
&& deviceService.lambdaQuery().ne(Device::getId, device.getId()).eq(Device::getDeviceNo, dto.getDeviceNo()).eq(Device::getDeviceState, state.getId()).exists()) {
sb.append("设备已存在;");
}
}
}
device.setCustomerId(0);
device.setCustomerName(dto.getCustomerName());
if (StrUtil.isNotBlank(dto.getAgentName())) {
TBaseCustomer customer = customerService.lambdaQuery()
.eq(TBaseCustomer::getDelIs, 0)
.eq(TBaseCustomer::getAgencyCompanyName, dto.getAgentName())
.one();
if (Objects.isNull(customer)) {
sb.append("代理商不存在;");
} else if (customer.getEnableState() == 0) {
sb.append("代理商状态为禁用;");
} else {
device.setAgentName(customer.getAgencyCompanyName());
device.setAgentCode(customer.getAgencyCompanyCode());
}
}else {
device.setAgentName("");
device.setAgentCode("");
}
if (StrUtil.isNotBlank(dto.getAreaName())) {
TBaseArea area = areaService.lambdaQuery().eq(TBaseArea::getDelIs, 0).eq(TBaseArea::getAreaName, dto.getAreaName()).one();
if (Objects.isNull(area)) {
sb.append("区域不存在;");
} else if (area.getAreaState().intValue() == 0) {
sb.append("区域为禁用;");
} else {
device.setAreaName(area.getAreaName());
device.setAreaCode(area.getAreaCode());
}
}else {
device.setAreaName("");
device.setAreaCode("");
}
if (StrUtil.isNotBlank(dto.getShipmentDate())) {
try {
LocalDate date = LocalDate.parse(dto.getShipmentDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
device.setShipmentDate(DateTimeUtil.asSystemDate(date));
} catch (DateTimeParseException e) {
sb.append("发货日期无效;");
}
}else {
device.setShipmentDate(null);
}
if (StrUtil.isNotBlank(dto.getStartWarrantyDate())) {
try {
LocalDate date = LocalDate.parse(dto.getStartWarrantyDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
device.setStartWarrantyDate(DateTimeUtil.asSystemDate(date));
if (StrUtil.isBlank(dto.getWarrantyMonth())) {
sb.append("质保期不能为空;");
} else {
int month = Integer.parseInt(dto.getWarrantyMonth());
device.setWarrantyMonth(month);
if (date.isAfter(LocalDate.now())) {
device.setWarrantyState(warrantyStates.stream().filter(s -> StrUtil.equals(s.getCode(), "NotStarted")).findFirst().get().getId());
} else if (date.plusMonths(month).isAfter(LocalDate.now())) {
device.setWarrantyState(warrantyStates.stream().filter(s -> StrUtil.equals(s.getCode(), "WithinWarranty")).findFirst().get().getId());
} else {
device.setWarrantyState(warrantyStates.stream().filter(s -> StrUtil.equals(s.getCode(), "OutsideWarranty")).findFirst().get().getId());
}
}
} catch (DateTimeParseException e) {
sb.append("开始质保日期无效;");
} catch (NumberFormatException e) {
sb.append("质保期无效;");
}
}else {
device.setStartWarrantyDate(null);
device.setWarrantyState(null);
}
device.setUpdateBy(AdminUserUtil.getUserName());
device.setUpdateTime(LocalDateTime.now());
device.setRemark("从excel更新");
dto.setError(sb.toString());
if (StrUtil.isBlank(dto.getError())){
devices.add(device);
if (!deviceTypeService.lambdaQuery().eq(TBaseDeviceType::getDeviceType, dto.getDeviceType()).exists()){
TBaseDeviceType ent = new TBaseDeviceType();
ent.setDeviceTypeCode(UniqueSequenceGenerator.generateCode(com.nflg.mobilebroken.admin.constant.Constant.DeviceTypeCodePrefix));
ent.setDeviceType(dto.getDeviceType());
ent.setDeviceState(Boolean.TRUE);
ent.setDataCreateUserNo(AdminUserUtil.getUserNo());
ent.setDataCreateUserName(AdminUserUtil.getUserName());
ent.setDataCreateTime(LocalDateTime.now());
ent.setDataModifyUserNo(AdminUserUtil.getUserNo());
ent.setDataModifyUserName(AdminUserUtil.getUserName());
ent.setDataModifyTime(LocalDateTime.now());
deviceTypes.add(ent);
}
if (!deviceComponentService.lambdaQuery().eq(DeviceComponent::getModelNo, dto.getModelNo()).exists()){
DeviceComponent ent = new DeviceComponent();
ent.setModelNo(dto.getModelNo());
ent.setComponent("");
ent.setEnable(true);
ent.setCreateBy(AdminUserUtil.getUserName());
ent.setCreateTime(LocalDateTime.now());
deviceComponents.add(ent);
}
}
}
if (data.stream().noneMatch(dto -> StrUtil.isNotBlank(dto.getError()))){
deviceService.updateBatchById(devices);
if (CollUtil.isNotEmpty(deviceTypes)){
deviceTypeService.saveBatch(deviceTypes);
}
if (CollUtil.isNotEmpty(deviceComponents)){
deviceComponentService.saveBatch(deviceComponents);
}
return true;
}
return false;
}
}

View File

@ -112,4 +112,8 @@ public class Constant {
public static final String DICTIONARY_SOLUTION_MEASURES = "SolutionMeasures";
public static final String DICTIONARY_SOLUTION_REVIEW_DEPARTMENT = "SolutionReviewDepartment";
public static final String DICTIONARY_DEVICE_STATE ="deviceState";
public static final String DICTIONARY_DEVICE_WARRANTY_STATE ="deviceWarrantyState";
}

View File

@ -0,0 +1,88 @@
package com.nflg.mobilebroken.common.pojo.dto;
import lombok.Data;
import lombok.experimental.Accessors;
import org.ttzero.excel.annotation.ExcelColumn;
@Data
@Accessors(chain = true)
public class DeviceAddImportDTO {
/**
* 设备编号
*/
@ExcelColumn("设备编号*")
private String deviceNo;
/**
* 设备名称
*/
@ExcelColumn("设备名称")
private String deviceName;
/**
* 设备类型
*/
@ExcelColumn("设备类型*")
private String deviceType;
/**
* 设备类型细分
*/
@ExcelColumn("设备类型细分*")
private String deviceTypeSub;
/**
* 设备机型
*/
@ExcelColumn("设备机型*")
private String modelNo;
/**
* 设备状态-来自字典
*/
@ExcelColumn("设备状态*")
private String deviceStateDesc;
/**
* 客户名称
*/
@ExcelColumn("客户名称")
private String customerName;
/**
* 代理商名称
*/
@ExcelColumn("代理商")
private String agentName;
/**
* 区域名称
*/
@ExcelColumn("区域")
private String areaName;
/**
* 发货日期
*/
@ExcelColumn("发货日期")
private String shipmentDate;
/**
* 开始质保日期
*/
@ExcelColumn("开始质保日期")
private String startWarrantyDate;
/**
* 质保期
*/
@ExcelColumn("质保期(月)")
private String warrantyMonth;
/**
* 错误信息
*/
@ExcelColumn("错误信息")
private String error;
}

View File

@ -0,0 +1,18 @@
package com.nflg.mobilebroken.common.pojo.dto;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.ttzero.excel.annotation.ExcelColumn;
@EqualsAndHashCode(callSuper = false)
@Data
@Accessors(chain = true)
public class DeviceUpdateImportDTO extends DeviceAddImportDTO{
/**
* id
*/
@ExcelColumn("id*")
private Integer id;
}

View File

@ -27,6 +27,13 @@ public class DateTimeUtil {
return dateTime.format(DateTimeFormatter.ofPattern(pattern));
}
public static String format(LocalDate date,String pattern){
if (Objects.isNull(date)){
return "";
}
return date.format(DateTimeFormatter.ofPattern(pattern));
}
public static LocalDate asSystemDate(LocalDate date){
if (Objects.isNull(date)){
return null;