教育训练处考试制证系统后端
heheng
2025-02-21 6448b8ad10f383dce2e3a2b4aab12965fe851d94
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
package com.gkhy.exam.pay.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.pay.dto.rep.NonCoalPayDetailH5RepDto;
import com.gkhy.exam.pay.dto.rep.NonCoalPayDetailRepDto;
import com.gkhy.exam.pay.dto.rep.NonCoalPayPageRepDto;
import com.gkhy.exam.pay.dto.rep.NonCoalPayStudentDetailRepDto;
import com.gkhy.exam.pay.dto.req.NonCoalPayCategoryReqDto;
import com.gkhy.exam.pay.dto.req.NonCoalPayReqDto;
import com.gkhy.exam.pay.dto.req.NonCoalPayTypeEditReqDto;
import com.gkhy.exam.pay.entity.NonCoalPay;
import com.gkhy.exam.pay.entity.NonCoalPayCategory;
import com.gkhy.exam.pay.entity.NonCoalPayStudent;
import com.gkhy.exam.pay.mapper.NonCoalPayMapper;
import com.gkhy.exam.pay.mapper.NonCoalPayStudentMapper;
import com.gkhy.exam.pay.service.NonCoalPayCategoryService;
import com.gkhy.exam.pay.service.NonCoalPayService;
import com.gkhy.exam.pay.service.NonCoalPayStudentService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
 
/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author hh
 * @date 2025-01-16
 */
@Service
public class NonCoalPayServiceImpl extends ServiceImpl<NonCoalPayMapper, NonCoalPay> implements NonCoalPayService {
    @Resource
    private NonCoalPayMapper nonCoalPayMapper;
    @Resource
    private NonCoalPayCategoryService nonCoalPayCategoryService;
    @Resource
    private NonCoalPayStudentService nonCoalPayStudentService;
    @Resource
    private NonCoalPayStudentMapper nonCoalPayStudentMapper;
 
    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public NonCoalPayDetailRepDto selectNonCoalPayById(Long id) {
        return nonCoalPayMapper.getNonCoalPayById(id);
    }
 
