package com.ydtech.modules.inv.service.impl; import com.alibaba.fastjson.JSONObject; 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.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.ydtech.constants.enums.inv.InvPayMethodEnum; import com.ydtech.core.page.HttpResult; import com.ydtech.core.page.PageRequest; import com.ydtech.modules.base.controller.BaseController; import com.ydtech.modules.inv.dao.InvAccountExcelMapper; import com.ydtech.modules.inv.model.InvAccount; import com.ydtech.modules.inv.model.InvAccountCard; import com.ydtech.modules.inv.model.InvAccountExcel; import com.ydtech.modules.inv.model.InvAccountOperate; import com.ydtech.modules.inv.service.InvAccountCardService; import com.ydtech.modules.inv.service.InvAccountExcelService; import com.ydtech.modules.inv.service.InvAccountOperateService; import com.ydtech.modules.inv.service.InvAccountService; 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.math.BigDecimal; import java.math.RoundingMode; import java.util.*; import static com.ydtech.modules.inv.utils.TransferUtils.*; /** * @author Administrator * @description 针对表【ins_account_excel(资金申请单表)】的数据库操作Service实现 * @createDate 2024-01-10 08:53:59 */ @Service public class InvAccountExcelServiceImpl extends ServiceImpl implements InvAccountExcelService { @Autowired private InvAccountExcelMapper insAccountExcelMapper; @Autowired private InvAccountOperateService insAccountOperateService; @Autowired private InvAccountCardService invAccountCardService; @Autowired private EasyExcelUtils easyExcelUtils; @Value("${upload.file.path}") String uploadFilePath; @Override public HttpResult excelAccount(List idList) { List insAccountExcels = insAccountExcelMapper.selectBatchIds(idList); if (!insAccountExcels.isEmpty()) { return HttpResult.ok("", easyExcelUtils.downAccountExcel(uploadFilePath, insAccountExcels)); } return HttpResult.error("导出失败"); } @Override public HttpResult selectAccountExcel(InvAccountExcel invAccountExcel) { PageRequest pageRequest = new PageRequest(); pageRequest.setPageNum(invAccountExcel.getPageNo()); pageRequest.setPageSize(invAccountExcel.getPageSize()); Page page = PageRequest.buildPageRequest(pageRequest); IPage invAccountExcelIPage = insAccountExcelMapper.selectExcelAccount(page, invAccountExcel); List records = invAccountExcelIPage.getRecords(); for (InvAccountExcel record:records){ StringBuilder a = new StringBuilder(); String[] split = record.getPayMethod().split(","); for (String i : split) { a.append(InvPayMethodEnum.getNameByCode(i)).append(","); } record.setPayMethod(a.toString()); } invAccountExcelIPage.setRecords(records); return HttpResult.ok("", invAccountExcelIPage); } /** * 修改已转账状态 * * @param excelAccountOperateId * @return */ @Override public HttpResult updateTransferFlag(String excelAccountOperateId) { InvAccountExcel invAccountExcel = insAccountExcelMapper.selectById(excelAccountOperateId); if (invAccountExcel == null) { throw new RuntimeException("该记录不存在"); } InvAccountCard outAccountCard = invAccountCardService.getById(invAccountExcel.getOutCardNum()); InvAccountCard enterAccountCard = invAccountCardService.getById(invAccountExcel.getEnterCardNum()); //修改转账状态 invAccountExcel.setTransferFlag("0"); //判断是转账给内部还是外部 if (invAccountExcel.getOutFlag().equals("0")) { //转账操作 //如果转账失败就返回错误 List insAccountCards = insideTransfer(outAccountCard, enterAccountCard, invAccountExcel); if (insAccountCards.isEmpty()) { throw new RuntimeException("转账失败"); } if (!invAccountCardService.updateBatchById(insAccountCards)){ throw new RuntimeException("转账失败"); } //内部转账操作 List list = new ArrayList<>(); JSONObject outProfit = invAccountExcel.getOutProfit(); JSONObject entProfit = invAccountExcel.getEntProfit(); InvAccountExcel out = outJsonUtil(invAccountExcel, outProfit); InvAccountExcel ent = entJsonUtil(invAccountExcel, entProfit); //支出信息 list.add(insertOutAccountOperate(out)); //收入信息 list.add(insertRevenueAccountOperate(ent)); if (!insAccountOperateService.saveBatch(list)){ throw new RuntimeException("添加转账信息失败"); } //重新将值赋值回去方便编辑 invAccountExcel.setOutProfit(outProfit); invAccountExcel.setEntProfit(entProfit); } else { //内转外部转账 List invAccounts = enterDeTransfer(outAccountCard, enterAccountCard, invAccountExcel); if (!invAccountCardService.saveBatch(invAccounts)){ throw new RuntimeException("添加转账信息失败"); } JSONObject outProfit = invAccountExcel.getOutProfit(); InvAccountExcel out = outJsonUtil(invAccountExcel, outProfit); insAccountOperateService.save(insertOutAccountOperate(out)); //重新将值赋值回去方便编辑 invAccountExcel.setOutProfit(outProfit); } return HttpResult.ok("", insAccountExcelMapper.updateById(invAccountExcel)); } private InvAccountExcel outJsonUtil(InvAccountExcel invAccountExcel, JSONObject outProfit) { JSONObject jsonObject = new JSONObject(); for (Map.Entry entry : outProfit.entrySet()){ BigDecimal multiply = new BigDecimal(String.valueOf(entry.getValue())).multiply(new BigDecimal(invAccountExcel.getApplyAmount())).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP); String string = multiply.negate().toString(); jsonObject.put(entry.getKey(), string); } invAccountExcel.setOutProfit(jsonObject); return invAccountExcel; } private InvAccountExcel entJsonUtil(InvAccountExcel invAccountExcel, JSONObject outProfit) { JSONObject jsonObject = new JSONObject(); for (Map.Entry entry : outProfit.entrySet()){ BigDecimal multiply = new BigDecimal(String.valueOf(entry.getValue())).multiply(new BigDecimal(invAccountExcel.getApplyAmount())).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP); jsonObject.put(entry.getKey(), multiply); } invAccountExcel.setOutProfit(jsonObject); return invAccountExcel; } }