From e46bfa3ff94a8a1b4daf37c7fcb79c2fab22a72c Mon Sep 17 00:00:00 2001
From: doum <doum>
Date: 星期五, 29 五月 2026 17:10:00 +0800
Subject: [PATCH] 新增智能电表、空调管理
---
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/YwDeviceServiceImpl.java | 747 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 679 insertions(+), 68 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 77e981f..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
@@ -1,20 +1,48 @@
package com.doumee.service.business.impl;
+import com.doumee.biz.system.SystemDictDataBiz;
+import com.doumee.core.constants.ResponseStatus;
+import com.doumee.core.exception.BusinessException;
+import com.doumee.core.model.LoginUserInfo;
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.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;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.github.yulichang.wrapper.MPJLambdaWrapper;
+import lombok.extern.java.Log;
+import org.apache.commons.lang3.StringUtils;
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.List;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.stream.Collectors;
/**
* 杩愮淮璁惧淇℃伅琛⊿ervice瀹炵幇
@@ -27,15 +55,103 @@
@Autowired
private YwDeviceMapper ywDeviceMapper;
+ @Autowired
+ 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) {
+ if(Objects.isNull(ywDevice)
+ || 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);
+ }
+ LoginUserInfo loginUserInfo = ywDevice.getLoginUserInfo();
+ if(ywDeviceMapper.selectCount(new QueryWrapper<YwDevice>().lambda().eq(YwDevice::getIsdeleted,Constants.ZERO).eq(YwDevice::getCode,ywDevice.getCode()))>Constants.ZERO){
+ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"璁惧缂栧彿閲嶅!");
+ }
+ ywDevice.setCreateDate(new Date());
+ ywDevice.setCreator(loginUserInfo.getId());
+ ywDevice.setIsdeleted(Constants.ZERO);
ywDeviceMapper.insert(ywDevice);
+
+ if(Objects.nonNull(ywDevice.getFileUrl())){
+ Multifile multifile = new Multifile();
+ multifile.setCreator(loginUserInfo.getId());
+ multifile.setCreateDate(new Date());
+ multifile.setIsdeleted(Constants.ZERO);
+ multifile.setObjType(Constants.MultiFile.FN_DEVICE_FILE.getKey());
+ 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.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) {
- ywDeviceMapper.deleteById(id);
+ public void deleteById(Integer id, LoginUserInfo user) {
+ //鏌ヨ璁惧鏄惁瀛樺湪杩愮淮璁板綍
+ 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,id)
+ );
}
@Override
@@ -45,7 +161,7 @@
}
@Override
- public void deleteByIdInBatch(List<Integer> ids) {
+ public void deleteByIdInBatch(List<Integer> ids, LoginUserInfo user) {
if (CollectionUtils.isEmpty(ids)) {
return;
}
@@ -54,7 +170,64 @@
@Override
public void updateById(YwDevice ywDevice) {
+ if(Objects.isNull(ywDevice)
+ || 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);
+ }
+ LoginUserInfo loginUserInfo = ywDevice.getLoginUserInfo();
+ if(ywDeviceMapper.selectCount(new QueryWrapper<YwDevice>().lambda().eq(YwDevice::getIsdeleted,Constants.ZERO)
+ .eq(YwDevice::getCode,ywDevice.getCode())
+ .ne(YwDevice::getId,ywDevice.getId()))>Constants.ZERO){
+ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"璁惧缂栧彿閲嶅!");
+ }
+ ywDevice.setEditDate(new Date());
+ ywDevice.setEditor(loginUserInfo.getId());
ywDeviceMapper.updateById(ywDevice);
+ multifileMapper.delete(new QueryWrapper<Multifile>().lambda()
+ .eq(Multifile::getObjId,ywDevice.getId())
+ .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();
+ multifile.setCreator(loginUserInfo.getId());
+ multifile.setCreateDate(new Date());
+ multifile.setIsdeleted(Constants.ZERO);
+ multifile.setObjType(Constants.MultiFile.FN_DEVICE_FILE.getKey());
+ 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);
+ }
+
+
}
@Override
@@ -73,6 +246,85 @@
}
@Override
+ public YwDevice getDetail(Integer id) {
+ 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
public YwDevice findOne(YwDevice ywDevice) {
QueryWrapper<YwDevice> wrapper = new QueryWrapper<>(ywDevice);
return ywDeviceMapper.selectOne(wrapper);
@@ -87,70 +339,29 @@
@Override
public PageData<YwDevice> findPage(PageWrap<YwDevice> pageWrap) {
IPage<YwDevice> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
- QueryWrapper<YwDevice> queryWrapper = new QueryWrapper<>();
+ MPJLambdaWrapper<YwDevice> queryWrapper = new MPJLambdaWrapper<YwDevice>();
Utils.MP.blankToNull(pageWrap.getModel());
- if (pageWrap.getModel().getId() != null) {
- queryWrapper.lambda().eq(YwDevice::getId, pageWrap.getModel().getId());
- }
- if (pageWrap.getModel().getCreator() != null) {
- queryWrapper.lambda().eq(YwDevice::getCreator, pageWrap.getModel().getCreator());
- }
- if (pageWrap.getModel().getCreateDate() != null) {
- queryWrapper.lambda().ge(YwDevice::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()));
- queryWrapper.lambda().le(YwDevice::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()));
- }
- if (pageWrap.getModel().getEditor() != null) {
- queryWrapper.lambda().eq(YwDevice::getEditor, pageWrap.getModel().getEditor());
- }
- if (pageWrap.getModel().getEditDate() != null) {
- queryWrapper.lambda().ge(YwDevice::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()));
- queryWrapper.lambda().le(YwDevice::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()));
- }
- if (pageWrap.getModel().getIsdeleted() != null) {
- queryWrapper.lambda().eq(YwDevice::getIsdeleted, pageWrap.getModel().getIsdeleted());
- }
- if (pageWrap.getModel().getName() != null) {
- queryWrapper.lambda().eq(YwDevice::getName, pageWrap.getModel().getName());
- }
- if (pageWrap.getModel().getRemark() != null) {
- queryWrapper.lambda().eq(YwDevice::getRemark, pageWrap.getModel().getRemark());
- }
- if (pageWrap.getModel().getStatus() != null) {
- queryWrapper.lambda().eq(YwDevice::getStatus, pageWrap.getModel().getStatus());
- }
- if (pageWrap.getModel().getSortnum() != null) {
- queryWrapper.lambda().eq(YwDevice::getSortnum, pageWrap.getModel().getSortnum());
- }
- if (pageWrap.getModel().getModelNo() != null) {
- queryWrapper.lambda().eq(YwDevice::getModelNo, pageWrap.getModel().getModelNo());
- }
- if (pageWrap.getModel().getCateId() != null) {
- queryWrapper.lambda().eq(YwDevice::getCateId, pageWrap.getModel().getCateId());
- }
- if (pageWrap.getModel().getUserId() != null) {
- queryWrapper.lambda().eq(YwDevice::getUserId, pageWrap.getModel().getUserId());
- }
- if (pageWrap.getModel().getCompany() != null) {
- queryWrapper.lambda().ge(YwDevice::getCompany, Utils.Date.getStart(pageWrap.getModel().getCompany()));
- queryWrapper.lambda().le(YwDevice::getCompany, Utils.Date.getEnd(pageWrap.getModel().getCompany()));
- }
- if (pageWrap.getModel().getContent() != null) {
- queryWrapper.lambda().eq(YwDevice::getContent, pageWrap.getModel().getContent());
- }
- if (pageWrap.getModel().getCode() != null) {
- queryWrapper.lambda().eq(YwDevice::getCode, pageWrap.getModel().getCode());
- }
- if (pageWrap.getModel().getAddr() != null) {
- queryWrapper.lambda().eq(YwDevice::getAddr, pageWrap.getModel().getAddr());
- }
- for(PageWrap.SortData sortData: pageWrap.getSorts()) {
- if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
- queryWrapper.orderByDesc(sortData.getProperty());
- } else {
- queryWrapper.orderByAsc(sortData.getProperty());
- }
- }
- return PageData.from(ywDeviceMapper.selectPage(page, queryWrapper));
+ YwDevice model = pageWrap.getModel();
+ queryWrapper.selectAll(YwDevice.class)
+ .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)
+ ;
+ IPage iPage = ywDeviceMapper.selectJoinPage(page,YwDevice.class,queryWrapper);
+ return PageData.from(iPage);
}
@Override
@@ -158,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