InvAccountOperateServiceImpl.java 13 KB

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