From 7ec3683c8e41460f4bb0bd3a6677198742313e2b Mon Sep 17 00:00:00 2001
From: doum <doum>
Date: 星期二, 02 六月 2026 14:34:57 +0800
Subject: [PATCH] 新增智能电表、空调管理

---
 server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwDeviceServiceImpl.java |  592 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 577 insertions(+), 15 deletions(-)

diff --git a/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwDeviceServiceImpl.java b/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwDeviceServiceImpl.java
index 11080ec..4754f5a 100644
--- a/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwDeviceServiceImpl.java
+++ b/server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwDeviceServiceImpl.java
@@ -7,13 +7,24 @@
 import com.doumee.core.model.PageData;
 import com.doumee.core.model.PageWrap;
 import com.doumee.core.utils.Constants;
+import com.doumee.core.utils.DateUtil;
 import com.doumee.core.utils.Utils;
+import com.doumee.core.annotation.excel.ExcelImporter;
+import com.doumee.dao.admin.request.DeviceImport;
+import com.doumee.dao.business.*;
 import com.doumee.dao.business.YwDeviceMapper;
-import com.doumee.dao.business.model.Category;
-import com.doumee.dao.business.model.YwDevice;
+import com.doumee.dao.business.YwDeviceRecordMapper;
+import com.doumee.dao.business.join.MemberJoinMapper;
+import com.doumee.dao.business.model.*;
+import com.doumee.dao.business.vo.YwDeviceCateDataVO;
+import com.doumee.dao.business.vo.YwDeviceDataVO;
+import com.doumee.dao.business.vo.YwDeviceParentCateDataVO;
+import com.doumee.dao.business.vo.YwDeviceStatusDataVO;
 import com.doumee.dao.system.MultifileMapper;
+import com.doumee.dao.system.SystemUserMapper;
 import com.doumee.dao.system.model.Multifile;
 import com.doumee.dao.system.model.SystemUser;
+import com.doumee.service.business.YwDeviceRecordService;
 import com.doumee.service.business.YwDeviceService;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -25,11 +36,13 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
+import org.springframework.web.multipart.MultipartFile;
 
-import java.util.Date;
-import java.util.List;
-import java.util.Objects;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * 杩愮淮璁惧淇℃伅琛⊿ervice瀹炵幇
@@ -46,10 +59,28 @@
     private SystemDictDataBiz systemDictDataBiz;
     @Autowired
     private MultifileMapper multifileMapper;
+    @Autowired
+    private YwDeviceRecordMapper ywDeviceRecordMapper;
 
     @Autowired
     private RedisTemplate<String,Object> redisTemplate;
 
+    @Autowired
+    private CategoryMapper categoryMapper;
+    @Autowired
+    private YwProjectMapper ywProjectMapper;
+    @Autowired
+    private YwBuildingMapper ywBuildingMapper;
+    @Autowired
+    private YwFloorMapper ywFloorMapper;
+    @Autowired
+    private YwRoomMapper ywRoomMapper;
+    @Autowired
+    private MemberJoinMapper memberJoinMapper;
+    @Autowired
+    private SystemUserMapper systemUserMapper;
+
+    private static final int IMPORT_EXCEL_ROW_OFFSET = 3;
 
     @Override
     public Integer create(YwDevice ywDevice) {
@@ -57,6 +88,10 @@
             || StringUtils.isBlank(ywDevice.getName())
             || StringUtils.isBlank(ywDevice.getCode())
             || Objects.isNull(ywDevice.getStatus())
+            || Objects.isNull(ywDevice.getProjectId())
+            || Objects.isNull(ywDevice.getFloorId())
+            || Objects.isNull(ywDevice.getBuildingId())
+            || Objects.isNull(ywDevice.getRoomId())
         ){
             throw new BusinessException(ResponseStatus.BAD_REQUEST);
         }
@@ -80,15 +115,42 @@
             multifileMapper.insert(multifile);
         }
 
