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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huimv.video.dhicc.result.R;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo02;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo03;
import com.huimv.video.dhicc.test.request.XunZhengRequest.Cn.XunZhengCountRequestCn;
import com.huimv.video.dhicc.test.request.XunZhengRequest.Cn.XunZhengRequestCn;
import com.huimv.video.dhicc.test.request.XunZhengRequest.Hn.XunZhengCountRequestHn;
import com.huimv.video.dhicc.test.request.XunZhengRequest.Sg.XunZhengCountRequestSg;
import com.huimv.video.dhicc.test.request.XunZhengRequest.Hn.XunZhengRequestHn;
import com.huimv.video.dhicc.test.request.XunZhengRequest.Sg.XunZhengRequestSg;
import com.huimv.video.dhicc.test.token.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("event")
public class ClientXunZhengControllerTest {
    @Autowired
    private OauthConfigBaseInfo oauthConfigBaseInfo;
    @Autowired
    private OauthConfigBaseInfo02 oauthConfigBaseInfo02;
    @Autowired
    private OauthConfigBaseInfo03 oauthConfigBaseInfo03;

    @RequestMapping
    public R xunZhengEvent(@RequestBody XunZhengRequestSg xunZhengRequestSg){
        Integer farmId = xunZhengRequestSg.getFarmId();
        if (farmId == 1){
            System.out.println("曙光");
            XunZhengRequestSg requestSg = new XunZhengRequestSg(oauthConfigBaseInfo);
            Token token = requestSg.getToken();

            System.out.println(token);
            System.out.println(token.getAccess_token());
            requestSg.setEventType(xunZhengRequestSg.getEventType());
            requestSg.setPageNum(xunZhengRequestSg.getPageNum());
            requestSg.setPageSize(xunZhengRequestSg.getPageSize());
            requestSg.setAlarmStartDateString(xunZhengRequestSg.getAlarmStartDateString());
            requestSg.setAlarmEndDateString(xunZhengRequestSg.getAlarmEndDateString());
            String result = requestSg.doAction();
            XunZhengCountRequestSg countRequestSg = new XunZhengCountRequestSg(oauthConfigBaseInfo);
            countRequestSg.setEventType(xunZhengRequestSg.getEventType());
            countRequestSg.setPageNum(xunZhengRequestSg.getPageNum());
            countRequestSg.setPageSize(xunZhengRequestSg.getPageSize());
            countRequestSg.setAlarmEndDateString(xunZhengRequestSg.getAlarmEndDateString());
            countRequestSg.setAlarmStartDateString(xunZhengRequestSg.getAlarmStartDateString());
            JSONObject jsonObject = JSON.parseObject(countRequestSg.doAction());
            String data = String.valueOf(jsonObject.get("data")) ;
            Object count = JSON.parseObject(data).get("value");
            return R.ok("请求成功").put("data",result).put("total",count);
        }
        else if (farmId == 22 ){
            System.out.println("海宁");
            XunZhengRequestHn requestHn = new XunZhengRequestHn(oauthConfigBaseInfo02);
            requestHn.setEventType(xunZhengRequestSg.getEventType());
            requestHn.setPageNum(xunZhengRequestSg.getPageNum());
            requestHn.setPageSize(xunZhengRequestSg.getPageSize());
            requestHn.setAlarmStartDateString(xunZhengRequestSg.getAlarmStartDateString());
            requestHn.setAlarmEndDateString(xunZhengRequestSg.getAlarmEndDateString());
            String result = requestHn.doAction();
            XunZhengCountRequestHn countRequestHn = new XunZhengCountRequestHn(oauthConfigBaseInfo02);
            countRequestHn.setEventType(xunZhengRequestSg.getEventType());
            countRequestHn.setPageNum(xunZhengRequestSg.getPageNum());
            countRequestHn.setPageSize(xunZhengRequestSg.getPageSize());
            countRequestHn.setAlarmEndDateString(xunZhengRequestSg.getAlarmEndDateString());
            countRequestHn.setAlarmStartDateString(xunZhengRequestSg.getAlarmStartDateString());
            JSONObject jsonObject = JSON.parseObject(countRequestHn.doAction());
            String data = String.valueOf(jsonObject.get("data")) ;
            Object count = JSON.parseObject(data).get("value");
            return R.ok("请求成功").put("data",result).put("total",count);
        }else if (farmId == 21){
            System.out.println("苍南");
            XunZhengRequestCn requestCn = new XunZhengRequestCn(oauthConfigBaseInfo03);
            requestCn.setEventType(xunZhengRequestSg.getEventType());
            requestCn.setPageNum(xunZhengRequestSg.getPageNum());
            requestCn.setPageSize(xunZhengRequestSg.getPageSize());
            requestCn.setAlarmStartDateString(xunZhengRequestSg.getAlarmStartDateString());
            requestCn.setAlarmEndDateString(xunZhengRequestSg.getAlarmEndDateString());
            String result = requestCn.doAction();
            XunZhengCountRequestCn countRequestCn = new XunZhengCountRequestCn(oauthConfigBaseInfo03);
            countRequestCn.setEventType(xunZhengRequestSg.getEventType());
            countRequestCn.setPageNum(xunZhengRequestSg.getPageNum());
            countRequestCn.setPageSize(xunZhengRequestSg.getPageSize());
            countRequestCn.setAlarmEndDateString(xunZhengRequestSg.getAlarmEndDateString());
            countRequestCn.setAlarmStartDateString(xunZhengRequestSg.getAlarmStartDateString());
            JSONObject jsonObject = JSON.parseObject(countRequestCn.doAction());
            String data = String.valueOf(jsonObject.get("data")) ;
            Object count = JSON.parseObject(data).get("value");
            return R.ok("请求成功").put("data",result).put("total",count);
        }else {
            return R.error();
        }
    }

}
