package com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.cico.nest.lcdp.boot.core.qxsd.domain.vehicle.HistoryTunnelVehicle;
import com.cico.nest.lcdp.framework.mybatis.core.type.StringListTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 历史事件记录表
 * @author zws
 * @date 2024/8/27
 */

@TableName(value = "qxsd_event_his",autoResultMap = true)
@KeySequence("qxsd_event_his_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库，可不写。
@Data
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EventHisDO {

    /**
     * 主键
     */
    @TableId(type = IdType.ASSIGN_UUID)
    private String id;

    /**
     * 事件id
     */
    private String eventId;

    /**
     * 拓展对象 {@link EventHisExpandInfo}
     *
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    private JSONArray expandInfo;
    /**
     * 历史车辆轨迹
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    private JSONArray hisVehicleInfo;

    /**
     * 事件照片
     */
    @TableField(typeHandler = StringListTypeHandler.class)
    private List<String> eventImagesPath;

    /**
     * 卡口图片
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    private JSONArray bayonetImage;

    /**
     * 海康摄像头唯一编码
     */
    private String cameraIndexCode;
    /**
     * 视频路径
     */
    private String videoPath;

    /**
     * 创建时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @Data
    public static class EventHisExpandInfo{

        private String eventId;
        //事件-桩号
        private String eventMilestone;
        private String eventStartTime;

        //车牌号
        String carPlate;
        //车辆入隧时间
        String vehicleInTunnelTime;
        //车辆出隧时间
        String vehicleOutTunnelTime;
        //车牌图片
        String plateImage;
        //车头图片
        String headImage;
        //小侧面图像
        String smallSideImage;
        //设备-id
        private String deviceId;
        //设备-桩号
        private String deviceMilestone;

        //视频开始时间偏移量
        private int videoOffsetStart = -6;
        //视频结束时间偏移量
        private int videoOffsetEnd = 24;
        //视频时长
        private int videoTime;
        //视频耗时时长
        private String videoTimeConsuming;
        //视频Rtsp的url
        private String videoRtspUrl;
        //视频开始时间
        private String videoBeginTime;
        //视频结束时间
        private String videoEndTime;
    }

    public static EventHisExpandInfo getEventHisExpandInfo(JSONArray jsonArray,String carPlate){
        if(CollectionUtils.isEmpty(jsonArray)){
            EventHisExpandInfo x = new EventHisExpandInfo();
            x.setCarPlate(carPlate);
            return x;
        }
        Map<String,EventHisExpandInfo> map = new HashMap<>(jsonArray.size());
        jsonArray.forEach(o -> {
            EventHisExpandInfo x = JSONObject.parseObject(JSONObject.toJSONString(o),EventHisExpandInfo.class);
            map.put(x.getCarPlate(),x);
        });
        if(!map.containsKey(carPlate)){
            EventHisExpandInfo x = new EventHisExpandInfo();
            x.setCarPlate(carPlate);
            map.put(carPlate,x);
        }
        return map.get(carPlate);
    }
    public static JSONArray setEventHisExpandInfo(JSONArray jsonArray ,EventHisExpandInfo eventHisExpandInfo){
        if(CollectionUtils.isEmpty(jsonArray)){
            jsonArray = new JSONArray();
            jsonArray.add(eventHisExpandInfo);
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            if(jsonArray.getJSONObject(i).getString("carPlate").equals(eventHisExpandInfo.getCarPlate())){
                jsonArray.set(i, eventHisExpandInfo);
                break;
            }
        }
        return jsonArray;
    }
}
