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

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.hmc.HmcVirtualFibreChannelMapping;
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;

@Deprecated
@Repository("hmcVirtualFibreChannelMapping")
public class HmcVirtualFibreChannelMappingRepository extends EnhancedBaseRepository<HmcVirtualFibreChannelMapping, Long> {

    public List<HmcVirtualFibreChannelMapping> listByViosId(Long viosId) {
        String sql = "select a.*, b.name as physical_fibre_channel_name, " +
                "c.name as server_vios_name, d.name as client_partition_name " +
                "from hmc_virtual_fibre_channel_mapping a " +
                "left join hmc_physical_fibre_channel b on a.physical_fibre_channel_id = b.id " +
                "left join hmc_partition c on a.cmp_vios_id = c.id " +
                "left join hmc_partition d on a.cmp_partition_id = d.id " +
                "where a.cmp_vios_id = :viosId";
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        List<Object> list = this.queryForList(sql, param, HmcVirtualFibreChannelMapping.class);
        List<HmcVirtualFibreChannelMapping> mappings = new ArrayList<>();
        for (Object obj : list) {
            mappings.add((HmcVirtualFibreChannelMapping) obj);
        }
        return mappings;
    }

    public List<HmcVirtualFibreChannelMapping> listByPartitionId(Long partitionId) {
        String sql = "select a.*, b.name as physical_fibre_channel_name, " +
            "c.name as server_vios_name, d.name as client_partition_name " +
            "from hmc_virtual_fibre_channel_mapping a " +
            "left join hmc_physical_fibre_channel b on a.physical_fibre_channel_id = b.id " +
            "left join hmc_partition c on a.cmp_vios_id = c.id " +
            "left join hmc_partition d on a.cmp_partition_id = d.id " +
            "where a.cmp_partition_id = :partitionId";
        Map<String, Object> param = MapTools.simpleMap("partitionId", partitionId);
        List<Object> list = this.queryForList(sql, param, HmcVirtualFibreChannelMapping.class);
        List<HmcVirtualFibreChannelMapping> mappings = new ArrayList<>();
        for (Object obj : list) {
            mappings.add((HmcVirtualFibreChannelMapping) obj);
        }
        return mappings;
    }

    public List<HmcVirtualFibreChannelMapping> queryMappingByViosId(Long viosId, Long uniqueDeviceId) {
        String sql = "select v.*, f.name physical_fibre_channel_name from hmc_virtual_fibre_channel_mapping v "
                + "left join hmc_physical_fibre_channel f on v.physical_unique_device_id = f.unique_device_id "
                + "where v.cmp_vios_id = :viosId";
        if (null != uniqueDeviceId) {
            sql += " and f.id = " + uniqueDeviceId;
        }
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        List<Object> list = this.queryForList(sql, param, HmcVirtualFibreChannelMapping.class);
        List<HmcVirtualFibreChannelMapping> mappings = new ArrayList<>();
        for (Object obj : list) {
            mappings.add((HmcVirtualFibreChannelMapping) obj);
        }
        return mappings;
    }

    public boolean deleteByViosId(Long viosId) {
        String sql = "delete from hmc_virtual_fibre_channel_mapping where cmp_vios_id  = :viosId";
        Map<String, Object> params = MapTools.simpleMap("viosId", viosId);
        return this.update(sql, params) >= 0;
    }

    public boolean updateByPartitionId(Long partitionId) {
        String sql = "update hmc_virtual_fibre_channel_mapping SET cmp_partition_id = null, client_wwpns = null where cmp_partition_id  = :partitionId";
        Map<String, Object> params = MapTools.simpleMap("partitionId", partitionId);
        return this.update(sql, params) >= 0;
    }

    public int countByFc(Long viosId, String deviceId) {
        String sql = "select count(1) from hmc_virtual_fibre_channel_mapping a where a.cmp_vios_id  = :viosId "
                + "and a.physical_unique_device_id = :deviceId";
        Map<String, Object> params = MapTools.simpleMap("viosId", viosId);
        params.put("deviceId", deviceId);
        return this.countQuery(sql, params).intValue();
    }

    /**
     * 根据vios的id和槽号查询vfc映射
     *
     * @param viosId   viosId
     * @param viosSlot vios槽号
     * @return vfc映射
     */
    public HmcVirtualFibreChannelMapping queryMappingByViosIdAndSlot(long viosId, int viosSlot) {
        String sql = "select a.*, b.name as physical_fibre_channel_name " +
                "from hmc_virtual_fibre_channel_mapping a " +
                "left join hmc_physical_fibre_channel b on a.physical_fibre_channel_id = b.id " +
                "where a.cmp_vios_id  = :viosId and a.server_vios_slot = :viosSlot";
        Map<String, Object> params = MapTools.simpleMap("viosId", viosId);
        params.put("viosSlot", viosSlot);
        List<HmcVirtualFibreChannelMapping> mappingList = list(HmcVirtualFibreChannelMapping.class, sql, params);
        return mappingList == null || mappingList.size() == 0 ? null : mappingList.get(0);
    }

    /**
     * 根据vios在hmc中的id和槽号查询vfc映射
     *
     * @param viosHmcId
     * @param viosSlot  vios槽号
     * @return
     * @throws Exception
     */
    public HmcVirtualFibreChannelMapping queryMappingByViosHmcIdAndSlot(long viosHmcId, int viosSlot) {
        String sql = "select a.*, b.name as physical_fibre_channel_name " +
                "from hmc_virtual_fibre_channel_mapping a " +
                "left join hmc_physical_fibre_channel b on a.physical_fibre_channel_id = b.id " +
                "where a.server_vios_id  = :viosHmcId and a.server_vios_slot = :viosSlot";
        Map<String, Object> params = MapTools.simpleMap("viosHmcId", viosHmcId);
        params.put("viosSlot", viosSlot);
        List<HmcVirtualFibreChannelMapping> mappingList = list(HmcVirtualFibreChannelMapping.class, sql, params);
        return mappingList == null || mappingList.size() == 0 ? null : mappingList.get(0);
    }

