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.InstanceGrid;
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 tangcq
 * @since 2018-02-01
 */
@Repository("instanceGridRepository")
public class InstanceGridRepository extends BasicGenericDao<InstanceGrid, Long> {

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

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

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

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

    public List<InstanceGrid> queryByPropertyCode(Long instanceId, String propertyCode) throws Exception {
        String sql = "select a.* from ci_instance_grid a where a.instance_id = :instanceId and a.property_code=:propertyCode";
        Map<String, Object> paramMap = MapTools.simpleMap("instanceId", instanceId);
        paramMap.put("propertyCode", propertyCode);
        List<InstanceGrid> list = this.list(InstanceGrid.class, sql, paramMap);
        return list;
    }

    public List<InstanceGrid> queryPropertysAndGridByModuleId(Long moduleId) throws Exception {
        String sql = "select g.id,g.instance_id,g.property_code,p.id as property_id,g.desc1,g.desc2,g.desc3,g.desc4,g.desc5,g.desc6,g.desc7,g.desc8,g.desc9,g.desc10,g.desc11,g.desc12,g.desc13,g.desc14,g.desc15,g.desc16,g.desc17,g.desc18,g.desc19,g.desc20,g.desc21,g.desc22,g.desc23,g.desc24,g.desc25,g.desc26,g.desc27,g.desc28,g.desc29,g.desc30 from (select g.*,ins.module_id from ci_instance_grid g ,ci_instance ins where g.instance_id=ins.id and ins.module_id=:moduleId) g  LEFT JOIN   ci_property p on p.code=g.property_code and g.module_id=p.module_id  where g.module_id=:moduleId ";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        List<InstanceGrid> list = this.list(InstanceGrid.class, sql, paramMap);
        return list;
    }
}
