教育训练处考试制证系统后端
“djh”
2025-01-24 5a88c770debe086dd82ab5ce8a4c957a3debdae7
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package com.gkhy.exam.pay.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.pay.dto.rep.CoalPayRepDto;
import com.gkhy.exam.pay.dto.rep.CoalPayStudentRep;
import com.gkhy.exam.pay.dto.req.*;
import com.gkhy.exam.pay.entity.*;
import com.gkhy.exam.pay.mapper.CoalCategoryMapper;
import com.gkhy.exam.pay.mapper.CoalPayCategoryMapper;
import com.gkhy.exam.pay.mapper.CoalPayMapper;
import com.gkhy.exam.pay.service.CoalPayService;
import com.gkhy.exam.pay.service.CoalPayStudentService;
import com.gkhy.exam.pay.utils.PayUtils;
import com.gkhy.exam.pay.utils.ResultVo;
import com.ruoyi.common.constant.ResultConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.RandomUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
@Service
public class CoalPayServiceImpl extends ServiceImpl<CoalPayMapper, CoalPay> implements CoalPayService {
 
    @Resource
    private CoalPayMapper coalPayMapper;
    @Resource
    private CoalPayCategoryMapper coalPayCategoryMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;
 
    @Resource
    private CoalCategoryMapper coalCategoryMapper;
    @Autowired
    private CoalPayStudentService coalPayStudentService;
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Autowired
    private PayUtils payUtils;
 
 
    /**
     * 缴费管理列表
     *
     * @param coalPay
     * @return
     */
    @Override
    public List<CoalPayRepDto> selectCoalPayList(CoalPayReq coalPay) {
        List<CoalPayRepDto> coalPayRepDtos = new ArrayList<>();
        List<CoalPay> coalPays = coalPayMapper.selectCoalPayList(coalPay);
        for (CoalPay pay : coalPays) {
            CoalPayRepDto coalPayRepDto = new CoalPayRepDto();
            BeanUtils.copyProperties(pay, coalPayRepDto);
            //部门数据
            SysDept sysDept = sysDeptMapper.selectDeptById(pay.getDeptId());
            coalPayRepDto.setDeptName(sysDept.getDeptName());
            //工种数据
            List<CoalCategory> coalCategories = coalCategoryMapper.selectByCoalPayId(pay.getId());
            coalPayRepDto.setCoalCategoryList(coalCategories);
            //学员数据
            List<CoalPayStudent> coalPayStudents = coalPayStudentService.selectByCoalPayId(pay.getId());
            List<CoalPayStudent> havePay = coalPayStudents.stream()
                    .filter(stu -> stu.getPayStatus() != null && stu.getPayStatus().equals(1))
                    .collect(Collectors.toList());
            coalPayRepDto.setTotalNum(coalPayStudents.size());
            coalPayRepDto.setHavePayNum(havePay.size());
            coalPayRepDtos.add(coalPayRepDto);
        }
        return coalPayRepDtos;
    }
 
    @Override
    public CoalPayRepDto selectCoalPayById(Long id) {
 
        CoalPayRepDto coalPayRepDto = new CoalPayRepDto();
 
        //基本数据
        CoalPay coalPay = coalPayMapper.selectById(id);
        BeanUtils.copyProperties(coalPay, coalPayRepDto);
        //考点名称
        SysDept sysDept = sysDeptMapper.selectDeptById(coalPay.getDeptId());
        coalPayRepDto.setDeptName(sysDept.getDeptName());
        //工种类别
        List<CoalCategory> coalCategories = coalCategoryMapper.selectByCoalPayId(coalPay.getId());
        coalPayRepDto.setCoalCategoryList(coalCategories);
 
        //学员数据
        List<CoalPayStudent> coalPayStudents = coalPayStudentService.selectByCoalPayId(id);
        List<CoalPayStudent> havePay = coalPayStudents.stream()
                .filter(stu -> stu.getPayStatus() != null && stu.getPayStatus().equals(0))
                .collect(Collectors.toList());
        coalPayRepDto.setTotalNum(coalPayStudents.size());
        coalPayRepDto.setHavePayNum(havePay.size());
 
        return coalPayRepDto;
    }
 
