package ${packageName}.service.impl;
<#if table.cacheServiceType==0 >
    <#assign Service = "AbstractCRUDServiceImpl">
<#elseif table.cacheServiceType==1 >
    <#assign Service = "AbstractCRUDCacheServiceImpl">
<#elseif table.cacheServiceType==2 >
    <#assign Service = "AbstractCRUDDynamicCacheServiceImpl">
<#else >
    <#assign Service = "AbstractCRUDServiceImpl">
</#if>
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.${Service};
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import ${packageName}.dao.${ClassName}Dao;
import ${packageName}.model.${ClassName}Entity;
import ${packageName}.service.${ClassName}Service;
<#if table.tplCategory=="sub" || table.tplCategory=="subone">
import org.springframework.beans.factory.annotation.Autowired;
import ${subPackageName}.model.${subClassName}Entity;
import ${subPackageName}.model.${subClassName}Query;
import ${subPackageName}.service.${subClassName}Service;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Arrays;
</#if>
<#if pkColumn??&&pkColumn.isIncrement==0 >
import cn.hutool.core.util.IdUtil;
</#if>
<#if pkColumn??&&pkColumn.isIncrement==0 >
    <#assign Temp>
    @Override
    protected void saveBefore(${ClassName}Entity entity, Context context) throws AppException {
    //非系统自增，需这里设置主键
    <#if pkColumn.javaType=="String" >
        entity.set${pkColumn.javaField?cap_first}(IdUtil.fastSimpleUUID());
    <#elseif pkColumn.javaType=="Integer">
        entity.set${pkColumn.javaField?cap_first}(((Long)IdUtil.getSnowflake(0,1).nextId()).intValue());
    <#elseif pkColumn.javaType=="Long">
        entity.set${pkColumn.javaField?cap_first}(IdUtil.getSnowflake(0,1).nextId());
    </#if>
    super.saveBefore(entity, context);
    }
    </#assign>
    <#else>
    <#assign Temp ></#assign>
