package com.pqs9900.controller.business;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.data.DataLoss;
import com.njcn.service.data.DataLossService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.login.AccountException;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "dataloss")
public class DataLossController {
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(DataLoss.class);

    @Autowired
    DataLossService dataLossService;
    @Autowired
    UserLogDetailService logDetailService;

    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 获取数据缺失情况
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping(value = "getDataLoss", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getDataLoss(HttpServletRequest request, @Param(value = "startTime") String startTime, @Param(value =
            "endTime") String endTime) throws
        JsonProcessingException, AccountException {
        HttpResult result = new HttpResult();
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "getDataLoss");
        if (!datePojo.getValidity()) {
            result = PubUtils.assignmentResult(null, 500, "校验前台传递时间参数报错");
            return result;
        }
        List<DataLoss> dataLosses = new ArrayList<>();
        List<List> list = new ArrayList<>();
        int i = 0;
        try {
            dataLosses = dataLossService.getDataLoss();
            if (dataLosses.isEmpty()) {
                result = PubUtils.assignmentResult(null, 500, "没有获取到数据缺失情况");
                logger.error("获取数据缺失情况失败");
                logDetailService.getLog("查询数据缺失", "失败",PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(),0);
            } else {
                for (DataLoss dataLoss : dataLosses) {
                    List<String> str = new ArrayList<String>();
                    str.add(String.valueOf(i += 1));
                    str.add(String.valueOf(dataLoss.getLineIndex()));
                    str.add(dataLoss.getGdName());
                    str.add(dataLoss.getSubName());
                    str.add(dataLoss.getScale());
                    str.add(dataLoss.getLineName());
                    List<DataLoss> result1 = dataLossService.getTj(startTime, endTime, dataLoss.getLineIndex());
                    float avg = 0;
                    if (result1.isEmpty()) {
                        avg = 0;
                    } else {
                        avg = result1.get(0).getAvg();
                    }
                    str.add(String.valueOf((float) (Math.round(avg * 100 * 100)) / 100));
                    if (avg * 100 > 90) {
                        str.add("合格");
                    } else if (avg * 100 < 60) {
                        str.add("警告");
                    } else {
                        str.add("注意");
                    }
                    list.add(str);
                }
                result = PubUtils.assignmentResult(list, 200, "获取数据缺失情况成功");
                logger.info("获取数据缺失情况成功");
                logDetailService.getLog("查询数据缺失", "成功",PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(),0);
            }
        } catch (Exception e) {
            result = PubUtils.assignmentResult(null, 500, "没有获取到数据缺失情况");
            logDetailService.getLog("查询数据缺失", "失败",PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(),0);
            logger.error("获取数据缺失情况失败,异常是" + e.toString());
        }
        return result;
    }

    /**
     * 获取数据缺失情况
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping(value = "getTj", method = RequestMethod.POST)
    @ResponseBody
    public List<DataLoss> getTj(HttpServletRequest request, @Param(value = "startTime") String startTime, @Param(value =
            "endTime") String endTime, @Param(value = "lineIndex") Long lineIndex) {
        List<DataLoss> dataLoss = new ArrayList<DataLoss>();
        try {
            dataLoss = dataLossService.getDataAll(startTime, endTime, lineIndex);
        } catch (Exception e) {
            logger.error("获取数据缺失情况异常，异常是" + e.toString());
        }
        return dataLoss;
    }
}
