package com.bocloud.ims.service.resource;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.LbTarget;
import com.bocloud.ims.interfaces.resource.LbTargetService;
import com.bocloud.ims.repository.resource.LbTargetRepository;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service("lbTargetService")
public class LbTargetServiceImpl implements LbTargetService {
	
	private static Logger logger = LoggerFactory.getLogger(LbTargetServiceImpl.class);
	@Autowired
	private LbTargetRepository lbTargetRepository;

	@Override
	public BocloudResult<GridBean<LbTarget>> list(Pager pager, RequestContext context) {
		try {
			List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
			Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
			sorter.put("gmtCreate", Common.ONE);
			Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
			Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
			int total = lbTargetRepository.count(params);
			List<LbTarget> list = lbTargetRepository.list(page, rows, params, sorter);
			GridBean<LbTarget> gridBean = GridHelper.getBean(page, rows, total, list);
			return new BocloudResult<GridBean<LbTarget>>(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("list elastic ip failure:", e);
			return new BocloudResult<GridBean<LbTarget>>(false, "查询失败", null, null);
		}
	}
}
