|
@@ -0,0 +1,241 @@
|
|
|
+package com.xy.service;
|
|
|
+
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.context.AnalysisContext;
|
|
|
+import com.alibaba.excel.read.listener.ReadListener;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.github.yitter.idgen.YitIdHelper;
|
|
|
+import com.xy.collections.list.JArrayList;
|
|
|
+import com.xy.collections.list.JList;
|
|
|
+import com.xy.collections.map.JMap;
|
|
|
+import com.xy.config.DeviceThreadPoolConfig;
|
|
|
+import com.xy.device.EnumSimConfig;
|
|
|
+import com.xy.dto.DeviceSimDto;
|
|
|
+import com.xy.entity.DeviceSim;
|
|
|
+import com.xy.entity.DeviceSimCharge;
|
|
|
+import com.xy.entity.SysDictRedis;
|
|
|
+import com.xy.mapper.DeviceSimMapper;
|
|
|
+import com.xy.utils.*;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static com.xy.utils.PlusBeans.toIPage;
|
|
|
+import static com.xy.utils.PlusBeans.toPageBean;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 设备流量卡 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author lijin
|
|
|
+ * @since 2023-10-16
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+@Api(tags = "设备流量卡")
|
|
|
+public class DeviceSimServiceImpl extends ServiceImpl<DeviceSimMapper, DeviceSim> implements DeviceSimService {
|
|
|
+
|
|
|
+ private DeviceSimChargeServiceImpl deviceSimChargeService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @ApiOperation("购买")
|
|
|
+ public R pay(DeviceSimDto.Pay pay) {
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ List<DeviceSimDto.Pay.DeviceSimCharge> deviceSimCharges = pay.getDeviceSimCharges();
|
|
|
+ List<DeviceSimCharge> deviceSimChargess = new ArrayList<>(deviceSimCharges.size());
|
|
|
+ deviceSimCharges.forEach(deviceSimCharge -> {
|
|
|
+ DeviceSimCharge deviceSimChargeInfo = new DeviceSimCharge()
|
|
|
+ .setId(YitIdHelper.nextId())
|
|
|
+ .setOrderId(pay.getOrderId())
|
|
|
+ .setSimId(deviceSimCharge.getSimId())
|
|
|
+ .setMercId(pay.getMercId())
|
|
|
+ .setMoney(deviceSimCharge.getMoney())
|
|
|
+ .setSize(deviceSimCharge.getSize())
|
|
|
+ .setPayType(pay.getPayType())
|
|
|
+ .setNote(pay.getNote())
|
|
|
+ .setCreateTime(now)
|
|
|
+ .setUpdateTime(now);
|
|
|
+ deviceSimChargess.add(deviceSimChargeInfo);
|
|
|
+ });
|
|
|
+ deviceSimChargeService.saveBatch(deviceSimChargess);
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @ApiOperation("购买回调")
|
|
|
+ public R payNotice(DeviceSimDto.PayNotice payNotice) {
|
|
|
+ //查询设备流量卡充值表
|
|
|
+ List<DeviceSimCharge> list = deviceSimChargeService.list(new LambdaQueryWrapper<DeviceSimCharge>().eq(DeviceSimCharge::getOrderId, payNotice.getOrderId()));
|
|
|
+ if (!Emptys.check(list)) {
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+ //查询设备流量卡
|
|
|
+ JList<DeviceSimCharge> deviceSimCharges = new JArrayList<>(list);
|
|
|
+ JMap<String, DeviceSimCharge> deviceSimChargesJMaps = deviceSimCharges.toMap(DeviceSimCharge::getSimId).cover();
|
|
|
+ List<DeviceSim> deviceSims = listByIds(deviceSimCharges.getProperty(DeviceSimCharge::getSimId));
|
|
|
+ //循环处理
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ deviceSims.forEach(deviceSim -> {
|
|
|
+ DeviceSimCharge deviceSimCharge = deviceSimChargesJMaps.get(deviceSim.getId());
|
|
|
+ String newTimeout = deviceSimCharge.getPayType() == 100 ? DataTime.getStringAround(0, 0, deviceSimCharge.getSize(), 0, 0, 0)
|
|
|
+ : DataTime.getStringAround(deviceSimCharge.getSize(), 0, 0, 0, 0, 0);
|
|
|
+ long d = DataTime.diff(now, deviceSim.getTimeout(), "d");
|
|
|
+ if (d > 0) {
|
|
|
+ newTimeout = DataTime.getStringAround(0, 0, (int) d, 0, 0, 0, newTimeout);
|
|
|
+ }
|
|
|
+ deviceSim.setTimeout(DataTime.toLocal(newTimeout))
|
|
|
+ .setLastRenewalTime(now)
|
|
|
+ .setUpdateTime(now);
|
|
|
+ });
|
|
|
+ updateBatchById(deviceSims);
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("page")
|
|
|
+ @ApiOperation("分页查询")
|
|
|
+ public R<PageBean<DeviceSimDto.PageVo>> page(@RequestBody DeviceSimDto.Page page) {
|
|
|
+ Map<String, SysDictRedis> simConfig = SysDictUtils.get(EnumSimConfig.Code.CODE.getCode());
|
|
|
+ Integer value = Integer.valueOf(simConfig.get(EnumSimConfig.N_200.getCode()).getValue());
|
|
|
+ String theTime = DataTime.getStringAround(0, 0, value, 0, 0, 0);
|
|
|
+ page.setThisTime(LocalDateTime.now()).setTheTime(DataTime.toLocal(theTime));
|
|
|
+ IPage<DeviceSimDto.PageVo> iPage = baseMapper.page(toIPage(page.getPage()), page);
|
|
|
+ List<DeviceSimDto.PageVo> records = iPage.getRecords();
|
|
|
+ if (Emptys.check(records)) {
|
|
|
+ String name = simConfig.get(EnumSimConfig.name.getCode()).getValue();
|
|
|
+ Integer money = Integer.valueOf(simConfig.get(EnumSimConfig.money.getCode()).getValue());
|
|
|
+ records.forEach(record -> {
|
|
|
+ //封装过期状态说明
|
|
|
+ String timeoutStatus;
|
|
|
+ DeviceSimDto.Vo sim = record.getSim();
|
|
|
+ LocalDateTime timeout = sim.getTimeout();
|
|
|
+ long s = DataTime.diff(page.getThisTime(), timeout, "s");
|
|
|
+ if (s <= 0) {
|
|
|
+ timeoutStatus = "欠费(" + (~(s / 86400 - 1)) + "天)";
|
|
|
+ } else {
|
|
|
+ timeoutStatus = s <= value * 86400 ? "即将到期(" + s / 86400 + "天)" : "正常(" + s / 86400 + "天)";
|
|
|
+ }
|
|
|
+ record.getSim().setTimeoutStatus(timeoutStatus);
|
|
|
+ record.setChargingName(name).setChargingMoney(money);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return R.ok(toPageBean(iPage));
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @ApiOperation("下载流量卡号模板")
|
|
|
+ @PostMapping("downloadSimTemplet")
|
|
|
+ public void downloadSnTemplet(HttpServletResponse response) {
|
|
|
+ InputStream inputStream = IoUtils.inputStream("sim_templet.xlsx").get();
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + "sim_templet.xlsx");
|
|
|
+ response.setContentType("application/xlsx");
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int bytesRead;
|
|
|
+ OutputStream outputStream = response.getOutputStream();
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("导入流量卡号数据")
|
|
|
+ @PostMapping("uploadSim")
|
|
|
+ public R uploadSim(@RequestParam("file") MultipartFile file) {
|
|
|
+ ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_COMMON_POLL, 1)
|
|
|
+ .execute(() -> {
|
|
|
+ try {
|
|
|
+ EasyExcel.read(file.getInputStream(), UploadSim.class, new UploadSimListener(this)).sheet().doRead();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("", e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Slf4j
|
|
|
+ @RequiredArgsConstructor
|
|
|
+ public static class UploadSimListener implements ReadListener<UploadSim> {
|
|
|
+
|
|
|
+ private final DeviceSimServiceImpl deviceSimService;
|
|
|
+
|
|
|
+ private JList<UploadSim> sims = new JArrayList<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 这个每一条数据解析都会来调用
|
|
|
+ *
|
|
|
+ * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()}
|
|
|
+ * @param context
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void invoke(UploadSim data, AnalysisContext context) {
|
|
|
+ sims.add(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 所有数据解析完成了 都会来调用
|
|
|
+ *
|
|
|
+ * @param context
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void doAfterAllAnalysed(AnalysisContext context) {
|
|
|
+ List<DeviceSim> deviceSims = deviceSimService.listByIds(sims.getProperty(UploadSim::getSimId));
|
|
|
+ JMap<String, DeviceSim> deviceSimsJMaps = new JArrayList<>(deviceSims).toMap(DeviceSim::getId).cover();
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ JList<DeviceSim> saveDeviceSims = new JArrayList<>();
|
|
|
+ sims.forEach(uploadSim -> {
|
|
|
+ if (deviceSimsJMaps.containsKey(uploadSim.getSimId())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ DeviceSim deviceSim = new DeviceSim()
|
|
|
+ .setId(uploadSim.getSimId())
|
|
|
+ .setActivateTime(DataTime.toLocal(uploadSim.getActivateTime()))
|
|
|
+ .setTimeout(DataTime.toLocal(uploadSim.getTimeout()))
|
|
|
+ .setCreateTime(now)
|
|
|
+ .setUpdateTime(now);
|
|
|
+ deviceSim.setLastRenewalTime(deviceSim.getActivateTime());
|
|
|
+ saveDeviceSims.add(deviceSim);
|
|
|
+ });
|
|
|
+ deviceSimService.saveBatch(saveDeviceSims);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Data
|
|
|
+ public static class UploadSim {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 流量卡号
|
|
|
+ */
|
|
|
+ private String simId;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 激活时间
|
|
|
+ */
|
|
|
+ private String activateTime;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 过期时间
|
|
|
+ */
|
|
|
+ private String timeout;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|