From 02fa12e93281fea472323201e5eb6ab55466a63d Mon Sep 17 00:00:00 2001
From: heheng <heheng@123456>
Date: 星期四, 23 一月 2025 08:35:48 +0800
Subject: [PATCH] 特种作业非煤缴费版本导入优化

---
 exam-system/src/main/java/com/gkhy/exam/pay/service/impl/NonCoalPayStudentServiceImpl.java |  178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 174 insertions(+), 4 deletions(-)

diff --git a/exam-system/src/main/java/com/gkhy/exam/pay/service/impl/NonCoalPayStudentServiceImpl.java b/exam-system/src/main/java/com/gkhy/exam/pay/service/impl/NonCoalPayStudentServiceImpl.java
index 86af351..3b78a96 100644
--- a/exam-system/src/main/java/com/gkhy/exam/pay/service/impl/NonCoalPayStudentServiceImpl.java
+++ b/exam-system/src/main/java/com/gkhy/exam/pay/service/impl/NonCoalPayStudentServiceImpl.java
@@ -1,14 +1,27 @@
 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.institutionalaccess.enums.StudentSex;
+import com.gkhy.exam.pay.dto.req.NonCoalPayStuImport;
+import com.gkhy.exam.pay.dto.req.NonCoalPayStudentReqDto;
+import com.gkhy.exam.pay.entity.NonCoalPay;
 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.NonCoalPayStudentService;
-import com.ruoyi.common.utils.DateUtils;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.StringUtils;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 
 /**
@@ -21,6 +34,8 @@
 public class NonCoalPayStudentServiceImpl extends ServiceImpl<NonCoalPayStudentMapper, NonCoalPayStudent> implements NonCoalPayStudentService {
     @Resource
     private NonCoalPayStudentMapper nonCoalPayStudentMapper;
+    @Resource
+    private NonCoalPayMapper nonCoalPayMapper;
 
     /**
      * 查询【请填写功能名称】
@@ -32,6 +47,7 @@
     public NonCoalPayStudent selectNonCoalPayStudentById(Long id) {
         return nonCoalPayStudentMapper.selectNonCoalPayStudentById(id);
     }
+
 
     /**
      * 查询【请填写功能名称】列表
@@ -52,7 +68,12 @@
      */
     @Override
     public int insertNonCoalPayStudent(NonCoalPayStudent nonCoalPayStudent) {
-        nonCoalPayStudent.setCreateTime(DateUtils.getNowDate());
+        checkSameData(nonCoalPayStudent);
+        NonCoalPay nonCoalPay = checkNonCoalPay(nonCoalPayStudent.getNonCoalPayId());
+        nonCoalPayStudent.setPayType(nonCoalPay.getPayPersonType().longValue());
+//        nonCoalPayStudent.setOrderNo(RandomUtil.generateOrderNumber());
+
+        nonCoalPayStudent.setCreateBy(SecurityUtils.getUsername());
         return nonCoalPayStudentMapper.insertNonCoalPayStudent(nonCoalPayStudent);
     }
 
@@ -64,9 +85,65 @@
      */
     @Override
     public int updateNonCoalPayStudent(NonCoalPayStudent nonCoalPayStudent) {
-        nonCoalPayStudent.setUpdateTime(DateUtils.getNowDate());
+        checkData(nonCoalPayStudent.getId());
+        checkSameData(nonCoalPayStudent);
+        nonCoalPayStudent.setPayType(null);
+        nonCoalPayStudent.setOrderNo(null);
+        nonCoalPayStudent.setUpdateBy(SecurityUtils.getUsername());
         return nonCoalPayStudentMapper.updateNonCoalPayStudent(nonCoalPayStudent);
     }