+        List<Multifile> fileList = new ArrayList<>();
+        if(ywDevice.getMultifileList()!=null && ywDevice.getMultifileList().size()>0){
+            for (int i = 0; i <  ywDevice.getMultifileList().size(); i++) {
+                Multifile multifile =  ywDevice.getMultifileList().get(i);
+                if(StringUtils.isBlank(multifile.getFileurl())){
+                    continue;
+                }
+                multifile.setCreateDate(ywDevice.getCreateDate());
+                multifile.setEditDate(ywDevice.getCreateDate());
+                multifile.setCreator(ywDevice.getCreator());
+                multifile.setIsdeleted(Constants.ZERO);
+                multifile.setObjId(ywDevice.getId());
+                multifile.setEditor(ywDevice.getCreator());
+                multifile.setObjType(Constants.MultiFile.FN_DEVICE_MAINTENANCE_FILE.getKey());
+                multifile.setSortnum(i+1);
+                fileList.add(multifile);
+            }
+        }
+        if(fileList.size()>0){
+            multifileMapper.insert(fileList);
+        }
+
         return ywDevice.getId();
     }
 
     @Override
     public void deleteById(Integer id, LoginUserInfo user) {
-        ywDeviceMapper.update(new UpdateWrapper<YwDevice>().lambda().set(YwDevice::getIsdeleted,Constants.ONE)
-                .set(YwDevice::getEditDate," now() ")
+        //鏌ヨ璁惧鏄惁瀛樺湪杩愮淮璁板綍
+        if(ywDeviceRecordMapper.selectCount(new QueryWrapper<YwDeviceRecord>().lambda().eq(YwDeviceRecord::getDeviceId,id).eq(YwDeviceRecord::getIsdeleted,Constants.ZERO))>Constants.ZERO){
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"褰撳墠璁惧瀛樺湪杩愮淮璁板綍锛屾棤娉曞垹闄�");
+        };
+        ywDeviceMapper.update(new UpdateWrapper<YwDevice>()
+                .lambda().set(YwDevice::getIsdeleted,Constants.ONE)
+                .set(YwDevice::getEditDate, DateUtil.getCurrDateTime())
                 .set(YwDevice::getEditor,user.getId())
-                .eq(YwDevice::getId,user.getId())
+                .eq(YwDevice::getId,id)
         );
     }
 
@@ -112,6 +174,10 @@
                 || StringUtils.isBlank(ywDevice.getName())
                 || StringUtils.isBlank(ywDevice.getCode())
                 || Objects.isNull(ywDevice.getStatus())
+                || Objects.isNull(ywDevice.getProjectId())
+                || Objects.isNull(ywDevice.getFloorId())
+                || Objects.isNull(ywDevice.getBuildingId())
+                || Objects.isNull(ywDevice.getRoomId())
         ){
             throw new BusinessException(ResponseStatus.BAD_REQUEST);
         }
@@ -126,7 +192,7 @@
         ywDeviceMapper.updateById(ywDevice);
         multifileMapper.delete(new QueryWrapper<Multifile>().lambda()
                 .eq(Multifile::getObjId,ywDevice.getId())
-                .eq(Multifile::getObjType,Constants.MultiFile.FN_DEVICE_FILE.getKey())
+                .in(Multifile::getObjType,Constants.MultiFile.FN_DEVICE_FILE.getKey(),Constants.MultiFile.FN_DEVICE_MAINTENANCE_FILE.getKey())
         );
         if(Objects.nonNull(ywDevice.getFileUrl())){
             Multifile multifile = new Multifile();
@@ -137,6 +203,28 @@
             multifile.setObjId(ywDevice.getId());
             multifile.setFileurl(ywDevice.getFileUrl());
             multifileMapper.insert(multifile);
+        }
+
+        List<Multifile> fileList = new ArrayList<>();
+        if(ywDevice.getMultifileList()!=null && ywDevice.getMultifileList().size()>0){
+            for (int i = 0; i <  ywDevice.getMultifileList().size(); i++) {
+                Multifile multifile =  ywDevice.getMultifileList().get(i);
+                if(StringUtils.isBlank(multifile.getFileurl())){
+                    continue;
+                }
+                multifile.setCreateDate(ywDevice.getCreateDate());
+                multifile.setEditDate(ywDevice.getCreateDate());
+                multifile.setCreator(ywDevice.getEditor());
+                multifile.setIsdeleted(Constants.ZERO);
+                multifile.setObjId(ywDevice.getId());
+                multifile.setEditor(ywDevice.getCreator());
+                multifile.setObjType(Constants.MultiFile.FN_DEVICE_MAINTENANCE_FILE.getKey());
+                multifile.setSortnum(i+1);
+                fileList.add(multifile);
+            }
+        }
+        if(fileList.size()>0){
+            multifileMapper.insert(fileList);
         }
 
 
