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.Relation;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * 关系类型dao层实现类
 *
 * @author lyy
 * @Version 1.0
 * @since 2017年9月18日
 */
@Repository("relationRepository")
public class RelationRepository extends BasicGenericDao<Relation, Long> {

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

    public List<Relation> list(Long categoryId) throws Exception {
        String sql = "select a.* from ci_relation a where a.category_id=:categoryId";
        Map<String, Object> params = MapTools.simpleMap("categoryId", categoryId);
        List<Relation> list = this.list(Relation.class, sql, params);
        return list;
    }

    public int countName(Long categoryId, String name) throws Exception {
        String sql = "select count(1) from ci_relation a where a.category_id=:categoryId and name=:name ";
        Map<String, Object> params = MapTools.simpleMap("categoryId", categoryId);
        params.put("name", name);
        return this.countQuery(sql, params).intValue();
    }

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

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

}