    /**
     * 根据逻辑分区的id和槽号查询vfc映射
     *
     * @param partitionId   逻辑分区id
     * @param partitionSlot 逻辑分区槽号
     * @return vfc映射
     */
    public HmcVirtualFibreChannelMapping queryMappingByPartitionIdAndSlot(long partitionId, int partitionSlot) {
        String sql = "select a.*, b.name as physical_fibre_channel_name " +
                "from hmc_virtual_fibre_channel_mapping a " +
                "left join hmc_physical_fibre_channel b on a.physical_fibre_channel_id = b.id " +
                "where a.cmp_partition_id  = :partitionId and a.client_partition_slot = :partitionSlot";
        Map<String, Object> params = MapTools.simpleMap("partitionId", partitionId);
        params.put("partitionSlot", partitionSlot);
        List<HmcVirtualFibreChannelMapping> mappingList = list(HmcVirtualFibreChannelMapping.class, sql, params);
        return mappingList == null || mappingList.size() == 0 ? null : mappingList.get(0);
    }

    /**
     * 根据vios的id和槽号删除vfc映射
     *
     * @param viosId   viosId
     * @param viosSlot vios槽号
     */
    public void deleteMappingByViosIdAndSlot(long viosId, Integer viosSlot) throws BoCloudDBException {
        String sql = "delete from hmc_virtual_fibre_channel_mapping " +
                "where cmp_vios_id  = :viosId and server_vios_slot = :viosSlot";
        Map<String, Object> params = MapTools.simpleMap("viosId", viosId);
        params.put("viosSlot", viosSlot);
        update(sql, params);
    }

    /**
     * 查询行数
     *
     * @param params 查询参数
     * @return 返回结果行数
     */
    public int count(List<Param> params) {
        String sql = "select count(1) from (" +
                " select a.*, b.name as server_vios_name, c.name as client_partition_name, f.name as physical_fibre_channel_name " +
                " from hmc_virtual_fibre_channel_mapping a " +
                " left join hmc_partition b on a.cmp_vios_id = b.id " +
                " left join hmc_partition c on a.cmp_partition_id = c.id " +
                " left join hmc_server d on b.server_id = d.id " +
                " left join cloud_vendor e on d.vendor_id = e.id " +
                " left join hmc_physical_fibre_channel f on a.physical_fibre_channel_id = f.id " +
                " ) temp where 1=1";
        sql = QueryBuilder.build(sql, params, null, "temp");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    /**
     * 分页查询
     *
     * @param page   页码
     * @param rows   页容量
     * @param params 查询参数
     * @param sorter 排序
     * @return 返回查询结果list
     */
    public List<HmcVirtualFibreChannelMapping> list(Integer page, Integer rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select * from (" +
                " select a.*, b.name as server_vios_name, IFNULL(c.name,'分区不存在或已被删除') as client_partition_name, " +
                " f.name as physical_fibre_channel_name, c.status as partition_status " +
                " from hmc_virtual_fibre_channel_mapping a " +
                " left join hmc_partition b on a.cmp_vios_id = b.id " +
                " left join hmc_partition c on a.cmp_partition_id = c.id " +
                " left join hmc_server d on b.server_id = d.id " +
                " left join cloud_vendor e on d.vendor_id = e.id " +
                " left join hmc_physical_fibre_channel f on a.physical_fibre_channel_id = f.id " +
                " ) temp where 1=1";
        sql = QueryBuilder.build(sql, params, null, "temp");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return list(HmcVirtualFibreChannelMapping.class, sql, paramMap);
    }

    /**
     * 按viosId和partitionId查询所有vfc映射
     *
     * @param viosId      viosId
     * @param partitionId partitionId
     * @return 查询结果集
     */
    public List<HmcVirtualFibreChannelMapping> listByViosAndPartition(long viosId, long partitionId) {
        String sql = "select a.*, b.name as physical_fibre_channel_name, " +
                "c.name as server_vios_name, d.name as client_partition_name " +
                "from hmc_virtual_fibre_channel_mapping a " +
                "left join hmc_physical_fibre_channel b on a.physical_fibre_channel_id = b.id " +
                "left join hmc_partition c on a.cmp_vios_id = c.id " +
                "left join hmc_partition d on a.cmp_partition_id = d.id " +
                "where a.cmp_vios_id = :viosId and a.cmp_partition_id = :partitionId";
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        param.put("partitionId", partitionId);
        List<Object> list = this.queryForList(sql, param, HmcVirtualFibreChannelMapping.class);
        List<HmcVirtualFibreChannelMapping> mappings = new ArrayList<>();
        for (Object obj : list) {
            mappings.add((HmcVirtualFibreChannelMapping) obj);
        }
        return mappings;
    }

    @Override
    public boolean delete(HmcVirtualFibreChannelMapping entity) {
        String sql = "delete from hmc_virtual_fibre_channel_mapping where id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", entity.getId());
        return this.update(sql, params) > 0;
    }

    @Override
    public HmcVirtualFibreChannelMapping get(Class<HmcVirtualFibreChannelMapping> clazz, Long id) {
        String sql = "select * from hmc_virtual_fibre_channel_mapping where id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<Object> objects = this.queryForList(sql, params, HmcVirtualFibreChannelMapping.class);
        return objects == null || objects.size() == 0 ? null : (HmcVirtualFibreChannelMapping) objects.get(0);
    }

}
