package com.gkhy.exam.system.service.impl;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.api.CommonResult;
import com.gkhy.exam.common.api.ImportResult;
import com.gkhy.exam.common.domain.entity.SysDept;
import com.gkhy.exam.common.domain.entity.SysUser;
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.system.domain.DTO.StandingBookImportDTO;
import com.gkhy.exam.system.domain.StandingBook;
import com.gkhy.exam.system.domain.vo.DeptVo;
import com.gkhy.exam.system.mapper.StandingBookMapper;
import com.gkhy.exam.system.mapper.SysDeptMapper;
import com.gkhy.exam.system.mapper.SysUserMapper;
import com.gkhy.exam.system.service.StandingBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
* 台账 服务实现类
*
*
* @author hh
* @since 2025-07-31 16:12:02
*/
@Service
public class StandingBookServiceImpl extends ServiceImpl implements StandingBookService {
@Autowired
private StandingBookMapper standingBookMapper;
@Autowired
private SysDeptMapper sysDeptMapper;
@Autowired
private SysUserMapper sysUserMapper;
@Override
public CommonPage selectStandingBookList(StandingBook standingBook) {
PageUtils.startPage();
List standingBooks = standingBookMapper.getStandingBooks(standingBook);
return CommonPage.restPage(standingBooks);
}
@Override
public CommonResult insertStandingBook(StandingBook standingBook) {
standingBook.setCreateBy(SecurityUtils.getUsername());
standingBook.setCreateTime(LocalDateTime.now());
int insert = standingBookMapper.insert(standingBook);
if (insert > 0){
return CommonResult.success();
}
return CommonResult.failed();
}
@Override
public CommonResult updateStandingBook(StandingBook standingBook) {
standingBook.setUpdateBy(SecurityUtils.getUsername());
standingBook.setUpdateTime(LocalDateTime.now());
int insert = standingBookMapper.updateById(standingBook);
if (insert > 0){
return CommonResult.success();
}
return CommonResult.failed();
}
@Override
public CommonResult deletedStandingBook(Integer id) {
StandingBook standingBook = new StandingBook();
standingBook.setId(id.longValue());
standingBook.setDelFlag(1);
standingBook.setUpdateBy(SecurityUtils.getUsername());
standingBook.setUpdateTime(LocalDateTime.now());
int update = standingBookMapper.updateById(standingBook);
if (update > 0){
return CommonResult.success();
}
return CommonResult.failed();
}
@Override
@Transactional
public ImportResult importStandingBooks(Long companyId, MultipartFile file) {
ImportResult result = new ImportResult();
result.setSuccessCount(0);
result.setFailCount(0);
result.setErrorMessages(new ArrayList<>());
SysDept sysDept = new SysDept();
sysDept.setCompanyId(companyId);
List deptVos = sysDeptMapper.selectDeptList(sysDept);
Map deptNameIdMap = deptVos.stream()
.collect(Collectors.toMap(DeptVo::getDeptName, DeptVo::getDeptId));
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysUser::getCompanyId, companyId);
queryWrapper.eq(SysUser::getDelFlag, 0);
List sysUsers = sysUserMapper.selectList(queryWrapper);
Map userNameIdMap = sysUsers.stream()
.collect(Collectors.toMap(SysUser::getName, SysUser::getId));
try {
// 使用EasyExcel读取文件
ReadListener listener = new ReadListener() {
@Override
public void invoke(StandingBookImportDTO dto, AnalysisContext context) {
try {
// 转换为实体对象
StandingBook standingBook = new StandingBook();
standingBook.setCompanyId(companyId);
standingBook.setDeptId(deptNameIdMap.get(dto.getDeptName()) == null ? 0L : deptNameIdMap.get(dto.getDeptName()));
standingBook.setName(dto.getName());
standingBook.setModel(dto.getModel());
standingBook.setPersonResponsible(userNameIdMap.get(dto.getPersonResponsible()) == null ? 0L : userNameIdMap.get(dto.getPersonResponsible()));
String deviceType = dto.getDeviceType();
switch (deviceType) {
case "计算机设备":
standingBook.setDeviceType(1);
break;
case "办公自动化设备":
standingBook.setDeviceType(2);
break;
case "外部设备":
standingBook.setDeviceType(3);
break;
case "生产设备":
standingBook.setDeviceType(5);
break;
default:
standingBook.setDeviceType(4);
break;
}
standingBook.setNumber(dto.getNumber());
standingBook.setBrand(dto.getBrand());
standingBook.setConfidentiality(dto.getConfidentiality());
switch (dto.getStatus()) {
case "完好":
standingBook.setStatus(1);
break;
case "需整改":
standingBook.setStatus(2);
break;
default:
standingBook.setStatus(3);
break;
}
standingBook.setPurpose(dto.getPurpose());
standingBook.setLocation(dto.getLocation());
standingBook.setUsed(dto.getUsed());
standingBook.setRemark(dto.getRemark());
standingBook.setCreateTime(LocalDateTime.now());
standingBook.setCreateBy(SecurityUtils.getUsername());
standingBookMapper.insert(standingBook);
result.setSuccessCount(result.getSuccessCount() + 1);
} catch (Exception e) {
result.getErrorMessages().add("行" + context.readRowHolder().getRowIndex() + "导入失败:" + e.getMessage());
result.setFailCount(result.getFailCount() + 1);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 导入完成后的处理
}
};
// 创建读取器
ExcelReader excelReader = EasyExcel.read(file.getInputStream(), StandingBookImportDTO.class, listener).build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
excelReader.read(readSheet);
} catch (Exception e) {
result.getErrorMessages().add("导入失败:" + e.getMessage());
}
return result;
}
}