package com.example.zxweb.utils;

import com.alibaba.fastjson.JSONObject;
import com.example.zxweb.common.constant.enums.RmsApiEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 访客机工具类
 * @Author ZhouWenTao
 * @Date 2023/9/8 13:18
 */
@Component
@Slf4j
public class RmsUtils {
    private static String TOKEN;
    private static JSONObject header=new JSONObject();
    private static String API_PREFIX="https://10.0.10.153:2443";
    static {
        //请求头
        header=new JSONObject();
        header.put("Content-Type","application/json");
        header.put("appCode","42142fd0jkbf4515853b7fcec64748f6");
        /*//header.put("devid","68EDA45C54F3");
        //header.put("devid","68EDA444D9DG");
        //68EDA444DODG

        //获取TOKEN
        getToken();
        header.put("token",TOKEN);*/
    }
    static List<String> devidList= Arrays.asList("68EDA45C54F3","68EDA444D9DG");

    public static void getToken() {
        JSONObject requestBody = JSONObject.parseObject("{\"email\":\"admin\", \"pswd\":\"cf79ae6addba60ad018347359bd144d2\"}");
        JSONObject responseBody = RestUtil.post(API_PREFIX+RmsApiEnum.getApiByText("系统登陆"), requestBody, header);
        log.error(responseBody.toJSONString());
        AssertUtils.notNull(responseBody,"获取TOKEN失败");
        String code = responseBody.getString("code");
        String msg = responseBody.getString("msg");
        AssertUtils.isTrue("0".equals(code),"获取TOKEN出错:"+msg);
        JSONObject content = responseBody.getJSONObject("content");
        TOKEN = content.getString("token");
        header.put("token",TOKEN);
    }

    /**
     * 访客查询
     * 本接口用于访客信息查询，查询出访客登记的所有信息，建议查询条件为当天时间+状态查询，不易跨月，多天的频繁调用，对服务压力大，查询结果为倒序排序。
     * @param requestBody {"starttime":"2022-11-09","status":"0","num":1,"pageNum":1,"endtime":"2022-11-09"}
     * @return {"code":0,"msg":"成功","pagecount":2,"pageNum":1,"content":[{"id":85,"rcode":"1486300085","parentid":null,"vid":88,"visitor":{"id":88,"name":"王麻子","sex":"男","nation":"汉","birthday":null,"idnum":"a543546aef4bdd46ac957554f3c4f8cd222498dd2c2d56d1a9e5939469930619","idnumAdd":"123456123456","idnumCipher":"04e08e6ebddced2dd94b588e039096a2111f8be98f9eb79e365a3443c697d699ac9f08b892821e2b3b4e22b54203084b4e111329918b810c2fc480637c830e2fe909a7b4e75c18c91da3a163441ea36123ca105526b68c11e570e6869bc4c6121825f116905de9107c1d7832b7","photo":null,"sitePhoto":null,"issuing":null,"validitydatestart":null,"validitydateend":null,"phone":"84537d95e88ce687e4efab251a746c7318dbe7cc2f6cecbb0840ce4889e7a2a5","phoneAdd":"13900074111","phoneCipher":"04d176b602a7ad1c6b9784adc7adef54e90c7e771f04aba9ca66c3cf3abfa0cc6163203fb4c1017381a7ccc6a396150af3070efa193cfc22d128294bc58a52705077cbba29dc322072e95815d5ed19e92ee7b2f2c0470e4e15ae0eb15fa8540df61b07dfd4a1256955bbd019","startTime":null,"endTime":null,"pinYin":"wmz","syncId":null,"companyCode":null,"isbindfinger":0,"isbindface":0,"wltPhoto":null,"tabTime":null,"uploadTime":null,"isVip":0,"sheetSize":null,"idphoto":null},"uid":20653,"user":{"id":20653,"nickname":"张三","userpinyin":null,"email":"abc10110","pswd":null,"terminalpswd":null,"createTime":null,"lastLoginTime":null,"status":1,"sex":1,"nation":null,"nations":null,"birthday":null,"address":null,"idNum":"1304331989111111","photo":"\\image\\idpic\\1304331989111111_20221108143849677.png","issuing":null,"validityDateStart":null,"validityDateEnd":null,"phone":"13931021490","dep":null,"depID":1049,"depSyncId":null,"depName":null,"telephone":"010123456","ecardNum":"12345678","ecardEndTime":"2042-11-30 00:00:00","pinYin":null,"syncId":"abc10110","companyNum":null,"sta":null,"imIP":null,"imTime":null,"entranceGuards":null,"isbindface":null,"visitable":null,"tabTime":null,"uploadTime":null,"idphoto":null,"plateNo":null,"plateTime":null,"plateIssue":null},"dept":{"id":1049,"value":"子级部门1","pinyin":"zjbm1","parentId":null,"syncId":"abc123","syncParentId":null,"companyNum":null,"sta":null,"visitable":null,"sortnum":null,"tabTime":null,"uploadTime":null},"createtime":"2022-11-09 13:44:46","starttime":"2022-11-09 13:44:00","endtime":"2022-11-09 23:59:00","logofftime":null,"status":0,"reasons":"访问","unit":"公司名称","num":1,"type":0,"isprintvoucher":0,"ispullcard":1,"cardtype":0,"cardnum":"1234512345","sitephoto":null,"headphoto":null,"vCar":{"id":180,"cardnum":"京A99999","cardcolor":null,"num":null,"arid":85,"companyCode":null,"tabTime":null,"uploadTime":null},"drList":[],"trList":[],"isUpload":0,"pageNum":null,"yyID":null,"companyCode":"000000","terminalCode":"1000002","terminalManagerId":1038,"zhuxiaoManagerId":null,"tabTime":"2022-11-09 13:44:46","uploadTime":null,"syncId":null,"tiwen":35.0,"terminalManagerName":null,"zhuxiaoManagerName":null,"zhuxiaoTerminalCode":null,"channels":"","comment":"","xckStatus":0,"xckvisitedArea":null,"xckPhone":null,"xckUpdateTime":null,"xckPhoto":null,"jkbPhoto":null,"healthStatus":null,"lastRNATime":null,"lastRNAResult":null,"travelStatus":null,"tel":null,"vcar":{"id":180,"cardnum":"京A99999","cardcolor":null,"num":null,"arid":85,"companyCode":null,"tabTime":null,"uploadTime":null}}],"success":true}
     */
    public static JSONObject fkterminalFindAccessRecord(JSONObject requestBody){
        JSONObject responseBody =null;
        for (String devid : devidList) {
            log.info("devid:"+devid);
            header.put("devid",devid);
            String starttime = requestBody.getString("starttime");//开始时间*
            String status = requestBody.getString("status");//状态：-1超时未注销、0登记中、1已注销3预约
            String pageNum = requestBody.getString("pageNum");//页数
            String num = requestBody.getString("num");//查询条数
            String endtime = requestBody.getString("endtime");//结束时间*
            AssertUtils.notEmpty(starttime,"请输入开始时间-[starttime]");
            AssertUtils.notEmpty(endtime,"请输入结束时间-[endtime]");
            responseBody = RestUtil.post(API_PREFIX+RmsApiEnum.getApiByText("访客查询"), requestBody, header);
            log.error(responseBody.toJSONString());
            AssertUtils.notNull(responseBody,"请求失败");
        }
        return responseBody;
    }

