package com.chinaitop.depot.intelligent.statisticalReport.controller;

import com.chinaitop.depot.intelligent.statisticalReport.service.StatisticalReportService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unissoft.model.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

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

/**
 * @author hf
 * @description: 统计报表
 * @create 2019-01-21
 */
@RestController
@RequestMapping(value = "/intelligents/statisticalReport")
@Api(value = "StatisticalReportController", description = "统计报表控制类")
@SuppressWarnings("all")
public class StatisticalReportController {

    @Resource
    private StatisticalReportService statisticalReportService;

    /**
     * 汇总报表
     * @param pageNum
     * @param pageSize
     * @param orgId
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "/getSummaryReportInfo", method = RequestMethod.GET)
    @ApiOperation(value = "汇总报表列表", notes = "查询汇总报表列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "机构id", paramType = "query"),
            @ApiImplicitParam(name = "startDate", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", paramType = "query")
    })
    public ResponseEntity<PageInfo<Map<String,Object>>> getSummaryReportInfo(Integer pageNum, Integer pageSize, Integer orgId, 
    		String startDate, String endDate) {
        List<Map<String,Object>> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            Map<String,Object> map = new HashMap<String, Object>();
            if (orgId != null) {
            	map.put("orgId", orgId);
            }
            if (StringUtils.isNotBlank(startDate)) {
            	map.put("startDate", startDate);
            }
            if (StringUtils.isNotBlank(endDate)) {
            	map.put("endDate", endDate);
            }
            list = statisticalReportService.getSummaryReportInfo(map);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(list);
        return ResponseEntity.ok(pageInfo);
    }
    
    /**
     * 详情报表
     * @param orgId
     * @param storehouseCode
     * @param startDate
     * @return
     */
    @RequestMapping(value = "/getDetailsReportInfo", method = RequestMethod.GET)
    @ApiOperation(value = "详情报表列表", notes = "查询详情报表列表")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "orgId", value = "机构id", paramType = "query"),
    	@ApiImplicitParam(name = "storehouseCode", value = "仓房code", paramType = "query"),
    	@ApiImplicitParam(name = "startDate", value = "检测日期", paramType = "query")
    })
    public ResponseEntity<Map<String,Object>> getDetailsReportInfo(Integer orgId, String storehouseCode,
    		String startDate) {
    	Map<String,Object> mapDetails = new HashMap<String,Object>();
    	try {
    		Map<String,Object> map = new HashMap<String, Object>();
    		if (orgId != null) {
    			map.put("orgId", orgId);
    		}
    		if (StringUtils.isNotBlank(storehouseCode)) {
    			map.put("storehouseCode", storehouseCode);
    		}
    		if (StringUtils.isNotBlank(startDate)) {
    			map.put("startDate", startDate);
    		}
    		List<Map<String,Object>> list = statisticalReportService.getDetailsReportInfo(map);
    		if(list.size() > 0) {
    			mapDetails = list.get(0);
    		}
    	} catch (Exception e) {
    		e.printStackTrace();
    		ResponseEntity.failed(e.getMessage());
    	}
    	return ResponseEntity.ok(mapDetails);
    }
    

    /**
     * 上报报表
     * @param pageNum
     * @param pageSize
     * @param orgId
     * @param startDate
     * @return
     */
    @RequestMapping(value = "/getSubmitReportInfo", method = RequestMethod.GET)
    @ApiOperation(value = "上报报表列表", notes = "查询上报报表列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "机构id", paramType = "query"),
            @ApiImplicitParam(name = "startDate", value = "检测日期", paramType = "query")
    })
    public ResponseEntity<PageInfo<Map<String,Object>>> getSubmitReportInfo(Integer pageNum, Integer pageSize, Integer orgId, 
    		String startDate) {
        List<Map<String,Object>> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            Map<String,Object> map = new HashMap<String, Object>();
            if (orgId != null) {
            	map.put("orgId", orgId);
            }
            if (StringUtils.isNotBlank(startDate)) {
            	map.put("startDate", startDate);
            }
            list = statisticalReportService.getSubmitReportInfo(map);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(list);
        return ResponseEntity.ok(pageInfo);
    }
    
}
