package com.bocloud.ims.repository.topology;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.topology.CiIcon;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 图表dao层实现类
 *
 * @author lyy
 * @Version 1.0
 * @since 2017年9月28日
 */
@Repository("iconRepository")
public class CiIconRepository extends BasicGenericDao<CiIcon, Long> {

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

    public List<CiIcon> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select * from ci_icon where 1=1 order by id desc";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<CiIcon> list = this.list(CiIcon.class, sql, paramMap);
        return list;
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.id,a.name from ci_icon a where 1=1 order by id desc";
        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<CiIcon> list = this.list(CiIcon.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (CiIcon icon : list) {
            beans.add(new SimpleBean(icon.getId(), icon.getName()));
        }
        return beans;
    }

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

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

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from ci_icon where 1=1";
        sql = QueryBuilder.buildRaw(sql, params, null, "");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }

    public int countUsed(Long iconId) throws Exception {
        String sql = "select count(1) from ci_module where icon_id=:iconId";
        Map<String, Object> params = MapTools.simpleMap("iconId", iconId);
        return this.countQuery(sql, params).intValue();

    }

    public CiIcon queryByModuleId(Long moduleId) throws Exception {
        String sql = "select a.* from ci_icon a,ci_module m where m.icon_id=a.id and m.id=:moduleId and m.is_deleted=0 ";
        Map<String, Object> params = MapTools.simpleMap("moduleId", moduleId);
        List<CiIcon> icons = this.list(CiIcon.class, sql, params);
        if (icons.isEmpty()) {
            return null;
        }
        return icons.get(0);
    }

    public Map<Long, CiIcon> query4ModuleAll() throws Exception {
        String sql = "select a.*,m.module_id from ci_icon a,ci_module m where m.icon_id=a.id and m.is_deleted=0 and m.is_disabled=0";
        List<CiIcon> icons = this.list(CiIcon.class, sql, null);
        Map<Long, CiIcon> result = Maps.newHashMap();
        for (CiIcon object : icons) {
            result.put(object.getModuleId(), object);
        }
        return result;
    }

}
