DetailServiceImpl.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. package com.loan.system.service.Impl;
  2. import com.loan.system.config.FileUploadConfig;
  3. import com.loan.system.domain.dto.DetailDTO;
  4. import com.loan.system.domain.entity.*;
  5. import com.loan.system.domain.enums.DecisionEnum;
  6. import com.loan.system.domain.enums.StepEnum;
  7. import com.loan.system.domain.enums.StepPropertyEnum;
  8. import com.loan.system.domain.pojo.Result;
  9. import com.loan.system.domain.vo.*;
  10. import com.loan.system.service.*;
  11. import com.loan.system.utils.ResultUtil;
  12. import lombok.RequiredArgsConstructor;
  13. import org.apache.poi.xssf.usermodel.XSSFRow;
  14. import org.apache.poi.xssf.usermodel.XSSFSheet;
  15. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  16. import org.springframework.stereotype.Service;
  17. import javax.servlet.ServletOutputStream;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.time.LocalDate;
  22. import java.time.LocalDateTime;
  23. import java.time.format.DateTimeFormatter;
  24. import java.util.*;
  25. import java.util.stream.Collectors;
  26. import java.util.stream.Stream;
  27. @Service
  28. @RequiredArgsConstructor
  29. public class DetailServiceImpl implements DetailService {
  30. private final LoanService loanService;
  31. private final RepaymentService repaymentService;
  32. private final RepaymentRecordService repaymentRecordService;
  33. private final FileUploadConfig fileUploadConfig;
  34. private final ContractService contractService;
  35. private final ContractRepaymentService contractRepaymentService;
  36. private final CustomerService customerService;
  37. private final CustomerOtherService customerOtherService;
  38. private final RecommenderService recommenderService;
  39. private final StepService stepService;
  40. private final UserService userService;
  41. private final DisbursementService disbursementService;
  42. private final CollateralService collateralService;
  43. private final ContractAndCollateralService contractAndCollateralService;
  44. private final CollateralPlanService collateralPlanService;
  45. private final LocationDatumService locationDatumService;
  46. @Override
  47. public Result getArrangeReport(LocalDate begin) {
  48. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  49. String endTime = begin.atTime(23, 59, 59).format(fmt);
  50. List<LoanCaseStatistic> loanCaseStatistics = new ArrayList<>();
  51. //指派人员只需要显示进行中的业务
  52. List<LoanCaseSimpleVO> loanCases = loanService.findLoanCaseSimpleByIsComplete(DecisionEnum.PROCESS.getMsg());
  53. for (LoanCaseSimpleVO loanCase : loanCases){
  54. LoanCaseStatistic loanCaseStatistic = new LoanCaseStatistic();
  55. List<StepVO> steps = stepService.getChildStepByCaseIdBeforeTime(loanCase.getId(), endTime);
  56. //仅显示派单环节的信息
  57. for (StepVO step : steps){
  58. if (step.getStatus().equals(StepEnum.UNSTART.getMsg()) || step.getUserId1() == null)
  59. continue;
  60. if (!step.getCode().equals(StepPropertyEnum.EVIDENCE_CONFIRMATION.getCode()) && !step.getCode().equals(StepPropertyEnum.DELIVERY_CONFIRMATION.getCode())
  61. &&!step.getCode().equals(StepPropertyEnum.DISBURSE_START.getCode()) && !step.getCode().equals(StepPropertyEnum.BALANCE_REPAY.getCode()))
  62. continue;
  63. loanCaseStatistic.setWeekSeq(calculateWeekSequence(LocalDate.parse(step.getBeginTime(), fmt)));
  64. loanCaseStatistic.setDisburseDate(getDateAndPlace( step ,loanCase.getId()).get(0));//2.
  65. loanCaseStatistic.setCustomerName(customerService.findByCustomerIdAndIsDelete(loanCase.getCustomerId(), false).getName());//3.
  66. loanCaseStatistic.setBusinessType(loanCase.getBusinessType());//4.
  67. loanCaseStatistic.setAmount(loanCase.getTotalLoanAmount());//5.
  68. loanCaseStatistic.setStepName(step.getStepName());//6.
  69. if (step.getCode().equals(StepPropertyEnum.BALANCE_REPAY.getCode())||step.getCode().equals(StepPropertyEnum.DISBURSE_START.getCode())){
  70. List<String> data = getDateAndPlace(step, loanCase.getId());
  71. if (data.size()>=3)
  72. loanCaseStatistic.setUserName1(data.get(2));
  73. if (data.size()==4)
  74. loanCaseStatistic.setUserName2(data.get(3));
  75. }else {
  76. String[] ids = new String[]{};
  77. if (step.getUserIds() != null)
  78. ids = step.getUserIds().split(",");
  79. String userName1 = "";
  80. if (ids.length > 0)
  81. userName1 = userService.findByIdAndIsDelete(Long.parseLong(ids[0])).getRealName();
  82. loanCaseStatistic.setUserName1(userName1);//7.
  83. String userName2 = "";
  84. Stream<String> distinct = Arrays.stream(ids).distinct();
  85. List<Long> userIds = distinct.map(Long::parseLong).collect(Collectors.toList());
  86. if(userIds.size() > 1){
  87. List<User> users = userService.findByIdsAndIsDelete(userIds.subList(1, userIds.size()));//保证了第一个不会出现在后面
  88. userName2 = users.stream().map(User::getRealName).collect(Collectors.toList()).toString();
  89. }
  90. loanCaseStatistic.setUserName2(userName2);//8.
  91. }
  92. loanCaseStatistic.setStartDate(step.getBeginTime());//9.
  93. loanCaseStatistic.setPlace(getDateAndPlace( step ,loanCase.getId()).get(1));//10.
  94. loanCaseStatistic.setEndDate(step.getStatus().equals(StepEnum.COMPLETED.getMsg()) ? step.getUpdateTime() : step.getStatus());//11.
  95. loanCaseStatistics.add(loanCaseStatistic);
  96. }
  97. }
  98. // 对 loanCaseStatistics 进行排序
  99. loanCaseStatistics.sort(Comparator
  100. .comparing(LoanCaseStatistic::getStartDate)
  101. .thenComparing(LoanCaseStatistic::getUserName1));
  102. return ResultUtil.success("success",loanCaseStatistics);
  103. }
  104. @Override
  105. public Result getLoanCaseReport(LocalDate begin, LocalDate end) {
  106. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  107. String beginTime = begin.atStartOfDay().format(fmt);
  108. String endTime = end.atTime(23, 59, 59).format(fmt);
  109. List<LoanCaseStatistic> loanCaseStatistics = new ArrayList<>();
  110. List<LoanCaseSimpleVO> loanCases = loanService.findLoanCaseBetweenRange(beginTime, endTime);
  111. // 批量获取所有相关的客户ID
  112. Set<Long> customerIds = loanCases.stream()
  113. .map(LoanCaseSimpleVO::getCustomerId)
  114. .collect(Collectors.toSet());
  115. // 批量获取客户信息
  116. Map<Long, CustomerVO> customerMap = new HashMap<>();
  117. if (!customerIds.isEmpty()) {
  118. List<CustomerVO> customers = customerService.findByIdsInAndIsDelete(customerIds, false);
  119. customerMap = customers.stream().collect(Collectors.toMap(CustomerVO::getId, customer -> customer));
  120. }
  121. // 遍历loanCases并收集所有相关的步骤
  122. Map<Long, List<StepVO>> caseStepsMap = new HashMap<>();
  123. for (LoanCaseSimpleVO loanCase : loanCases) {
  124. List<StepVO> steps = stepService.getChildStepByCaseIdBetweenRange(loanCase.getId(), beginTime, endTime);
  125. caseStepsMap.put(loanCase.getId(), steps);
  126. }
  127. // 收集所有需要的用户ID用于批量查询
  128. Set<Long> userIds = new HashSet<>();
  129. for (LoanCaseSimpleVO loanCase : loanCases) {
  130. List<StepVO> steps = caseStepsMap.get(loanCase.getId());
  131. for (StepVO step : steps) {
  132. if (step.getStatus().equals(StepEnum.UNSTART.getMsg()) || step.getUserId1() == null)
  133. continue;
  134. // 添加主要用户ID
  135. if (step.getUserId1() != null) {
  136. userIds.add(step.getUserId1());
  137. }
  138. // 添加其他用户ID
  139. if (step.getUserIds() != null) {
  140. String[] ids = step.getUserIds().split(",");
  141. for (String idStr : ids) {
  142. try {
  143. Long userId = Long.parseLong(idStr.trim());
  144. userIds.add(userId);
  145. } catch (NumberFormatException e) {
  146. // 忽略无效的ID
  147. }
  148. }
  149. }
  150. }
  151. }
  152. // 批量获取用户信息
  153. Map<Long, User> userMap = new HashMap<>();
  154. if (!userIds.isEmpty()) {
  155. List<User> users = userService.findByIdsAndIsDelete(new ArrayList<>(userIds));
  156. userMap = users.stream().collect(Collectors.toMap(User::getId, user -> user));
  157. }
  158. // 处理数据
  159. for (LoanCaseSimpleVO loanCase : loanCases){
  160. List<StepVO> steps = caseStepsMap.get(loanCase.getId());
  161. for (StepVO step : steps){
  162. if (step.getStatus().equals(StepEnum.UNSTART.getMsg()) || step.getUserId1() == null)
  163. continue;
  164. LoanCaseStatistic loanCaseStatistic = new LoanCaseStatistic();
  165. loanCaseStatistic.setWeekSeq(calculateWeekSequence(LocalDate.parse(step.getBeginTime(), fmt)));
  166. List<String> dateAndPlace = getDateAndPlace(step, loanCase.getId());
  167. if(!dateAndPlace.isEmpty())
  168. loanCaseStatistic.setDisburseDate(dateAndPlace.get(0));//2.
  169. // 使用缓存的客户信息
  170. CustomerVO customer = customerMap.get(loanCase.getCustomerId());
  171. if (customer != null) {
  172. loanCaseStatistic.setCustomerName(customer.getName());//3.
  173. }
  174. loanCaseStatistic.setBusinessType(loanCase.getBusinessType());//4.
  175. loanCaseStatistic.setAmount(loanCase.getTotalLoanAmount());//5.
  176. loanCaseStatistic.setStepName(step.getStepName());//6.
  177. if (step.getCode().equals(StepPropertyEnum.BALANCE_REPAY.getCode())||step.getCode().equals(StepPropertyEnum.DISBURSE_START.getCode())){
  178. List<String> data = getDateAndPlace(step, loanCase.getId());
  179. if (data.size()>=3)
  180. loanCaseStatistic.setUserName1(data.get(2));
  181. if (data.size()==4)
  182. loanCaseStatistic.setUserName2(data.get(3));
  183. }else {
  184. String[] ids = new String[]{};
  185. if (step.getUserIds() != null)
  186. ids = step.getUserIds().split(",");
  187. String userName1 = "";
  188. if (ids.length > 0) {
  189. Long userId = Long.parseLong(ids[0]);
  190. User user = userMap.get(userId);
  191. if (user != null) {
  192. userName1 = user.getRealName();
  193. }
  194. }
  195. loanCaseStatistic.setUserName1(userName1);//7.
  196. String userName2 = "";
  197. Stream<String> distinct = Arrays.stream(ids).distinct();
  198. List<Long> userIdsList = distinct.map(Long::parseLong).collect(Collectors.toList());
  199. if(userIdsList.size() > 1){
  200. List<User> userList = new ArrayList<>();
  201. for (int i = 1; i < userIdsList.size(); i++) {
  202. User user = userMap.get(userIdsList.get(i));
  203. if (user != null) {
  204. userList.add(user);
  205. }
  206. }
  207. userName2 = userList.stream().map(User::getRealName).collect(Collectors.toList()).toString();
  208. }
  209. loanCaseStatistic.setUserName2(userName2);//8.
  210. }
  211. loanCaseStatistic.setStartDate(step.getBeginTime());//9.
  212. if(dateAndPlace.size()>1)
  213. loanCaseStatistic.setPlace(dateAndPlace.get(1));//10.
  214. loanCaseStatistic.setEndDate(step.getStatus().equals(StepEnum.COMPLETED.getMsg()) ? step.getUpdateTime() : step.getStatus());//11.
  215. loanCaseStatistics.add(loanCaseStatistic);
  216. }
  217. }
  218. // 对 loanCaseStatistics 进行排序
  219. loanCaseStatistics.sort(Comparator
  220. .comparing(LoanCaseStatistic::getStartDate)
  221. .thenComparing(LoanCaseStatistic::getUserName1));
  222. return ResultUtil.success("success",loanCaseStatistics);
  223. }
  224. @Override
  225. public void exportLoanCaseReport(DetailDTO<LoanCaseStatistic> detailDTO, LocalDate begin, LocalDate end, HttpServletResponse response) {
  226. List<LoanCaseStatistic> loanCaseStatistics = detailDTO.getData();
  227. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  228. String beginTime = begin.atStartOfDay().format(fmt);
  229. String endTime = end.atTime(23, 59, 59).format(fmt);
  230. List<LoanCaseStatistic> loanCaseStatisticList = new ArrayList<>();
  231. for (LoanCaseStatistic loanCaseStatistic : loanCaseStatistics){
  232. if (loanCaseStatistic.getEndDate() != null && !loanCaseStatistic.getEndDate().equals("进行中")){
  233. LocalDateTime endDate = LocalDateTime.parse(loanCaseStatistic.getEndDate(), fmt);
  234. LocalDateTime begin1 = LocalDateTime.parse(beginTime, fmt);
  235. LocalDateTime end1 = LocalDateTime.parse(endTime, fmt);
  236. if (endDate.isAfter(begin1) && endDate.isBefore(end1))
  237. loanCaseStatisticList.add(loanCaseStatistic);
  238. }
  239. }
  240. System.out.println(loanCaseStatisticList);
  241. //查询概览运营数据,提供给Excel模板文件
  242. InputStream inputStream = getClass().getClassLoader().getResourceAsStream("file_store/template/loanStatistics.xlsx");
  243. if (inputStream == null) {
  244. throw new RuntimeException("无法找到Excel模板文件: " + fileUploadConfig.getUploadDir() + "template/" + "loanStatistics.xlsx");
  245. }
  246. try {
  247. //基于提供好的模板文件创建一个新的Excel表格对象
  248. XSSFWorkbook excel = new XSSFWorkbook(inputStream);
  249. //获得Excel文件中的一个Sheet页
  250. XSSFSheet sheet = excel.getSheet("Sheet1");
  251. //TODO:row(从0开始)行,cell(从0开始)获取单元格
  252. //获得第1行
  253. XSSFRow row = sheet.getRow(0);
  254. row.getCell(1).setCellValue(begin.format(DateTimeFormatter.ofPattern("yyyy年M月")));
  255. //获取单元格
  256. for (int i = 0; i < loanCaseStatisticList.size(); i++) {
  257. LoanCaseStatistic loanCaseStatistic = loanCaseStatisticList.get(i);
  258. int rowIdx = i + 4;
  259. row = sheet.getRow(rowIdx);
  260. if (row == null) {
  261. row = sheet.createRow(rowIdx);
  262. }
  263. System.out.println(row);
  264. row.getCell(0).setCellValue(loanCaseStatistic.getWeekSeq());
  265. row.getCell(1).setCellValue(loanCaseStatistic.getStartDate());
  266. row.getCell(2).setCellValue(loanCaseStatistic.getCustomerName());
  267. row.getCell(3).setCellValue(loanCaseStatistic.getBusinessType());
  268. row.getCell(4).setCellValue(loanCaseStatistic.getAmount());
  269. row.getCell(5).setCellValue(loanCaseStatistic.getStepName());
  270. row.getCell(6).setCellValue(loanCaseStatistic.getUserName1());
  271. row.getCell(7).setCellValue(loanCaseStatistic.getUserName2());
  272. row.getCell(8).setCellValue(loanCaseStatistic.getStartDate());
  273. row.getCell(9).setCellValue(loanCaseStatistic.getPlace());
  274. row.getCell(10).setCellValue(loanCaseStatistic.getEndDate());
  275. }
  276. //通过输出流将文件下载到客户端浏览器中
  277. ServletOutputStream out = response.getOutputStream();
  278. excel.write(out);
  279. //关闭资源
  280. out.flush();
  281. out.close();
  282. excel.close();
  283. } catch (IOException e) {
  284. e.printStackTrace();
  285. }
  286. }
  287. @Override
  288. public Result getAccountReport(Long caseId, Integer pageNum, Integer pageSize) {
  289. int begin = pageNum * pageSize;
  290. List<LoanCaseDetails> loanCaseDetails = new ArrayList<>();
  291. //获取业务信息
  292. LoanCaseSimpleVO loanCase = loanService.findLoanCaseSimpleByIdAndIsDelete(caseId, false);
  293. //获取客户与其它客户信息
  294. Customer customer = customerService.findByIdAndIsDelete(loanCase.getCustomerId());
  295. List<CustomersOtherVO> customersOtherVOS = customerOtherService.findByCaseId(caseId);
  296. //获取回款记录信息(核心)
  297. List<RepaymentRecord> repaymentRecords = repaymentRecordService.findByRepaymentIdAndIsInterestAndIsDelete(repaymentService.findByCaseIdAndIsDelete(caseId, false).getId(), false);
  298. List<Long> recordIds = repaymentRecords.stream().map(RepaymentRecord::getId).collect(Collectors.toList());
  299. List<ContractRepayment> contractRepayments = contractRepaymentService.findByRepaymentRecordIdsAndIsDelete(recordIds);
  300. List<Long> contractIds = contractRepayments.stream().map(ContractRepayment::getContractId).collect(Collectors.toList());
  301. for (Long contractId : contractIds) {
  302. double amount = 0.0;//累计回款
  303. Contract contract = contractService.findContractById(contractId);
  304. for (ContractRepayment contractRepayment : contractRepayments) {
  305. if (contractRepayment.getContractId().equals(contractId)) {
  306. LoanCaseDetails loanCaseDetail = new LoanCaseDetails();
  307. loanCaseDetail.setContractNo(contract.getContractNo());//1
  308. // PawnTicketInfo pawnTicketInfo = pawnTicketService.findByContractIdAndIsDelete(contractId);
  309. // if(pawnTicketInfo!=null){
  310. // loanCaseDetail.setPawnTicketNo(pawnTicketInfo.getPawnTicketNo());//2.
  311. // loanCaseDetail.setRedeemTime(pawnTicketInfo.getEndTime());//16.
  312. // loanCaseDetail.setRedeemTicketNo(pawnTicketInfo.getRedeemTicketNo());//17.
  313. // }
  314. loanCaseDetail.setLoanTime(disbursementService.getLoanTime(caseId));//3.
  315. loanCaseDetail.setCustomerName(customer.getName());//4.
  316. loanCaseDetail.setCustomerName2(customersOtherVOS.get(0).getName());//5.
  317. loanCaseDetail.setPhoneNumber(customer.getMobile());//6.
  318. loanCaseDetail.setPawnAmount(loanCase.getTotalLoanAmount());//7.
  319. List<String> attributes = new ArrayList<>();
  320. List<String> locations = new ArrayList<>();
  321. List<ContractAndCollateral> contractAndCollaterals = contractAndCollateralService.listByContractId(contractId);
  322. for (ContractAndCollateral contractAndCollateral : contractAndCollaterals) {
  323. if (contractAndCollateral.getContractId().equals(contractId)){
  324. Collateral collateral = collateralService.findCollateralById(contractAndCollateral.getCollateralId());
  325. attributes.add(contract.getBusinessAttr());
  326. locations.add(collateral.getAddress());
  327. }
  328. }
  329. loanCaseDetail.setLocations( locations);
  330. loanCaseDetail.setAttributes(attributes);
  331. loanCaseDetail.setChannelName(loanCase.getChannelName());//9.
  332. List<String> userNames=new ArrayList<>();
  333. StepVO stepVO = stepService.findByStepCodeAndCaseId(StepPropertyEnum.BUSINESS_ACCEPT.getCode(), caseId);
  334. StepVO stepVO1 = stepService.findByStepCodeAndCaseId(StepPropertyEnum.PRE_TRIAL.getCode(), caseId);
  335. if(stepVO!=null){
  336. userNames.add(userService.findByIdAndIsDelete(stepVO.getUserId1()).getRealName());
  337. }
  338. if(stepVO1!=null){
  339. userNames.add(userService.findByIdAndIsDelete(stepVO1.getUserId1()).getRealName());
  340. }
  341. loanCaseDetail.setUserName(userNames);//10.
  342. amount += contractRepayment.getAmount();
  343. loanCaseDetail.setRepayTime(contractRepayment.getCreateTime());//11.
  344. loanCaseDetail.setRepayTotalAmount(amount);//12
  345. loanCaseDetail.setLastRepayAmount(contractRepayment.getAmount());//13.
  346. loanCaseDetail.setBalance(loanCase.getTotalLoanAmount() - amount);//14.
  347. if(Math.abs(loanCase.getTotalLoanAmount() - amount)<Double.MIN_VALUE)
  348. loanCaseDetail.setInterestAmount(contract.getInterestAmount());//15.
  349. loanCaseDetails.add(loanCaseDetail);
  350. }
  351. }
  352. }
  353. return ResultUtil.success("success", loanCaseDetails.stream().skip( begin).limit(pageSize).collect(Collectors.toList()));
  354. }
  355. @Override
  356. public void exportAccountReport(List<LoanCaseDetails> loanCaseDetails, Integer begin, Integer end, HttpServletResponse response) {
  357. //查询概览运营数据,提供给Excel模板文件
  358. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileUploadConfig.getUploadDir() + "业务台账.xlsx");
  359. try {
  360. //基于提供好的模板文件创建一个新的Excel表格对象
  361. XSSFWorkbook excel = new XSSFWorkbook(inputStream);
  362. //获得Excel文件中的一个Sheet页
  363. XSSFSheet sheet = excel.getSheet("Sheet1");
  364. //TODO:row(从0开始)行,cell(从0开始)获取单元格
  365. //获得第1行
  366. XSSFRow row = sheet.getRow(1);
  367. //获取单元格
  368. for (int i = begin; i < end; i++) {
  369. LoanCaseDetails loanCaseDetail = loanCaseDetails.get(i);
  370. row = sheet.getRow(i);
  371. row.getCell(0).setCellValue(loanCaseDetail.getContractNo());
  372. row.getCell(1).setCellValue(loanCaseDetail.getPawnTicketNo());
  373. row.getCell(2).setCellValue(loanCaseDetail.getLoanTime());
  374. row.getCell(3).setCellValue(loanCaseDetail.getCustomerName());
  375. row.getCell(4).setCellValue(loanCaseDetail.getCustomerName2());
  376. row.getCell(5).setCellValue(loanCaseDetail.getPhoneNumber());
  377. row.getCell(6).setCellValue(loanCaseDetail.getPawnAmount());
  378. row.getCell(7).setCellValue(loanCaseDetail.getLocations().toString());
  379. row.getCell(8).setCellValue(loanCaseDetail.getAttributes().toString());
  380. row.getCell(9).setCellValue(loanCaseDetail.getChannelName());
  381. row.getCell(10).setCellValue(loanCaseDetail.getUserName().toString());
  382. row.getCell(11).setCellValue(loanCaseDetail.getRepayTime());
  383. row.getCell(12).setCellValue(loanCaseDetail.getRepayTotalAmount());
  384. row.getCell(13).setCellValue(loanCaseDetail.getLastRepayAmount());
  385. row.getCell(14).setCellValue(loanCaseDetail.getBalance());
  386. row.getCell(15).setCellValue(loanCaseDetail.getInterestAmount());
  387. row.getCell(16).setCellValue(loanCaseDetail.getRedeemTime());
  388. row.getCell(17).setCellValue(loanCaseDetail.getRedeemTicketNo());
  389. row.getCell(18).setCellValue(loanCaseDetail.getComment());
  390. }
  391. //通过输出流将文件下载到客户端浏览器中
  392. ServletOutputStream out = response.getOutputStream();
  393. excel.write(out);
  394. //关闭资源
  395. out.flush();
  396. out.close();
  397. excel.close();
  398. } catch (IOException e) {
  399. e.printStackTrace();
  400. }
  401. }
  402. @Override
  403. public Result getDisbursementReport(Long caseId, Integer pageNum, Integer pageSize) {
  404. int begin = pageNum * pageSize;
  405. List<DisbursementDetails> disbursementDetails = new ArrayList<>();
  406. //获取业务信息
  407. LoanCaseSimpleVO loanCase = loanService.findLoanCaseSimpleByIdAndIsDelete(caseId, false);
  408. //获取合同信息
  409. List<ContractVO> contractVOS = contractService.findContractByCaseId(caseId);
  410. for(ContractVO contractVO : contractVOS){
  411. DisbursementDetails disbursementDetail = new DisbursementDetails();
  412. disbursementDetail.setContractNo(contractVO.getContractNo());//1.
  413. // PawnTicketInfo pawnTicketInfo = pawnTicketService.findByContractIdAndIsDelete(contractVO.getId());
  414. // if(pawnTicketInfo!=null){
  415. // disbursementDetail.setPawnTicketNo(pawnTicketInfo.getPawnTicketNo());//2.
  416. // disbursementDetail.setRedeemTime(pawnTicketInfo.getEndTime());//9.
  417. // disbursementDetail.setRedeemTicketNo(pawnTicketInfo.getRedeemTicketNo());//10.
  418. // }
  419. // disbursementDetail.setLoanTime(disbursementService.getLoanTime(caseId));//3.
  420. disbursementDetail.setCustomerName(customerService.findByCustomerIdAndIsDelete(loanCase.getCustomerId(), false).getName());//4.
  421. disbursementDetail.setPawnAmount(contractVO.getContractAmount());//5.
  422. List<String> userNames=new ArrayList<>();
  423. StepVO stepVO = stepService.findByStepCodeAndCaseId(StepPropertyEnum.BUSINESS_ACCEPT.getCode(), caseId);
  424. StepVO stepVO1 = stepService.findByStepCodeAndCaseId(StepPropertyEnum.PRE_TRIAL.getCode(), caseId);
  425. if(stepVO!=null){
  426. userNames.add(userService.findByIdAndIsDelete(stepVO.getUserId1()).getRealName());
  427. }
  428. if(stepVO1!=null){
  429. userNames.add(userService.findByIdAndIsDelete(stepVO1.getUserId1()).getRealName());
  430. }
  431. disbursementDetail.setUserName(userNames);//6.
  432. disbursementDetail.setRecommenderName(recommenderService.getRecommenderById(loanCase.getRecommenderId()).getRecommenderName());//7.
  433. disbursementDetail.setChannelName(loanCase.getChannelName());//8.
  434. disbursementDetails.add(disbursementDetail);
  435. }
  436. //获取当票
  437. return ResultUtil.success("success", disbursementDetails.stream().skip( begin).limit(pageSize).collect(Collectors.toList()));
  438. }
  439. @Override
  440. public void exportDisbursementReport(List<DisbursementDetails> disbursementDetails, Integer begin, Integer end, HttpServletResponse response) {
  441. //查询概览运营数据,提供给Excel模板文件
  442. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileUploadConfig.getUploadDir() + "出账明细.xlsx");
  443. try {
  444. //基于提供好的模板文件创建一个新的Excel表格对象
  445. XSSFWorkbook excel = new XSSFWorkbook(inputStream);
  446. //获得Excel文件中的一个Sheet页
  447. XSSFSheet sheet = excel.getSheet("Sheet1");
  448. //TODO:row(从0开始)行,cell(从0开始)获取单元格
  449. //获得第1行
  450. XSSFRow row = sheet.getRow(1);
  451. //获取单元格
  452. for (int i = begin; i < end; i++) {
  453. DisbursementDetails disbursementDetail = disbursementDetails.get(i);
  454. row = sheet.getRow(i);
  455. row.getCell(0).setCellValue(disbursementDetail.getContractNo());
  456. row.getCell(1).setCellValue(disbursementDetail.getLoanTime());
  457. row.getCell(2).setCellValue(disbursementDetail.getPawnTicketNo());
  458. row.getCell(3).setCellValue(disbursementDetail.getCustomerName());
  459. row.getCell(4).setCellValue(disbursementDetail.getPawnAmount());
  460. row.getCell(5).setCellValue(disbursementDetail.getUserName().toString());
  461. row.getCell(6).setCellValue(disbursementDetail.getRecommenderName());
  462. row.getCell(7).setCellValue(disbursementDetail.getChannelName());
  463. row.getCell(8).setCellValue(disbursementDetail.getRedeemTime());
  464. row.getCell(9).setCellValue(disbursementDetail.getRedeemTicketNo());
  465. }
  466. //通过输出流将文件下载到客户端浏览器中
  467. ServletOutputStream out = response.getOutputStream();
  468. excel.write(out);
  469. //关闭资源
  470. out.flush();
  471. out.close();
  472. excel.close();
  473. } catch (IOException e) {
  474. e.printStackTrace();
  475. }
  476. }
  477. @Override
  478. public Result repaymentReport(Long caseId, int begin, Integer pageSize) {
  479. List<RepaymentDetails> repaymentDetails = new ArrayList<>();
  480. //获取当票信息
  481. //获取业务信息
  482. LoanCaseSimpleVO loanCase = loanService.findLoanCaseSimpleByIdAndIsDelete(caseId, false);
  483. //获取客户与其它客户信息
  484. Customer customer = customerService.findByIdAndIsDelete(loanCase.getCustomerId());
  485. //获取回款记录信息(核心)
  486. List<RepaymentRecord> repaymentRecords = repaymentRecordService.findByRepaymentIdAndIsInterestAndIsDelete(repaymentService.findByCaseIdAndIsDelete(caseId, false).getId(), false);
  487. List<Long> recordIds = repaymentRecords.stream().map(RepaymentRecord::getId).collect(Collectors.toList());
  488. List<ContractRepayment> contractRepayments = contractRepaymentService.findByRepaymentRecordIdsAndIsDelete(recordIds);
  489. List<Long> contractIds = contractRepayments.stream().map(ContractRepayment::getContractId).collect(Collectors.toList());
  490. for (Long contractId : contractIds) {
  491. double amount = 0.0;//累计回款
  492. Contract contract = contractService.findContractById(contractId);
  493. for (ContractRepayment contractRepayment : contractRepayments) {
  494. if (contractRepayment.getContractId().equals(contractId)) {
  495. RepaymentDetails repaymentDetail = new RepaymentDetails();
  496. repaymentDetail.setContractNo(contract.getContractNo());//1
  497. // PawnTicketInfo pawnTicketInfo = pawnTicketService.findByContractIdAndIsDelete(contractId);
  498. // if(pawnTicketInfo!=null){
  499. // repaymentDetail.setPawnTicketNo(pawnTicketInfo.getPawnTicketNo());//2.
  500. // repaymentDetail.setRedeemTicketNo(pawnTicketInfo.getRedeemTicketNo());//10.
  501. // }
  502. repaymentDetail.setLoanTime(disbursementService.getLoanTime(caseId));//3.
  503. repaymentDetail.setCustomerName(customer.getName());//4.
  504. repaymentDetail.setPawnAmount(loanCase.getTotalLoanAmount());//5.
  505. repaymentDetail.setRepayTime(contractRepayment.getCreateTime());//6.
  506. repaymentDetail.setRepayAmount(contractRepayment.getAmount());//7.
  507. amount += contractRepayment.getAmount();
  508. repaymentDetail.setBalance(loanCase.getTotalLoanAmount() - amount);//8.
  509. if(Math.abs(loanCase.getTotalLoanAmount() - amount)<Double.MIN_VALUE)
  510. repaymentDetail.setInterestAmount(contract.getInterestAmount());//9.
  511. List<String> userNames=new ArrayList<>();
  512. StepVO stepVO = stepService.findByStepCodeAndCaseId(StepPropertyEnum.BUSINESS_ACCEPT.getCode(), caseId);
  513. StepVO stepVO1 = stepService.findByStepCodeAndCaseId(StepPropertyEnum.PRE_TRIAL.getCode(), caseId);
  514. if(stepVO!=null){
  515. userNames.add(userService.findByIdAndIsDelete(stepVO.getUserId1()).getRealName());
  516. }
  517. if(stepVO1!=null){
  518. userNames.add(userService.findByIdAndIsDelete(stepVO1.getUserId1()).getRealName());
  519. }
  520. repaymentDetail.setUserName(userNames);//11.
  521. repaymentDetail.setRecommenderName(recommenderService.getRecommenderById(loanCase.getRecommenderId()).getRecommenderName());//12.
  522. repaymentDetail.setChannelName(loanCase.getChannelName());//13.
  523. repaymentDetails.add(repaymentDetail);
  524. }
  525. }
  526. }
  527. return ResultUtil.success("success", repaymentDetails.stream().skip( begin).limit(pageSize).collect(Collectors.toList()));
  528. }
  529. @Override
  530. public void exportRepaymentReport(List<RepaymentDetails> repaymentDetails, Integer begin, Integer end, HttpServletResponse response) {
  531. //查询概览运营数据,提供给Excel模板文件
  532. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileUploadConfig.getUploadDir() + "出账明细.xlsx");
  533. try {
  534. //基于提供好的模板文件创建一个新的Excel表格对象
  535. XSSFWorkbook excel = new XSSFWorkbook(inputStream);
  536. //获得Excel文件中的一个Sheet页
  537. XSSFSheet sheet = excel.getSheet("Sheet1");
  538. //TODO:row(从0开始)行,cell(从0开始)获取单元格
  539. //获得第1行
  540. XSSFRow row = sheet.getRow(1);
  541. int index=1;
  542. //获取单元格
  543. for (int i = begin; i < end; i++) {
  544. RepaymentDetails repaymentDetail = repaymentDetails.get(i);
  545. row = sheet.getRow(index);
  546. row.getCell(0).setCellValue(repaymentDetail.getContractNo());
  547. row.getCell(1).setCellValue(repaymentDetail.getPawnTicketNo());
  548. row.getCell(2).setCellValue(repaymentDetail.getLoanTime());
  549. row.getCell(3).setCellValue(repaymentDetail.getCustomerName());
  550. row.getCell(4).setCellValue(repaymentDetail.getPawnAmount());
  551. row.getCell(5).setCellValue(repaymentDetail.getRepayTime());
  552. row.getCell(6).setCellValue(repaymentDetail.getRepayAmount());
  553. row.getCell(7).setCellValue(repaymentDetail.getBalance());
  554. row.getCell(8).setCellValue(repaymentDetail.getInterestAmount());
  555. row.getCell(9).setCellValue(repaymentDetail.getRedeemTicketNo());
  556. row.getCell(10).setCellValue(repaymentDetail.getUserName().toString());
  557. row.getCell(11).setCellValue(repaymentDetail.getRecommenderName());
  558. row.getCell(12).setCellValue(repaymentDetail.getChannelName());
  559. row.getCell(13).setCellValue(repaymentDetail.getComment());
  560. index++;
  561. }
  562. //通过输出流将文件下载到客户端浏览器中
  563. ServletOutputStream out = response.getOutputStream();
  564. excel.write(out);
  565. //关闭资源
  566. out.flush();
  567. out.close();
  568. excel.close();
  569. } catch (IOException e) {
  570. e.printStackTrace();
  571. }
  572. }
  573. private String calculateWeekSequence(LocalDate date) {
  574. // 获取月份的第一天
  575. LocalDate firstDayOfMonth = date.withDayOfMonth(1);
  576. // 获取第一天是星期几(1=Monday, 7=Sunday)
  577. int firstDayOfWeek = firstDayOfMonth.getDayOfWeek().getValue();
  578. // 计算指定日期是当月第几天
  579. int dayOfMonth = date.getDayOfMonth();
  580. // 计算是第几周 (W1, W2, W3...)TODO:取余计算需要从0开始
  581. // 例如:如果1号是周三,那么1-3号属于第一周,4-10号属于第二周...
  582. int weekSequence = ((dayOfMonth + firstDayOfWeek - 2) / 7) + 1;
  583. return "W" + weekSequence;
  584. }
  585. private List<String > getDateAndPlace(StepVO step , Long caseId){
  586. List<String> list = new ArrayList<>();//有两个,第一个是date,第二个是place
  587. String date = "";
  588. String place = "";
  589. String mainUSerName="";
  590. String assistUserName="";
  591. if (step.getCode().equals(StepPropertyEnum.EVIDENCE_CONFIRMATION.getCode())){
  592. List<CollateralPlan> collateralPlans = collateralPlanService.findByCaseIdAndIsDelete(caseId, false);
  593. for (CollateralPlan plan : collateralPlans){
  594. if (plan.getTime()== null || plan.getPlace()== null)
  595. continue;
  596. if (plan.getFlag().equals(DecisionEnum.ENTER_WAREHOUSE.getMsg())){
  597. date = date.isEmpty() ? plan.getTime() :date.concat("、").concat(plan.getTime());
  598. place = place.isEmpty() ? plan.getPlace() :place.concat("、").concat(plan.getPlace());
  599. }
  600. }
  601. list.add(date);
  602. list.add(place);
  603. }else if (step.getCode().equals(StepPropertyEnum.DELIVERY_CONFIRMATION.getCode())){
  604. List<CollateralPlan> collateralPlans = collateralPlanService.findByCaseIdAndIsDelete(caseId, false);
  605. for (CollateralPlan plan : collateralPlans){
  606. if (plan.getTime()== null || plan.getPlace()== null)
  607. continue;
  608. if (plan.getFlag().equals(DecisionEnum.OUT_WAREHOUSE.getMsg())){
  609. date = date.isEmpty() ? plan.getTime() :date.concat("、").concat(plan.getTime());
  610. place = place.isEmpty() ? plan.getPlace() :place.concat("、").concat(plan.getPlace());
  611. }
  612. }
  613. list.add(date);
  614. list.add(place);
  615. }else if (step.getCode().equals(StepPropertyEnum.DISBURSE_START.getCode())) {
  616. List<Disbursement> disbursements = disbursementService.getDisbursementByCaseId(caseId);
  617. for (Disbursement disbursement : disbursements){
  618. if(disbursement.getPlannedTime()!=null)
  619. date = date.isEmpty() ? disbursement.getPlannedTime() :date.concat("、").concat(disbursement.getPlannedTime());
  620. if(disbursement.getLocationId()!=null){
  621. LocationDatum locationDatum = locationDatumService.findById(disbursement.getLocationId());
  622. place = place.isEmpty() ? locationDatum.getSimpleAddress() :place.concat("、").concat(locationDatum.getSimpleAddress());
  623. }
  624. User user = userService.findByIdAndIsDelete(disbursement.getMainUserId());
  625. if (user !=null)
  626. mainUSerName = mainUSerName.isEmpty() ? user.getRealName() :mainUSerName.concat("、").concat(user.getRealName());
  627. User user1 = userService.findByIdAndIsDelete(disbursement.getAssistUserId());
  628. if (user1 !=null)
  629. assistUserName = assistUserName.isEmpty() ? user1.getRealName() :assistUserName.concat( "、").concat(user1.getRealName());
  630. }
  631. list.add( date);
  632. list.add(place);
  633. list.add(mainUSerName);
  634. list.add(assistUserName);
  635. }else if(step.getCode().equals(StepPropertyEnum.BALANCE_REPAY.getCode())){
  636. Repayment repayment = repaymentService.findByCaseIdAndIsDelete(caseId, false);
  637. if (repayment!=null){
  638. List<RepaymentRecord> records = repaymentRecordService.findByRepaymentIdAndIsInterestAndIsDelete(repayment.getId(), false);
  639. for (RepaymentRecord record : records){
  640. if (record.getRepayTime()!= null || record.getRepayLocation()== null)
  641. date = date.isEmpty() ? record.getRepayTime() :date.concat("、").concat(record.getRepayTime());
  642. if(record.getRepayLocation()!=null)
  643. place = place.isEmpty() ? record.getRepayLocation() :place.concat("、").concat(record.getRepayLocation());
  644. User user = userService.findByIdAndIsDelete(record.getMainUser());
  645. if (user !=null)
  646. mainUSerName = mainUSerName.isEmpty() ? user.getRealName() :mainUSerName.concat("、").concat(user.getRealName());
  647. User user1 = userService.findByIdAndIsDelete(record.getAssistUser());
  648. if (user1 !=null)
  649. assistUserName = assistUserName.isEmpty() ? user1.getRealName() :assistUserName.concat( "、").concat(user1.getRealName());
  650. }
  651. }
  652. list.add( date);
  653. list.add(place);
  654. list.add(mainUSerName);
  655. list.add(assistUserName);
  656. }
  657. return list;
  658. }
  659. }