package com.bocloud.ims.repository.topology;

import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.topology.PropertyItem;
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;

/**
 * PropertyItem Dao层实体类
 *
 * @author lyy
 * @Version 1.0
 * @since 2017年9月19日
 */
@Repository("propertyItemRepository")
public class PropertyItemRepository extends BasicGenericDao<PropertyItem, Long> {

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

    public List<PropertyItem> queryByPropertyId(Long propertyId) throws Exception {
        String sql = "select a.* from ci_property_item a where a.property_id = :propertyId";
        Map<String, Object> paramMap = MapTools.simpleMap("propertyId", propertyId);
        List<PropertyItem> list = this.list(PropertyItem.class, sql, paramMap);
        return list;
    }

    public PropertyItem queryPropertyItemByName(Long propertyId, String name) throws Exception {
        String sql = "select a.* from ci_property_item a where a.property_id = :propertyId and a.name=:name";
        Map<String, Object> paramMap = MapTools.simpleMap("propertyId", propertyId);
        paramMap.put("name", name);
        List<PropertyItem> list = this.list(PropertyItem.class, sql, paramMap);
        if (ListTool.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

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

    public PropertyItem queryPropertyItemByValue(Long propertyId, String value) throws Exception {
        String sql = "select a.* from ci_property_item a where a.property_id = :propertyId and a.value=:value";
        Map<String, Object> paramMap = MapTools.simpleMap("propertyId", propertyId);
        paramMap.put("value", value);
        List<PropertyItem> list = this.list(PropertyItem.class, sql, paramMap);
        if (ListTool.isEmpty(list)) {
            return null;
        }
        return list.get(0);

    }

    public Boolean validateIteam(Long moduleId, Long propertyId, String value) throws Exception {
        String sql = "select count(1) from ci_property_item it where it.module_id=:moduleId and it.property_id=:propertyId and it.value=:value";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        paramMap.put("propertyId", propertyId);
        paramMap.put("value", value);
        return this.countQuery(sql, paramMap).intValue() > 0;
    }

    public Boolean deleteByModuleId(Long moduleId) throws Exception {
        String sql = "delete from ci_property_item where module_id = :moduleId";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        return this.execute(sql, paramMap) > 0;
    }

    public Boolean deleteByPropertyId(Long propertyId) throws Exception {
        String sql = "delete from ci_property_item where property_id = :propertyId";
        Map<String, Object> paramMap = MapTools.simpleMap("propertyId", propertyId);
        return this.execute(sql, paramMap) > 0;
    }

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

    public Integer queryMaxDisplaytocol(Long moduleId, Long propertyId) throws Exception {
        String sql = "select max(SUBSTR(a.value,5)) as max from ci_property_item a where a.module_id=:moduleId and a.property_id=:propertyId";
        Map<String, Object> paramMap = MapTools.simpleMap("propertyId", propertyId);
        paramMap.put("moduleId", moduleId);
        List<Map<String, Object>> list = this.list(sql, paramMap);
        if (ListTool.isEmpty(list) || list.get(0).get("max") == null || "".equals(list.get(0).get("max").toString())) {
            return null;
        }
        return Integer.parseInt(list.get(0).get("max").toString());
    }

}
