package {{packageName}}.{{entity.module}}.mapper;

import java.util.List;
import org.apache.ibatis.annotations.*;
import java.util.Map;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import java.util.Map;
import org.apache.ibatis.annotations.Select;
import {{packageName}}.{{entity.module}}.domain.{{entity.codeName}};
import {{packageName}}.{{entity.module}}.filter.{{entity.codeName}}SearchContext;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import java.io.Serializable;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.alibaba.fastjson.JSONObject;

{{#dsName}}
@DS("{{entity.dataSource}}")
{{/dsName}}
public interface {{entity.codeName}}Mapper extends BaseMapper<{{entity.codeName}}> {


    {{#entity.dataSets}}
    Page<{{#if enableGroup}}Map{{else}}{{entity.codeName}}{{/if}}> search{{pascalCase codeName}}(IPage page, @Param("ctx") {{entity.codeName}}SearchContext context, @Param("ew") Wrapper<{{entity.codeName}}> wrapper);
    List<{{#if enableGroup}}Map{{else}}{{entity.codeName}}{{/if}}> list{{pascalCase codeName}}(@Param("ctx") {{entity.codeName}}SearchContext context, @Param("ew") Wrapper<{{entity.codeName}}> wrapper);

    {{/entity.dataSets}}
    @Override
    {{#entity.enableEntityCache}}
    @Cacheable(value = "{{lowerCase entity.codeName}}", key = "'row:'+#p0")
    {{/entity.enableEntityCache}}
    {{entity.codeName}} selectById(Serializable id);

    {{#entity.enableEntityCache}}
    @Cacheable(value = "{{lowerCase entity.codeName}}", key = "'row:'+#p0.{{camelCase entity.keyField.codeName}}")
    {{/entity.enableEntityCache}}
    {{entity.codeName}} selectEntity({{entity.codeName}} entity);

    List<{{entity.codeName}}> selectEntities(@Param("list") List<{{entity.codeName}}> list);

    @Override
    {{#entity.enableEntityCache}}
    @CacheEvict(value = "{{lowerCase entity.codeName}}", key = "'row:'+#p0.{{camelCase entity.keyField.codeName}}")
    {{/entity.enableEntityCache}}
    int insert({{entity.codeName}} entity);

    @Override
    {{#entity.enableEntityCache}}
    @CacheEvict(value = "{{lowerCase entity.codeName}}", key = "'row:'+#p0.{{camelCase entity.keyField.codeName}}")
    {{/entity.enableEntityCache}}
    int updateById(@Param(Constants.ENTITY) {{entity.codeName}} entity);

    @Override
    {{#entity.enableEntityCache}}
    @CacheEvict(value = "{{lowerCase entity.codeName}}", key = "'row:'+#p0.{{camelCase entity.keyField.codeName}}", condition ="#p0 != null")
    {{/entity.enableEntityCache}}
    int update(@Param(Constants.ENTITY) {{entity.codeName}} entity, @Param("ew") Wrapper<{{entity.codeName}}> updateWrapper);

    @Override
    {{#entity.enableEntityCache}}
    @CacheEvict(value = "{{lowerCase entity.codeName}}", key = "'row:'+#p0")
    {{/entity.enableEntityCache}}
    int deleteById(Serializable id);

    /**
    * 自定义查询SQL
    * @param sql
    * @return
    */
    @Select("${sql}")
    List<JSONObject> selectBySQL(@Param("sql") String sql, @Param("et")Map param);

    /**
    * 自定义更新SQL
    * @param sql
    * @return
    */
    @Update("${sql}")
    boolean updateBySQL(@Param("sql") String sql, @Param("et")Map param);

    /**
    * 自定义插入SQL
    * @param sql
    * @return
    */
    @Insert("${sql}")
    boolean insertBySQL(@Param("sql") String sql, @Param("et")Map param);

    /**
    * 自定义删除SQL
    * @param sql
    * @return
    */
    @Delete("${sql}")
    boolean deleteBySQL(@Param("sql") String sql, @Param("et")Map param);

    {{#entity.references}}
    List<{{entity.codeName}}> selectBy{{pascalCase fkField.codeName}}(@Param("{{camelCase fkField.codeName}}") Serializable {{camelCase fkField.codeName}});
    {{/entity.references}}

}