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.EnableScheduling;
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;

@EnableScheduling
@Component
public class Task {

    @Resource
    private RecordDataMapper recordDataMapper;

    /**
     *定时任务（定时取海康威视的进场数据）
     */
    @Scheduled(cron = "0 55 23 * * ? ")
    //@Scheduled(cron = "0 0/2 * * * ? ")
    public void parkingLotData() {
        System.out.println("定时任务");
        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 outParkSyscode = "b6f0ed8118284a21a30e68fe795545f7";
        String outEntranceSyscode = "d24901cb065e4acabb66777aaa2654db";
        List<Map<String,Object>> outListAll = new ArrayList<>();
        for (int i = 1; i > 0; i++) {
            int pageNo = 1;
            String outfieldData = timingURL.timingURL(outParkSyscode,outEntranceSyscode,startTime,endTime,pageNo);
            JSONObject jsonObject = JSONObject.parseObject(outfieldData);
            String data = jsonObject.getString("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 infieldParkSyscode = "fa12398af6a546e897e240b7bbe0de00";
        String infieldEntranceSyscode = "9e816223c86548c4a15c431aba8efc07";
        List<Map<String,Object>> infieldListAll = new ArrayList<>();
        for (int i = 1; i > 0; i++) {
            int pageNo = 1;
            String infieldfieldData = timingURL.timingURL(infieldParkSyscode,infieldEntranceSyscode,startTime,endTime,pageNo);
            JSONObject jsonObject = JSONObject.parseObject(infieldfieldData);
            String data = jsonObject.getString("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;
        }
    }




}
