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;
/**
*
* 考生表 服务实现类
*
*
* @author kzy
* @since 2023-09-19 17:45:28
*/
@Service
public class ExamineeServiceImpl extends ServiceImpl 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 examineeList=baseMapper.examineeList(name);
return CommonPage.restPage(examineeList);
// CommonPage 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.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;
}
}