package com.ruoyi.system.service;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.RecordData;
import com.ruoyi.system.mapper.RecordDataMapper;
import com.ruoyi.system.utils.timingURL;
import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class Task {

    @Resource
    private RecordDataMapper recordDataMapper;

    /**
     *定时任务（定时取海康威视的进场数据）
     */
    //@Scheduled(cron = "0 10,40 * * * ? ")
    public void parkingLotData() {
        DateTime dt = new DateTime();
        String endTime=dt.toString("yyyy-MM-dd'T'HH:mm:ss")+"+08:00";

        DateTime dateTime = dt.minusHours(24);
        String startTime=dateTime.toString("yyyy-MM-dd'T'HH:mm:ss")+"+08:00";


        //查询内部车辆进场信息（外场）
        String outApi = "";
        String outParkSyscode = "";
        String outEntranceSyscode = "";
        List<Map<String,Object>> outListAll = new ArrayList<>();
        for (int i = 1; i > 0; i++) {
            int pageNo = 1;
            String outfieldData = timingURL.timingURL(outApi,outParkSyscode,outEntranceSyscode,startTime,endTime,pageNo);
            JSONObject jsonObject = JSONObject.parseObject(outfieldData);
            String data = (String) jsonObject.get("data");
            JSONObject outData = JSONObject.parseObject(data);
            List<Map<String,Object>> list = (List<Map<String, Object>>) outData.get("list");
            outListAll.addAll(list);
            if(list.size()<1000){
                i=-2;
            }else{
                pageNo++;
            }
        }
        //查询内部车辆进场信息（内场） infield
        String infieldApi = "";
        String infieldParkSyscode = "";
        String infieldEntranceSyscode = "";
        List<Map<String,Object>> infieldListAll = new ArrayList<>();
        for (int i = 1; i > 0; i++) {
            int pageNo = 1;
            String infieldfieldData = timingURL.timingURL(infieldApi,infieldParkSyscode,infieldEntranceSyscode,startTime,endTime,pageNo);
            JSONObject jsonObject = JSONObject.parseObject(infieldfieldData);
            String data = (String) jsonObject.get("data");
            JSONObject infieldData = JSONObject.parseObject(data);
            List<Map<String,Object>> list = (List<Map<String, Object>>) infieldData.get("list");
            outListAll.addAll(list);
            if(list.size()<1000){
                i=-2;
            }else{
                pageNo++;
            }
        }

        for (Map<String, Object> stringObjectMap : outListAll) {
            boolean bl = true;
            for (Map<String, Object> objectMap : infieldListAll) {
                if(stringObjectMap.get("plateNo").equals(objectMap.get("plateNo"))){
                    bl = false;
                }
            }
            if(bl){
                RecordData recordData = new RecordData();
                recordData.setId((String) stringObjectMap.get("crossRecordSyscode"));
                recordData.setParkSyscode((String) stringObjectMap.get("parkSyscode"));
                recordData.setCardNo((String) stringObjectMap.get("cardNo"));
                recordData.setParkName((String) stringObjectMap.get("parkName"));
                recordData.setEntranceName((String) stringObjectMap.get("entranceName"));
                recordData.setPlateNo((String) stringObjectMap.get("plateNo"));
                recordData.setEntranceSyscode((String) stringObjectMap.get("entranceSyscode"));
                recordData.setReleaseMode((Integer) stringObjectMap.get("releaseMode"));
                recordData.setRoadwayName((String) stringObjectMap.get("roadwayName"));
                recordData.setRoadwaySyscode((String) stringObjectMap.get("roadwaySyscode"));
                //2018-07-26T15:00:00+08:00
                recordData.setCrossTime(zDate((String) stringObjectMap.get("crossTime")));
                //将信息保存进数据库
                recordDataMapper.insertRecordData(recordData);
            }
        }


    }


    //将yyyy-MM-dd'T'HH:mm:ss转换成yyyy-MM-dd HH:mm:ss
    public static String zDate(String tDate){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date2 = null;
        try {
            date2 = format.parse(tDate);
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date2);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {

        DateTime dt = new DateTime();
        String endTime=dt.toString("yyyy-MM-dd'T'HH:mm:ss")+"+08:00";

        DateTime dateTime = dt.minusHours(24);
        String startTime=dateTime.toString("yyyy-MM-dd'T'HH:mm:ss")+"+08:00";

        System.out.println(endTime);
        System.out.println(startTime);

    }



}
