package com.bocloud.ims.repository.topology;

import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.topology.RelationCategory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 关系类型dao层实现类
 *
 * @author tangcq
 * @Version 1.0
 * @since 2017年12月5日
 */
@Repository("relationCategoryRepository")
public class RelationCategoryRepository extends BasicGenericDao<RelationCategory, Long> {

    public RelationCategoryRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public List<RelationCategory> list() throws Exception {
        String sql = "select a.* from ci_relation_category a";
        List<RelationCategory> list = this.list(RelationCategory.class, sql, new HashMap<String, Object>(10));
        return list;
    }

    public RelationCategory query(Long id) throws Exception {
        String sql = "select * from ci_relation_category where id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<RelationCategory> relationCategorys = this.list(RelationCategory.class, sql, params);
        if (relationCategorys.isEmpty()) {
            return null;
        }
        return relationCategorys.get(0);
    }

    public Boolean remove(Long id) throws Exception {
        String sql = "delete from ci_relation_category where id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.execute(sql, params) > 0;
    }

    public RelationCategory queryByName(String name) throws Exception {
        String sql = "select * from ci_relation_category where name = :name";
        Map<String, Object> params = MapTools.simpleMap(Common.NAME, name);
        List<RelationCategory> relationCategorys = this.list(RelationCategory.class, sql, params);
        if (relationCategorys.isEmpty()) {
            return null;
        }
        return relationCategorys.get(0);
    }

}