    @Override
    public int insertCoalPay(CoalPayDto coalPayDto) {
        CoalPay coalPay = new CoalPay();
        BeanUtils.copyProperties(coalPayDto, coalPay);
        coalPay.setCreateBy(SecurityUtils.getUsername());
        coalPay.setCreateTime(new Date());
        int insert = coalPayMapper.insertBath(coalPay);
        if (CollectionUtils.isEmpty(coalPayDto.getCoalPayCategoryies())) {
            throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "工种类别不能为空");
        }
        List<CoalPayCategory> coalPayCategories = coalPayDto.getCoalPayCategoryies();
        for (CoalPayCategory coalPayCategory : coalPayCategories) {
            coalPayCategory.setCoalPayId(coalPay.getId());
            coalPayCategoryMapper.insert(coalPayCategory);
        }
        return insert;
    }
 
    /**
     * 修改缴费信息
     *
     * @param coalPayDto
     * @return
     */
    @Override
    public int updateCoalPay(CoalPayDto coalPayDto) {
        CoalPay coalPay = new CoalPay();
        BeanUtils.copyProperties(coalPayDto, coalPay);
        coalPay.setUpdateBy(SecurityUtils.getUsername());
        coalPay.setUpdateTime(new Date());
        int i = coalPayMapper.updateCoalPayById(coalPay);
        int update = coalPayCategoryMapper.deleteByCoalPayId(coalPayDto.getId());
        if (update > 0) {
            List<CoalPayCategory> coalPayCategories = coalPayDto.getCoalPayCategoryies();
            for (CoalPayCategory coalPayCategory : coalPayCategories) {
                coalPayCategory.setCoalPayId(coalPay.getId());
                coalPayCategoryMapper.insert(coalPayCategory);
            }
        }
        return i;
    }
 
    @Override
    public AjaxResult deleteCoalPayByIds(Long[] ids) {
        for (Long id : ids) {
            List<CoalPayStudent> coalPayStudents = coalPayStudentService.selectByCoalPayIdAndPayStatus(id, 1);
            if (!CollectionUtils.isEmpty(coalPayStudents)) {
                throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "已有学员完成缴费,请勿删除");
            }
        }
        int i = coalPayMapper.updateByIds(ids);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
 
    //个人查询缴费
    @Override
    public List<CoalPayStudentRep> selectCoalPay(String idcard, String phone) {
        CoalPayStudentReq coalPayStudentReq = new CoalPayStudentReq();
        coalPayStudentReq.setIdCard(idcard);
        coalPayStudentReq.setPhone(phone);
        //查询个人需要缴费
        List<CoalPayStudent> coalPayStudents = coalPayStudentService.selectbyIdcard(coalPayStudentReq);
        List<CoalPayStudentRep> coalPayStudentReps = new ArrayList<>();
        for (CoalPayStudent payStudent : coalPayStudents) {
            //封装学生基础信息
            CoalPayStudentRep coalPayStudentRep = new CoalPayStudentRep();
            coalPayStudentRep.setName(payStudent.getName());
            coalPayStudentRep.setIdCard(payStudent.getIdCard());
            coalPayStudentRep.setPhone(payStudent.getPhone());
            coalPayStudentRep.setSex(payStudent.getSex());
            coalPayStudentRep.setPayStatus(payStudent.getPayStatus());
 
            //查询对应批次以及批次包含工种类别
            CoalPay coalPay = coalPayMapper.selectById(payStudent.getCoalPayId());
            CoalPayRepDto coalPayRepDto = new CoalPayRepDto();
            BeanUtils.copyProperties(coalPay, coalPayRepDto);
            //考点名称
            SysDept sysDept = sysDeptMapper.selectDeptById(coalPay.getDeptId());
            coalPayRepDto.setDeptName(sysDept.getDeptName());
            //查询批次对应工种类别
            List<CoalCategory> coalCategories = coalCategoryMapper.selectByCoalPayId(payStudent.getCoalPayId());
            coalPayRepDto.setCoalCategoryList(coalCategories);
            coalPayStudentRep.setCoalPays(coalPayRepDto);
            coalPayStudentReps.add(coalPayStudentRep);
        }
        return coalPayStudentReps;
    }
 
    @Override
    public int updateCoalPayType(CoalPayTypeReq coalPayTypeReq) {
        CoalPay byId = coalPayMapper.selectById(coalPayTypeReq.getCoalPayId());
        if (coalPayTypeReq.getPayPersonType() != null && byId.getPayPersonType() == 2) {
            throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "已为团体缴费,不可更改");
        }
        int i = coalPayMapper.updateByPayId(coalPayTypeReq);
        if (i>0){
            CoalPayStudent coalPayStudent = new CoalPayStudent();
            coalPayStudent.setCoalPayId(coalPayTypeReq.getCoalPayId());
            coalPayStudent.setPayType(2);
            coalPayStudentService.updateByCoalPayId(coalPayStudent);
        }
        return i;
    }
 
    @Override
    public String personPayMoney(Long coalPayId, Long studentId) {
        CoalPayStudent payStudent = new CoalPayStudent();
 
        RLock lock = redissonClient.getLock("SWSPKMAS_PAY_PERSON_" + studentId);
        try {
            lock.lock(10, TimeUnit.SECONDS);
 
            List<CoalPayStudent> coalPayStudent = coalPayStudentService.selectbyId(studentId);
            CoalPayStudent student = coalPayStudent.get(0);
            CoalPay coalPay = coalPayMapper.selectById(coalPayId);
            List<CoalCategory> coalCategories = coalCategoryMapper.selectByCoalPayId(coalPayId);
            CoalTicket coalTicket = coalCategoryMapper.selectCoalTicket();
            if (CollectionUtils.isEmpty(coalPayStudent)){
                throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"缴费学员不存在");
            }
            if (coalPay==null){
                throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"未找到相关缴费信息");
            }
            if (student.getPayStatus()==1){
                throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"请勿重新缴费");
            }
 
            PayReqData payReqData = fillData(coalPayStudent,coalPay,coalCategories,coalTicket,1,null);
 
            ResultVo resultVo = payUtils.sendApiPost(payReqData);
            if (resultVo.getRespcode().equals("BUS0000")) {
                payStudent.setId(studentId);
                payStudent.setOrderId(resultVo.getRespdata().getOrderNo());
                payStudent.setGovPayStatus(1);
                payStudent.setPayCode(resultVo.getRespdata().getBillNo());
                coalPayStudentService.updateByCoalPayStudent(payStudent);
                return resultVo.getRespdata().getOrderId();
            } else {
                throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, resultVo.getRespmsg());
            }
        } catch (BusinessException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
    }
 
    @Override
    public String teamPayMoney(CoalTeamPayReq coalTeamPayReq) {
        RLock lock = redissonClient.getLock("SWSPKMAS_TEAM_PAY_" + coalTeamPayReq.getCoalPayId());
        try {
            lock.lock(10,TimeUnit.SECONDS);
            List<CoalPayStudent> coalPayStudents = coalPayStudentService.selectByCoalPayIdAndPayStatus(coalTeamPayReq.getCoalPayId(), 0);
            CoalPay coalPay = coalPayMapper.selectById(coalTeamPayReq.getCoalPayId());
            List<CoalCategory> coalCategories = coalCategoryMapper.selectByCoalPayId(coalTeamPayReq.getCoalPayId());
            CoalTicket coalTicket = coalCategoryMapper.selectCoalTicket();
            if (CollectionUtils.isEmpty(coalPayStudents)){
                throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"缴费学员不存在");
            }
            if (coalPay==null){
                throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"未找到相关缴费信息");
            }
 
            CoalPayStudent payStudent = new CoalPayStudent();
            PayReqData payReqData = fillData(coalPayStudents, coalPay, coalCategories, coalTicket,2,coalTeamPayReq);
            ResultVo resultVo = payUtils.sendApiPost(payReqData);
            if (resultVo.getRespcode().equals("BUS0000")) {
                payStudent.setCoalPayId(coalPay.getId());
                payStudent.setOrderId(resultVo.getRespdata().getOrderNo());
                payStudent.setGovPayStatus(1);
                payStudent.setPayCode(resultVo.getRespdata().getBillNo());
                coalPayStudentService.updateByCoalPayIdAndStatus(payStudent);
                return resultVo.getRespdata().getOrderId();
            } else {
                throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "发起支付失败,请稍后重试");
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
    }
 
    private PayReqData fillData(List<CoalPayStudent>  coalPayStudent, CoalPay coalPay, List<CoalCategory> coalCategories, CoalTicket coalTicket,Integer payType,CoalTeamPayReq coalTeamPayReq) {
        PayReqData payReqData = new PayReqData();
        payReqData.setOrderNo(RandomUtil.generateOrderNumber(coalPay.getId(), "CO"));
        payReqData.setMoney(coalPay.getAmount().multiply(BigDecimal.valueOf(coalPayStudent.size())));
        payReqData.setAmount(coalCategories.size()*coalPayStudent.size());
        payReqData.setInvoiceSocialCode(coalTicket.getTicketCompanyCode());
        payReqData.setHandlingPerson(coalTicket.getDrawer());
        payReqData.setChecker(coalTicket.getCheck());
        payReqData.setEnterCode(coalTicket.getCompanyCode());
        payReqData.setDesc(coalPay.getBatchName());
 
 
        List<PayReqData.Feedata> feedatas = new ArrayList<>();
        for (CoalCategory coalCategory : coalCategories) {
            for (CoalPayStudent payStudent : coalPayStudent) {
                PayReqData.Feedata feedata = new PayReqData.Feedata();
                feedata.setAmount(1);
                feedata.setPrice(coalCategory.getAmount());
                feedata.setBusCode(coalCategory.getBusinessCode());
                feedatas.add(feedata);
            }
        }
        payReqData.setFeeDatas(feedatas);
 
        if (1==payType){
            payReqData.setPayerName(coalPayStudent.get(0).getName());
            payReqData.setCertNo(coalPayStudent.get(0).getIdCard());
            payReqData.setPayerType(1);
        }else if (2==payType){
            payReqData.setPayerName(coalTeamPayReq.getPayCompanyName());
            payReqData.setCertNo(coalTeamPayReq.getPayCompanyCard());
            payReqData.setPayerType(2);
        }
        return payReqData;
 
    }
}