package com.mortals.xhx.module.hik.person.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.person.model.req.org.OrgListReq;
import com.mortals.xhx.module.hik.person.model.req.person.PersonReq;
import com.mortals.xhx.module.hik.person.model.rsp.org.OrgDataInfo;
import com.mortals.xhx.module.hik.person.model.rsp.person.PersonDataInfo;
import com.mortals.xhx.module.hik.person.service.IHikPersonService;
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("hikPersonService")
@Slf4j
public class HikPersonServiceImpl extends AbstractHikService implements IHikPersonService {

    @Override
    public Rest<PersonDataInfo> getPersonList(PersonReq personReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/person/personList";
        Map<String, String> path = new HashMap<>(2);
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(personReq), null, null, "application/json");
            HikApiRest<PersonDataInfo> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<PersonDataInfo>>() {
            });
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getData());
            } else {
                log.info("person error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("获取人员异常", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public String getPersonListTrans(PersonReq personReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/person/personList";
        Map<String, String> path = new HashMap<>(2);
        path.put(protocol, getCamsApi);
        try {
            return ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(personReq), null, null, "application/json");
        } catch (Exception e) {
            log.error("获取人员异常", e);
            return JSON.toJSONString(Rest.fail(e.getMessage()));
        }
    }

    @Override
    public Rest<OrgDataInfo> getOrgList(OrgListReq orgListReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/org/orgList";
        Map<String, String> path = new HashMap<>(2);
        path.put(protocol, getCamsApi);
        try {
            String respJson = ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(orgListReq), null, null, "application/json");
            HikApiRest<OrgDataInfo> rest = JSON.parseObject(respJson, new TypeReference<HikApiRest<OrgDataInfo>>() {
            });
            if ("0".equals(rest.getCode())) {
                return Rest.ok(rest.getData());
            } else {
                log.info("org list error resp=>{}", respJson);
                return Rest.fail(rest.getMsg());
            }
        } catch (Exception e) {
            log.error("org list error ", e);
            return Rest.fail(e.getMessage());
        }
    }

    @Override
    public String getOrgListTrans(OrgListReq orgListReq) {
        ArtemisConfig config = getArtemisConfig();
        String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/org/orgList";
        Map<String, String> path = new HashMap<>(2);
        path.put(protocol, getCamsApi);
        try {
            return ArtemisHttpUtil.doPostStringArtemis(config, path, JSON.toJSONString(orgListReq), null, null, "application/json");
        } catch (Exception e) {
            log.error("org list error ", e);
            return JSON.toJSONString(Rest.fail(e.getMessage()));
        }
    }
}
