教育训练处考试制证系统后端
“djh”
2025-04-24 f9cb0f3f384279b2cbe87c35dde3ba573a0197f3
exam-system/src/main/java/com/gkhy/exam/pay/service/impl/CoalPayServiceImpl.java
@@ -1,18 +1,17 @@
package com.gkhy.exam.pay.service.impl;
import com.alibaba.fastjson2.JSONObject;
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.CoalPayDto;
import com.gkhy.exam.pay.dto.req.CoalPayReq;
import com.gkhy.exam.pay.dto.req.CoalPayStudentReq;
import com.gkhy.exam.pay.dto.req.CoalPayTypeReq;
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.BillSignException;
import com.gkhy.exam.pay.utils.PayUtils;
import com.gkhy.exam.pay.utils.ResultVo;
import com.ruoyi.common.constant.ResultConstants;
@@ -21,9 +20,8 @@
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.aspectj.lang.reflect.UnlockSignature;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
@@ -33,14 +31,15 @@
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.concurrent.locks.Lock;
import java.util.stream.Collectors;
@Service
@Slf4j
public class CoalPayServiceImpl extends ServiceImpl<CoalPayMapper, CoalPay> implements CoalPayService {
    @Resource
@@ -58,37 +57,39 @@
    @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);
//        List<CoalPayRepDto> coalPayRepDtos = new ArrayList<>();
        List<CoalPayRepDto> coalPays = coalPayMapper.selectCoalPayList(coalPay);
        for (CoalPayRepDto pay : coalPays) {
//            CoalPayRepDto coalPayRepDto = new CoalPayRepDto();
//            BeanUtils.copyProperties(pay, coalPayRepDto);
            //部门数据
            SysDept sysDept = sysDeptMapper.selectDeptById(pay.getDeptId());
            coalPayRepDto.setDeptName(sysDept.getDeptName());
            pay.setDeptName(sysDept.getDeptName());
            //工种数据
            List<CoalCategory> coalCategories = coalCategoryMapper.selectByCoalPayId(pay.getId());
            coalPayRepDto.setCoalCategoryList(coalCategories);
            pay.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);
//            List<CoalPayStudent> coalPayStudents = coalPayStudentService.selectByCoalPayId(pay.getId());
//            List<CoalPayStudent> havePay = coalPayStudents.stream()
//                    .filter(stu -> stu.getPayStatus() != null && stu.getPayStatus() == 1)
//                    .collect(Collectors.toList());
//            pay.setTotalNum(coalPayStudents.size());
//            pay.setHavePayNum(havePay.size());
//            coalPayRepDtos.add(coalPayRepDto);
        }
        return coalPayRepDtos;
        return coalPays;
    }
    @Override
@@ -98,7 +99,7 @@
        //基本数据
        CoalPay coalPay = coalPayMapper.selectById(id);
        BeanUtils.copyProperties(coalPay,coalPayRepDto);
        BeanUtils.copyProperties(coalPay, coalPayRepDto);
        //考点名称
        SysDept sysDept = sysDeptMapper.selectDeptById(coalPay.getDeptId());
        coalPayRepDto.setDeptName(sysDept.getDeptName());
