DeviceChargingHistoryServiceImpl.java 12 KB

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