package ${packageName}.service.impl; import cn.hutool.core.util.ObjectUtil; import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.StringUtils; #if($table.crud) import org.dromara.common.core.domain.PageResult; import org.dromara.common.mybatis.core.page.PageQuery; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; #end import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; #if($enableUnique) import com.baomidou.mybatisplus.core.toolkit.Wrappers; #end import org.dromara.common.mybatis.core.query.QueryBuilder; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import ${packageName}.domain.bo.${ClassName}Bo; import ${packageName}.domain.vo.${ClassName}Vo; import ${packageName}.domain.${ClassName}; import ${packageName}.mapper.${ClassName}Mapper; import ${packageName}.service.I${ClassName}Service; #if($table.tree) import org.dromara.common.core.exception.ServiceException; #end import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Collection; /** * ${functionName}Service业务层处理 * * @author ${author} * @date ${datetime} */ @Slf4j @RequiredArgsConstructor @Service public class ${ClassName}ServiceImpl implements I${ClassName}Service { private final ${ClassName}Mapper ${className}Mapper; #set($TreeParentCap = "") #if("" != $treeParentCode) #set($TreeParentCap = $treeParentCode.substring(0,1).toUpperCase() + $treeParentCode.substring(1)) #end #set($TreeAncestorsCap = "") #if("" != $treeAncestorsField) #set($TreeAncestorsCap = $treeAncestorsField.substring(0,1).toUpperCase() + $treeAncestorsField.substring(1)) #end #set($TreeOrderCap = "") #if("" != $treeOrderField) #set($TreeOrderCap = $treeOrderField.substring(0,1).toUpperCase() + $treeOrderField.substring(1)) #end /** * 查询${functionName} * * @param ${pkColumn.javaField} 主键 * @return ${functionName} */ @Override public ${ClassName}Vo queryById(${pkColumn.javaType} ${pkColumn.javaField}) { return ${className}Mapper.selectVoById(${pkColumn.javaField}); } #if($table.crud) /** * 分页查询${functionName}列表 * * @param bo 查询条件 * @param pageQuery 分页参数 * @return ${functionName}分页列表 */ @Override public PageResult<${ClassName}Vo> queryPageList(${ClassName}Bo bo, PageQuery pageQuery) { LambdaQueryWrapper<${ClassName}> lqw = buildQueryWrapper(bo); Page<${ClassName}Vo> result = ${className}Mapper.selectVoPage(pageQuery.build(), lqw); return PageResult.build(result.getRecords(), result.getTotal()); } #end /** * 查询符合条件的${functionName}列表 * * @param bo 查询条件 * @return ${functionName}列表 */ @Override public List<${ClassName}Vo> queryList(${ClassName}Bo bo) { LambdaQueryWrapper<${ClassName}> lqw = buildQueryWrapper(bo); return ${className}Mapper.selectVoList(lqw); } #if($enableUnique) /** * 校验${functionName}是否满足组合唯一约束 * * @param bo ${functionName} * @return 是否唯一 */ @Override public boolean checkUnique(${ClassName}Bo bo) { boolean hasUniqueValue = true; #foreach($column in $uniqueColumns) #if($column.javaType == 'String') hasUniqueValue = hasUniqueValue && StringUtils.isNotBlank(bo.get${column.capJavaField}()); #else hasUniqueValue = hasUniqueValue && bo.get${column.capJavaField}() != null; #end #end if (!hasUniqueValue) { return true; } LambdaQueryWrapper<${ClassName}> lqw = Wrappers.lambdaQuery(); #foreach($column in $uniqueColumns) lqw.eq(${ClassName}::get${column.capJavaField}, bo.get${column.capJavaField}()); #end lqw.ne(bo.get${pkColumn.capJavaField}() != null, ${ClassName}::get${pkColumn.capJavaField}, bo.get${pkColumn.capJavaField}()); return !${className}Mapper.exists(lqw); } #end private LambdaQueryWrapper<${ClassName}> buildQueryWrapper(${ClassName}Bo bo) { #set($hasBetween = false) #foreach ($column in $columns) #if($column.query && $column.queryType == 'BETWEEN') #set($hasBetween = true) #end #end #if($hasBetween) Map params = bo.getParams(); #end return QueryBuilder.lambda(${ClassName}.class) #foreach($column in $columns) #if($column.query) #set($queryType=$column.queryType) #set($javaType=$column.javaType) #set($AttrName=$column.capJavaField) #set($mpMethod=$column.queryType.toLowerCase()) #if($queryType != 'BETWEEN') #if($javaType == 'String') #set($condition='StringUtils.isNotBlank(bo.get'+$AttrName+'())') #if($queryType == 'LIKE') .likeIfText(${ClassName}::get$AttrName, bo.get$AttrName()) #elseif($queryType == 'EQ') .eqIfText(${ClassName}::get$AttrName, bo.get$AttrName()) #elseif($queryType == 'NE') .neIfText(${ClassName}::get$AttrName, bo.get$AttrName()) #else .$mpMethod($condition, ${ClassName}::get$AttrName, bo.get$AttrName()) #end #else #set($condition='bo.get'+$AttrName+'() != null') #if($queryType == 'EQ') .eqIfPresent(${ClassName}::get$AttrName, bo.get$AttrName()) #elseif($queryType == 'NE') .neIfPresent(${ClassName}::get$AttrName, bo.get$AttrName()) #elseif($queryType == 'GT') .gtIfPresent(${ClassName}::get$AttrName, bo.get$AttrName()) #elseif($queryType == 'LT') .ltIfPresent(${ClassName}::get$AttrName, bo.get$AttrName()) #else .$mpMethod($condition, ${ClassName}::get$AttrName, bo.get$AttrName()) #end #end #else .betweenParams(${ClassName}::get$AttrName, params, "begin$AttrName", "end$AttrName") #end #end #end #if($table.tree && "" != $treeAncestorsField) .orderByAsc(${ClassName}::get${TreeAncestorsCap}) #end #if($table.tree && "" != $treeParentCode) .orderByAsc(${ClassName}::get${TreeParentCap}) #end #if($table.tree && "" != $treeOrderField) .orderByAsc(${ClassName}::get${TreeOrderCap}) #elseif($enableSort) .orderByAsc(${ClassName}::get${sortColumn.capJavaField}) #end .orderByAsc(${ClassName}::get${pkColumn.capJavaField}) .build(); } /** * 新增${functionName} * * @param bo ${functionName} * @return 是否新增成功 */ @Override public Boolean insertByBo(${ClassName}Bo bo) { ${ClassName} add = MapstructUtils.convert(bo, ${ClassName}.class); #if($table.tree) fillTreeMetaBeforeSave(add, false); #end validEntityBeforeSave(add); boolean flag = ${className}Mapper.insert(add) > 0; if (flag) { bo.set${pkColumn.capJavaField}(add.get${pkColumn.capJavaField}()); } return flag; } /** * 修改${functionName} * * @param bo ${functionName} * @return 是否修改成功 */ @Override public Boolean updateByBo(${ClassName}Bo bo) { ${ClassName} update = MapstructUtils.convert(bo, ${ClassName}.class); #if($table.tree) fillTreeMetaBeforeSave(update, true); #end validEntityBeforeSave(update); return ${className}Mapper.updateById(update) > 0; } #if($enableStatus) /** * 修改${functionName}状态 * * @param ${pkColumn.javaField} 主键 * @param status 状态值 * @return 是否修改成功 */ @Override public Boolean updateStatus(${pkColumn.javaType} ${pkColumn.javaField}, ${statusColumn.javaType} status) { return ${className}Mapper.lambda() .set(${ClassName}::get${statusColumn.capJavaField}, status) .eq(${ClassName}::get${pkColumn.capJavaField}, ${pkColumn.javaField}) .update(); } #end #if($enableSort) /** * 调整${functionName}排序 * * @param ${pkColumn.javaField} 主键 * @param sortValue 排序值 * @return 是否修改成功 */ @Override public Boolean updateSort(${pkColumn.javaType} ${pkColumn.javaField}, ${sortColumn.javaType} sortValue) { return ${className}Mapper.lambda() .set(${ClassName}::get${sortColumn.capJavaField}, sortValue) .eq(${ClassName}::get${pkColumn.capJavaField}, ${pkColumn.javaField}) .update(); } #end /** * 保存前的数据校验 */ private void validEntityBeforeSave(${ClassName} entity) { // 可在此扩展通用业务校验 } #if($table.tree) private void fillTreeMetaBeforeSave(${ClassName} entity, boolean updateMode) { #if("" != $treeParentCode) if (entity.get${TreeParentCap}() == null) { entity.set${TreeParentCap}(${treeRootValueJavaLiteral}); } if (ObjectUtil.equal(entity.get${pkColumn.capJavaField}(), entity.get${TreeParentCap}())) { throw new ServiceException("${functionName}父节点不能选择自身"); } #if("" != $treeAncestorsField) ${ClassName} parent = null; if (!ObjectUtil.equal(entity.get${TreeParentCap}(), ${treeRootValueJavaLiteral})) { parent = ${className}Mapper.selectById(entity.get${TreeParentCap}()); if (ObjectUtil.isNull(parent)) { throw new ServiceException("${functionName}父节点不存在"); } } if (updateMode && entity.get${pkColumn.capJavaField}() != null && ObjectUtil.isNotNull(parent) && containsAncestor(parent.get${TreeAncestorsCap}(), entity.get${pkColumn.capJavaField}())) { throw new ServiceException("不能选择当前节点或其子节点作为父节点"); } String newAncestors = resolveAncestors(entity.get${TreeParentCap}(), parent); if (updateMode && entity.get${pkColumn.capJavaField}() != null) { ${ClassName} oldEntity = ${className}Mapper.selectById(entity.get${pkColumn.capJavaField}()); if (ObjectUtil.isNull(oldEntity)) { throw new ServiceException("${functionName}不存在,无法修改"); } String oldAncestors = oldEntity.get${TreeAncestorsCap}(); entity.set${TreeAncestorsCap}(newAncestors); if (!StringUtils.equals(oldAncestors, newAncestors)) { updateChildrenAncestors(entity.get${pkColumn.capJavaField}(), newAncestors, oldAncestors); } } else { entity.set${TreeAncestorsCap}(newAncestors); } #end #end } #if("" != $treeAncestorsField) private String resolveAncestors(${treeParentColumn.javaType} parentId, ${ClassName} parent) { if (ObjectUtil.equal(parentId, ${treeRootValueJavaLiteral})) { return "${treeRootValue}"; } String parentAncestors = parent.get${TreeAncestorsCap}(); if (StringUtils.isBlank(parentAncestors)) { return String.valueOf(parentId); } return parentAncestors + StringUtils.SEPARATOR + parentId; } private void updateChildrenAncestors(${pkColumn.javaType} currentId, String newAncestors, String oldAncestors) { List<${ClassName}> children = ${className}Mapper.lambda() .select(${ClassName}::get${pkColumn.capJavaField}, ${ClassName}::get${TreeAncestorsCap}) .findInSet(currentId, ${ClassName}::get${TreeAncestorsCap}) .list(); List<${ClassName}> updateList = new ArrayList<>(); for (${ClassName} child : children) { String ancestors = child.get${TreeAncestorsCap}(); if (StringUtils.isBlank(ancestors)) { continue; } ${ClassName} update = new ${ClassName}(); update.set${pkColumn.capJavaField}(child.get${pkColumn.capJavaField}()); update.set${TreeAncestorsCap}(StringUtils.replaceOnce(ancestors, oldAncestors, newAncestors)); updateList.add(update); } if (!updateList.isEmpty()) { ${className}Mapper.updateBatchById(updateList); } } private boolean containsAncestor(String ancestors, ${pkColumn.javaType} nodeId) { for (String item : StringUtils.splitList(ancestors)) { if (StringUtils.equals(item, String.valueOf(nodeId))) { return true; } } return false; } #end #end /** * 校验并批量删除${functionName}信息 * * @param ids 待删除的主键集合 * @param isValid 是否进行有效性校验 * @return 是否删除成功 */ @Override public Boolean deleteWithValidByIds(Collection<${pkColumn.javaType}> ids, Boolean isValid) { if (isValid) { // 可在此扩展删除前业务校验 } return ${className}Mapper.deleteByIds(ids) > 0; } }