package com.huatek.frame.base.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.frame.base.entity.RequestLog;
import com.huatek.frame.base.mapper.RequestLogDao;
import com.huatek.frame.base.mapper.RequestLogMapper;
import com.huatek.frame.base.service.RequestLogService;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.util.date.TimeUtil;
import com.huatek.torch.util.uuid.Uuid;

@Component
@Service(interfaceClass = RequestLogService.class)
public class RequestLogServiceImpl implements RequestLogService {

	@Autowired
	private RequestLogMapper requestLogMapper;

	@Autowired
	private RequestLogDao requestLogDao;

	@Override
	public Map<String, Object> findLogPage(RequestLog log) {
		PageHelper.startPage(log.getPage(), log.getLimit());
		Page<RequestLog> logs = requestLogMapper.selectRequestLogPage(log);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, logs.getTotal(), "", logs.getResult());
	}

	@Override
	public Map<String, Object> deleteLogs(String[] ids) {
		for (String id : ids) {
			requestLogDao.del(id);
		}
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}

	@Override
	public Map<String, Object> addLogs(RequestLog log) {
		log.setId(Uuid.getUUID());
		log.setCreateDate(TimeUtil.getLocalDateTime());
		int count = requestLogDao.add(log);
		if (count > 0) {
			return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
		} else {
			return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "", "");
		}
	}

	@Override
	public Map<String, Object> findLogs(String id) {
		RequestLog log = requestLogDao.get(id);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", log);
	}

}
