package com.huimv.video.dhicc.controller.ClientController;

import com.alibaba.fastjson.JSON;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.DefaultClient;
import com.dahuatech.icc.oauth.http.IClient;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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


@RestController
@RequestMapping("car")
public class ClientCarManageController {

    //列表车辆
    @RequestMapping("/geCarList")
    public GeneralResponse geCarList(@RequestParam(name = "farmId") Integer farmId,
                                     @RequestParam(name = "pageNum") Integer pageNum,
                                     @RequestParam(name = "pageSize") Integer pageSize
    ) throws ClientException {
        if (farmId != 1) {
            //返回为空数据
            return null;
        }
        Long aLong = Long.valueOf(String.valueOf((new Date()).getTime()));

        String URL = "/evo-apigw/ipms/car/list?";  // 人员列表的接口
        IClient iClient = new DefaultClient();
        System.out.println("开始执行");
        GeneralRequest generalRequest = new GeneralRequest(URL + "pageNum=" + pageNum + "&pageSize=" + pageSize + "&_=" + aLong, Method.GET);
        generalRequest.header("Content-Type", " application/json");
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        System.out.println("执行结束");
        return generalResponse;
    }


    //添加基础车辆
    @RequestMapping("/addCarBase")
    public GeneralResponse addCarBase(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId,
                                      // @RequestParam(name = "carCode") String carCode,//车辆编号
                                      @RequestParam(name = "carNum") String carNum //车牌号
    ) throws ClientException {
        if (farmId != 1) {
            return null;
        }

        String URL = "/evo-apigw/ipms/car/add";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        System.out.println("开始执行");
        Map<String, Object> value = new HashMap<>();
        value.put("carCode", (int) ((Math.random() * 9 + 1) * 10000));
        value.put("carNum", carNum);
        value.put("carNumColor", 0);
        value.put("carType", 1);
        value.put("carBrand", -1);
        value.put("carColor", 0);
        value.put("listType", 0);
        value.put("parkType", 1);
        value.put("owner", null);
        value.put("ownerId", null);
        value.put("parkinglot", null);
        value.put("memo", null);
        value.put("deptName", "技术部门");
        value.put("deptId", "2");
        value.put("parkinglotCode", "1");
        value.put("noAllowEditParkingLot", new ArrayList<>());
        List<Map> a = new ArrayList<>();
        Map<String, Object> value1 = new HashMap<>();
        value1.put("code", "0");
        value1.put("name", "未使用");
        Map<String, Object> value2 = new HashMap<>();
        value2.put("code", "1");
        value2.put("name", "使用");
        a.add(value1);
        a.add(value2);
        value.put("carStatusList", a);
        generalRequest.body(JSON.toJSONString(value));
        System.out.println(JSON.toJSONString(value));
        //这种已经在配置文件里面安排了账号ip以及密码
        generalRequest.header("Content-Type", " application/json");
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        return generalResponse;
    }


    //删除基础车辆
    @RequestMapping("/deleteFromCarBase")
    public GeneralResponse deleteFromCarBase(@RequestBody Map<String, Object> params
    ) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        if (farmId != 1) {
            return null;
        }
        List<String> carIdList = (List<String>) params.get("carIdList");
        System.out.println(carIdList);

