package com.uas.platform.b2b.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.uas.platform.b2b.erp.model.ErpLog;
import com.uas.platform.b2b.erp.model.ErpLogMessageStat;
import com.uas.platform.b2b.erp.service.ErpLogService;
import com.uas.platform.b2b.model.SearchFilter;
import com.uas.platform.b2b.support.JxlsExcelView;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.PageParams;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * ERP数据传输日志
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping(value = "/log/erp")
public class ErpLogController {

	@Autowired
	private ErpLogService erpLogService;

	/**
	 * 分页查找ERP数据交互日志
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public Page<ErpLog> getErpLogs(PageParams params) {
		PageInfo info = new PageInfo(params);
		info.filter("enUU", SystemSession.getUser().getEnterprise().getUu());
		return erpLogService.findSearchByPageInfo(info, null);
	}

	/**
	 * 分页查找平台使用日志
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/xls", method = RequestMethod.GET)
	public ModelAndView exportErpLogs() {
		final PageInfo pageInfo = new PageInfo(1, JxlsExcelView.MAX_SIZE, 0);
		pageInfo.sorting("time", Direction.DESC);
		pageInfo.filter("enUU", SystemSession.getUser().getEnterprise().getUu());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", erpLogService.findSearchByPageInfo(pageInfo, null).getContent());
		return new ModelAndView(new JxlsExcelView("classpath:jxls-tpl/account/log/ErpLog$List", "数据传输日志"), map);
	}
	
	/**
	 * 获取不同数据内容的单据传输总数
	 * @return
	 */
	@RequestMapping(value = "/stat/message", method = RequestMethod.GET)
	@ResponseBody
	public List<ErpLogMessageStat> getMessageStat(Long enuu, String searchFilter) {
		SearchFilter filter = null;
		if (StringUtils.hasText(searchFilter)) {
			filter = FlexJsonUtils.fromJson(searchFilter, SearchFilter.class);
		}
		return erpLogService.findMessageStat(enuu, filter);
	}
}
