InvAccountExcelServiceImpl.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.ydtech.modules.inv.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.google.gson.JsonArray;
  7. import com.google.gson.JsonElement;
  8. import com.google.gson.JsonObject;
  9. import com.ydtech.constants.enums.inv.InvPayMethodEnum;
  10. import com.ydtech.core.page.HttpResult;
  11. import com.ydtech.core.page.PageRequest;
  12. import com.ydtech.modules.base.controller.BaseController;
  13. import com.ydtech.modules.inv.dao.InvAccountExcelMapper;
  14. import com.ydtech.modules.inv.model.InvAccount;
  15. import com.ydtech.modules.inv.model.InvAccountCard;
  16. import com.ydtech.modules.inv.model.InvAccountExcel;
  17. import com.ydtech.modules.inv.model.InvAccountOperate;
  18. import com.ydtech.modules.inv.service.InvAccountCardService;
  19. import com.ydtech.modules.inv.service.InvAccountExcelService;
  20. import com.ydtech.modules.inv.service.InvAccountOperateService;
  21. import com.ydtech.modules.inv.service.InvAccountService;
  22. import com.ydtech.modules.inv.utils.EasyExcelUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Value;
  25. import org.springframework.stereotype.Service;
  26. import java.math.BigDecimal;
  27. import java.math.RoundingMode;
  28. import java.util.*;
  29. import static com.ydtech.modules.inv.utils.TransferUtils.*;
  30. /**
  31. * @author Administrator
  32. * @description 针对表【ins_account_excel(资金申请单表)】的数据库操作Service实现
  33. * @createDate 2024-01-10 08:53:59
  34. */
  35. @Service
  36. public class InvAccountExcelServiceImpl extends ServiceImpl<InvAccountExcelMapper, InvAccountExcel>
  37. implements InvAccountExcelService {
  38. @Autowired
  39. private InvAccountExcelMapper insAccountExcelMapper;
  40. @Autowired
  41. private InvAccountOperateService insAccountOperateService;
  42. @Autowired
  43. private InvAccountCardService invAccountCardService;
  44. @Autowired
  45. private EasyExcelUtils easyExcelUtils;
  46. @Value("${upload.file.path}")
  47. String uploadFilePath;
  48. @Override
  49. public HttpResult excelAccount(List<String> idList) {
  50. List<InvAccountExcel> insAccountExcels = insAccountExcelMapper.selectBatchIds(idList);
  51. if (!insAccountExcels.isEmpty()) {
  52. return HttpResult.ok("", easyExcelUtils.downAccountExcel(uploadFilePath, insAccountExcels));
  53. }
  54. return HttpResult.error("导出失败");
  55. }
  56. @Override
  57. public HttpResult selectAccountExcel(InvAccountExcel invAccountExcel) {
  58. PageRequest pageRequest = new PageRequest();
  59. pageRequest.setPageNum(invAccountExcel.getPageNo());
  60. pageRequest.setPageSize(invAccountExcel.getPageSize());
  61. Page page = PageRequest.buildPageRequest(pageRequest);
  62. IPage<InvAccountExcel> invAccountExcelIPage = insAccountExcelMapper.selectExcelAccount(page, invAccountExcel);
  63. List<InvAccountExcel> records = invAccountExcelIPage.getRecords();
  64. for (InvAccountExcel record:records){
  65. StringBuilder a = new StringBuilder();
  66. String[] split = record.getPayMethod().split(",");
  67. for (String i : split) {
  68. a.append(InvPayMethodEnum.getNameByCode(i)).append(",");
  69. }
  70. record.setPayMethod(a.toString());
  71. }
  72. invAccountExcelIPage.setRecords(records);
  73. return HttpResult.ok("", invAccountExcelIPage);
  74. }
  75. /**
  76. * 修改已转账状态
  77. *
  78. * @param excelAccountOperateId
  79. * @return
  80. */
  81. @Override
  82. public HttpResult updateTransferFlag(String excelAccountOperateId) {
  83. InvAccountExcel invAccountExcel = insAccountExcelMapper.selectById(excelAccountOperateId);
  84. if (invAccountExcel == null) {
  85. throw new RuntimeException("该记录不存在");
  86. }
  87. InvAccountCard outAccountCard = invAccountCardService.getById(invAccountExcel.getOutCardNum());
  88. InvAccountCard enterAccountCard = invAccountCardService.getById(invAccountExcel.getEnterCardNum());
  89. //修改转账状态
  90. invAccountExcel.setTransferFlag("0");
  91. //判断是转账给内部还是外部
  92. if (invAccountExcel.getOutFlag().equals("0")) {
  93. //转账操作
  94. //如果转账失败就返回错误
  95. List<InvAccountCard> insAccountCards = insideTransfer(outAccountCard, enterAccountCard, invAccountExcel);
  96. if (insAccountCards.isEmpty()) {
  97. throw new RuntimeException("转账失败");
  98. }
  99. if (!invAccountCardService.updateBatchById(insAccountCards)){
  100. throw new RuntimeException("转账失败");
  101. }
  102. //内部转账操作
  103. List<InvAccountOperate> list = new ArrayList<>();
  104. JSONObject outProfit = invAccountExcel.getOutProfit();
  105. JSONObject entProfit = invAccountExcel.getEntProfit();
  106. InvAccountExcel out = outJsonUtil(invAccountExcel, outProfit);
  107. InvAccountExcel ent = entJsonUtil(invAccountExcel, entProfit);
  108. //支出信息
  109. list.add(insertOutAccountOperate(out));
  110. //收入信息
  111. list.add(insertRevenueAccountOperate(ent));
  112. if (!insAccountOperateService.saveBatch(list)){
  113. throw new RuntimeException("添加转账信息失败");
  114. }
  115. //重新将值赋值回去方便编辑
  116. invAccountExcel.setOutProfit(outProfit);
  117. invAccountExcel.setEntProfit(entProfit);
  118. } else {
  119. //内转外部转账
  120. List<InvAccountCard> invAccounts = enterDeTransfer(outAccountCard, enterAccountCard, invAccountExcel);
  121. if (!invAccountCardService.saveBatch(invAccounts)){
  122. throw new RuntimeException("添加转账信息失败");
  123. }
  124. JSONObject outProfit = invAccountExcel.getOutProfit();
  125. InvAccountExcel out = outJsonUtil(invAccountExcel, outProfit);
  126. insAccountOperateService.save(insertOutAccountOperate(out));
  127. //重新将值赋值回去方便编辑
  128. invAccountExcel.setOutProfit(outProfit);
  129. }
  130. return HttpResult.ok("", insAccountExcelMapper.updateById(invAccountExcel));
  131. }
  132. private InvAccountExcel outJsonUtil(InvAccountExcel invAccountExcel, JSONObject outProfit) {
  133. JSONObject jsonObject = new JSONObject();
  134. for (Map.Entry<String, Object> entry : outProfit.entrySet()){
  135. BigDecimal multiply = new BigDecimal(String.valueOf(entry.getValue())).multiply(new BigDecimal(invAccountExcel.getApplyAmount())).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
  136. String string = multiply.negate().toString();
  137. jsonObject.put(entry.getKey(), string);
  138. }
  139. invAccountExcel.setOutProfit(jsonObject);
  140. return invAccountExcel;
  141. }
  142. private InvAccountExcel entJsonUtil(InvAccountExcel invAccountExcel, JSONObject outProfit) {
  143. JSONObject jsonObject = new JSONObject();
  144. for (Map.Entry<String, Object> entry : outProfit.entrySet()){
  145. BigDecimal multiply = new BigDecimal(String.valueOf(entry.getValue())).multiply(new BigDecimal(invAccountExcel.getApplyAmount())).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
  146. jsonObject.put(entry.getKey(), multiply);
  147. }
  148. invAccountExcel.setOutProfit(jsonObject);
  149. return invAccountExcel;
  150. }
  151. }