package com.pqs9000.controller.report;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.TimeInterval;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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 com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import com.njcn.utils.UserUtil;
import com.pqs9000.controller.area.IntegralityController;
import com.pqs9000.pojo.report.FpyReportData;
import com.pqs9000.service.report.FpyReportService;

/**
 * 合格率报告
 */
@Controller
@RequestMapping(value = "fpyreport")
public class FpyReportController {
    private static final Logger logger = LoggerFactory.getLogger(IntegralityController.class);

    @Autowired
    private UserUtil userUtil;
    @Autowired
    private FpyReportService fpyReportService;
    @Autowired
    UserLogDetailService userLog;

	@RequestMapping(value = "fpyData", method = RequestMethod.POST)
	@ResponseBody
	public HttpResult fpyData(HttpServletRequest request, HttpSession session, String startTime, String endTime, String area) {

	    HttpResult result = new HttpResult();
		DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "fpyData");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }

        List<Integer> lineIndexs = userUtil.getAllLineIndexs(area);

        List<FpyReportData> list = new ArrayList<>();
        try {
            TimeInterval timeInterval = new TimeInterval();
        	list = fpyReportService.fpyExcel(lineIndexs,datePojo.getStartTime(), datePojo.getEndTime());
            logger.info("方法耗时+++++++++++++++++++++++"+timeInterval.interval());
        	if (null == list) {
                result = PubUtils.assignmentResult(null, 500, "获取合格率报表数据失败");
            } else {
                result = PubUtils.assignmentResult(list, 200, "获取合格率报表数据成功");
            }
        } catch (Exception e) {
            logger.error("获取合格率报表数据异常,异常为：" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取合格率报表数据异常");
        }

		return result;
	}


    @RequestMapping(value = "qualifiedRate", method = RequestMethod.GET)
    @ResponseBody
    public HttpResult qualifiedRate(String startTime, String endTime, String area) {
        TimeInterval timeInterval = new TimeInterval();
        HttpResult result = new HttpResult();
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "qualifiedRate");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        List<Integer> lineIndexs = userUtil.getAllLineIndexs(area);



        List<FpyReportData> fpyReportDataList = fpyReportService.qualifiedRate(lineIndexs,datePojo.getStartTime(), datePojo.getEndTime());

            if (CollectionUtil.isEmpty(fpyReportDataList)) {
                result = PubUtils.assignmentResult(null, 500, "获取合格率报表数据失败");
            } else {
                result = PubUtils.assignmentResult(fpyReportDataList, 200, "获取合格率报表数据成功");
            }
        logger.info("方法耗时+++++++++++++++++++++++"+timeInterval.interval());
        return result;
    }
}
