StatisticsController.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. package com.loan.system.controller.wechat;
  2. import com.loan.system.config.FileUploadConfig;
  3. import com.loan.system.domain.entity.*;
  4. import com.loan.system.domain.enums.DecisionEnum;
  5. import com.loan.system.domain.enums.StepEnum;
  6. import com.loan.system.domain.enums.StepPropertyEnum;
  7. import com.loan.system.domain.pojo.Result;
  8. import com.loan.system.domain.vo.*;
  9. import com.loan.system.service.*;
  10. import com.loan.system.utils.ResultUtil;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import org.apache.commons.lang3.ObjectUtils;
  14. import org.apache.poi.ss.util.CellRangeAddress;
  15. import org.apache.poi.xssf.usermodel.XSSFRow;
  16. import org.apache.poi.xssf.usermodel.XSSFSheet;
  17. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.format.annotation.DateTimeFormat;
  20. import org.springframework.web.bind.annotation.*;
  21. import javax.servlet.ServletOutputStream;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.time.LocalDate;
  26. import java.time.LocalDateTime;
  27. import java.time.LocalTime;
  28. import java.time.format.DateTimeFormatter;
  29. import java.util.*;
  30. import java.util.stream.Collectors;
  31. import java.util.stream.Stream;
  32. @RestController
  33. @RequestMapping("/wechat/statistics")
  34. @Api(tags = "数据统计接口")
  35. public class StatisticsController {
  36. @Autowired
  37. private LoanService loanService;
  38. @Autowired
  39. private DisbursementService disbursementService;
  40. @Autowired
  41. private DisbursementRecordService disbursementRecordService;
  42. @Autowired
  43. private RepaymentService repaymentService;
  44. @Autowired
  45. private RepaymentRecordService repaymentRecordService;
  46. @Autowired
  47. private FileUploadConfig fileUploadConfig;
  48. @Autowired
  49. private ContractService contractService;
  50. @Autowired
  51. private CustomerService customerService;
  52. @Autowired
  53. private StepService stepService;
  54. @Autowired
  55. private UserService userService;
  56. @Autowired
  57. private CollateralPlanService collateralPlanService;
  58. @Autowired
  59. private LocationDatumService locationDatumService;
  60. @GetMapping("/dailyReport")
  61. @ApiOperation("获取日报表")
  62. public Result dailyReport(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
  63. @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {//TODO:利息也要
  64. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  65. String beginTime = begin.atStartOfDay().format(fmt);
  66. String endTime = begin.atTime(23, 59, 59).format(fmt);
  67. List<DailyReport> dailyReports =new ArrayList<>();
  68. //TODO:repayTime要改成数据库标准时间
  69. List<RepaymentRecord> records = repaymentRecordService.findBetweenRange(beginTime, endTime);
  70. for (RepaymentRecord record : records){
  71. DailyReport dailyReport = new DailyReport();
  72. Repayment repayment = repaymentService.findByIdAndIsDelete(record.getRepaymentId());
  73. Long caseId = repayment.getCaseId();
  74. LoanCaseSimpleVO loanCase = loanService.findLoanCaseSimpleByIdAndIsDelete(caseId, false);
  75. dailyReport.setCustomerName(customerService.findByIdAndIsDelete(loanCase.getCustomerId()).getName());//1.
  76. dailyReport.setLoanCaseAttribute(loanCase.getBusinessAttrs());//2.
  77. dailyReport.setDisbursementDate(disbursementService.getLoanTime(caseId));//3.
  78. dailyReport.setLoanAmount(loanCase.getTotalLoanAmount());//4.
  79. List<RepaymentRecord> repaymentRecords = repaymentRecordService.findByRepaymentIdAndIsDelete(record.getRepaymentId(), false);
  80. double totalRepayAmount = 0.0;
  81. for (RepaymentRecord repaymentRecord : repaymentRecords){
  82. if(!repaymentRecord.getIsInterest())
  83. totalRepayAmount += repaymentRecord.getAmount();
  84. }
  85. dailyReport.setRepayTotalAmount(totalRepayAmount);//5.
  86. dailyReport.setRepayDate(record.getRepayTime());//7.
  87. dailyReport.setRemainAmount(loanCase.getTotalLoanAmount()-totalRepayAmount);//6.
  88. if (dailyReport.getRemainAmount()<Double.MIN_VALUE){
  89. //还清才能结算利息
  90. List<ContractVO> contracts = contractService.findContractByCaseId(caseId);
  91. double totalInterest = 0.0;
  92. for (ContractVO contract : contracts)
  93. if (contract.getClearedStatus().equals(DecisionEnum.CLEAR_AMOUNT_YES.getMsg()))
  94. totalInterest += contract.getInterestAmount();
  95. dailyReport.setLoanInterest(totalInterest);//8.
  96. }
  97. dailyReports.add(dailyReport);
  98. }
  99. return ResultUtil.success("success",dailyReports);
  100. }
  101. @GetMapping("/dailyReport/export")
  102. @ApiOperation("导出日报表")
  103. public void exportDailyReport(@RequestBody List<DailyReport> dailyReports ,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
  104. @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
  105. HttpServletResponse response) {
  106. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss");
  107. String beginTime = begin.atStartOfDay().format(fmt);
  108. String endTime = begin.atTime(23, 59, 59).format(fmt);
  109. List<DailyReport> dailyReportList = dailyReports.stream()
  110. .filter(Objects::nonNull)
  111. .filter(c -> c.getRepayDate() != null && !c.getRepayDate().equals(" "))
  112. .filter(c -> {
  113. try {
  114. LocalDate d = LocalDate.parse(c.getRepayDate(), fmt);
  115. return !d.isBefore(LocalDate.parse(beginTime, fmt)) && !d.isAfter(LocalDate.parse(endTime, fmt));
  116. } catch (Exception e) {
  117. return false; // 格式不对直接丢弃
  118. }
  119. })
  120. .collect(Collectors.toList());
  121. //查询概览运营数据,提供给Excel模板文件
  122. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileUploadConfig.getUploadDir() + "日报汇总.xlsx");
  123. try {
  124. //基于提供好的模板文件创建一个新的Excel表格对象
  125. XSSFWorkbook excel = new XSSFWorkbook(inputStream);
  126. //获得Excel文件中的一个Sheet页
  127. XSSFSheet sheet = excel.getSheet("Sheet1");
  128. //TODO:row(从0开始)行,cell(从0开始)获取单元格
  129. //获得第1行
  130. XSSFRow row = sheet.getRow(0);
  131. row.getCell(0).setCellValue(begin.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
  132. //获取单元格
  133. for (int i = 0; i <dailyReportList.size(); i++) {
  134. DailyReport dailyReport = dailyReportList.get(i);
  135. row = sheet.getRow(3+i);
  136. row.getCell(0).setCellValue(dailyReport.getCustomerName());
  137. row.getCell(1).setCellValue(dailyReport.getLoanCaseAttribute());
  138. row.getCell(2).setCellValue(dailyReport.getDisbursementDate());
  139. row.getCell(3).setCellValue(dailyReport.getLoanAmount());
  140. row.getCell(4).setCellValue(dailyReport.getRepayTotalAmount());
  141. row.getCell(5).setCellValue(dailyReport.getRemainAmount());
  142. row.getCell(6).setCellValue(dailyReport.getRepayDate());
  143. row.getCell(7).setCellValue(dailyReport.getLoanInterest());
  144. }
  145. //通过输出流将文件下载到客户端浏览器中
  146. ServletOutputStream out = response.getOutputStream();
  147. excel.write(out);
  148. //关闭资源
  149. out.flush();
  150. out.close();
  151. excel.close();
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }
  155. }
  156. @GetMapping("/arrangeReport")
  157. @ApiOperation("获取排期统计表")
  158. public Result arrangeReport(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin) {
  159. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  160. String endTime = begin.atTime(23, 59, 59).format(fmt);
  161. List<LoanCaseStatistic> loanCaseStatistics = new ArrayList<>();
  162. //指派人员只需要显示进行中的业务
  163. List<LoanCaseSimpleVO> loanCases = loanService.findLoanCaseSimpleByIsComplete(DecisionEnum.PROCESS.getMsg());
  164. for (LoanCaseSimpleVO loanCase : loanCases){
  165. LoanCaseStatistic loanCaseStatistic = new LoanCaseStatistic();
  166. List<StepVO> steps = stepService.getChildStepByCaseIdBeforeTime(loanCase.getId(), endTime);
  167. //仅显示派单环节的信息
  168. for (StepVO step : steps){
  169. if (step.getStatus().equals(StepEnum.UNSTART.getMsg()) || step.getUserId1() == null)
  170. continue;
  171. if (!step.getCode().equals(StepPropertyEnum.EVIDENCE_CONFIRMATION.getCode()) && !step.getCode().equals(StepPropertyEnum.DELIVERY_CONFIRMATION.getCode())
  172. &&!step.getCode().equals(StepPropertyEnum.DISBURSE_START.getCode()) && !step.getCode().equals(StepPropertyEnum.BALANCE_REPAY.getCode()))
  173. continue;
  174. loanCaseStatistic.setWeekSeq(calculateWeekSequence(LocalDate.parse(step.getBeginTime(), fmt)));
  175. loanCaseStatistic.setDisburseDate(getDateAndPlace( step ,loanCase.getId()).get(0));//2.
  176. loanCaseStatistic.setCustomerName(customerService.findByCustomerIdAndIsDelete(loanCase.getCustomerId(), false).getName());//3.
  177. loanCaseStatistic.setBusinessType(loanCase.getBusinessType());//4.
  178. loanCaseStatistic.setAmount(loanCase.getTotalLoanAmount());//5.
  179. loanCaseStatistic.setStepName(step.getStepName());//6.
  180. if (step.getCode().equals(StepPropertyEnum.BALANCE_REPAY.getCode())||step.getCode().equals(StepPropertyEnum.DISBURSE_START.getCode())){
  181. List<String> data = getDateAndPlace(step, loanCase.getId());
  182. if (data.size()>=3)
  183. loanCaseStatistic.setUserName1(data.get(2));
  184. if (data.size()==4)
  185. loanCaseStatistic.setUserName2(data.get(3));
  186. }else {
  187. String[] ids = new String[]{};
  188. if (step.getUserIds() != null)
  189. ids = step.getUserIds().split(",");
  190. String userName1 = "";
  191. if (ids.length > 0)
  192. userName1 = userService.findByIdAndIsDelete(Long.parseLong(ids[0])).getRealName();
  193. loanCaseStatistic.setUserName1(userName1);//7.
  194. String userName2 = "";
  195. Stream<String> distinct = Arrays.stream(ids).distinct();
  196. List<Long> userIds = distinct.map(Long::parseLong).collect(Collectors.toList());
  197. if(userIds.size() > 1){
  198. List<User> users = userService.findByIdsAndIsDelete(userIds.subList(1, userIds.size()));//保证了第一个不会出现在后面
  199. userName2 = users.stream().map(User::getRealName).collect(Collectors.toList()).toString();
  200. }
  201. loanCaseStatistic.setUserName2(userName2);//8.
  202. }
  203. loanCaseStatistic.setStartDate(step.getBeginTime());//9.
  204. loanCaseStatistic.setPlace(getDateAndPlace( step ,loanCase.getId()).get(1));//10.
  205. loanCaseStatistic.setEndDate(step.getStatus().equals(StepEnum.COMPLETED.getMsg()) ? step.getUpdateTime() : step.getStatus());//11.
  206. loanCaseStatistics.add(loanCaseStatistic);
  207. }
  208. }
  209. // 对 loanCaseStatistics 进行排序
  210. loanCaseStatistics.sort(Comparator
  211. .comparing(LoanCaseStatistic::getStartDate)
  212. .thenComparing(LoanCaseStatistic::getUserName1));
  213. return ResultUtil.success("success",loanCaseStatistics);
  214. }
  215. @GetMapping("/loanCaseReport")
  216. @ApiOperation("获取业务统计表")
  217. public Result loanCaseReport(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
  218. @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
  219. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  220. String beginTime = begin.atStartOfDay().format(fmt);
  221. String endTime = end.atTime(23, 59, 59).format(fmt);
  222. List<LoanCaseStatistic> loanCaseStatistics = new ArrayList<>();
  223. List<LoanCaseSimpleVO> loanCases = loanService.findLoanCaseBetweenRange(beginTime, endTime);
  224. for (LoanCaseSimpleVO loanCase : loanCases){
  225. LoanCaseStatistic loanCaseStatistic = new LoanCaseStatistic();
  226. List<StepVO> steps = stepService.getChildStepByCaseIdBetweenRange(loanCase.getId(), beginTime, endTime);
  227. for (StepVO step : steps){
  228. if (step.getStatus().equals(StepEnum.UNSTART.getMsg()) || step.getUserId1() == null)
  229. continue;
  230. loanCaseStatistic.setWeekSeq(calculateWeekSequence(LocalDate.parse(step.getBeginTime(), fmt)));
  231. List<String> dateAndPlace = getDateAndPlace(step, loanCase.getId());
  232. if(!dateAndPlace.isEmpty())
  233. loanCaseStatistic.setDisburseDate(dateAndPlace.get(0));//2.
  234. loanCaseStatistic.setCustomerName(customerService.findByCustomerIdAndIsDelete(loanCase.getCustomerId(), false).getName());//3.
  235. loanCaseStatistic.setBusinessType(loanCase.getBusinessType());//4.
  236. loanCaseStatistic.setAmount(loanCase.getTotalLoanAmount());//5.
  237. // String stepName = "";
  238. // if(step.getCode()==StepPropertyEnum.CHANNEL_PUSH.getCode())
  239. // stepName += "(取证)";
  240. // else if (step.getCode()==StepPropertyEnum.CHANNEL_PUSH_2.getCode())
  241. // stepName += "(送证)";
  242. // else
  243. // stepName = step.getStepName();
  244. loanCaseStatistic.setStepName(step.getStepName());//6.
  245. if (step.getCode().equals(StepPropertyEnum.BALANCE_REPAY.getCode())||step.getCode().equals(StepPropertyEnum.DISBURSE_START.getCode())){
  246. List<String> data = getDateAndPlace(step, loanCase.getId());
  247. if (data.size()>=3)
  248. loanCaseStatistic.setUserName1(data.get(2));
  249. if (data.size()==4)
  250. loanCaseStatistic.setUserName2(data.get(3));
  251. }else {
  252. String[] ids = new String[]{};
  253. if (step.getUserIds() != null)
  254. ids = step.getUserIds().split(",");
  255. String userName1 = "";
  256. if (ids.length > 0)
  257. userName1 = userService.findByIdAndIsDelete(Long.parseLong(ids[0])).getRealName();
  258. loanCaseStatistic.setUserName1(userName1);//7.
  259. String userName2 = "";
  260. Stream<String> distinct = Arrays.stream(ids).distinct();
  261. List<Long> userIds = distinct.map(Long::parseLong).collect(Collectors.toList());
  262. if(userIds.size() > 1){
  263. List<User> users = userService.findByIdsAndIsDelete(userIds.subList(1, userIds.size()));//保证了第一个不会出现在后面
  264. userName2 = users.stream().map(User::getRealName).collect(Collectors.toList()).toString();
  265. }
  266. loanCaseStatistic.setUserName2(userName2);//8.
  267. }
  268. loanCaseStatistic.setStartDate(step.getBeginTime());//9.
  269. if(dateAndPlace.size()>1)
  270. loanCaseStatistic.setPlace(dateAndPlace.get(1));//10.
  271. loanCaseStatistic.setEndDate(step.getStatus().equals(StepEnum.COMPLETED.getMsg()) ? step.getUpdateTime() : step.getStatus());//11.
  272. loanCaseStatistics.add(loanCaseStatistic);
  273. }
  274. // 对 loanCaseStatistics 进行排序
  275. loanCaseStatistics.sort(Comparator
  276. .comparing(LoanCaseStatistic::getStartDate)
  277. .thenComparing(LoanCaseStatistic::getUserName1));
  278. }
  279. return ResultUtil.success("success",loanCaseStatistics);
  280. }
  281. private String calculateWeekSequence(LocalDate date) {
  282. // 获取月份的第一天
  283. LocalDate firstDayOfMonth = date.withDayOfMonth(1);
  284. // 获取第一天是星期几(1=Monday, 7=Sunday)
  285. int firstDayOfWeek = firstDayOfMonth.getDayOfWeek().getValue();
  286. // 计算指定日期是当月第几天
  287. int dayOfMonth = date.getDayOfMonth();
  288. // 计算是第几周 (W1, W2, W3...)TODO:取余计算需要从0开始
  289. // 例如:如果1号是周三,那么1-3号属于第一周,4-10号属于第二周...
  290. int weekSequence = ((dayOfMonth + firstDayOfWeek - 2) / 7) + 1;
  291. return "W" + weekSequence;
  292. }
  293. private List<String > getDateAndPlace(StepVO step , Long caseId){
  294. List<String> list = new ArrayList<>();//有两个,第一个是date,第二个是place
  295. String date = "";
  296. String place = "";
  297. String mainUSerName="";
  298. String assistUserName="";
  299. if (step.getCode().equals(StepPropertyEnum.EVIDENCE_CONFIRMATION.getCode())){
  300. List<CollateralPlan> collateralPlans = collateralPlanService.findByCaseIdAndIsDelete(caseId, false);
  301. for (CollateralPlan plan : collateralPlans){
  302. if (plan.getTime()== null || plan.getPlace()== null)
  303. continue;
  304. if (plan.getFlag().equals(DecisionEnum.ENTER_WAREHOUSE.getMsg())){
  305. date = date.isEmpty() ? plan.getTime() :date.concat("、").concat(plan.getTime());
  306. place = place.isEmpty() ? plan.getPlace() :place.concat("、").concat(plan.getPlace());
  307. }
  308. }
  309. list.add(date);
  310. list.add(place);
  311. }else if (step.getCode().equals(StepPropertyEnum.DELIVERY_CONFIRMATION.getCode())){
  312. List<CollateralPlan> collateralPlans = collateralPlanService.findByCaseIdAndIsDelete(caseId, false);
  313. for (CollateralPlan plan : collateralPlans){
  314. if (plan.getTime()== null || plan.getPlace()== null)
  315. continue;
  316. if (plan.getFlag().equals(DecisionEnum.OUT_WAREHOUSE.getMsg())){
  317. date = date.isEmpty() ? plan.getTime() :date.concat("、").concat(plan.getTime());
  318. place = place.isEmpty() ? plan.getPlace() :place.concat("、").concat(plan.getPlace());
  319. }
  320. }
  321. list.add(date);
  322. list.add(place);
  323. }else if (step.getCode().equals(StepPropertyEnum.DISBURSE_START.getCode())) {
  324. List<Disbursement> disbursements = disbursementService.getDisbursementByCaseId(caseId);
  325. for (Disbursement disbursement : disbursements){
  326. if(disbursement.getPlannedTime()!=null)
  327. date = date.isEmpty() ? disbursement.getPlannedTime() :date.concat("、").concat(disbursement.getPlannedTime());
  328. if(disbursement.getLocationId()!=null){
  329. LocationDatum locationDatum = locationDatumService.findById(disbursement.getLocationId());
  330. place = place.isEmpty() ? locationDatum.getSimpleAddress() :place.concat("、").concat(locationDatum.getSimpleAddress());
  331. }
  332. User user = userService.findByIdAndIsDelete(disbursement.getMainUserId());
  333. if (user !=null)
  334. mainUSerName = mainUSerName.isEmpty() ? user.getRealName() :mainUSerName.concat("、").concat(user.getRealName());
  335. User user1 = userService.findByIdAndIsDelete(disbursement.getAssistUserId());
  336. if (user1 !=null)
  337. assistUserName = assistUserName.isEmpty() ? user1.getRealName() :assistUserName.concat( "、").concat(user1.getRealName());
  338. }
  339. list.add( date);
  340. list.add(place);
  341. list.add(mainUSerName);
  342. list.add(assistUserName);
  343. }else if(step.getCode().equals(StepPropertyEnum.BALANCE_REPAY.getCode())){
  344. Repayment repayment = repaymentService.findByCaseIdAndIsDelete(caseId, false);
  345. if (repayment!=null){
  346. List<RepaymentRecord> records = repaymentRecordService.findByRepaymentIdAndIsInterestAndIsDelete(repayment.getId(), false);
  347. for (RepaymentRecord record : records){
  348. if (record.getRepayTime()!= null || record.getRepayLocation()== null)
  349. date = date.isEmpty() ? record.getRepayTime() :date.concat("、").concat(record.getRepayTime());
  350. if(record.getRepayLocation()!=null)
  351. place = place.isEmpty() ? record.getRepayLocation() :place.concat("、").concat(record.getRepayLocation());
  352. User user = userService.findByIdAndIsDelete(record.getMainUser());
  353. if (user !=null)
  354. mainUSerName = mainUSerName.isEmpty() ? user.getRealName() :mainUSerName.concat("、").concat(user.getRealName());
  355. User user1 = userService.findByIdAndIsDelete(record.getAssistUser());
  356. if (user1 !=null)
  357. assistUserName = assistUserName.isEmpty() ? user1.getRealName() :assistUserName.concat( "、").concat(user1.getRealName());
  358. }
  359. }
  360. list.add( date);
  361. list.add(place);
  362. list.add(mainUSerName);
  363. list.add(assistUserName);
  364. }
  365. return list;
  366. }
  367. @GetMapping("/loanCaseReport/export")
  368. @ApiOperation("导出业务统计表")
  369. public void exportLaonCaseReport(@RequestBody List<LoanCaseStatistic> loanCaseStatistics, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
  370. @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end, HttpServletResponse response) {
  371. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss");
  372. String beginTime = begin.atStartOfDay().format(fmt);
  373. String endTime = end.atTime(23, 59, 59).format(fmt);
  374. List<LoanCaseStatistic> loanCaseStatisticList = loanCaseStatistics.stream()
  375. .filter(Objects::nonNull)
  376. .filter(c -> c.getEndDate() != null && !c.getEndDate().equals(" "))
  377. .filter(c -> {
  378. try {
  379. LocalDate d = LocalDate.parse(c.getEndDate(), fmt);
  380. return !d.isBefore(LocalDate.parse(beginTime, fmt)) && !d.isAfter(LocalDate.parse(endTime, fmt));
  381. } catch (Exception e) {
  382. return false; // 格式不对直接丢弃
  383. }
  384. })
  385. .collect(Collectors.toList());
  386. //查询概览运营数据,提供给Excel模板文件
  387. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileUploadConfig.getUploadDir() + "工作计划排期&业务统计表.xlsx");
  388. try {
  389. //基于提供好的模板文件创建一个新的Excel表格对象
  390. XSSFWorkbook excel = new XSSFWorkbook(inputStream);
  391. //获得Excel文件中的一个Sheet页
  392. XSSFSheet sheet = excel.getSheet("Sheet1");
  393. //TODO:row(从0开始)行,cell(从0开始)获取单元格
  394. //获得第1行
  395. XSSFRow row = sheet.getRow(0);
  396. row.getCell(1).setCellValue(begin.format(DateTimeFormatter.ofPattern("M月")));
  397. //获取单元格
  398. for (int i = 0; i < loanCaseStatisticList.size(); i++) {
  399. LoanCaseStatistic loanCaseStatistic = loanCaseStatisticList.get(i);
  400. row = sheet.getRow(3+i);
  401. row.getCell(0).setCellValue(loanCaseStatistic.getWeekSeq());
  402. row.getCell(1).setCellValue(loanCaseStatistic.getStartDate());
  403. row.getCell(2).setCellValue(loanCaseStatistic.getCustomerName());
  404. row.getCell(3).setCellValue(loanCaseStatistic.getBusinessType());
  405. row.getCell(4).setCellValue(loanCaseStatistic.getAmount());
  406. row.getCell(5).setCellValue(loanCaseStatistic.getStepName());
  407. row.getCell(6).setCellValue(loanCaseStatistic.getUserName1());
  408. row.getCell(7).setCellValue(loanCaseStatistic.getUserName2());
  409. row.getCell(8).setCellValue(loanCaseStatistic.getEndDate());
  410. row.getCell(9).setCellValue(loanCaseStatistic.getPlace());
  411. }
  412. //通过输出流将文件下载到客户端浏览器中
  413. ServletOutputStream out = response.getOutputStream();
  414. excel.write(out);
  415. //关闭资源
  416. out.flush();
  417. out.close();
  418. excel.close();
  419. } catch (IOException e) {
  420. e.printStackTrace();
  421. }
  422. }
  423. @GetMapping("/financeEfficiency")
  424. @ApiOperation("获取资金效率表")
  425. public Result financeEfficiency(@DateTimeFormat(pattern = "yyyy-MM") LocalDate begin,
  426. @DateTimeFormat(pattern = "yyyy-MM") LocalDate end) {
  427. return ResultUtil.success();
  428. }
  429. @GetMapping("/financeBalance")
  430. @ApiOperation("获取资金余额")
  431. public Result financeBalance(@DateTimeFormat(pattern = "yyyy-MM") LocalDate begin,
  432. @DateTimeFormat(pattern = "yyyy-MM") LocalDate end) {
  433. return ResultUtil.success();
  434. }
  435. @GetMapping("/schedule")
  436. @ApiOperation("获取业务安排表")
  437. public Result schedule(@RequestParam Long caseId) {
  438. return ResultUtil.success();
  439. }
  440. //到处数据
  441. public void exportData(HttpServletResponse response, String templatePath) {
  442. LocalDate begin = LocalDate.now().minusDays(30);
  443. LocalDate end = LocalDate.now().minusDays(1);
  444. //查询概览运营数据,提供给Excel模板文件
  445. //BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));
  446. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");
  447. try {
  448. //基于提供好的模板文件创建一个新的Excel表格对象
  449. XSSFWorkbook excel = new XSSFWorkbook(inputStream);
  450. //获得Excel文件中的一个Sheet页
  451. XSSFSheet sheet = excel.getSheet("Sheet1");
  452. sheet.getRow(1).getCell(1).setCellValue(begin + "至" + end);
  453. //获得第4行
  454. XSSFRow row = sheet.getRow(3);
  455. //获取单元格
  456. // row.getCell(2).setCellValue(businessData.getTurnover());
  457. // row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
  458. // row.getCell(6).setCellValue(businessData.getNewUsers());
  459. //
  460. // row = sheet.getRow(4);
  461. // row.getCell(2).setCellValue(businessData.getValidOrderCount());
  462. // row.getCell(4).setCellValue(businessData.getUnitPrice());
  463. //
  464. // for (int i = 0; i < 30; i++) {
  465. // LocalDate date = begin.plusDays(i);
  466. // //准备明细数据
  467. // businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
  468. // row = sheet.getRow(7 + i);
  469. // row.getCell(1).setCellValue(date.toString());
  470. // row.getCell(2).setCellValue(businessData.getTurnover());
  471. // row.getCell(3).setCellValue(businessData.getValidOrderCount());
  472. // row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
  473. // row.getCell(5).setCellValue(businessData.getUnitPrice());
  474. // row.getCell(6).setCellValue(businessData.getNewUsers());
  475. // }
  476. //通过输出流将文件下载到客户端浏览器中
  477. ServletOutputStream out = response.getOutputStream();
  478. excel.write(out);
  479. //关闭资源
  480. out.flush();
  481. out.close();
  482. excel.close();
  483. } catch (IOException e) {
  484. e.printStackTrace();
  485. }
  486. }
  487. // 示例:合并连续相同userName1的情况
  488. private void mergeSameCells(XSSFSheet sheet, List<LoanCaseStatistic> dataList, int columnIndex) {
  489. int startRow = 3; // 数据起始行
  490. String currentValue = null;
  491. int mergeStart = startRow;
  492. for (int i = 0; i < dataList.size(); i++) {
  493. // String value = getCellValueByColumn(dataList.get(i), columnIndex);
  494. String value = " ";
  495. if (currentValue == null || !currentValue.equals(value)) {
  496. // 发现不同的值,检查是否需要合并前面的单元格
  497. if (i > mergeStart) {
  498. // 合并单元格
  499. sheet.addMergedRegion(new CellRangeAddress(mergeStart, i + startRow - 1, columnIndex, columnIndex));
  500. }
  501. currentValue = value;
  502. mergeStart = i + startRow;
  503. }
  504. }
  505. // 处理最后一组
  506. if (mergeStart < dataList.size() + startRow - 1) {
  507. sheet.addMergedRegion(new CellRangeAddress(mergeStart, dataList.size() + startRow - 1, columnIndex, columnIndex));
  508. }
  509. }
  510. }