kongzy
2023-09-22 3124f3a5b7f45d043b228829b6b3a2e541b31574
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
package com.nms.swspkmas_standalone.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nms.swspkmas_standalone.entity.Examinee;
import com.nms.swspkmas_standalone.entity.vo.ExamineeVO;
import com.nms.swspkmas_standalone.entity.vo.UploadObjectVO;
import com.nms.swspkmas_standalone.exception.ApiException;
import com.nms.swspkmas_standalone.mapper.ExamineeMapper;
import com.nms.swspkmas_standalone.response.CommonPage;
import com.nms.swspkmas_standalone.service.ExamineeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.nio.file.Paths;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
 
/**
 * <p>
 * 考生表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2023-09-19 17:45:28
 */
@Service
public class ExamineeServiceImpl extends ServiceImpl<ExamineeMapper, Examinee> implements ExamineeService {
 
    @Value("${image.upload_image}")
    private String imagePath;
 
    public Examinee getExamById(Long id){
        return getById(id);
    }
 
    @Override
    public void addExaminee(ExamineeVO examineeVO) {
        Examinee examinee=new Examinee();
        BeanUtils.copyProperties(examineeVO,examinee);
        save(examinee);
    }
 
    @Override
    public void updateExaminee(Long id, ExamineeVO examineeVO) {
        Examinee examinee=getById(id);
        if(examinee==null){
            throw new ApiException("考生信息不存在");
        }
        BeanUtils.copyProperties(examineeVO,examinee,new String[]{"id"});
        examinee.setUpdateTime(LocalDateTime.now());
        updateById(examinee);
    }
 
    @Override
    public void deleteExaminee(Long id) {
        Examinee examinee=getById(id);
        if(examinee==null){
            throw new ApiException("考生信息不存在");
        }
        removeById(id);
    }
 
    @Override
    public CommonPage examineeList(Integer pageNum, Integer pageSize,String name) {
        pageNum=pageNum<1?1:pageNum;
      //  Long total= baseMapper.examineeCount(name);
        PageHelper.startPage(pageNum,pageSize);
        List<Examinee> examineeList=baseMapper.examineeList(name);
        return CommonPage.restPage(examineeList);
 
//        CommonPage<Examinee> commonPage=new CommonPage<>();
//        commonPage.setList(examineeList);
//        commonPage.setPageSize(pageSize);
//        commonPage.setPageNum(pageNum);
//        commonPage.setTotal(total);
//        commonPage.setTotalPage(CommonPage.getTotalPage(total,pageSize));
    //    return commonPage;
    }
 
    @Override
    public UploadObjectVO uploadIcon(MultipartFile file) {
        UploadObjectVO uploadObjectVO=uploadFile(file);
        return uploadObjectVO;
    }
 
    @Override
    public Examinee getByIdNumber(String idNumber) {
        Examinee examinee=getOne(Wrappers.<Examinee>lambdaQuery()
                .eq(true,Examinee::getIdNumber,idNumber));
        return examinee;
    }
 
    public UploadObjectVO uploadFile(MultipartFile file){
        String filename=file.getOriginalFilename();
        String subfix=filename.substring(filename.lastIndexOf("."));
        filename=UUID.randomUUID()+subfix;
        String systemDir=System.getProperty("user.dir");
        String filePath=systemDir+File.separator+imagePath;
        File dirFile=new File(filePath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        filePath=filePath+File.separator+filename;
        try {
            file.transferTo(new File(filePath));
        } catch (FileNotFoundException e) {
            throw new ApiException("找不到文件");
        } catch (IOException e) {
            throw new ApiException("发生错误,请联系管理员");
        }
        UploadObjectVO uploadObjectVO=new UploadObjectVO().setFilename(filename)
                .setPath(filePath);
        return uploadObjectVO;
    }
}