package com.bocloud.ims.service.resource;

import com.bocloud.common.model.*;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.AccessControl;
import com.bocloud.ims.interfaces.resource.AccessControlService;
import com.bocloud.ims.repository.resource.AccessControlRepository;
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;

/**
 * @author lyy
 * @version 1.0
 * @date 2021/1/14 15:37
 */
@Service("accessControlService")
public class AccessControlServiceImpl implements AccessControlService {
	private static Logger logger = LoggerFactory.getLogger(AccessControlServiceImpl.class);
	@Autowired
	private AccessControlRepository accessControlRepository;

	@Override
	public BocloudResult list(int page, int rows, List<Param> params, Map<String, String> sorter, boolean simple,
			RequestContext context) {
		List<AccessControl> list;
		int total = 0;
		GridBean gridBean;
		try {
			if (null == params) {
				params = new ArrayList<>();
			}
			if (null == sorter) {
				sorter = Maps.newHashMap();
			}
			total = accessControlRepository.count(params);
			if (simple) {
				List<SimpleBean> beans = this.accessControlRepository.list(params, sorter);
				gridBean = new GridBean(1, 1, total, beans);
			} else {
				list = accessControlRepository.list(page, rows, params, sorter);
				gridBean = GridHelper.getBean(page, rows, total, list);
			}
			return new BocloudResult(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("list access control failure:", e);
			return new BocloudResult(false, "查询失败");
		}
	}
}
