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

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.BasicDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.fusionsphere.FusionSphereOsVersion;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: FusionSphereOsVersionRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/11/3 13:39
 */
@Repository
public class FusionSphereOsVersionRepository extends BasicDao {

    public List<FusionSphereOsVersion> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "select a.*  "
                + " from fusion_sphere_os_version a "
                + " where  1=1  ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.queryForList(sql, paramMap, FusionSphereOsVersion.class).stream().map(o -> (FusionSphereOsVersion) o).collect(Collectors.toList());
    }

    public int count(List<Param> params) throws BoCloudDBException {
        String sql = "SELECT COUNT(1) "
                + " from fusion_sphere_os_version a "
                + " WHERE 1=1 ";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    public FusionSphereOsVersion query(Long id) throws BoCloudDBException {
        String sql = "select a.* "
                + " from fusion_sphere_os_version a "
                + " where  a.id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<FusionSphereOsVersion> list = this.queryForList(sql, params, FusionSphereOsVersion.class).stream().map(o -> (FusionSphereOsVersion) o).collect(Collectors.toList());
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public FusionSphereOsVersion save(FusionSphereOsVersion ip) throws BoCloudDBException {
        return (FusionSphereOsVersion) this.saveEntity(ip);
    }

    public Boolean update(FusionSphereOsVersion ip) throws BoCloudDBException {
        return this.updateEntity(ip);
    }

    public Boolean delete(Long id) throws BoCloudDBException {
        String sql = "delete from fusion_sphere_os_version where id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.update(sql, params) > 0;
    }

    public Boolean deleteByVdcId(Long id) throws BoCloudDBException {
        String sql = "delete from fusion_sphere_os_version where vdc_id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        return this.update(sql, params) > 0;
    }

    public List<FusionSphereOsVersion> listByVdcId(Long id) throws BoCloudDBException {
        String sql = "select * from fusion_sphere_os_version where vdc_id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        return this.queryForList(sql, params, FusionSphereOsVersion.class).stream().map(o -> (FusionSphereOsVersion) o).collect(Collectors.toList());
    }

    public Boolean deleteByVendorId(Long id) throws BoCloudDBException {
        String sql = "delete from fusion_sphere_os_version where vendor_id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        return this.update(sql, params) > 0;
    }

    public List<FusionSphereOsVersion> listByVendorId(Long id) throws BoCloudDBException {
        String sql = "select * from fusion_sphere_os_version where vendor_id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        return this.queryForList(sql, params, FusionSphereOsVersion.class).stream().map(o -> (FusionSphereOsVersion) o).collect(Collectors.toList());
    }


}