+
+    @Override
+    public int updateNonCoalStu(NonCoalPayStudentReqDto nonCoalPayStudent) {
+
+        if (nonCoalPayStudent.getId() != null) {
+            NonCoalPayStudent nonCoalPayStudent1 = selectNonCoalPayStudentById(nonCoalPayStudent.getId());
+            if (nonCoalPayStudent1 != null) {
+                nonCoalPayStudent1.setPayCode(nonCoalPayStudent.getPayCode());
+                nonCoalPayStudent1.setPayStatus(1L);
+                nonCoalPayStudent1.setUpdateBy(SecurityUtils.getUsername());
+            } else {
+                log.error("修改缴费学员失败,该缴费学员不存在:" + nonCoalPayStudent.getId());
+            }
+        } else {
+            if (nonCoalPayStudent.getNonCoalPayId() != null) {
+                baseMapper.update(null, Wrappers.<NonCoalPayStudent>lambdaUpdate()
+                        .set(NonCoalPayStudent::getUpdateBy, SecurityUtils.getUsername())
+                        .set(NonCoalPayStudent::getPayCode, nonCoalPayStudent.getPayCode())
+                        .set(NonCoalPayStudent::getPayStatus, 1L)
+                        .eq(NonCoalPayStudent::getNonCoalPayId, nonCoalPayStudent.getNonCoalPayId())
+                        .eq(NonCoalPayStudent::getPayStatus, 0L).eq(NonCoalPayStudent::getDelFlag, 0));
+            }
+        }
+        return 0;
+    }
+
+    private void checkSameData(NonCoalPayStudent nonCoalPay) {
+        LambdaQueryWrapper<NonCoalPayStudent> lambdaQueryWrapper = Wrappers.<NonCoalPayStudent>lambdaQuery()
+                .eq(NonCoalPayStudent::getNonCoalPayId, nonCoalPay.getNonCoalPayId())
+                .eq(NonCoalPayStudent::getIdCard, nonCoalPay.getIdCard())
+                .eq(NonCoalPayStudent::getDelFlag, 0);
+        if (nonCoalPay.getId() != null) {
+            lambdaQueryWrapper.ne(NonCoalPayStudent::getId, nonCoalPay.getId());
+        }
+        Long l = baseMapper.selectCount(lambdaQueryWrapper);
+        if (l > 0) {
+            throw new ServiceException("该缴费学员已存在");
+        }
+    }
+
+    @Override
+    public void checkHavePayData(Long nonPayId) {
+        LambdaQueryWrapper<NonCoalPayStudent> lambdaQueryWrapper = Wrappers.<NonCoalPayStudent>lambdaQuery()
+                .eq(NonCoalPayStudent::getNonCoalPayId, nonPayId)
+                .eq(NonCoalPayStudent::getDelFlag, 0).eq(NonCoalPayStudent::getPayStatus, 1L);
+
+        Long l = baseMapper.selectCount(lambdaQueryWrapper);
+        if (l > 0) {
+            throw new ServiceException("已开始缴费不可修改");
+        }
+    }
+
 
     /**
      * 批量删除【请填写功能名称】
@@ -87,6 +164,99 @@
      */
     @Override
     public int deleteNonCoalPayStudentById(Long id) {
-        return nonCoalPayStudentMapper.deleteNonCoalPayStudentById(id);
+        checkData(id);
+        return nonCoalPayStudentMapper.deleteNonCoalPayStudentById(id, SecurityUtils.getUsername());
+    }
+
+    @Override
+    public String importData(List<NonCoalPayStuImport> nonCoalPayStudents, String operName, Long nonCoalPayId) {
+        if (StringUtils.isNull(nonCoalPayStudents) || nonCoalPayStudents.isEmpty()) {
+            throw new ServiceException("导入数据不能为空!");
+        }
+        NonCoalPay nonCoalPay = checkNonCoalPay(nonCoalPayId);
+        Integer payPersonType = nonCoalPay.getPayPersonType();
+        Set<String> collect = nonCoalPayStudents.stream()
+                .map(NonCoalPayStuImport::getIdCard)
+                .collect(Collectors.toSet());
+        if (collect.size() != nonCoalPayStudents.size()) {
+            throw new ServiceException("导入数据不能有重复身份证号!");
+        }
+        Map<String, String> idCardToNameMap = new HashMap<>();
+        NonCoalPayStudent student = new NonCoalPayStudent();
+        student.setNonCoalPayId(nonCoalPayId);
+
+        List<NonCoalPayStudent> nonCoalPayStudents1 = nonCoalPayStudentMapper.selectNonCoalPayStudentList(student);
+        if (nonCoalPayStudents1 != null && !nonCoalPayStudents1.isEmpty()) {
+            idCardToNameMap = nonCoalPayStudents1.stream()
+                    .collect(Collectors.toMap(
+                            NonCoalPayStudent::getIdCard,
+                            NonCoalPayStudent::getIdCard
+                    ));
+        }
+        int successNum = 0;
+        int failureNum = 0;
+        StringBuilder successMsg = new StringBuilder();
+        StringBuilder failureMsg = new StringBuilder();
+        for (NonCoalPayStuImport stu : nonCoalPayStudents) {
+            try {
+                if (StringUtils.isEmpty(stu.getIdCard()) || StringUtils.isEmpty(stu.getName()) || StringUtils.isEmpty(stu.getPhone()) || stu.getSex() == null) {
+                    failureMsg.append("<br/>" + failureNum + "、导入数据为空: " + stu.getIdCard() + " 数据错误");
+                } else if (idCardToNameMap.get(stu.getIdCard().trim()) != null) {
+                    failureMsg.append("<br/>" + failureNum + "、身份证号: " + stu.getIdCard() + " 数据已存在");
+                } else {
+                    NonCoalPayStudent stu1 = new NonCoalPayStudent();
+                    stu1.setPayStatus(0L);
+                    stu1.setPayType(payPersonType.longValue());
+                    stu1.setNonCoalPayId(nonCoalPayId);
+                    stu1.setCreateBy(operName);
+                    stu1.setName(stu.getName());
+                    stu1.setIdCard(stu.getIdCard());
+                    stu1.setPhone(stu.getPhone());
+               
+//                    stu1.setOrderNo(RandomUtil.generateOrderNumber());
+                    stu1.setSex(StudentSex.BOY.getStatus().equals(stu.getSex().trim()) ? 0L : 1L);
+                    nonCoalPayStudentMapper.insertNonCoalPayStudent(stu1);
+                    successNum++;
+                    successMsg.append("<br/>" + successNum + "、身份证号 " + stu.getIdCard() + " 导入成功");
+                    idCardToNameMap.put(stu.getIdCard(), stu.getIdCard());
+                }
+
+            } catch (Exception e) {
+                failureNum++;
+                String msg = "<br/>" + failureNum + "、身份证号 " + stu.getIdCard() + " 导入失败:";
+                failureMsg.append(msg + e.getMessage());
+                log.error(msg, e);
+            }
+        }
+        if (failureNum > 0) {
+            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
+            throw new ServiceException(failureMsg.toString());
+        } else {
+            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
+        }
+        return successMsg.toString();
+    }
+
+    private NonCoalPayStudent checkData(Long id) {
+        NonCoalPayStudent byId = nonCoalPayStudentMapper.selectNonCoalPayStudentById(id);
+        if (byId == null) {
+            throw new RuntimeException("该数据不存在");
+        }
+        if (byId.getDelFlag() == 2) {
+            throw new RuntimeException("该数据已删除");
+        }
+        if (byId.getPayStatus() == 1) {
+            throw new RuntimeException("该数据已缴费,不可操作");
+        }
+        return byId;
+    }
+
+
+    private NonCoalPay checkNonCoalPay(Long nonCoalPayId) {
+        NonCoalPay nonCoalPay = nonCoalPayMapper.selectNonCoalPayById(nonCoalPayId);
+        if (nonCoalPay == null) {
+            throw new ServiceException("该主数据不存在");
+        }
+        return nonCoalPay;
     }
 }
\ No newline at end of file

--
Gitblit v1.9.2