package com.bocloud.ims.repository.topology;

import com.alibaba.fastjson.JSONObject;
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.ListTool;
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.Module;
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月13日
 */
@Repository("moduleRepository")
public class ModuleRepository extends BasicGenericDao<Module, Long> {

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

    public List<Module> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select * from ci_module where 1=1 and is_deleted=0 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<Module> list = this.list(Module.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_module a where 1=1 and is_deleted=0 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<Module> list = this.list(Module.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (Module module : list) {
            beans.add(new SimpleBean(module.getId(), module.getName()));
        }
        return beans;
    }

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

    public Module queryByLinkId(Long id) throws Exception {
        String sql = "select * from ci_module a where  is_deleted=0 and is_disabled=0 and exists(select 1 from ci_property b where a.id=b.module_id and b.link_module_id=:id) ";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<Module> ciModule = this.list(Module.class, sql, params);
        if (ciModule.isEmpty()) {
            return null;
        }
        return ciModule.get(0);
    }

    public boolean delete(Long id) throws Exception {
        String sql = "update  ci_module set is_deleted=1 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_module where 1=1 and is_deleted=0 ";
        sql = QueryBuilder.buildRaw(sql, params, null, "");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }

    public int countSameCode(String name, String code) throws Exception {
        String sql = "select count(1) from ci_module where 1=1 and is_deleted=0 ";
        if (null != code) {
            sql += " and code = :code ";
        } else if (null != name) {
            sql += " and name = :name ";
        }
        Map<String, Object> params = MapTools.simpleMap("code", code);
        params.put("name", name);
        return this.countQuery(sql, params).intValue();
    }

    public List<Module> list(Long categoryId, List<Long> moduleId) throws Exception {
        String sql = "select a.* from ci_module a";
        Map<String, Object> paramMap = null;
        if (ListTool.isEmpty(moduleId)) {
            sql += " where a.category_id = :categoryId and is_deleted=0";
            paramMap = MapTools.simpleMap("categoryId", categoryId);
        } else {
            sql += " where a.category_id = :categoryId and is_deleted=0 and a.id not in(:moduleId)";
            paramMap = MapTools.simpleMap("categoryId", categoryId);
            paramMap.put("moduleId", moduleId);
        }
        List<Module> list = this.list(Module.class, sql, paramMap);
        return list;
    }

    public List<Module> list() throws Exception {
        String sql = "select * from ci_module where 1=1 and is_deleted=0 ";
        List<Module> list = this.list(Module.class, sql, null);
        return list;
    }

    public List<Module> listEnabled(Long categoryId) throws Exception {
        String sql = "select a.* from ci_module a where a.category_id = :categoryId and is_deleted=0 and is_disabled=0";
        Map<String, Object> paramMap = MapTools.simpleMap("categoryId", categoryId);
        List<Module> list = this.list(Module.class, sql, paramMap);
        return list;
    }

    public List<Module> listEnabled() throws Exception {
        String sql = "select * from ci_module where 1=1 and is_deleted=0 and is_disabled=0";
        List<Module> list = this.list(Module.class, sql, null);
        return list;
    }

    public Module queryByCode(String code) throws Exception {
        String sql = "select * from ci_module a where a.code = :code";
        Map<String, Object> paramMap = MapTools.simpleMap("code", code);
        List<Module> ciModule = this.list(Module.class, sql, paramMap);
        if (ciModule.isEmpty()) {
            return null;
        }
        return ciModule.get(0);
    }

    public Map<Long, Module> queryAll4Cache() throws Exception {
        String sql = "select * from ci_module where 1=1 and is_deleted=0 and is_disabled=0";
        List<Module> list = this.list(Module.class, sql, null);
        Map<Long, Module> result = Maps.newHashMap();
        for (Object object : list) {
            result.put(((Module) object).getId(), (Module) object);
        }
        return result;
    }

    public Module queryProperties(JSONObject param) throws Exception {
        String sql = "select * from ci_module a where a.is_deleted=0 ";
        Long id = param.getLong("id");
        String name = param.getString("name");
        String code = param.getString("code");
        Map<String, Object> params = null;
        if (null != id && null == name && null == code) {
            sql += " and a.id = :id ";
            params = MapTools.simpleMap(Common.ID, id);
        } else if (null != name && null == id && null == code) {
            sql += " and a.name = :name ";
            params = MapTools.simpleMap(Common.NAME, name);
        } else if (null != code && null == id && null == name) {
            sql += " and a.code = :code ";
            params = MapTools.simpleMap(Common.CODE, code);
        }
        List<Module> ciModule = this.list(Module.class, sql, params);
        if (ciModule.isEmpty()) {
            return null;
        }
        return ciModule.get(0);
    }

}