@@ -159,15 +247,81 @@
 
     @Override
     public YwDevice getDetail(Integer id) {
-        YwDevice ywDevice = ywDeviceMapper.selectById(id);
-        Multifile multifile = multifileMapper.selectOne(new QueryWrapper<Multifile>().lambda().eq(Multifile::getObjId,id).eq(Multifile::getObjType,Constants.MultiFile.FN_DEVICE_FILE).last(" limit 1"));
+        YwDevice ywDevice = ywDeviceMapper.selectJoinOne(YwDevice.class,
+                new MPJLambdaWrapper<YwDevice>()
+                .selectAll(YwDevice.class)
+                        .select("c.name",YwDevice::getCategoryName)
+                        .select("c1.name",YwDevice::getCategoryParentName)
+                        .select("s.realname",YwDevice::getRealName)
+                        .select("s1.realname",YwDevice::getMaintenanceUserName)
+                        .selectAs(YwProject::getName,YwDevice::getProjectName)
+                        .selectAs(YwFloor::getName,YwDevice::getFloorName)
+                        .selectAs(YwBuilding::getName,YwDevice::getBuildingName)
+                        .selectAs(YwRoom::getCode,YwDevice::getRoomName)
+                        .leftJoin(YwRoom.class,YwRoom::getId,YwDevice::getRoomId)
+                        .leftJoin(YwFloor.class,YwFloor::getId,YwRoom::getFloor)
+                        .leftJoin(YwProject.class,YwProject::getId,YwRoom::getProjectId)
+                        .leftJoin(YwBuilding.class,YwBuilding::getId,YwRoom::getBuildingId)
+                        .leftJoin("category c on t.CATE_ID = c.id")
+                        .leftJoin("category c1 on c.PARENT_ID = c1.id")
+                        .leftJoin("system_user s on t.user_id = s.id")
+                        .leftJoin("system_user s1 on t.MAINTENANCE_USER_ID = s1.id")
+                        .eq(YwDevice::getId,id)
+                        .last(" limit 1 ")
+        );
+        Multifile multifile = multifileMapper.selectOne(new QueryWrapper<Multifile>().lambda()
+                .eq(Multifile::getObjId,id).eq(Multifile::getObjType,Constants.MultiFile.FN_DEVICE_FILE.getKey()).last(" limit 1"));
         if(Objects.nonNull(multifile)){
             String path = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode()
                     +systemDictDataBiz.queryByCode(Constants.FTP,Constants.YW_DEVICE).getCode();
             ywDevice.setFileFullUrl(path + multifile.getFileurl());
         }
+
+        //闄勪欢鏁版嵁
+        List<Multifile> multifileList = multifileMapper.selectJoinList(Multifile.class,new MPJLambdaWrapper<Multifile>()
+                .selectAll(Multifile.class)
+                .selectAs(SystemUser::getRealname,Multifile::getUserName)
+                .leftJoin(SystemUser.class,SystemUser::getId,Multifile::getCreator)
+                .eq(Multifile::getObjId,ywDevice.getId())
+                .eq(Multifile::getIsdeleted,Constants.ZERO)
+                .eq(Multifile::getObjType,Constants.MultiFile.FN_DEVICE_MAINTENANCE_FILE.getKey()));
+        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(multifileList)){
+            String path = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode()
+                    +systemDictDataBiz.queryByCode(Constants.FTP,Constants.YW_PATROL).getCode();
+            for (Multifile m:multifileList) {
+                if(StringUtils.isNotBlank(m.getFileurl())){
+                    m.setFileurlFull(path + m.getFileurl());
+                }
+            }
+            ywDevice.setMultifileList(multifileList);
+        }
+
         return ywDevice;
     }
+
+
+    /**
+     * 鏍规嵁缂栫爜鏌ヨ
+     * @param deviceCode
+     * @return
+     */
+    @Override
+    public YwDevice findByCode(String deviceCode) {
+        YwDevice ywDevice =  ywDeviceMapper.selectOne(
+                new QueryWrapper<YwDevice>()
+                        .lambda()
+                        .eq(YwDevice::getIsdeleted,Constants.ZERO)
+                        .eq(YwDevice::getCode,deviceCode)
+                        .last(" limit  1 ")
+        );
+        if(Objects.isNull(ywDevice)){
+            throw new BusinessException(ResponseStatus.DATA_EMPTY);
+        }
+
+        return ywDevice;
+    }
+
+
 
 
     @Override
@@ -189,12 +343,20 @@
         Utils.MP.blankToNull(pageWrap.getModel());
         YwDevice model = pageWrap.getModel();
         queryWrapper.selectAll(YwDevice.class)
-                .selectAs(Category::getName,YwDevice::getCategoryName)
-                .selectAs(SystemUser::getRealname,YwDevice::getRealName)
-                .leftJoin(Category.class,Category::getId,YwDevice::getCateId)
-                .leftJoin(SystemUser.class,SystemUser::getId,YwDevice::getUserId)
+                .select("c.name",YwDevice::getCategoryName)
+                .select("c1.name",YwDevice::getCategoryParentName)
+                .select("s.realname",YwDevice::getRealName)
+                .select("s1.realname",YwDevice::getMaintenanceUserName)
+                .leftJoin("category c on t.CATE_ID = c.id")
+                .leftJoin("category c1 on c.PARENT_ID = c1.id")
+                .leftJoin("system_user s on t.user_id = s.id")
+                .leftJoin("system_user s1 on t.MAINTENANCE_USER_ID = s1.id")
                 .and(Objects.nonNull(model)&&StringUtils.isNotBlank(model.getName()),i->i.like(YwDevice::getName,model.getName()).or().like(YwDevice::getCode,model.getName()))
                 .eq(Objects.nonNull(model.getStatus()),YwDevice::getStatus,model.getStatus())
