import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.util.HttpUtils;
import com.shanghaigeography.Util.DateUtils;
import com.shcd.GHJGApplication;
import com.shcd.entity.*;
import com.shcd.service.*;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@SpringBootTest(classes = GHJGApplication.class)
public class test {
    @Resource
    private ShipLockDoorStatusRealtimeService shipLockDoorStatusRealtimeService;

    @Test
    public void test() {

    }


    @Autowired
    private ShipIllegalAutoService shipIllegalAutoService;
    @Autowired
    private ShipIllegalArtificialService shipIllegalArtificialService;

    @Test
    public void test2() {
        Date date = new Date();
//        String end = DateUtils.dateToString(date, "yyyy-MM-dd HH:mm");
//        String start = DateUtils.dateToString(new Date(date.getTime() - 5 * 60 * 1000), "yyyy-MM-dd HH:mm");
        String start = "2023-10-17 00:00";
        String end = "2023-10-17 23:59";
        HttpResponse execute = HttpUtil.createGet("http://222.66.154.74:8024/xunxiang/ship/open/get-illegal-info?start=" + start + "&end=" + end).
                header("secretId", "durr").
                header("secretKey", "WpEuxS+GyBzT0go10TGJeQjR5reA0LolEkmVzfmS87dU7H7LNP2xYsD1rmGgoEHx8ApuX8GaIPgZsUhOa8wJ+YkfkTsuMC74zMOVCyt5xOw=").execute();
        execute.close();
        HashMap hashMap = JSONObject.parseObject(execute.body(), HashMap.class);
        HashMap hashMap1 = JSONObject.parseObject(hashMap.get("data").toString(), HashMap.class);
        List<ShipIllegalAuto> auto = JSONObject.parseArray(hashMap1.get("auto").toString(), ShipIllegalAuto.class);
        List<ShipIllegalArtificial> artificial = JSONObject.parseArray(hashMap1.get("handle").toString(), ShipIllegalArtificial.class);
        if (!auto.isEmpty()) {
            auto.forEach(i->i.setIndext(i.getIndex()));
//            for (ShipIllegalAuto shipIllegalAuto : auto) {
//                shipIllegalAutoService.saveOrUpdate(shipIllegalAuto);
//            }
           shipIllegalAutoService.saveOrUpdateBatch(auto);
        }
        if (!artificial.isEmpty()) {
            shipIllegalArtificialService.saveOrUpdateBatch(artificial);
        }
    }


    @Autowired
    private ShipLockStatisticDetailService shipLockStatisticDetailService;
    @Autowired
    private ShipLockStatisticService shipLockStatisticService;

    @Test
    public void test3() {
        HttpResponse execute = HttpUtil.createGet("http://222.66.154.74:8024/xunxiang/ship/open/get-lock-statistic?date=2023-10-17").
                header("secretId", "durr").
                header("secretKey", "WpEuxS+GyBzT0go10TGJeQjR5reA0LolEkmVzfmS87dU7H7LNP2xYsD1rmGgoEHx8ApuX8GaIPgZsUhOa8wJ+YkfkTsuMC74zMOVCyt5xOw=").execute();
        execute.close();
        HashMap hashMap = JSONObject.parseObject(execute.body(), HashMap.class);
        HashMap data = JSONObject.parseObject(hashMap.get("data").toString(), HashMap.class);
        ShipLockStatistic shipLockShipStatus = JSONObject.parseObject(hashMap.get("data").toString(), ShipLockStatistic.class);
        List<ShipLockStatisticDetail> details = JSONObject.parseArray(data.get("details").toString(), ShipLockStatisticDetail.class);
        shipLockStatisticDetailService.saveOrUpdateBatch(details);
        shipLockStatisticService.saveOrUpdate(shipLockShipStatus);
//        for (ShipLockStatisticDetail detail : details) {
//            System.out.println(detail);
//        }
//        System.out.println(shipLockShipStatus);
    }

    private static final String[] status = {"预申报", "候泊中", "待闸中", "过闸中", "已过闸"};

    @Test
    public void test4() {
        for (String s : status) {
            HttpResponse execute = HttpUtil.createGet("http://222.66.154.74:8024/xunxiang/ship/open/get-ship-by-status?status=" + s).
                    header("secretId", "durr").
                    header("secretKey", "WpEuxS+GyBzT0go10TGJeQjR5reA0LolEkmVzfmS87dU7H7LNP2xYsD1rmGgoEHx8ApuX8GaIPgZsUhOa8wJ+YkfkTsuMC74zMOVCyt5xOw=").execute();
            execute.close();
            HashMap hashMap = JSONObject.parseObject(execute.body(), HashMap.class);
            List<ShipLockShipStatus> data = JSONObject.parseArray(hashMap.get("data").toString(), ShipLockShipStatus.class);
            for (ShipLockShipStatus datum : data) {
                System.out.println(datum);
            }
        }
    }

    @Autowired
    private ShipRunInfoService shipRunInfoService;
    @Autowired
    private ShipLockWaterLevelService shipLockWaterLevelService;

    @Test
    public void test5() {
        HttpResponse execute = HttpUtil.createGet("http://222.66.154.74:8024/xunxiang/ship/info/query/newData").
                header("secretId", "yangshuai").
                header("secretKey", "rlDR2kzLkZ5mpJbUscF7Si9EnlLJWcAc3uCnIiZdAvfm9haipGiCBZ0GuyFypkSWQ2hVgpTgAu0MdgLfLw64TkOvTmKkleci0rgE55wQuck=").execute();
        execute.close();
        HashMap hashMap = JSONObject.parseObject(execute.body(), HashMap.class);
        if (new Integer(0).equals(hashMap.get("code"))){
            System.out.println(123);
        }
        HashMap hashMap1 = JSONObject.parseObject(hashMap.get("data").toString(), HashMap.class);
        List<ShipRunInfo> shipRunInfos = JSONObject.parseArray(hashMap1.get("shipRunInfos").toString(), ShipRunInfo.class);
        shipRunInfos.forEach(i -> i.setDateTime(i.getDate()));
        shipRunInfoService.saveOrUpdateBatch(shipRunInfos);
        ShipLockWaterLevel data = JSONObject.parseObject(hashMap.get("data").toString(), ShipLockWaterLevel.class);
        data.setDateTime(data.getDate());
        shipLockWaterLevelService.saveOrUpdate(data);
        System.out.println(data);
    }

    @Test
    public void test6(){
        Date date = new Date();
        String end = DateUtils.dateToString(date, "yyyy-MM-dd HH:mm");
        String start = DateUtils.dateToString(new Date(date.getTime() - 5 * 60 * 1000), "yyyy-MM-dd HH:mm");
        System.out.println(end);
        System.out.println(start);
    }
}