教育训练处考试制证系统后端
“djh”
2025-02-20 4bfab467a5645b9b0595ed4e2ee5970f0a449446
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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 List<CoalPayStudent> selectbyId(Long studentId) {
        List<Long> 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);
    }
}