package me.zhengjie.modules.quartz.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.dm.dmLane.domain.DmLane;
import me.zhengjie.modules.dm.dmLane.repository.DmLaneRepository;
import me.zhengjie.utils.BeanMapUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class LaneDataTask {
    @Value("${laneUrl}")
    private String url;
    private final DmLaneRepository dmLaneRepository;

    public void run() throws IOException {
        log.info("run 执行开始");
        asyncLaneAll();
        log.info("run 执行结束");
    }

    public void asyncLaneAll() throws IOException {
//        String url = "http://10.208.128.14:9999/api/park/getlane";
        String data = BeanMapUtils.sendPost(url, new JSONObject().toJSONString());
        JSONObject dataJSON = JSONObject.parseObject(data);
        JSONArray arr = dataJSON.getJSONArray("data");
        for (int i = 0; i < arr.size(); i++) {
            JSONObject attendance = arr.getJSONObject(i);
            DmLane dmLane = new DmLane();
            dmLane.setId(attendance.getInteger("ctrlnumber"));
            dmLane.setInoutType(attendance.getInteger("inout"));
            dmLane.setInoutname(attendance.getString("inoutname"));
            dmLane.setBigsmall(attendance.getInteger("bigsmall"));
            dmLane.setPcname(attendance.getString("pcname"));
            dmLaneRepository.save(dmLane);
        }
    }
}
