package cn.com.oa.controller;

import cn.com.oa.model.CarInoutRecord;
import cn.com.oa.model.dto.BarrierCameraAlarmDTO;
import cn.com.oa.service.CarInoutRecordService;
import cn.com.v2.common.domain.AjaxResult;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取天气
 *
 * @author: wangpx
 * @date: 2025-02-12 10:40
 */
@RestController
@RequestMapping("/api/common")
public class CommonController {

    @Autowired
    private CarInoutRecordService carInoutRecordService;

    @GetMapping("/weather")
    public AjaxResult getWeather(@RequestParam(required = false) String latitude, @RequestParam(required = false) String longitude) {
        if (latitude == null) {
            latitude = "25.9645";
        }
        if (longitude == null) {
            longitude = "119.2197";
        }
        HttpResponse response = HttpRequest.get("https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/" + latitude + "%2C" + longitude + "?unitGroup=metric&elements=datetime%2Ctemp%2Cpreciptype&include=hours&key=PA6TZ5RK5NS48644CHCKX6FGK&contentType=json").execute();
        Map<String, String> map = new HashMap<>();
        map.put("date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        map.put("data", response.body());
        return AjaxResult.successData(200, map).put("msg","获取成功");
    }

    @GetMapping("/date")
    public AjaxResult getDate() {
        return AjaxResult.successData(200, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).put("msg","获取成功");
    }

    @PostMapping("/carin")
    public AjaxResult carin(@RequestBody BarrierCameraAlarmDTO dto) throws JsonProcessingException {
        CarInoutRecord record = new CarInoutRecord();
        record.setDeviceIp(dto.getIpaddr());
        record.setSerialNo(dto.getSerialno());
        record.setLicensePlate(dto.getLicense());
        record.setPlateConfidence(dto.getConfidence());
        record.setRealPlate(dto.getRealplate());
        record.setPlateColorType(dto.getColorType());
        record.setVehicleId(dto.getVehicleId());
        record.setWhitelistType(dto.getWhitelist());
        record.setTimeStamp(dto.getTimeStamp());
        record.setDirection(1);
        if (carInoutRecordService.save(record)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @PostMapping("/carout")
    public AjaxResult carout(@RequestBody BarrierCameraAlarmDTO dto) throws JsonProcessingException {
        CarInoutRecord record = new CarInoutRecord();
        record.setDeviceIp(dto.getIpaddr());
        record.setSerialNo(dto.getSerialno());
        record.setLicensePlate(dto.getLicense());
        record.setPlateConfidence(dto.getConfidence());
        record.setRealPlate(dto.getRealplate());
        record.setPlateColorType(dto.getColorType());
        record.setVehicleId(dto.getVehicleId());
        record.setWhitelistType(dto.getWhitelist());
        record.setTimeStamp(dto.getTimeStamp());
        record.setDirection(2);
        if (carInoutRecordService.save(record)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @GetMapping("/carinrecords")
    public AjaxResult getCarInRecords() {
        CarInoutRecord record = new CarInoutRecord();
        record.setDirection(1);
        Map<LocalDateTime, Map<Integer, List<CarInoutRecord>>> map = new HashMap<>();
        for (int i = 0; i < 7; i++) {
            record.setTimeStamp(LocalDateTime.now().minusDays(i));
            map.put(record.getTimeStamp(), carInoutRecordService.getCarInRecordsByDateGroupByWhiteList(record));
        }
        return AjaxResult.successData(200, map);
    }

}
