InvAccountServiceImpl.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package com.ydtech.modules.inv.service.impl;
  2. import com.alibaba.excel.util.ListUtils;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.ydtech.core.page.HttpResult;
  8. import com.ydtech.core.page.PageRequest;
  9. import com.ydtech.exception.SystemException;
  10. import com.ydtech.modules.admin.constants.BusinessTypeEnum;
  11. import com.ydtech.modules.inv.dao.InvAccountMapper;
  12. import com.ydtech.modules.inv.model.*;
  13. import com.ydtech.modules.inv.model.dto.InvAccountCardQueryDto;
  14. import com.ydtech.modules.inv.model.dto.InvAccountOpDto;
  15. import com.ydtech.modules.inv.model.excel.AccountExcel;
  16. import com.ydtech.modules.inv.model.vo.InvAccountQueryVo;
  17. import com.ydtech.modules.inv.service.InvAccountOpService;
  18. import com.ydtech.modules.inv.service.InvAccountProfitService;
  19. import com.ydtech.modules.inv.service.InvAccountService;
  20. import com.ydtech.modules.inv.utils.EasyExcelUtils;
  21. import com.ydtech.modules.inv.utils.LocalDateTimeUtils;
  22. import com.ydtech.modules.order.entity.BasePageResult;
  23. import org.joda.time.LocalDateTime;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.stereotype.Service;
  27. import java.io.File;
  28. import java.math.BigDecimal;
  29. import java.math.RoundingMode;
  30. import java.text.DecimalFormat;
  31. import java.util.*;
  32. import java.util.stream.Collectors;
  33. /**
  34. * @author Administrator
  35. * @description 针对表【ins_account(账户信息表)】的数据库操作Service实现
  36. * @createDate 2024-01-04 18:29:20
  37. */
  38. @Service
  39. public class InvAccountServiceImpl extends ServiceImpl<InvAccountMapper, InvAccount>
  40. implements InvAccountService {
  41. @Autowired
  42. private InvAccountMapper insAccountMapper;
  43. @Autowired
  44. private InvAccountProfitService invAccountProfitService;
  45. @Autowired
  46. private InvAccountOpService invAccountOpService;
  47. @Value("${upload.file.path}")
  48. String uploadFilePath;
  49. /**
  50. * 分页查询
  51. *
  52. * @return
  53. */
  54. @Override
  55. public IPage<InvAccount> selectAccountsListPage(InvAccountQueryVo invAccountQueryVo) {
  56. PageRequest pageRequest = new PageRequest();
  57. pageRequest.setPageNum(invAccountQueryVo.getPageNo());
  58. pageRequest.setPageSize(invAccountQueryVo.getPageSize());
  59. invAccountQueryVo.setInvoiceTimeStart(LocalDateTimeUtils.get12MonthsBefore()); // 获取12月前月的第一天
  60. invAccountQueryVo.setInvoiceTimeEnd(LocalDateTimeUtils.getTodayLastDay()); //获取当前月最后一天
  61. List<InvAccount> invAccountIPage = insAccountMapper.selectAccountsListPage(invAccountQueryVo);
  62. for (InvAccount invAccount : invAccountIPage) {
  63. if (invAccount.getInvoicingAmountExcess() != null) {
  64. String invoicingAmountExcess = invAccount.getInvoicingAmountExcess();
  65. BigDecimal amount = new BigDecimal(invoicingAmountExcess);
  66. BigDecimal roundedAmount = amount.setScale(2, RoundingMode.HALF_UP);
  67. invAccount.setInvoicingAmountExcess(roundedAmount.toString());
  68. } else {
  69. invAccount.setInvoicingAmountExcess(null);
  70. }
  71. }
  72. //计算当前季度剩余300000减去
  73. invAccountQueryVo.setInvoiceTimeStart(LocalDateTimeUtils.quarterStartTime());
  74. invAccountQueryVo.setInvoiceTimeEnd(LocalDateTimeUtils.quarterEndTime());
  75. List<InvAccount> invAccountIPage1 = insAccountMapper.selectAccountsListPage(invAccountQueryVo);
  76. //将当前季度额度赋值
  77. List<InvAccount> collect = invAccountIPage.stream()
  78. .map(rel -> invAccountIPage1.stream()
  79. .filter(ord -> Objects.equals(rel.getAccountId(), ord.getAccountId()))
  80. .findFirst()
  81. .map(ord -> {
  82. if (ord.getInvoicingAmountExcess() != null) {
  83. rel.setQuarterAmountExcess(new BigDecimal("300000").subtract(new BigDecimal("5000000").subtract(new BigDecimal(ord.getInvoicingAmountExcess()))).toString());
  84. rel.setSumInvoicingAmount("0");
  85. } else {
  86. if (!rel.getBusinessQuality().equals("0")&&rel.getOuterFlag().equals("0")) {
  87. rel.setQuarterAmountExcess("300000");
  88. }
  89. rel.setSumInvoicingAmount("0");
  90. if (rel.getInvoicingAmountExcess() == null){
  91. rel.setInvoicingAmountExcess(rel.getInvoicingAmount());
  92. }
  93. }
  94. return rel;
  95. }).orElse(null)
  96. ).collect(Collectors.toList());
  97. //将总开票额度赋值
  98. List<InvAccount> sumInvoicingAmount = insAccountMapper.getSumInvoicingAmount(invAccountQueryVo);
  99. List<InvAccount> collect1 = collect.stream()
  100. .map(rel -> sumInvoicingAmount.stream()
  101. .filter(ord -> Objects.equals(rel.getAccountId(), ord.getAccountId()))
  102. .findFirst()
  103. .map(ord -> {
  104. if (ord.getSumInvoicingAmount() != null) {
  105. BigDecimal sumInvoicingAmountDecimal = new BigDecimal(rel.getSumInvoicingAmount()).add(new BigDecimal(ord.getSumInvoicingAmount()));
  106. DecimalFormat df = new DecimalFormat("0.00");
  107. String sumInvoicingAmountString = df.format(sumInvoicingAmountDecimal);
  108. rel.setSumInvoicingAmount(sumInvoicingAmountString);
  109. }
  110. return rel;
  111. }).orElse(null)
  112. ).collect(Collectors.toList());
  113. //本季度开票总额度和本年开票总额度
  114. List<InvAccountSumAmount> sumYearAmount = insAccountMapper.getYearAndQuarterAmount();
  115. collect1.forEach(invAccount -> {
  116. if (invAccount != null) {
  117. if (invAccount.getInvAccountCardList().size() < 2) {
  118. if (invAccount.getInvAccountCardList().get(0).getBankCardNum() == null) {
  119. invAccount.setInvAccountCardList(null);
  120. }
  121. }
  122. if (invAccount.getBusinessQuality().equals("3")) {
  123. invAccount.setQuarterAmountExcess(null);
  124. invAccount.setInvoicingAmountExcess(null);
  125. }
  126. }
  127. boolean found = false;
  128. for (InvAccountSumAmount yearAmount : sumYearAmount) {
  129. if (yearAmount.getAccountId().equals(invAccount.getAccountId())) {
  130. DecimalFormat df = new DecimalFormat("0.00");
  131. String formattedYearAmount = df.format(Double.parseDouble(yearAmount.getYearAmount()));
  132. String formattedQuarterAmount = df.format(Double.parseDouble(yearAmount.getQuarterAmount()));
  133. invAccount.setYearAmount(formattedYearAmount);
  134. invAccount.setQuarterAmount(formattedQuarterAmount);
  135. found = true;
  136. }
  137. }
  138. if (!found) {
  139. invAccount.setYearAmount("0.00");
  140. invAccount.setQuarterAmount("0.00");
  141. }
  142. });
  143. IPage ipage = PageRequest.buildPageRequest(pageRequest);
  144. ipage.setRecords(collect1.stream().skip((invAccountQueryVo.getPageNo() - 1) * invAccountQueryVo.getPageSize()).limit(invAccountQueryVo.getPageSize()).
  145. collect(Collectors.toList()));
  146. ipage.setTotal(collect1.size());
  147. return ipage;
  148. }
  149. /**
  150. * 根据户名分类
  151. *
  152. * @return
  153. */
  154. @Override
  155. public HttpResult selectByAccountName() {
  156. InvAccountQueryVo invAccountQueryVo = new InvAccountQueryVo();
  157. List<InvAccount> insAccounts = insAccountMapper.selectAccountsList(invAccountQueryVo);
  158. if (!insAccounts.isEmpty()) {
  159. insAccounts.forEach(invAccountN -> {
  160. if (invAccountN.getInvAccountCardList().size() < 2) {
  161. if (invAccountN.getInvAccountCardList().get(0).getBankCardNum() == null) {
  162. invAccountN.setInvAccountCardList(null);
  163. }
  164. }
  165. invAccountN.setBusinessQuality(BusinessTypeEnum.getBusinessType(invAccountN.getBusinessQuality()));
  166. });
  167. return HttpResult.ok("", insAccounts);
  168. }
  169. return HttpResult.error("查询错误");
  170. }
  171. @Override
  172. public HttpResult selectAccountExcel(InvAccountQueryVo invAccountQueryVo) {
  173. List<InvAccount> insAccounts = insAccountMapper.selectAccountsList(invAccountQueryVo);
  174. List<InvAccountProfit> invAccountProfitList = invAccountProfitService.list();
  175. LambdaQueryWrapper<InvAccountOp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  176. lambdaQueryWrapper.eq(InvAccountOp::getDelFlag,"0");
  177. List<InvAccountOp> list = invAccountOpService.list(lambdaQueryWrapper);
  178. File folder = new File(uploadFilePath);
  179. if (folder.exists()) {
  180. return HttpResult.ok("", EasyExcelUtils.noModelWrite(uploadFilePath, invAccountProfitList, insAccounts, list));
  181. } else {
  182. if (folder.mkdirs()) {
  183. return HttpResult.ok("", EasyExcelUtils.noModelWrite(uploadFilePath, invAccountProfitList, insAccounts, list));
  184. } else {
  185. throw new SystemException("文件导出失败");
  186. }
  187. }
  188. }
  189. @Override
  190. public HttpResult selectAccountList(InvAccountQueryVo invAccountQueryVo) {
  191. invAccountQueryVo.setInvoiceTimeStart(LocalDateTimeUtils.get12MonthsBefore()); // 获取12月前月的第一天
  192. invAccountQueryVo.setInvoiceTimeEnd(LocalDateTimeUtils.getTodayLastDay()); //获取当前月最后一天
  193. List<InvAccount> invAccountIPage = insAccountMapper.selectAccountsListPage(invAccountQueryVo);
  194. //计算当前季度剩余300000减去
  195. invAccountQueryVo.setInvoiceTimeStart(LocalDateTimeUtils.quarterStartTime());
  196. invAccountQueryVo.setInvoiceTimeEnd(LocalDateTimeUtils.quarterEndTime());
  197. List<InvAccount> invAccountIPage1 = insAccountMapper.selectAccountsListPage(invAccountQueryVo);
  198. //将当前季度额度赋值
  199. List<InvAccount> collect = invAccountIPage.stream()
  200. .map(rel -> invAccountIPage1.stream()
  201. .filter(ord -> Objects.equals(rel.getAccountId(), ord.getAccountId()))
  202. .findFirst()
  203. .map(ord -> {
  204. rel.setSumInvoicingAmount("0");
  205. if (ord.getInvoicingAmountExcess() != null) {
  206. rel.setQuarterAmountExcess(new BigDecimal("300000").subtract(new BigDecimal("5000000").subtract(new BigDecimal(ord.getInvoicingAmountExcess()))).toString());
  207. } else {
  208. if (!rel.getBusinessQuality().equals("0")&&rel.getOuterFlag().equals("0")) {
  209. rel.setQuarterAmountExcess("300000");
  210. }
  211. rel.setSumInvoicingAmount("0");
  212. if (rel.getInvoicingAmountExcess() == null){
  213. rel.setInvoicingAmountExcess(rel.getInvoicingAmount());
  214. }
  215. }
  216. return rel;
  217. }).orElse(null)
  218. ).collect(Collectors.toList());
  219. //将总开票额度赋值
  220. List<InvAccount> sumInvoicingAmount = insAccountMapper.getSumInvoicingAmount(invAccountQueryVo);
  221. List<InvAccount> collect1 = collect.stream()
  222. .map(rel -> sumInvoicingAmount.stream()
  223. .filter(ord -> Objects.equals(rel.getAccountId(), ord.getAccountId()))
  224. .findFirst()
  225. .map(ord -> {
  226. if (ord.getSumInvoicingAmount() != null) {
  227. BigDecimal sumSumInvoicingAmountDecimal = new BigDecimal(rel.getSumInvoicingAmount()).add(new BigDecimal(ord.getSumInvoicingAmount()));
  228. DecimalFormat df = new DecimalFormat("0.00");
  229. String sumQuarterAmountExcessString = df.format(sumSumInvoicingAmountDecimal);
  230. rel.setSumInvoicingAmount(sumQuarterAmountExcessString);
  231. }
  232. return rel;
  233. }).orElse(null)
  234. ).collect(Collectors.toList());
  235. collect1.forEach(invAccount -> {
  236. if (invAccount != null) {
  237. if (invAccount.getInvAccountCardList().size() < 2) {
  238. if (invAccount.getInvAccountCardList().get(0).getBankCardNum() == null) {
  239. invAccount.setInvAccountCardList(null);
  240. }
  241. }
  242. if (invAccount.getBusinessQuality().equals("3")) {
  243. invAccount.setQuarterAmountExcess(null);
  244. invAccount.setInvoicingAmountExcess(null);
  245. }
  246. }
  247. });
  248. List<AccountExcel> accountExcels = new ArrayList<>();
  249. collect1.forEach(invAccount -> {
  250. if (invAccount.getInvAccountCardList() != null) {
  251. AccountExcel accountExcel = new AccountExcel();
  252. accountExcel.setAccountId(invAccount.getAccountId());
  253. accountExcel.setAccountName(invAccount.getAccountName());
  254. if (invAccount.getBusinessQuality() != null) {
  255. if (invAccount.getBusinessQuality().equals("3")) {
  256. accountExcel.setBusinessQuality("一般纳税");
  257. } else if (invAccount.getBusinessQuality().equals("1")) {
  258. accountExcel.setBusinessQuality("个体户");
  259. } else if (invAccount.getBusinessQuality().equals("2")) {
  260. accountExcel.setBusinessQuality("小规模");
  261. }
  262. }
  263. accountExcel.setOwned(invAccount.getOwned());
  264. if (invAccount.getEstablishTime() != null) {
  265. LocalDateTime establishTime = LocalDateTime.fromDateFields(invAccount.getEstablishTime());
  266. accountExcel.setEstablishTime(establishTime.toString().substring(0, establishTime.toString().indexOf("T")));
  267. }
  268. List<InvAccountSumAmount> sumYearAmount = insAccountMapper.getYearAndQuarterAmount();
  269. boolean found = false;
  270. for (InvAccountSumAmount yearAmount : sumYearAmount) {
  271. if (yearAmount.getAccountId().equals(invAccount.getAccountId())) {
  272. DecimalFormat df = new DecimalFormat("0.00");
  273. String formattedYearAmount = df.format(Double.parseDouble(yearAmount.getYearAmount()));
  274. String formattedQuarterAmount = df.format(Double.parseDouble(yearAmount.getQuarterAmount()));
  275. accountExcel.setYearAmount(formattedYearAmount);
  276. accountExcel.setQuarterAmount(formattedQuarterAmount);
  277. found = true;
  278. }
  279. }
  280. if (!found) {
  281. accountExcel.setYearAmount("0.00");
  282. accountExcel.setQuarterAmount("0.00");
  283. }
  284. accountExcel.setSumInvoicingAmount(invAccount.getSumInvoicingAmount());
  285. accountExcel.setQuarterAmountExcess(invAccount.getQuarterAmountExcess());
  286. accountExcel.setInvoicingAmountExcess(invAccount.getInvoicingAmountExcess());
  287. accountExcel.setEstimate(invAccount.getTentativeEstimates());
  288. accountExcels.add(accountExcel);
  289. }
  290. });
  291. return HttpResult.ok("", EasyExcelUtils.downExcel(uploadFilePath, AccountExcel.class, accountExcels));
  292. }
  293. @Override
  294. public HttpResult look(InvAccountQueryVo invAccountQueryVo) {
  295. List<InvAccount> insAccounts = insAccountMapper.selectAccountsList(invAccountQueryVo);
  296. List<InvAccountProfit> invAccountProfitList = invAccountProfitService.list();
  297. LambdaQueryWrapper<InvAccountOp> queryWrapper = new LambdaQueryWrapper<InvAccountOp>();
  298. queryWrapper.eq(InvAccountOp::getDelFlag,"0");
  299. List<InvAccountOp> list = invAccountOpService.list(queryWrapper);
  300. List<Map<String, String>> head = head(invAccountProfitList);
  301. List<Map<String, String>> maps = dataList(insAccounts, list, invAccountProfitList);
  302. List<Map<String, String>> subList = maps.stream().skip((invAccountQueryVo.getPageNo()-1)*invAccountQueryVo.getPageSize()).limit(invAccountQueryVo.getPageSize()).
  303. collect(Collectors.toList());
  304. InvAccountOpDto invAccountOpDto = new InvAccountOpDto();
  305. invAccountOpDto.setHead(head);
  306. invAccountOpDto.setMaps(subList);
  307. invAccountOpDto.setTotal(maps.size());
  308. invAccountOpDto.setPageSize(invAccountQueryVo.getPageSize());
  309. invAccountOpDto.setPageNo(invAccountQueryVo.getPageNo());
  310. return HttpResult.ok("",invAccountOpDto);
  311. }
  312. private static List<Map<String, String>> head(List<InvAccountProfit> invAccountProfitList) {
  313. List<Map<String, String>> list = ListUtils.newArrayList();
  314. Map<String, String> mm = new HashMap<>();
  315. mm.put("accountXing", "账户性质");
  316. mm.put("accountName", "户名");
  317. mm.put("bank", "开户银行");
  318. mm.put("banlce", "银行卡余额");
  319. for (Map.Entry<String, String> m : mm.entrySet()) {
  320. Map<String, String> map = new HashMap<>();
  321. map.put(m.getKey(), m.getValue());
  322. map.put("eng",m.getKey());
  323. list.add(map);
  324. }
  325. invAccountProfitList.forEach(invAccountProfit -> {
  326. Map<String, String> map = new HashMap<>();
  327. map.put(invAccountProfit.getProfitEng(), invAccountProfit.getProfitName());
  328. map.put("eng",invAccountProfit.getProfitEng());
  329. list.add(map);
  330. });
  331. return list;
  332. }
  333. private static List<Map<String,String>> dataList(List<InvAccount> invAccounts, List<InvAccountOp> invAccountOpList, List<InvAccountProfit> invAccountProfitList) {
  334. List<Map<String,String>> map = new ArrayList<>();
  335. invAccounts.forEach(invAccount -> {
  336. invAccount.getInvAccountCardList().forEach(invAccountCard -> {
  337. Map<String, String> data = new HashMap<>();
  338. if (invAccountCard.getBankCardNum() != null) {
  339. data.put("accountXing",invAccount.getOuterFlag().equals("0") ? "内部账户" : "外部账户");
  340. data.put("accountName",invAccount.getAccountName());
  341. data.put("bank",invAccountCard.getBankName());
  342. data.put("banlce",invAccountCard.getBalance());
  343. invAccountProfitList.forEach(invAccountProfitList1 -> {
  344. String a = "0";
  345. for (InvAccountOp invAccountProfit : invAccountOpList) {
  346. if (invAccountProfitList1.getProfitEng().equals(invAccountProfit.getProfit()) && invAccountCard.getBankCardNum().equals(invAccountProfit.getCardNum())) {
  347. a = new BigDecimal(a).add(new BigDecimal(invAccountProfit.getMoney())).toString();
  348. }
  349. }
  350. data.put(invAccountProfitList1.getProfitEng(),a);
  351. });
  352. map.add(data);
  353. }
  354. });
  355. });
  356. return map;
  357. }
  358. /**
  359. * @version
  360. * @author: hxl
  361. * @Date: 2024/7/8 10:43
  362. * @Description: 通过银行卡号和户名分页查询对象
  363. */
  364. @Override
  365. public BasePageResult<InvAccountCard> queryPage(InvAccountCardQueryDto invAccountCardQueryDto) {
  366. Page<InvAccountCard> page = new Page<>(invAccountCardQueryDto.getPageNum(),
  367. invAccountCardQueryDto.getPageSize());
  368. Page<InvAccountCard> result = insAccountMapper.queryPage(page, invAccountCardQueryDto);
  369. return new BasePageResult<>(invAccountCardQueryDto.getPageNum(), page.getSize(), result.getTotal(), page.getPages(),
  370. result.getRecords());
  371. }
  372. @Override
  373. public List<InvAccount> getInvAccountList(InvAccountQueryVo invAccountQueryVo) {
  374. return baseMapper.selectAccountsListPage(invAccountQueryVo);
  375. }
  376. }