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 com.ruoyi.common.utils.StringUtils; import org.apache.poi.ss.usermodel.*; 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 implements CoalPayStudentService { @Resource private CoalPayStudentMapper coalPayStudentMapper; @Override public List 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 coalPayStudents = coalPayStudentMapper.selectByIds(ids); List 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 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()); String idCard = row.getCell(1).getStringCellValue(); coalPayStudent.setIdCard(idCard); coalPayStudent.setPhone(row.getCell(2).getStringCellValue()); Cell cell = row.getCell(3); if (cell!=null){ if (StringUtils.isEmpty(cell.getStringCellValue())){ coalPayStudent.setTrain("-"); }else{ coalPayStudent.setTrain(cell.getStringCellValue()); } } char c = idCard.charAt(idCard.length() - 1); if (Character.isDigit(c)){ int genderDigit = Character.getNumericValue(c); if (genderDigit % 2 == 1) { coalPayStudent.setSex(0L); } else { coalPayStudent.setSex(1L); } } coalPayStudent.setPayStatus(0L); coalPayStudent.setCreateBy(loginUser.getUsername()); coalPayStudent.setCreateTime(new Date()); coalPayStudents.add(coalPayStudent); } } // 根据身份证号去重 List 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 selectByCoalPayId(Long id) { return coalPayStudentMapper.selectByCoalPayId(id); } @Override public List selectByCoalPayIdAndPayStatus(Long id, int status) { return coalPayStudentMapper.selectByCoalPayIdAndPayStatus(id,status); } @Override public List selectbyIdcard(CoalPayStudentReq coalPayStudent) { return coalPayStudentMapper.selectByIdcard(coalPayStudent); } @Override public List selectbyId(Long studentId) { List longs = new ArrayList<>(); longs.add(studentId); return coalPayStudentMapper.selectBatchIds(longs); } @Override public void updateByCoalPayId(CoalPayStudent coalPayStudent) { coalPayStudentMapper.updateByCoalPayId(coalPayStudent); } @Override public void updateByCoalPayIdAndStatus(CoalPayStudent payStudent) { coalPayStudentMapper.updateByCoalPayIdAndStatus(payStudent); } @Override public void updateByIdAndPayType(CoalPayStudent payStudent) { coalPayStudentMapper.updateByIdAndPayType(payStudent); } }