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.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.hmc.HmcVirtualSwitch;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

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

/**
 * @author ShaolongTan
 */
@Deprecated
@Component("hmcVirtualSwitch")
public class HmcVirtualSwitchRepository extends JdbcGenericDao<HmcVirtualSwitch, Long> {

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

    public List<HmcVirtualSwitch> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long vendorId) {
        String sql = "select a.*, b.name create_user_name, h.name server_name from hmc_virtual_switch a "
                + "left join hmc_server h on a.server_id = h.id left join user b on a.creator_id = b.id "
                + "where a.is_deleted = 0";
        if (null != vendorId) {
            sql += " and h.vendor_id = " + vendorId;
        }
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.list(HmcVirtualSwitch.class, sql, paramMap);
    }

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) {
        String sql = "select a.id,a.name,a.switch_id from hmc_virtual_switch a "
                + "left join hmc_server h on a.server_id = h.id " + "where a.is_deleted = 0";
        sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<HmcVirtualSwitch> pars = this.list(HmcVirtualSwitch.class, sql, paramMap);
        List<SimpleBean> beans = new ArrayList<SimpleBean>();
        for (HmcVirtualSwitch par : pars) {
            SimpleBean bean = new SimpleBean(par.getId(), par.getName(), par.getSwitchId().toString());
            bean.setUuid(par.getUuid());
            beans.add(bean);
        }
        return beans;
    }

    public int count(List<Param> params, Long vendorId) {
        String sql = "select count(1) from hmc_virtual_switch a left join hmc_server h on a.server_id = h.id "
                + "where a.is_deleted = 0";
        if (null != vendorId) {
            sql += " and h.vendor_id = " + vendorId;
        }
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    public boolean removeByServerId(Long serverId, Long userId) {
        String sql = "update hmc_virtual_switch set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and server_id = :serverId";
        Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public List<HmcVirtualSwitch> listByServerId(Long serverId) {
        String sql = "select a.* from hmc_virtual_switch a where a.is_deleted = 0 and a.server_id = :serverId";
        Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
        return this.list(HmcVirtualSwitch.class, sql, params);
    }

    public boolean remove(Long id, Long userId) {
        String sql = "update hmc_virtual_switch set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public boolean removeByUuid(String uuid, Long userId) {
        String sql = "update hmc_virtual_switch set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and uuid = :uuid";
        Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
        params.put("menderId", userId);
        params.put("gmtModify", new Date());
        return this.execute(sql, params) > 0;
    }

    public HmcVirtualSwitch getById(Long id) {
        String sql = "select a.* from hmc_virtual_switch a where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<HmcVirtualSwitch> profiles = this.list(HmcVirtualSwitch.class, sql, params);
        if (profiles.isEmpty()) {
            return null;
        }
        return profiles.get(0);
    }

    public HmcVirtualSwitch getBySwitchId(Integer switchId, Long serverId) {
        String sql = "select a.* from hmc_virtual_switch a where a.is_deleted = 0 and a.switch_id = :switchId and a.server_id = :serverId";
        Map<String, Object> params = MapTools.simpleMap("switchId", switchId);
        params.put("serverId", serverId);
        List<HmcVirtualSwitch> profiles = this.list(HmcVirtualSwitch.class, sql, params);
        if (profiles.isEmpty()) {
            return null;
        }
        return profiles.get(0);
    }

    public HmcVirtualSwitch getByName(String name, Long serverId) {
        String sql = "select a.* from hmc_virtual_switch a where a.is_deleted = 0 and a.name = :name and a.server_id = :serverId";
        Map<String, Object> paramMap = MapTools.simpleMap("name", name);
        paramMap.put("serverId", serverId);
        List<HmcVirtualSwitch> list = this.list(HmcVirtualSwitch.class, sql, paramMap);
        if (ListTool.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }
}
