|
@@ -1,5 +1,8 @@
|
|
|
package com.xy.service;
|
|
|
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.context.AnalysisContext;
|
|
|
+import com.alibaba.excel.read.listener.ReadListener;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
@@ -21,6 +24,7 @@ import com.xy.mapper.DeviceCreateIdsMapper;
|
|
|
import com.xy.utils.*;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.Data;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -28,7 +32,13 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
@@ -187,6 +197,34 @@ public class DeviceCreateIdsServiceImpl extends ServiceImpl<DeviceCreateIdsMappe
|
|
|
QRCodeUtils.create(infos, 400, 400, "back1.png", list).download(list);
|
|
|
}
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
+ @ApiOperation("下载自定义设备id模板")
|
|
|
+ @PostMapping("downloadIdTemplet")
|
|
|
+ public void downloadIdTemplet(HttpServletResponse response) {
|
|
|
+ InputStream inputStream = IoUtils.inputStream("id_templet.xlsx").get();
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + "id_templet.xlsx");
|
|
|
+ response.setContentType("application/xlsx");
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int bytesRead;
|
|
|
+ OutputStream outputStream = response.getOutputStream();
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("批量添加自定义机器号")
|
|
|
+ @PostMapping("uploadSaveCustom")
|
|
|
+ public R uploadSaveCustom(@RequestParam("file") MultipartFile file, @RequestParam("deviceType") Integer deviceType, @RequestParam("doorType") String doorType, @RequestParam("createPc") String createPc) {
|
|
|
+ ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_COMMON_POLL, 1)
|
|
|
+ .execute(() -> {
|
|
|
+ try {
|
|
|
+ EasyExcel.read(file.getInputStream(), UploadSaveCustom.class, new UploadSaveCustomListener(deviceType, doorType, createPc, AuthorizeUtils.getLoginId(Long.class), this)).sheet().doRead();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("", e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
|
|
|
private void weight(JMap<Long, DeviceCreateIds> deviceCreateIdss, int num) {
|
|
|
List<DeviceCreateIds> list = list(new LambdaQueryWrapper<DeviceCreateIds>().in(DeviceCreateIds::getDeviceId, deviceCreateIdss.toList().key()));
|
|
@@ -202,4 +240,72 @@ public class DeviceCreateIdsServiceImpl extends ServiceImpl<DeviceCreateIdsMappe
|
|
|
weight(deviceCreateIdss, num);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Slf4j
|
|
|
+ @RequiredArgsConstructor
|
|
|
+ public static class UploadSaveCustomListener implements ReadListener<UploadSaveCustom> {
|
|
|
+
|
|
|
+ private final Integer deviceType;
|
|
|
+
|
|
|
+ private final String doorType;
|
|
|
+
|
|
|
+ private final String createPc;
|
|
|
+
|
|
|
+ private final Long loginId;
|
|
|
+
|
|
|
+ private final DeviceCreateIdsServiceImpl deviceCreateIdsService;
|
|
|
+
|
|
|
+ private JMap<Long, UploadSaveCustom> deviceIds = new JHashMap<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 这个每一条数据解析都会来调用
|
|
|
+ *
|
|
|
+ * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()}
|
|
|
+ * @param context
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void invoke(UploadSaveCustom data, AnalysisContext context) {
|
|
|
+ deviceIds.put(data.getDeviceId(), data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 所有数据解析完成了 都会来调用
|
|
|
+ *
|
|
|
+ * @param context
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void doAfterAllAnalysed(AnalysisContext context) {
|
|
|
+ List<DeviceCreateIds> list = deviceCreateIdsService.list(new LambdaQueryWrapper<DeviceCreateIds>().in(DeviceCreateIds::getDeviceId, deviceIds.toList().key()));
|
|
|
+ if (Emptys.check(list)) {
|
|
|
+ list.forEach(deviceCreateIds -> {
|
|
|
+ deviceIds.remove(deviceCreateIds.getDeviceId());
|
|
|
+ log.info("重复的设备号:{}", deviceCreateIds.getDeviceId());
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (Emptys.check(deviceIds)) {
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ JList<Long> deviceIds2 = deviceIds.toList().key();
|
|
|
+ List<DeviceCreateIds> saves = new ArrayList<>(deviceIds2.size());
|
|
|
+ deviceIds2.forEach(deviceId -> {
|
|
|
+ DeviceCreateIds deviceCreateIdsInfo = new DeviceCreateIds()
|
|
|
+ .setDeviceId(deviceId)
|
|
|
+ .setDeviceType(deviceType)
|
|
|
+ .setDoorType(doorType)
|
|
|
+ .setCreatePc(createPc)
|
|
|
+ .setCreateUserId(loginId)
|
|
|
+ .setIsCustom(true)
|
|
|
+ .setCreateTime(now);
|
|
|
+ saves.add(deviceCreateIdsInfo);
|
|
|
+ });
|
|
|
+ deviceCreateIdsService.saveBatch(saves);
|
|
|
+ log.info("批量添加自定义机器号完成,共{}条", saves.size());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Data
|
|
|
+ public static class UploadSaveCustom {
|
|
|
+
|
|
|
+ private Long deviceId;
|
|
|
+ }
|
|
|
}
|