package ${packageName}.service.impl;

import java.util.List;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.utils.StringUtils;
#foreach ($column in $columns)
#if($column.javaField == 'createTime' || $column.javaField == 'updateTime')
import com.ruoyi.common.utils.DateUtils;
#break
#end
#end
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
#if($table.sub)
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import ${packageName}.domain.${subClassName};
#end
import ${packageName}.mapper.${ClassName}Mapper;
import ${packageName}.domain.${ClassName};
import ${packageName}.service.I${ClassName}Service;

/**
 * ${functionName}Service业务层处理
 * 
 * @author ${author}
 * @date ${datetime}
 */
@Service
public class ${ClassName}ServiceImpl extends MPJBaseServiceImpl<${ClassName}Mapper, ${ClassName}> implements I${ClassName}Service
{
    @Resource
    private ${ClassName}Mapper ${className}Mapper;
#if($table.sub)
    @Resource
    private ${subClassName}Mapper ${subClassName}Mapper;
#end


    /**
     * 查询${functionName}
     * 
     * @param ${pkColumn.javaField} ${functionName}主键
     * @return ${functionName}
     */
    @Override
    public ${ClassName} select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField})
    {
        return getById(${pkColumn.javaField});
    }

    /**
     * 查询${functionName}列表
     * 
     * @param ${className} ${functionName}
     * @return ${functionName}
     */
    @Override
    public List<${ClassName}> select${ClassName}List(${ClassName} ${className})
    {
        MPJLambdaWrapper<${ClassName}> select${ClassName}List = new MPJLambdaWrapper<${ClassName}>()
                    .selectAll(${ClassName}.class)
        #foreach($column in $columns)
            #set($queryType=$column.queryType)
            #set($javaField=$column.javaField)
            #set($javaType=$column.javaType)
            #set($columnName=$column.columnName)
            #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
            #if($column.query)
                #if($column.queryType == "EQ")
                    .eq(StringUtils.isNotBlank(${className}.get${AttrName}()),${ClassName}::get${AttrName},${className}.get${AttrName}())
                #elseif($queryType == "NE")
                    .ne(StringUtils.isNotBlank(${className}.get${AttrName}()),${ClassName}::get${AttrName},${className}.get${AttrName}())
                #elseif($queryType == "GT")
                    .gt(StringUtils.isNotBlank(${className}.get${AttrName}()),${ClassName}::get${AttrName},${className}.get${AttrName}())
                #elseif($queryType == "GTE")
                    .ge(StringUtils.isNotBlank(${className}.get${AttrName}()),${ClassName}::get${AttrName},${className}.get${AttrName}())
                #elseif($queryType == "LT")
                    .lt(StringUtils.isNotBlank(${className}.get${AttrName}()),${ClassName}::get${AttrName},${className}.get${AttrName}())
                #elseif($queryType == "LTE")
                    .le(StringUtils.isNotBlank(${className}.get${AttrName}()),${ClassName}::get${AttrName},${className}.get${AttrName}())
                #elseif($queryType == "LIKE")
                    .like(StringUtils.isNotBlank(${className}.get${AttrName}()),${ClassName}::get${AttrName},${className}.get${AttrName}())
                #elseif($queryType == "BETWEEN")
                    .between(StringUtils.isNotBlank(${className}.getParams().get("begin${AttrName}")) && StringUtils.isNotBlank(${className}.getParams().get("end${AttrName}")),
                    ${ClassName}::get${AttrName}, ${className}.getParams().get("begin${AttrName}"), ${className}.getParams().get("endn${AttrName}"))
                #end
            #end
        #end ;

        return selectJoinList(${ClassName}.class, select${ClassName}List);
    }

    /**
     * 新增${functionName}
     * 
     * @param ${className} ${functionName}
     * @return 结果
     */
#if($table.sub)
    @Transactional
#end
    @Override
    public boolean insert${ClassName}(${ClassName} ${className})
    {
#if($table.sub)
        boolean rows = save(${className});
        insert${subClassName}(${className});
        return rows;
#else
        return save(${className});
#end
    }

    /**
     * 修改${functionName}
     * 
     * @param ${className} ${functionName}
     * @return 结果
     */
#if($table.sub)
    @Transactional
#end
    @Override
    public boolean update${ClassName}(${ClassName} ${className})
    {
#if($table.sub)
    subTableFkName
        #set($SubTableFkName=$subTableFkName.substring(0,1).toUpperCase() + ${subTableFkName.substring(1)})
        ${subClassName}Mapper.delete(new LambdaQueryWrapper<${subClassName}>()
                .eq(${subClassName}::get${SubTableFkName},${className}.get${pkColumn.capJavaField}()))
        insert${subClassName}(${className});
#end
        return updateById(${className});
    }

    /**
     * 批量删除${functionName}
     * 
     * @param ${pkColumn.javaField}s 需要删除的${functionName}主键
     * @return 结果
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public boolean delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaType}[] ${pkColumn.javaField}s)
    {
#if($table.sub)
    #set($SubTableFkName=$subTableFkName.substring(0,1).toUpperCase() + ${subTableFkName.substring(1)})
        ${subClassName}Mapper.delete(new LambdaQueryWrapper<${subClassName}>()
            .eq(${subClassName}::get${SubTableFkName},${pkColumn.javaField}s));
#end
        return removeBatchByIds(Arrays.asList(${pkColumn.javaField}s));
    }

    /**
     * 删除${functionName}信息
     * 
     * @param ${pkColumn.javaField} ${functionName}主键
     * @return 结果
     */
#if($table.sub)
    @Transactional
#end
    @Override
    public boolean delete${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField})
    {
#if($table.sub)
    #set($SubTableFkName=$subTableFkName.substring(0,1).toUpperCase() + ${subTableFkName.substring(1)})
        ${subClassName}Mapper.delete(new LambdaQueryWrapper<${subClassName}>()
            .eq(${subClassName}::get${SubTableFkName},${pkColumn.javaField}))
#end
        return removeById(${pkColumn.javaField});
    }
#if($table.sub)

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param ${className} ${functionName}对象
     */
    public void insert${subClassName}(${ClassName} ${className})
    {
        List<${subClassName}> ${subclassName}List = ${className}.get${subClassName}List();
        ${pkColumn.javaType} ${pkColumn.javaField} = ${className}.get${pkColumn.capJavaField}();
        if (StringUtils.isNotNull(${subclassName}List))
        {
            List<${subClassName}> list = new ArrayList<${subClassName}>();
            for (${subClassName} ${subclassName} : ${subclassName}List)
            {
                ${subclassName}.set${subTableFkClassName}(${pkColumn.javaField});
                list.add(${subclassName});
            }
            if (list.size() > 0)
            {
                ${subClassName}Mapper.saveBatch(list);
            }
        }
    }
#end
}
