heheng
2025-10-14 93565578181199248beda6f610d95d07b261630e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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;
 
/**
 * <p>
 * 台账 服务实现类
 * </p>
 *
 * @author hh
 * @since 2025-07-31 16:12:02
 */
@Service
public class StandingBookServiceImpl extends ServiceImpl<StandingBookMapper, StandingBook> implements StandingBookService {
 
    @Autowired
    private StandingBookMapper standingBookMapper;
 
    @Autowired
    private SysDeptMapper sysDeptMapper;
 
    @Autowired
 
    private SysUserMapper sysUserMapper;
    @Override
    public CommonPage selectStandingBookList(StandingBook standingBook) {
        PageUtils.startPage();
        List<StandingBook> 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<DeptVo> deptVos = sysDeptMapper.selectDeptList(sysDept);
        Map<String, Long> deptNameIdMap = deptVos.stream()
                .collect(Collectors.toMap(DeptVo::getDeptName, DeptVo::getDeptId));
 
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getCompanyId, companyId);
        queryWrapper.eq(SysUser::getDelFlag, 0);
        List<SysUser> sysUsers = sysUserMapper.selectList(queryWrapper);
        Map<String, Long> userNameIdMap = sysUsers.stream()
                .collect(Collectors.toMap(SysUser::getName, SysUser::getId));
 
        try {
            // 使用EasyExcel读取文件
            ReadListener<StandingBookImportDTO> listener = new ReadListener<StandingBookImportDTO>() {
                @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;
    }
 
 
 
 
}