package com.bocloud.ims.service.resource;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;
import com.bocloud.cmp.driver.common.TaskMsgDriver;
import com.bocloud.cmp.model.TaskMsgModel;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.interfaces.resource.TaskMsgService;
import com.bocloud.ims.repository.resource.CloudVendorRepository;

/**
 * 查询vc平台任务
 * 
 * @ClassName: TaskMsgServiceImpl.java
 * @Description:
 *
 * @author: ymh
 * @date: 2021年4月30日 上午11:50:38
 */
@Service
public class TaskMsgServiceImpl implements TaskMsgService {

    private static Logger logger = LoggerFactory.getLogger(TaskMsgServiceImpl.class);

    @Autowired
    private CloudVendorRepository cloudVendorRepository;
    @Autowired
    private TaskMsgDriver taskMsgDriver;

    @Override
    public BocloudResult<List<TaskMsgModel>> list(Long vendorId) {
        try {
            CloudVendor vendor = cloudVendorRepository.query(vendorId);
            Assert.notNull(vendor, "云平台不存在");
            BocloudResult bocloudResult = taskMsgDriver.list(vendor.getUuid());
            if (bocloudResult.isFailed()) {
                return bocloudResult;
            }
            List<TaskMsgModel> taskMsgModels = (List<TaskMsgModel>)bocloudResult.getData();
            Collections.sort(taskMsgModels, new Comparator<TaskMsgModel>() {
                @Override
                public int compare(TaskMsgModel model1, TaskMsgModel model2) {
                    if (null != model2 && null != model1 && null != model2.getStartTime()
                        && null != model1.getStartTime()) {
                        return (int)(model2.getStartTime().getTime() - model1.getStartTime().getTime());
                    }
                    return 0;
                }
            });
            return new BocloudResult<>(true, taskMsgModels, "查询成功");
        } catch (Exception e) {
            logger.error("查询平台任务异常：" + e.getMessage());
            return new BocloudResult<>(false, "查询平台任务异常：" + e.getMessage());
        }
    }

}
