InvAccountOperateServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package com.ydtech.modules.inv.service.impl;
  2. import cn.hutool.extra.spring.SpringUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  6. import com.baomidou.mybatisplus.core.metadata.IPage;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  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.exception.SystemException;
  13. import com.ydtech.modules.base.controller.BaseController;
  14. import com.ydtech.modules.inv.dao.InvAccountOperateMapper;
  15. import com.ydtech.modules.inv.model.*;
  16. import com.ydtech.modules.inv.service.*;
  17. import com.ydtech.modules.inv.utils.EasyExcelUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.stereotype.Service;
  21. import java.io.File;
  22. import java.math.BigDecimal;
  23. import java.time.LocalDateTime;
  24. import java.util.ArrayList;
  25. import java.util.Date;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.stream.Collectors;
  29. import static com.ydtech.modules.inv.utils.TransferUtils.*;
  30. /**
  31. * @author Administrator
  32. * @description 针对表【ins_account_operate(账户操作记录表)】的数据库操作Service实现
  33. * @createDate 2024-01-04 18:29:20
  34. */
  35. @Service
  36. public class InvAccountOperateServiceImpl extends ServiceImpl<InvAccountOperateMapper, InvAccountOperate>
  37. implements InvAccountOperateService {
  38. @Autowired
  39. private InvAccountOperateMapper invAccountOperateMapper;
  40. @Autowired
  41. private InvAccountOpService invAccountOpService;
  42. @Autowired
  43. private InvAccountCardService invAccountCardService;
  44. @Autowired
  45. private InvAccountService invAccountService;
  46. @Autowired
  47. private InvAccountExcelService invAccountExcelService;
  48. @Autowired
  49. private InvAccountProfitService invAccountProfitService;
  50. @Autowired
  51. private InvAccountIncoiceService invAccountIncoiceService;
  52. @Value("${upload.file.path}")
  53. String uploadFilePath;
  54. /**
  55. * 查询操作列表
  56. *
  57. * @param accountOperate
  58. * @return
  59. */
  60. @Override
  61. public IPage<InvAccountOperate> selectList(InvAccountOperate accountOperate) {
  62. PageRequest pageRequest = new PageRequest();
  63. pageRequest.setPageNum(accountOperate.getPageNo());
  64. pageRequest.setPageSize(accountOperate.getPageSize());
  65. Page page = PageRequest.buildPageRequest(pageRequest);
  66. IPage<InvAccountOperate> invAccountOperateIPage = invAccountOperateMapper.selectInsAccountOperate(page, accountOperate);
  67. List<InvAccountOperate> records = invAccountOperateIPage.getRecords();
  68. for (InvAccountOperate record : records) {
  69. StringBuilder a = new StringBuilder();
  70. if (record.getPayMethod() != null) {
  71. String[] split = record.getPayMethod().split(",");
  72. for (int i = 0; i < split.length; i++) {
  73. a.append(InvPayMethodEnum.getNameByCode(split[i]));
  74. if (i < split.length - 1) {
  75. a.append(",");
  76. }
  77. }
  78. record.setPayMethod(a.toString());
  79. }
  80. }
  81. invAccountOperateIPage.setRecords(records);
  82. return invAccountOperateIPage;
  83. }
  84. /**
  85. * 外部转内部
  86. *
  87. * @param invAccountOperate
  88. * @return
  89. */
  90. @Override
  91. public HttpResult insert(InvAccountOperate invAccountOperate) {
  92. InvAccountCard entAccountCard = invAccountCardService.getById(invAccountOperate.getEnterCardId());
  93. InvAccountCard outAccountCard = invAccountCardService.getById(invAccountOperate.getOutCardId());
  94. InvAccount entAccount = invAccountService.getById(entAccountCard.getAccountId());
  95. InvAccount outAccount = invAccountService.getById(outAccountCard.getAccountId());
  96. String applyAmount = invAccountOperate.getApplyAmount();
  97. JSONObject outProfit = invAccountOperate.getOutProfit();
  98. JSONObject entProfit = invAccountOperate.getEntProfit();
  99. if (entAccount.getOuterFlag().equals("1")) {
  100. return HttpResult.error("此操作收入账户不可为外部账户");
  101. }
  102. if (entAccountCard.equals(outAccountCard)) {
  103. for (Map.Entry<String, Object> entry : outProfit.entrySet()) {
  104. for (Map.Entry<String, Object> entry2 : entProfit.entrySet()) {
  105. if (new BigDecimal(entry.getValue().toString()).compareTo(BigDecimal.ZERO) != 0 && new BigDecimal(entry2.getValue().toString()).compareTo(BigDecimal.ZERO) != 0) {
  106. if (entry.getKey().equals(entry2.getKey())) {
  107. throw new SystemException("无法向同公司同板块互相转账");
  108. }
  109. }
  110. }
  111. }
  112. }
  113. if (entAccount.getOuterFlag().equals("0") && !entAccountCard.getAccountId().equals(outAccountCard.getAccountId())) {
  114. InvAccountCard invAccount = outsideTransfer(outAccountCard, outAccount, entAccountCard, entAccount, applyAmount);
  115. invAccountCardService.updateById(invAccount);
  116. BaseController baseController = new BaseController();
  117. invAccountOperate.setCreateTime(LocalDateTime.now());
  118. invAccountOperate.setOutProfit(null);
  119. invAccountOperate.setEnterCardNum(entAccountCard.getBankCardNum());
  120. invAccountOperate.setCreateBy(baseController.getUserName());
  121. invAccountOperate.setCreateId(baseController.getUserId());
  122. invAccountOperateMapper.insert(invAccountOperate);
  123. }
  124. List<InvAccountOp> list = invAccountEntOpList(entProfit, entAccount, entAccountCard,invAccountOperate.getAccountOperateId());
  125. if (!entAccountCard.getAccountId().equals(outAccountCard.getAccountId())){
  126. list.forEach(invAccountOp -> {
  127. invAccountOp.setInvoiceId(invAccountOperate.getAccountOperateId());
  128. });
  129. }else {
  130. List<InvAccountOp> list1 = invAccountOutOpList(outProfit, outAccount, outAccountCard, invAccountOperate.getAccountOperateId());
  131. list.forEach(invAccountOp -> {
  132. invAccountOp.setInvoiceId(invAccountOperate.getAccountOperateId());
  133. });
  134. list1.forEach(invAccountOp -> {
  135. invAccountOp.setInvoiceId(invAccountOperate.getAccountOperateId());
  136. });
  137. list.addAll(list1);
  138. }
  139. invAccountOpService.saveBatch(list);
  140. return HttpResult.ok("新增收入成功");
  141. }
  142. @Override
  143. public int updateOperateByIncoiceId(InvAccountOperate invAccountIncoiceId) {
  144. return invAccountOperateMapper.updateOperateByIncoiceId(invAccountIncoiceId);
  145. }
  146. @Override
  147. public HttpResult excelAccount(InvAccountOperate accountOperate) {
  148. List<InvAccountOperate> invAccountOperates = invAccountOperateMapper.selectInsAccountOperateList(accountOperate);
  149. List<InvAccountProfit> list = invAccountProfitService.list();
  150. for (InvAccountOperate record : invAccountOperates) {
  151. StringBuilder a = new StringBuilder();
  152. if (record.getPayMethod() != null) {
  153. String[] split = record.getPayMethod().split(",");
  154. for (String i : split) {
  155. a.append(InvPayMethodEnum.getNameByCode(i)).append(",");
  156. }
  157. record.setPayMethod(a.toString());
  158. }
  159. if (record.getOutProfit() != null) {
  160. StringBuffer buffer = new StringBuffer();
  161. for (Map.Entry<String, Object> entry : record.getOutProfit().entrySet()) {
  162. list.forEach(invAccountProfit -> {
  163. if (invAccountProfit.getProfitEng().equals(entry.getKey())) {
  164. buffer.append(invAccountProfit.getProfitName()).append(" ");
  165. }
  166. });
  167. }
  168. record.setPlate(buffer.toString());
  169. }
  170. }
  171. File folder = new File(uploadFilePath);
  172. if (folder.exists()) {
  173. return HttpResult.ok("", EasyExcelUtils.noModelOperateWrite(uploadFilePath, list, invAccountOperates));
  174. } else {
  175. if (folder.mkdirs()) {
  176. return HttpResult.ok("", EasyExcelUtils.noModelOperateWrite(uploadFilePath, list, invAccountOperates));
  177. } else {
  178. throw new SystemException("文件导出失败");
  179. }
  180. }
  181. }
  182. @Override
  183. public HttpResult deleteById(String accountOperateId) {
  184. InvAccountOperate invAccountOperate = invAccountOperateMapper.selectById(accountOperateId);
  185. if (invAccountOperate == null) {
  186. throw new SystemException("操作为空无法删除");
  187. }
  188. List<InvAccountOperate> invAccountOperates = new ArrayList<>();
  189. if (invAccountOperate.getDel() != null) {
  190. LambdaQueryWrapper<InvAccountOperate> queryWrapper = new LambdaQueryWrapper<InvAccountOperate>();
  191. queryWrapper.eq(InvAccountOperate::getDel, invAccountOperate.getDel());
  192. invAccountOperates = invAccountOperateMapper.selectList(queryWrapper);
  193. }
  194. if (invAccountOperates.isEmpty()){
  195. invAccountOperates.add(invAccountOperate);
  196. }
  197. if (invAccountOperate.getRevenueOutlay().equals("0")) {
  198. String outCardNum = invAccountOperate.getOutCardNum();
  199. String enterCardNum = invAccountOperate.getEnterCardNum();
  200. InvAccountCard outAccountCard = invAccountCardService.getById(outCardNum);
  201. InvAccountCard entAccountCard = invAccountCardService.getById(enterCardNum);
  202. InvAccount entAccount = invAccountService.getById(entAccountCard.getAccountId());
  203. List<InvAccountCard> invAccountCards = quashOutsideTransfer(outAccountCard, entAccountCard, new BigDecimal(invAccountOperate.getApplyAmount()),entAccount);
  204. List<String> accountOperateIds = invAccountOperates.stream()
  205. .map(InvAccountOperate::getAccountOperateId)
  206. .distinct()
  207. .collect(Collectors.toList());
  208. BaseController baseController = new BaseController();
  209. invAccountOperates.forEach(invAccountOperate1 -> {
  210. invAccountOperate1.setUpdateBy(baseController.getUserName());
  211. invAccountOperate1.setUpdateId(baseController.getUserId());
  212. invAccountOperate1.setUpdateTime(LocalDateTime.now());
  213. invAccountOperate1.setDelFlag("1");
  214. });
  215. LambdaUpdateWrapper<InvAccountOp> lambdaUpdateWrapper = new LambdaUpdateWrapper<InvAccountOp>();
  216. lambdaUpdateWrapper.in(InvAccountOp::getAccountOperateId, accountOperateIds);
  217. lambdaUpdateWrapper.set(InvAccountOp::getDelFlag,"1");
  218. if(invAccountOperate.getInvoiceId() != null){
  219. InvAccountIncoice byId = invAccountIncoiceService.getById(invAccountOperate.getInvoiceId());
  220. if (new BigDecimal(byId.getPayoutMoney()).compareTo(new BigDecimal(invAccountOperate.getApplyAmount())) < 0){
  221. throw new SystemException("撤销金额大于已进账金额");
  222. }
  223. byId.setPayoutMoney(new BigDecimal(byId.getPayoutMoney()).subtract(new BigDecimal(invAccountOperate.getApplyAmount())).toString());
  224. byId.setNotPayoutMoney(new BigDecimal(byId.getNotPayoutMoney()).add(new BigDecimal(invAccountOperate.getApplyAmount())).toString());
  225. invAccountIncoiceService.updateById(byId);
  226. }
  227. if (invAccountOperate.getExcelAccountOperateId() != null){
  228. InvAccountExcel invAccountExcel = new InvAccountExcel();
  229. invAccountExcel.setExcelAccountOperateId(invAccountOperate.getExcelAccountOperateId());
  230. invAccountExcel.setUpdateBy(baseController.getUserName());
  231. invAccountExcel.setUpdateTime(new Date());
  232. invAccountExcel.setTransferFlag("1");
  233. invAccountExcelService.updateById(invAccountExcel);
  234. }
  235. invAccountOpService.update(lambdaUpdateWrapper);
  236. invAccountCardService.updateBatchById(invAccountCards);
  237. InvAccountOperateService invAccountOperateService = new InvAccountOperateServiceImpl();
  238. invAccountOperateService.updateBatchById(invAccountOperates);
  239. return HttpResult.ok("删除成功");
  240. } else {
  241. return HttpResult.error("此操作为收入账户,无法删除");
  242. }
  243. }
  244. }