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

import com.bocloud.cmp.params.fusionsphere.StorageDeviceParams;
import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.resource.fusionsphere.FusionSphereStorageDevice;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: FusionSphereStorageDeviceRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/10/21 9:58
 */
@Repository
public class FusionSphereStorageDeviceRepository extends BasicGenericDao<FusionSphereStorageDevice, Long> {

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

    public List<FusionSphereStorageDevice> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "select a.*,v.name vendor_name,v.type vendor_type,t.name tenant_name, p.name project_name "
                + " from fusion_sphere_storage_device a "
                + " left join tenant t on a.tenant_id = t.id "
                + " left join project p on a.project_id = p.id "
                + " left join cloud_vendor v on a.vendor_id = v.id "
                + " where a.is_deleted = 0 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(FusionSphereStorageDevice.class, sql);
    }

    public int count(List<Param> params) throws BoCloudDBException {
        String sql = "select count(1) "
                + " from fusion_sphere_storage_device a "
                + " left join tenant t on a.tenant_id = t.id "
                + " left join project p on a.project_id = p.id "
                + " left join cloud_vendor v on a.vendor_id = v.id "
                + " where a.is_deleted = 0 ";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }


    public FusionSphereStorageDevice query(long id) throws BoCloudDBException {
        String sql = "select a.*,v.name vendor_name,v.type vendor_type,t.name tenant_name, p.name project_name "
                + " from fusion_sphere_storage_device a "
                + " left join tenant t on a.tenant_id = t.id "
                + " left join project p on a.project_id = p.id "
                + " left join cloud_vendor v on a.vendor_id = v.id "
                + " where a.is_deleted = 0 and a.id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<FusionSphereStorageDevice> list = this.list(FusionSphereStorageDevice.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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

    public List<FusionSphereStorageDevice> listByVendor(Long vendorId) {
        String sql = "select a.* from fusion_sphere_storage_device  a where a.is_deleted = 0 and a.vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.list(FusionSphereStorageDevice.class, sql, params);
    }

    public boolean remove(Long id, Long userId) {
        String sql = "update fusion_sphere_storage_device 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 List<FusionSphereStorageDevice> listStorage(StorageDeviceParams storageDeviceParams) {
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, storageDeviceParams.getVendorId());
        params.put("vdcId", storageDeviceParams.getVdcId());
        String sql = "select c.* FROM ";
        if (null != storageDeviceParams.getHostId()) {
            sql += "(select * from fusion_sphere_storage_device a WHERE a.vendor_id = :vendorId and a.vdc_id = :vdcId and a.type = 'LOCAL' and a.id in ( " +
                    "select b.device_id from fusion_sphere_device_host b where b.host_id = :hostId " +
                    ")) c ";
            params.put("hostId", storageDeviceParams.getHostId());
        } else {
            sql += "fusion_sphere_storage_device c ";
        }
        sql += "where c.vendor_id = :vendorId and c.vdc_id = :vdcId and c.is_deleted=0 and c.type = 'LOCAL' and c.id not in ( " +
                "select b.storage_device_id from data_store b WHERE b.TYPE = 'LOCALPOME' AND b.vendor_id = :vendorId and b.vdc_id = :vdcId and b.is_deleted = 0) ";


        return this.list(FusionSphereStorageDevice.class, sql, params);
    }
}