package com.bocloud.ims.repository.resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.SnapshotTask;

/**
 * 快照任务数据访问实现类
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("snapshotTaskRepository")
public class SnapshotTaskRepository extends BasicGenericDao<SnapshotTask, Long> {

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

    /**
     * 删除快照任务
     *
     * @param id
     * @return
     * @throws Exception
     */

    public boolean remove(Long id) throws Exception {
        String sql = "delete from snapshot_task where id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        return this.execute(sql, params) > 0;
    }

    /**
     * 查询快照任务详情
     *
     * @param id
     * @return
     * @throws Exception
     */

    public SnapshotTask query(Long id) throws Exception {
        String sql = "select * from snapshot_task where id = :id";
        Map<String, Object> paramMap = MapTools.simpleMap("id", id);
        List<SnapshotTask> tasks = this.list(SnapshotTask.class, sql, paramMap);
        if (ListTool.isEmpty(tasks)) {
            return null;
        }
        return (SnapshotTask)tasks.get(0);
    }

    /**
     * 根据云平台ID查询
     *
     * @param vendorId
     * @return
     * @throws Exception
     */

    public List<SnapshotTask> listByVendorId(Long vendorId) throws Exception {
        String sql = "select * from snapshot_task where 1 = 1";
        Map<String, Object> paramMap = new HashMap<>(8);
        if (null != vendorId && 0L != vendorId) {
            sql += " and vendor_id = :vendorId";
            paramMap.put(Common.VENDORID, vendorId);
        }
        return this.list(SnapshotTask.class, sql, paramMap);
    }

    /**
     * 根据虚拟机ID查询
     *
     * @param serverId
     * @return
     * @throws Exception
     */
    public List<SnapshotTask> listByServerId(Long serverId) throws Exception {
        String sql = "select * from snapshot_task where server_id = :serverId";
        Map<String, Object> paramMap = MapTools.simpleMap("serverId", serverId);
        return this.list(SnapshotTask.class, sql, paramMap);
    }

    public SnapshotTask queryByTaskId(String taskUuid) throws Exception {
        String sql = "select * from snapshot_task where task_id = :taskUuid ";
        Map<String, Object> paramMap = MapTools.simpleMap("taskUuid", taskUuid);
        List<SnapshotTask> list = this.list(SnapshotTask.class, sql, paramMap);
        if (ListTool.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    public Boolean hasTask(Long serverId) throws Exception {
        String sql = "select id from snapshot_task where server_id = :serverId limit 1";
        Map<String, Object> paramMap = MapTools.simpleMap("serverId", serverId);
        return ListTool.isEmpty(this.list(SnapshotTask.class, sql, paramMap)) ? false : true;
    }

    /**
     * 根据云硬盘ID查询
     *
     * @param volumeId
     * @return
     * @throws Exception
     */

    public List<SnapshotTask> listByVolumeId(Long volumeId) throws Exception {
        String sql = "select * from snapshot_task where volume_id = :volumeId";
        Map<String, Object> paramMap = MapTools.simpleMap("volumeId", volumeId);
        return this.list(SnapshotTask.class, sql, paramMap);
    }

    /**
     * 根据云平台ID删除
     *
     * @param vendorId
     * @return
     * @throws Exception
     */

    public boolean removeByVendorId(Long vendorId) throws Exception {
        String sql = "delete from snapshot_task where vendor_id = :vendorId";
        Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
        return this.execute(sql, params) > 0;
    }

    /**
     * 根据虚拟机ID删除
     *
     * @param serverId
     * @return
     * @throws Exception
     */

    public boolean removeByServerId(Long serverId) throws Exception {
        String sql = "delete from snapshot_task where server_id = :serverId";
        Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
        return this.execute(sql, params) > 0;
    }

    /**
     * 根据云硬盘ID删除
     *
     * @param volumeId
     * @return
     * @throws Exception
     */

    public boolean removeByVolumeId(Long volumeId) throws Exception {
        String sql = "delete from snapshot_task where volume_id = :volumeId";
        Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
        return this.execute(sql, params) > 0;
    }
}
