|
@@ -0,0 +1,216 @@
|
|
|
+package com.usky.iot.service.impl;
|
|
|
+
|
|
|
+
|
|
|
+import cn.afterturn.easypoi.excel.ExcelExportUtil;
|
|
|
+import cn.afterturn.easypoi.excel.ExcelImportUtil;
|
|
|
+import cn.afterturn.easypoi.excel.entity.ExportParams;
|
|
|
+import cn.afterturn.easypoi.excel.entity.ImportParams;
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.usky.common.core.bean.CommonPage;
|
|
|
+import com.usky.common.core.exception.BusinessException;
|
|
|
+import com.usky.common.core.util.BeanMapperUtils;
|
|
|
+import com.usky.common.core.util.UUIDUtils;
|
|
|
+import com.usky.common.mybatis.core.AbstractCrudService;
|
|
|
+import com.usky.iot.domain.DmpDeviceInfo;
|
|
|
+import com.usky.iot.mapper.DmpDeviceInfoMapper;
|
|
|
+import com.usky.iot.service.DmpDeviceInfoService;
|
|
|
+import com.usky.iot.service.vo.DmpDeviceExcelVO;
|
|
|
+import com.usky.iot.service.vo.DmpDeviceExportVO;
|
|
|
+import com.usky.iot.service.vo.DmpDeviceInfoRequest;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 设备信息表 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author ya
|
|
|
+ * @since 2022-10-08
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoMapper, DmpDeviceInfo> implements DmpDeviceInfoService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean add(DmpDeviceInfo dmpDeviceInfo) {
|
|
|
+ dmpDeviceInfo.setDeviceId(UUIDUtils.uuid());
|
|
|
+ if (checkNameUnique(dmpDeviceInfo)){
|
|
|
+ throw new BusinessException("新增设备信息'" + dmpDeviceInfo.getDeviceName() + "'失败,设备信息已存在");
|
|
|
+ }
|
|
|
+ return this.save(dmpDeviceInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void update(DmpDeviceInfo dmpDeviceInfo) {
|
|
|
+ dmpDeviceInfo.setUpdatedTime(new Date());
|
|
|
+ if (checkNameUnique(dmpDeviceInfo)){
|
|
|
+ throw new BusinessException("修改设备信息'" + dmpDeviceInfo.getDeviceName() + "'失败,设备信息已存在");
|
|
|
+ }
|
|
|
+ this.updateById(dmpDeviceInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean checkNameUnique(DmpDeviceInfo dmpDeviceInfo) {
|
|
|
+ Integer id = null == dmpDeviceInfo.getId() ? -1 : dmpDeviceInfo.getId();
|
|
|
+ LambdaQueryWrapper<DmpDeviceInfo> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper
|
|
|
+ .eq(DmpDeviceInfo::getDeviceName,dmpDeviceInfo.getDeviceName())
|
|
|
+ .eq(DmpDeviceInfo::getProductId,dmpDeviceInfo.getProductId())
|
|
|
+ .eq(DmpDeviceInfo::getDeleteFlag,0);
|
|
|
+ DmpDeviceInfo one = this.getOne(queryWrapper);
|
|
|
+ return null != one && !Objects.equals(one.getId(), id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CommonPage<DmpDeviceInfo> page(DmpDeviceInfoRequest diRequest) {
|
|
|
+ IPage<DmpDeviceInfo> page = new Page<>(diRequest.getCurrent(), diRequest.getSize());
|
|
|
+ page = baseMapper.page(page,diRequest);
|
|
|
+ return new CommonPage<>(page.getRecords(),page.getTotal(),page.getCurrent(),page.getSize());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean remove(Integer id) {
|
|
|
+ DmpDeviceInfo dmpDeviceInfo = this.getById(id);
|
|
|
+ Optional.ofNullable(dmpDeviceInfo).orElseThrow(() -> new BusinessException("设备不存在"));
|
|
|
+ dmpDeviceInfo.setDeleteFlag(1);
|
|
|
+ return this.updateById(dmpDeviceInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Map<String, Object>> deviceCollect(List<Integer> productIds) {
|
|
|
+ QueryWrapper<DmpDeviceInfo> query = Wrappers.query();
|
|
|
+ query.select("product_id as productId","count(*) as count")
|
|
|
+ .in(CollectionUtil.isNotEmpty(productIds),"product_id",productIds)
|
|
|
+ .groupBy("product_id");
|
|
|
+ return this.listMaps(query);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int deviceCountByProductId(Integer productId) {
|
|
|
+ LambdaQueryWrapper<DmpDeviceInfo> lambdaQuery = Wrappers.lambdaQuery();
|
|
|
+ lambdaQuery.eq(DmpDeviceInfo::getProductId,productId)
|
|
|
+ .eq(DmpDeviceInfo::getDeleteFlag,0);
|
|
|
+ return this.count(lambdaQuery);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addByFile(Integer productId, MultipartFile multipartFiles) {
|
|
|
+ List<DmpDeviceInfo> deviceByFile = getDeviceByFile(productId, multipartFiles);
|
|
|
+ this.saveBatch(deviceByFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void export(DmpDeviceInfoRequest dmpDeviceInfoRequest, HttpServletResponse response) {
|
|
|
+ Workbook workbook = null;
|
|
|
+ try {
|
|
|
+ ExportParams params = new ExportParams(null, "设备列表");
|
|
|
+ workbook = ExcelExportUtil.exportBigExcel(params, DmpDeviceExcelVO.class,
|
|
|
+ (o, i) -> {
|
|
|
+ dmpDeviceInfoRequest.setCurrent(i);
|
|
|
+ dmpDeviceInfoRequest.setSize(30);
|
|
|
+ CommonPage<DmpDeviceInfo> list = this.page(dmpDeviceInfoRequest);
|
|
|
+ return new ArrayList<>(BeanMapperUtils.mapList(list.getRecords(),DmpDeviceInfo.class,DmpDeviceExcelVO.class));
|
|
|
+ },null);
|
|
|
+ if (null != workbook) {
|
|
|
+ exportExcel(workbook,response);
|
|
|
+ } else {
|
|
|
+ throw new BusinessException("表格数据为空");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("导出文件失败", e);
|
|
|
+ throw new BusinessException("导出文件失败"+e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (workbook != null) {
|
|
|
+ try {
|
|
|
+ workbook.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("===export spec=== 关闭workbook失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void exportExcel(Workbook workbook, HttpServletResponse response){
|
|
|
+ //定义一个流,用作流式输出
|
|
|
+ ServletOutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ //使用流的形式传输
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+ //防止中文乱码
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ workbook.write(outputStream);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if(null!=outputStream){
|
|
|
+ try {
|
|
|
+ //关闭流
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取表格数据
|
|
|
+ * @param multipartFile
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<DmpDeviceInfo> getDeviceByFile(Integer productId,MultipartFile multipartFile){
|
|
|
+ ImportParams importParams = new ImportParams();
|
|
|
+ importParams.setHeadRows(1);
|
|
|
+ try {
|
|
|
+ List<DmpDeviceExportVO> dmpDeviceExportVOS = ExcelImportUtil.importExcel(
|
|
|
+ multipartFile.getInputStream(),
|
|
|
+ DmpDeviceExportVO.class,
|
|
|
+ importParams
|
|
|
+ );
|
|
|
+ if (CollectionUtils.isEmpty(dmpDeviceExportVOS)){
|
|
|
+ throw new BusinessException("表格数据为空");
|
|
|
+ }
|
|
|
+ List<DmpDeviceInfo> deviceInfos = BeanMapperUtils.mapList(dmpDeviceExportVOS, DmpDeviceExportVO.class, DmpDeviceInfo.class);
|
|
|
+ long count = deviceInfos.stream()
|
|
|
+ .peek(device -> {device.setProductId(productId);
|
|
|
+ device.setDeviceId(UUIDUtils.uuid());})
|
|
|
+ .filter(device -> StringUtils.isBlank(device.getDeviceName()) ||
|
|
|
+ StringUtils.isBlank(device.getDeviceName())
|
|
|
+ || StringUtils.isBlank(device.getDeviceCode())
|
|
|
+ || StringUtils.isBlank(device.getSimCode())
|
|
|
+ || null == device.getSubscribeFlag()
|
|
|
+ ).count();
|
|
|
+ long count1 = deviceInfos.stream()
|
|
|
+ .collect(Collectors.toMap(DmpDeviceInfo::getDeviceName, s -> 1, Integer::sum))
|
|
|
+ .entrySet()
|
|
|
+ .stream()
|
|
|
+ .filter(entry -> entry.getValue() > 1)
|
|
|
+ .count();
|
|
|
+ if (count > 0){
|
|
|
+ throw new BusinessException("表格字段不能为空");
|
|
|
+ }
|
|
|
+ if (count1 > 0){
|
|
|
+ throw new BusinessException("设备名称不能重复");
|
|
|
+ }
|
|
|
+ return deviceInfos;
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new BusinessException("解析表格异常,检查表格格式"+e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|