package com.bocloud.ims.repository.topology;

import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.topology.SynMapping;
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("synMappingRepository")
public class SynMappingRepository extends BasicGenericDao<SynMapping, Long> {

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

    public List<SynMapping> query(String serviceName, String moduleCode) throws Exception {
        String sql = "select p.code as property_code,ifnull(m.target_code,p.code) as target_code from (select mo.code as module_code,pro.* from ci_property pro,ci_module mo where pro.module_id=mo.id and mo.code=:moduleCode ) p LEFT JOIN (select * from ci_syn_mapping a where a.service_name=:serviceName and a.module_code=:moduleCode )m on p.code=m.property_code and p.module_code=m.module_code ";
        Map<String, Object> params = MapTools.simpleMap("serviceName", serviceName);
        params.put("moduleCode", moduleCode);
        List<SynMapping> list = this.list(SynMapping.class, sql, params);
        return list;
    }

}