    @Override
    public List<NonCoalPayDetailH5RepDto> selectNonCoalPayByStu(String phone, String idCard) {
        return nonCoalPayMapper.selectNonCoalPayByParam(phone, idCard);
    }
 
 
    /**
     * 查询【请填写功能名称】列表
     *
     * @param nonCoalPay 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<NonCoalPayPageRepDto> selectNonCoalPayList(NonCoalPay nonCoalPay) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            if (!Constants.DIS_XINJIANG_CODE.equals(SecurityUtils.getDeptDistrictCode())) {
                nonCoalPay.setDistrictCode(SecurityUtils.getDeptDistrictCode());
            }
        }
        return nonCoalPayMapper.getNonCoalPayList(nonCoalPay);
    }
 
    @Override
    public NonCoalPayStudentDetailRepDto getNonCoalPayStudentByPayId(Long id) {
        NonCoalPayDetailRepDto nonCoalPayById = nonCoalPayMapper.getNonCoalPayById(id);
        NonCoalPayStudentDetailRepDto data = new NonCoalPayStudentDetailRepDto();
        if (nonCoalPayById != null) {
            BeanUtils.copyProperties(nonCoalPayById, data);
            NonCoalPayStudent student = new NonCoalPayStudent();
            student.setNonCoalPayId(nonCoalPayById.getId());
            List<NonCoalPayStudent> nonCoalPayStudents = nonCoalPayStudentService.selectNonCoalPayStudentList(student);
            data.setNonCoalPayStudentList(nonCoalPayStudents);
            if (nonCoalPayStudents != null && !nonCoalPayStudents.isEmpty()) {
                int payCount = (int) nonCoalPayStudents.stream()
                        .filter(stu -> stu.getPayStatus() == 1)
                        .count();
                int size = nonCoalPayStudents.size();
                data.setPayCount(payCount);
                data.setUnPayCount(size - payCount);
                data.setTotalCount(size);
                data.setPayCountFee(new BigDecimal(payCount).multiply(data.getAmount()));
                data.setUnPayCountFee(new BigDecimal(size - payCount).multiply(data.getAmount()));
                data.setTotalCountFee(new BigDecimal(size).multiply(data.getAmount()));
            } else {
                data.setPayCount(0);
                data.setUnPayCount(0);
                data.setTotalCount(0);
                data.setPayCountFee(new BigDecimal(0));
                data.setUnPayCountFee(new BigDecimal(0));
                data.setTotalCountFee(new BigDecimal(0));
            }
 
            return data;
        }
        return null;
    }
 
    /**
     * 新增【请填写功能名称】
     *
     * @param nonCoalPay 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNonCoalPay(NonCoalPayReqDto nonCoalPay) {
        if (!nonCoalPay.getDistrictCode().startsWith(Constants.DIS_XINJIANG_CODE)) {
            throw new RuntimeException("地州信息错误");
        }
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            if (Constants.DIS_XINJIANG_CODE.equals(nonCoalPay.getDistrictCode())) {
                throw new RuntimeException("仅可选择下级地州");
            }
 
        }
        checkData(nonCoalPay);
        NonCoalPay nonCoalPay1 = new NonCoalPay();
        BeanUtils.copyProperties(nonCoalPay, nonCoalPay1);
        nonCoalPay1.setCreateBy(SecurityUtils.getUsername());
 
        int i = nonCoalPayMapper.insertNonCoalPay(nonCoalPay1);
        if (i > 0) {
            saveNonCoalPayCategory(nonCoalPay.getNonCoalPayCategoryList(), nonCoalPay1.getId());
        }
        return i;
    }
 
    private void saveNonCoalPayCategory(List<NonCoalPayCategoryReqDto> nonCoalPayCategoryList, Long id) {
        List<NonCoalPayCategory> nonCoalPayCategories = new ArrayList<>();
        for (NonCoalPayCategoryReqDto nonCoalPayCategory : nonCoalPayCategoryList) {
            NonCoalPayCategory nonCoalPayCategory1 = new NonCoalPayCategory();
            BeanUtils.copyProperties(nonCoalPayCategory, nonCoalPayCategory1);
            nonCoalPayCategory1.setNonCoalPayId(id);
            nonCoalPayCategory1.setCreateBy(SecurityUtils.getUsername());
            nonCoalPayCategories.add(nonCoalPayCategory1);
        }
        boolean b = nonCoalPayCategoryService.saveBatchData(nonCoalPayCategories);
        if (!b) {
            throw new ServiceException("保存失败");
        }
    }
 
 
    private void checkData(NonCoalPayReqDto nonCoalPay) {
        LambdaQueryWrapper<NonCoalPay> lambdaQueryWrapper = Wrappers.<NonCoalPay>lambdaQuery()
                .eq(NonCoalPay::getDistrictCode, nonCoalPay.getDistrictCode())
                .eq(NonCoalPay::getBatchName, nonCoalPay.getBatchName())
                .eq(NonCoalPay::getDelFlag, 0);
        if (nonCoalPay.getId() != null) {
            lambdaQueryWrapper.ne(NonCoalPay::getId, nonCoalPay.getId());
        }
        Long l = baseMapper.selectCount(lambdaQueryWrapper);
        if (l > 0) {
            throw new ServiceException("该批次已存在");
        }
    }
 
    /**
     * 修改【请填写功能名称】
     *
     * @param nonCoalPay 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNonCoalPay(NonCoalPayReqDto nonCoalPay) {
 
        checkData(nonCoalPay);
        checkHavePay(nonCoalPay.getId());
        NonCoalPay nonCoalPay1 = new NonCoalPay();
        BeanUtils.copyProperties(nonCoalPay, nonCoalPay1);
        nonCoalPay1.setDistrictCode(null);
        nonCoalPay1.setUpdateBy(SecurityUtils.getUsername());
        int i = nonCoalPayMapper.updateNonCoalPay(nonCoalPay1);
        if (i > 0) {
            int i1 = nonCoalPayCategoryService.deleteNonCoalPayCategoryByPayId(nonCoalPay.getId());
            if (i1 < 1) {
                throw new ServiceException("更新失败");
            }
            saveNonCoalPayCategory(nonCoalPay.getNonCoalPayCategoryList(), nonCoalPay1.getId());
        }
        return i;
    }
 
    @Override
    public int updateNonCoalPayType(NonCoalPayTypeEditReqDto nonCoalPay) {
        NonCoalPay nonCoalPay1 = new NonCoalPay();
        BeanUtils.copyProperties(nonCoalPay, nonCoalPay1);
        nonCoalPay1.setUpdateBy(SecurityUtils.getUsername());
        return nonCoalPayMapper.updateNonCoalPay(nonCoalPay1);
    }
 
    @Override
    @Transactional
    public int updateNonCoalPayTypeStatus(int payPersonType, Long id) {
        NonCoalPay byId = nonCoalPayMapper.selectNonCoalPayById(id);
        if (byId == null) {
            throw new ServiceException("该数据不存在");
        }
        if (byId.getPayPersonType() == 2) {
            throw new ServiceException("已是团体缴费不可变更缴费方式!");
        }
        NonCoalPay nonCoalPay1 = new NonCoalPay();
        nonCoalPay1.setPayPersonType(payPersonType);
        nonCoalPay1.setId(id);
        nonCoalPay1.setUpdateBy(SecurityUtils.getUsername());
        int i = nonCoalPayMapper.updateNonCoalPay(nonCoalPay1);
        if (payPersonType == 2) {
            //更新未缴费的为团体
            nonCoalPayStudentMapper.updateNonCoalPayStudentType(id, SecurityUtils.getUsername());
        }
        return i;
    }
 
    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteNonCoalPayByIds(Long[] ids) {
        return nonCoalPayMapper.deleteNonCoalPayByIds(ids);
    }
 
    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteNonCoalPayById(Long id) {
        NonCoalPay nonCoalPay = nonCoalPayMapper.selectNonCoalPayById(id);
        if (nonCoalPay == null) {
            throw new ServiceException("该数据不存在");
        }
        //验证是否有学员已缴费
        checkHavePay(id);
        return nonCoalPayMapper.deleteNonCoalPayById(id, SecurityUtils.getUsername());
    }
 
    private void checkHavePay(Long id) {
        nonCoalPayStudentService.checkHavePayData(id);
    }
}