liusheng
2024-05-08 39405c053391f29d2f2c0e2c3104bea111e0e460
ruoyi-project/src/main/java/com/ruoyi/project/service/impl/ServiceFundServiceImpl.java
@@ -1,41 +1,41 @@
package com.ruoyi.project.service.impl;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.tax.TaxtUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.HttpClientKit;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.DtoConversionUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.domain.vo.*;
import com.ruoyi.project.mapper.*;
import com.ruoyi.project.service.IServiceFundSharedService;
import com.ruoyi.project.service.IServiceFunddetailService;
import com.ruoyi.project.service.IServiceFunddetailSharedService;
import com.ruoyi.project.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
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.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.service.IServiceFundService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 费用申请主Service业务层处理
@@ -73,6 +73,15 @@
    @Autowired
    private IServiceFunddetailService serviceFunddetailService;
    @Autowired
    private VExpertfeeExpertMapper vExpertfeeExpertMapper;
    @Autowired
    private VExpertfeeDonorMapper vExpertfeeDonorMapper;
    @Autowired
    private VExpertfeeTotalMapper vExpertfeeTotalMapper;
    /**
     * 查询费用申请主列表(包含删除的)
@@ -88,6 +97,9 @@
        }
        if (serviceFund.getId() != null) {
            wrappers.eq(ServiceFund::getId, serviceFund.getId());
        }
        if (serviceFund.getFundTaxId() != null) {
            wrappers.eq(ServiceFund::getFundTaxId, serviceFund.getFundTaxId());
        }
        if (StringUtils.isNotBlank(serviceFund.getUserno())) {
            wrappers.eq(ServiceFund::getUserno, serviceFund.getUserno());
@@ -110,8 +122,14 @@
        if (serviceFund.getUploadtime() != null) {
            wrappers.eq(ServiceFund::getUploadtime, serviceFund.getUploadtime());
        }
        if (serviceFund.getUploadStates() != null) {
            wrappers.eq(ServiceFund::getUploadStates, serviceFund.getUploadStates());
        }
        if (serviceFund.getFlowlevel() != null) {
            wrappers.eq(ServiceFund::getFlowlevel, serviceFund.getFlowlevel());
        }
        if (serviceFund.getJxrq() != null) {
            wrappers.eq(ServiceFund::getJxrq, serviceFund.getJxrq());
        }
        return this.list(wrappers);
    }
@@ -140,8 +158,18 @@
    }
    @Override
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public Boolean addFundSharedInfo(Long id) {
        log.info("fund表的id:{}", id);
        //判断一下,share表是否已经存在了
        Map<String, Object> map = new HashMap<>();
        map.put("serfunid", id);
        map.put("del_flag", 0);
        List<ServiceFundShared> serviceFundSharedList = serviceFundSharedMapper.selectByMap(map);
        if (!CollectionUtils.isEmpty(serviceFundSharedList)) {
            return true;
        }
        ServiceFund serviceFund = serviceFundMapper.selectById(id);
        ServiceFundShared serviceFundShared = DtoConversionUtils.sourceToTarget(serviceFund, ServiceFundShared.class);
        serviceFundShared.setSerfunid(serviceFundShared.getId());
@@ -161,7 +189,17 @@
            serviceFunddetailShareds.get(i).setFundid(serviceFundShared.getId());
            serviceFunddetailShareds.get(i).setId(null);
            //数据发送给财务
            uploadOAFileAndUpdateDb(serviceFunddetailShareds.get(i));
            try {
                uploadOAFileAndUpdateDb(serviceFunddetailShareds.get(i));
            } catch (Exception e) {
                e.printStackTrace();
                log.error("Exception中入参的ID为:{},异常信息为 : {}", id, e.getMessage());
                if (id != null) {
                    Boolean aBoolean = sharedService.delfundsharedInfoById(null, id);
                    log.error("fund2分享表里的数据是否删除成功:{}", aBoolean);
                }
                return false;
            }
        }
        boolean result = false;
        for (ServiceFunddetailShared serviceFunddetailShared : serviceFunddetailShareds) {
@@ -669,9 +707,13 @@
    }
    @Override
    public List<SpFinancialExpensesFundOut> getExpertfeeList(SpSelectExpertfee spSelectExpertfee) {
        return serviceFundMapper.getExpertfeeList(spSelectExpertfee.getPaApplyType(), spSelectExpertfee.getDonorname(), spSelectExpertfee.getPacheckno(), spSelectExpertfee.getCheckstatus(), spSelectExpertfee.getPadepartno(), spSelectExpertfee.getPausername());
    }
    @Override
    public int countItem(Long infoid, Long itemid) {
        List<ServiceFund> serviceFunds = serviceFundMapper.getInfoByInfoId(infoid);
        int count = 0;
        for (ServiceFund s : serviceFunds) {
            long fundid = s.getId();
@@ -687,32 +729,50 @@
    public Long addOrUpdateNew(ServiceFundVO serviceFundVO) {
        Long id = null;
        //将数据保存到fund表
        serviceFundVO.setUploadStates(0);
        id = saveFund(serviceFundVO);
        List<ServiceFunddetailVO> serviceFunddetails = serviceFundVO.getServiceFunddetails();
        if (CollectionUtils.isEmpty(serviceFunddetails)) {
            System.out.println("serviceFunddetails为空喽");
            return null;
        }
        // 保存详情数据
        for (ServiceFunddetailVO serviceFunddetailVO : serviceFunddetails) {
            serviceFunddetailVO.setFundid(id);
            //將附件转成json
            if (!CollectionUtils.isEmpty(serviceFunddetailVO.getAnnexfilesList())) {
                serviceFunddetailVO.setAnnexfiles(JSON.toJSONString(serviceFunddetailVO.getAnnexfilesList()));
            }
            if (!CollectionUtils.isEmpty(serviceFunddetailVO.getInvoicefilesList())) {
                serviceFunddetailVO.setInvoicefiles(JSON.toJSONString(serviceFunddetailVO.getInvoicefilesList()));
            }
            ServiceFunddetail serviceFunddetail = DtoConversionUtils.sourceToTarget(serviceFunddetailVO, ServiceFunddetail.class);
            if (serviceFunddetailVO.getId() == null) {
                serviceFunddetailService.save(serviceFunddetail);
            } else {
                serviceFunddetailService.updateById(serviceFunddetail);
        BigDecimal bigDecimal = new BigDecimal(0.0);
        if (!CollectionUtils.isEmpty(serviceFunddetails)) {
            // 保存详情数据
            for (ServiceFunddetailVO serviceFunddetailVO : serviceFunddetails) {
                //去掉身份证的首尾空格
                if (StringUtils.isNotEmpty(serviceFunddetailVO.getIdcardno())) {
                    serviceFunddetailVO.setIdcardno(serviceFunddetailVO.getIdcardno().trim());
                }
                if (StringUtils.isEmpty(serviceFunddetailVO.getBankcardno())) {
                    throw new BaseException("请检查银行卡号是否为空,姓名:" + serviceFunddetailVO.getBeneficiaryname());
                }
                serviceFunddetailVO.setFundid(id);
                //將附件转成json
                if (!CollectionUtils.isEmpty(serviceFunddetailVO.getAnnexfilesList())) {
                    serviceFunddetailVO.setAnnexfiles(JSON.toJSONString(serviceFunddetailVO.getAnnexfilesList()));
                } else {
                    serviceFunddetailVO.setAnnexfiles(null);
                }
                if (!CollectionUtils.isEmpty(serviceFunddetailVO.getInvoicefilesList())) {
                    serviceFunddetailVO.setInvoicefiles(JSON.toJSONString(serviceFunddetailVO.getInvoicefilesList()));
                } else {
                    serviceFunddetailVO.setInvoicefiles(null);
                }
                ServiceFunddetail serviceFunddetail = DtoConversionUtils.sourceToTarget(serviceFunddetailVO, ServiceFunddetail.class);
                if (serviceFunddetailVO.getId() == null) {
                    serviceFunddetailService.save(serviceFunddetail);
                } else {
                    serviceFunddetailService.updateById(serviceFunddetail);
                }
                bigDecimal = bigDecimal.add(BigDecimal.valueOf(serviceFunddetailVO.getAmount()));
            }
        }
        serviceFundVO.setPretaxcost(bigDecimal.doubleValue());
        serviceFundMapper.updateById(serviceFundVO);
        return id;
    }
@@ -753,17 +813,23 @@
    }
    @Override
    public Boolean totaltax(TotalTaxVO totalTaxVO) {
    public Map<String, List<ServiceFunddetailExcel>> totaltax(TotalTaxVO totalTaxVO) {
        if (CollectionUtils.isEmpty(totalTaxVO.getFundids())) {
            throw new BaseException("请勾选之后,再进行合计个税");
        }
        Map<String, List<ServiceFunddetailExcel>> map = new HashMap<>();
        if (totalTaxVO.getIsTaxAfter() == null) {
            //如何为空,默认当没有算过税,进行合计
            totalTaxVO.setIsTaxAfter(0);
        }
        List<ServiceFunddetail> serviceFunddetails = serviceFunddetailMapper.totlaTax(totalTaxVO.getFundids());
        if (CollectionUtils.isEmpty(serviceFunddetails)) {
            throw new BaseException("查询的详情数据为空");
        }
        //用于返回excel
        List<ServiceFunddetailExcel> serviceFunddetailList = new ArrayList<>();
        //打印excel
        ExcelUtil<ServiceFunddetailExcel> util = new ExcelUtil<ServiceFunddetailExcel>(ServiceFunddetailExcel.class);
        if (totalTaxVO.getIsTaxAfter() == 1) {
            //通过受益人名称,将税前、税后的值相加
@@ -788,10 +854,13 @@
                }
                serviceFunddetailList.add(serviceFunddetailExcel);
            }
            //打印算过税之后的,合计excel
            util.exportExcel(serviceFunddetailList, "合计个税(税后)");
            //返回算过税之后的,合计
            map.put("合计个税(税后)", serviceFunddetailList);
            return map;
        } else {
            a : for (ServiceFunddetail serviceFunddetail : serviceFunddetails) {
            a:
            for (ServiceFunddetail serviceFunddetail : serviceFunddetails) {
                ServiceFunddetailExcel sfeBefore = new ServiceFunddetailExcel();
                ServiceFunddetailExcel sfeAfter = new ServiceFunddetailExcel();
@@ -804,7 +873,14 @@
                for (ServiceFunddetail serviceFunddetail2 : serviceFunddetails) {
                    if (serviceFunddetail.getBeneficiaryname().trim().equals(serviceFunddetail2.getBeneficiaryname().trim()) && !(serviceFunddetail2.getServicesscopename().trim().equals("税后"))) {
                    if (StringUtils.isEmpty(serviceFunddetail2.getServicesscopename())) {
                        ServiceFund serviceFund = null;
                        if (serviceFunddetail2.getFundid() == null) {
                            serviceFund = serviceFundMapper.selectById(serviceFunddetail2.getFundid());
                        }
                        throw new BaseException("服务说明为空,请检查后再计算,捐献者为 :" + serviceFund.getDonorname());
                    }
                    if (serviceFunddetail.getBeneficiaryname().equals(serviceFunddetail2.getBeneficiaryname()) && !(serviceFunddetail2.getServicesscopename().equals("税后"))) {
                        //累计税前
                        sfeBefore.setIdcardno(serviceFunddetail2.getIdcardno());
                        sfeBefore.setBankcardno(serviceFunddetail2.getBankcardno());
@@ -820,7 +896,7 @@
                        }
                        sfeBefore.setTaxedamount(null);
                    } else if (serviceFunddetail.getBeneficiaryname().trim().equals(serviceFunddetail2.getBeneficiaryname().trim()) && serviceFunddetail2.getServicesscopename().trim().equals("税后")) {
                    } else if (serviceFunddetail.getBeneficiaryname().equals(serviceFunddetail2.getBeneficiaryname()) && serviceFunddetail2.getServicesscopename().equals("税后")) {
                        //累计税后
                        sfeAfter.setIdcardno(serviceFunddetail2.getIdcardno());
                        sfeAfter.setBankcardno(serviceFunddetail2.getBankcardno());
@@ -844,14 +920,46 @@
                if (ObjectUtils.isNotEmpty(sfeAfter.getBeneficiaryname())) serviceFunddetailList.add(sfeAfter);
            }
            //打印算税之前的,合计excel
            util.exportExcel(serviceFunddetailList, "合计个税(税前)");
            //返回算税之前的,合计
            map.put("合计个税(税前)", serviceFunddetailList);
            return map;
        }
        return true;
    }
    @Override
    public Map<String, Object> exportFeeSum(Integer faxId) {
        List<VExpertfeeTotal> list = vExpertfeeTotalMapper.selectVExpertfeeTotalListByTaxId(faxId);
        ExcelUtil<VExpertfeeTotal> util = new ExcelUtil<VExpertfeeTotal>(VExpertfeeTotal.class);
        AjaxResult ajaxResult = util.exportExcel(list, "vExpertfeeTotal");
        String filePath = RuoYiConfig.getDownloadPath() + ajaxResult.get("msg");
        List<VExpertfeeDonor> list2 = vExpertfeeDonorMapper.selectVExpertfeeDonorList2(faxId);
        ExcelUtil<VExpertfeeDonor> util2= new ExcelUtil<VExpertfeeDonor>(VExpertfeeDonor.class);
        AjaxResult ajaxResult2 = util2.exportExcel(list2, "VExpertfeeDonor");
        List<VExpertfeeExpert> list3 = vExpertfeeExpertMapper.selectVExpertfeeExpertListByFaxId(faxId);
        ExcelUtil<VExpertfeeExpert> util3 = new ExcelUtil<VExpertfeeExpert>(VExpertfeeExpert.class);
        AjaxResult ajaxResult3 = util3.exportExcel(list3, "vExpertfeeExpert");
        List<String> list1 = new ArrayList<>();
        list1.add(ajaxResult.get("msg").toString());
        list1.add(ajaxResult2.get("msg").toString());
        list1.add(ajaxResult3.get("msg").toString());
        String name = "专家费汇总单" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xlsx";
        mergeExcel(list1, RuoYiConfig.getDownloadPath() + name);
        for (String path : list1) {
            new File(RuoYiConfig.getDownloadPath() + path).delete();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("downloadUrl", "/profile/download/" + name);
        map.put("downloadName", name);
        return map;
    }
    int uploadOAFileAndUpdateDb(ServiceFunddetailShared remShare) {
    int uploadOAFileAndUpdateDb(ServiceFunddetailShared remShare) throws Exception {
        //上传OA文件
        //String strUrl = "http://129.88.242.39:8899/seeyon/rest/token?userName=opo&password=127814f8-84e8-4304-84a5-a71573567efd&loginName=demo3";
        String strUrl = "http://129.88.242.39:8899/seeyon/rest/token";
@@ -891,7 +999,7 @@
    }
    private Integer uploadFile(String filePath, String strFUrl, List<RbDetailFile> parseArray2, List<RbDetailFile> parseArray, ServiceFunddetailShared remShare, String flag) {
    private Integer uploadFile(String filePath, String strFUrl, List<RbDetailFile> parseArray2, List<RbDetailFile> parseArray, ServiceFunddetailShared remShare, String flag) throws Exception {
        if (!CollectionUtils.isEmpty(parseArray)) {
            for (int i = 0; i < parseArray.size(); i++) {
                RbDetailFile rbDetailFile = parseArray.get(i);
@@ -903,24 +1011,24 @@
                System.out.println("fund服务的filePath + substring: " + filePath + substring);
                File filetest = new File(filePath + substring);
                try {
                    String struploadResult = HttpClientKit.sendPostWithFile(filetest, strFUrl);
                    log.info("第三方传回的数据: {}", struploadResult);
                    if (StringUtils.isEmpty(struploadResult)) {
                        log.error("HttpClientKit.sendPostWithFile 请求为空了 filetest:{},  strFUrl:{} ", filetest, strFUrl);
                        return 0;
                    }
                    //获取fileid
                    JSONObject jsonR = JSONObject.parseObject(struploadResult);
                    JSONArray jsonArr = jsonR.getJSONArray("atts");
                    log.info("第三方传回的数据获取的atts : {}", jsonArr);
                    for (int j = 0; j < jsonArr.size(); j++) {
                        JSONObject jsonRet = jsonArr.getJSONObject(j);
                        rbDetailFile.setFileid(jsonRet.get("fileUrl").toString());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                String struploadResult = HttpClientKit.sendPostWithFile(filetest, strFUrl);
                log.info("第三方传回的数据: {}", struploadResult);
                if (StringUtils.isEmpty(struploadResult)) {
                    log.error("HttpClientKit.sendPostWithFile 请求为空了 filetest:{},  strFUrl:{} ", filetest, strFUrl);
                    return 0;
                }
                //获取fileid
                JSONObject jsonR = JSONObject.parseObject(struploadResult);
                JSONArray jsonArr = jsonR.getJSONArray("atts");
                log.info("第三方传回的数据获取的atts : {}", jsonArr);
                for (int j = 0; j < jsonArr.size(); j++) {
                    JSONObject jsonRet = jsonArr.getJSONObject(j);
                    if (StringUtils.isEmpty(jsonRet.get("fileUrl").toString())) {
                        throw new BaseException("fileID为空,请联系第三方处理");
                    }
                    rbDetailFile.setFileid(jsonRet.get("fileUrl").toString());
                }
                parseArray2.add(rbDetailFile);
            }
            if (flag.equals("1")) {
@@ -1041,4 +1149,46 @@
        return id;
    }
    public void mergeExcel(List<String> files, String outputFile) {
        try {
            Workbook resultWorkbook = new XSSFWorkbook();
            for (String file : files) {
                InputStream is1 = new FileInputStream(RuoYiConfig.getDownloadPath() + file);
                Workbook workbook1 = new XSSFWorkbook(is1);
                Sheet sheet1 = workbook1.getSheetAt(0);
                // 复制第一个文件的Sheet
                Sheet newSheet1 = resultWorkbook.createSheet(file.substring(0, file.indexOf(".")));
                for (Row row : sheet1) {
                    Row newRow = newSheet1.createRow(row.getRowNum());
                    int columns = row.getLastCellNum();
                    for (int i = 0; i < columns; i++) {
                        newSheet1.setColumnWidth(i, 5000);
                        Cell cell = row.getCell(i);
                        Cell newCell = newRow.createCell(cell.getColumnIndex());
                        newCell.setCellValue(cell.getStringCellValue());
                        // 创建单元格样式
                        CellStyle style = resultWorkbook.createCellStyle();
                        style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
                        style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
                        if (row.getRowNum() == 0) {
                            // 设置第一行背景色
                            style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
                            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                        }
                        newCell.setCellStyle(style);
                    }
                }
            }
            FileOutputStream out = new FileOutputStream(outputFile);
            resultWorkbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateFundTaxIdById(Long id, Long fundTaxId) {
        return serviceFundMapper.updateFundTaxIdById(id, fundTaxId);
    }
}