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.PropertyGroup;
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;

/**
 * iPropertyGroup dao层实现类
 *
 * @author lyy
 * @Version 1.0
 * @since 2017年9月13日
 */
@Repository("propertyGroupRepository")
public class PropertyGroupRepository extends BasicGenericDao<PropertyGroup, Long> {

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

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

    public int countSameCode(Long id) throws Exception {
        String sql = "select count(1) from ci_property_group where 1=1 and id=:id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        return this.countQuery(sql, params).intValue();
    }

    public List<PropertyGroup> queryByModuleId(Long moduleId) throws Exception {
        String sql = "select a.* from ci_property_group a where a.module_id = :moduleId order by a.order_num";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        List<PropertyGroup> list = this.list(PropertyGroup.class, sql, paramMap);
        return list;
    }

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

}
