MrShi
2026-01-29 8676f4cb37ef31fa9fcfe2a7faf5f4c4ea77cc1a
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.doumee.service.business.impl;
 
import com.doumee.core.annotation.excel.ExcelImporter;
import com.doumee.core.constants.Constants;
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.DateUtil;
import com.doumee.dao.business.dto.CasesImport;
import com.doumee.dao.business.dto.MemberImport;
import com.doumee.dao.business.model.ImportRecord;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.ImportRecordMapper;
import com.doumee.dao.business.model.Member;
import com.doumee.service.business.ImportRecordService;
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 org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
/**
 * 分类信息表Service实现
 * @author doumee
 * @date 2026-01-27 16:02:37
 */
@Service
public class ImportRecordServiceImpl implements ImportRecordService {
 
    @Autowired
    private ImportRecordMapper importRecordMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
 
    @Override
    public Integer create(ImportRecord importRecord) {
        if(StringUtils.isBlank(importRecord.getImgurl())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        importRecord.setDeleted(Constants.ZERO);
        importRecord.setStatus(Constants.ZERO);
        importRecord.setCreateTime(new Date());
        importRecord.setCreateUser(loginUserInfo.getId());
        importRecord.setUpdateTime(new Date());
        importRecord.setUpdateUser(loginUserInfo.getId());
        importRecordMapper.insert(importRecord);
        return importRecord.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        importRecordMapper.deleteById(id);
    }
 
    @Override
    public void delete(ImportRecord importRecord) {
        UpdateWrapper<ImportRecord> deleteWrapper = new UpdateWrapper<>(importRecord);
        importRecordMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        importRecordMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(ImportRecord importRecord) {
        importRecordMapper.updateById(importRecord);
    }
 
    @Override
    public void updateByIdInBatch(List<ImportRecord> importRecords) {
        if (CollectionUtils.isEmpty(importRecords)) {
            return;
        }
        for (ImportRecord importRecord: importRecords) {
            this.updateById(importRecord);
        }
    }
 
    @Override
    public ImportRecord findById(Integer id) {
        return importRecordMapper.selectById(id);
    }
 
    @Override
    public ImportRecord findOne(ImportRecord importRecord) {
        QueryWrapper<ImportRecord> wrapper = new QueryWrapper<>(importRecord).last("limit 1");
        return importRecordMapper.selectOne(wrapper);
    }
 
    @Override
    public List<ImportRecord> findList(ImportRecord importRecord) {
        QueryWrapper<ImportRecord> wrapper = new QueryWrapper<>(importRecord);
        return importRecordMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<ImportRecord> findPage(PageWrap<ImportRecord> pageWrap) {
        IPage<ImportRecord> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<ImportRecord> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
               queryWrapper.lambda().eq(pageWrap.getModel().getId() != null,ImportRecord::getId, pageWrap.getModel().getId());
               queryWrapper.lambda().eq(pageWrap.getModel().getDeleted() != null,ImportRecord::getDeleted, pageWrap.getModel().getDeleted());
               queryWrapper.lambda().eq(pageWrap.getModel().getCreateUser() != null,ImportRecord::getCreateUser, pageWrap.getModel().getCreateUser());
             if (pageWrap.getModel().getId() != null) {
                  queryWrapper.lambda().ge(ImportRecord::getCreateTime, Utils.Date.getStart(pageWrap.getModel().getCreateTime()));
                  queryWrapper.lambda().le(ImportRecord::getCreateTime, Utils.Date.getEnd(pageWrap.getModel().getCreateTime()));
             }
               queryWrapper.lambda().eq(pageWrap.getModel().getUpdateUser() != null,ImportRecord::getUpdateUser, pageWrap.getModel().getUpdateUser());
             if (pageWrap.getModel().getId() != null) {
                  queryWrapper.lambda().ge(ImportRecord::getUpdateTime, Utils.Date.getStart(pageWrap.getModel().getUpdateTime()));
                  queryWrapper.lambda().le(ImportRecord::getUpdateTime, Utils.Date.getEnd(pageWrap.getModel().getUpdateTime()));
             }
               queryWrapper.lambda().eq(pageWrap.getModel().getRemark() != null,ImportRecord::getRemark, pageWrap.getModel().getRemark());
               queryWrapper.lambda().eq(pageWrap.getModel().getStatus() != null,ImportRecord::getStatus, pageWrap.getModel().getStatus());
               queryWrapper.lambda().eq(pageWrap.getModel().getTitle() != null,ImportRecord::getTitle, pageWrap.getModel().getTitle());
               queryWrapper.lambda().eq(pageWrap.getModel().getTotalNum() != null,ImportRecord::getTotalNum, pageWrap.getModel().getTotalNum());
               queryWrapper.lambda().eq(pageWrap.getModel().getDetail() != null,ImportRecord::getDetail, pageWrap.getModel().getDetail());
               queryWrapper.lambda().eq(pageWrap.getModel().getImgurl() != null,ImportRecord::getImgurl, pageWrap.getModel().getImgurl());
             if (pageWrap.getModel().getId() != null) {
                  queryWrapper.lambda().ge(ImportRecord::getStartDate, Utils.Date.getStart(pageWrap.getModel().getStartDate()));
                  queryWrapper.lambda().le(ImportRecord::getStartDate, Utils.Date.getEnd(pageWrap.getModel().getStartDate()));
             }
             if (pageWrap.getModel().getId() != null) {
                  queryWrapper.lambda().ge(ImportRecord::getEndDate, Utils.Date.getStart(pageWrap.getModel().getEndDate()));
                  queryWrapper.lambda().le(ImportRecord::getEndDate, Utils.Date.getEnd(pageWrap.getModel().getEndDate()));
             }
               queryWrapper.lambda().eq(pageWrap.getModel().getSortnum() != null,ImportRecord::getSortnum, pageWrap.getModel().getSortnum());
               queryWrapper.lambda().eq(pageWrap.getModel().getType() != null,ImportRecord::getType, pageWrap.getModel().getType());
               queryWrapper.lambda().eq(pageWrap.getModel().getDoneNum() != null,ImportRecord::getDoneNum, pageWrap.getModel().getDoneNum());
               queryWrapper.lambda().eq(pageWrap.getModel().getErrorNum() != null,ImportRecord::getErrorNum, pageWrap.getModel().getErrorNum());
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(importRecordMapper.selectPage(page, queryWrapper));
    }
 
    @Override
    public long count(ImportRecord importRecord) {
        QueryWrapper<ImportRecord> wrapper = new QueryWrapper<>(importRecord);
        return importRecordMapper.selectCount(wrapper);
    }
    @Override
    public ImportRecord importBatch(MultipartFile file,int type ){
        Boolean importing = (Boolean) redisTemplate.opsForValue().get(Constants.RedisKeys.IMPORTING_RECORD);
        if(importing!=null && importing){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,已存在导入任务正在执行中,请稍后再试!");
        }
        redisTemplate.opsForValue().set(Constants.RedisKeys.IMPORTING_RECORD,true,30, TimeUnit.MINUTES);
        try {
            ImportRecord model = new ImportRecord();
            LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
            model.setDeleted(Constants.ZERO);
            model.setStatus(Constants.ONE);//异步处理中
            model.setCreateTime(new Date());
            model.setCreateUser(loginUserInfo.getId());
            model.setUpdateTime(model.getCreateTime());
            model.setUpdateUser(loginUserInfo.getId());
            model.setType(type);
            model.setTitle((type==1?"案例信息批量导入":"老师信息批量导入")+ DateUtil.getPlusTime2(model.getCreateTime()));
            model.setTotalNum(0);
            ExcelImporter ie= new ExcelImporter(file,0,0, CellType.STRING); // 确保单元格类型为字符串);
            if(type == 1) {
              model.setCaseList(ie.getDataList(CasesImport.class,null));
              if(model.getCaseList() ==null || model.getCaseList().size()==0){
                  throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"导入文件案例信息内容为空!");
              }
              model.setTotalNum(model.getCaseList().size());
            }else{
              model.setMemberList(ie.getDataList(MemberImport.class,null));
              if(model.getMemberList() ==null || model.getMemberList().size()==0){
                  throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"导入文件老师信息内容为空!");
              }
              model.setTotalNum(model.getMemberList().size());
            }
//            model.setPictureDataList(ie);
            importRecordMapper.insert(model);
            return model;
        }catch (Exception e){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"文件信息读取失败,请检查文件内容后重试!");
        }finally {
            redisTemplate.delete(Constants.RedisKeys.IMPORTING_RECORD);
        }
    }
 
