serviceImpl.java.ftl 4.07 KB
package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import javax.annotation.Resource;
import com.hotent.base.util.BeanUtils;

<#------------ 子表Manager包引入  ---------->
<#if cfg.boEntList?exists>
	<#list cfg.cgUtil.getSubModelInfo(cfg.boEntList,table.name) as subModel>
import ${package.Entity}.${subModel.className};
import ${package.Service}.${subModel.className}Manager;
	</#list>
</#if>
<#------------ END 子表Manager包引入 ---------->

/**
 * ${table.comment!} 服务实现类
 *
 <#if cfg.companyName??>
 * @company ${cfg.companyName}
 </#if>
 * @author ${author}
 <#if cfg.authorEmail??>
 * @email ${cfg.authorEmail}
 </#if>
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {
<#if cfg.type != "querySqldef">
<#------------ 子表Manager注入  ---------->
<#if cfg.boEntList?exists>
	<#list cfg.cgUtil.getSubModelInfo(cfg.boEntList,table.name) as subModel>
	
 	@Resource
	${subModel.className}Manager ${subModel.className?uncap_first}Manager;
	</#list>
</#if>
<#------------END 子表Manager注入  ---------->

	@Override
	public ${entity} getDetail(String id) {
    	${entity} ${entity?uncap_first} = this.get(id);
    	
<#------------ 子表数据获取  ---------->		
<#if cfg.boEntList?exists>
	<#list cfg.cgUtil.getSubModelInfo(cfg.boEntList,table.name) as subModel>
		// 获取子表 ${subModel.comment}数据
		QueryWrapper<${subModel.className}> ${subModel.className?uncap_first}QueryWrapper = new QueryWrapper<>();
		${subModel.className?uncap_first}QueryWrapper.eq("REF_ID_", id);
		List<${subModel.className}> ${subModel.className?uncap_first}List = ${subModel.className?uncap_first}Manager.list(${subModel.className?uncap_first}QueryWrapper);
		${entity?uncap_first}.set${subModel.propertyName?cap_first}(${subModel.className?uncap_first}List);
		<#if (cfg.cgUtil.getSubModelInfo(cfg.boEntList,subModel.tableName)?size>0) >
		${subModel.className?uncap_first}List.forEach(e->{
			try {
				BeanUtils.copyNotNullProperties(e, ${subModel.className?uncap_first}Manager.getDetail(e.getId()));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		});
		</#if>
	</#list>
</#if>
<#------------ END 子表数据获取  ---------->	

		return ${entity?uncap_first};
	}
	@Override
	@Transactional
	public void createOrUpdate(${entity} ${entity?uncap_first}) {
		//新建或更新
		this.saveOrUpdate(${entity?uncap_first});
<#------------ 子表数据增删改查  ---------->		
<#if cfg.boEntList?exists>
	<#if cfg.cgUtil.getSubModelInfo(cfg.boEntList,table.name)?? && (cfg.cgUtil.getSubModelInfo(cfg.boEntList,table.name)?size>0) >
		//主表主键
		String refId = ${entity?uncap_first}.getId();
		</#if>
		<#list cfg.cgUtil.getSubModelInfo(cfg.boEntList,table.name) as subModel>
		//删除${subModel.comment}数据
		QueryWrapper<${subModel.className}> ${subModel.className?uncap_first}QueryWrapper = new QueryWrapper<>();
		${subModel.className?uncap_first}QueryWrapper.eq("REF_ID_", refId);
		${subModel.className?uncap_first}Manager.remove(${subModel.className?uncap_first}QueryWrapper);
		List<${subModel.className}> ${subModel.className?uncap_first}List = ${entity?uncap_first}.get${subModel.propertyName?cap_first}();
		if(BeanUtils.isNotEmpty(${subModel.className?uncap_first}List)){
			${subModel.className?uncap_first}List.forEach(item->{
				//设置主表主键
				item.setRefId(refId);
				//保存${subModel.comment}数据
				${subModel.className?uncap_first}Manager.createOrUpdate(item);
			});
		}
	
	</#list>
</#if>
<#------------ END 子表数据增删改查  ---------->		
	}
</#if>
}
</#if>