package com.pqs9900.controller.terminal;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import com.pqs9900.pojo.terminal.FlowDetailRVO;
import com.pqs9900.pojo.terminal.FlowStatisticsRVO;
import com.pqs9900.pojo.terminal.TerminalParamVO;
import com.pqs9900.pojo.terminal.TerminalRVO;
import com.pqs9900.service.terminal.TerminalStateService;
import com.pqs9900.serviceImpl.webtool.CsvDataServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Description 终端运行管理
 * @Author cdf
 * @Date 2020/8/28 15:10
 **/
@RestController
@RequestMapping("terminalmanger")
public class TerminalStateController {
    private static final Logger logger = LoggerFactory.getLogger(TerminalStateController.class);
    @Resource
    private UserLogDetailService userLog;
    @Autowired
    private TerminalStateService terminalStateService;
    /**
     * @Author chendaofei
     * @Description 获取终端版本流量列表
     * @Date 10:40 2020/8/31
     **/
    @GetMapping("getTerminalList")
    public HttpResult getTerminalList(String devType, Integer status,String startTime){
        List<TerminalRVO> list = terminalStateService.getTerminalList(devType,status,startTime);
        return PubUtils.assignmentResult(list,200,"获取成功");
    }

    /**
     * @Author chendaofei
     * @Description 获取终端版本流量策略列表
     * @Date 10:40 2020/8/31
     **/
    @PostMapping("getTerminalList2")
    public HttpResult getTerminalList2(@RequestBody List<String>list){
        List<TerminalRVO> res = terminalStateService.getTerminalList2(list);
        return PubUtils.assignmentResult(res,200,"获取成功");
    }

    /**
     * @Author chendaofei
     * @Description 批量修改终端运行状态
     * @Date 9:23 2020/9/2
     **/
    @PostMapping("updateDevFlag")
    public HttpResult updateDevFlag(HttpServletRequest request,@RequestBody TerminalParamVO terminalParamVO){
        if(CollectionUtil.isEmpty(terminalParamVO.getList())||terminalParamVO.getDevFlag()==null || StrUtil.isBlank(terminalParamVO.getDescribe())){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }
        int res = terminalStateService.updateDevFlag(terminalParamVO);
        if(res==terminalParamVO.getList().size()){
            userLog.getLog("修改终端运行状态", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null,200,"修改状态成功");
        }else {
            userLog.getLog("修改终端运行状态", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null,500,"修改状态失败");
        }
    }

