package com.bocloud.ims.service.resource;

import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.StackTemplateVersion;
import com.bocloud.ims.interfaces.resource.StackTemplateVersionService;
import com.bocloud.ims.repository.resource.StackTemplateVersionRepository;
import com.bocloud.service.lock.BocloudACLock;
import com.bocloud.service.lock.LockFactory;
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 java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * stack version
 *
 * @author ymh
 * @时间 2019年11月13日
 */
@Service("stackTemplateVersionService")
public class StackTemplateVersionServiceImpl implements StackTemplateVersionService {

    private static Logger logger = LoggerFactory.getLogger(StackTemplateVersionServiceImpl.class);
    @Autowired
    private LockFactory lockFactory;
    @Autowired
    private StackTemplateVersionRepository stackTemplateVersionRepository;

    @Override
    public BocloudResult<GridBean<StackTemplateVersion>> list(Pager pager, RequestContext context) {
        List<StackTemplateVersion> list;
        GridBean gridBean;
        try {
            pager.getSorter().put("gmtCreate", Common.ONE);
            int total = stackTemplateVersionRepository.count(pager.getParams());
            if (pager.getSimple()) {
                List<SimpleBean> beans = stackTemplateVersionRepository.list(pager.getParams(), pager.getSorter());
                gridBean = new GridBean(1, 1, total, beans);
            } else {
                list = stackTemplateVersionRepository.list(pager.getPage(), pager.getRows(), pager.getParams(), pager.getSorter());
                gridBean = GridHelper.getBean(pager.getPage(), pager.getRows(), total, list);
            }
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("list cloudStack failure:", e);
            return new BocloudResult<>(false, "查询失败", null, null);
        }

    }

    @Override
    public BocloudResult create(StackTemplateVersion stackTemplateVersion, RequestContext context) {
        try {
            return new BocloudResult<>(true, "开发中！");
        } catch (Exception e) {
            logger.error("新建失败：" + e.getMessage());
            return new BocloudResult<>(false, "新建失败：" + e.getMessage());
        }
    }

    @Override
    public BocloudResult modify(StackTemplateVersion stackTemplateVersion, Long userId) {
        String path = StackTemplateVersion.class.getSimpleName() + "_" + stackTemplateVersion.getId();
        try (BocloudACLock lock = lockFactory.getACLock(path)) {
            Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
            return new BocloudResult<>(true, "开发中！");
        } catch (Exception e) {
            logger.error("编辑失败：" + e.getMessage());
            return new BocloudResult<>(false, "编辑失败：" + e.getMessage());
        }
    }

    @Override
    public BocloudResult remove(Long id, RequestContext context) {
        try {
            return new BocloudResult<>(true, "删除成功！");
        } catch (Exception e) {
            logger.error("删除失败：" + e.getMessage());
            return new BocloudResult<>(false, "删除失败：" + e.getMessage());
        }
    }

    @Override
    public BocloudResult detail(Long id) {
        try {
            StackTemplateVersion stackTemplateVersion = stackTemplateVersionRepository.query(id);
            if (null == stackTemplateVersion) {
                return new BocloudResult<>(false, "数据不存在！");
            }
            return new BocloudResult<>(true, stackTemplateVersion, "success!");
        } catch (Exception e) {
            logger.error("查询失败：" + e.getMessage());
            return new BocloudResult<>(false, "查询失败：" + e.getMessage());
        }
    }


}
