package com.ydtech.modules.inv.service.impl; import cn.hutool.extra.spring.SpringUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ydtech.constants.enums.inv.InvPayMethodEnum; import com.ydtech.core.page.HttpResult; import com.ydtech.core.page.PageRequest; import com.ydtech.exception.SystemException; import com.ydtech.modules.base.controller.BaseController; import com.ydtech.modules.inv.dao.InvAccountOperateMapper; import com.ydtech.modules.inv.model.*; import com.ydtech.modules.inv.service.*; import com.ydtech.modules.inv.utils.EasyExcelUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.File; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import static com.ydtech.modules.inv.utils.TransferUtils.*; /** * @author Administrator * @description 针对表【ins_account_operate(账户操作记录表)】的数据库操作Service实现 * @createDate 2024-01-04 18:29:20 */ @Service public class InvAccountOperateServiceImpl extends ServiceImpl implements InvAccountOperateService { @Autowired private InvAccountOperateMapper invAccountOperateMapper; @Autowired private InvAccountOpService invAccountOpService; @Autowired private InvAccountCardService invAccountCardService; @Autowired private InvAccountService invAccountService; @Autowired private InvAccountExcelService invAccountExcelService; @Autowired private InvAccountProfitService invAccountProfitService; @Autowired private InvAccountIncoiceService invAccountIncoiceService; @Value("${upload.file.path}") String uploadFilePath; /** * 查询操作列表 * * @param accountOperate * @return */ @Override public IPage selectList(InvAccountOperate accountOperate) { PageRequest pageRequest = new PageRequest(); pageRequest.setPageNum(accountOperate.getPageNo()); pageRequest.setPageSize(accountOperate.getPageSize()); Page page = PageRequest.buildPageRequest(pageRequest); IPage invAccountOperateIPage = invAccountOperateMapper.selectInsAccountOperate(page, accountOperate); List records = invAccountOperateIPage.getRecords(); for (InvAccountOperate record : records) { StringBuilder a = new StringBuilder(); if (record.getPayMethod() != null) { String[] split = record.getPayMethod().split(","); for (int i = 0; i < split.length; i++) { a.append(InvPayMethodEnum.getNameByCode(split[i])); if (i < split.length - 1) { a.append(","); } } record.setPayMethod(a.toString()); } } invAccountOperateIPage.setRecords(records); return invAccountOperateIPage; } /** * 外部转内部 * * @param invAccountOperate * @return */ @Override public HttpResult insert(InvAccountOperate invAccountOperate) { InvAccountCard entAccountCard = invAccountCardService.getById(invAccountOperate.getEnterCardId()); InvAccountCard outAccountCard = invAccountCardService.getById(invAccountOperate.getOutCardId()); InvAccount entAccount = invAccountService.getById(entAccountCard.getAccountId()); InvAccount outAccount = invAccountService.getById(outAccountCard.getAccountId()); String applyAmount = invAccountOperate.getApplyAmount(); JSONObject outProfit = invAccountOperate.getOutProfit(); JSONObject entProfit = invAccountOperate.getEntProfit(); if (entAccount.getOuterFlag().equals("1")) { return HttpResult.error("此操作收入账户不可为外部账户"); } if (entAccountCard.equals(outAccountCard)) { for (Map.Entry entry : outProfit.entrySet()) { for (Map.Entry entry2 : entProfit.entrySet()) { if (new BigDecimal(entry.getValue().toString()).compareTo(BigDecimal.ZERO) != 0 && new BigDecimal(entry2.getValue().toString()).compareTo(BigDecimal.ZERO) != 0) { if (entry.getKey().equals(entry2.getKey())) { throw new SystemException("无法向同公司同板块互相转账"); } } } } } if (entAccount.getOuterFlag().equals("0") && !entAccountCard.getAccountId().equals(outAccountCard.getAccountId())) { InvAccountCard invAccount = outsideTransfer(outAccountCard, outAccount, entAccountCard, entAccount, applyAmount); invAccountCardService.updateById(invAccount); BaseController baseController = new BaseController(); invAccountOperate.setCreateTime(LocalDateTime.now()); invAccountOperate.setOutProfit(null); invAccountOperate.setEnterCardNum(entAccountCard.getBankCardNum()); invAccountOperate.setCreateBy(baseController.getUserName()); invAccountOperate.setCreateId(baseController.getUserId()); invAccountOperateMapper.insert(invAccountOperate); } List list = invAccountEntOpList(entProfit, entAccount, entAccountCard,invAccountOperate.getAccountOperateId()); if (!entAccountCard.getAccountId().equals(outAccountCard.getAccountId())){ list.forEach(invAccountOp -> { invAccountOp.setInvoiceId(invAccountOperate.getAccountOperateId()); }); }else { List list1 = invAccountOutOpList(outProfit, outAccount, outAccountCard, invAccountOperate.getAccountOperateId()); list.forEach(invAccountOp -> { invAccountOp.setInvoiceId(invAccountOperate.getAccountOperateId()); }); list1.forEach(invAccountOp -> { invAccountOp.setInvoiceId(invAccountOperate.getAccountOperateId()); }); list.addAll(list1); } invAccountOpService.saveBatch(list); return HttpResult.ok("新增收入成功"); } @Override public int updateOperateByIncoiceId(InvAccountOperate invAccountIncoiceId) { return invAccountOperateMapper.updateOperateByIncoiceId(invAccountIncoiceId); } @Override public HttpResult excelAccount(InvAccountOperate accountOperate) { List invAccountOperates = invAccountOperateMapper.selectInsAccountOperateList(accountOperate); List list = invAccountProfitService.list(); for (InvAccountOperate record : invAccountOperates) { StringBuilder a = new StringBuilder(); if (record.getPayMethod() != null) { String[] split = record.getPayMethod().split(","); for (String i : split) { a.append(InvPayMethodEnum.getNameByCode(i)).append(","); } record.setPayMethod(a.toString()); } if (record.getOutProfit() != null) { StringBuffer buffer = new StringBuffer(); for (Map.Entry entry : record.getOutProfit().entrySet()) { list.forEach(invAccountProfit -> { if (invAccountProfit.getProfitEng().equals(entry.getKey())) { buffer.append(invAccountProfit.getProfitName()).append(" "); } }); } record.setPlate(buffer.toString()); } } File folder = new File(uploadFilePath); if (folder.exists()) { return HttpResult.ok("", EasyExcelUtils.noModelOperateWrite(uploadFilePath, list, invAccountOperates)); } else { if (folder.mkdirs()) { return HttpResult.ok("", EasyExcelUtils.noModelOperateWrite(uploadFilePath, list, invAccountOperates)); } else { throw new SystemException("文件导出失败"); } } } @Override public HttpResult deleteById(String accountOperateId) { InvAccountOperate invAccountOperate = invAccountOperateMapper.selectById(accountOperateId); if (invAccountOperate == null) { throw new SystemException("操作为空无法删除"); } List invAccountOperates = new ArrayList<>(); if (invAccountOperate.getDel() != null) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); queryWrapper.eq(InvAccountOperate::getDel, invAccountOperate.getDel()); invAccountOperates = invAccountOperateMapper.selectList(queryWrapper); } if (invAccountOperates.isEmpty()){ invAccountOperates.add(invAccountOperate); } if (invAccountOperate.getRevenueOutlay().equals("0")) { String outCardNum = invAccountOperate.getOutCardNum(); String enterCardNum = invAccountOperate.getEnterCardNum(); InvAccountCard outAccountCard = invAccountCardService.getById(outCardNum); InvAccountCard entAccountCard = invAccountCardService.getById(enterCardNum); InvAccount entAccount = invAccountService.getById(entAccountCard.getAccountId()); List invAccountCards = quashOutsideTransfer(outAccountCard, entAccountCard, new BigDecimal(invAccountOperate.getApplyAmount()),entAccount); List accountOperateIds = invAccountOperates.stream() .map(InvAccountOperate::getAccountOperateId) .distinct() .collect(Collectors.toList()); BaseController baseController = new BaseController(); invAccountOperates.forEach(invAccountOperate1 -> { invAccountOperate1.setUpdateBy(baseController.getUserName()); invAccountOperate1.setUpdateId(baseController.getUserId()); invAccountOperate1.setUpdateTime(LocalDateTime.now()); invAccountOperate1.setDelFlag("1"); }); LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper(); lambdaUpdateWrapper.in(InvAccountOp::getAccountOperateId, accountOperateIds); lambdaUpdateWrapper.set(InvAccountOp::getDelFlag,"1"); if(invAccountOperate.getInvoiceId() != null){ InvAccountIncoice byId = invAccountIncoiceService.getById(invAccountOperate.getInvoiceId()); if (new BigDecimal(byId.getPayoutMoney()).compareTo(new BigDecimal(invAccountOperate.getApplyAmount())) < 0){ throw new SystemException("撤销金额大于已进账金额"); } byId.setPayoutMoney(new BigDecimal(byId.getPayoutMoney()).subtract(new BigDecimal(invAccountOperate.getApplyAmount())).toString()); byId.setNotPayoutMoney(new BigDecimal(byId.getNotPayoutMoney()).add(new BigDecimal(invAccountOperate.getApplyAmount())).toString()); invAccountIncoiceService.updateById(byId); } if (invAccountOperate.getExcelAccountOperateId() != null){ InvAccountExcel invAccountExcel = new InvAccountExcel(); invAccountExcel.setExcelAccountOperateId(invAccountOperate.getExcelAccountOperateId()); invAccountExcel.setUpdateBy(baseController.getUserName()); invAccountExcel.setUpdateTime(new Date()); invAccountExcel.setTransferFlag("1"); invAccountExcelService.updateById(invAccountExcel); } invAccountOpService.update(lambdaUpdateWrapper); invAccountCardService.updateBatchById(invAccountCards); InvAccountOperateService invAccountOperateService = new InvAccountOperateServiceImpl(); invAccountOperateService.updateBatchById(invAccountOperates); return HttpResult.ok("删除成功"); } else { return HttpResult.error("此操作为收入账户,无法删除"); } } }