@@ -120,12 +121,12 @@
    @Override
    public int insertCoalPay(CoalPayDto coalPayDto) {
        CoalPay coalPay = new CoalPay();
        BeanUtils.copyProperties(coalPayDto,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,"工种类别不能为空");
        if (CollectionUtils.isEmpty(coalPayDto.getCoalPayCategoryies())) {
            throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "工种类别不能为空");
        }
        List<CoalPayCategory> coalPayCategories = coalPayDto.getCoalPayCategoryies();
        for (CoalPayCategory coalPayCategory : coalPayCategories) {
@@ -137,18 +138,19 @@
    /**
     * 修改缴费信息
     *
     * @param coalPayDto
     * @return
     */
    @Override
    public int updateCoalPay(CoalPayDto coalPayDto) {
        CoalPay coalPay = new CoalPay();
        BeanUtils.copyProperties(coalPayDto,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){
        if (update > 0) {
            List<CoalPayCategory> coalPayCategories = coalPayDto.getCoalPayCategoryies();
            for (CoalPayCategory coalPayCategory : coalPayCategories) {
                coalPayCategory.setCoalPayId(coalPay.getId());
@@ -162,12 +164,12 @@
    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,"已有学员完成缴费,请勿删除");
            if (!CollectionUtils.isEmpty(coalPayStudents)) {
                throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "已有学员完成缴费,请勿删除");
            }
        }
        int i = coalPayMapper.updateByIds(ids);
        if (i>0){
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
@@ -175,7 +177,7 @@
    //个人查询缴费
    @Override
    public List<CoalPayStudentRep> selectCoalPay(String idcard,String phone) {
    public List<CoalPayStudentRep> selectCoalPay(String idcard, String phone) {
        CoalPayStudentReq coalPayStudentReq = new CoalPayStudentReq();
        coalPayStudentReq.setIdCard(idcard);
        coalPayStudentReq.setPhone(phone);
@@ -185,16 +187,19 @@
        for (CoalPayStudent payStudent : coalPayStudents) {
            //封装学生基础信息
            CoalPayStudentRep coalPayStudentRep = new CoalPayStudentRep();
            coalPayStudentRep.setId(payStudent.getId());
            coalPayStudentRep.setName(payStudent.getName());
            coalPayStudentRep.setIdCard(payStudent.getIdCard());
            coalPayStudentRep.setPhone(payStudent.getPhone());
            coalPayStudentRep.setSex(payStudent.getSex());
            coalPayStudentRep.setPayType(payStudent.getPayType());
            coalPayStudentRep.setPayCode(payStudent.getPayCode());
            coalPayStudentRep.setPayStatus(payStudent.getPayStatus());
            //查询对应批次以及批次包含工种类别
            CoalPay coalPay = coalPayMapper.selectById(payStudent.getCoalPayId());
            CoalPayRepDto coalPayRepDto = new CoalPayRepDto();
            BeanUtils.copyProperties(coalPay,coalPayRepDto);
            BeanUtils.copyProperties(coalPay, coalPayRepDto);
            //考点名称
            SysDept sysDept = sysDeptMapper.selectDeptById(coalPay.getDeptId());
            coalPayRepDto.setDeptName(sysDept.getDeptName());
@@ -210,74 +215,239 @@
    @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,"已为团体缴费,不可更改");
        if (coalPayTypeReq.getPayPersonType() != null && byId.getPayPersonType() == 2) {
            throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "已为团体缴费,不可更改");
        }
        return coalPayMapper.updateByPayId(coalPayTypeReq);
        int i = coalPayMapper.updateByPayId(coalPayTypeReq);
        if (i > 0) {
            CoalPayStudent coalPayStudent = new CoalPayStudent();
            coalPayStudent.setCoalPayId(coalPayTypeReq.getCoalPayId());
            coalPayStudent.setPayType(2);
            coalPayStudent.setGovPayStatus(0);
            coalPayStudentService.updateByCoalPayId(coalPayStudent);
        }
        return i;
    }
    @Override
    public String personPayMoney(Long coalPayId, Long studentId) {
        CoalPayStudent coalPayStudent = coalPayStudentService.selectbyId(studentId);
        CoalPay coalPay = coalPayMapper.selectById(coalPayId);
        List<CoalCategory> coalCategories = coalCategoryMapper.selectByCoalPayId(coalPayId);
        CoalTicket coalTicket = coalCategoryMapper.selectCoalTicket();
        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, "请勿重新缴费");
            }
            if (student.getGovPayStatus() == 2){
                return student.getOrderId();
            }
            PayReqData payReqData = fillData(coalPayStudent, coalPay, coalCategories, coalTicket, 1, null);
            ResultVo resultVo = payUtils.faqiV2(payReqData);
            if (resultVo.getRespcode().equals("BUS0000")) {
                //进行票据签名并校验
//                ResultVo resultVo1 = payUtils.uploadXml(resultVo.getRespdata().getOrderNo(), resultVo.getRespdata().getFileData());
//              if (!resultVo1.getRespcode().equals("BUS0000")){
//                  throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "签名验证错误");
//              }
                payStudent.setId(studentId);
                payStudent.setOrderNo(resultVo.getRespdata().getOrderNo());
                payStudent.setOrderId(resultVo.getRespdata().getOrderId());
                payStudent.setGovPayStatus(2);
                payStudent.setPayCode(resultVo.getRespdata().getBillNo());
                coalPayStudentService.updateByIdAndPayType(payStudent);
                return resultVo.getRespdata().getOrderId();
            } else {
                throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, resultVo.getRespmsg());
            }
        } catch (BusinessException 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, "未找到相关缴费信息");
            }
            if (coalPayStudents.get(0).getGovPayStatus() == 2){
                return coalPayStudents.get(0).getOrderId();
            }
            CoalPayStudent payStudent = new CoalPayStudent();
            PayReqData payReqData = fillData(coalPayStudents, coalPay, coalCategories, coalTicket, 2, coalTeamPayReq);
            ResultVo resultVo = payUtils.faqiV2(payReqData);
            if (resultVo.getRespcode().equals("BUS0000")) {
                payStudent.setCoalPayId(coalPay.getId());
                payStudent.setOrderId(resultVo.getRespdata().getOrderId());
                payStudent.setOrderNo(resultVo.getRespdata().getOrderNo());
                payStudent.setGovPayStatus(2);
                payStudent.setPayCode(resultVo.getRespdata().getBillNo());
                coalPayStudentService.updateByCoalPayIdAndStatus(payStudent);
                CoalPay coalPay1 = new CoalPay();
                coalPay1.setPayCompanyCard(coalTeamPayReq.getPayCompanyCard());
                coalPay1.setPayCompanyName(coalTeamPayReq.getPayCompanyName());
                coalPay1.setId(coalTeamPayReq.getCoalPayId());
                coalPay1.setPayPersonType(2);
                coalPayMapper.updateCoalPayById(coalPay1);
                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();
            }
        }
    }
    @Override
    public ResultVo topay() throws IOException, BillSignException {
        PayReqData payReqData = new PayReqData();
        payReqData.setOrderNo(RandomUtil.generateOrderNumber());
        payReqData.setMoney(coalPay.getAmount());
        PayReqData.Feedata feedatas = new PayReqData.Feedata();
        //订单编号
        payReqData.setOrderNo(RandomUtil.generateOrderNumber(1L, "NC"));
        //订单总金额
        payReqData.setMoney(BigDecimal.valueOf(203));
        //子订单数目
        payReqData.setAmount(1);
        payReqData.setPayerName(coalPayStudent.getName());
        payReqData.setCertNo(coalPayStudent.getIdCard());
        //缴费人姓名(单位填单位名称)阿克苏地区博安煤矿安全技术服务中心
        payReqData.setPayerName("杨治国");
        //缴费人证件号(单位填同一信用代码)52652900789893140A
        payReqData.setCertNo("610481199801166233");
        //缴款人类型(1个人  2单位)
        payReqData.setPayerType(1);
        //开票单位社会信用代码12650000MB1A9612XD
        payReqData.setInvoiceSocialCode("11650100745216623A");
        //开票人
        payReqData.setHandlingPerson("朱晓明");
        //复核人
        payReqData.setChecker("董莹");
        //单位编码547185129
        payReqData.setEnterCode("482911639");
        //订单描述(非必填)
        payReqData.setDesc("非煤安全作业实操考试_焊接与热切割作业-012413");
        //订单明细
        List<PayReqData.Feedata> feedatas1 = new ArrayList<>();
        //数量
        feedatas.setAmount(1);
        //业务代码
        feedatas.setBusCode("DZ012413");
        //单价
        feedatas.setPrice(BigDecimal.valueOf(203));
        PayReqData.Feedata feedata = new PayReqData.Feedata();
//        feedata.setBusCode("DZ011574");
//        feedata.setAmount(1);
//        feedata.setPrice(BigDecimal.valueOf(103));
        feedatas1.add(feedatas);
//        feedatas1.add(feedata);
        payReqData.setFeeDatas(feedatas1);
        log.info("请求参数:" + JSONObject.toJSONString(payReqData));
        PayUtils payUtils = new PayUtils();
        ResultVo resultVo = payUtils.faqiV2(payReqData);
        if (!resultVo.getRespcode().equals("BUS0000")) {
            throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "签名验证错误");
        } else {
            //todo 业务处理
        }
        log.info("请求结果:" + JSONObject.toJSONString(resultVo));
