package com.example.zxweb.init;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.zxweb.common.constant.DevConstant;
import com.example.zxweb.common.constant.enums.IotApiEnum;
import com.example.zxweb.entity.ZxCelue;
import com.example.zxweb.service.IZxCelueService;
import com.example.zxweb.utils.IotUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 启动项目后获取策略信息
 * @Author ZhouWenTao
 * @Date 2023/9/13 13:48
 */
@Configuration
@Slf4j
public class LoadCelueInit implements ApplicationRunner{
    @Resource
    IZxCelueService zxCelueService;
    @Value("${iot.active}")
    private boolean active;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        Thread.sleep(5000L);
        if (false) {
            //获取网关的数据，同步到数据库中
            String gateway_5lqh = DevConstant.gateway.gateway_5lqh;//5楼网关
            String format = String.format("{\"serial\":\"%s\",\"productId\":\"\",\"methodName\":\"celueListQuery\",\"inputData\":{\"devid\":\"%s\"}}", gateway_5lqh,gateway_5lqh);
            JSONObject requestBody = JSONObject.parseObject(format);
            JSONObject responseBody = IotUtils.postApi(IotApiEnum.getPathByText("向设备发送方法"), requestBody);
            log.info(responseBody.toJSONString());
            //JSONObject responseBody = JSONObject.parseObject("{\"code\":\"0\",\"data\":{\"outputData\":{\"devid\":\"0cefafd605f7\",\"code\":1,\"list\":[{\"devid\":\"3381bbfeffea3590\",\"triggereTime\":\"20 18 * * 1,4,6\",\"name\":\"策略1\",\"action\":\"light00001.light.controlLight.ctrl_ch0_status=\\\"0\\\"&&chazuo000001.plug.controlPlug.ctrl_ch0_status=\\\"0\\\"\",\"startTime\":\"2023-11-12 10:00:00\",\"endTime\":\"2023-11-12 11:30:00\",\"celueid\":\"100000000001\"},{\"devid\":\"0cefafd605f7\",\"triggerCondition\":\"body0001.body_exist.status.report_status=\\\"0\\\"##900\",\"name\":\"策略2\",\"action\":\"light00001.light.controlLight.ctrl_ch0_status=\\\"0\\\"&&chazuo000001.plug.controlPlug.ctrl_ch0_status=\\\"0\\\"\",\"startTime\":\"2023-11-12 10:00:00\",\"endTime\":\"2023-11-12 11:30:00\",\"celueid\":\"100000000002\"},{\"devid\":\"0cefafd605f7\",\"triggereTime\":\"20 18 * * 1,4,6\",\"name\":\"策略3\",\"action\":\"e826ecfeff81f68c.light.controlLight.ctrl_ch0_status=\\\"0\\\"\",\"startTime\":\"2023-09-12 10:00:00\",\"endTime\":\"2023-09-30 11:30:00\",\"celueid\":\"100000000003\"}]}}}\n");
            JSONObject data = responseBody.getJSONObject("data");
            if (data!=null) {
                JSONObject outputData = data.getJSONObject("outputData");
                if (outputData!=null) {
                    JSONArray list = outputData.getJSONArray("list");
                    if (!CollectionUtils.isEmpty(list)) {
                        List<ZxCelue> zxCelues = list.toJavaList(ZxCelue.class);
                        for (ZxCelue zxCelue : zxCelues) {
                            List<String> devidList=new ArrayList<>();
                            String action = zxCelue.getAction();
                            if (StringUtils.isNotBlank(action)) {
                                String[] split = action.split("&&");
                                for (String s : split) {
                                    String[] split1 = s.split("\\.");
                                    String s1 = split1[0];
                                    devidList.add(s1);
                                    //判断是1 还是 0
                                    String[] split2 = s.split("=");
                                    if (split2!=null && split2.length>1) {
                                        zxCelue.setType(split2[1].contains("1")?"1":"0");
                                    }
                                }
                            }
                            zxCelue.setDevid(String.join(",",devidList));
                            //获取状态
                            zxCelue.setStatus(IotUtils.getCelueStatus(zxCelue.getCelueid()));
                        }
                        //更新库表
                        zxCelueService.saveOrUpdateBatch(zxCelues);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        String str="light00001.light.controlLight.ctrl_ch0_status=\"0\"&&chazuo000001.plug.controlPlug.ctrl_ch0_status=\"0\"";
        String[] split = str.split("&&");
        for (String s : split) {
            String[] split1 = s.split("=");
            String s1 = split1[1];
            System.out.println(s1);
        }
        /*ArrayList<String> devidList = new ArrayList<>(Arrays.asList("1,2"));
        System.out.println(String.join(",",devidList));;*/
    }
}