</#if>
/**
* ${ClassName}Service
* ${functionName} service实现
*
* @author ${author}
* @date ${datetime}
*/
@Service("${ClassName?uncap_first}Service")
public class ${ClassName}ServiceImpl extends ${Service}<${ClassName}Dao, ${ClassName}Entity, ${pkColumn.javaType}> implements ${ClassName}Service {
<#if table.tplCategory=="sub">
    @Autowired
    private ${subClassName}Service ${subclassName}Service;
    ${Temp}
    @Override
    protected void saveAfter(${ClassName}Entity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.get${subClassName}List())){
            entity.get${subClassName}List().stream().peek(item->{
            <#if subPkColumn??&&subPkColumn.isIncrement==0 >
            <#if subPkColumn.javaType=="String" >
                item.set${subPkColumn.javaField?cap_first}(IdUtil.fastSimpleUUID());
            <#elseif subPkColumn.javaType=="Integer">
                item.set${subPkColumn.javaField?cap_first}(((Long)IdUtil.getSnowflake(1024,512).nextId()).intValue());
            <#elseif subPkColumn.javaType=="Long">
                item.set${subPkColumn.javaField?cap_first}(IdUtil.getSnowflake(1024,512).nextId());
            </#if>
            </#if>
                item.set${subTableFkClassName}(entity.get${pkColumn.javaField?cap_first}());
                item.setCreateUser(context==null?"":context.getUser().getLoginName());
                item.setCreateUserId(context==null?"":context.getUser().getId());
                item.setCreateTime(new Date());
            }).count();
            ${subclassName}Service.save(entity.get${subClassName}List());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(${ClassName}Entity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.get${subClassName}List())){
            ${subPkColumn.javaType}[] ${subclassName}Ids = ${subclassName}Service.find(new ${subClassName}Query().${subTableFkclassName}(entity.get${pkColumn.javaField?cap_first}())).stream().map(${subClassName}Entity::get${subPkColumn.javaField?cap_first}).toArray(${subPkColumn.javaType}[]::new);
            ${subclassName}Service.remove(${subclassName}Ids,context);
            entity.get${subClassName}List().stream().peek(item ->{
        <#if subPkColumn??&&subPkColumn.isIncrement==0 >
            <#if subPkColumn.javaType=="String" >
                item.set${subPkColumn.javaField?cap_first}(IdUtil.fastSimpleUUID());
            <#elseif subPkColumn.javaType=="Integer">
                item.set${subPkColumn.javaField?cap_first}(((Long)IdUtil.getSnowflake(1024,512).nextId()).intValue());
            <#elseif subPkColumn.javaType=="Long">
                item.set${subPkColumn.javaField?cap_first}(IdUtil.getSnowflake(1024,512).nextId());
            </#if>
        </#if>
                item.set${subTableFkClassName}(entity.get${pkColumn.javaField?cap_first}());
                item.setCreateUser(context==null?"":context.getUser().getLoginName());
                item.setCreateUserId(context==null?"":context.getUser().getId());
                item.setCreateTime(new Date());
                item.setUpdateUser(context==null?"":context.getUser().getLoginName());
                item.setUpdateUserId(context==null?"":context.getUser().getId());
                item.setUpdateTime(new Date());
            }).count();
            ${subclassName}Service.save(entity.get${subClassName}List());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(${pkColumn.javaType}[] ids, Context context, int result) throws AppException {
        List<${subClassName}Entity> ${subclassName}list = ${subclassName}Service.find(new ${subClassName?cap_first}Query().${subTableFkClassName?uncap_first}List(Arrays.asList(ids)));
        ${subclassName}Service.removeList(${subclassName}list,context);
        super.removeAfter(ids, context, result);
    }
    <#elseif table.tplCategory=="subone">
    @Autowired
    private ${subClassName}Service ${subclassName}Service;
    ${Temp}
    @Override
    protected void saveAfter(${ClassName}Entity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.get${subClassName}Entity())){
            ${subClassName}Entity ${subclassName}Entity = new ${subClassName}Entity();
            ${subclassName}Entity.initAttrValue();
            ${subClassName}Entity subEntity=entity.get${subClassName}Entity();
            BeanUtils.copyProperties(subEntity,${subclassName}Entity);
        <#if subPkColumn??&&subPkColumn.isIncrement==0&&subPkColumn.javaType=="String" >
            ${subclassName}Entity.set${subPkColumn.javaField?cap_first}(IdUtil.fastSimpleUUID());
        <#elseif subPkColumn??&&subPkColumn.isIncrement==0&&subPkColumn.javaType=="Integer">
            ${subclassName}Entity.set${subPkColumn.javaField?cap_first}(((Long)IdUtil.getSnowflake(1024,512).nextId()).intValue());
        <#elseif subPkColumn??&&subPkColumn.isIncrement==0&&subPkColumn.javaType=="Long">
            ${subclassName}Entity.set${subPkColumn.javaField?cap_first}(IdUtil.getSnowflake(1024,512).nextId());
        </#if>
            ${subclassName}Entity.set${subTableFkClassName}(entity.get${pkColumn.javaField?cap_first}());
            ${subclassName}Entity.setCreateUser(context==null?"":context.getUser().getLoginName());
            ${subclassName}Entity.setCreateUserId(context==null?"":context.getUser().getId());
            ${subclassName}Entity.setCreateTime(new Date());
            ${subclassName}Service.save(${subclassName}Entity,context);
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(${ClassName}Entity entity, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(entity.get${subClassName}Entity())){
            ${subClassName}Entity subEntity=entity.get${subClassName}Entity();
            subEntity.setUpdateUser(context==null?"":context.getUser().getLoginName());
            subEntity.setUpdateUserId(context==null?"":context.getUser().getId());
            subEntity.setUpdateTime(new Date());
            ${subclassName}Service.update(subEntity,context);
        }
        super.updateAfter(entity, context);
    }
    @Override
    protected void removeAfter(${pkColumn.javaType}[] ids, Context context, int result) throws AppException {
        List<${subClassName}Entity> ${subclassName}list = ${subclassName}Service.find(new ${subClassName?cap_first}Query().${subTableFkClassName?uncap_first}List(Arrays.asList(ids)));
        ${subclassName}Service.removeList(${subclassName}list,context);
        super.removeAfter(ids, context, result);
    }
        <#else>
    ${Temp}
</#if>
}