package com.bocloud.ims.service.resource;

import com.bocloud.common.model.*;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.Icon;
import com.bocloud.ims.interfaces.resource.ResourceIconService;
import com.bocloud.ims.repository.resource.ResourceIconRepository;
import com.bocloud.service.lock.BocloudACLock;
import com.bocloud.service.lock.LockFactory;
import com.google.common.collect.Maps;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 镜像图标service实现层
 *
 * @author lyy
 * @Version 1.0
 * @since 2017年10月26日
 */
@Service("resourceIconService")
public class ResourceIconServiceImpl implements ResourceIconService {
	private static final Logger logger = LoggerFactory.getLogger(ResourceIconServiceImpl.class);

	@Autowired
	private LockFactory lockFactory;
	@Autowired
	private ResourceIconRepository resourceIconRepository;

	@Override
	public BocloudResult<GridBean<Icon>> list(Pager pager) {
		GridBean gridBean = null;
		try {
			int page = pager.getPage();
			int rows = pager.getRows();
			List<Param> params = pager.getParams();
			Map<String, String> sorter = pager.getSorter();
			boolean simple = pager.getSimple();

			if (null == params) {
				params = new ArrayList<Param>();
			}
			if (null == sorter) {
				sorter = Maps.newHashMap();
			}
			int total = this.resourceIconRepository.count(params);
			if (simple) {
				List<SimpleBean> beans = this.resourceIconRepository.list(params, sorter);
				gridBean = new GridBean(1, 1, total, beans);
			} else {
				List<Icon> list = this.resourceIconRepository.list(page, rows, params, sorter);
				gridBean = GridHelper.getBean(page, rows, total, list);
			}
			return new BocloudResult<>(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("Query notice icon list fail:", e);
			return new BocloudResult<>(false, "查询失败");
		}
	}

	@Override
	public BocloudResult<Icon> create(Icon icon, RequestContext context) {
		try {
			this.resourceIconRepository.save(icon);
			return new BocloudResult<>(true, icon, "添加成功");
		} catch (Exception e) {
			logger.error("Create notice icon error:", e);
			return new BocloudResult<>(false, "添加失败");
		}
	}

	@Override
	public BocloudResult<String> remove(Long id) {
		String path = Icon.class.getSimpleName() + "_" + id;
		try (BocloudACLock lock = lockFactory.getACLock(path)) {
			Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
			Icon icon = resourceIconRepository.query(id);
			if (icon.getReserved().equals(true)) {
				return new BocloudResult<>(false, "此图标不可删除");
			} else {
				this.resourceIconRepository.delete(id);
				return new BocloudResult<>(true, "删除成功");
			}
		} catch (Exception e) {
			logger.error("Remove notice icon fail:", e);
			return new BocloudResult<>(false, "删除失败");
		}
	}

}
