|
@@ -0,0 +1,140 @@
|
|
|
+package com.xy.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xy.annotation.Lock;
|
|
|
+import com.xy.collections.list.JArrayList;
|
|
|
+import com.xy.collections.list.JList;
|
|
|
+import com.xy.collections.map.JMap;
|
|
|
+import com.xy.config.ThreadPoolConfig;
|
|
|
+import com.xy.dto.DeviceCreateIdsDto;
|
|
|
+import com.xy.dto.SysWorkUser.ListDto;
|
|
|
+import com.xy.dto.SysWorkUser.SysWorkVo;
|
|
|
+import com.xy.entity.DeviceCreateIds;
|
|
|
+import com.xy.mapper.DeviceCreateIdsMapper;
|
|
|
+import com.xy.service.DeviceCreateIdsService;
|
|
|
+import com.xy.service.SysWorkUserService;
|
|
|
+import com.xy.utils.*;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.CountDownLatch;
|
|
|
+
|
|
|
+import static com.xy.utils.Beans.copy;
|
|
|
+import static com.xy.utils.PlusBeans.toIPage;
|
|
|
+import static com.xy.utils.PlusBeans.toPageBean;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 设备-机器ID号生成 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author lijin
|
|
|
+ * @since 2022-12-29
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Api(tags = "设备-机器ID号生成")
|
|
|
+public class DeviceCreateIdsServiceImpl extends ServiceImpl<DeviceCreateIdsMapper, DeviceCreateIds> implements DeviceCreateIdsService {
|
|
|
+
|
|
|
+ @Resource(name = ThreadPoolConfig.DEVICE_CREATE_IDS_SAVE)
|
|
|
+ private ThreadPoolTaskExecutor executor;
|
|
|
+
|
|
|
+ private final SysWorkUserService sysWorkUserService;
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @PostMapping("save")
|
|
|
+ @ApiOperation("添加")
|
|
|
+ @Lock("device-create-ids-save")
|
|
|
+ public R save(@RequestBody @Validated DeviceCreateIdsDto.Save save) {
|
|
|
+ LocalDate localDate = LocalDate.now();
|
|
|
+ String year = String.valueOf(localDate.getYear());
|
|
|
+ String month = String.valueOf(localDate.getMonthValue());
|
|
|
+ String yearMonth = year.substring(2) + (month.length() == 1 ? "0" + month : month);
|
|
|
+ String property = LambdaUtils.getProperty(DeviceCreateIds::getDeviceId);
|
|
|
+ QueryWrapper<DeviceCreateIds> queryWrapper = new QueryWrapper<DeviceCreateIds>().select(String.format("max(%s) as %s", StringTools.humpToLine(property), property));
|
|
|
+ DeviceCreateIds deviceCreateIds = getOne(queryWrapper);
|
|
|
+ JList<DeviceCreateIds> deviceCreateIdss = new JArrayList<>(save.getNum());
|
|
|
+ for (int i = 0; i < save.getNum(); i++) {
|
|
|
+ String tail = null;
|
|
|
+ if (deviceCreateIds != null) {
|
|
|
+ String substring = String.valueOf(deviceCreateIds.getDeviceId()).substring(0, 4);
|
|
|
+ if (substring.equals(yearMonth)) {
|
|
|
+ tail = String.valueOf(deviceCreateIds.getDeviceId() + (i + 1)).substring(4);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (tail == null) {
|
|
|
+ tail = String.valueOf((i + 1));
|
|
|
+ int i0 = 6 - tail.length();
|
|
|
+ for (int i1 = 0; i1 < i0; i1++) {
|
|
|
+ tail = 0 + tail;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Long deviceId = Long.valueOf(yearMonth + tail);
|
|
|
+ DeviceCreateIds deviceCreateIdsInfo = copy(DeviceCreateIds.class, save)
|
|
|
+ .setDeviceId(deviceId)
|
|
|
+ //.setCreateUserId(AuthorizeUtils.getLoginId(Long.class))
|
|
|
+ .setCreateTime(LocalDateTime.now());
|
|
|
+ deviceCreateIdss.add(deviceCreateIdsInfo);
|
|
|
+ }
|
|
|
+ JList<JList<DeviceCreateIds>> partition = deviceCreateIdss.partition(20);
|
|
|
+ CountDownLatch latch = new CountDownLatch(partition.size());
|
|
|
+ for (JList<DeviceCreateIds> createIds : partition) {
|
|
|
+ executor.execute(() -> {
|
|
|
+ try {
|
|
|
+ saveBatch(createIds);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("", e);
|
|
|
+ } finally {
|
|
|
+ latch.countDown();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ latch.await();
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("page")
|
|
|
+ @ApiOperation("分页查询")
|
|
|
+ public R<PageBean<DeviceCreateIdsDto.Vo>> page(@RequestBody DeviceCreateIdsDto.Page page) {
|
|
|
+ PageBean pageBean = page.getPage();
|
|
|
+ LambdaQueryWrapper<DeviceCreateIds> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceCreateIds.class)
|
|
|
+ .ge(DeviceCreateIds::getCreateTime, page.getBeginCreateTime())
|
|
|
+ .le(DeviceCreateIds::getCreateTime, page.getEndCreateTime())
|
|
|
+ .ge(DeviceCreateIds::getBindTime, page.getBeginBindTime())
|
|
|
+ .le(DeviceCreateIds::getBindTime, page.getEndBindTime())
|
|
|
+ .build()
|
|
|
+ .orderByDesc(!Emptys.check(pageBean.getOrders()), DeviceCreateIds::getCreateTime);
|
|
|
+ IPage<DeviceCreateIds> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
|
|
|
+ IPage<DeviceCreateIdsDto.Vo> voIPage = PlusBeans.copy(DeviceCreateIdsDto.Vo.class, iPage);
|
|
|
+ PageBean<DeviceCreateIdsDto.Vo> voPageBean = toPageBean(voIPage);
|
|
|
+ List<DeviceCreateIdsDto.Vo> records = voPageBean.getRecords();
|
|
|
+
|
|
|
+ JList<Long> createUserIds = new JArrayList<>(records).getProperty(DeviceCreateIdsDto.Vo::getCreateUserId);
|
|
|
+ List<SysWorkVo> sysWorkVos = sysWorkUserService.list(new ListDto().setId(createUserIds)).getData();
|
|
|
+ JMap<Long, SysWorkVo> sysWorkVoJMap = new JArrayList<>(sysWorkVos).toMap(SysWorkVo::getId).cover();
|
|
|
+ records.forEach(vo -> {
|
|
|
+ SysWorkVo sysWorkVo = sysWorkVoJMap.get(vo.getCreateUserId());
|
|
|
+ if (sysWorkVo == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ vo.setCreateUserName(sysWorkVo.getAccount());
|
|
|
+ });
|
|
|
+ return R.ok(voPageBean);
|
|
|
+ }
|
|
|
+}
|