This commit is contained in:
2024-05-23 20:42:04 +08:00
parent 8c965a4838
commit 311b9c0ca9
11 changed files with 217 additions and 10 deletions

View File

@@ -13,7 +13,6 @@ import cn.lihongjie.coal.base.mapper.BaseMapper;
import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.common.DictCode;
import cn.lihongjie.coal.common.TreeUtils;
import cn.lihongjie.coal.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.exception.BizException;
@@ -208,9 +207,13 @@ public class Codegen {
"archiveStatusName",
Modifier.PRIVATE)
.addAnnotation(
AnnotationSpec.builder(
DictTranslate.class)
.addMember("dictKey", "$T.$N", ClassName.get(DictCode.class), "ARCHIVESTATUS" )
AnnotationSpec.builder(DictTranslate.class)
.addMember(
"dictKey",
"$T.$N",
ClassName.get(
"cn.lihongjie.coal.common", "DictCode"),
"ARCHIVESTATUS")
.build())
.build())
.build();

View File

@@ -52,6 +52,9 @@ public class DictCode {
public static final String EXCEL_TEMPLATE_TYPE = "excel.template.type";
public static final String DEVICE_CATEGORY_DEVICECODEGENRULE =
"device.category.deviceCodeGenRule";
public static final String EXCEL_GENERATOR_DATASOURCE_TYPE = "excel.generator.datasource.type";
public static final String DEVICE_WORKORDER_STATUS = "device.workOrder.status";

View File

@@ -4,6 +4,7 @@ import cn.lihongjie.coal.annotation.OrgScope;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.dto.R;
import cn.lihongjie.coal.device.dto.CreateDeviceDto;
import cn.lihongjie.coal.device.dto.DeviceDto;
import cn.lihongjie.coal.device.dto.UpdateDeviceDto;
@@ -31,6 +32,11 @@ public class DeviceController {
return this.service.create(request);
}
@PostMapping("/genCode")
public R<String> genCode(@RequestBody CreateDeviceDto request) {
return R.success(this.service.genCode(request));
}
@PostMapping("/update")
public DeviceDto update(@RequestBody UpdateDeviceDto request) {
return this.service.update(request);

View File

@@ -10,7 +10,10 @@ import cn.lihongjie.coal.device.dto.UpdateDeviceDto;
import cn.lihongjie.coal.device.entity.DeviceEntity;
import cn.lihongjie.coal.device.mapper.DeviceMapper;
import cn.lihongjie.coal.device.repository.DeviceRepository;
import cn.lihongjie.coal.deviceCategory.entity.DeviceCategoryEntity;
import cn.lihongjie.coal.deviceCategory.service.DeviceCategoryService;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.sequence.SequenceService;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
@@ -20,6 +23,7 @@ import jakarta.persistence.criteria.JoinType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
@@ -46,15 +50,106 @@ public class DeviceService extends BaseService<DeviceEntity, DeviceRepository> {
return getById(entity.getId());
}
@Autowired DeviceCategoryService deviceCategoryService;
@Autowired SequenceService sequenceService;
public String genCode(CreateDeviceDto request) {
String categoryId = request.getCategory();
DeviceCategoryEntity category = null;
try {
category = deviceCategoryService.get(categoryId);
} catch (Exception e) {
throw new BizException("设备类目不存在");
}
String genRule = StringUtils.defaultIfBlank(category.getDeviceCodeGenRule(), "3");
if (!StringUtils.equals(genRule, "3")
&& category.getCodeLength() == null) {
throw new BizException("设备类目编码长度不能为空");
}
StringBuilder sb = new StringBuilder();
switch (genRule) {
case "0":
String key = "device.global." + Ctx.currentUser().getOrganizationId();
sequenceService.createSequence(key, 0);
Long next = sequenceService.nextVal(key);
return String.format(
"%s%0" + category.getCodeLength() + "d",
StringUtils.defaultIfBlank(category.getCodePrefix(), ""),
next);
case "1":
var curr = category;
while (curr != null) {
if (StringUtils.isBlank(curr.getCode())) {
throw new BizException("设备类目编码不能为空: " + curr.getName());
}
sb.insert(0, curr.getCode());
curr = curr.getParent();
}
key = "device.category." + category.getId();
sequenceService.createSequence(key, 0);
next = sequenceService.nextVal(key);
sb.append(
String.format(
"%0" + category.getCodeLength() + "d",
next));
return StringUtils.defaultIfBlank(category.getCodePrefix(), "") + sb;
case "2":
key = "device.category." + category.getId();
sequenceService.createSequence(key, 0);
next = sequenceService.nextVal(key);
if (StringUtils.isBlank(category.getCode())) {
throw new BizException("设备类目编码不能为空: " + category.getName());
}
sb.append(category.getCode());
sb.append(
String.format(
"%0" + category.getCodeLength() + "d",
next));
return StringUtils.defaultIfBlank(category.getCodePrefix(), "") + sb;
case "3":
return "";
}
return null;
}
@PersistenceContext EntityManager em;
public DeviceDto update(UpdateDeviceDto request) {
DeviceEntity entity = this.repository.get(request.getId());
String orgCode = entity.getCode();
if (this.repository.containArchived(request.getId())) {
throw new BizException("部分数据已归档,无法编辑或删除");
}
this.mapper.updateEntity(entity, request);
entity.setCode(orgCode);
this.repository.save(entity);
return getById(entity.getId());
@@ -123,7 +218,8 @@ public class DeviceService extends BaseService<DeviceEntity, DeviceRepository> {
public Object workOrderCount(String id) {
return em.createQuery(
"select type as type, count(1) as cnt from DeviceWorkOrderEntity where device.id = :id group by type", DeviceWorkOrderSummary.class)
"select type as type, count(1) as cnt from DeviceWorkOrderEntity where device.id = :id group by type",
DeviceWorkOrderSummary.class)
.setParameter("id", id)
.getResultList();
}

View File

@@ -4,10 +4,17 @@ import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import org.hibernate.annotations.Comment;
import java.util.List;
@Data
public class CreateDeviceCategoryDto extends OrgCommonDto {
private String parent;
private List<String> users;
@Comment("设备编码生成规则")
private String deviceCodeGenRule;
private Integer codeLength;
private String codePrefix;
}

View File

@@ -2,9 +2,13 @@ package cn.lihongjie.coal.deviceCategory.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import cn.lihongjie.coal.base.dto.SimpleDto;
import cn.lihongjie.coal.common.DictCode;
import cn.lihongjie.coal.pojoProcessor.DictTranslate;
import lombok.Data;
import org.hibernate.annotations.Comment;
import java.util.List;
@Data
@@ -16,4 +20,15 @@ public class DeviceCategoryDto extends OrgCommonDto {
private List<String> childrenDeviceIds;
private List<String> selfDeviceIds;
@Comment("设备编码生成规则")
private String deviceCodeGenRule;
private Integer codeLength;
private String codePrefix;
@DictTranslate(dictKey = DictCode.DEVICE_CATEGORY_DEVICECODEGENRULE)
private String deviceCodeGenRuleName;
}

View File

@@ -2,9 +2,13 @@ package cn.lihongjie.coal.deviceCategory.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import cn.lihongjie.coal.base.dto.SimpleDto;
import cn.lihongjie.coal.common.DictCode;
import cn.lihongjie.coal.pojoProcessor.DictTranslate;
import lombok.Data;
import org.hibernate.annotations.Comment;
import java.util.List;
@Data
@@ -19,4 +23,15 @@ public class DeviceCategoryTreeDto extends OrgCommonDto {
private List<DeviceCategoryTreeDto> children;
private String parent;
@Comment("设备编码生成规则")
private String deviceCodeGenRule;
private Integer codeLength;
private String codePrefix;
@DictTranslate(dictKey = DictCode.DEVICE_CATEGORY_DEVICECODEGENRULE)
private String deviceCodeGenRuleName;
}

View File

@@ -4,10 +4,18 @@ import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import org.hibernate.annotations.Comment;
import java.util.List;
@Data
public class UpdateDeviceCategoryDto extends OrgCommonDto {
private String parent;
private List<String> users;
@Comment("设备编码生成规则")
private String deviceCodeGenRule;
private Integer codeLength;
private String codePrefix;
}

View File

@@ -7,6 +7,8 @@ import jakarta.persistence.*;
import lombok.Data;
import org.hibernate.annotations.Comment;
import java.util.List;
@Data
@@ -18,6 +20,13 @@ public class DeviceCategoryEntity extends OrgCommonEntity {
private List<UserEntity> users;
@Comment("设备编码生成规则")
private String deviceCodeGenRule;
private Integer codeLength;
private String codePrefix;
@ManyToOne private DeviceCategoryEntity parent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)

View File

@@ -15,13 +15,23 @@ public class SequenceService {
@PersistenceContext EntityManager em;
public void createSequence(String name, Integer start) {
createSequence(name, start, 1);
}
public void createSequence(String name, Integer start, Integer step) {
if (start <= 1){
start = 1;
}
name = processName(name);
int count =
em.createNativeQuery(
"CREATE SEQUENCE IF NOT EXISTS "
"CREATE SEQUENCE IF NOT EXISTS \""
+ name
+ " START WITH "
+ "\" START WITH "
+ start
+ " INCREMENT BY "
+ step)
@@ -30,18 +40,30 @@ public class SequenceService {
log.info("create sequence {} update count {}", name, count);
}
private String processName(String name) {
return name.replace(".", "_")
.replace("-", "_")
.replace(" ", "_")
.replace("/", "_")
.replace("\\", "_");
}
public Long nextVal(String name) {
return ((Number) em.createNativeQuery("select nextval(\"" + name + "\")").getSingleResult())
name = processName(name);
return ((Number) em.createNativeQuery("select nextval('" + name + "')").getSingleResult())
.longValue();
}
public Long currVal(String name) {
return ((Number) em.createNativeQuery("select currval(\"" + name + "\")").getSingleResult())
name = processName(name);
return ((Number) em.createNativeQuery("select currval('" + name + "')").getSingleResult())
.longValue();
}
public void dropSequence(String name) {
int count = em.createNativeQuery("DROP SEQUENCE IF EXISTS " + name).executeUpdate();
name = processName(name);
int count = em.createNativeQuery("DROP SEQUENCE IF EXISTS '" + name + "'").executeUpdate();
log.info("drop sequence {} update count {}", name, count);
}

View File

@@ -2068,6 +2068,29 @@
}
]
},
{
"code": "device.category.deviceCodeGenRule",
"name": "设备编码生成规则",
"item": [
{
"code": "0",
"name": "全局递增"
},
{
"code": "1",
"name": "父级类目编码+本级类目编码+递增"
},
{
"code": "2",
"name": "本级类目编码+递增"
},
{
"code": "3",
"name": "手动输入"
}
]
},
{
"code": "excel.generator.datasource.type",
"name": "excel生成器数据源类型",