        String URL = "/evo-apigw/ipms/car/delete/batch";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        System.out.println("开始执行");
        generalRequest.body(JSON.toJSONString(carIdList));
        System.out.println(JSON.toJSONString(carIdList));
        //这种已经在配置文件里面安排了账号ip以及密码
        generalRequest.header("Content-Type", " application/json");
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        return generalResponse;
    }


    //白名单列表
    @RequestMapping("/ShowCarWhiteList")
    public GeneralResponse ShowCarWhiteList(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId,
                                            @RequestParam(name = "pageNum") String pageNum,//车辆编号
                                            @RequestParam(name = "pageSize") String pageSize //车牌号
    ) throws ClientException {
        if (farmId != 1) {
            return null;
        }
        String timestamp = String.valueOf((new Date()).getTime());
        Long aLong = Long.valueOf(timestamp);
        String Url = "/evo-apigw/ipms/carWhiteList/sendCars?pageNum=" + pageNum + "&pageSize=" + pageSize + "&_=" + aLong;
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(Url, Method.GET);
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        return generalResponse;
    }


    //黑名单列表
    @RequestMapping("/ShowCarBlackList")
    public GeneralResponse ShowCarBlackList(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId,
                                            @RequestParam(name = "pageNum") String pageNum,//车辆编号
                                            @RequestParam(name = "pageSize") String pageSize //车牌号
    ) throws ClientException {
        if (farmId != 1) {
            return null;
        }
        String timestamp = String.valueOf((new Date()).getTime());
        Long aLong = Long.valueOf(timestamp);
        String Url = "/evo-apigw/ipms/carBlackList/sendCars?pageNum=" + pageNum + "&pageSize=" + pageSize + "&_=" + aLong;
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(Url, Method.GET);
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        return generalResponse;
    }


    //为添加到任何名单  //draw=4是白名单    draw=5是黑名单
    @RequestMapping("/ShowUnSendCar")
    public GeneralResponse ShowUnSendCar(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId,
                                         @RequestParam(name = "pageNum") String pageNum,//车辆编号
                                         @RequestParam(name = "draw") Integer draw,//车辆编号
                                         @RequestParam(name = "pageSize") String pageSize //车牌号
    ) throws ClientException {
        if (farmId != 1) {
            return null;
        }
        String timestamp = String.valueOf((new Date()).getTime());
        Long aLong = Long.valueOf(timestamp);
        String Url = "/evo-apigw/ipms/carBlackList/unSendCars?pageNum=" + pageNum + "&draw=" + draw + "&pageSize=" + pageSize + "&pageSize=" + pageSize + "&_=" + aLong;
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(Url, Method.GET);
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        return generalResponse;
    }


    //添加车辆进黑名单列表
    @RequestMapping("/addCarBlackList")
    public GeneralResponse addCarBlackList(@RequestBody Map<String, Object> params) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        if (farmId != 1) {
            return null;
        }
        List<String> carIdList = (List<String>) params.get("carIdList");
        String URL = "/evo-apigw/ipms/carBlackList/update";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);

        List<Object> a = new ArrayList<>();
        for (String s : carIdList) {
            Map<String, Object> value = new HashMap<>();
            value.put("carId", s);
            value.put("validStartDay", "2021-12-22");
            value.put("validEndDay", "2036-12-22");
            value.put("deviceCodes", "1002931,1002928");
            a.add(value);
        }
        generalRequest.body(JSON.toJSONString(a));
        System.out.println(JSON.toJSONString(a));
        //这种已经在配置文件里面安排了账号ip以及密码
        generalRequest.header("Content-Type", " application/json");
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        return generalResponse;
    }

    public static void main(String[] args) {
        System.out.println((int) ((Math.random() * 9 + 1) * 10000));
    }


    //添加车辆进白名单列表
    @RequestMapping("/addCarWhiteList")
    public GeneralResponse addCarWhiteList(@RequestBody Map<String, Object> params) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        if (farmId != 1) {
            return null;
        }
        List<String> carIdList = (List<String>) params.get("carIdList");
        String URL = "/evo-apigw/ipms/carWhiteList/update";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        List<Object> a = new ArrayList<>();

        for (String s : carIdList) {
            Map<String, Object> value = new HashMap<>();
            value.put("carId", s);
            value.put("validStartDay", "2021-12-22");
            value.put("validEndDay", "2036-12-22");
            value.put("deviceCodes", "1002931,1002928");
            a.add(value);
        }

        generalRequest.body(JSON.toJSONString(a));
        System.out.println(JSON.toJSONString(a));
        //这种已经在配置文件里面安排了账号ip以及密码
        generalRequest.header("Content-Type", "application/json;charset=UTF-8");
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        return generalResponse;
    }


    //从白名单删除
    @RequestMapping("/deleteFromCarWhiteList")
    public GeneralResponse deleteFromCarWhiteList(@RequestBody Map<String, Object> params
    ) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        if (farmId != 1) {
            return null;
        }
        List<String> carIdList = (List<String>) params.get("carIdList");
        System.out.println(carIdList);

        String URL = "/evo-apigw/ipms/carWhiteList/delete/batch";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        System.out.println("开始执行");
        generalRequest.body(JSON.toJSONString(carIdList));
        System.out.println(JSON.toJSONString(carIdList));
        //这种已经在配置文件里面安排了账号ip以及密码
        generalRequest.header("Content-Type", " application/json");
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        return generalResponse;
    }

    //从黑名单删除
    @RequestMapping("/deleteFromCarBlackList")
    public GeneralResponse deleteFromCarBlackList(@RequestBody Map<String, Object> params
    ) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        if (farmId != 1) {
            return null;
        }
        List<String> carIdList = (List<String>) params.get("carIdList");
        System.out.println(carIdList);
        String URL = "/evo-apigw/ipms/carBlackList/delete/batch";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        System.out.println("开始执行");
        generalRequest.body(JSON.toJSONString(carIdList));
        System.out.println(JSON.toJSONString(carIdList));
        //这种已经在配置文件里面安排了账号ip以及密码
        generalRequest.header("Content-Type", " application/json");
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        return generalResponse;
    }


    //从基础表中删除
    @RequestMapping("/deleteFromBaseCarList")
    public GeneralResponse deleteFromBaseCarList(@RequestBody Map<String, Object> params
    ) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        if (farmId != 1) {
            return null;
        }
        List<String> carIdList = (List<String>) params.get("carIdList");
        String URL = "/evo-apigw/ipms/car/delete/batch";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        System.out.println("开始执行");
        generalRequest.body(JSON.toJSONString(carIdList));
        System.out.println(JSON.toJSONString(carIdList));
        //这种已经在配置文件里面安排了账号ip以及密码
        generalRequest.header("Content-Type", " application/json");
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        return generalResponse;
    }


    //过车的数据

    /**
     * 保存ntamade
     */
    @RequestMapping("/get_car_record")
    public GeneralResponse getCarRecord(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId) throws ClientException {
        if (farmId != 1) {
            //返回为空数据
            return new GeneralResponse();
        }
        System.out.println(request.getRequestURL());
        System.out.println(request.getQueryString());
        String queryString = request.getQueryString();
        String URL = "/evo-apigw/ipms/carcapture/find/conditions?";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        System.out.println("开始执行");
        String newUrl = URL + queryString;
        System.out.println(newUrl);
        //这种已经在配置文件里面安排了账号ip以及密码    现在变了
        GeneralRequest generalRequest = new GeneralRequest(newUrl, Method.GET);
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        System.out.println("执行结束");
        return generalResponse;
    }


    @RequestMapping("/get_person_detail")
    public GeneralResponse getPersonDetail(@RequestBody Map<String, Object> params) throws ClientException {

        Integer farmId = (int) params.get("farmId");
        if (farmId != 1) {
            //返回为空数据
            return new GeneralResponse();
        }
        String URL = "/evo-apigw/evo-accesscontrol/${version}/card/accessControl/swingCardRecord/19241";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        System.out.println("开始执行");
        String URL_id = URL + params.get("id");
        //这种已经在配置文件里面安排了账号ip以及密码
        GeneralRequest generalRequest = new GeneralRequest(URL_id, Method.POST);
        generalRequest.header("Content-Type", " application/json");
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        System.out.println("执行结束");
        return generalResponse;
    }


}
