package com.bonus.hn.bearing.capacity.backstage.manager.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bonus.hn.bearing.capacity.backstage.manager.dao.SysLogsDao;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.PublicBean;
import com.bonus.hn.bearing.capacity.backstage.manager.model.SysLogs;
import com.bonus.hn.bearing.capacity.backstage.manager.service.RoleService;
import com.bonus.hn.bearing.capacity.backstage.manager.table.PageTableHandler;
import com.bonus.hn.bearing.capacity.backstage.manager.table.PageTableRequest;
import com.bonus.hn.bearing.capacity.backstage.manager.table.PageTableResponse;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.ExcelUtils;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @author bonus
 */
@Api(tags = "日志")
@RestController
@RequestMapping("/logs")
public class SysLogsController {

	@Autowired
	private SysLogsDao sysLogsDao;
	@Autowired
	private RoleService roleService;

	@GetMapping
	@PreAuthorize("hasAuthority('sys:log:query')")
	@ApiOperation(value = "日志列表")
	public PageTableResponse list(PageTableRequest request) {
		return new PageTableHandler(new PageTableHandler.CountHandler() {

			@Override
			public int count(PageTableRequest request) {
				return sysLogsDao.count(request.getParams());
			}
		}, new PageTableHandler.ListHandler() {

			@Override
			public List<SysLogs> list(PageTableRequest request) {
				return sysLogsDao.list(request.getParams(), request.getOffset(), request.getLimit());
			}
		}).handle(request);
	}


	@PostMapping("getMsgContent")
	@ApiOperation(value = "日志管理-列表")
	public R getList(PublicBean o){
		List<PublicBean> list = roleService.getorgList(o);
		return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
	}

	/**
	 * 日志管理导出
	 * @param request
	 * @param response
	 * @param page
	 */
	@RequestMapping(value = "/exportPhysicalCheckResult", method = RequestMethod.GET)
	@ApiOperation(value = "日志管理-导出")
	public void exportPayroll(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
		PublicBean b = new PublicBean();
		b.setPhyTimeStart(request.getParameter("phyTimeStart"));
		b.setPhyTimeEnd(request.getParameter("phyTimeEnd"));
		b.setKeyWord(request.getParameter("keyword"));
		List<PublicBean> list = roleService.getorgList(b);
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		String[] excelHeader = {"序号","用户名","操作功能","资源类型","日志","是否成功"};
		int[] ds_format = {2, 2, 2, 2, 2, 2};
		String fileName = "系统日志信息表";
		if (list != null) {
			int i = 0;
			for (PublicBean bean : list){
				i++;
				Map<String, Object> map = new HashMap<>(100);
				map.put("序号",i);
				map.put("用户名",bean.getUsername());
				map.put("操作功能",bean.getModule());
				map.put("资源类型",bean.getModule());
				map.put("日志",bean.getSysLogs());
				map.put("是否成功",bean.getFlag());
				mapList.add(map);
			}
		}
		try {
			ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
					response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
