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.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.text.SimpleDateFormat;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* 运维设备信息表Service实现
|
* @author 江蹄蹄
|
* @date 2024/11/19 16:07
|
*/
|
@Service
|
public class YwDeviceServiceImpl implements YwDeviceService {
|
|
@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, 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
|
public void delete(YwDevice ywDevice) {
|
UpdateWrapper<YwDevice> deleteWrapper = new UpdateWrapper<>(ywDevice);
|
ywDeviceMapper.delete(deleteWrapper);
|
}
|
|
@Override
|
public void deleteByIdInBatch(List<Integer> ids, LoginUserInfo user) {
|
if (CollectionUtils.isEmpty(ids)) {
|
return;
|
}
|
ywDeviceMapper.deleteBatchIds(ids);
|
}
|
|
@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
|
public void updateByIdInBatch(List<YwDevice> ywDevices) {
|
if (CollectionUtils.isEmpty(ywDevices)) {
|
return;
|
}
|
for (YwDevice ywDevice: ywDevices) {
|
this.updateById(ywDevice);
|
}
|
}
|
|
@Override
|
public YwDevice findById(Integer id) {
|
return ywDeviceMapper.selectById(id);
|
}
|
|
@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);
|
}
|
|
@Override
|
public List<YwDevice> findList(YwDevice ywDevice) {
|
QueryWrapper<YwDevice> wrapper = new QueryWrapper<>(ywDevice);
|
return ywDeviceMapper.selectList(wrapper);
|
}
|
|
@Override
|
public PageData<YwDevice> findPage(PageWrap<YwDevice> pageWrap) {
|
IPage<YwDevice> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
|
MPJLambdaWrapper<YwDevice> queryWrapper = new MPJLambdaWrapper<YwDevice>();
|
Utils.MP.blankToNull(pageWrap.getModel());
|
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
|
public long count(YwDevice ywDevice) {
|
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(), "对不起,导入文件解析失败,请检查表格格式!");
|
}
|
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 + "】格式不正确,请使用一级/二级格式!");
|
}
|
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() + "】格式不正确,请使用楼宇/楼层/房源格式!");
|
}
|
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 ("正常".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 + "】不正确,仅支持正常/损坏/报废!");
|
}
|
|
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 + "】格式不正确,请使用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<>();
|
}
|
|
}
|