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.common.Tenant;
import com.bocloud.ims.entity.common.User;
import com.bocloud.ims.entity.resource.ResourceEvent;
import com.bocloud.ims.interfaces.resource.ResourceEventService;
import com.bocloud.ims.repository.resource.ResourceEventRepository;
import com.bocloud.ims.service.internal.SmsManagerService;
import com.bocloud.ims.service.internal.SmsTenantService;
import com.bocloud.ims.service.internal.SmsUserService;
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接口实现
 * 
 * @author dongkai
 *
 */
@Service("resourceEventService")
public class ResourceEventServiceImpl implements ResourceEventService {

	private static Logger logger = LoggerFactory.getLogger(ImageServiceImpl.class);
	@Autowired
	private ResourceEventRepository resourceEventRepository;
	@Autowired
	private SmsUserService smsUserService;
	@Autowired
	private SmsTenantService smsTenantService;
	@Autowired
	private SmsManagerService smsManagerService;

	@Override
	public BocloudResult<GridBean<ResourceEvent>> 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("gmtOperate", Common.ONE);
			Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
			Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
			List<ResourceEvent> list = resourceEventRepository.list(page, rows, params, sorter);
			for (ResourceEvent event : list) {
				if (null == event.getCatalog()) {
					continue;
				}
				switch (event.getCatalog()) {
				case Manager:
					BocloudResult<User> managerResult = smsManagerService.detail(event.getOperatorId());
					if (managerResult.isSuccess() && null != managerResult.getData()) {
						event.setOperator(managerResult.getData().getName());
					}
					break;
				case Tenant:
					BocloudResult<Tenant> tenantResult = smsTenantService.detail(event.getOperatorId());
					if (tenantResult.isSuccess() && null != tenantResult.getData()) {
						event.setOperator(tenantResult.getData().getName());
					}
					break;
				case User:
					BocloudResult<User> userResult = smsUserService.detail(event.getOperatorId());
					if (userResult.isSuccess() && null != userResult.getData()) {
						event.setOperator(userResult.getData().getName());
					}
					break;
				default:
					break;
				}
			}
			int total = resourceEventRepository.count(params);
			GridBean<ResourceEvent> gridBean = GridHelper.getBean(page, rows, total, list);
			return new BocloudResult<GridBean<ResourceEvent>>(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("list resourceEvent failure:", e);
			return new BocloudResult<GridBean<ResourceEvent>>(false, "查询失败", null, null);
		}
	}

}