    /**
     * 异步执行文件任务
     * @param importRecord
     */
    @Override
    @Async
    public void dealImporTask(ImportRecord importRecord){
        int success = 0;
        if(Constants.equalsInteger(importRecord.getType(),0)){
           dealUserImportBiz(importRecord);
        }else{
            dealCaseImportBiz(importRecord);
        }
        importRecord.setStatus(Constants.TWO);
        importRecord.setUpdateTime(new Date());
        importRecordMapper.updateById(importRecord);
 
    }
 
    /**
     * 处理案例导入任务
     * @param importRecord
     */
 
    private int dealCaseImportBiz(ImportRecord importRecord) {
        int success=0;
        String msg ="";
        try {
            for(CasesImport param:importRecord.getCaseList()){
 
            }
        }catch (Exception e){
 
        }
        importRecord.setDoneNum(success);
        importRecord.setErrorNum(importRecord.getTotalNum() - success);
        return success;
    }
 
    /**
     * 处理人员导入记录
     * @param importRecord
     */
    private int dealUserImportBiz(ImportRecord importRecord) {
        int success=0;
        String msg = "";
        try {
            for(MemberImport param:importRecord.getMemberList()){
 
            }
        }catch (Exception e){
 
        }
        importRecord.setDoneNum(success);
        importRecord.setErrorNum(importRecord.getTotalNum() - success);
        importRecord.setDetail(msg);
 
        return success;
    }
}