package com.pqs9900.controller.terminal;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.configuration.DicData;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import com.pqs9900.pojo.terminal.FlowStrategyBindDTO;
import com.pqs9900.pojo.terminal.FlowStrategyVO;
import com.pqs9900.service.terminal.FlowStrategyService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description 流量策略
 * @Author cdf
 * @Date 2020/8/10 14:58
 **/
@RestController
@RequestMapping("flowstrategy")
public class FlowStrategyController {
    @Resource
    private UserLogDetailService userLog;

    @Autowired
    private FlowStrategyService flowStrategyService;

    @PostMapping("addStrategy")
    public HttpResult addStrategy(HttpServletRequest request, @RequestBody FlowStrategyVO flowStrategyVO) {
        if(PubUtils.isBlank(flowStrategyVO.getName())|| flowStrategyVO.getComPer()==null || flowStrategyVO.getWarnPer()==null||
        flowStrategyVO.getAlertPer()==null || flowStrategyVO.getType() == null || CollectionUtil.isEmpty(flowStrategyVO.getComList())
        ||CollectionUtil.isEmpty(flowStrategyVO.getWarnList())||CollectionUtil.isEmpty(flowStrategyVO.getAlertList())){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }

        try {
            String res = flowStrategyService.addStrategy(flowStrategyVO);
            if("新增成功".equals(res)){
                userLog.getLog("新增流量策略", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                return PubUtils.assignmentResult(null,200,res);
            }else {
                userLog.getLog("新增流量策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                return PubUtils.assignmentResult(null,500,res);
            }
        }catch (Exception e){
            userLog.getLog("新增流量策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null,500,"新增异常");
        }

    }

   @PostMapping("updateStrategy")
    public HttpResult updateStrategy(HttpServletRequest request, @RequestBody FlowStrategyVO flowStrategyVO) {
       if(PubUtils.isBlank(flowStrategyVO.getFsIndex())||PubUtils.isBlank(flowStrategyVO.getName())||
               flowStrategyVO.getComPer()==null || flowStrategyVO.getWarnPer()==null||
               flowStrategyVO.getAlertPer()==null || flowStrategyVO.getType() == null ||CollectionUtil.isEmpty(flowStrategyVO.getComList())
               ||CollectionUtil.isEmpty(flowStrategyVO.getWarnList())||CollectionUtil.isEmpty(flowStrategyVO.getAlertList())){
           return PubUtils.assignmentResult(null,500,"参数不可为空");
       }
       try {
           String res = flowStrategyService.updateStrategy(flowStrategyVO);
           if("操作成功".equals(res)){
               String r = flowStrategyService.addFunTran(flowStrategyVO);
               if("操作成功".equals(r)){
                   userLog.getLog("修改流量策略", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                   return PubUtils.assignmentResult(null,200,res);
               }else {
                   userLog.getLog("修改流量策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
                   return PubUtils.assignmentResult(null,500,"更新异常");
               }
           }else {
               userLog.getLog("修改流量策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
               return PubUtils.assignmentResult(null,500,res);
           }
       }catch (Exception e){
           userLog.getLog("修改流量策略", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
           return PubUtils.assignmentResult(null,500,"更新异常");
       }

    }

    @GetMapping("delStrategy")
    public HttpResult delStrategy(HttpServletRequest request, String fsIndex) {
        if(PubUtils.isBlank(fsIndex)){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }

        Integer res=  flowStrategyService.delStrategy(fsIndex);
        if(res == 1){
            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,200,"删除失败");
        }
    }

    @GetMapping("getStrategy")
    public HttpResult getStrategy(String fsIndex) {
        if(PubUtils.isBlank(fsIndex)){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }
        FlowStrategyVO flowStrategyVO = flowStrategyService.getStrategy(fsIndex);
        if(flowStrategyVO == null){
            return PubUtils.assignmentResult(null,500,"获取失败");
        }else {
            return PubUtils.assignmentResult(flowStrategyVO,200,"获取成功");
        }
    }

    /**
    * @Author chendaofei
    * @Description 获取流量策略列表
    * @Date 10:31 2020/10/15
    **/
    @GetMapping("getStrategyList")
    public HttpResult getStrategyList() {
        List<FlowStrategyVO> list =  flowStrategyService.getStrategyList();
        return PubUtils.assignmentResult(list,200,"获取成功");
    }

    /**
     * @Author chendaofei
     * @Description 获取所有装置功能
     * @Date 10:14 2020/8/11
     **/
    @GetMapping("getDic")
    public  HttpResult getDic(){
        List<DicData> list =  flowStrategyService.getTartgetTypeForDic();
        return PubUtils.assignmentResult(list,200,"获取成功");
    }

    /**
     * @Author chendaofei
     * @Description
     * @Date 16:57 2020/8/20
     **/
    @GetMapping("getFun")
    public HttpResult getFun(String fsdIndex){
        if(PubUtils.isBlank(fsdIndex)){
            return PubUtils.assignmentResult(null,500,"参数错误");
        }
        List<String> f = flowStrategyService.getFun(fsdIndex);
            return PubUtils.assignmentResult(f,200,"查询成功");
    }




}