    /**
     * 访客预约
     * @param requestBody {"reason":"拜访","synId":"abc10110","visitorBeginTime":"2022-11-08 10:00:00","visitorEndTime":"2022-11-08 18:00:00","visitorCmp":"访客部","visitorIdNum":"1234567890123","visitorName":"访客","visitorPhone":"1300000000"}
     * @return {"code":0,"msg":"预约成功","pagecount":null,"pageNum":null,"content":null,"success":true}
     */
    public static JSONObject syncOrderrecord(JSONObject requestBody){
        getToken();
        JSONObject responseBody=null;
        for (String devid : devidList) {
            header.put("devid",devid);
            String reason = requestBody.getString("reason");//状态
            String synId = requestBody.getString("synId");//被访人ID*
            String visitorBeginTime = requestBody.getString("visitorBeginTime");//来访时间 yyyy-MM-dd HH:mm:ss yyyy*
            String visitorEndTime = requestBody.getString("visitorEndTime");//结束时间 yyyy-MM-dd HH:mm:ss*
            String visitorCmp = requestBody.getString("visitorCmp");//访客单位
            String visitorIdNum = requestBody.getString("visitorIdNum");//访客证件号码*
            String visitorName = requestBody.getString("visitorName");//访客姓名*
            String visitorPhone = requestBody.getString("visitorPhone");//访客手机号
            AssertUtils.notEmpty(synId,"请输入被访客ID-[synId]");
            AssertUtils.notEmpty(visitorBeginTime,"请输入来访时间-[visitorBeginTime]");
            AssertUtils.notEmpty(visitorEndTime,"请输入结束时间-[visitorEndTime]");
            AssertUtils.notEmpty(visitorIdNum,"请输入访客证件号-[visitorIdNum]");
            AssertUtils.notEmpty(visitorName,"请输入访客姓名-[visitorName]");
            responseBody = RestUtil.post(API_PREFIX+RmsApiEnum.getApiByText("访客预约"), requestBody, header);
            log.error(responseBody.toJSONString());
            AssertUtils.notNull(responseBody,"请求接口异常");
        }
        String devid = requestBody.getString("devid");
        System.out.println(header.toJSONString());

        //Integer code = responseBody.getInteger("code");
        //AssertUtils.isTrue(code==0,"请求访客预约接口异常:"+requestBody.getOrDefault("msg",""));
        return responseBody;
    }

    /**
     * 访客注销
     * 本接口用于主动调用访客系统注销，针对于发卡用户使用，比如：访客登记发放门禁卡，出门注销时可调用此接口
     * @param requestBody {"cardNum":"1234567888"}
     * @return {"code":0,"msg":"成功","pagecount":null,"pageNum":null,"content":null,"success":true}
     */
    public static JSONObject fkterminalRecoverCard(JSONObject requestBody) {
        getToken();
        System.out.println(header.toJSONString());
        JSONObject responseBody=null;
        for (String devid : devidList) {
            header.put("devid",devid);
            String cardNum = requestBody.getString("cardNum");
            AssertUtils.notEmpty(cardNum,"清输入访客证件号-[cardNum]");
            responseBody = RestUtil.post(API_PREFIX+RmsApiEnum.getApiByText("访客注销")+"/"+cardNum, null, header);
            log.error(responseBody.toJSONString());
            AssertUtils.notNull(responseBody,"请求失败");
        }
        //Integer code = responseBody.getInteger("code");
        //AssertUtils.isTrue(0==code,"请求异常:"+responseBody.getString("msg"));
        return responseBody;
    }
}
