| | |
| | | package com.ruoyi.cwgl.service.impl; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.enums.SystemDataNoEnum; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import javax.annotation.Resource; |
| | | |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.cwgl.domain.InvoiceDetail; |
| | | import com.ruoyi.cwgl.domain.*; |
| | | import com.ruoyi.cwgl.domain.dto.CallbackReceiptDto; |
| | | import com.ruoyi.cwgl.service.IInvoiceBusinessService; |
| | | import com.ruoyi.system.service.ISystemDataNoService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.scheduling.annotation.Async; |
| | |
| | | import com.ruoyi.common.core.service.BaseService; |
| | | |
| | | import com.ruoyi.cwgl.mapper.InvoiceManageMapper; |
| | | import com.ruoyi.cwgl.domain.InvoiceManage; |
| | | import com.ruoyi.cwgl.service.IInvoiceManageService; |
| | | import com.ruoyi.cwgl.service.IInvoiceManageLogService; |
| | | import com.ruoyi.common.core.text.Convert; |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Service |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public class InvoiceManageServiceImpl extends BaseService<InvoiceManageMapper, InvoiceManage> implements IInvoiceManageService |
| | | { |
| | | public class InvoiceManageServiceImpl extends BaseService<InvoiceManageMapper, InvoiceManage> implements IInvoiceManageService { |
| | | protected final Logger logger = LoggerFactory.getLogger(getClass()); |
| | | @Resource |
| | | private InvoiceManageMapper invoiceManageMapper; |
| | | |
| | | @Resource |
| | | private IInvoiceManageLogService invoiceManageLogService; |
| | | |
| | | @Autowired |
| | | private ISystemDataNoService systemDataNoService; |
| | | @Autowired |
| | | private IInvoiceBusinessService businessService; |
| | | @Value("${custom.gdpaasApi.extensionNumber}") |
| | | private String extensionNumber; |
| | | |
| | | |
| | | @Value("${custom.gdpaasApi.clerk}") |
| | | private String clerk; |
| | | @Value("${custom.gdpaasApi.invoicingAddress}") |
| | | private String invoicingAddress; |
| | | @Value("${custom.gdpaasApi.returnAddress}") |
| | | private String returnAddress; |
| | | |
| | | /** |
| | | * 查询发票管理 |
| | |
| | | */ |
| | | @DataSource(DataSourceType.SLAVE) |
| | | @Override |
| | | public InvoiceManage selectInvoiceManageById(Integer id) |
| | | { |
| | | public InvoiceManage selectInvoiceManageById(Integer id) { |
| | | return invoiceManageMapper.selectInvoiceManageById(id); |
| | | } |
| | | |
| | |
| | | */ |
| | | @DataSource(DataSourceType.SLAVE) |
| | | @Override |
| | | public int selectInvoiceManageCount(InvoiceManage invoiceManage) |
| | | { |
| | | public int selectInvoiceManageCount(InvoiceManage invoiceManage) { |
| | | return invoiceManageMapper.selectInvoiceManageCount(invoiceManage); |
| | | } |
| | | |
| | |
| | | */ |
| | | @DataSource(DataSourceType.SLAVE) |
| | | @Override |
| | | public List<InvoiceManage> selectInvoiceManageList(InvoiceManage invoiceManage) |
| | | { |
| | | public List<InvoiceManage> selectInvoiceManageList(InvoiceManage invoiceManage) { |
| | | return invoiceManageMapper.selectInvoiceManageList(invoiceManage); |
| | | } |
| | | |
| | |
| | | * 查询发票管理列表 异步 导出 |
| | | * |
| | | * @param invoiceManage 发票管理 |
| | | * @param exportKey 导出功能的唯一标识 |
| | | * @param exportKey 导出功能的唯一标识 |
| | | * @return 发票管理集合 |
| | | */ |
| | | @DataSource(DataSourceType.SLAVE) |
| | | @Async |
| | | @Override |
| | | public void export(InvoiceManage invoiceManage,String exportKey) { |
| | | public void export(InvoiceManage invoiceManage, String exportKey) { |
| | | |
| | | super.export(InvoiceManage.class,exportKey,"invoiceManageData",(pageNum)->{ |
| | | super.export(InvoiceManage.class, exportKey, "invoiceManageData", (pageNum) -> { |
| | | PageUtils.startPage(pageNum, Constants.EXPORT_PATE_SIZE); |
| | | return selectInvoiceManageList(invoiceManage); |
| | | }); |
| | |
| | | * @param invoiceManage 发票管理 |
| | | * @return 结果 |
| | | */ |
| | | @Transactional |
| | | @Override |
| | | public int insertInvoiceManage(InvoiceManage invoiceManage) |
| | | { |
| | | public int insertInvoiceManage(InvoiceManage invoiceManage) { |
| | | invoiceManage.setCreateTime(DateUtils.getNowDate()); |
| | | invoiceManage.setOrderNo(systemDataNoService.getNoByKey(SystemDataNoEnum.FP)); |
| | | int rows = invoiceManageMapper.insertInvoiceManage(invoiceManage); |
| | | insertInvoiceDetail(invoiceManage); |
| | | |
| | | // 记录新增发票管理日志 |
| | | if (rows > 0) { |
| | | recordInvoiceManageLog(invoiceManage, "新增发票管理"); |
| | | } |
| | | |
| | | return rows; |
| | | } |
| | | |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int insertInvoiceManageBatch(List<InvoiceManage> invoiceManages) |
| | | { |
| | | public int insertInvoiceManageBatch(List<InvoiceManage> invoiceManages) { |
| | | int rows = invoiceManageMapper.insertInvoiceManageBatch(invoiceManages); |
| | | |
| | | // 记录批量新增发票管理日志 |
| | | if (rows > 0 && invoiceManages != null && !invoiceManages.isEmpty()) { |
| | | for (InvoiceManage invoiceManage : invoiceManages) { |
| | | recordInvoiceManageLog(invoiceManage, "批量新增发票管理"); |
| | | } |
| | | } |
| | | |
| | | return rows; |
| | | } |
| | | |
| | |
| | | */ |
| | | @Transactional |
| | | @Override |
| | | public int updateInvoiceManage(InvoiceManage invoiceManage) |
| | | { |
| | | public int updateInvoiceManage(InvoiceManage invoiceManage) { |
| | | invoiceManage.setUpdateTime(DateUtils.getNowDate()); |
| | | invoiceManageMapper.deleteInvoiceDetailByInvoiceManageId(invoiceManage.getId()); |
| | | insertInvoiceDetail(invoiceManage); |
| | | return invoiceManageMapper.updateInvoiceManage(invoiceManage); |
| | | int rows = invoiceManageMapper.updateInvoiceManage(invoiceManage); |
| | | |
| | | // 记录修改发票管理日志 |
| | | if (rows > 0) { |
| | | recordInvoiceManageLog(invoiceManage, "修改发票管理"); |
| | | } |
| | | |
| | | return rows; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int updateInvoiceManageBatch(List<InvoiceManage> invoiceManages){ |
| | | return invoiceManageMapper.updateInvoiceManageBatch(invoiceManages); |
| | | public int updateInvoiceManageBatch(List<InvoiceManage> invoiceManages) { |
| | | int rows = invoiceManageMapper.updateInvoiceManageBatch(invoiceManages); |
| | | |
| | | // 记录批量修改发票管理日志 |
| | | if (rows > 0 && invoiceManages != null && !invoiceManages.isEmpty()) { |
| | | for (InvoiceManage invoiceManage : invoiceManages) { |
| | | recordInvoiceManageLog(invoiceManage, "批量修改发票管理"); |
| | | } |
| | | } |
| | | |
| | | return rows; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Transactional |
| | | @Override |
| | | public int deleteInvoiceManageByIds(String ids) |
| | | { |
| | | public int deleteInvoiceManageByIds(String ids) { |
| | | return deleteInvoiceManageByIds(Convert.toIntArray(ids)); |
| | | } |
| | | |
| | | /** |
| | | * 删除发票管理对象 |
| | | * |
| | | * |
| | | * @param ids 需要删除的数据ID |
| | | * @return 结果 |
| | | */ |
| | | @Transactional |
| | | @Override |
| | | public int deleteInvoiceManageByIds(Integer[] ids) |
| | | { |
| | | public int deleteInvoiceManageByIds(Integer[] ids) { |
| | | invoiceManageMapper.deleteInvoiceDetailByInvoiceManageIds(ids); |
| | | return invoiceManageMapper.deleteInvoiceManageByIds(ids); |
| | | } |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int deleteInvoiceManageById(Integer id) |
| | | { |
| | | public int deleteInvoiceManageById(Integer id) { |
| | | invoiceManageMapper.deleteInvoiceDetailByInvoiceManageId(id); |
| | | return invoiceManageMapper.deleteInvoiceManageById(id); |
| | | } |
| | |
| | | * @param invoiceManage 发票管理对象 |
| | | */ |
| | | @Override |
| | | public void insertInvoiceDetail(InvoiceManage invoiceManage) |
| | | { |
| | | public void insertInvoiceDetail(InvoiceManage invoiceManage) { |
| | | List<InvoiceDetail> invoiceDetailList = invoiceManage.getInvoiceDetailList(); |
| | | Integer id = invoiceManage.getId(); |
| | | if (StringUtils.isNotNull(invoiceDetailList)) |
| | | { |
| | | if (StringUtils.isNotNull(invoiceDetailList)) { |
| | | List<InvoiceDetail> list = new ArrayList<InvoiceDetail>(); |
| | | for (InvoiceDetail invoiceDetail : invoiceDetailList) |
| | | { |
| | | for (InvoiceDetail invoiceDetail : invoiceDetailList) { |
| | | invoiceDetail.setInvoiceManageId(id); |
| | | invoiceDetail.setCreateTime(new Date()); |
| | | list.add(invoiceDetail); |
| | | } |
| | | if (list.size() > 0) |
| | | { |
| | | if (list.size() > 0) { |
| | | invoiceManageMapper.batchInvoiceDetail(list); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 记录发票管理操作日志 |
| | | * |
| | | * @param invoiceManage 发票管理对象 |
| | | * @param operationType 操作类型 |
| | | */ |
| | | private void recordInvoiceManageLog(InvoiceManage invoiceManage, String operationType) { |
| | | try { |
| | | InvoiceManageLog log = new InvoiceManageLog(); |
| | | log.setInvoiceManageId(invoiceManage.getId()); |
| | | log.setOperator(SecurityUtils.getUsername()); |
| | | log.setOperationTime(DateUtils.getNowDate()); |
| | | |
| | | // 构建操作描述,包含开票金额、关联账单编号和对应开票金额 |
| | | StringBuilder descBuilder = new StringBuilder(); |
| | | descBuilder.append(operationType).append(": "); |
| | | |
| | | if (invoiceManage.getInvoiceAmount() != null) { |
| | | descBuilder.append("开票金额=").append(invoiceManage.getInvoiceAmount()).append("; "); |
| | | } |
| | | |
| | | // 获取关联账单信息 |
| | | if (invoiceManage.getInvoiceDetailList() != null && !invoiceManage.getInvoiceDetailList().isEmpty()) { |
| | | descBuilder.append("关联账单信息: "); |
| | | for (InvoiceDetail detail : invoiceManage.getInvoiceDetailList()) { |
| | | if (detail.getReceivableBillNo() != null) { |
| | | descBuilder.append("账单编号=").append(detail.getReceivableBillNo()); |
| | | } |
| | | if (detail.getCurrentBilledAmount() != null) { |
| | | descBuilder.append(", 本次开票金额=").append(detail.getCurrentBilledAmount()); |
| | | } |
| | | descBuilder.append("; "); |
| | | } |
| | | } |
| | | |
| | | log.setOperationDesc(descBuilder.toString()); |
| | | log.setCreateTime(DateUtils.getNowDate()); |
| | | |
| | | invoiceManageLogService.insertInvoiceManageLog(log); |
| | | } catch (Exception e) { |
| | | logger.error("记录发票管理操作日志失败: {}", e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public AjaxResult applyInvoice(Integer id, List<InvoiceBillDetail> invoiceBillDetails) { |
| | | InvoiceManage invoiceManage = selectInvoiceManageById(id); |
| | | if (invoiceManage == null) { |
| | | throw new ServiceException("发票记录不存在"); |
| | | } |
| | | if (invoiceManage.getStatus().equals(1)) { |
| | | throw new ServiceException("请勿重复开票"); |
| | | |
| | | } |
| | | List<InvoiceDetail> invoiceDetailList = invoiceManage.getInvoiceDetailList(); |
| | | if (CollectionUtil.isEmpty(invoiceDetailList)) { |
| | | throw new ServiceException("收费明细无数据"); |
| | | |
| | | } |
| | | |
| | | // 记录申请开票日志 |
| | | recordApplyInvoiceLog(invoiceManage); |
| | | |
| | | componentInvoice(invoiceManage.getInvoiceNo(),"FP",invoiceManage,invoiceBillDetails); |
| | | return AjaxResult.success("申请开票成功"); |
| | | } |
| | | |
| | | public AjaxResult componentInvoice(String orderNo, String businessType, InvoiceManage invoiceManage, List<InvoiceBillDetail> invoiceBillDetails) { |
| | | |
| | | InvoicingVo invoicingVo = new InvoicingVo(); |
| | | Order order = new Order(); |
| | | order.setSalerTaxNum(invoiceManage.getInvoiceSellerCreditCode()); |
| | | order.setSalerTel(invoiceManage.getInvoiceSellerPhone()); |
| | | order.setSalerAddress(invoiceManage.getInvoiceSellerAddress()); |
| | | order.setInvoiceDate(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS)); |
| | | order.setClerk(clerk); |
| | | order.setInvoiceType("1"); |
| | | order.setPushMode("0"); |
| | | order.setExtensionNumber(extensionNumber); |
| | | |
| | | order.setOrderNo(orderNo); |
| | | order.setEmail(invoiceManage.getInvoiceOperatingLicenseEmail()); |
| | | order.setBuyerName(invoiceManage.getInvoiceCompanyName()); |
| | | order.setInvoiceLine(invoiceManage.getInvoiceType()); |
| | | order.setBuyerPhone(invoiceManage.getInvoiceOperatingLicensePhone()); |
| | | order.setBuyerTaxNum(invoiceManage.getInvoiceCreditCode()); |
| | | order.setBuyerTel(invoiceManage.getInvoiceOperatingLicensePhone()); |
| | | if (StringUtils.isNotBlank(invoiceManage.getInvoiceBankNo()) && |
| | | StringUtils.isNotBlank(invoiceManage.getInvoiceBankName())) { |
| | | order.setBuyerAccount(invoiceManage.getInvoiceBankName() + " " + invoiceManage.getInvoiceBankNo()); |
| | | } |
| | | |
| | | List<InvoiceDetailApi> invoiceDetailList = new ArrayList<>(); |
| | | //商品明细 |
| | | for (InvoiceBillDetail billingDetail : invoiceBillDetails) { |
| | | InvoiceDetailApi invoiceDetail = new InvoiceDetailApi(); |
| | | |
| | | |
| | | invoiceDetail.setGoodsName(invoiceDetail.getGoodsName()); |
| | | |
| | | invoiceDetail.setWithTaxFlag(billingDetail.getWithTaxFlag().toString()); |
| | | invoiceDetail.setTaxRate("0.06"); |
| | | invoiceDetail.setUnit("项"); |
| | | invoiceDetail.setNum("1"); |
| | | invoiceDetail.setPrice(billingDetail.getPrice().toString()); // 初始价格为0 |
| | | invoiceDetailList.add(invoiceDetail); |
| | | } |
| | | |
| | | |
| | | order.setInvoiceDetail(invoiceDetailList); |
| | | invoicingVo.setOrder(order); |
| | | String body = JSONObject.toJSONString(invoicingVo); |
| | | String json = HttpUtil.post(invoicingAddress + "?businessType=" + businessType + "&returnUrl=" + returnAddress, body); |
| | | |
| | | if (json == null) { |
| | | logger.info("开票失败"); |
| | | return AjaxResult.error("开票失败"); |
| | | } |
| | | JSONObject jsonObject = JSONObject.parseObject(json); |
| | | String code = jsonObject.getString("code"); |
| | | |
| | | String invoiceSerialNum = null; |
| | | InvoiceBusiness record = new InvoiceBusiness(); |
| | | record.setReqData(JSONObject.toJSONString(invoicingVo)); |
| | | record.setResData(json); |
| | | record.setCreateTime(new Date()); |
| | | if ("0".equals(code)) { |
| | | String data = jsonObject.getString("data"); |
| | | |
| | | JSONObject dataJson = JSONObject.parseObject(data); |
| | | invoiceSerialNum = dataJson.getString("invoiceSerialNum"); |
| | | logger.info("开票成功发票序列号为{}", invoiceSerialNum); |
| | | record.setInvoiceSerialNum(invoiceSerialNum); |
| | | record.setInvoiceManageId(invoiceManage.getId()); |
| | | return AjaxResult.success(); |
| | | } |
| | | return AjaxResult.error("开票失败"); |
| | | } |
| | | |
| | | /** |
| | | * 记录申请开票日志 |
| | | * |
| | | * @param invoiceManage 发票管理对象 |
| | | */ |
| | | private void recordApplyInvoiceLog(InvoiceManage invoiceManage) { |
| | | try { |
| | | InvoiceManageLog log = new InvoiceManageLog(); |
| | | log.setInvoiceManageId(invoiceManage.getId()); |
| | | log.setOperator(SecurityUtils.getUsername()); |
| | | log.setOperationTime(new java.util.Date()); |
| | | |
| | | // 构建操作描述,包含开票金额、关联账单编号和对应开票金额 |
| | | StringBuilder descBuilder = new StringBuilder(); |
| | | descBuilder.append("申请开票: "); |
| | | |
| | | if (invoiceManage.getInvoiceAmount() != null) { |
| | | descBuilder.append("开票金额=").append(invoiceManage.getInvoiceAmount()).append("; "); |
| | | } |
| | | |
| | | // 获取关联账单信息 |
| | | if (invoiceManage.getInvoiceDetailList() != null && !invoiceManage.getInvoiceDetailList().isEmpty()) { |
| | | descBuilder.append("关联账单信息: "); |
| | | for (com.ruoyi.cwgl.domain.InvoiceDetail detail : invoiceManage.getInvoiceDetailList()) { |
| | | if (detail.getReceivableBillNo() != null) { |
| | | descBuilder.append("账单编号=").append(detail.getReceivableBillNo()); |
| | | } |
| | | if (detail.getCurrentBilledAmount() != null) { |
| | | descBuilder.append(", 本次开票金额=").append(detail.getCurrentBilledAmount()); |
| | | } |
| | | descBuilder.append("; "); |
| | | } |
| | | } |
| | | |
| | | log.setOperationDesc(descBuilder.toString()); |
| | | log.setCreateTime(new java.util.Date()); |
| | | |
| | | invoiceManageLogService.insertInvoiceManageLog(log); |
| | | } catch (Exception e) { |
| | | logger.error("记录申请开票日志失败: {}", e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void invoicingSuccess(CallbackReceiptDto callbackReceiptDto) { |
| | | InvoiceManage invoiceBusiness = invoiceManageMapper.selectInvoiceManageLogByOrderNo(callbackReceiptDto.getOrderno()); |
| | | if (invoiceBusiness == null) { |
| | | return; |
| | | } |
| | | String content = callbackReceiptDto.getContent(); |
| | | if (StringUtils.isEmpty(content)) { |
| | | return; |
| | | } |
| | | JSONObject jsonObject = JSONObject.parseObject(content); |
| | | |
| | | int c_status = Integer.parseInt(jsonObject.getString("c_status")); |
| | | invoiceBusiness.setStatus(c_status); |
| | | if (c_status == 1) { |
| | | |
| | | |
| | | String invoiceNumStr = jsonObject.getString("c_fphm"); |
| | | |
| | | InvoiceManage invoiceManageUpdate = new InvoiceManage(); |
| | | invoiceManageUpdate.setInvoiceNo(invoiceNumStr); |
| | | invoiceManageUpdate.setId(invoiceBusiness.getId()); |
| | | invoiceManageUpdate.setStatus(2); |
| | | invoiceManageMapper.updateById(invoiceBusiness); |
| | | } |
| | | } |
| | | } |
| | | |