package com.mortals.xhx.module.hik.door.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.module.hik.AbstractHikService;
import com.mortals.xhx.module.hik.HikApiRest;
import com.mortals.xhx.module.hik.door.model.req.door.DoorEventReq;
import com.mortals.xhx.module.hik.door.model.rsp.door.DoorEventDataInfo;
import com.mortals.xhx.module.hik.door.service.IHikDoorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 海康人员接口类
 *
 * @author: zxfei
 * @date: 2023/4/12 17:51
 */
@Service("hikDoorService")
@Slf4j
public class HikDoorServiceImpl extends AbstractHikService implements IHikDoorService {


    @Override
    public Rest<DoorEventDataInfo> getDoorEvents(DoorEventReq doorEventReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/acs/v2/door/events";
        Map<String, String> path = new HashMap<>(2);
        path.put(protocol, getCamsApi);
        try {
            log.info("door events req=>{}", JSON.toJSONString(doorEventReq));
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(doorEventReq), null, null, "application/json");
             //log.info("door events error resp=>{}", respJson);
            HikApiRest<DoorEventDataInfo> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<DoorEventDataInfo>>() {
            });
            log.info("door events  resp=>{}", rest.getCode());
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getData());
            } else {
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("获取门禁事件异常", e);
            return Rest.fail(e.getMessage());
        }
    }
}
