package com.pqs9000.controller.responsibility;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.responsibility.ResponsibilityData;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import com.pqs9000.pojo.responsibility.ResponsibilityResult;
import com.pqs9000.service.responsibility.ResponsibilityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author hongawen
 * @Date: 2019/3/21 15:34
 */
@Controller
@RequestMapping("responsibility")
public class ResponsibilityListController {

    private static final Logger logger = LoggerFactory.getLogger(ResponsibilityListController.class);

    @Autowired
    private ResponsibilityService responsibilityService;

    @Autowired
    UserLogDetailService userLog;

    /**
     * 高级分析--》责任划分列表
     */
    @GetMapping("responsibilitylist")
    public String responsibilityList() {
        return "business/responsibility/responsibilityList";
    }

    /**
     * 责任划分列表的数据获取
     */
    @PostMapping("responsibilityDataList")
    @ResponseBody
    public HttpResult responsibilityDataList(String startTime, String endTime, HttpServletRequest request) {
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "responsibilityDataList");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        List<ResponsibilityData> responsibilityDataList;
        try {
            responsibilityDataList = responsibilityService.responsibilityDataList(datePojo.getStartTime(), datePojo.getEndTime());
            if (CollectionUtils.isEmpty(responsibilityDataList)) {
                result = PubUtils.assignmentResult(null, 500, "时间范围内责任数据为空");
            } else {
                result = PubUtils.assignmentResult(responsibilityDataList, 200, "获取时间范围内责任数据成功");
            }
            userLog.getLog("查询责任划分列表的数据", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        } catch (Exception e) {
            logger.error("获取时间范围内责任数据异常，异常为：" + e.toString());
            userLog.getLog("查询责任划分列表的数据", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            result = PubUtils.assignmentResult(null, 500, "获取事件范围内责任数据异常");
        }
        return result;
    }

    /**
     * 谐波责任列表中，点击详情，根据索引返回具体信息
     *
     * @param resIndex 责任计算索引
     */
    @GetMapping("viewDetail")
    public ModelAndView viewDetail(String resIndex) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("business/responsibility/viewDetail");
        mv.addObject("resIndex", resIndex);
        return mv;
    }


    /**
     * 根据索引返回具体信息
     *
     * @param resIndex 责任计算索引
     */
    @PostMapping("getResponsbilityData")
    @ResponseBody
    public HttpResult getResponsbilityData(String resIndex) {
        HttpResult result;
        ResponsibilityData responsibilityData;
        try {
            responsibilityData = responsibilityService.selectByIndex(resIndex);
            if (null == responsibilityData) {
                result = PubUtils.assignmentResult(null, 500, "根据索引获取责任计算详细数据为空");
            } else {
                result = PubUtils.assignmentResult(responsibilityData, 200, "根据索引获取责任计算详细数据成功");
            }
        } catch (Exception e) {
            logger.error("根据索引获取责任计算详细数据异常,异常为：" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "根据索引获取责任计算详细数据异常");
        }
        return result;
    }


    /**
     * 根据页面条件显示动态波形和责任表格
     * @param resIndex 责任计算索引
     */
    @PostMapping("displayHistoryData")
    @ResponseBody
    public HttpResult displayHistoryData(Integer time, String resIndex) {
        HttpResult result;
        List<ResponsibilityResult> responsibilityResults;
        try {
            responsibilityResults = responsibilityService.displayHistoryData(time,resIndex);
            if(CollectionUtils.isEmpty(responsibilityResults)){
                result=PubUtils.assignmentResult(null,500,"历史谐波责任计算数据为空");
            }else{
                result=PubUtils.assignmentResult(responsibilityResults,200,"获取历史谐波责任计算数据成功");
            }
        } catch (Exception e) {
            logger.error("获取历史谐波责任计算数据异常,异常为：" + e.toString());
            result=PubUtils.assignmentResult(null,500,"获取历史谐波责任计算数据异常");
        }
        return result;
    }

    /**
     * 根据索引删除责任记录和文件数据
     * @param resIndex 责任计算索引
     */
    @PostMapping("deleteResponsibility")
    @ResponseBody
    public HttpResult deleteResponsibility(String resIndex,HttpServletRequest request) {
        HttpResult result;
        try {
            responsibilityService.deleteResponsibilityByResIndex(resIndex);
            result=PubUtils.assignmentResult(null,200,"删除记录成功");
            userLog.getLog("删除责任记录", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        } catch (Exception e) {
            logger.error("根据索引删除责任记录和文件数据异常,异常为：" + e.toString());
            userLog.getLog("删除责任记录", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            result=PubUtils.assignmentResult(null,500,"删除记录失败");
        }
        return result;
    }



}
