kongzy
2024-09-14 f0f00e9ba8a755e4317e029d73b69a92ad9f9df1
exam-system/src/main/java/com/gkhy/exam/system/service/impl/SysCommonServiceImpl.java
@@ -2,22 +2,35 @@
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gkhy.exam.common.config.FilePathConfig;
import com.gkhy.exam.common.config.MinioConfig;
import com.gkhy.exam.common.domain.entity.SysUser;
import com.gkhy.exam.common.enums.ResourceTypeEnum;
import com.gkhy.exam.common.enums.UserTypeEnum;
import com.gkhy.exam.common.excel.StudentExcelData;
import com.gkhy.exam.common.excel.StudentExcelDataListener;
import com.gkhy.exam.common.exception.ApiException;
import com.gkhy.exam.common.utils.*;
import com.gkhy.exam.system.domain.ExResource;
import com.gkhy.exam.system.domain.ExStudent;
import com.gkhy.exam.system.domain.SysCompany;
import com.gkhy.exam.system.domain.vo.UploadObjectVO;
import com.gkhy.exam.system.mapper.ExResourceMapper;
import com.gkhy.exam.system.service.ExStudentService;
import com.gkhy.exam.system.service.SysCommonService;
import com.gkhy.exam.system.service.SysCompanyService;
import com.gkhy.exam.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
@@ -33,6 +46,9 @@
@Slf4j
public class SysCommonServiceImpl implements SysCommonService {
    @Autowired
    private MinioConfig minioConfig;
    @Value("${image.upload_image}")
    private String uploadPath;
    @Autowired
@@ -43,9 +59,15 @@
    private ExResourceMapper resourceMapper;
    @Resource
    private FilePathConfig filePathConfig;
    @Autowired
    private SysCompanyService companyService;
    @Autowired
    private ExStudentService studentService;
    @Autowired
    private SysUserService userService;
    // 使用HashSet存储允许的文件格式,提高查找效率
    private final HashSet<String> FORMATSET = new HashSet<>(Arrays.asList(".mp4", ".mp3", ".xls", ".xlsx", ".doc", ".docx", ".ppt", ".pptx", ".pdf"));
    private final HashSet<String> FORMATSET = new HashSet<>(Arrays.asList(".mp4",".doc", ".docx", ".ppt", ".pptx", ".pdf"));
    @Resource(name = "threadPoolTaskExecutor")
@@ -202,6 +224,7 @@
            log.warn("临时目录 {}已删除", new File(localPath).getParent());
        });
        uploadObjectVO.setPath(minioPath);
        uploadObjectVO.setUrl(minioConfig.getEndpoint()+minioConfig.getBucketName()+"/"+minioPath);
        return uploadObjectVO;
    }
@@ -236,6 +259,87 @@
        }
    }
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void importStudent() {
        String path="/home/java/train_exam/back/安全教育学员模板.xlsx";
      //  String path="F:/kzy/乱七八糟/安全教育学员模板.xlsx";
        List<StudentExcelData> studentExcelDataList=EasyExcel.read(path, StudentExcelData.class,new StudentExcelDataListener()).sheet().doReadSync();
        List<ExStudent> students=new ArrayList<>();
        List<StudentExcelData> errorStudents=new ArrayList<>();
        SysCompany company=companyService.getOne(Wrappers.<SysCompany>lambdaQuery().eq(SysCompany::getName,studentExcelDataList.get(0).getCompanyName())
                .eq(SysCompany::getDelFlag,0));
        if(company==null){
            throw new ApiException("公司不存在");
        }
        for(StudentExcelData studentExcelData:studentExcelDataList){
            String errorMessage=validateData(studentExcelData);
            if(StringUtils.isNotBlank(errorMessage)){
                studentExcelData.setRemark(errorMessage);
                errorStudents.add(studentExcelData);
                continue;
            }
            ExStudent dbStudent=studentService.getOne(Wrappers.<ExStudent>lambdaQuery().eq(ExStudent::getPhone,studentExcelData.getPhone()));
            if(dbStudent!=null){
                if(!dbStudent.getName().equals(studentExcelData.getName())){
                    studentExcelData.setRemark("序号"+studentExcelData.getIndex()+"学员用户已存在");
                    errorStudents.add(studentExcelData);
                }
                continue;
            }
            ExStudent student=new ExStudent();
            BeanUtils.copyProperties(studentExcelData,student,new String[]{"sex"});
            if("男".equals(studentExcelData.getSex())){
                student.setSex(0);
            }else{
                student.setSex(1);
            }
            String departName=studentExcelData.getDeptName();
            List<SysUser> users=userService.list(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getName,departName).eq(SysUser::getUserType, UserTypeEnum.DEPART_USER.getCode()).eq(SysUser::getDelFlag,0).last(" limit 1"));
            if(users.isEmpty()){
                studentExcelData.setRemark("序号"+studentExcelData.getIndex()+"未找到相应的部门账号");
                errorStudents.add(studentExcelData);
                continue;
            }
            student.setPassword(SecurityUtils.encryptPassword("123456@a"));
            student.setCompanyId(company.getId());
            student.setCreateId(users.get(0).getId());
            students.add(student);
        }
        if(!errorStudents.isEmpty()){
            EasyExcel.write("error.xlsx").head(StudentExcelData.class)
                    .sheet("异常用户列表")
                    .doWrite(errorStudents);
        }
        studentService.saveBatch(students);
    }
    public String validateData(StudentExcelData studentExcelData){
        if(StringUtils.isBlank(studentExcelData.getName())){
            return "序号"+studentExcelData.getIndex()+"姓名为空";
        }
        if(StringUtils.isBlank(studentExcelData.getSex())){
            return "序号"+studentExcelData.getIndex()+"性别为空";
        }
        if(StringUtils.isBlank(studentExcelData.getIdNo())||studentExcelData.getIdNo().length()!=18){
            return "序号"+studentExcelData.getIndex()+"身份证为空或者长度不正确";
        }
        if(StringUtils.isBlank(studentExcelData.getPhone())||studentExcelData.getPhone().length()!=11){
            return "序号"+studentExcelData.getIndex()+"手机号为空或者长度不正确";
        }
        if(StringUtils.isBlank(studentExcelData.getDeptName())){
            return "序号"+studentExcelData.getIndex()+"部门为空";
        }
        return "";
    }
    public UploadObjectVO mergeFile(String fileMd5,String fileName){
        String subfix = fileName.substring(fileName.lastIndexOf("."));
        String systemDir=System.getProperty("user.dir");