|
|
@@ -0,0 +1,141 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.entity.FamilyRelationType;
|
|
|
+import com.etotem.cfc.entity.FamilyRelationship;
|
|
|
+import com.etotem.cfc.mapper.FamilyRelationTypeMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyRelationshipMapper;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import com.etotem.cfc.util.ParamUtils;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class FamilyRelationTypeService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FamilyRelationTypeMapper familyRelationTypeMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FamilyRelationshipMapper familyRelationshipMapper;
|
|
|
+
|
|
|
+ public List<FamilyRelationType> listTypes(Map<String, Object> body) {
|
|
|
+ LambdaQueryWrapper<FamilyRelationType> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ String typeKey = ParamUtils.getString(body.get("typeKey"));
|
|
|
+ if (typeKey != null) {
|
|
|
+ wrapper.eq(FamilyRelationType::getTypeKey, typeKey);
|
|
|
+ }
|
|
|
+ String typeName = ParamUtils.getString(body.get("typeName"));
|
|
|
+ if (typeName != null) {
|
|
|
+ wrapper.like(FamilyRelationType::getTypeName, typeName);
|
|
|
+ }
|
|
|
+ Integer enabled = ParamUtils.getBoolean(body.get("enabled")) == null ? null
|
|
|
+ : (ParamUtils.getBoolean(body.get("enabled")) ? 1 : 0);
|
|
|
+ if (enabled != null) {
|
|
|
+ wrapper.eq(FamilyRelationType::getEnabled, enabled);
|
|
|
+ }
|
|
|
+ wrapper.orderByAsc(FamilyRelationType::getSortOrder).orderByDesc(FamilyRelationType::getId);
|
|
|
+ List<FamilyRelationType> rows = familyRelationTypeMapper.selectList(wrapper);
|
|
|
+ for (FamilyRelationType row : rows) {
|
|
|
+ row.setReferenceCount(countReferences(row.getTypeKey()));
|
|
|
+ }
|
|
|
+ return rows;
|
|
|
+ }
|
|
|
+
|
|
|
+ public FamilyRelationType saveType(Map<String, Object> body, Long adminId) {
|
|
|
+ String typeKey = ParamUtils.getString(body.get("typeKey"));
|
|
|
+ String typeName = ParamUtils.getString(body.get("typeName"));
|
|
|
+ if (typeKey == null || typeName == null) {
|
|
|
+ throw new IllegalArgumentException("typeKey和typeName不能为空");
|
|
|
+ }
|
|
|
+ FamilyRelationType existing = familyRelationTypeMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<FamilyRelationType>()
|
|
|
+ .eq(FamilyRelationType::getTypeKey, typeKey)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ if (existing != null) {
|
|
|
+ throw new IllegalArgumentException("关系类型已存在");
|
|
|
+ }
|
|
|
+ FamilyRelationType row = new FamilyRelationType();
|
|
|
+ row.setTypeKey(typeKey);
|
|
|
+ row.setTypeName(typeName);
|
|
|
+ row.setLabel(ParamUtils.getString(body.get("label")));
|
|
|
+ row.setDescription(ParamUtils.getString(body.get("description")));
|
|
|
+ row.setSortOrder(ParamUtils.getLong(body.get("sortOrder")) == null ? 0
|
|
|
+ : ParamUtils.getLong(body.get("sortOrder")).intValue());
|
|
|
+ row.setEnabled(ParamUtils.getBoolean(body.get("enabled")) != null
|
|
|
+ ? (ParamUtils.getBoolean(body.get("enabled")) ? 1 : 0) : 1);
|
|
|
+ Date now = new Date();
|
|
|
+ row.setCreatedAt(now);
|
|
|
+ row.setUpdatedAt(now);
|
|
|
+ familyRelationTypeMapper.insert(row);
|
|
|
+ return row;
|
|
|
+ }
|
|
|
+
|
|
|
+ public FamilyRelationType updateType(Map<String, Object> body) {
|
|
|
+ Long id = ParamUtils.getLong(body.get("id"));
|
|
|
+ if (id == null) {
|
|
|
+ throw new IllegalArgumentException("id不能为空");
|
|
|
+ }
|
|
|
+ FamilyRelationType existing = familyRelationTypeMapper.selectById(id);
|
|
|
+ if (existing == null) {
|
|
|
+ throw new IllegalArgumentException("关系类型不存在");
|
|
|
+ }
|
|
|
+ String typeName = ParamUtils.getString(body.get("typeName"));
|
|
|
+ if (typeName != null) {
|
|
|
+ existing.setTypeName(typeName);
|
|
|
+ }
|
|
|
+ String label = ParamUtils.getString(body.get("label"));
|
|
|
+ if (label != null) {
|
|
|
+ existing.setLabel(label);
|
|
|
+ }
|
|
|
+ String description = ParamUtils.getString(body.get("description"));
|
|
|
+ if (description != null) {
|
|
|
+ existing.setDescription(description);
|
|
|
+ }
|
|
|
+ Long sortOrder = ParamUtils.getLong(body.get("sortOrder"));
|
|
|
+ if (sortOrder != null) {
|
|
|
+ existing.setSortOrder(sortOrder.intValue());
|
|
|
+ }
|
|
|
+ Boolean enabled = ParamUtils.getBoolean(body.get("enabled"));
|
|
|
+ if (enabled != null) {
|
|
|
+ existing.setEnabled(enabled ? 1 : 0);
|
|
|
+ }
|
|
|
+ existing.setUpdatedAt(new Date());
|
|
|
+ familyRelationTypeMapper.updateById(existing);
|
|
|
+ existing.setReferenceCount(countReferences(existing.getTypeKey()));
|
|
|
+ return existing;
|
|
|
+ }
|
|
|
+
|
|
|
+ public FamilyRelationType disableType(Map<String, Object> body) {
|
|
|
+ Long id = ParamUtils.getLong(body.get("id"));
|
|
|
+ if (id == null) {
|
|
|
+ throw new IllegalArgumentException("id不能为空");
|
|
|
+ }
|
|
|
+ Boolean confirm = ParamUtils.getBoolean(body.get("confirm"));
|
|
|
+ if (!Boolean.TRUE.equals(confirm)) {
|
|
|
+ throw new IllegalArgumentException("被引用关系类型需先解绑或确认停用");
|
|
|
+ }
|
|
|
+ FamilyRelationType existing = familyRelationTypeMapper.selectById(id);
|
|
|
+ if (existing == null) {
|
|
|
+ throw new IllegalArgumentException("关系类型不存在");
|
|
|
+ }
|
|
|
+ existing.setEnabled(0);
|
|
|
+ existing.setUpdatedAt(new Date());
|
|
|
+ familyRelationTypeMapper.updateById(existing);
|
|
|
+ existing.setReferenceCount(countReferences(existing.getTypeKey()));
|
|
|
+ return existing;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int countReferences(String typeKey) {
|
|
|
+ if (typeKey == null || typeKey.isEmpty()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ Long count = familyRelationshipMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<FamilyRelationship>()
|
|
|
+ .eq(FamilyRelationship::getRelationshipKey, typeKey));
|
|
|
+ return count == null ? 0 : count.intValue();
|
|
|
+ }
|
|
|
+}
|