| | |
| | | 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实现 |
| | |
| | | |
| | | @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(); |
| | | } |
| | |
| | | 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; |
| | | } |
| | | } |