package com.sptg.third.biz.qinlin.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.UserFlagConstant;
import com.sptg.common.core.enums.Instruct;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.entity.InstructPublishLog;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.device.api.feigns.InstructPublishLogFeign;
import com.sptg.estate.api.entity.AccessRecords;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.feigns.AccessRecordsFeign;
import com.sptg.estate.api.feigns.ResidentialFeign;
import com.sptg.permissions.api.entity.ApiLog;
import com.sptg.permissions.api.feigns.SysApiLogFeign;
import com.sptg.third.api.beans.ApiResultBean;
import com.sptg.third.biz.qinlin.conf.QinLinConfig;
import com.sptg.third.api.entity.qinlin.*;
import com.sptg.third.api.feigns.qinlin.QLinFeign;
import com.sptg.third.biz.qinlin.util.QinLinHttpUtil;
import com.sptg.third.biz.qinlin.service.QLinApiLogServiceInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zwx
 * @create 2020/12/16 15:57
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class QLinController implements QLinFeign {

    private final DeviceFeign deviceFeign;
    private final InstructPublishLogFeign instructPublishLogFeign;
    private final AccessRecordsFeign accessRecordsFeign;
    private final QLinApiLogServiceInterface qLinApiLogServiceInterface;
    private final FastFileStorageClient fastFileStorageClient;
    private final QinLinConfig qinLinConfig;
    private final ResidentialFeign residentialFeign;

    @Override
    @PostMapping("/qinLin/addUser")
    public ResultBean<String> addUser(@RequestBody QLinUser qLinUser){
        ResultBean<String> resultBean = null;
        InstructPublishLog instructPublishLog = new InstructPublishLog();
        instructPublishLog.setCreateTime(new Date());
        ApiResultBean apiResultBean = null;
        try {
            String expires = qLinUser.getExpires();
            if(!StringUtils.hasText(expires)){
                Calendar cd = Calendar.getInstance();
                cd.setTime(new Date());
                cd.add(Calendar.YEAR, 10);
                Date expireDate = cd.getTime();
                String strDateFormat = "yyyy-MM-dd HH:mm:ss";
                SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
                qLinUser.setExpires(sdf.format(expireDate));
            }
            apiResultBean = QinLinHttpUtil.addUser(qLinUser, qinLinConfig);
            QLinApiLog qLinApiLog = new QLinApiLog();
            qLinApiLog.setCode(apiResultBean.getCode());
            qLinApiLog.setMessage(apiResultBean.getMessage());
            qLinApiLog.setReqData(JsonUtils.objectToJson(qLinUser));
            qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
            qLinApiLog.setCreateDate(new Date());
            qLinApiLog.setMethod("新增用户");
            this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
            boolean addSuccess = apiResultBean.getSuccess();
//            Device device = deviceFeign.getEntityBySerialNumber(qLinUser.getDeviceIds());
            List<Device> deviceList = deviceFeign.getListBySerialNumbers(qLinUser.getDeviceIds());

            long id = IdWorker.getId();
            String data = JsonUtils.objectToJson(qLinUser);
            instructPublishLog.setId(id);
            instructPublishLog.setPublishData(data);
            instructPublishLog.setSerialNumber(qLinUser.getDeviceIds());
            instructPublishLog.setStatus(OperationConstant.TWO);
            if(addSuccess){
                instructPublishLog.setHandleStatus(OperationConstant.TWO);
                if(qLinUser.getUserId().startsWith(UserFlagConstant.SERVER_USER_NUMBER_FLAG)){
                    instructPublishLog.setErrorMsg("添加/更新服务员成功");
                    resultBean = new ResultBean<>(ResultEnum.SUCCESS, "添加服务员成功");
                }else{
                    instructPublishLog.setErrorMsg("添加/更新住户成功");
                    resultBean = new ResultBean<>(ResultEnum.FAILURE, "添加住户成功");
                }
            }else{
                instructPublishLog.setHandleStatus(OperationConstant.THREE);
                if(qLinUser.getUserId().startsWith(UserFlagConstant.SERVER_USER_NUMBER_FLAG)){
                    instructPublishLog.setErrorMsg("添加/更新服务员失败");
                    resultBean = new ResultBean<>(ResultEnum.SUCCESS, "添加服务员失败");
                }else{
                    instructPublishLog.setErrorMsg("添加/更新住户失败");
                    resultBean = new ResultBean<>(ResultEnum.FAILURE, "添加住户失败");
                }
            }
            instructPublishLog.setInstructCode(Instruct.add_user_face.getCode());
            instructPublishLog.setInstructDescribe(Instruct.add_user_face.getDescribe());
            instructPublishLog.setLastModifyTime(new Date());
            instructPublishLog.setXqName(deviceList.stream().map(Device::getXqName).collect(Collectors.joining(",")));
            instructPublishLog.setUnitName(deviceList.stream().map(Device::getDyName).collect(Collectors.joining(",")));
            this.instructPublishLogFeign.create(instructPublishLog);
        }catch (Exception e){
            QLinApiLog qLinApiLog = new QLinApiLog();
            qLinApiLog.setCode(apiResultBean.getCode());
            qLinApiLog.setMessage(e.getMessage());
            qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
            qLinApiLog.setCreateDate(new Date());
            qLinApiLog.setMethod("新增用户异常");
            this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
            resultBean = new ResultBean<>(ResultEnum.FAILURE, "新增用户异常");
        }
        return resultBean;
    }

    @Override
    @PostMapping("/qinLin/deleteUser")
    public ResultBean<String> deleteUser(@RequestBody QLinUser qLinUser){
        ResultBean<String> resultBean = null;
        ApiResultBean apiResultBean = QinLinHttpUtil.deleteUser(qLinUser, qinLinConfig);
        QLinApiLog qLinApiLog = new QLinApiLog();
        qLinApiLog.setCode(apiResultBean.getCode());
        qLinApiLog.setMessage(apiResultBean.getMessage());
        qLinApiLog.setReqData(JsonUtils.objectToJson(qLinUser));
        qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
        qLinApiLog.setCreateDate(new Date());
        qLinApiLog.setMethod("删除用户");
        this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
        boolean deleteSuccess = apiResultBean.getCode().equals(OperationConstant.ZERO);
        Device device = deviceFeign.getEntityBySerialNumber(qLinUser.getDeviceIds());
        long id = IdWorker.getId();
        String data = JsonUtils.objectToJson(qLinUser);
        InstructPublishLog instructPublishLog = new InstructPublishLog();
        instructPublishLog.setCreateTime(new Date());
        instructPublishLog.setId(id);
        instructPublishLog.setPublishData(data);
        instructPublishLog.setStatus(OperationConstant.TWO);
        instructPublishLog.setSerialNumber(device.getSerialNumber());
        if(deleteSuccess){
            instructPublishLog.setHandleStatus(OperationConstant.TWO);
            if(qLinUser.getUserId().startsWith(UserFlagConstant.SERVER_USER_NUMBER_FLAG)){
                instructPublishLog.setErrorMsg("删除服务员成功");
                resultBean = new ResultBean<>(ResultEnum.SUCCESS, "删除服务员成功");
            }else{
                instructPublishLog.setErrorMsg("删除住户成功");
                resultBean = new ResultBean<>(ResultEnum.FAILURE, "删除住户成功");
            }
        }else{
            instructPublishLog.setHandleStatus(OperationConstant.THREE);
            if(qLinUser.getUserId().startsWith(UserFlagConstant.SERVER_USER_NUMBER_FLAG)){
                instructPublishLog.setErrorMsg("删除服务员失败");
                resultBean = new ResultBean<>(ResultEnum.SUCCESS, "删除服务员失败");
            }else{
                instructPublishLog.setErrorMsg("删除住户失败");
                resultBean = new ResultBean<>(ResultEnum.FAILURE, "删除住户失败");
            }
        }
        instructPublishLog.setInstructCode(Instruct.delete_user.getCode());
        instructPublishLog.setInstructDescribe(Instruct.delete_user.getDescribe());
        instructPublishLog.setLastModifyTime(new Date());
        instructPublishLog.setXqName(device.getXqName());
        if(!StringUtils.isEmpty(device.getDyName())){
            instructPublishLog.setUnitName(device.getDyName());
        }
        this.instructPublishLogFeign.create(instructPublishLog);
        return resultBean;
    }

    @Override
    @PostMapping("/qinLin/deleteUserPermission")
    public ResultBean<String> deleteUserPermission(@RequestBody QLinUserDevice qLinUserDevice){
        ResultBean<String> resultBean = null;
        ApiResultBean apiResultBean = QinLinHttpUtil.deleteUserPermission(qLinUserDevice, qinLinConfig);
        QLinApiLog qLinApiLog = new QLinApiLog();
        qLinApiLog.setCode(apiResultBean.getCode());
        qLinApiLog.setMessage(apiResultBean.getMessage());
        qLinApiLog.setReqData(JsonUtils.objectToJson(qLinUserDevice));
        qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
        qLinApiLog.setCreateDate(new Date());
        qLinApiLog.setMethod("删除用户权限");
        this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
        boolean deleteSuccess = apiResultBean.getCode().equals(OperationConstant.ZERO);
//        Device device = deviceFeign.getEntityBySerialNumber(qLinUserDevice.getDeviceIds());
        List<Device> deviceList = deviceFeign.getListBySerialNumbers(qLinUserDevice.getDeviceIds());


        InstructPublishLog instructPublishLog = new InstructPublishLog();
        instructPublishLog.setCreateTime(new Date());
        long id = IdWorker.getId();
        String data = JsonUtils.objectToJson(qLinUserDevice);
        instructPublishLog.setId(id);
        instructPublishLog.setPublishData(data);
        instructPublishLog.setSerialNumber(qLinUserDevice.getDeviceIds());
        instructPublishLog.setStatus(OperationConstant.TWO);
        if(deleteSuccess){
            instructPublishLog.setHandleStatus(OperationConstant.TWO);
            if(qLinUserDevice.getUserId().startsWith(UserFlagConstant.SERVER_USER_NUMBER_FLAG)){
                instructPublishLog.setErrorMsg("删除服务员人脸权限成功");
                resultBean = new ResultBean<>(ResultEnum.SUCCESS, "删除服务员人脸权限成功");
            }else{
                instructPublishLog.setErrorMsg("删除住户人脸权限成功");
                resultBean = new ResultBean<>(ResultEnum.FAILURE, "删除住户人脸权限成功");
            }
        }else{
            instructPublishLog.setHandleStatus(OperationConstant.THREE);
            if(qLinUserDevice.getUserId().startsWith(UserFlagConstant.SERVER_USER_NUMBER_FLAG)){
                instructPublishLog.setErrorMsg("删除服务员人脸权限失败");
                resultBean = new ResultBean<>(ResultEnum.SUCCESS, "删除服务员人脸权限失败");
            }else{
                instructPublishLog.setErrorMsg("删除住户人脸权限失败");
                resultBean = new ResultBean<>(ResultEnum.FAILURE, "删除住户人脸权限失败");
            }
        }
        instructPublishLog.setInstructCode(Instruct.delete_user_face.getCode());
        instructPublishLog.setInstructDescribe(Instruct.delete_user_face.getDescribe());
        instructPublishLog.setLastModifyTime(new Date());
        instructPublishLog.setXqName(deviceList.stream().map(Device::getXqName).collect(Collectors.joining(",")));
        instructPublishLog.setUnitName(deviceList.stream().map(Device::getDyName).collect(Collectors.joining(",")));
//        instructPublishLog.setXqName(device.getXqName());
//        if(!StringUtils.isEmpty(device.getDyName())){
//            instructPublishLog.setUnitName(device.getDyName());
//        }
        this.instructPublishLogFeign.create(instructPublishLog);
        return resultBean;
    }

    @Override
    @PostMapping("/qinLin/deleteDevice")
    public ResultBean<String> deleteDevice(@RequestBody QLinDevice qLinDevice){
//        QinLinHttpUtil httpUtil = new QinLinHttpUtil();
//        Map<String, String> params = new HashMap<>();
//        params.put("deviceId", qLinDevice.getDeviceId());
//        ApiResultBean apiResultBean = httpUtil.deleteDevice(params);
//        QLinApiLog qLinApiLog = new QLinApiLog();
//        qLinApiLog.setCode(apiResultBean.getCode());
//        qLinApiLog.setMessage(apiResultBean.getMessage());
//        qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
//        this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
//        if(apiResultBean.getCode().equals(OperationConstant.ZERO)){
//            return new ResultBean<>(ResultEnum.SUCCESS, "删除设备成功");
//        }else{
//            return new ResultBean<>(ResultEnum.FAILURE, "删除设备失败");
//        }
        return new ResultBean<>();
    }

    @Override
    @PostMapping("/qinLin/getDeviceInfo")
    public ResultBean<String> getDeviceInfo(@RequestBody QLinDevice qLinDevice){
//        QinLinHttpUtil httpUtil = new QinLinHttpUtil();
//        Map<String, String> params = new HashMap<>();
//        params.put("deviceIds", qLinDevice.getDeviceIds());
//        ApiResultBean<List<QLinDevice>> apiResultBean = httpUtil.getDeviceInfo(params);
//        QLinApiLog qLinApiLog = new QLinApiLog();
//        qLinApiLog.setCode(apiResultBean.getCode());
//        qLinApiLog.setMessage(apiResultBean.getMessage());
//        qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
//        this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
//        if(apiResultBean.getCode().equals(OperationConstant.ZERO)){
//            List<QLinDevice> sourceList = apiResultBean.getData();
//            sourceList.forEach(source ->{
//                String serialNumber = source.getDeviceId();
//                Device target = this.deviceFeign.getEntityBySerialNumber(serialNumber);
//                target.setOnlineStatus(Integer.parseInt(source.getOnlineStatus()));
//                target.setNetworkType(source.getNetworkType());
//                target.setVersionNumber(source.getSoftwareVersion());
//                this.deviceFeign.heartbeat(target);
//            });
//            return new ResultBean<>(ResultEnum.SUCCESS, "查询设备信息成功");
//        }else{
//            return new ResultBean<>(ResultEnum.FAILURE, "查询设备信息失败");
//        }
        return new ResultBean<>();
    }

    @Override
    @PostMapping("/qinLin/remoteOpenDoor")
    public ResultBean<String> remoteOpenDoor(@RequestBody QLinUserDevice qLinUserDevice){
        ResultBean<String> resultBean = null;
        ApiResultBean apiResultBean = QinLinHttpUtil.remoteOpenDoor(qLinUserDevice, qinLinConfig);
        QLinApiLog qLinApiLog = new QLinApiLog();
        qLinApiLog.setCode(apiResultBean.getCode());
        qLinApiLog.setMessage(apiResultBean.getMessage());
        qLinApiLog.setReqData(JsonUtils.objectToJson(qLinUserDevice));
        qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
        qLinApiLog.setCreateDate(new Date());
        qLinApiLog.setMethod("远程开门");
        this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
        boolean openSuccess = apiResultBean.getCode().equals(OperationConstant.ZERO);
        Device device = deviceFeign.getEntityBySerialNumber(qLinUserDevice.getDeviceId());
        long id = IdWorker.getId();
        String data = JsonUtils.objectToJson(qLinUserDevice);
        InstructPublishLog instructPublishLog = new InstructPublishLog();
        instructPublishLog.setCreateTime(new Date());
        instructPublishLog.setId(id);
        instructPublishLog.setSerialNumber(device.getSerialNumber());
        instructPublishLog.setPublishData(data);
        instructPublishLog.setStatus(OperationConstant.TWO);
        if(openSuccess){
            instructPublishLog.setHandleStatus(OperationConstant.TWO);
            instructPublishLog.setErrorMsg("开门成功");
            resultBean = new ResultBean<>(ResultEnum.SUCCESS, "远程开门成功");
        }else{
            instructPublishLog.setHandleStatus(OperationConstant.THREE);
            instructPublishLog.setErrorMsg("开门失败");
            resultBean = new ResultBean<>(ResultEnum.FAILURE.getCode(), apiResultBean.getMessage());
        }
        instructPublishLog.setInstructCode(Instruct.open_door.getCode());
        instructPublishLog.setInstructDescribe(Instruct.open_door.getDescribe());
        instructPublishLog.setLastModifyTime(new Date());
        instructPublishLog.setXqName(device.getXqName());
        if(!StringUtils.isEmpty(device.getDyName())){
            instructPublishLog.setUnitName(device.getDyName());
        }
        if(openSuccess){
            this.instructPublishLogFeign.create(instructPublishLog);
//            AccessRecords records = new AccessRecords();
//            records.setOpenDoorType(OperationConstant.THREE);
//            records.setAccessDate(new Date());
//            records.setUserId(qLinUserDevice.getUserId());
//            records.setDeviceId(device.getId());
//            records.setDeviceName(device.getName());
//            records.setSerialNumber(device.getSerialNumber());
//            records.setInstallPath(device.getInstallPath());
//            records.setIsOut(device.getIsOut());
//            //  小区信息
//            records.setResidentialId(device.getXqId());
//            records.setResidentialName(device.getXqName());
//            //  机构信息
//            records.setOrgPosition(device.getOrgPosition());
//            records.setEstatePosition(device.getEstatePosition());
//            records.setRootOrgId(device.getRootOrgId());
//            //创建远程开门出入记录
//            this.accessRecordsFeign.addAccessRecords(records);
        }
        return resultBean;
    }

    @Override
    @PostMapping("/qinLin/getLogs")
    public ResultBean<String> getLogs(@RequestBody QLinQuery qLinQuery){
        ApiResultBean<List<QLinOpenRecord>> apiResultBean = QinLinHttpUtil.getLogs(qLinQuery, qinLinConfig, fastFileStorageClient);
//        QLinApiLog qLinApiLog = new QLinApiLog();
//        qLinApiLog.setCode(apiResultBean.getCode());
//        qLinApiLog.setMessage(apiResultBean.getMessage());
//        qLinApiLog.setReqData(JsonUtils.objectToJson(qLinQuery));
//        qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
//        qLinApiLog.setCreateDate(new Date());
//        qLinApiLog.setMethod("获取开门日志");
//        this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
        List<QLinOpenRecord> list = apiResultBean.getData();
        if(list.size() > 0){
            list.forEach(data -> {
                //是因为亲邻的日志接口，如果传deviceIds过去，它是会把所有日志都拿回来。所以如果调4次接口，就会重复返回所有日志
                //但实际上只有个别设备才会有记录。
                if(qLinQuery.getDeviceId().equals(data.getDeviceId())){
                    String serialNumber = data.getDeviceId();
                    Device dbDevice = this.deviceFeign.getEntityBySerialNumber(serialNumber);
                    if(dbDevice != null){
                        AccessRecords records = new AccessRecords();
                        String openType = data.getOpenType();
                        if(openType.equals(OperationConstant.ONE + "")){
                            records.setOpenDoorType(OperationConstant.ZERO); //人脸开门
                            records.setSnapshoUri(data.getRealFaceUrl());
                        }else{
                            records.setOpenDoorType(OperationConstant.THREE); //远程开门
                        }
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date accessDate = null;
                        try {
                            accessDate = sdf.parse(data.getOpenTime());
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        records.setAccessDate(accessDate);
                        records.setUserId(data.getUserId());
                        records.setDeviceId(dbDevice.getId());
                        records.setDeviceName(dbDevice.getName());
                        records.setSerialNumber(serialNumber);
                        records.setInstallPath(dbDevice.getInstallPath());
                        records.setIsOut(dbDevice.getIsOut());
                        records.setTemperature(data.getTemperature());
                        //  小区信息
                        records.setResidentialId(dbDevice.getXqId());
                        records.setResidentialName(dbDevice.getXqName());
                        //  机构信息
                        records.setOrgPosition(dbDevice.getOrgPosition());
                        records.setEstatePosition(dbDevice.getEstatePosition());
                        records.setRootOrgId(dbDevice.getRootOrgId());
                        //创建远程开门出入记录
                        this.accessRecordsFeign.addAccessRecords(records);
                    }
                }
            });
        }
        return new ResultBean<>(ResultEnum.SUCCESS);
    }

    @Override
    @PostMapping(value = "/qinLin/recordQinLinApiLog")
    public ResultBean<String> recordQinLinApiLog(@RequestBody QLinApiLog qLinApiLog) {
        ResultBean<String> resultBean = this.recordQinLinApiLog(qLinApiLog);
        return resultBean;
    }

    /**
     * 新增小区
     * @param qLinCommunityDevices
     * @return
     */
    @Override
    @PostMapping(value = "/qinLin/addCommunity")
    public ResultBean<String> addCommunity(@RequestBody QLinCommunityDevices qLinCommunityDevices) {
        log.warn("============================后台亲邻新增小区(/qinLin/addCommunity)请求数据==============================");
        log.warn(JsonUtils.objectToJson(qLinCommunityDevices));
        ResultBean<String> resultBean = null;
        ApiResultBean<Long> apiResultBean = null;
        try {
            apiResultBean = QinLinHttpUtil.addCommunity(qLinCommunityDevices, qinLinConfig);
            QLinApiLog qLinApiLog = new QLinApiLog();
            qLinApiLog.setCode(apiResultBean.getCode());
            qLinApiLog.setMessage(apiResultBean.getMessage());
            qLinApiLog.setReqData(JsonUtils.objectToJson(qLinCommunityDevices));
            qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
            qLinApiLog.setCreateDate(new Date());
            qLinApiLog.setMethod("新增小区");
            this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
            boolean addSuccess = apiResultBean.getSuccess();
            if(addSuccess){
                Long residentialId = qLinCommunityDevices.getResidentialId();
                Residential residential = this.residentialFeign.findResidentialById(residentialId);
                residential.setQinLinResidentialId(apiResultBean.getData());
                this.residentialFeign.updateEntity(residential);
                resultBean = new ResultBean<>(ResultEnum.SUCCESS, "添加小区成功");
            }else{
                resultBean = new ResultBean(ResultEnum.FAILURE.getCode(), "添加小区失败：" + apiResultBean.getMessage());
            }
        }catch (Exception e){
            QLinApiLog qLinApiLog = new QLinApiLog();
            qLinApiLog.setCode(-1);
            qLinApiLog.setMessage(e.getMessage());
//            qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
            qLinApiLog.setCreateDate(new Date());
            qLinApiLog.setMethod("新增小区");
            this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
            resultBean = new ResultBean<>(ResultEnum.FAILURE, "添加小区异常");
        }
        return resultBean;
    }

    /**
     * 新增设备
     * @param qLinCommunityDevices
     * @return
     */
    @Override
    @PostMapping(value = "/qinLin/addDevices")
    public ResultBean<String> addDevices(@RequestBody QLinCommunityDevices qLinCommunityDevices) {
        log.warn("============================后台亲邻新增设备(/qinLin/addDevices)请求数据==============================");
        log.warn(JsonUtils.objectToJson(qLinCommunityDevices));
        ResultBean<String> resultBean = null;
        ApiResultBean apiResultBean = null;
        try {
            apiResultBean = QinLinHttpUtil.addDevices(qLinCommunityDevices, qinLinConfig);
            QLinApiLog qLinApiLog = new QLinApiLog();
            qLinApiLog.setCode(apiResultBean.getCode());
            qLinApiLog.setMessage(apiResultBean.getMessage());
            qLinApiLog.setReqData(JsonUtils.objectToJson(qLinCommunityDevices));
            qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
            qLinApiLog.setCreateDate(new Date());
            qLinApiLog.setMethod("新增门禁设备");
            this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
            boolean addSuccess = apiResultBean.getSuccess();
            if(addSuccess){
                resultBean = new ResultBean<>(ResultEnum.SUCCESS, "新增门禁设备成功");
            }else{
                resultBean = new ResultBean(ResultEnum.FAILURE.getCode(), "新增门禁设备失败：" + apiResultBean.getMessage());
            }
        }catch (Exception e){
            QLinApiLog qLinApiLog = new QLinApiLog();
            qLinApiLog.setCode(-1);
            qLinApiLog.setMessage(e.getMessage());
//            qLinApiLog.setData(JsonUtils.objectToJson(apiResultBean.getData()));
            qLinApiLog.setCreateDate(new Date());
            qLinApiLog.setMethod("新增门禁设备");
            this.qLinApiLogServiceInterface.recordQinLinApiLog(qLinApiLog);
            resultBean = new ResultBean<>(ResultEnum.FAILURE, "新增门禁设备异常：" + apiResultBean.getMessage());
        }
        return resultBean;
    }

    @GetMapping("/test/qinlin")
    public void test(){
        System.out.println("");
        return;
    }
}
