package ${packageName}.service.impl;

import ${packageName}.entity.${ClassName};
import ${packageName}.entity.dto.${ClassName}Dto;
import ${packageName}.entity.vo.${ClassName}Vo;
import ${packageName}.entity.query.${ClassName}Query;
import ${packageName}.mapper.${ClassName}Mapper;
import ${packageName}.service.${ClassName}Service;
#if($table.hasOptionApi())
import cn.fateverse.common.core.entity.Option;
#end
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.mybatis.utils.PageUtils;
import cn.fateverse.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ${functionName} Controller
 *
 * @author ${author}
 * @date ${dateTime}
 */
@Slf4j
@Service
public class ${ClassName}ServiceImpl implements ${ClassName}Service {

    private final ${ClassName}Mapper ${className}Mapper;

    public ${ClassName}ServiceImpl(${ClassName}Mapper ${className}Mapper) {
        this.${className}Mapper = ${className}Mapper;
    }

    @Override
    public ${ClassName}Vo searchById(${pkColumn.javaType} ${pkColumn.javaField}){
        ${ClassName} ${className} = ${className}Mapper.selectById(${pkColumn.javaField});
        return ${ClassName}Vo.to${ClassName}Vo(${className});
    }

    @Override
    public TableDataInfo<${ClassName}Vo> searchList(${ClassName}Query query){
        PageUtils.startPage();
        List<${ClassName}> list = ${className}Mapper.selectList(query);
        return PageUtils.convertDataTable(list, ${ClassName}Vo::to${ClassName}Vo);
    }

#if($table.hasOptionApi())
    @Override
    public List<Option> searchOptionList(){
        ${ClassName}Query query = new ${ClassName}Query();
        List<${ClassName}> list = ${className}Mapper.selectList(query);
        return list.stream().map(item-> Option.builder()
                                      .value(item.get${table.getOptionValueFiled()}())
                                      .label(item.get${table.getOptionLabelFiled()}())
                                      .build()).collect(Collectors.toList());
    }
#end

    @Override
    public List<${ClassName}Vo> exportList(${ClassName}Query query){
        List<${ClassName}> list = ${className}Mapper.selectList(query);
        return list.stream().map(${ClassName}Vo::to${ClassName}Vo)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(${ClassName}Dto ${className}){
        ${ClassName} info = ${className}.to${ClassName}();
        info.setCreateBy(SecurityUtils.getUsername());
        return ${className}Mapper.insert(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(${ClassName}Dto ${className}){
        ${ClassName} info = ${className}.to${ClassName}();
        info.setUpdateBy(SecurityUtils.getUsername());
        return ${className}Mapper.update(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(${pkColumn.javaType} ${pkColumn.javaField}){
        return ${className}Mapper.deleteById(${pkColumn.javaField});
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeBatch(List<${pkColumn.javaType}> ${pkColumn.javaField}List){
        return ${className}Mapper.deleteBatchByIdList(${pkColumn.javaField}List);
    }

}