package ssw.{app_code}.service.impl; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import jnpf.base.service.SuperServiceImpl; import jnpf.mapper.XxxMapper; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import ssw.{app_code}.entity.XxxEntity; import ssw.{app_code}.service.XxxService; import java.util.HashMap; import java.util.List; import java.util.Map; /** * {业务描述} Service 实现 * * ⭐ FIX-TEMPLATE-V0.12+ (jxcj 实战验证, 端到端跑通 200): * - extends SuperServiceImpl — jnpf 基类 * - @DS("{app_code}") 在 Service 类上 * - 配套: Entity extends SuperBaseEntity, Mapper 放 jnpf.mapper, Controller 也加 @DS * * ⚠️ FIX-TEMPLATE-2/3: 关键 2 修复 * 1. create() 必须设 fSortCode=0 默认值 (PLT-NOT-NULL 根因) * 2. update() 用反射合并非空字段 (PUT 时未传字段保留原值) * * ⚠️ FIX-TEMPLATE-1: 必须用 @DS("{app_code}") 注解指定业务库 * 不加 @DS 会使用 master 主库, 导致找不到业务表 */ @Service @DS("{app_code}") // 对应 application-dev.yml 里 spring.datasource.dynamic.datasource.{app_code} @RequiredArgsConstructor public class XxxServiceImpl extends SuperServiceImpl implements XxxService { @Override public List getList(String keyword, String status) { QueryWrapper qw = new QueryWrapper<>(); if (keyword != null && !keyword.isEmpty()) { qw.and(w -> w.like("f_xxx_name", keyword).or().like("f_xxx_code", keyword)); } if (status != null) qw.eq("f_status", status); qw.orderByDesc("f_create_time"); return list(qw); } @Override public XxxEntity getInfo(String id) { return getById(id); } @Override public void create(XxxEntity entity) { // fId 由父类 SuperBaseEntity.@TableId 自动生成雪花 ID, 无需手动 set // ⭐ FIX-TEMPLATE-2: fSortCode 默认值 (避免 INSERT 触发 f_sort_code NOT NULL 违规) if (entity.getSortCode() == null) entity.setSortCode(0L); save(entity); } @Override public boolean update(XxxEntity entity) { // ⭐ FIX-TEMPLATE-3: 反射合并非空字段 (避免 updateById 带 null 字段触发 NOT NULL) XxxEntity db = this.getById(entity.getId()); if (db == null) return false; try { java.lang.reflect.Field[] fields = XxxEntity.class.getDeclaredFields(); for (java.lang.reflect.Field fld : fields) { fld.setAccessible(true); Object v = fld.get(entity); if (v != null) fld.set(db, v); } } catch (Exception e) { e.printStackTrace(); return false; } return updateById(db); } @Override public boolean delete(String id) { return removeById(id); } @Override public Map getStats() { Map stats = new HashMap<>(); List all = list(); stats.put("total", all.size()); return stats; } }