package ${packageName}.service.impl;

import java.util.*;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiaoba.api.auth.utils.SecurityUtil;
import com.qiaoba.common.base.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;

import ${packageName}.entity.${ClassName};
import ${packageName}.entity.dto.${ClassName}Dto;
import ${packageName}.entity.param.${ClassName}Param;
import ${packageName}.service.${ClassName}Service;
import ${packageName}.mapper.${ClassName}Mapper;

/**
 * ${functionName} 服务层实现
 *
 * @author ${author}
 * @version 1.0
 * @since ${datetime}
 */
@Service
@RequiredArgsConstructor
public class ${ClassName}ServiceImpl implements ${ClassName}Service {

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

    @Override
    public List<${ClassName}> selectList(${ClassName}Param param) {
        return ${className}Mapper.selectList(param2Wrapper(param));
    }

    @Override
    public TableDataInfo<${ClassName}> selectPageList(${ClassName}Param param, PageQuery pageQuery) {
        return TableDataInfo.build(${className}Mapper.selectPage(pageQuery.build(), param2Wrapper(param)));
    }

    @Override
    public ${ClassName} selectById(String ${pkColumn.javaField}, boolean allowNull) {
        ${ClassName} ${className} =${className}Mapper.selectById(${pkColumn.javaField});

        if (!allowNull && Objects.isNull(${className})) {
            throw new ServiceException("未查询到有关信息");
        }

        return ${className};
    }

    @Override
    public int insert(${ClassName}Dto dto) {

        ${ClassName} ${className} =dto2Entity(dto);
        ${className}.setCreateTime(new Date());
        ${className}.setCreateUser(SecurityUtil.getLoginUsername());

        return ${className}Mapper.insert(${className});
    }

    @Override
    public int updateById(${ClassName}Dto dto) {

        ${ClassName} ${className} =dto2Entity(dto);
        ${className}.setUpdateTime(new Date());
        ${className}.setUpdateUser(SecurityUtil.getLoginUsername());

        return ${className}Mapper.updateById(${className});
    }

    @Override
    public int deleteById(String ${pkColumn.javaField}) {
        return ${className}Mapper.deleteById(${pkColumn.javaField});
    }

    @Override
    public int deleteByIds(List<String> ${pkColumn.javaField}s) {
        return ${className}Mapper.deleteBatchIds(${pkColumn.javaField}s);
    }

    private ${ClassName} dto2Entity(${ClassName}Dto dto) {
        ${ClassName} ${className} = new ${ClassName}();
    #foreach ($column in $columns)
    #if(!$generator.isBaseColumn($column.javaField))
    ## 首字母大写
    #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
    ${className}.set$AttrName(dto.get$AttrName());
    #end
    #end
        return ${className};
    }

    private QueryWrapper<${ClassName}> param2Wrapper(${ClassName}Param param) {
        QueryWrapper<${ClassName}> wrapper = new QueryWrapper<>();
#if($queryColumns.size() > 0)
        wrapper.lambda()
#foreach ($column in $queryColumns)
#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#if($column.queryType == "EQ")
               .eq(ObjectUtil.isNotEmpty(param.get${AttrName}()), ${ClassName}::get${AttrName}, param.get${AttrName}())#if($foreach.count == $queryColumns.size());#end
#elseif($column.queryType == "NE")
               .ne(ObjectUtil.isNotEmpty(param.get${AttrName}()), ${ClassName}::get${AttrName}, param.get${AttrName}())#if($foreach.count == $queryColumns.size());#end
#elseif($column.queryType == "GT")
               .gt(ObjectUtil.isNotEmpty(param.get${AttrName}()), ${ClassName}::get${AttrName}, param.get${AttrName}())#if($foreach.count == $queryColumns.size());#end
#elseif($column.queryType == "GTE")
               .ge(ObjectUtil.isNotEmpty(param.get${AttrName}()), ${ClassName}::get${AttrName}, param.get${AttrName}())#if($foreach.count == $queryColumns.size());#end
#elseif($column.queryType == "LT")
               .lt(ObjectUtil.isNotEmpty(param.get${AttrName}()), ${ClassName}::get${AttrName}, param.get${AttrName}())#if($foreach.count == $queryColumns.size());#end
#elseif($column.queryType == "LTE")
               .le(ObjectUtil.isNotEmpty(param.get${AttrName}()), ${ClassName}::get${AttrName}, param.get${AttrName}())#if($foreach.count == $queryColumns.size());#end
#elseif($column.queryType == "LIKE")
               .like(ObjectUtil.isNotEmpty(param.get${AttrName}()), ${ClassName}::get${AttrName}, param.get${AttrName}())#if($foreach.count == $queryColumns.size());#end
#elseif($column.queryType == "BETWEEN")
               .between((ObjectUtil.isNotEmpty(param.getStart${AttrName}()) && ObjectUtil.isNotEmpty(param.getEnd${AttrName}())),
               ${ClassName}::get${AttrName}, param.getStart${AttrName}(), param.getEnd${AttrName}())#if($foreach.count == $queryColumns.size());#end
#end
#end
#end
        return wrapper;
    }
}