    /**
     * @Author chendaofei
     * @Description 配置终端流量套餐
     * @Date 12:26 2020/9/21
     **/
    @PostMapping("addDevMealAss")
    public HttpResult addDevMealAss(HttpServletRequest request,@RequestBody TerminalParamVO terminalParamVO){
        if(CollectionUtil.isEmpty(terminalParamVO.getList())||PubUtils.isBlank(terminalParamVO.getFmBaseIndex())){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }
        try {
            Integer res = terminalStateService.addDevMealAss(terminalParamVO.getList(), terminalParamVO.getFmBaseIndex(), terminalParamVO.getFmReamIndex());

            if (res == terminalParamVO.getList().size()) {
                userLog.getLog("配置终端流量套餐", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                return PubUtils.assignmentResult(null, 200, "配置成功");
            } else {
                userLog.getLog("配置终端流量套餐", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                return PubUtils.assignmentResult(null, 500, "配置失败");
            }
        }catch (Exception e){
            logger.error("错误为："+e.getMessage());
            userLog.getLog("配置终端流量套餐", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null, 500, "配置失败");
        }
    }

    /**
     * @Author chendaofei
     * @Description 配置终端流量策略
     * @Date 12:26 2020/9/21
     **/
    @PostMapping("addDevStrategyAss")
    public HttpResult addDevStrategyAss(HttpServletRequest request,@RequestBody TerminalParamVO terminalParamVO){
        if(CollectionUtil.isEmpty(terminalParamVO.getList())||PubUtils.isBlank(terminalParamVO.getFsIndex())){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }
        try {
            Integer res = terminalStateService.addDevStrategyAss(terminalParamVO.getList(), terminalParamVO.getFsIndex());

            if (res == terminalParamVO.getList().size()) {
                userLog.getLog("配置终端流量策略", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                return PubUtils.assignmentResult(null, 200, "配置成功");
            } else {
                userLog.getLog("配置终端流量策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                return PubUtils.assignmentResult(null, 500, "配置失败");
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            userLog.getLog("配置终端流量策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null, 500, "配置失败");
        }
    }

    /**
     * @Author chendaofei
     * @Description 获取ip
     * @Date 12:26 2020/9/21
     **/
    @PostMapping("getWebIpList")
    public HttpResult getWebIpList(){
       String res = terminalStateService.getWebIpList();
       if("执行成功".equals(res)){
           return PubUtils.assignmentResult(null, 200, res);
       }else {
           return PubUtils.assignmentResult(null, 500, res);
       }
    }

    /**
     * @Author chendaofei
     * @Description 获取流量详情
     * @Date 12:26 2020/9/21
     **/
    @PostMapping("getTerminalFlowDetail")
    public HttpResult getTerminalFlowDetail(@RequestParam("devIndex")String devIndex,@RequestParam("startTime")String startTime){
        if(PubUtils.isBlank(devIndex)||PubUtils.isBlank(startTime)){
            return PubUtils.assignmentResult(null, 500, "参数不可为空");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        try {
            sdf.parse(startTime);
        } catch (ParseException e) {
            return PubUtils.assignmentResult(null, 500, "时间格式有误");
        }
        FlowDetailRVO res = terminalStateService.getTerminalFlowDetail(devIndex,startTime);
        if(res!=null){
            return PubUtils.assignmentResult(res, 200, "获取成功");
        }else {
            return PubUtils.assignmentResult(null, 500, "暂无数据");
        }
    }


    /**
     * @Author chendaofei
     * @Description 流量统计柱状图数据获取
     * @Date 20:39 2020/10/9
     **/
    @PostMapping("getFlowStatiscsList")
    public HttpResult getFlowStatiscsList(@RequestBody Map<String,Object> map){
        if(map==null){
            return PubUtils.assignmentResult(null, 500, "参数不可为空");
        }
       String startTime =(String)map.get("startTime");
        List<String> list = (List<String>) map.get("list");
        String mealBi = (String) map.get("mealBi");
        String mealUse = (String)map.get("mealUse");
        String mealAll = (String)map.get("mealAll");
        if(PubUtils.isBlank(mealBi) && PubUtils.isBlank(mealUse) && PubUtils.isBlank(mealAll) ){
            return PubUtils.assignmentResult(null, 500, "参数错误");
        }
        if(!PubUtils.isBlank(mealBi) && !PubUtils.isBlank(mealUse) && !PubUtils.isBlank(mealAll) ){
            return PubUtils.assignmentResult(null, 500, "参数错误");
        }
        if(CollectionUtil.isEmpty(list)){
            return PubUtils.assignmentResult(null, 500, "参数不可为空");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        try {
            sdf.parse(startTime);
        } catch (ParseException e) {
            return PubUtils.assignmentResult(null, 500, "时间格式有误");
        }
        List<FlowStatisticsRVO> list1 = terminalStateService.getFlowStatiscsList(startTime,list,mealBi,mealUse,mealAll);
        return PubUtils.assignmentResult(list1, 200, "获取成功");
    }

    /**
    * @Author chendaofei
    * @Description 查看终端性能展示
    * @Date 9:42 2020/10/14
    **/
    @PostMapping("getQuailty")
    public HttpResult getQuailty(@RequestParam("devIndex")String devIndex){
        String res = terminalStateService.getQuailty(devIndex);
        if("获取成功".equals(res)){
            return PubUtils.assignmentResult(res, 200, "获取成功");
        }else {
            return PubUtils.assignmentResult(res, 500, "获取失败");
        }
    }

    @PostMapping("closeSocket")
    public HttpResult closeSocket(String devIndex){
        String res = terminalStateService.closeSocket(devIndex);
        if("执行成功".equals(res)){
            return PubUtils.assignmentResult(res, 200, "执行成功");
        }else {
            return PubUtils.assignmentResult(res, 500, "执行失败");
        }
    }

    @PostMapping("closeSocket2")
    public void closeSocket2(String devIndex){
        String res = terminalStateService.closeSocket(devIndex);
    }


    /**
    * @Author chendaofei
    * @Description 心跳机制
    * @Date 12:00 2020/10/21
    **/
    @PostMapping("heartSocket")
    public HttpResult heartSocket(String devIndex){
        String res = terminalStateService.heartSocket(devIndex);
        if("发送成功".equals(res)){
            return PubUtils.assignmentResult(null, 200, res);
        }else {
            return PubUtils.assignmentResult(null, 500, res);
        }
    }

}
