package com.yuchen.saas.park.service.service.door.impl;

import com.alibaba.fastjson.JSON;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.door.DoorDeviceParameter;
import com.yuchen.saas.park.api.vo.door.DoorIotParameterVO;
import com.yuchen.saas.park.service.iot.IoTRemoteService;
import com.yuchen.saas.park.service.mapper.door.DoorDeviceParameterMapper;
import com.yuchen.saas.park.service.service.PmgBatSmartEquipmentService;
import com.yuchen.saas.park.service.service.door.DoorDeviceParameterService;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.utils.BeanUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 门参数 服务实现类
 * </p>
 *
 * @author miaoqs
 * @since 2023-12-19
 */
@Service
@Slf4j
public class DoorDeviceParameterServiceImpl extends BaseServiceImpl<DoorDeviceParameterMapper, DoorDeviceParameter> implements DoorDeviceParameterService {

    @Resource
    private IoTRemoteService ioTRemoteService;
    @Resource
    private PmgBatSmartEquipmentService pmgBatSmartEquipmentService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String setDefaultParamIot(Long id) {
        String msg = "";
        DoorDeviceParameter deviceParameter = getById(id);
        deviceParameter.setMagneticType("1");
        deviceParameter.setOpenButtonType("1");
        deviceParameter.setOpenDuration(5);
        deviceParameter.setLeaderCardOpenDuration(0);
        deviceParameter.setDisabledOpenDuration(15);
        deviceParameter.setMagneticAlarmTimeout(30);
        deviceParameter.setStressPassword("");
        deviceParameter.setSuperPassword("");
        //调用iot设置默认值
        DoorIotParameterVO doorIotParameterVO = new DoorIotParameterVO();
        BeanUtil.copyProperties(deviceParameter, doorIotParameterVO);
        String deviceName = "";
        if(Func.isNotEmpty(deviceParameter.getEquipmentId())){
            PmgBatSmartEquipment equipment = pmgBatSmartEquipmentService.getById(deviceParameter.getEquipmentId());
            deviceName = equipment.getDeviceName();
            doorIotParameterVO.setDoorName(deviceName);
            doorIotParameterVO.setDoorId("1");
        }
        //调用iot接口
        Map<String,Object> params = new HashMap<>();
        Map<String,Object> body = Func.toMap(doorIotParameterVO);
        params.put("params", body);
        params.put("productKey", "Fsod5zVVEfCLJXTJ");  //门禁设备代码
        params.put("identify", "setDoorParams");
        params.put("deviceName", deviceName);
        if(ioTRemoteService.callService(params)){
            log.info("====== 调用iot接口设置门参成功:{} ======", JSON.toJSONString(params));
            //保存
            saveOrUpdate(deviceParameter);
        }else {
            msg = "调用iot设置门参失败";
            log.error("====== 调用iot接口设置门参失败:{} ======", JSON.toJSONString(params));
        }
        return msg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String saveOrUpdateIot(DoorDeviceParameter doorDeviceParameter) {
        String msg = "";
        DoorIotParameterVO doorIotParameterVO = new DoorIotParameterVO();
        BeanUtil.copyProperties(doorDeviceParameter, doorIotParameterVO);
        String deviceName = "";
        String productKey = "";
        if(Func.isNotEmpty(doorDeviceParameter.getEquipmentId())){
            PmgBatSmartEquipment equipment = pmgBatSmartEquipmentService.getById(doorDeviceParameter.getEquipmentId());
            productKey = equipment.getProductKey();
            deviceName = equipment.getDeviceName();
            doorIotParameterVO.setDoorName(deviceName);
            doorIotParameterVO.setDoorId("1");
        }
        //调用iot接口
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> body = Func.toMap(doorIotParameterVO);
        params.put("params", body);
        params.put("productKey", productKey);  //门禁设备代码
        params.put("deviceName", deviceName);
        params.put("identify", "setDoorParams");

        if (ioTRemoteService.callService(params)) {
            log.info("====== 调用iot接口设置门参成功:{} ======", JSON.toJSONString(params));
            //保存
            saveOrUpdate(doorDeviceParameter);
        } else {
            msg = "调用iot设置门参失败";
            log.error("====== 调用iot接口设置门参失败:{} ======", JSON.toJSONString(params));
        }
        return msg;
    }

    @Override
    public DoorDeviceParameter getDoorParams(String doorId) {
        PmgBatSmartEquipment equipment = pmgBatSmartEquipmentService.getById(doorId);
        String deviceName = equipment.getDeviceName();
        String productKey = equipment.getProductKey();
        //调用iot接口
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> body = new HashMap<>();
        body.put("doorId", doorId);
        params.put("params", body);
        params.put("productKey", productKey);  //门禁设备代码
        params.put("deviceName", deviceName);
        params.put("identify", "getDoorParams");
        if (ioTRemoteService.callService(params)) {
            log.info("====== 调用iot接口查看门参数成功:{} ======");
        } else {
            log.error("====== 调用iot接口查看门参数失败:{} ======");
        }
        return null;
    }

}
