DeviceSimServiceImpl.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. package com.xy.service;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.annotation.ExcelProperty;
  4. import com.alibaba.excel.annotation.write.style.ColumnWidth;
  5. import com.alibaba.excel.context.AnalysisContext;
  6. import com.alibaba.excel.read.listener.ReadListener;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.metadata.IPage;
  9. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  10. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  11. import com.github.yitter.idgen.YitIdHelper;
  12. import com.xy.collections.list.JArrayList;
  13. import com.xy.collections.list.JList;
  14. import com.xy.collections.map.JMap;
  15. import com.xy.config.DeviceThreadPoolConfig;
  16. import com.xy.config.FileConfig;
  17. import com.xy.device.EnumDeviceChargingHistoryStatus;
  18. import com.xy.device.EnumSimConfig;
  19. import com.xy.dto.DeviceSimDto;
  20. import com.xy.entity.DeviceSim;
  21. import com.xy.entity.DeviceSimCharge;
  22. import com.xy.entity.SysDictRedis;
  23. import com.xy.mapper.DeviceSimMapper;
  24. import com.xy.util.ExcelUtils;
  25. import com.xy.utils.*;
  26. import io.swagger.annotations.Api;
  27. import io.swagger.annotations.ApiOperation;
  28. import lombok.AllArgsConstructor;
  29. import lombok.Data;
  30. import lombok.RequiredArgsConstructor;
  31. import lombok.SneakyThrows;
  32. import lombok.extern.slf4j.Slf4j;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.validation.annotation.Validated;
  35. import org.springframework.web.bind.annotation.PostMapping;
  36. import org.springframework.web.bind.annotation.RequestBody;
  37. import org.springframework.web.bind.annotation.RequestParam;
  38. import org.springframework.web.multipart.MultipartFile;
  39. import javax.servlet.http.HttpServletResponse;
  40. import java.io.File;
  41. import java.io.IOException;
  42. import java.io.InputStream;
  43. import java.io.OutputStream;
  44. import java.time.LocalDate;
  45. import java.time.LocalDateTime;
  46. import java.time.format.DateTimeFormatter;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. import java.util.Map;
  50. import static com.xy.utils.PlusBeans.toIPage;
  51. import static com.xy.utils.PlusBeans.toPageBean;
  52. /**
  53. * <p>
  54. * 设备流量卡 服务实现类
  55. * </p>
  56. *
  57. * @author lijin
  58. * @since 2023-10-16
  59. */
  60. @Service
  61. @AllArgsConstructor
  62. @Api(tags = "设备流量卡")
  63. public class DeviceSimServiceImpl extends ServiceImpl<DeviceSimMapper, DeviceSim> implements DeviceSimService {
  64. private DeviceSimChargeServiceImpl deviceSimChargeService;
  65. private HttpServletResponse response;
  66. private FileConfig fileConfig;
  67. @PostMapping("countWaitHandle")
  68. @ApiOperation("抄送流量卡-运营首页概况")
  69. public R<Long> countWaitHandle() {
  70. long count = count(Wrappers.<DeviceSim>lambdaQuery().eq(DeviceSim::getWaitHandle, true));
  71. return R.ok(count);
  72. }
  73. @Override
  74. @ApiOperation("购买")
  75. public R pay(DeviceSimDto.Pay pay) {
  76. LocalDateTime now = LocalDateTime.now();
  77. List<DeviceSimDto.Pay.DeviceSimCharge> deviceSimCharges = pay.getDeviceSimCharges();
  78. List<DeviceSimCharge> deviceSimChargess = new ArrayList<>(deviceSimCharges.size());
  79. deviceSimCharges.forEach(deviceSimCharge -> {
  80. DeviceSimCharge deviceSimChargeInfo = new DeviceSimCharge()
  81. .setId(YitIdHelper.nextId())
  82. .setOrderId(pay.getOrderId())
  83. .setSimId(deviceSimCharge.getSimId())
  84. .setMercId(pay.getMercId())
  85. .setMoney(deviceSimCharge.getMoney())
  86. .setSize(deviceSimCharge.getSize())
  87. .setPayType(pay.getPayType())
  88. .setNote(pay.getNote())
  89. .setCreateTime(now)
  90. .setUpdateTime(now);
  91. deviceSimChargess.add(deviceSimChargeInfo);
  92. });
  93. deviceSimChargeService.saveBatch(deviceSimChargess);
  94. return R.ok();
  95. }
  96. @Override
  97. @ApiOperation("购买回调")
  98. public R payNotice(DeviceSimDto.PayNotice payNotice) {
  99. //查询设备流量卡充值表
  100. List<DeviceSimCharge> list = deviceSimChargeService.list(new LambdaQueryWrapper<DeviceSimCharge>().eq(DeviceSimCharge::getOrderId, payNotice.getOrderId()));
  101. if (!Emptys.check(list)) {
  102. return R.ok();
  103. }
  104. //查询设备流量卡
  105. JList<DeviceSimCharge> deviceSimCharges = new JArrayList<>(list);
  106. JMap<String, DeviceSimCharge> deviceSimChargesJMaps = deviceSimCharges.toMap(DeviceSimCharge::getSimId).cover();
  107. List<DeviceSim> deviceSims = listByIds(deviceSimCharges.getProperty(DeviceSimCharge::getSimId));
  108. //循环处理
  109. LocalDateTime now = LocalDateTime.now();
  110. deviceSims.forEach(deviceSim -> {
  111. DeviceSimCharge deviceSimCharge = deviceSimChargesJMaps.get(deviceSim.getId());
  112. String newTimeout = deviceSimCharge.getPayType() == 100 ? DataTime.getStringAround(0, 0, deviceSimCharge.getSize(), 0, 0, 0)
  113. : DataTime.getStringAround(deviceSimCharge.getSize(), 0, 0, 0, 0, 0);
  114. long d = DataTime.diff(now, deviceSim.getTimeout(), "d");
  115. if (d > 0) {
  116. newTimeout = DataTime.getStringAround(0, 0, (int) d, 0, 0, 0, newTimeout);
  117. }
  118. deviceSim.setTimeout(DataTime.toLocal(newTimeout))
  119. .setWaitHandle(true)
  120. .setLastRenewalTime(now)
  121. .setUpdateTime(now);
  122. });
  123. //修改设备流量卡信息
  124. updateBatchById(deviceSims);
  125. //修改设备流量卡充值
  126. Integer status = SysDictUtils.getValue(EnumDeviceChargingHistoryStatus.Code.CODE.getCode(), EnumDeviceChargingHistoryStatus.N_2.getCode(), Integer.class);
  127. deviceSimCharges.forEach(deviceSimCharge -> deviceSimCharge.setStatus(status).setUpdateTime(now));
  128. deviceSimChargeService.updateBatchById(deviceSimCharges);
  129. return R.ok();
  130. }
  131. @Override
  132. @ApiOperation("查询过期条数")
  133. public R<DeviceSimDto.DeviceSimTimeoutCountVo> deviceSimTimeoutCount(DeviceSimDto.DeviceSimTimeoutCount deviceSimTimeoutCount) {
  134. String now = DataTime.getSring();
  135. int day = SysDictUtils.getValue(EnumSimConfig.Code.CODE.getCode(), EnumSimConfig.N_200.getCode(), Integer.class);
  136. String end1 = DataTime.getStringAround(0, 0, day, 0, 0, 0, now);
  137. deviceSimTimeoutCount.setBegin1(now)
  138. .setEnd1(end1)
  139. .setEnd2(now);
  140. DeviceSimDto.DeviceSimTimeoutCountVo deviceSimTimeoutCountVo = baseMapper.deviceSimTimeoutCount(deviceSimTimeoutCount);
  141. return R.ok(deviceSimTimeoutCountVo);
  142. }
  143. @PostMapping("page")
  144. @ApiOperation("分页查询")
  145. public R<PageBean<DeviceSimDto.PageVo>> page(@RequestBody @Validated DeviceSimDto.Page page) {
  146. Map<String, SysDictRedis> simConfig = SysDictUtils.get(EnumSimConfig.Code.CODE.getCode());
  147. Integer value = Integer.valueOf(simConfig.get(EnumSimConfig.N_200.getCode()).getValue());
  148. String theTime = DataTime.getStringAround(0, 0, value, 0, 0, 0);
  149. page.setThisTime(LocalDateTime.now()).setTheTime(DataTime.toLocal(theTime));
  150. IPage<DeviceSimDto.PageVo> iPage = baseMapper.page(toIPage(page.getPage()), page);
  151. List<DeviceSimDto.PageVo> records = iPage.getRecords();
  152. if (Emptys.check(records)) {
  153. String name = simConfig.get(EnumSimConfig.name.getCode()).getValue();
  154. Integer money = Integer.valueOf(simConfig.get(EnumSimConfig.money.getCode()).getValue());
  155. records.forEach(record -> {
  156. //封装过期状态说明
  157. DeviceSimDto.Vo sim = record.getSim();
  158. if (Emptys.check(sim)) {
  159. LocalDateTime timeout = sim.getTimeout();
  160. if (timeout == null) {
  161. record.getSim().setTimeoutStatus("未初始化");
  162. } else {
  163. String timeoutStatus;
  164. long s = DataTime.diff(page.getThisTime(), timeout, "s");
  165. if (s <= 0) {
  166. timeoutStatus = "欠费(" + (~(s / 86400 - 1)) + "天)";
  167. } else {
  168. timeoutStatus = s <= value * 86400 ? "即将到期(" + s / 86400 + "天)" : "正常(" + s / 86400 + "天)";
  169. }
  170. record.getSim().setTimeoutStatus(timeoutStatus);
  171. }
  172. }
  173. //封装计费标准
  174. record.setChargingName(name).setChargingMoney(money);
  175. });
  176. }
  177. return R.ok(toPageBean(iPage));
  178. }
  179. @PostMapping("pageByNotInit")
  180. @ApiOperation("未初始化分页查询")
  181. public R<PageBean<DeviceSimDto.PageVo>> pageByNotInit(@RequestBody DeviceSimDto.Page page) {
  182. Map<String, SysDictRedis> simConfig = SysDictUtils.get(EnumSimConfig.Code.CODE.getCode());
  183. IPage<DeviceSimDto.PageVo> iPage = baseMapper.page2(toIPage(page.getPage()), page);
  184. List<DeviceSimDto.PageVo> records = iPage.getRecords();
  185. if (Emptys.check(records)) {
  186. String name = simConfig.get(EnumSimConfig.name.getCode()).getValue();
  187. Integer money = Integer.valueOf(simConfig.get(EnumSimConfig.money.getCode()).getValue());
  188. records.forEach(record -> {
  189. //封装计费标准
  190. record.setChargingName(name).setChargingMoney(money);
  191. });
  192. }
  193. return R.ok(toPageBean(iPage));
  194. }
  195. @PostMapping("pageCount")
  196. @ApiOperation("分页数量查询")
  197. public R<DeviceSimDto.PageCountVo> pageCount(@RequestBody @Validated DeviceSimDto.PageCount pageCount) {
  198. Map<String, SysDictRedis> simConfig = SysDictUtils.get(EnumSimConfig.Code.CODE.getCode());
  199. Integer value = Integer.valueOf(simConfig.get(EnumSimConfig.N_200.getCode()).getValue());
  200. String theTime = DataTime.getStringAround(0, 0, value, 0, 0, 0);
  201. //并行数据
  202. DeviceSimDto.PageCountVo pageCountVo = new DeviceSimDto.PageCountVo();
  203. ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_COMMON_POLL, 6)
  204. .execute(() -> {
  205. //全部
  206. DeviceSimDto.PageCount paramsObj = Beans.copy(DeviceSimDto.PageCount.class, pageCount);
  207. int count = baseMapper.pageCount(paramsObj);
  208. pageCountVo.setAllCount(count);
  209. })
  210. .execute(() -> {
  211. //即将过期
  212. DeviceSimDto.PageCount paramsObj = Beans.copy(DeviceSimDto.PageCount.class, pageCount)
  213. .setChargingStatus(1)
  214. .setThisTime(LocalDateTime.now())
  215. .setTheTime(DataTime.toLocal(theTime));
  216. int count = baseMapper.pageCount(paramsObj);
  217. pageCountVo.setBeTimeoutCount(count);
  218. })
  219. .execute(() -> {
  220. //已过期
  221. DeviceSimDto.PageCount paramsObj = Beans.copy(DeviceSimDto.PageCount.class, pageCount)
  222. .setChargingStatus(2)
  223. .setThisTime(LocalDateTime.now())
  224. .setTheTime(DataTime.toLocal(theTime));
  225. int count = baseMapper.pageCount(paramsObj);
  226. pageCountVo.setTimeoutCount(count);
  227. })
  228. .execute(() -> {
  229. //已激活
  230. DeviceSimDto.PageCount paramsObj = Beans.copy(DeviceSimDto.PageCount.class, pageCount)
  231. .setIsActivate(true);
  232. int count = baseMapper.pageCount(paramsObj);
  233. pageCountVo.setIsActivate(count);
  234. })
  235. .execute(() -> {
  236. //未激活
  237. DeviceSimDto.PageCount paramsObj = Beans.copy(DeviceSimDto.PageCount.class, pageCount)
  238. .setIsActivate(false);
  239. int count = baseMapper.pageCount(paramsObj);
  240. pageCountVo.setIsNotActivate(count);
  241. })
  242. .execute(() -> {
  243. //未初始化
  244. DeviceSimDto.PageCount paramsObj = Beans.copy(DeviceSimDto.PageCount.class, pageCount);
  245. int count = baseMapper.pageCount2(paramsObj);
  246. pageCountVo.setNotInitCount(count);
  247. })
  248. .end();
  249. return R.ok(pageCountVo);
  250. }
  251. @SneakyThrows
  252. @ApiOperation("导出流量卡数据")
  253. @PostMapping("download")
  254. public void download(@RequestBody DeviceSimDto.Download download) {
  255. //生成excel
  256. String name = YitIdHelper.nextId() + ".xlsx";
  257. String path = fileConfig.getPath() + File.separator + name;
  258. ExcelUtils.SheetAndData<UploadSim> sheetAndData = ExcelUtils.create(path, UploadSim.class);
  259. sheetAndData.sheet("流量卡数据", () -> {
  260. //生成导出数据
  261. List<DeviceSimDto.PageVo> pageVos;
  262. if (download.getIsNotInit()) {
  263. pageVos = baseMapper.page2(download);
  264. } else {
  265. Map<String, SysDictRedis> simConfig = SysDictUtils.get(EnumSimConfig.Code.CODE.getCode());
  266. Integer value = Integer.valueOf(simConfig.get(EnumSimConfig.N_200.getCode()).getValue());
  267. String theTime = DataTime.getStringAround(0, 0, value, 0, 0, 0);
  268. download.setThisTime(LocalDateTime.now()).setTheTime(DataTime.toLocal(theTime));
  269. pageVos = baseMapper.page(download);
  270. }
  271. List<UploadSim> uploadSims = new ArrayList<>();
  272. pageVos.forEach(pageVo -> {
  273. UploadSim uploadSim = new UploadSim();
  274. uploadSim.setSimId(pageVo.getSimIccid());
  275. DeviceSimDto.Vo sim = pageVo.getSim();
  276. if (sim != null) {
  277. uploadSim.setType(sim.getType());
  278. uploadSim.setActivateTime(Emptys.check(sim.getActivateTime()) ? DataTime.toString(sim.getActivateTime(), "yyyy-MM-dd") : null);
  279. uploadSim.setTimeout(Emptys.check(sim.getTimeout()) ? DataTime.toString(sim.getTimeout(), "yyyy-MM-dd") : null);
  280. uploadSim.setLastRenewalTime(Emptys.check(sim.getLastRenewalTime()) ? DataTime.toString(sim.getLastRenewalTime(), "yyyy-MM-dd") : null);
  281. uploadSim.setBatchNo(sim.getBatchNo());
  282. uploadSim.setIsTest(Emptys.check(sim.getIsTest()) && sim.getIsTest() ? "是" : "否");
  283. }
  284. uploadSims.add(uploadSim);
  285. });
  286. //修改批次号
  287. String updateBatchNo = download.getUpdateBatchNo();
  288. Boolean isUpdateBatchNo = download.getIsUpdateBatchNo();
  289. if (Emptys.check(updateBatchNo) && Emptys.check(isUpdateBatchNo) && isUpdateBatchNo) {
  290. LocalDateTime now = LocalDateTime.now();
  291. List<DeviceSim> deviceSims = new ArrayList<>(uploadSims.size());
  292. uploadSims.forEach(uploadSim -> {
  293. DeviceSim deviceSim = new DeviceSim()
  294. .setId(uploadSim.getSimId())
  295. .setBatchNo(updateBatchNo)
  296. .setWaitHandle(false)
  297. .setUpdateTime(now);
  298. deviceSims.add(deviceSim);
  299. });
  300. updateBatchById(deviceSims);
  301. }
  302. return uploadSims;
  303. }).builder();
  304. //下载文件
  305. InputStream inputStream = IoUtils.inputStream(path).get();
  306. response.setHeader("Content-Disposition", "attachment; filename=" + "sim_data.xlsx");
  307. response.setContentType("application/xlsx");
  308. byte[] buffer = new byte[1024];
  309. int bytesRead;
  310. OutputStream outputStream = response.getOutputStream();
  311. while ((bytesRead = inputStream.read(buffer)) != -1) {
  312. outputStream.write(buffer, 0, bytesRead);
  313. }
  314. inputStream.close();
  315. outputStream.close();
  316. //删除文件
  317. new File(path).delete();
  318. }
  319. @ApiOperation("导入流量卡号数据")
  320. @PostMapping("uploadSim")
  321. public R uploadSim(@RequestParam("file") MultipartFile file) {
  322. ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_COMMON_POLL, 1)
  323. .execute(() -> {
  324. try {
  325. EasyExcel.read(file.getInputStream(), UploadSim.class, new UploadSimListener(this)).sheet().doRead();
  326. } catch (IOException e) {
  327. log.error("", e);
  328. }
  329. });
  330. return R.ok();
  331. }
  332. @PostMapping("update")
  333. @ApiOperation("修改流量卡信息")
  334. public void update(@RequestBody @Validated DeviceSimDto.Update update) {
  335. List<String> ids = update.getId();
  336. LocalDateTime now = LocalDateTime.now();
  337. List<DeviceSim> deviceSims = new ArrayList<>(ids.size());
  338. ids.forEach(id -> {
  339. DeviceSim deviceSim = new DeviceSim()
  340. .setId(id)
  341. .setType(update.getType())
  342. .setActivateTime(update.getActivateTime())
  343. .setTimeout(update.getTimeout())
  344. .setLastRenewalTime(update.getLastRenewalTime())
  345. .setBatchNo(update.getBatchNo())
  346. .setUpdateTime(now)
  347. .setIsTest(update.getIsTest());
  348. deviceSims.add(deviceSim);
  349. });
  350. updateBatchById(deviceSims);
  351. }
  352. @Slf4j
  353. @RequiredArgsConstructor
  354. public static class UploadSimListener implements ReadListener<UploadSim> {
  355. private final DeviceSimServiceImpl deviceSimService;
  356. private JList<UploadSim> sims = new JArrayList<>();
  357. /**
  358. * 这个每一条数据解析都会来调用
  359. *
  360. * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()}
  361. * @param context
  362. */
  363. @Override
  364. public void invoke(UploadSim data, AnalysisContext context) {
  365. sims.add(data);
  366. }
  367. /**
  368. * 所有数据解析完成了 都会来调用
  369. *
  370. * @param context
  371. */
  372. @Override
  373. public void doAfterAllAnalysed(AnalysisContext context) {
  374. LocalDateTime now = LocalDateTime.now();
  375. JList<DeviceSim> saveDeviceSims = new JArrayList<>();
  376. DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  377. sims.forEach(uploadSim -> {
  378. DeviceSim deviceSim = new DeviceSim()
  379. .setId(uploadSim.getSimId().trim().replace("\"", "").replace("'", ""))
  380. .setType(uploadSim.getType())
  381. .setActivateTime(Emptys.check(uploadSim.getActivateTime()) ? LocalDate.parse(uploadSim.getActivateTime(), df).atTime(0, 0, 0) : null)
  382. .setTimeout(Emptys.check(uploadSim.getTimeout()) ? LocalDate.parse(uploadSim.getTimeout(), df).atTime(0, 0, 0) : null)
  383. .setLastRenewalTime(Emptys.check(uploadSim.getLastRenewalTime()) ? LocalDate.parse(uploadSim.getLastRenewalTime(), df).atTime(0, 0, 0) : null)
  384. .setCreateTime(now)
  385. .setUpdateTime(now)
  386. .setIsTest(Emptys.check(uploadSim.getIsTest()) && uploadSim.getIsTest().equals("是") ? true : false);
  387. saveDeviceSims.add(deviceSim);
  388. });
  389. deviceSimService.saveOrUpdateBatch(saveDeviceSims);
  390. }
  391. }
  392. @Data
  393. public static class UploadSim {
  394. @ColumnWidth(25)
  395. @ExcelProperty(value = "流量卡号")
  396. private String simId;
  397. @ColumnWidth(25)
  398. @ExcelProperty(value = "类型")
  399. private String type;
  400. @ColumnWidth(25)
  401. @ExcelProperty(value = "批次号")
  402. private String batchNo;
  403. @ColumnWidth(50)
  404. @ExcelProperty(value = "激活时间")
  405. private String activateTime;
  406. @ColumnWidth(50)
  407. @ExcelProperty(value = "过期时间")
  408. private String timeout;
  409. @ColumnWidth(50)
  410. @ExcelProperty(value = "最后续费时间")
  411. private String lastRenewalTime;
  412. @ColumnWidth(25)
  413. @ExcelProperty(value = "是否测试卡")
  414. private String isTest;
  415. }
  416. }