规范化类的名称
This commit is contained in:
@@ -0,0 +1,294 @@
|
||||
package com.rj.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.rj.entity.LbFanManagement;
|
||||
import com.rj.mapper.LbFanManagementMapper;
|
||||
import com.rj.service.ILbFanManagementService;
|
||||
import com.rj.tenant.TenantContextHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 粉丝管理服务实现
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class LbFanManagementServiceImpl
|
||||
extends ServiceImpl<LbFanManagementMapper, LbFanManagement>
|
||||
implements ILbFanManagementService {
|
||||
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
@Override
|
||||
public Map<String, Object> add(LbFanManagement entity) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
trimStringFields(entity);
|
||||
fillTenantIdIfAbsent(entity);
|
||||
Map<String, Object> keyError = validateCompositeKey(entity);
|
||||
if (keyError != null) {
|
||||
return keyError;
|
||||
}
|
||||
Map<String, Object> idTenantError = fillAndValidateIdTenantId(entity);
|
||||
if (idTenantError != null) {
|
||||
return idTenantError;
|
||||
}
|
||||
if (existsByIdTenantId(entity.getIdTenantId())) {
|
||||
result.put("success", false);
|
||||
result.put("message", "唯一ID已存在:" + entity.getIdTenantId());
|
||||
return result;
|
||||
}
|
||||
if (entity.getShareNum() == null) {
|
||||
entity.setShareNum(0);
|
||||
}
|
||||
if (entity.getCreatedAt() == null) {
|
||||
entity.setCreatedAt(LocalDateTime.now());
|
||||
}
|
||||
|
||||
boolean ok = this.save(entity);
|
||||
result.put("success", ok);
|
||||
result.put("message", ok ? "新增成功" : "新增失败");
|
||||
if (ok) {
|
||||
result.put("data", entity);
|
||||
}
|
||||
return result;
|
||||
} catch (DuplicateKeyException e) {
|
||||
log.warn("粉丝管理新增重复键, idTenantId={}", entity.getIdTenantId());
|
||||
result.put("success", false);
|
||||
result.put("message", "唯一ID已存在:" + entity.getIdTenantId());
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("粉丝管理新增异常", e);
|
||||
result.put("success", false);
|
||||
result.put("message", "新增异常:" + e.getMessage());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> update(LbFanManagement entity) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
trimStringFields(entity);
|
||||
fillTenantIdIfAbsent(entity);
|
||||
Map<String, Object> keyError = validateCompositeKey(entity);
|
||||
if (keyError != null) {
|
||||
return keyError;
|
||||
}
|
||||
Map<String, Object> idTenantError = fillAndValidateIdTenantId(entity);
|
||||
if (idTenantError != null) {
|
||||
return idTenantError;
|
||||
}
|
||||
LbFanManagement existing = this.getById(entity.getIdTenantId());
|
||||
if (existing == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "记录不存在,id_tenant_id=" + entity.getIdTenantId());
|
||||
return result;
|
||||
}
|
||||
if (entity.getCreatedAt() == null) {
|
||||
entity.setCreatedAt(existing.getCreatedAt());
|
||||
}
|
||||
// 唯一主键与联合键字段不可变更
|
||||
entity.setIdTenantId(existing.getIdTenantId());
|
||||
entity.setTenantId(existing.getTenantId());
|
||||
entity.setId(existing.getId());
|
||||
|
||||
boolean ok = this.updateById(entity);
|
||||
result.put("success", ok);
|
||||
result.put("message", ok ? "修改成功" : "修改失败");
|
||||
if (ok) {
|
||||
result.put("data", this.getById(entity.getIdTenantId()));
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("粉丝管理修改异常", e);
|
||||
result.put("success", false);
|
||||
result.put("message", "修改异常:" + e.getMessage());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> deleteByIdTenantId(String idTenantId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (idTenantId == null || idTenantId.trim().isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "唯一ID(id_tenant_id)不能为空");
|
||||
return result;
|
||||
}
|
||||
String key = idTenantId.trim();
|
||||
boolean ok = this.removeById(key);
|
||||
result.put("success", ok);
|
||||
result.put("message", ok ? "删除成功" : "删除失败,记录可能不存在");
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("粉丝管理删除异常, idTenantId={}", idTenantId, e);
|
||||
result.put("success", false);
|
||||
result.put("message", "删除异常:" + e.getMessage());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> pageQuery(Integer current,
|
||||
Integer size,
|
||||
String nickname,
|
||||
String mobile,
|
||||
Integer shareNumMin,
|
||||
Integer shareNumMax,
|
||||
String createdAtStart,
|
||||
String createdAtEnd) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (current == null || current < 1) {
|
||||
current = 1;
|
||||
}
|
||||
if (size == null || size < 1) {
|
||||
size = 10;
|
||||
}
|
||||
|
||||
LambdaQueryWrapper<LbFanManagement> queryWrapper = new LambdaQueryWrapper<>();
|
||||
if (nickname != null && !nickname.trim().isEmpty()) {
|
||||
queryWrapper.like(LbFanManagement::getNickname, nickname.trim());
|
||||
}
|
||||
if (mobile != null && !mobile.trim().isEmpty()) {
|
||||
queryWrapper.like(LbFanManagement::getMobile, mobile.trim());
|
||||
}
|
||||
if (shareNumMin != null) {
|
||||
queryWrapper.ge(LbFanManagement::getShareNum, shareNumMin);
|
||||
}
|
||||
if (shareNumMax != null) {
|
||||
queryWrapper.le(LbFanManagement::getShareNum, shareNumMax);
|
||||
}
|
||||
|
||||
LocalDateTime start = parseDateTime(createdAtStart);
|
||||
if (createdAtStart != null && !createdAtStart.trim().isEmpty() && start == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "createdAtStart 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
|
||||
return result;
|
||||
}
|
||||
LocalDateTime end = parseDateTime(createdAtEnd);
|
||||
if (createdAtEnd != null && !createdAtEnd.trim().isEmpty() && end == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "createdAtEnd 格式错误,请使用 yyyy-MM-dd HH:mm:ss");
|
||||
return result;
|
||||
}
|
||||
if (start != null) {
|
||||
queryWrapper.ge(LbFanManagement::getCreatedAt, start);
|
||||
}
|
||||
if (end != null) {
|
||||
queryWrapper.le(LbFanManagement::getCreatedAt, end);
|
||||
}
|
||||
queryWrapper.orderByDesc(LbFanManagement::getCreatedAt);
|
||||
|
||||
Page<LbFanManagement> page = this.page(new Page<>(current, size), queryWrapper);
|
||||
result.put("success", true);
|
||||
result.put("message", "查询成功");
|
||||
result.put("data", page.getRecords());
|
||||
result.put("total", page.getTotal());
|
||||
result.put("current", page.getCurrent());
|
||||
result.put("size", page.getSize());
|
||||
result.put("pages", page.getPages());
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("粉丝管理分页查询异常", e);
|
||||
result.put("success", false);
|
||||
result.put("message", "查询异常:" + e.getMessage());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成唯一ID:id + '_' + tenant_id
|
||||
*/
|
||||
public static String buildIdTenantId(Long id, String tenantId) {
|
||||
return id + "_" + tenantId;
|
||||
}
|
||||
|
||||
private Map<String, Object> validateCompositeKey(LbFanManagement entity) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
if (entity.getTenantId() == null || entity.getTenantId().trim().isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "租户ID不能为空");
|
||||
return result;
|
||||
}
|
||||
if (entity.getId() == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "粉丝ID不能为空");
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充 id_tenant_id,并与传入值校验一致。
|
||||
*/
|
||||
private Map<String, Object> fillAndValidateIdTenantId(LbFanManagement entity) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
String expected = buildIdTenantId(entity.getId(), entity.getTenantId());
|
||||
if (entity.getIdTenantId() == null || entity.getIdTenantId().trim().isEmpty()) {
|
||||
entity.setIdTenantId(expected);
|
||||
return null;
|
||||
}
|
||||
String actual = entity.getIdTenantId().trim();
|
||||
if (!expected.equals(actual)) {
|
||||
result.put("success", false);
|
||||
result.put("message", "id_tenant_id 与 id、tenant_id 不一致,应为:" + expected);
|
||||
return result;
|
||||
}
|
||||
entity.setIdTenantId(actual);
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean existsByIdTenantId(String idTenantId) {
|
||||
return this.getById(idTenantId) != null;
|
||||
}
|
||||
|
||||
private void fillTenantIdIfAbsent(LbFanManagement entity) {
|
||||
if (entity.getTenantId() == null || entity.getTenantId().trim().isEmpty()) {
|
||||
String tenantId = TenantContextHolder.getTenantId();
|
||||
if (tenantId != null && !tenantId.trim().isEmpty()) {
|
||||
entity.setTenantId(tenantId.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void trimStringFields(LbFanManagement entity) {
|
||||
if (entity.getIdTenantId() != null) {
|
||||
entity.setIdTenantId(entity.getIdTenantId().trim());
|
||||
}
|
||||
if (entity.getTenantId() != null) {
|
||||
entity.setTenantId(entity.getTenantId().trim());
|
||||
}
|
||||
if (entity.getAvatar() != null) {
|
||||
entity.setAvatar(entity.getAvatar().trim());
|
||||
}
|
||||
if (entity.getNickname() != null) {
|
||||
entity.setNickname(entity.getNickname().trim());
|
||||
}
|
||||
if (entity.getMobile() != null) {
|
||||
entity.setMobile(entity.getMobile().trim());
|
||||
}
|
||||
}
|
||||
|
||||
private LocalDateTime parseDateTime(String text) {
|
||||
if (text == null || text.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return LocalDateTime.parse(text.trim(), DATE_TIME_FORMATTER);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user