//        String fileData = resultVo.getRespdata().getFileData();
//        //票据原文转为byte字节文件
//        byte[] decode = Base64.getDecoder().decode(fileData);
//        //byte字节文件转为xml字符串
//        String xmlString = new String(decode, StandardCharsets.UTF_8);
//        Document document = null;
//        try {
//            document = DocumentHelper.parseText(xmlString);
//        } catch (DocumentException e) {
//            throw new RuntimeException(e);
//        }
//        log.info("票据原文为:" + document.asXML());
//        BillSign billSign = new BillSign();
//        String s = billSign.readRefSignDto(document);
//        Element rootElement = document.getRootElement();
//        SignResult sign = payUtils.sign(s);
//        MOFVerifyResult verify = payUtils.verify(sign.getSignData(), s);
//        SignDto signDto = new SignDto(verify.getSignTime(), new String(Base64.getEncoder().encode(sign.getSignData())), verify.getIssure(), verify.getSn());
//        log.info("拼接对象为:" + JSONObject.toJSONString(signDto));
//        Sign sign1 = new Sign();
//        Document signature = sign1.getSignature(signDto);
////        Element rootElement1 = signature.getRootElement();
//        log.info("拼接结果为:" + signature.asXML());
//        rootElement.add(signature.getRootElement());
////        EnvelopResult envelopResult = payUtils.encryptEnvelop(document.asXML().getBytes());
////        log.info("制作数字信封为:"+ Arrays.toString(envelopResult.getEnvelopData()));
//        log.info("签名后票据为:" + document.asXML());
//        ResultVo resultVo1 = payUtils.uploadXml(resultVo.getRespdata().getOrderNo(), document.asXML());
        return resultVo;
    }
    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());
        payReqData.setInvoiceSocialCode(coalTicket.getTicketCompanyCode());
        payReqData.setHandlingPerson(coalTicket.getDrawer());
        payReqData.setChecker(coalTicket.getCheck());
        payReqData.setEnterCode(coalTicket.getCompanyCode());
        payReqData.setDesc(coalPay.getBatchName());
        PayReqData.Feedata feedata = new PayReqData.Feedata();
        List<PayReqData.Feedata> feedatas = new ArrayList<>();
        for (CoalCategory coalCategory : coalCategories) {
            feedata.setAmount(1);
            feedata.setPrice(coalCategory.getAmount());
            PayReqData.Feedata feedata = new PayReqData.Feedata();
            feedata.setAmount(coalPayStudent.size());
            feedata.setPrice(coalCategory.getAmount().multiply(BigDecimal.valueOf(coalPayStudent.size())));
            feedata.setBusCode(coalCategory.getBusinessCode());
            feedatas.add(feedata);
        }
        payReqData.setFeeDatas(feedatas);
        CoalPayStudent payStudent = new CoalPayStudent();
        RLock lock = redissonClient.getLock("SWSPKMAS_PAY_PERSON");
        try {
            lock.lock(10, TimeUnit.SECONDS);
            if (coalPayStudent.getGovPayStatus()!=0){
                throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"请勿重复发起支付");
            }
            if (StringUtils.isEmpty(coalPayStudent.getPayCode())){
            }
            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().getBillNo();
            }else {
                throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"发起支付失败,请稍后重试");
            }
        }catch (BusinessException e){
            throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"请稍后重试");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(lock.isLocked()){
                lock.unlock();
            }
        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;
    }
}