package com.gkhy.exam.pay.service.impl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.gkhy.exam.pay.dto.req.CoalPayStudentReq;
|
import com.gkhy.exam.pay.entity.CoalPayStudent;
|
import com.gkhy.exam.pay.mapper.CoalPayStudentMapper;
|
import com.gkhy.exam.pay.service.CoalPayStudentService;
|
import com.ruoyi.common.constant.ResultConstants;
|
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
import com.ruoyi.common.exception.BusinessException;
|
import com.ruoyi.common.utils.SecurityUtils;
|
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
import org.aspectj.weaver.loadtime.Aj;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
@Service
|
public class CoalPayStudentServiceImpl extends ServiceImpl<CoalPayStudentMapper, CoalPayStudent> implements CoalPayStudentService {
|
|
@Resource
|
private CoalPayStudentMapper coalPayStudentMapper;
|
|
|
@Override
|
public List<CoalPayStudent> selectCoalPayStudentList(Long coalPayId) {
|
return coalPayStudentMapper.selectByCoalPayId(coalPayId);
|
}
|
|
@Override
|
public AjaxResult updateByCoalPayStudent(CoalPayStudent coalPayStudent) {
|
CoalPayStudent payStudent = coalPayStudentMapper.selectById(coalPayStudent.getId());
|
if(payStudent==null){
|
throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"学生不存在");
|
}
|
if (payStudent.getPayStatus()==1){
|
throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"学生已完成缴费,不可修改");
|
}
|
coalPayStudent.setUpdateBy(SecurityUtils.getUsername());
|
coalPayStudent.setUpdateTime(new Date());
|
int i = coalPayStudentMapper.updateCoalPayStudentById(coalPayStudent);
|
if (i>0){
|
return AjaxResult.success();
|
}
|
return AjaxResult.error();
|
}
|
|
@Override
|
public int insertStudent(CoalPayStudent coalPayStudent) {
|
coalPayStudent.setCreateBy(SecurityUtils.getUsername());
|
coalPayStudent.setCreateTime(new Date());
|
return coalPayStudentMapper.insertCoalPayStudent(coalPayStudent);
|
}
|
|
@Override
|
public int deleteStudent(Long[] ids) {
|
List<CoalPayStudent> coalPayStudents = coalPayStudentMapper.selectByIds(ids);
|
List<CoalPayStudent> collect = coalPayStudents.stream().filter(cps -> cps.getPayStatus().equals(1)).collect(Collectors.toList());
|
if (!CollectionUtils.isEmpty(collect)){
|
throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"学生已完成缴费,请勿删除");
|
}
|
return coalPayStudentMapper.updateByIds(ids);
|
}
|
|
/**
|
* 导入学生
|
* @param file
|
* @param coalPayId
|
* @return
|
* @throws IOException
|
*/
|
@Override
|
public AjaxResult uploadStudent(MultipartFile file,Long coalPayId) throws IOException {
|
Workbook workbook = WorkbookFactory.create(file.getInputStream());
|
|
List<CoalPayStudent> coalPayStudents = new ArrayList<>();
|
LoginUser loginUser = SecurityUtils.getLoginUser();
|
try {
|
Sheet sheetAt = workbook.getSheetAt(0);
|
for (int i = 0; i < sheetAt.getLastRowNum(); i++) {
|
Row row = sheetAt.getRow(i + 1);
|
CoalPayStudent coalPayStudent = new CoalPayStudent();
|
coalPayStudent.setCoalPayId(coalPayId);
|
if (row!=null){
|
coalPayStudent.setName(row.getCell(0).getStringCellValue());
|
coalPayStudent.setIdCard(row.getCell(1).getStringCellValue());
|
coalPayStudent.setPhone(row.getCell(2).getStringCellValue());
|
String stringCellValue = row.getCell(3).getStringCellValue();
|
if (stringCellValue.equals("男")){
|
coalPayStudent.setSex(0L);
|
}else if (stringCellValue.equals("女")){
|
coalPayStudent.setSex(1L);
|
}
|
coalPayStudent.setPayStatus(0L);
|
coalPayStudent.setCreateBy(loginUser.getUsername());
|
coalPayStudent.setCreateTime(new Date());
|
coalPayStudents.add(coalPayStudent);
|
}
|
}
|
// 根据身份证号去重
|
List<CoalPayStudent> distinctStudents = coalPayStudents.stream()
|
.collect(Collectors.toMap(
|
CoalPayStudent::getIdCard, // 使用身份证号作为键
|
student -> student, // 使用学生对象作为值
|
(existing, replacement) -> existing // 如果键重复,选择保留第一个
|
))
|
.values()
|
.stream()
|
.collect(Collectors.toList());
|
for (CoalPayStudent coalPayStudent : distinctStudents) {
|
coalPayStudentMapper.insert(coalPayStudent);
|
}
|
return AjaxResult.success();
|
}finally {
|
workbook.close();
|
}
|
}
|
|
@Override
|
public List<CoalPayStudent> selectByCoalPayId(Long id) {
|
return coalPayStudentMapper.selectByCoalPayId(id);
|
}
|
|
@Override
|
public List<CoalPayStudent> selectByCoalPayIdAndPayStatus(Long id, int status) {
|
return coalPayStudentMapper.selectByCoalPayIdAndPayStatus(id,status);
|
}
|
|
@Override
|
public List<CoalPayStudent> selectbyIdcard(CoalPayStudentReq coalPayStudent) {
|
return coalPayStudentMapper.selectByIdcard(coalPayStudent);
|
}
|
|
@Override
|
public CoalPayStudent selectbyId(Long studentId) {
|
return coalPayStudentMapper.selectById(studentId);
|
}
|
}
|