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.ModuleRelation;
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年10月11日
 */
@Repository("moduleRelationRepository")
public class ModuleRelationRepository extends BasicGenericDao<ModuleRelation, Long> {

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

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

    public List<ModuleRelation> queryByModuleIdEnable(Long moduleId, Long categoryId) throws Exception {
        String sql = "select * from ci_module_relation a where (a.source_id = :moduleId or target_id = :moduleId) and category_id=:categoryId and is_deleted = 0"
                + " and exists(select 1 from ci_module m where a.source_id=m.id and m.is_deleted=0 and m.is_disabled=0) and "
                + "exists(select 1 from ci_module m where a.target_id=m.id and m.is_deleted=0 and m.is_disabled=0)";

        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        paramMap.put("categoryId", categoryId);
        List<ModuleRelation> moduleRelations = this.list(ModuleRelation.class, sql, paramMap);
        return moduleRelations;
    }

    public List<ModuleRelation> queryByModuleIdAllStatus(Long moduleId, Long categoryId) throws Exception {
        String sql = "select * from ci_module_relation a where (a.source_id = :moduleId or target_id = :moduleId) and category_id=:categoryId and is_deleted = 0"
                + " and exists(select 1 from ci_module m where a.source_id=m.id and m.is_deleted=0 ) and "
                + "exists(select 1 from ci_module m where a.target_id=m.id and m.is_deleted=0 )";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        paramMap.put("categoryId", categoryId);
        List<ModuleRelation> moduleRelations = this.list(ModuleRelation.class, sql, paramMap);
        return moduleRelations;
    }

    public List<ModuleRelation> queryByModuleIdEnable(Long moduleId) throws Exception {
        String sql = "select * from ci_module_relation a where (a.source_id = :moduleId or target_id = :moduleId)  and is_deleted = 0";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        List<ModuleRelation> moduleRelations = this.list(ModuleRelation.class, sql, paramMap);
        return moduleRelations;
    }

    public ModuleRelation queryBySidAndTid(Long sourceId, Long targetId, Long categoryId) throws Exception {
        String sql = "select * from ci_module_relation a where ((a.target_id = :targetId and a.source_id = :sourceId) or(a.target_id = :sourceId and a.source_id = :targetId)) and category_id=:categoryId and is_deleted = 0";
        Map<String, Object> paramMap = MapTools.simpleMap("targetId", targetId);
        paramMap.put("sourceId", sourceId);
        paramMap.put("categoryId", categoryId);
        List<ModuleRelation> moduleRelations = this.list(ModuleRelation.class, sql, paramMap);
        if (moduleRelations.isEmpty()) {
            return null;
        }
        return moduleRelations.get(0);
    }

    public ModuleRelation queryById(Long id) throws Exception {
        String sql = "select * from ci_module_relation a where a.id = :id and is_deleted = 0";
        Map<String, Object> paramMap = MapTools.simpleMap("id", id);
        List<ModuleRelation> moduleRelations = this.list(ModuleRelation.class, sql, paramMap);
        if (moduleRelations.isEmpty()) {
            return null;
        }
        return moduleRelations.get(0);
    }

    public List<ModuleRelation> queryByRelationId(Long relationId) throws Exception {
        String sql = "select * from ci_module_relation a where a.relation_id = :relationId and is_deleted = 0";
        Map<String, Object> paramMap = MapTools.simpleMap("relationId", relationId);
        List<ModuleRelation> moduleRelations = this.list(ModuleRelation.class, sql, paramMap);
        return moduleRelations;
    }

    public boolean deleteByModuleId(Long moduleId) throws Exception {
        String sql = "update ci_module_relation set is_deleted=1 where (source_id = :moduleId or target_id = :moduleId) and is_deleted = 0";
        Map<String, Object> params = MapTools.simpleMap("moduleId", moduleId);
        return this.execute(sql, params) > 0;
    }

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