package me.zhengjie.modules.quartz.task;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.ykt.domain.DmRoomDevice;
import me.zhengjie.ykt.service.DmRoomDeviceService;
import me.zhengjie.ykt.service.dto.DmRoomDeviceDto;
import me.zhengjie.ykt.service.dto.DmRoomDeviceQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Component
public class RoomDeviceTask {
    @Value("${deviceUrl}")
    private String url;
    private final DmRoomDeviceService dmRoomDeviceService;

    public void SyncRoomDevice(){
//        String url = "http://10.208.128.14:8069/openapi/getDoorList";
        String res = HttpRequest.post(url).execute().body();
        log.info("res:"+res);
        JSONObject result = JSONObject.parseObject(res);
        Integer code = result.getInteger("result");
        if(code == 1){
            JSONObject dataObject = result.getJSONObject("data");
            JSONArray dataArray = dataObject.getJSONArray("data");
            for(int i = 0 ;i<dataArray.size();i++){
                 // 根据索引  获取数组数据
                JSONObject roomDeviceJSON = dataArray.getJSONObject(i);
                // 新建个 设备查询对象
                DmRoomDeviceQueryCriteria dmRoomDeviceQueryCriteria = new DmRoomDeviceQueryCriteria();
                //获取deviceID 并设置deviceID
                String deviceID = roomDeviceJSON.getString("deviceID");
                Long doorId = roomDeviceJSON.getLong("doorID");
                //根据 设备id查询查看数据库中是否有这个设备
                 List<DmRoomDevice> byCode = dmRoomDeviceService.findByCode(deviceID, doorId.toString());
                 log.info("数据库中的设备权限：code:{} +++++++++ doorId{}",deviceID,deviceID);
                // 新建个设备对象
                DmRoomDevice dmRoomDevice = new DmRoomDevice();
                // set设备code
                dmRoomDevice.setCode(deviceID);
                // 获取设备名称并设置
                dmRoomDevice.setName(roomDeviceJSON.getString("deviceName"));
                //获取门禁id 并设置
                dmRoomDevice.setDoorID(doorId);
                //获取门禁名称 并设置
                dmRoomDevice.setDoorName(roomDeviceJSON.getString("doorName"));
                //获取设备类型 并设置
                dmRoomDevice.setDeviceType(roomDeviceJSON.getInteger("deviceType"));
                if(byCode.isEmpty()){
                    log.info("新增设备权限：+++++++++++data：{}",dmRoomDevice);
                    dmRoomDeviceService.create(dmRoomDevice);
                }else {
                    dmRoomDevice.setId(byCode.get(0).getId());
                    log.info("修改设备权限：+++++++++++data：{}",dmRoomDevice);
                    dmRoomDeviceService.update(dmRoomDevice);
                }
            }
        } else {
            log.info("code is not 1.");
        }
    }

}
