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.printingQueue.domain.DmPrintingQueue;
import me.zhengjie.modules.dm.printingQueue.service.DmPrintingQueueService;
import me.zhengjie.utils.BeanMapUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;

@Slf4j
@RequiredArgsConstructor
@Component
public class PrintingStatusTask {
    private final DmPrintingQueueService dmPrintingQueueService;



    public void syncStatus() throws ParseException, IOException {
        String url = "http://xxx:xx/xxx";

        JSONObject data = new JSONObject();
        data.put("Action",1);//当前页数
        data.put("printer","");
        data.put("user","");
        String generateProc = BeanMapUtils.sendPost(url,data.toString());
        JSONArray arr = JSONArray.parseArray(generateProc);
        for (int i = 0; i < arr.size(); i++) {
            JSONObject attendance = arr.getJSONObject(i);
            DmPrintingQueue printingQueue = new DmPrintingQueue();
            printingQueue.setStatus(attendance.getString("status"));
            printingQueue.setStartTime(Timestamp.valueOf(attendance.getString("time")));
            dmPrintingQueueService.update(printingQueue);
        }
    }
}
