DeviceChargingHistoryServiceImpl.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.xy.service;
  2. import com.alibaba.excel.annotation.ExcelProperty;
  3. import com.alibaba.excel.annotation.write.style.ColumnWidth;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.metadata.IPage;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.github.yitter.idgen.YitIdHelper;
  9. import com.xy.collections.list.JArrayList;
  10. import com.xy.collections.list.JList;
  11. import com.xy.config.DeviceThreadPoolConfig;
  12. import com.xy.config.FileConfig;
  13. import com.xy.dto.DeviceChargingHistoryDto;
  14. import com.xy.dto.OrderMercManageDto;
  15. import com.xy.dto.be.MercDto;
  16. import com.xy.entity.DeviceChargingHistory;
  17. import com.xy.mapper.DeviceChargingHistoryMapper;
  18. import com.xy.service.be.MercService;
  19. import com.xy.util.ExcelUtils;
  20. import com.xy.utils.*;
  21. import io.swagger.annotations.Api;
  22. import io.swagger.annotations.ApiOperation;
  23. import lombok.AllArgsConstructor;
  24. import lombok.Data;
  25. import lombok.SneakyThrows;
  26. import lombok.experimental.Accessors;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.web.bind.annotation.PostMapping;
  29. import org.springframework.web.bind.annotation.RequestBody;
  30. import javax.servlet.http.HttpServletResponse;
  31. import java.io.File;
  32. import java.io.InputStream;
  33. import java.io.OutputStream;
  34. import java.time.YearMonth;
  35. import java.time.temporal.ChronoUnit;
  36. import java.util.ArrayList;
  37. import java.util.Arrays;
  38. import java.util.List;
  39. import static com.xy.utils.Beans.copy;
  40. import static com.xy.utils.PlusBeans.toPageBean;
  41. /**
  42. * <p>
  43. * 设备计费历史表 服务实现类
  44. * </p>
  45. *
  46. * @author lijin
  47. * @since 2023-04-14
  48. */
  49. @Service
  50. @AllArgsConstructor
  51. @Api(tags = "设备计费历史表")
  52. public class DeviceChargingHistoryServiceImpl extends ServiceImpl<DeviceChargingHistoryMapper, DeviceChargingHistory> implements DeviceChargingHistoryService {
  53. private OrderMercManageService orderMercManageService;
  54. private MercService mercService;
  55. private HttpServletResponse response;
  56. private FileConfig fileConfig;
  57. @Override
  58. @ApiOperation("分页查询")
  59. public R<PageBean<DeviceChargingHistoryDto.Vo>> page(DeviceChargingHistoryDto.Page page) {
  60. LambdaQueryWrapper<DeviceChargingHistory> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceChargingHistory.class)
  61. .build()
  62. .orderByDesc(DeviceChargingHistory::getCreateTime);
  63. IPage<DeviceChargingHistory> iPage = page(PlusBeans.toIPage(page.getPage()), lambdaQueryWrapper);
  64. PageBean<DeviceChargingHistoryDto.Vo> pageBean = toPageBean(DeviceChargingHistoryDto.Vo.class, iPage);
  65. List<DeviceChargingHistoryDto.Vo> records = pageBean.getRecords();
  66. if (Emptys.check(records)) {
  67. copy(records)
  68. .target(() -> orderMercManageService.list(new OrderMercManageDto.SelectList().setId(new JArrayList<>(records).getProperty(DeviceChargingHistoryDto.Vo::getOrderId))).getData()
  69. , DeviceChargingHistoryDto.Vo::getOrderId, DeviceChargingHistoryDto.Vo::getFiles, OrderMercManageDto.Vo::getId, OrderMercManageDto.Vo::getFiles
  70. )
  71. .target(() -> mercService.list(new MercDto.SelectList().setMercIds(new JArrayList<>(records).getProperty(DeviceChargingHistoryDto.Vo::getMercId).comparing())).getData(),
  72. DeviceChargingHistoryDto.Vo::getMercId, DeviceChargingHistoryDto.Vo::getMercName, MercDto.Vo::getId, MercDto.Vo::getName)
  73. .builder();
  74. }
  75. return R.ok(pageBean);
  76. }
  77. @ApiOperation("月统计")
  78. @PostMapping("moonCount")
  79. public R<List<DeviceChargingHistoryDto.MoonCountVo>> moonCount(@RequestBody DeviceChargingHistoryDto.MoonCount moonCount) {
  80. JList<DeviceChargingHistoryDto.MoonCountVo> moonCountVos = new JArrayList<>();
  81. //查询数据
  82. List<String> attrNames = Arrays.asList(
  83. LambdaUtils.getUnderlineCaseName(DeviceChargingHistory::getMercId),
  84. "DATE_FORMAT(" + LambdaUtils.getUnderlineCaseName(DeviceChargingHistory::getCreateTime) + ", '%Y-%m')"
  85. );
  86. List<String> selectNames = Arrays.asList(
  87. attrNames.get(0),
  88. attrNames.get(1) + " " + LambdaUtils.getProperty(DeviceChargingHistory::getNote),
  89. String.format("count(*) %s", LambdaUtils.getProperty(DeviceChargingHistory::getChargingSize)),
  90. String.format("sum(%s) %s", LambdaUtils.getUnderlineCaseName(DeviceChargingHistory::getChargingMoney), LambdaUtils.getProperty(DeviceChargingHistory::getChargingMoney))
  91. );
  92. List<String> dateDifference = getDateDifference(moonCount.getBeginDate(), moonCount.getEndDate());
  93. ThreadPoolUtils.Execute execute = ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_COMMON_POLL, dateDifference.size());
  94. dateDifference.forEach(date -> execute.execute(() -> {
  95. QueryWrapper<DeviceChargingHistory> queryWrapper = new QueryWrapper<DeviceChargingHistory>()
  96. .select(selectNames.get(0), selectNames.get(1), selectNames.get(2), selectNames.get(3))
  97. .in(Emptys.check(moonCount.getMercIds()), attrNames.get(0), moonCount.getMercIds())
  98. .eq(attrNames.get(1), date)
  99. .groupBy(attrNames.get(0), attrNames.get(1));
  100. List<DeviceChargingHistory> deviceChargingHistories = list(queryWrapper);
  101. deviceChargingHistories.forEach(deviceChargingHistory -> {
  102. DeviceChargingHistoryDto.MoonCountVo moonCountVo = new DeviceChargingHistoryDto.MoonCountVo()
  103. .setDate(deviceChargingHistory.getNote())
  104. .setMercId(deviceChargingHistory.getMercId())
  105. .setDeviceSize(deviceChargingHistory.getChargingSize())
  106. .setChargingMoney(deviceChargingHistory.getChargingMoney());
  107. moonCountVos.add(moonCountVo);
  108. });
  109. }));
  110. execute.end();
  111. //翻译商户名称
  112. Beans.copy(moonCountVos).target(() -> mercService.list(new MercDto.SelectList().setMercIds(moonCountVos.getProperty(DeviceChargingHistoryDto.MoonCountVo::getMercId).comparing())).getData(),
  113. DeviceChargingHistoryDto.MoonCountVo::getMercId, DeviceChargingHistoryDto.MoonCountVo::getMercName, MercDto.Vo::getId, MercDto.Vo::getName)
  114. .builder();
  115. return R.ok(moonCountVos.asc(DeviceChargingHistoryDto.MoonCountVo::getDate));
  116. }
  117. @SneakyThrows
  118. @ApiOperation("月统计导出")
  119. @PostMapping("moonCountDownload")
  120. public void moonCountDownload(@RequestBody DeviceChargingHistoryDto.MoonCount moonCount) {
  121. //生成excel
  122. String name = YitIdHelper.nextId() + ".xlsx";
  123. String path = fileConfig.getPath() + File.separator + name;
  124. ExcelUtils.SheetAndData<MoonCountData> sheetAndData = ExcelUtils.create(path, MoonCountData.class);
  125. sheetAndData.sheet("设备管理费月统计数据", () -> {
  126. List<DeviceChargingHistoryDto.MoonCountVo> data = moonCount(moonCount).getData();
  127. if (!Emptys.check(data)) {
  128. return null;
  129. }
  130. return Beans.copy(MoonCountData.class, data);
  131. }).builder();
  132. //下载文件
  133. InputStream inputStream = IoUtils.inputStream(path).get();
  134. response.setHeader("Content-Disposition", "attachment; filename=" + "设备管理费月统计数据.xlsx");
  135. response.setContentType("application/xlsx");
  136. byte[] buffer = new byte[1024];
  137. int bytesRead;
  138. OutputStream outputStream = response.getOutputStream();
  139. while ((bytesRead = inputStream.read(buffer)) != -1) {
  140. outputStream.write(buffer, 0, bytesRead);
  141. }
  142. inputStream.close();
  143. outputStream.close();
  144. //删除文件
  145. new File(path).delete();
  146. }
  147. public List<String> getDateDifference(String date1, String date2) {
  148. List<String> dateDifference = new ArrayList<>();
  149. // 解析日期字符串
  150. YearMonth yearMonth1 = YearMonth.parse(date1);
  151. YearMonth yearMonth2 = YearMonth.parse(date2);
  152. // 计算日期差异
  153. long monthsDiff = ChronoUnit.MONTHS.between(yearMonth1, yearMonth2);
  154. // 将差异的每个月添加到集合中
  155. YearMonth temp = yearMonth1;
  156. for (int i = 0; i <= monthsDiff; i++) {
  157. dateDifference.add(temp.toString());
  158. temp = temp.plusMonths(1);
  159. }
  160. return dateDifference;
  161. }
  162. @Data
  163. @Accessors(chain = true)
  164. public static class MoonCountData {
  165. @ColumnWidth(25)
  166. @ExcelProperty(value = "统计时间 yyyy-MM")
  167. private String date;
  168. @ColumnWidth(25)
  169. @ExcelProperty(value = "商户id")
  170. private Long mercId;
  171. @ColumnWidth(25)
  172. @ExcelProperty(value = "商户名称")
  173. private String mercName;
  174. @ColumnWidth(25)
  175. @ExcelProperty(value = "设备数")
  176. private Integer deviceSize;
  177. @ColumnWidth(25)
  178. @ExcelProperty(value = "续费金额")
  179. private Integer chargingMoney;
  180. }
  181. }