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.InstanceValue;
import com.google.common.collect.Maps;
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;

/**
 * @author liuyy
 */
@Repository("instanceValueRepository")
public class InstanceValueRepository extends BasicGenericDao<InstanceValue, Long> {

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

    public List<InstanceValue> getByInstanceId(Long instanceId) throws Exception {
        String sql = "select a.* from ci_instance_value a where a.instance_id = :instanceId";
        Map<String, Object> paramMap = MapTools.simpleMap("instanceId", instanceId);
        List<InstanceValue> list = this.list(InstanceValue.class, sql, paramMap);
        return list;
    }

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

    public List<InstanceValue> queryPropertysAndValue(Long moduleId) throws Exception {
        String sql = "select v.id,v.instance_id,v.property_value,p.code as property_code,p.id as property_id  from ci_instance_value  v,ci_property p ,ci_instance ins where p.id=v.property_id  and v.instance_id=ins.id and ins.is_deleted=0 and  ins.module_id=:moduleId and  p.module_id=:moduleId ";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        List<InstanceValue> list = this.list(InstanceValue.class, sql, paramMap);
        return list;
    }

    public boolean deleteByInstanceId(Long instanceId) throws Exception {
        String sql = "delete from ci_instance_value where instance_id = :instanceId";
        Map<String, Object> params = MapTools.simpleMap("instanceId", instanceId);
        return this.execute(sql, params) > 0;
    }

    public InstanceValue queryTitleByInstanceId(Long instanceId) throws Exception {
        String sql = "SELECT v.* FROM ci_instance_value v,ci_instance i,ci_property p WHERE p.is_title =1 and v.property_id=p.id and v.instance_id=i.id AND i.id =:instanceId AND v.property_value IS NOT NULL";
        Map<String, Object> params = MapTools.simpleMap("instanceId", instanceId);
        List<InstanceValue> list = this.list(InstanceValue.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public Map<Long, InstanceValue> queryTitleByInstanceIds(List<Long> instanceIds) throws Exception {
        String sql = "SELECT v.* FROM ci_instance_value v,ci_instance i,ci_property p WHERE p.is_title =1 and v.property_id=p.id and v.instance_id=i.id AND i.id IN (:instanceId) AND v.property_value IS NOT NULL";
        Map<String, Object> params = MapTools.simpleMap("instanceId", instanceIds);
        List<InstanceValue> list = this.list(InstanceValue.class, sql, params);
        Map<Long, InstanceValue> maps = Maps.newHashMap();
        for (InstanceValue instancevalue : list) {
            maps.put(instancevalue.getInstanceId(), instancevalue);
        }
        return maps;
    }

    public Map<Long, InstanceValue> queryTitle4InstanceAll() throws Exception {
        String sql = "select val.* from ci_instance_value val,ci_instance ins,ci_property p where ins.is_deleted=0 and  val.instance_id=ins.id and ins.module_id=p.module_id and val.property_id=p.id and p.is_title=1 and val.property_value is not null ";
        List<InstanceValue> list = this.list(InstanceValue.class, sql, null);
        Map<Long, InstanceValue> map = Maps.newHashMap();
        if (list.isEmpty()) {
            return map;
        }
        for (InstanceValue instanceValue : list) {
            map.put(instanceValue.getInstanceId(), instanceValue);
        }
        return map;
    }

    public InstanceValue queryByInstanceIdAndPropertyCode(Long instanceId, String propertyCode) throws Exception {
        String sql = "select * from ci_instance_value where instance_id = :instanceId and property_code = :propertyCode ";
        Map<String, Object> params = MapTools.simpleMap("instanceId", instanceId);
        params.put("propertyCode", propertyCode);
        List<InstanceValue> list = this.list(InstanceValue.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }
}
