package com.dcsoft.system.uniubi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dcsoft.common.core.constant.CacheConstants;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.system.domain.SysEmpowerRecord;
import com.dcsoft.system.domain.SysEquipment;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.domain.SysRule;
import com.dcsoft.system.service.ISysEmpowerRecordService;
import com.dcsoft.system.service.ISysEquipmentService;
import com.dcsoft.system.service.ISysRuleService;
import com.dcsoft.system.uniubi.service.ISysApiService;
import com.dcsoft.system.utils.HttpUtil;
import com.dcsoft.system.utils.ImageToBase64Utils;
import com.dcsoft.system.visitor.domain.VisVisitorExamine;
import com.dcsoft.system.visitor.domain.Visitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.dcsoft.common.redis.service.RedisService;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.*;

/**
 * 用户管理 服务层处理
 *
 * @author xueyi
 */
@Service
public class SysApiServiceImpl implements ISysApiService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private ISysEquipmentService equipmentService;

    @Autowired
    private ISysEmpowerRecordService empowerRecordService;

    @Autowired
    private ISysRuleService ruleService;


    @Value("${uni.url}")
    public String uniUrl;
    //** 资源映射路径 前缀 *//*
    @Value("${uni.appKey}")
    public String appKey;
    @Value("${uni.appSecret}")
    public String appSecret;

    @Value("${uni.projectGuid}")
    public String projectGuid;

    //获取token值
    @Override
    public String getToken() {
        //通过缓存获取，如果没有就重新获取，时间间隔为20小时
        //token 有效期为 24 小时，过期后需再次获取；建议定时更新 token，比如每 20 小时更新一次
        //所有请求 token 及其他参数都写在 header 中
        String verifyKey = CacheConstants.AUTH_KEY + appKey;
        String token = redisService.getCacheObject(verifyKey);
        if(StringUtils.isEmpty(token)){//重新获取token
            String url=uniUrl+"/v1/"+projectGuid+"/auth?";
            Map<String, Object> headMap=new HashMap<String, Object>();
            headMap.put("appKey",appKey);
            String timestamp=new Date().getTime()+"";
            headMap.put("timestamp",timestamp);
            String sign= EncodeByMD5(appKey+timestamp+appSecret).toLowerCase();
            headMap.put("sign",sign);
            String response= HttpUtil.getUrl(url,headMap,projectGuid);
            JSONObject json= JSONObject.parseObject(response);
            token=json.get("data").toString();
            redisService.setCacheObject(verifyKey,token);
            redisService.expire(verifyKey,20*60*60);
        }
        return token;
    }

    //人员接口--识别主体创建
    public String admitCreate(SysPeople people){
        String url=uniUrl+"/v2/admit/create?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("name",people.getName());
        paramMap.put("phone",people.getPhone());
        paramMap.put("idCardNo",people.getIdcard());
        paramMap.put("cardNo",people.getDoorNo());
        paramMap.put("tag",people.getRemark());
        //获取识别主体admitGuid 人员 guid需保存
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //人员接口--更新识别对象的主体信息
    public String admitUpdate(SysPeople people){
        String url=uniUrl+"/v2/admit/update?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("admitGuid",people.getGuid());
        paramMap.put("name",people.getName());
        paramMap.put("cardNo",people.getDoorNo());
        paramMap.put("phone",people.getPhone());
        paramMap.put("idCardNo",people.getIdcard());
        paramMap.put("tag",people.getRemark());
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //人员接口--删除识别对象的主体信息
    public String admitDelete(String admitGuids){
        String url=uniUrl+"/v2/admit/delete?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("admitGuids",admitGuids);
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //人像注册--上传人像照片
    public String faceRegister(SysPeople people){
        String url=uniUrl+"/v2/face/register?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        String base64= ImageToBase64Utils.getImgStrToBase64(people.getAvatar());
        paramMap.put("base64",base64);
        paramMap.put("admitGuid",people.getGuid());
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //人像注册--删除人像照片
    public String faceDelete(SysPeople people){
        String url=uniUrl+"/v2/face/delete?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("admitGuid",people.getGuid());
        paramMap.put("faceGuid",people.getFaceGuid());
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //设备信息-设备添加
    @Override
    public String deviceCreate(SysEquipment equipment) {
        String url=uniUrl+"/v2/device/create";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("name",equipment.getName());
        paramMap.put("source","000000");//设备来源-Uface 系列
        paramMap.put("deviceNo",equipment.getSequence());
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //设备信息-设备修改
    @Override
    public String deviceUpdate(SysEquipment equipment) {
        String url=uniUrl+"/v2/device/update?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("name",equipment.getName());
        paramMap.put("source","000000");//设备来源-Uface 系列
        paramMap.put("deviceNo",equipment.getSequence());
        paramMap.put("addition","{'recType':'1'}");
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //设备信息-删除设备信息
    @Override
    public String deviceDelete(SysEquipment equipment) {
        String url=uniUrl+"/v2/device/delete?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("source","000000");//设备来源-Uface 系列
        paramMap.put("deviceNo",equipment.getSequence());
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //设备信息-设备配置查询
    @Override
    public String deviceSettingQuery(SysEquipment equipment) {
        String url=uniUrl+"/v2/device/setting/query";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("source","000000");//设备来源-Uface 系列
        paramMap.put("deviceNo",equipment.getSequence());
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //设备信息-远程配置设备参数
    @Override
    public String deviceSettingUpdate(SysEquipment equipment) {
        String url=uniUrl+"/v2/device/setting/update?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("source","000000");//设备来源-Uface 系列
        paramMap.put("deviceNo",equipment.getSequence());
        //不对接内容
        SysEquipment equipments=equipment;
        equipments.setId(null);
        equipments.setProductId(null);
        equipments.setSequence(null);
        equipments.setIp(null);
        equipments.setPassword(null);
        equipments.setPointId(null);
        equipments.setRemark(null);
        equipments.setState(null);
        equipments.setUpdateBy(null);
        equipments.setCreateBy(null);
        Map map = JSON.parseObject(JSON.toJSONString(equipments), Map.class);
        JSONArray objs= new JSONArray();
        Set<Map.Entry<Integer, String>> entrys = map.entrySet(); //此行可省略，直接将map.entrySet()写在for-each循环的条件中
        for(Map.Entry<Integer, String> entry:entrys){
            JSONObject jsons=new JSONObject();
            jsons.put("key",entry.getKey()+"");
            jsons.put("value",entry.getValue());
            objs.add(jsons);
        }
        paramMap.put("deviceConfigs",objs);
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    @Override
    public String deviceOnlineState(List<SysEquipment> equipment) {
        String url=uniUrl+"/v2/device/onlineState?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("source","000000");//设备来源-Uface 系列
        String eqs="";
        for(SysEquipment eq:equipment){
            eqs+=eq.getSequence()+",";
        }
        paramMap.put("deviceNos",eqs);
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    @Override
    public String deviceCommand(SysEquipment equipment, int operateType) {
        String url=uniUrl+"/v2/device/command?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("source","000000");//设备来源-Uface 系列
        paramMap.put("deviceNo",equipment.getSequence());
        paramMap.put("operateType",operateType);//1 重启 2：重置 3：启用 4 禁用
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    @Override
    public String deviceInteractive(SysEquipment equipment) {
        String url=uniUrl+"/v2/device/interactive?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("type","1");//远程开门
        paramMap.put("deviceNo",equipment.getSequence());
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //人像门禁服务-设备授权识别主体
    @Override
    public String authDevice(SysRule rule, SysPeople people) {
        String response="";
        String url=uniUrl+"/v2/auth/device?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        String[] pointIds=rule.getPointId().split(",");
        List<SysEquipment> equipList=new ArrayList<>();
        if(pointIds!=null){
            for(String point:pointIds){
                SysEquipment equipment=new SysEquipment();
                equipment.setPointId(Long.parseLong(point));
                List<SysEquipment> equipmentDtoList=equipmentService.selectSysEquipmentList(equipment);
                equipList.addAll(equipmentDtoList);
            }
        }
        //通过规则获取绑定设备信息
        if(equipList!=null){
            for(SysEquipment e:equipList){
                Map<String, Object> paramMap=new HashMap<String, Object>();
                paramMap.put("deviceNo",e.getSequence());
                paramMap.put("admitGuids",people.getGuid());//设备来源-Uface 系列
                paramMap.put("type",rule.getType());
                paramMap.put("passTime",rule.getAdmittanceStart()+","+rule.getAdmittanceEnd());
                paramMap.put("permissionTime",rule.getStartTime().getTime()+","+rule.getEndTime().getTime());
                JSONObject jsons=new JSONObject();
                if(rule.getPermission().contains("idCardFacePermission")){
                    jsons.put("idCardFacePermission",2);
                }else{
                    jsons.put("idCardFacePermission",1);
                }
                if(rule.getPermission().contains("passwordPermission")){
                    jsons.put("passwordPermission",2);
                }else{
                    jsons.put("passwordPermission",1);
                }
                if(rule.getPermission().contains("facePermission")){
                    jsons.put("facePermission",2);
                }else{
                    jsons.put("facePermission",1);
                }
                if(rule.getPermission().contains("idCardPermission")){
                    jsons.put("idCardPermission",2);
                }else{
                    jsons.put("idCardPermission",1);
                }
                if(rule.getPermission().contains("faceAndCardPermission")){
                    jsons.put("faceAndCardPermission",2);
                }else{
                    jsons.put("faceAndCardPermission",1);
                }
                if(rule.getPermission().contains("QRCodePermission")){
                    jsons.put("QRCodePermission",2);
                }else{
                    jsons.put("QRCodePermission",1);
                }
                paramMap.put("permission",jsons);
                response= HttpUtil.postData(url,headMap,paramMap);
                JSONObject json= JSONObject.parseObject(response);
                if("1".equals(json.get("result")+"")){//授权记录保存
                    SysEmpowerRecord manageRecordDto=new SysEmpowerRecord();
                    manageRecordDto.setPeopleId(people.getId());
                    manageRecordDto.setEmpower(rule.getType());
                    manageRecordDto.setEquipmentId(e.getId());
                    manageRecordDto.setStatus("0");
                    manageRecordDto.setInfoDown("0");
                    manageRecordDto.setRuleId(rule.getId());
                    empowerRecordService.insertSysEmpowerRecord(manageRecordDto);
                }else{
                    SysEmpowerRecord manageRecordDto=new SysEmpowerRecord();
                    manageRecordDto.setPeopleId(people.getId());
                    manageRecordDto.setEmpower(rule.getType());
                    manageRecordDto.setEquipmentId(e.getId());
                    manageRecordDto.setStatus("0");
                    manageRecordDto.setInfoDown("1");
                    manageRecordDto.setRuleId(rule.getId());
                    empowerRecordService.insertSysEmpowerRecord(manageRecordDto);
                }
            }
        }
        return response;
    }

    //人像门禁服务-设备授权识别主体
    @Override
    public String authDevice(SysRule rule, SysPeople people, SysEquipment equipment) {
        String url=uniUrl+"/v2/auth/device?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("deviceNo",equipment.getSequence());
        paramMap.put("admitGuids",people.getGuid());//设备来源-Uface 系列
        paramMap.put("type",rule.getType());
        paramMap.put("passTime",rule.getAdmittanceStart()+","+rule.getAdmittanceEnd());
        paramMap.put("permissionTime",rule.getStartTime().getTime()+","+rule.getEndTime().getTime());
        JSONObject jsons=new JSONObject();
        if(rule.getPermission().contains("idCardFacePermission")){
            jsons.put("idCardFacePermission",2);
        }else{
            jsons.put("idCardFacePermission",1);
        }
        if(rule.getPermission().contains("passwordPermission")){
            jsons.put("passwordPermission",2);
        }else{
            jsons.put("passwordPermission",1);
        }
        if(rule.getPermission().contains("facePermission")){
            jsons.put("facePermission",2);
        }else{
            jsons.put("facePermission",1);
        }
        if(rule.getPermission().contains("idCardPermission")){
            jsons.put("idCardPermission",2);
        }else{
            jsons.put("idCardPermission",1);
        }
        if(rule.getPermission().contains("faceAndCardPermission")){
            jsons.put("faceAndCardPermission",2);
        }else{
            jsons.put("faceAndCardPermission",1);
        }
        if(rule.getPermission().contains("QRCodePermission")){
            jsons.put("QRCodePermission",2);
        }else{
            jsons.put("QRCodePermission",1);
        }
        paramMap.put("permission",jsons);
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //人像门禁服务-设备修改识别主体权限
    @Override
    public String authDeviceUpdate(SysRule rule, SysPeople people) {
        return null;
    }

    //人像门禁服务-设备销权识别主体
    @Override
    public String authDeviceRevoke(SysEquipment equipment, SysPeople people) {
        String url=uniUrl+"/v2/auth/device/revoke?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("deviceNo",equipment.getSequence());
        paramMap.put("admitGuids",people.getGuid());//设备来源-Uface 系列
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //人像门禁服务-授权列表查询
    @Override
    public String sceneAuth() {
        return null;
    }


    //访客人员接口--识别主体创建
    public String admitCreate(Visitor visitor){
        String url=uniUrl+"/v2/admit/create?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("name",visitor.getName());
        paramMap.put("phone",visitor.getPhone());
        paramMap.put("idCardNo",visitor.getIdcard());
        paramMap.put("tag",visitor.getRemark());
        //获取识别主体admitGuid 人员 guid需保存
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //访客人员接口--更新识别对象的主体信息
    public String admitUpdate(Visitor visitor){
        String url=uniUrl+"/v2/admit/update?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("admitGuid",visitor.getGuid());
        paramMap.put("name",visitor.getName());
        paramMap.put("phone",visitor.getPhone());
        paramMap.put("idCardNo",visitor.getIdcard());
        paramMap.put("tag",visitor.getRemark());
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //访客人像注册--上传人像照片
    public String faceRegister(Visitor visitor){
        String url=uniUrl+"/v2/face/register?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        String base64= ImageToBase64Utils.getImgStrToBase64(visitor.getAvatar());
        paramMap.put("base64",base64);
        paramMap.put("admitGuid",visitor.getGuid());
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }

    //访客人像注册--删除人像照片
    public String faceDelete(Visitor visitor){
        String url=uniUrl+"/v2/face/delete?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        paramMap.put("admitGuid",visitor.getGuid());
        paramMap.put("faceGuid",visitor.getFaceGuid());
        String response= HttpUtil.postData(url,headMap,paramMap);
        return response;
    }



    /**
     * 对字符串进行32位MD5加密
     * @param str
     * @return
     */
    public static String EncodeByMD5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes("UTF-8"));
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            String md5=new BigInteger(1, md.digest()).toString(16);
            //BigInteger会把0省略掉，需补全至32位
            return fillMD5(md5);
        } catch (Exception e) {
            throw new RuntimeException("MD5加密错误:"+e.getMessage(),e);
        }
    }

    @Override
    public String authDeviceExamine(Visitor visitor) {
        String response="";
        String url=uniUrl+"/v2/auth/device?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        VisVisitorExamine examine= visitor.getExamine();
        //通过访问人员的地点
        SysRule rule=ruleService.selectSysRuleById(examine.getRuleId());
        String[] pointIds=rule.getPointId().split(",");
        List<SysEquipment> equipList=new ArrayList<>();
        if(pointIds!=null){
            for(String point:pointIds){
                SysEquipment equipment=new SysEquipment();
                equipment.setPointId(Long.parseLong(point));
                List<SysEquipment> equipmentDtoList=equipmentService.selectSysEquipmentList(equipment);
                equipList.addAll(equipmentDtoList);
            }
        }
        //授权人员
        String admitGuids="";
        if(StringUtils.isNotEmpty(visitor.getGuid())){
            admitGuids+=visitor.getGuid()+",";
        }
        if(visitor.getItemList()!=null){
            for(Visitor item:visitor.getItemList()){
                admitGuids+= item.getGuid()+",";
            }
        }
        if(!"".equals(admitGuids)){
            admitGuids=admitGuids.substring(0,admitGuids.length()-1);
        }

        //通过规则获取绑定设备信息
        if(equipList!=null){
            for(SysEquipment e:equipList){
                Map<String, Object> paramMap=new HashMap<String, Object>();
                paramMap.put("deviceNo",e.getSequence());
                paramMap.put("admitGuids",admitGuids);
                Long endTime= visitor.getEndTime().getTime()+24*60*60*1000;
                paramMap.put("permissionTime",visitor.getStartTime().getTime()+","+endTime);
                paramMap.put("type",rule.getType());
                paramMap.put("passTime",rule.getAdmittanceStart()+","+rule.getAdmittanceEnd());
                JSONObject jsons=new JSONObject();
                if(rule.getPermission().contains("idCardFacePermission")){
                    jsons.put("idCardFacePermission",2);
                }else{
                    jsons.put("idCardFacePermission",1);
                }
                if(rule.getPermission().contains("passwordPermission")){
                    jsons.put("passwordPermission",2);
                }else{
                    jsons.put("passwordPermission",1);
                }
                if(rule.getPermission().contains("facePermission")){
                    jsons.put("facePermission",2);
                }else{
                    jsons.put("facePermission",1);
                }
                if(rule.getPermission().contains("idCardPermission")){
                    jsons.put("idCardPermission",2);
                }else{
                    jsons.put("idCardPermission",1);
                }
                if(rule.getPermission().contains("faceAndCardPermission")){
                    jsons.put("faceAndCardPermission",2);
                }else{
                    jsons.put("faceAndCardPermission",1);
                }
                if(rule.getPermission().contains("QRCodePermission")){
                    jsons.put("QRCodePermission",2);
                }else{
                    jsons.put("QRCodePermission",1);
                }
                paramMap.put("permission",jsons);
                response= HttpUtil.postData(url,headMap,paramMap);
            }
        }
        return response;
    }

    //人像门禁服务-设备销权识别主体
    @Override
    public String authDeviceRevoke(Visitor visitor) {
        String response="";
        String url=uniUrl+"/v2/auth/device/revoke?";
        Map<String, Object> headMap=new HashMap<String, Object>();
        headMap.put("token",getToken());
        headMap.put("projectGuid",projectGuid);
        Map<String, Object> paramMap=new HashMap<String, Object>();
        VisVisitorExamine examine= visitor.getExamine();
        //通过访问人员的地点
        SysRule rule=ruleService.selectSysRuleById(examine.getRuleId());
        String[] pointIds=rule.getPointId().split(",");
        List<SysEquipment> equipList=new ArrayList<>();
        if(pointIds!=null){
            for(String point:pointIds){
                SysEquipment equipment=new SysEquipment();
                equipment.setPointId(Long.parseLong(point));
                List<SysEquipment> equipmentDtoList=equipmentService.selectSysEquipmentList(equipment);
                equipList.addAll(equipmentDtoList);
            }
        }
        //授权人员
        String admitGuids="";
        if(StringUtils.isNotEmpty(visitor.getGuid())){
            admitGuids+=visitor.getGuid()+",";
        }
        if(visitor.getItemList()!=null){
            for(Visitor item:visitor.getItemList()){
                admitGuids+= item.getGuid()+",";
            }
        }
        if(!"".equals(admitGuids)){
            admitGuids=admitGuids.substring(0,admitGuids.length()-1);
        }

        //通过规则获取绑定设备信息
        if(equipList!=null){
            for(SysEquipment e:equipList){
                paramMap.put("deviceNo",e.getSequence());
                paramMap.put("admitGuids",admitGuids);//设备来源-Uface 系列
                response= HttpUtil.postData(url,headMap,paramMap);
            }
        }
        return response;
    }

    private static String fillMD5(String md5) {
        //如果不够32位则回调自身补零，最后返回32位长度的签名
        return md5.length() == 32 ? md5 : fillMD5("0" + md5);
    }

}