+                .eq(Objects.nonNull(model.getRoomId()),YwDevice::getRoomId,model.getRoomId())
+                .eq(Objects.nonNull(model.getFloorId()),YwDevice::getFloorId,model.getFloorId())
+                .eq(Objects.nonNull(model.getProjectId()),YwDevice::getProjectId,model.getProjectId())
+                .eq(Objects.nonNull(model.getBuildingId()),YwDevice::getBuildingId,model.getBuildingId())
                 .eq(YwDevice::getIsdeleted,Constants.ZERO)
                 .orderByDesc(YwDevice::getCreateDate)
         ;
@@ -207,4 +369,404 @@
         QueryWrapper<YwDevice> wrapper = new QueryWrapper<>(ywDevice);
         return ywDeviceMapper.selectCount(wrapper);
     }
+
+
+
+    @Override
+    public YwDeviceDataVO getYwDeviceData(){
+        YwDeviceDataVO ywDeviceDataVO = new YwDeviceDataVO();
+        List<YwDevice> ywDeviceList = ywDeviceMapper.selectList(new QueryWrapper<YwDevice>().lambda().eq(YwDevice::getIsdeleted,Constants.ZERO));
+        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ywDeviceList)){
+            ywDeviceDataVO.setQualityNum(ywDeviceList.size());
+            ywDeviceDataVO.setTotalNum(ywDeviceList.size());
+            ywDeviceDataVO.setNormalNum(ywDeviceList.stream().filter(i->Constants.equalsInteger(i.getStatus(),Constants.ZERO)).collect(Collectors.toList()).size());
+            ywDeviceDataVO.setAbnormalNum(ywDeviceList.stream().filter(i->Constants.equalsInteger(i.getStatus(),Constants.ONE)).collect(Collectors.toList()).size());
+            ywDeviceDataVO.setScrapNum(ywDeviceList.stream().filter(i->Constants.equalsInteger(i.getStatus(),Constants.TWO)).collect(Collectors.toList()).size());
+        }
+        return ywDeviceDataVO;
+    }
+
+    @Override
+    public Set<YwDeviceParentCateDataVO> getDeviceCateData(YwDevice model){
+        MPJLambdaWrapper<YwDevice> queryWrapper = new MPJLambdaWrapper<YwDevice>();
+        queryWrapper.selectAll(YwDevice.class)
+                .select("c.name",YwDevice::getCategoryName)
+                .select("c1.name",YwDevice::getCategoryParentName)
+                .select("c1.id",YwDevice::getCateParentId)
+                .selectAs(SystemUser::getRealname,YwDevice::getRealName)
+                .leftJoin(SystemUser.class,SystemUser::getId,YwDevice::getUserId)
+                .leftJoin("category c on t.CATE_ID = c.id")
+                .leftJoin("category c1 on c.PARENT_ID = c1.id")
+                .eq(Objects.nonNull(model.getRoomId()),YwDevice::getRoomId,model.getRoomId())
+                .eq(Objects.nonNull(model.getFloorId()),YwDevice::getFloorId,model.getFloorId())
+                .eq(Objects.nonNull(model.getProjectId()),YwDevice::getProjectId,model.getProjectId())
+                .eq(Objects.nonNull(model.getBuildingId()),YwDevice::getBuildingId,model.getBuildingId())
+                .eq(YwDevice::getIsdeleted,Constants.ZERO)
+                .isNotNull(YwDevice::getCateId)
+                .orderByDesc(YwDevice::getCreateDate)
+        ;
+        List<YwDevice> ywDeviceList = ywDeviceMapper.selectJoinList(YwDevice.class,queryWrapper);
+        Set<YwDeviceCateDataVO> ywDeviceCateDataVOSet = new HashSet<>();
+        Set<YwDeviceParentCateDataVO> ywDeviceParentCateDataVOSet = new HashSet<>();
+        for (YwDevice y: ywDeviceList) {
+            YwDeviceCateDataVO ywDeviceCateDataVO = new YwDeviceCateDataVO();
+            ywDeviceCateDataVO.setCateId(y.getCateId());
+            ywDeviceCateDataVO.setCateName(y.getCategoryName());
+            ywDeviceCateDataVO.setCateParentId(y.getCateParentId());
+            ywDeviceCateDataVOSet.add(ywDeviceCateDataVO);
+
+            YwDeviceParentCateDataVO ywDeviceParentCateDataVO = new YwDeviceParentCateDataVO();
+            ywDeviceParentCateDataVO.setCateId(y.getCateParentId());
+            ywDeviceParentCateDataVO.setCateName(y.getCategoryParentName());
+            ywDeviceParentCateDataVOSet.add(ywDeviceParentCateDataVO);
+        }
+
+        for (YwDeviceCateDataVO ywDeviceCateDataVO:ywDeviceCateDataVOSet) {
+            ywDeviceCateDataVO.setDeviceAmount(
+                    ywDeviceList.stream().filter(i->Objects.nonNull(i.getCateId()) && Constants.equalsInteger(i.getCateId(),ywDeviceCateDataVO.getCateId())).collect(Collectors.toList()).size()
+            );
+        }
+
+        for (YwDeviceParentCateDataVO ywDeviceParentCateDataVO:ywDeviceParentCateDataVOSet) {
+            ywDeviceParentCateDataVO.setDeviceAmount(
+                    ywDeviceList.stream().filter(i->Objects.nonNull(i.getCateParentId()) && Constants.equalsInteger(i.getCateParentId(),ywDeviceParentCateDataVO.getCateId())).collect(Collectors.toList()).size()
+            );
+            ywDeviceParentCateDataVO.setYwDeviceCateDataVOList(
+                    ywDeviceCateDataVOSet.stream().filter(i->Constants.equalsInteger(i.getCateParentId(),ywDeviceParentCateDataVO.getCateId())).collect(Collectors.toList())
+            );
+        }
+        return ywDeviceParentCateDataVOSet;
+    }
+
+
+    @Override
+    public YwDeviceStatusDataVO getDeviceStatus(YwDevice model){
+        YwDeviceStatusDataVO ywDeviceStatusDataVO = new YwDeviceStatusDataVO();
+        MPJLambdaWrapper<YwDevice> queryWrapper = new MPJLambdaWrapper<YwDevice>();
+        queryWrapper.selectAll(YwDevice.class)
+                .eq(Objects.nonNull(model.getRoomId()),YwDevice::getRoomId,model.getRoomId())
+                .eq(Objects.nonNull(model.getFloorId()),YwDevice::getFloorId,model.getFloorId())
+                .eq(Objects.nonNull(model.getProjectId()),YwDevice::getProjectId,model.getProjectId())
+                .eq(Objects.nonNull(model.getBuildingId()),YwDevice::getBuildingId,model.getBuildingId())
+                .eq(YwDevice::getIsdeleted,Constants.ZERO)
+        ;
+        List<YwDevice> ywDeviceList = ywDeviceMapper.selectJoinList(YwDevice.class,queryWrapper);
+        ywDeviceStatusDataVO.setAmount(ywDeviceList.size());
+        ywDeviceStatusDataVO.setWorkAmount(ywDeviceList.stream().filter(i->Objects.nonNull(i.getStatus())&&Constants.equalsInteger(i.getStatus(),Constants.ZERO)).collect(Collectors.toList()).size());
+        ywDeviceStatusDataVO.setExceptionAmount(ywDeviceList.stream().filter(i->Objects.nonNull(i.getStatus())&&Constants.equalsInteger(i.getStatus(),Constants.ONE)).collect(Collectors.toList()).size());
+        ywDeviceStatusDataVO.setErrAmount(ywDeviceList.stream().filter(i->Objects.nonNull(i.getStatus())&&Constants.equalsInteger(i.getStatus(),Constants.TWO)).collect(Collectors.toList()).size());
+        return ywDeviceStatusDataVO;
+
+    }
+
+    @Override
+    @Transactional(rollbackFor = {BusinessException.class, Exception.class})
+    public String importBatch(MultipartFile file, LoginUserInfo loginUserInfo) {
+        List<DeviceImport> dataList;
+        try {
+            ExcelImporter ie = new ExcelImporter(file, 1, 0);
+            dataList = ie.getDataList(DeviceImport.class, null);
+        } catch (Exception e) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝瀵煎叆鏂囦欢瑙f瀽澶辫触锛岃妫�鏌ヨ〃鏍兼牸寮忥紒");
+        }
+        if (CollectionUtils.isEmpty(dataList)) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝褰曞叆鏁版嵁涓虹┖锛�");
+        }
+        DeviceImportCache cache = buildImportCache();
+        List<YwDevice> insertList = new ArrayList<>();
+        List<YwDevice> updateList = new ArrayList<>();
+        Set<String> fileCodes = new HashSet<>();
+        Date now = new Date();
+        for (int i = 0; i < dataList.size(); i++) {
+            DeviceImport row = dataList.get(i);
+            if (isImportBlankRow(row)) {
+                continue;
+            }
+            YwDevice device = checkImportRow(row, i, cache, fileCodes, loginUserInfo, now);
+            if (device.getId() != null) {
+                updateList.add(device);
+            } else {
+                insertList.add(device);
+            }
+        }
+        if (CollectionUtils.isEmpty(insertList) && CollectionUtils.isEmpty(updateList)) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝褰曞叆鏈夋晥鏁版嵁涓虹┖锛�");
+        }
+        if (!CollectionUtils.isEmpty(insertList)) {
+            ywDeviceMapper.insert(insertList);
+        }
+        for (YwDevice device : updateList) {
+            ywDeviceMapper.updateById(device);
+        }
+        return String.format("鎿嶄綔鎴愬姛锛屾湰娆″鍏� 鏂板%d涓澶囷紝鏇存柊%d涓澶�", insertList.size(), updateList.size());
+    }
+
+    private boolean isImportBlankRow(DeviceImport row) {
+        return StringUtils.isBlank(row.getCode())
+                && StringUtils.isBlank(row.getName())
+                && StringUtils.isBlank(row.getProjectName())
+                && StringUtils.isBlank(row.getRoomPath());
+    }
+
+    private DeviceImportCache buildImportCache() {
+        DeviceImportCache cache = new DeviceImportCache();
+        List<Category> categories = categoryMapper.selectList(new QueryWrapper<Category>().lambda()
+                .eq(Category::getIsdeleted, Constants.ZERO)
+                .eq(Category::getType, Constants.FIVE));
+        Map<Integer, Category> categoryById = categories.stream()
+                .collect(Collectors.toMap(Category::getId, c -> c, (a, b) -> a));
+        for (Category category : categories) {
+            if (category.getParentId() == null || category.getParentId() <= 0) {
+                continue;
+            }
+            Category parent = categoryById.get(category.getParentId());
+            if (parent == null || StringUtils.isBlank(parent.getName()) || StringUtils.isBlank(category.getName())) {
+                continue;
+            }
+            cache.categoryPathMap.put(parent.getName() + "/" + category.getName(), category.getId());
+        }
+        List<YwProject> projects = ywProjectMapper.selectList(new QueryWrapper<YwProject>().lambda()
+                .eq(YwProject::getIsdeleted, Constants.ZERO));
+        for (YwProject project : projects) {
+            if (StringUtils.isNotBlank(project.getName())) {
+                cache.projectNameMap.put(project.getName(), project.getId());
+            }
+        }
+        List<YwBuilding> buildings = ywBuildingMapper.selectList(new QueryWrapper<YwBuilding>().lambda()
+                .eq(YwBuilding::getIsdeleted, Constants.ZERO));
+        for (YwBuilding building : buildings) {
+            if (building.getProjectId() != null && StringUtils.isNotBlank(building.getName())) {
+                cache.buildingMap.put(building.getProjectId() + "|" + building.getName(), building.getId());
+            }
+        }
+        List<YwFloor> floors = ywFloorMapper.selectList(new QueryWrapper<YwFloor>().lambda()
+                .eq(YwFloor::getIsdeleted, Constants.ZERO));
+        for (YwFloor floor : floors) {
+            if (floor.getBuildingId() != null && StringUtils.isNotBlank(floor.getName())) {
+                cache.floorMap.put(floor.getBuildingId() + "|" + floor.getName(), floor.getId());
+            }
+        }
+        List<YwRoom> rooms = ywRoomMapper.selectList(new QueryWrapper<YwRoom>().lambda()
+                .eq(YwRoom::getIsdeleted, Constants.ZERO));
+        for (YwRoom room : rooms) {
+            if (room.getProjectId() == null || room.getBuildingId() == null || room.getFloor() == null) {
+                continue;
+            }
+            if (StringUtils.isNotBlank(room.getCode())) {
+                cache.roomMap.put(roomKey(room.getProjectId(), room.getBuildingId(), room.getFloor(), room.getCode()), room.getId());
+            }
+            if (StringUtils.isNotBlank(room.getRoomNum())) {
+                cache.roomMap.put(roomKey(room.getProjectId(), room.getBuildingId(), room.getFloor(), room.getRoomNum()), room.getId());
+            }
+        }
+        List<YwDevice> devices = ywDeviceMapper.selectList(new QueryWrapper<YwDevice>().lambda()
+                .eq(YwDevice::getIsdeleted, Constants.ZERO)
+                .select(YwDevice::getId, YwDevice::getCode));
+        for (YwDevice device : devices) {
+            if (StringUtils.isNotBlank(device.getCode())) {
+                cache.existingCodeIdMap.put(device.getCode(), device.getId());
+            }
+        }
+        List<Member> members = memberJoinMapper.selectJoinList(Member.class, new MPJLambdaWrapper<Member>()
+                .select(Member::getId, Member::getName)
+                .leftJoin(Company.class, Company::getId, Member::getCompanyId)
+                .eq(Member::getIsdeleted, Constants.ZERO)
+                .eq(Company::getType, Constants.ONE));
+        if (!CollectionUtils.isEmpty(members)) {
+            List<Integer> memberIds = members.stream().map(Member::getId).filter(Objects::nonNull).collect(Collectors.toList());
+            if (!CollectionUtils.isEmpty(memberIds)) {
+                List<SystemUser> systemUsers = systemUserMapper.selectList(new QueryWrapper<SystemUser>().lambda()
+                        .eq(SystemUser::getDeleted, Boolean.FALSE)
+                        .in(SystemUser::getMemberId, memberIds));
+                Map<Integer, Integer> memberUserMap = systemUsers.stream()
+                        .filter(u -> u.getMemberId() != null)
+                        .collect(Collectors.toMap(SystemUser::getMemberId, SystemUser::getId, (a, b) -> a));
+                for (Member member : members) {
+                    if (StringUtils.isBlank(member.getName())) {
+                        continue;
+                    }
+                    Integer userId = memberUserMap.get(member.getId());
+                    if (userId == null) {
+                        continue;
+                    }
+                    if (cache.internalUserNameMap.containsKey(member.getName())) {
+                        cache.duplicateInternalUserNames.add(member.getName());
+                    }
+                    cache.internalUserNameMap.put(member.getName(), userId);
+                }
+            }
+        }
+        return cache;
+    }
+
+    private String roomKey(Integer projectId, Integer buildingId, Integer floorId, String roomName) {
+        return projectId + "|" + buildingId + "|" + floorId + "|" + roomName;
+    }
+
+    private YwDevice checkImportRow(DeviceImport row, int index, DeviceImportCache cache, Set<String> fileCodes,
+                                    LoginUserInfo loginUserInfo, Date now) {
+        int rowNum = index + IMPORT_EXCEL_ROW_OFFSET;
+        if (StringUtils.isBlank(row.getCode())) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛岃澶囩紪鍙蜂笉鑳戒负绌猴紝璇锋鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+        if (StringUtils.isBlank(row.getName())) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛岃澶囧悕绉颁笉鑳戒负绌猴紝璇锋鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+        if (StringUtils.isBlank(row.getProjectName())) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛屾墍灞為」鐩笉鑳戒负绌猴紝璇锋鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+        if (StringUtils.isBlank(row.getRoomPath())) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛屽叧鑱旀埧婧愪笉鑳戒负绌猴紝璇锋鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+        String code = row.getCode().trim();
+        if (fileCodes.contains(code)) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛岃澶囩紪鍙枫��" + code + "銆戦噸澶嶏紝璇锋鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+        fileCodes.add(code);
+        Integer existingId = cache.existingCodeIdMap.get(code);
+
+        Integer cateId = null;
+        if (StringUtils.isNotBlank(row.getCategoryPath())) {
+            String categoryPath = row.getCategoryPath().trim();
+            String[] cateParts = categoryPath.split("/");
+            if (cateParts.length < 2 || StringUtils.isBlank(cateParts[1])) {
+                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛岃澶囧垎绫汇��" + categoryPath + "銆戞牸寮忎笉姝g‘锛岃浣跨敤涓�绾�/浜岀骇鏍煎紡锛�");
+            }
+            cateId = cache.categoryPathMap.get(categoryPath);
+            if (cateId == null) {
+                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛岃澶囧垎绫汇��" + categoryPath + "銆戞湭鎵惧埌锛岃妫�鏌ヨ〃鏍煎唴瀹癸紒");
+            }
+        }
+
+        Integer projectId = cache.projectNameMap.get(row.getProjectName().trim());
+        if (projectId == null) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛屾墍灞為」鐩��" + row.getProjectName() + "銆戞湭鎵惧埌锛岃妫�鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+
+        String[] roomParts = row.getRoomPath().trim().split("/");
+        if (roomParts.length < 3 || StringUtils.isAnyBlank(roomParts[0], roomParts[1], roomParts[2])) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛屽叧鑱旀埧婧愩��" + row.getRoomPath() + "銆戞牸寮忎笉姝g‘锛岃浣跨敤妤煎畤/妤煎眰/鎴挎簮鏍煎紡锛�");
+        }
+        Integer buildingId = cache.buildingMap.get(projectId + "|" + roomParts[0].trim());
+        if (buildingId == null) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛屽叧鑱旀埧婧愩��" + row.getRoomPath() + "銆戞湭鎵惧埌锛岃妫�鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+        Integer floorId = cache.floorMap.get(buildingId + "|" + roomParts[1].trim());
+        if (floorId == null) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛屽叧鑱旀埧婧愩��" + row.getRoomPath() + "銆戞湭鎵惧埌锛岃妫�鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+        Integer roomId = cache.roomMap.get(roomKey(projectId, buildingId, floorId, roomParts[2].trim()));
+        if (roomId == null) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛屽叧鑱旀埧婧愩��" + row.getRoomPath() + "銆戞湭鎵惧埌锛岃妫�鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+
+        Integer status = parseImportStatus(row.getStatusText(), rowNum);
+
+        Integer userId = resolveInternalUserId(row.getAdminUserName(), rowNum, "璁惧绠$悊鍛�", cache);
+        Integer maintenanceUserId = resolveInternalUserId(row.getMaintenanceUserName(), rowNum, "缁翠繚璐熻矗浜�", cache);
+
+        YwDevice device = new YwDevice();
+        device.setCode(code);
+        device.setName(row.getName().trim());
+        device.setCateId(cateId);
+        device.setModelNo(StringUtils.trimToNull(row.getModelNo()));
+        device.setUserId(userId);
+        device.setAddr(StringUtils.trimToNull(row.getAddr()));
+        device.setProjectId(projectId);
+        device.setBuildingId(buildingId);
+        device.setFloorId(floorId);
+        device.setRoomId(roomId);
+        device.setBuyDate(parseImportDate(row.getBuyDate(), rowNum, "璐叆鏃堕棿"));
+        device.setStatus(status);
+        device.setContent(StringUtils.trimToNull(row.getContent()));
+        device.setSupplier(StringUtils.trimToNull(row.getSupplier()));
+        device.setSupplierLinker(StringUtils.trimToNull(row.getSupplierLinker()));
+        device.setSupplierPhone(StringUtils.trimToNull(row.getSupplierPhone()));
+        device.setMaintenanceUserId(maintenanceUserId);
+        device.setMaintenanceOverDate(parseImportDate(row.getMaintenanceOverDate(), rowNum, "缁翠繚鍒版湡鏃�"));
+        device.setMaintenanceContent(StringUtils.trimToNull(row.getMaintenanceContent()));
+        if (existingId != null) {
+            device.setId(existingId);
+            device.setEditor(loginUserInfo.getId());
+            device.setEditDate(now);
+        } else {
+            device.setCreator(loginUserInfo.getId());
+            device.setCreateDate(now);
+            device.setIsdeleted(Constants.ZERO);
+        }
+        return device;
+    }
+
+    private Integer resolveInternalUserId(String userName, int rowNum, String fieldLabel, DeviceImportCache cache) {
+        if (StringUtils.isBlank(userName)) {
+            return null;
+        }
+        String name = userName.trim();
+        if (cache.duplicateInternalUserNames.contains(name)) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛�" + fieldLabel + "銆�" + name + "銆戝瓨鍦ㄩ噸鍚嶏紝璇锋鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+        Integer userId = cache.internalUserNameMap.get(name);
+        if (userId == null) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛�" + fieldLabel + "銆�" + name + "銆戞湭鎵惧埌锛岃妫�鏌ヨ〃鏍煎唴瀹癸紒");
+        }
+        return userId;
+    }
+
+    private Integer parseImportStatus(String statusText, int rowNum) {
+        if (StringUtils.isBlank(statusText)) {
+            return Constants.ZERO;
+        }
+        String text = statusText.trim();
+        if ("姝e父".equals(text)) {
+            return Constants.ZERO;
+        }
+        if ("鎹熷潖".equals(text)) {
+            return Constants.ONE;
+        }
+        if ("鎶ュ簾".equals(text)) {
+            return Constants.TWO;
+        }
+        throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝绗�" + rowNum + "琛岃澶囩姸鎬併��" + statusText + "銆戜笉姝g‘锛屼粎鏀寔姝e父/鎹熷潖/鎶ュ簾锛�");
+    }
+
+    private Date parseImportDate(String dateText, int rowNum, String fieldLabel) {
+        if (StringUtils.isBlank(dateText)) {
+            return null;
+        }
+        String text = dateText.trim();
+        if (StringUtils.endsWith(text, ".0")) {
+            text = StringUtils.substringBefore(text, ".0");
+        }
+        Date date = DateUtil.parseFromFormats(text);
+        if (date != null) {
+            return date;
+        }
+        try {
+            return new SimpleDateFormat("yyyy-MM-dd").parse(text);
+        } catch (Exception ignored) {
+            // try excel serial number below
+        }
+        try {
+            return org.apache.poi.ss.usermodel.DateUtil.getJavaDate(Double.parseDouble(text));
+        } catch (Exception ignored) {
+            // fall through to error
+        }
+        throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),
+                "瀵逛笉璧凤紝绗�" + rowNum + "琛�" + fieldLabel + "銆�" + dateText + "銆戞牸寮忎笉姝g‘锛岃浣跨敤yyyy-MM-dd鏍煎紡锛�");
+    }
+
+    private static class DeviceImportCache {
+        private final Map<String, Integer> categoryPathMap = new HashMap<>();
+        private final Map<String, Integer> projectNameMap = new HashMap<>();
+        private final Map<String, Integer> buildingMap = new HashMap<>();
+        private final Map<String, Integer> floorMap = new HashMap<>();
+        private final Map<String, Integer> roomMap = new HashMap<>();
+        private final Map<String, Integer> existingCodeIdMap = new HashMap<>();
+        private final Map<String, Integer> internalUserNameMap = new HashMap<>();
+        private final Set<String> duplicateInternalUserNames = new HashSet<>();
+    }
+
 }
+
+
+

--
Gitblit v1.9.3