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.Property;
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;

/**
 * CiProperty Dao层实现类
 *
 * @author lyy
 * @Version 1.0
 * @since 2017年9月13日
 */
@Repository("propertyRepository")
public class PropertyRepository extends BasicGenericDao<Property, Long> {

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

    public List<Property> queryPropertysAndValue(Long instanceId, Long moduleId) throws Exception {
        String sql = "select p.*,v.property_value from ci_property p LEFT JOIN ci_instance_value v on p.id=v.property_id and v.instance_id=:instanceId  where module_id=:moduleId order by order_num";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        paramMap.put("instanceId", instanceId);
        List<Property> list = this.list(Property.class, sql, paramMap);
        return list;
    }

    public Property query(Long id) throws Exception {
        String sql = "select * from ci_property a where a.id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<Property> property = this.list(Property.class, sql, params);
        if (property.isEmpty()) {
            return null;
        }
        return property.get(0);
    }

    public List<Property> queryByModuleId(Long moduleId) throws Exception {
        String sql = "select a.* from ci_property a left join ci_property_group g on a.module_id=g.module_id and a.group_name=g.name where a.module_id = :moduleId order by g.order_num,a.order_num";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        List<Property> list = this.list(Property.class, sql, paramMap);
        return list;
    }

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

    public List<Property> queryByPropertyStatusId(Long moduleId, Long userId) throws Exception {
        String sql = "select p.id,p.type,p.unit,p.name,lower(p.code) code,IF(IFNULL(c.user_id ,0)>0,'true','false') checked from ci_property p LEFT JOIN ci_columns_config  c  on p.id=c.property_id and c.user_id=:userId where p.module_id=:moduleId and p.code<>'name' and p.type<>'TABLE' order by p.id";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        paramMap.put("userId", userId);
        List<Property> list = this.list(Property.class, sql, paramMap);
        return list;
    }

    public Property queryByCode(String code, Long moduleId) throws Exception {
        String sql = "select * from ci_property a where a.code = :code and a.module_id=:moduleId";
        Map<String, Object> simpleMap = MapTools.simpleMap("code", code);
        simpleMap.put("moduleId", moduleId);
        List<Property> propertys = this.list(Property.class, sql, simpleMap);
        if (propertys.isEmpty()) {
            return null;
        }
        return propertys.get(0);
    }

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

    public List<Property> queryByQuoteForDiscovery(Long moduleId) throws Exception {
        String sql = "select * from ci_property a where  a.module_id=:moduleId and type='QUOTE' and a.is_auto_discovery=1";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        List<Property> list = this.list(Property.class, sql, paramMap);
        return list;
    }

    public List<Property> queryQuoteByModuleId(Long moduleId) throws Exception {
        String sql = "select p.* from ci_property p,ci_module m where p.module_id=m.id and m.is_deleted=0 and m.is_disabled=0 and p.link_module_id=:moduleId";
        Map<String, Object> paramMap = MapTools.simpleMap("moduleId", moduleId);
        List<Property> list = this.list(Property.class, sql, paramMap);
        return list;
    }

    public Property queryForTitle(Long moduleId) throws Exception {
        String sql = "select * from ci_property a where a.is_title = 1 and a.module_id=:moduleId";
        Map<String, Object> simpleMap = MapTools.simpleMap("moduleId", moduleId);
        List<Property> propertys = this.list(Property.class, sql, simpleMap);
        if (propertys.isEmpty()) {
            return null;
        }
        return propertys.get(0);
    }

}
