package com.bocloud.ims.repository.resource.hmc;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.hmc.HmcIoAdapter;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * HMC输入输出适配器数据访问对象
 *
 * @author sxp
 * @since May 22, 2018
 */
@Deprecated
@Repository("hmcIoAdapterRepository")
public class HmcIoAdapterRepository extends EnhancedBaseRepository<HmcIoAdapter, Long> {

    public List<HmcIoAdapter> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        StringBuilder sb = new StringBuilder();
        sb.append("select a.*, b.io_bus_id io_bus_id  ");
        sb.append("  from hmc_io_adapter a ");
        sb.append(" inner join hmc_io_bus b on b.id = a.hmc_io_bus_id  ");
        sb.append(" where 1 = 1 ");
        String sql = sb.toString();

        sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<HmcIoAdapter> entities = this.list(HmcIoAdapter.class, sql, paramMap);

        return entities;
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) {
        StringBuilder sb = new StringBuilder();
        sb.append("select a.id, a.device_name  ");
        sb.append("  from hmc_io_adapter a  ");
        sb.append(" inner join hmc_io_bus b on b.id = a.hmc_io_bus_id  ");
        sb.append(" where 1 = 1 ");
        String sql = sb.toString();

        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<HmcIoAdapter> entities = this.list(HmcIoAdapter.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<>();

        for (HmcIoAdapter entity : entities) {
            beans.add(new SimpleBean(entity.getId(), entity.getDeviceName()));
        }
        return beans;
    }

    public int count(List<Param> params) {
        StringBuilder sb = new StringBuilder();
        sb.append("select count('x')        ");
        sb.append("  from hmc_io_adapter a  ");
        sb.append(" inner join hmc_io_bus b on b.id = a.hmc_io_bus_id  ");
        sb.append(" where 1 = 1  ");
        String sql = sb.toString();
        sql = QueryBuilder.build(sql, params, null, "a");

        Map<String, Object> paramMap = QueryBuilder.getParam(params);

        return this.countQuery(sql, paramMap).intValue();
    }

    public void deleteByHmcServerId(Long hmcServerId) {
        String sql = "delete from hmc_io_adapter where hmc_server_id = :hmcServerId";
        Map<String, Object> params = MapTools.simpleMap("hmcServerId", hmcServerId);
        this.update(sql, params);
    }

}
