package ${packageName}.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;
import java.util.List;
import java.util.Map;
#if($table.sub)
import java.util.Arrays;
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 ServiceImpl<${ClassName}Mapper, ${ClassName}> implements I${ClassName}Service {

    @Autowired
    private ${ClassName}Mapper ${className}Mapper;

    /**
     * 查询${functionName}列表
     *
     * @param ${className} ${functionName}
     * @return ${functionName}
     */
    @Override
    public List<${ClassName}> select${ClassName}List(${ClassName} ${className})
    {
        List<${ClassName}> ${className}List = ${className}Mapper.selectList(buildQueryWrapper(${className}));
#if($table.sub)
        if (StringUtils.isNotNull(${className}List)) {
            for (${ClassName} temp : ${className}List) {
                List<${subClassName}> ${subclassName}List = ${className}Mapper.select${subClassName}By${pkColumn.capJavaField}(temp.get${pkColumn.capJavaField}());
                temp.set${subClassName}List(${subclassName}List);
            }
        }
#end
        return ${className}List;
    }

#if($table.sub)
    /**
     * 查询${functionName}
     *
     * @param ${pkColumn.javaField} ${functionName}主键
     * @return ${functionName}
     */
    @Override
    public ${ClassName} select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField})
    {
        ${ClassName} ${className} = ${className}Mapper.selectById(${pkColumn.javaField});
        List<${subClassName}> ${subclassName}List = ${className}Mapper.select${subClassName}By${pkColumn.capJavaField}(${pkColumn.javaField});
        ${className}.set${subClassName}List(${subclassName}List);
        return ${className};
    }

    /**
     * 新增${functionName}
     *
     * @param ${className} ${functionName}
     * @return 结果
     */
    @Transactional
    @Override
    public int insert${ClassName}(${ClassName} ${className})
    {
        int rows = ${className}Mapper.insert(${className});
        insert${subClassName}(${className});
        return rows;
    }

    /**
     * 修改${functionName}
     *
     * @param ${className} ${functionName}
     * @return 结果
     */
    @Transactional
    @Override
    public int update${ClassName}(${ClassName} ${className})
    {
        ${className}Mapper.delete${subClassName}By${subTableFkClassName}(${className}.get${pkColumn.capJavaField}());
        insert${subClassName}(${className});
        return ${className}Mapper.updateById(${className});
    }

    /**
     * 批量删除${functionName}
     *
     * @param ${pkColumn.javaField}s 需要删除的${functionName}主键
     * @return 结果
     */
    @Transactional
    @Override
    public int delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaType}[] ${pkColumn.javaField}s)
    {
        List<${pkColumn.javaType}> ${pkColumn.javaField}List = Arrays.asList(${pkColumn.javaField}s);
        for (${pkColumn.javaType} ${pkColumn.javaField} : ${pkColumn.javaField}List) {
            ${className}Mapper.delete${subClassName}By${subTableFkClassName}(${pkColumn.javaField});
        }
        return ${className}Mapper.deleteBatchIds(${pkColumn.javaField}List);
    }

    /**
     * 删除${functionName}信息
     *
     * @param ${pkColumn.javaField} ${functionName}主键
     * @return 结果
     */
    @Transactional
    @Override
    public int delete${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField})
    {
        ${className}Mapper.delete${subClassName}By${subTableFkClassName}(${pkColumn.javaField});
        return ${className}Mapper.deleteById(${pkColumn.javaField});
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param ${className} ${functionName}对象
     */
    private 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)) {
            for (${subClassName} ${subclassName} : ${subclassName}List) {
                ${subclassName}.set${subTableFkClassName}(${pkColumn.javaField});
            }
            ${className}Mapper.batchInsert${subClassName}(${subclassName}List);
        }
    }
#end

    private LambdaQueryWrapper<${ClassName}> buildQueryWrapper(${ClassName} query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<${ClassName}> lqw = Wrappers.lambdaQuery();
#foreach($column in $columns)
    #if($column.query)
        #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)})
        #set($mpMethod=$column.queryType.toLowerCase())
        #if($queryType != 'BETWEEN')
            #if($javaType == 'String')
                #set($condition='StringUtils.isNotBlank(query.get'+$AttrName+'())')
            #else
                #set($condition='query.get'+$AttrName+'() != null')
            #end
        lqw.$mpMethod($condition, ${ClassName}::get$AttrName, query.get$AttrName());
        #else
        lqw.between(params.get("begin$AttrName") != null && params.get("end$AttrName") != null,
                ${ClassName}::get$AttrName ,params.get("begin$AttrName"), params.get("end$AttrName"));
        #end
    #end
    #if($column.javaField == 'createTime')
        #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
        lqw.orderByDesc(${ClassName}::get$AttrName);
    #end
#end
        return lqw;
    }

}
