package com.fuzamei.web;

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fuzamei.constant.HintMSG;
import com.fuzamei.constant.Role;
import com.fuzamei.entity.OperationType;
import com.fuzamei.entity.Params;
import com.fuzamei.service.OperationHistoryService;
import com.fuzamei.service.UserAuthoricationService;
import com.fuzamei.util.JSONUtil;
import com.fuzamei.util.PageDTO;
import com.fuzamei.util.ReadConfUtil;
import com.fuzamei.util.ValidationUtil;

@RestController
@RequestMapping(value="/operationHistory")
public class OperationHistoryAction {

	@Autowired
	private OperationHistoryService operationHistoryService;
	
	@Autowired
	private UserAuthoricationService userAuthoricationService;
	
	@Autowired
	private HttpServletRequest req;
	/**
	 * 
	* @Title: queryOperationHistory
	* @Description: TODO(管理员查看操作历史)
	{
		"userId":"",
		"page":"1",
		"startTime":"",
		"endTime":""
	}
	* @params @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午6:34:22
	* @throws
	 */
	@RequestMapping(value="/queryOperationHistoryByUser",method=RequestMethod.POST)
	public Map<String, Object> queryOperationHistoryByUser(@RequestBody Params params){
		try {
			String userId = req.getHeader("Authorization").split("&")[1];
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(userId), Role.PLANNER,Role.SUPPLIER,Role.CARRIER,Role.STOCKER);
			params.setUserId(Integer.parseInt(userId));
			int page = ValidationUtil.checkMinAndAssignInt(params.getPage(), 1);
			int rowNum=ValidationUtil.checkMinAndAssignInt(params.getRowNum(), 1);
			Long startTime=ValidationUtil.checkAndAssignDefaultLong(params.getStartTime(), 0L);
			Long endTime=ValidationUtil.checkAndAssignDefaultLong(params.getEndTime(), Long.MAX_VALUE);
			params.setStartTime(startTime);
			if(startTime<=endTime) params.setEndTime(endTime);
			else params.setEndTime(Long.MAX_VALUE);
			params.setStartPage((page - 1) * rowNum);
			params.setRowNum(rowNum);
			PageDTO pageDto = operationHistoryService.queryOperationHistory(params);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, pageDto);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
	
	/**
	 * 
	* @Title: queryOperationHistoryByAdmin
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param params
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年2月2日 下午5:08:43
	* @throws
	 */
	@RequestMapping(value="/queryOperationHistoryByAdmin",method=RequestMethod.POST)
	public Map<String, Object> queryOperationHistoryByAdmin(@RequestBody Params params){
		try {
			String userId = req.getHeader("Authorization").split("&")[1];
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(userId), Role.ADMIN);
			params.setUserId(Integer.parseInt(userId));
			int page = ValidationUtil.checkMinAndAssignInt(params.getPage(), 1);
			int rowNum=ValidationUtil.checkMinAndAssignInt(params.getRowNum(), 1);
			Long startTime=ValidationUtil.checkAndAssignDefaultLong(params.getStartTime(), 0L);
			Long endTime=ValidationUtil.checkAndAssignDefaultLong(params.getEndTime(), Long.MAX_VALUE);
			params.setStartTime(startTime);
			if(startTime<=endTime) params.setEndTime(endTime);
			else params.setEndTime(Long.MAX_VALUE);
			params.setStartPage((page - 1) * rowNum);
			params.setRowNum(rowNum);
			PageDTO pageDto = operationHistoryService.queryOperationHistoryByAdmin(params);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, pageDto);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
	/**
	 * 
	* @Title: showAllOperationTypeOfUser
	* @Description: TODO(给普通用户显示操作类型id和操作类型名称)
	* @param @param params
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年2月2日 下午5:32:53
	* @throws
	 */
	@RequestMapping(value="/showAllOperationTypeOfUser",method=RequestMethod.POST)
	public Map<String, Object> showAllOperationTypeOfUser(){
		try {
			String userId = req.getHeader("Authorization").split("&")[1];
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(userId), Role.PLANNER,Role.SUPPLIER,Role.CARRIER,Role.STOCKER);
			List<OperationType> operationTypeList = operationHistoryService.showAllOperationTypeOfUser();
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, operationTypeList);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
	/**
	 * 
	* @Title: showAllOperationTypeOfAdmin
	* @Description: TODO(给管理员显示操作类型id和操作类型名称)
	* @param @param params
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年2月2日 下午5:32:57
	* @throws
	 */
	@RequestMapping(value="/showAllOperationTypeOfAdmin",method=RequestMethod.POST)
	public Map<String, Object> showAllOperationTypeOfAdmin(){
		try {
			String userId = req.getHeader("Authorization").split("&")[1];
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(userId), Role.ADMIN);
			List<OperationType> operationTypeList = operationHistoryService.showAllOperationTypeOfAdmin();
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, operationTypeList);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
	
}
